xref: /openbsd-src/sys/netinet6/mld6.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: mld6.c,v 1.25 2008/06/11 19:00:50 mcbride Exp $	*/
2 /*	$KAME: mld6.c,v 1.26 2001/02/16 14:50:35 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) 1988 Stephen Deering.
35  * Copyright (c) 1992, 1993
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * This code is derived from software contributed to Berkeley by
39  * Stephen Deering of Stanford University.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. Neither the name of the University nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63  * SUCH DAMAGE.
64  *
65  *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
66  */
67 
68 #include <sys/param.h>
69 #include <sys/systm.h>
70 #include <sys/mbuf.h>
71 #include <sys/socket.h>
72 #include <sys/protosw.h>
73 #include <sys/syslog.h>
74 #include <dev/rndvar.h>
75 
76 #include <net/if.h>
77 
78 #include <netinet/in.h>
79 #include <netinet/in_var.h>
80 #include <netinet/ip6.h>
81 #include <netinet6/ip6_var.h>
82 #include <netinet/icmp6.h>
83 #include <netinet6/mld6_var.h>
84 
85 /*
86  * Protocol constants
87  */
88 
89 /* denotes that the MLD max response delay field specifies time in milliseconds */
90 #define MLD_TIMER_SCALE	1000
91 /*
92  * time between repetitions of a node's initial report of interest in a
93  * multicast address(in seconds)
94  */
95 #define MLD_UNSOLICITED_REPORT_INTERVAL	10
96 
97 static struct ip6_pktopts ip6_opts;
98 static int mld_timers_are_running;
99 /* XXX: These are necessary for KAME's link-local hack */
100 static struct in6_addr mld_all_nodes_linklocal = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
101 static struct in6_addr mld_all_routers_linklocal = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
102 
103 static void mld6_sendpkt(struct in6_multi *, int, const struct in6_addr *);
104 
105 void
106 mld6_init()
107 {
108 	static u_int8_t hbh_buf[8];
109 	struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
110 	u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
111 
112 	mld_timers_are_running = 0;
113 
114 	/* ip6h_nxt will be fill in later */
115 	hbh->ip6h_len = 0;	/* (8 >> 3) - 1 */
116 
117 	/* XXX: grotty hard coding... */
118 	hbh_buf[2] = IP6OPT_PADN;	/* 2 byte padding */
119 	hbh_buf[3] = 0;
120 	hbh_buf[4] = IP6OPT_ROUTER_ALERT;
121 	hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
122 	bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
123 
124 	ip6_opts.ip6po_hbh = hbh;
125 }
126 
127 void
128 mld6_start_listening(struct in6_multi *in6m)
129 {
130 	int s = splsoftnet();
131 
132 	/*
133 	 * RFC2710 page 10:
134 	 * The node never sends a Report or Done for the link-scope all-nodes
135 	 * address.
136 	 * MLD messages are never sent for multicast addresses whose scope is 0
137 	 * (reserved) or 1 (node-local).
138 	 */
139 	mld_all_nodes_linklocal.s6_addr16[1] =
140 	    htons(in6m->in6m_ifp->if_index); /* XXX */
141 	if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld_all_nodes_linklocal) ||
142 	    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
143 		in6m->in6m_timer = 0;
144 		in6m->in6m_state = MLD_OTHERLISTENER;
145 	} else {
146 		mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
147 		in6m->in6m_timer =
148 		    MLD_RANDOM_DELAY(MLD_UNSOLICITED_REPORT_INTERVAL *
149 		    PR_FASTHZ);
150 		in6m->in6m_state = MLD_IREPORTEDLAST;
151 		mld_timers_are_running = 1;
152 	}
153 	splx(s);
154 }
155 
156 void
157 mld6_stop_listening(struct in6_multi *in6m)
158 {
159 	mld_all_nodes_linklocal.s6_addr16[1] =
160 	    htons(in6m->in6m_ifp->if_index); /* XXX */
161 	mld_all_routers_linklocal.s6_addr16[1] =
162 	    htons(in6m->in6m_ifp->if_index); /* XXX: necessary when mrouting */
163 
164 	if (in6m->in6m_state == MLD_IREPORTEDLAST &&
165 	    (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld_all_nodes_linklocal)) &&
166 	    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > IPV6_ADDR_SCOPE_INTFACELOCAL)
167 		mld6_sendpkt(in6m, MLD_LISTENER_DONE,
168 		    &mld_all_routers_linklocal);
169 }
170 
171 void
172 mld6_input(struct mbuf *m, int off)
173 {
174 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
175 	struct mld_hdr *mldh;
176 	struct ifnet *ifp = m->m_pkthdr.rcvif;
177 	struct in6_multi *in6m;
178 	struct in6_ifaddr *ia;
179 	int timer;		/* timer value in the MLD query header */
180 
181 	IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
182 	if (mldh == NULL) {
183 		icmp6stat.icp6s_tooshort++;
184 		return;
185 	}
186 
187 	/* source address validation */
188 	ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
189 	if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
190 #if 0
191 		log(LOG_ERR,
192 		    "mld_input: src %s is not link-local (grp=%s)\n",
193 		    ip6_sprintf(&ip6->ip6_src),
194 		    ip6_sprintf(&mldh->mld_addr));
195 #endif
196 		/*
197 		 * spec (RFC2710) does not explicitly
198 		 * specify to discard the packet from a non link-local
199 		 * source address. But we believe it's expected to do so.
200 		 */
201 		m_freem(m);
202 		return;
203 	}
204 
205 	/*
206 	 * In the MLD6 specification, there are 3 states and a flag.
207 	 *
208 	 * In Non-Listener state, we simply don't have a membership record.
209 	 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
210 	 * In Idle Listener state, our timer is not running (in6m->in6m_timer==0)
211 	 *
212 	 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
213 	 * we have heard a report from another member, or MLD_IREPORTEDLAST
214 	 * if we sent the last report.
215 	 */
216 	switch(mldh->mld_type) {
217 	case MLD_LISTENER_QUERY:
218 		if (ifp->if_flags & IFF_LOOPBACK)
219 			break;
220 
221 		if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) &&
222 		    !IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
223 			break;	/* print error or log stat? */
224 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
225 			mldh->mld_addr.s6_addr16[1] =
226 			    htons(ifp->if_index); /* XXX */
227 
228 		/*
229 		 * - Start the timers in all of our membership records
230 		 *   that the query applies to for the interface on
231 		 *   which the query arrived excl. those that belong
232 		 *   to the "all-nodes" group (ff02::1).
233 		 * - Restart any timer that is already running but has
234 		 *   A value longer than the requested timeout.
235 		 * - Use the value specified in the query message as
236 		 *   the maximum timeout.
237 		 */
238 		IFP_TO_IA6(ifp, ia);
239 		if (ia == NULL)
240 			break;
241 
242 		/*
243 		 * XXX: System timer resolution is too low to handle Max
244 		 * Response Delay, so set 1 to the internal timer even if
245 		 * the calculated value equals to zero when Max Response
246 		 * Delay is positive.
247 		 */
248 		timer = ntohs(mldh->mld_maxdelay)*PR_FASTHZ/MLD_TIMER_SCALE;
249 		if (timer == 0 && mldh->mld_maxdelay)
250 			timer = 1;
251 		mld_all_nodes_linklocal.s6_addr16[1] =
252 			htons(ifp->if_index); /* XXX */
253 
254 		LIST_FOREACH(in6m, &ia->ia6_multiaddrs, in6m_entry) {
255 			if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr,
256 						&mld_all_nodes_linklocal) ||
257 			    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
258 			    IPV6_ADDR_SCOPE_LINKLOCAL)
259 				continue;
260 
261 			if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) ||
262 			    IN6_ARE_ADDR_EQUAL(&mldh->mld_addr,
263 						&in6m->in6m_addr))
264 			{
265 				if (timer == 0) {
266 					/* send a report immediately */
267 					mld6_sendpkt(in6m, MLD_LISTENER_REPORT,
268 					    NULL);
269 					in6m->in6m_timer = 0; /* reset timer */
270 					in6m->in6m_state = MLD_IREPORTEDLAST;
271 				} else if (in6m->in6m_timer == 0 || /* idle */
272 					in6m->in6m_timer > timer) {
273 					in6m->in6m_timer =
274 					    MLD_RANDOM_DELAY(timer);
275 					mld_timers_are_running = 1;
276 				}
277 			}
278 		}
279 
280 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
281 			mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
282 		break;
283 	case MLD_LISTENER_REPORT:
284 		/*
285 		 * For fast leave to work, we have to know that we are the
286 		 * last person to send a report for this group.  Reports
287 		 * can potentially get looped back if we are a multicast
288 		 * router, so discard reports sourced by me.
289 		 * Note that it is impossible to check IFF_LOOPBACK flag of
290 		 * ifp for this purpose, since ip6_mloopback pass the physical
291 		 * interface to looutput.
292 		 */
293 		if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
294 			break;
295 
296 		if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
297 			break;
298 
299 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
300 			mldh->mld_addr.s6_addr16[1] =
301 				htons(ifp->if_index); /* XXX */
302 		/*
303 		 * If we belong to the group being reported, stop
304 		 * our timer for that group.
305 		 */
306 		IN6_LOOKUP_MULTI(mldh->mld_addr, ifp, in6m);
307 		if (in6m) {
308 			in6m->in6m_timer = 0; /* transit to idle state */
309 			in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
310 		}
311 
312 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
313 			mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
314 		break;
315 	default:		/* this is impossible */
316 #if 0
317 		/*
318 		 * this case should be impossible because of filtering in
319 		 * icmp6_input().  But we explicitly disabled this part
320 		 * just in case.
321 		 */
322 		log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type);
323 #endif
324 		break;
325 	}
326 
327 	m_freem(m);
328 }
329 
330 void
331 mld6_fasttimeo()
332 {
333 	struct in6_multi *in6m;
334 	struct in6_multistep step;
335 	int s;
336 
337 	/*
338 	 * Quick check to see if any work needs to be done, in order
339 	 * to minimize the overhead of fasttimo processing.
340 	 */
341 	if (!mld_timers_are_running)
342 		return;
343 
344 	s = splsoftnet();
345 	mld_timers_are_running = 0;
346 	IN6_FIRST_MULTI(step, in6m);
347 	while (in6m != NULL) {
348 		if (in6m->in6m_timer == 0) {
349 			/* do nothing */
350 		} else if (--in6m->in6m_timer == 0) {
351 			mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
352 			in6m->in6m_state = MLD_IREPORTEDLAST;
353 		} else {
354 			mld_timers_are_running = 1;
355 		}
356 		IN6_NEXT_MULTI(step, in6m);
357 	}
358 	splx(s);
359 }
360 
361 static void
362 mld6_sendpkt(struct in6_multi *in6m, int type, const struct in6_addr *dst)
363 {
364 	struct mbuf *mh, *md;
365 	struct mld_hdr *mldh;
366 	struct ip6_hdr *ip6;
367 	struct ip6_moptions im6o;
368 	struct in6_ifaddr *ia;
369 	struct ifnet *ifp = in6m->in6m_ifp;
370 	int ignflags;
371 
372 	/*
373 	 * At first, find a link local address on the outgoing interface
374 	 * to use as the source address of the MLD packet.
375 	 * We do not reject tentative addresses for MLD report to deal with
376 	 * the case where we first join a link-local address.
377 	 */
378 	ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
379 	if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
380 		return;
381 	if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
382 		ia = NULL;
383 
384 	/*
385 	 * Allocate mbufs to store ip6 header and MLD header.
386 	 * We allocate 2 mbufs and make chain in advance because
387 	 * it is more convenient when inserting the hop-by-hop option later.
388 	 */
389 	MGETHDR(mh, M_DONTWAIT, MT_HEADER);
390 	if (mh == NULL)
391 		return;
392 	MGET(md, M_DONTWAIT, MT_DATA);
393 	if (md == NULL) {
394 		m_free(mh);
395 		return;
396 	}
397 	mh->m_next = md;
398 
399 	mh->m_pkthdr.rcvif = NULL;
400 	mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
401 	mh->m_len = sizeof(struct ip6_hdr);
402 	MH_ALIGN(mh, sizeof(struct ip6_hdr));
403 
404 	/* fill in the ip6 header */
405 	ip6 = mtod(mh, struct ip6_hdr *);
406 	ip6->ip6_flow = 0;
407 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
408 	ip6->ip6_vfc |= IPV6_VERSION;
409 	/* ip6_plen will be set later */
410 	ip6->ip6_nxt = IPPROTO_ICMPV6;
411 	/* ip6_hlim will be set by im6o.im6o_multicast_hlim */
412 	ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
413 	ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
414 
415 	/* fill in the MLD header */
416 	md->m_len = sizeof(struct mld_hdr);
417 	mldh = mtod(md, struct mld_hdr *);
418 	mldh->mld_type = type;
419 	mldh->mld_code = 0;
420 	mldh->mld_cksum = 0;
421 	/* XXX: we assume the function will not be called for query messages */
422 	mldh->mld_maxdelay = 0;
423 	mldh->mld_reserved = 0;
424 	mldh->mld_addr = in6m->in6m_addr;
425 	if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
426 		mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
427 	mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
428 	    sizeof(struct mld_hdr));
429 
430 	/* construct multicast option */
431 	bzero(&im6o, sizeof(im6o));
432 	im6o.im6o_multicast_ifp = ifp;
433 	im6o.im6o_multicast_hlim = 1;
434 
435 	/*
436 	 * Request loopback of the report if we are acting as a multicast
437 	 * router, so that the process-level routing daemon can hear it.
438 	 */
439 #ifdef MROUTING
440 	im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
441 #endif
442 
443 	/* increment output statictics */
444 	icmp6stat.icp6s_outhist[type]++;
445 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
446 	switch (type) {
447 	case MLD_LISTENER_QUERY:
448 		icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
449 		break;
450 	case MLD_LISTENER_REPORT:
451 		icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
452 		break;
453 	case MLD_LISTENER_DONE:
454 		icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
455 		break;
456 	}
457 
458 	ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC, &im6o, NULL,
459 	    NULL);
460 }
461