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