mld6.c revision 1.29 1 /* $NetBSD: mld6.c,v 1.29 2006/01/21 00:15:36 rpaulo 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.29 2006/01/21 00:15:36 rpaulo Exp $");
106
107 #include "opt_inet.h"
108
109 #include <sys/param.h>
110 #include <sys/systm.h>
111 #include <sys/mbuf.h>
112 #include <sys/socket.h>
113 #include <sys/protosw.h>
114 #include <sys/syslog.h>
115
116 #include <net/if.h>
117
118 #include <netinet/in.h>
119 #include <netinet/in_var.h>
120 #include <netinet/ip6.h>
121 #include <netinet6/ip6_var.h>
122 #include <netinet6/scope6_var.h>
123 #include <netinet/icmp6.h>
124 #include <netinet6/mld6_var.h>
125
126 #include <net/net_osdep.h>
127
128 /*
129 * Protocol constants
130 */
131
132 /* denotes that the MLD max response delay field specifies time in milliseconds */
133 #define MLD_TIMER_SCALE 1000
134 /*
135 * time between repetitions of a node's initial report of interest in a
136 * multicast address(in seconds)
137 */
138 #define MLD_UNSOLICITED_REPORT_INTERVAL 10
139
140 static struct ip6_pktopts ip6_opts;
141 static int mld_timers_are_running;
142
143 static void mld6_sendpkt __P((struct in6_multi *, int, const struct in6_addr *));
144
145 void
146 mld6_init()
147 {
148 static u_int8_t hbh_buf[8];
149 struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
150 u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
151
152 mld_timers_are_running = 0;
153
154 /* ip6h_nxt will be fill in later */
155 hbh->ip6h_len = 0; /* (8 >> 3) - 1 */
156
157 /* XXX: grotty hard coding... */
158 hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */
159 hbh_buf[3] = 0;
160 hbh_buf[4] = IP6OPT_RTALERT;
161 hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
162 bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
163
164 ip6_opts.ip6po_hbh = hbh;
165 /* We will specify the hoplimit by a multicast option. */
166 ip6_opts.ip6po_hlim = -1;
167 }
168
169 void
170 mld6_start_listening(in6m)
171 struct in6_multi *in6m;
172 {
173 struct in6_addr all_in6;
174
175 /*
176 * RFC2710 page 10:
177 * The node never sends a Report or Done for the link-scope all-nodes
178 * address.
179 * MLD messages are never sent for multicast addresses whose scope is 0
180 * (reserved) or 1 (node-local).
181 */
182 all_in6 = in6addr_linklocal_allnodes;
183 if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) {
184 /* XXX: this should not happen! */
185 in6m->in6m_timer = 0;
186 in6m->in6m_state = MLD_OTHERLISTENER;
187 }
188 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
189 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
190 in6m->in6m_timer = 0;
191 in6m->in6m_state = MLD_OTHERLISTENER;
192 } else {
193 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
194 in6m->in6m_timer =
195 MLD_RANDOM_DELAY(MLD_UNSOLICITED_REPORT_INTERVAL *
196 PR_FASTHZ);
197 in6m->in6m_state = MLD_IREPORTEDLAST;
198 mld_timers_are_running = 1;
199 }
200 }
201
202 void
203 mld6_stop_listening(in6m)
204 struct in6_multi *in6m;
205 {
206 struct in6_addr allnode, allrouter;
207
208 allnode = in6addr_linklocal_allnodes;
209 if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) {
210 /* XXX: this should not happen! */
211 return;
212 }
213 allrouter = in6addr_linklocal_allrouters;
214 if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) {
215 /* XXX impossible */
216 return;
217 }
218
219 if (in6m->in6m_state == MLD_IREPORTEDLAST &&
220 (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode)) &&
221 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
222 IPV6_ADDR_SCOPE_INTFACELOCAL) {
223 mld6_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
224 }
225 }
226
227 void
228 mld6_input(m, off)
229 struct mbuf *m;
230 int off;
231 {
232 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
233 struct mld_hdr *mldh;
234 struct ifnet *ifp = m->m_pkthdr.rcvif;
235 struct in6_multi *in6m;
236 struct in6_addr mld_addr, all_in6;
237 struct in6_ifaddr *ia;
238 int timer; /* timer value in the MLD query header */
239
240 IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
241 if (mldh == NULL) {
242 icmp6stat.icp6s_tooshort++;
243 return;
244 }
245
246 /* source address validation */
247 ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
248 if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
249 #if 0
250 log(LOG_ERR,
251 "mld_input: src %s is not link-local (grp=%s)\n",
252 ip6_sprintf(&ip6->ip6_src),
253 ip6_sprintf(&mldh->mld_addr));
254 #endif
255 /*
256 * spec (RFC2710) does not explicitly
257 * specify to discard the packet from a non link-local
258 * source address. But we believe it's expected to do so.
259 * XXX: do we have to allow :: as source?
260 */
261 m_freem(m);
262 return;
263 }
264
265 /*
266 * make a copy for local work (in6_setscope() may modify the 1st arg)
267 */
268 mld_addr = mldh->mld_addr;
269 if (in6_setscope(&mld_addr, ifp, NULL)) {
270 /* XXX: this should not happen! */
271 m_free(m);
272 return;
273 }
274
275 /*
276 * In the MLD6 specification, there are 3 states and a flag.
277 *
278 * In Non-Listener state, we simply don't have a membership record.
279 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
280 * In Idle Listener state, our timer is not running (in6m->in6m_timer==0)
281 *
282 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
283 * we have heard a report from another member, or MLD_IREPORTEDLAST
284 * if we sent the last report.
285 */
286 switch (mldh->mld_type) {
287 case MLD_LISTENER_QUERY:
288 if (ifp->if_flags & IFF_LOOPBACK)
289 break;
290
291 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
292 !IN6_IS_ADDR_MULTICAST(&mld_addr))
293 break; /* print error or log stat? */
294
295 all_in6 = in6addr_linklocal_allnodes;
296 if (in6_setscope(&all_in6, ifp, NULL)) {
297 /* XXX: this should not happen! */
298 break;
299 }
300
301 /*
302 * - Start the timers in all of our membership records
303 * that the query applies to for the interface on
304 * which the query arrived excl. those that belong
305 * to the "all-nodes" group (ff02::1).
306 * - Restart any timer that is already running but has
307 * A value longer than the requested timeout.
308 * - Use the value specified in the query message as
309 * the maximum timeout.
310 */
311 IFP_TO_IA6(ifp, ia);
312 if (ia == NULL)
313 break;
314
315 /*
316 * XXX: System timer resolution is too low to handle Max
317 * Response Delay, so set 1 to the internal timer even if
318 * the calculated value equals to zero when Max Response
319 * Delay is positive.
320 */
321 timer = ntohs(mldh->mld_maxdelay)*PR_FASTHZ/MLD_TIMER_SCALE;
322 if (timer == 0 && mldh->mld_maxdelay)
323 timer = 1;
324
325 for (in6m = ia->ia6_multiaddrs.lh_first;
326 in6m;
327 in6m = in6m->in6m_entry.le_next)
328 {
329 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
330 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
331 IPV6_ADDR_SCOPE_LINKLOCAL)
332 continue;
333
334 if (IN6_IS_ADDR_UNSPECIFIED(&mld_addr) ||
335 IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr)) {
336 if (timer == 0) {
337 /* send a report immediately */
338 mld6_sendpkt(in6m, MLD_LISTENER_REPORT,
339 NULL);
340 in6m->in6m_timer = 0; /* reset timer */
341 in6m->in6m_state = MLD_IREPORTEDLAST;
342 }
343 else if (in6m->in6m_timer == 0 || /*idle state*/
344 in6m->in6m_timer > timer) {
345 in6m->in6m_timer =
346 MLD_RANDOM_DELAY(timer);
347 mld_timers_are_running = 1;
348 }
349 }
350 }
351 break;
352
353 case MLD_LISTENER_REPORT:
354 /*
355 * For fast leave to work, we have to know that we are the
356 * last person to send a report for this group. Reports
357 * can potentially get looped back if we are a multicast
358 * router, so discard reports sourced by me.
359 * Note that it is impossible to check IFF_LOOPBACK flag of
360 * ifp for this purpose, since ip6_mloopback pass the physical
361 * interface to looutput.
362 */
363 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
364 break;
365
366 if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
367 break;
368
369 /*
370 * If we belong to the group being reported, stop
371 * our timer for that group.
372 */
373 IN6_LOOKUP_MULTI(mld_addr, ifp, in6m);
374 if (in6m) {
375 in6m->in6m_timer = 0; /* transit to idle state */
376 in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
377 }
378 break;
379 default: /* this is impossible */
380 #if 0
381 /*
382 * this case should be impossible because of filtering in
383 * icmp6_input(). But we explicitly disabled this part
384 * just in case.
385 */
386 log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld6_type);
387 #endif
388 break;
389 }
390
391 m_freem(m);
392 }
393
394 void
395 mld6_fasttimeo()
396 {
397 struct in6_multi *in6m;
398 struct in6_multistep step;
399 int s;
400 /*
401 * Quick check to see if any work needs to be done, in order
402 * to minimize the overhead of fasttimo processing.
403 */
404 if (!mld_timers_are_running)
405 return;
406
407 s = splsoftnet();
408 mld_timers_are_running = 0;
409 IN6_FIRST_MULTI(step, in6m);
410 while (in6m != NULL) {
411 if (in6m->in6m_timer == 0) {
412 /* do nothing */
413 } else if (--in6m->in6m_timer == 0) {
414 mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
415 in6m->in6m_state = MLD_IREPORTEDLAST;
416 } else {
417 mld_timers_are_running = 1;
418 }
419 IN6_NEXT_MULTI(step, in6m);
420 }
421 splx(s);
422 }
423
424 static void
425 mld6_sendpkt(in6m, type, dst)
426 struct in6_multi *in6m;
427 int type;
428 const struct in6_addr *dst;
429 {
430 struct mbuf *mh, *md;
431 struct mld_hdr *mldh;
432 struct ip6_hdr *ip6;
433 struct ip6_moptions im6o;
434 struct in6_ifaddr *ia;
435 struct ifnet *ifp = in6m->in6m_ifp;
436 int ignflags;
437
438 /*
439 * At first, find a link local address on the outgoing interface
440 * to use as the source address of the MLD packet.
441 * We do not reject tentative addresses for MLD report to deal with
442 * the case where we first join a link-local address.
443 */
444 ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
445 if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
446 return;
447 if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
448 ia = NULL;
449
450 /*
451 * Allocate mbufs to store ip6 header and MLD header.
452 * We allocate 2 mbufs and make chain in advance because
453 * it is more convenient when inserting the hop-by-hop option later.
454 */
455 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
456 if (mh == NULL)
457 return;
458 MGET(md, M_DONTWAIT, MT_DATA);
459 if (md == NULL) {
460 m_free(mh);
461 return;
462 }
463 mh->m_next = md;
464
465 mh->m_pkthdr.rcvif = NULL;
466 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
467 mh->m_len = sizeof(struct ip6_hdr);
468 MH_ALIGN(mh, sizeof(struct ip6_hdr));
469
470 /* fill in the ip6 header */
471 ip6 = mtod(mh, struct ip6_hdr *);
472 ip6->ip6_flow = 0;
473 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
474 ip6->ip6_vfc |= IPV6_VERSION;
475 /* ip6_plen will be set later */
476 ip6->ip6_nxt = IPPROTO_ICMPV6;
477 /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
478 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
479 ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
480
481 /* fill in the MLD header */
482 md->m_len = sizeof(struct mld_hdr);
483 mldh = mtod(md, struct mld_hdr *);
484 mldh->mld_type = type;
485 mldh->mld_code = 0;
486 mldh->mld_cksum = 0;
487 /* XXX: we assume the function will not be called for query messages */
488 mldh->mld_maxdelay = 0;
489 mldh->mld_reserved = 0;
490 mldh->mld_addr = in6m->in6m_addr;
491 in6_clearscope(&mldh->mld_addr); /* XXX */
492 mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
493 sizeof(struct mld_hdr));
494
495 /* construct multicast option */
496 bzero(&im6o, sizeof(im6o));
497 im6o.im6o_multicast_ifp = ifp;
498 im6o.im6o_multicast_hlim = 1;
499
500 /*
501 * Request loopback of the report if we are acting as a multicast
502 * router, so that the process-level routing daemon can hear it.
503 */
504 im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
505
506 /* increment output statictics */
507 icmp6stat.icp6s_outhist[type]++;
508 icmp6_ifstat_inc(ifp, ifs6_out_msg);
509 switch (type) {
510 case MLD_LISTENER_QUERY:
511 icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
512 break;
513 case MLD_LISTENER_REPORT:
514 icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
515 break;
516 case MLD_LISTENER_DONE:
517 icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
518 break;
519 }
520
521 ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
522 &im6o, (struct socket *)NULL, NULL);
523 }
524