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