mld6.c revision 1.2.2.2 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 #endif
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/protosw.h>
78 #include <sys/syslog.h>
79
80 #include <net/if.h>
81
82 #include <netinet/in.h>
83 #include <netinet/in_var.h>
84 #include <netinet6/in6.h>
85 #include <netinet6/ip6.h>
86 #include <netinet6/ip6_var.h>
87 #include <netinet6/icmp6.h>
88 #include <netinet6/mld6_var.h>
89
90 /*
91 * Protocol constants
92 */
93
94 /* denotes that the MLD max response delay field specifies time in milliseconds */
95 #define MLD6_TIMER_SCALE 1000
96 /*
97 * time between repetitions of a node's initial report of interest in a
98 * multicast address(in seconds)
99 */
100 #define MLD6_UNSOLICITED_REPORT_INTERVAL 10
101
102 static struct ip6_pktopts ip6_opts;
103 static int mld6_timers_are_running;
104 /* XXX: These are necessary for KAME's link-local hack */
105 static struct in6_addr mld6_all_nodes_linklocal = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
106 static struct in6_addr mld6_all_routers_linklocal = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
107
108 static void mld6_sendpkt __P((struct in6_multi *, int, const struct in6_addr *));
109
110 void
111 mld6_init()
112 {
113 static u_int8_t hbh_buf[8];
114 struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
115 u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
116
117 mld6_timers_are_running = 0;
118
119 /* ip6h_nxt will be fill in later */
120 hbh->ip6h_len = 0; /* (8 >> 3) - 1*/
121
122 /* XXX: grotty hard coding... */
123 hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */
124 hbh_buf[3] = 0;
125 hbh_buf[4] = IP6OPT_RTALERT;
126 hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
127 bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
128
129 ip6_opts.ip6po_hbh = hbh;
130 /* We will specify the hoplimit by a multicast option. */
131 ip6_opts.ip6po_hlim = -1;
132 }
133
134 void
135 mld6_start_listening(in6m)
136 struct in6_multi *in6m;
137 {
138 int s = splnet();
139
140 /*
141 * (draft-ietf-ipngwg-mld, page 10)
142 * The node never sends a Report or Done for the link-scope all-nodes
143 * address.
144 * MLD messages are never sent for multicast addresses whose scope is 0
145 * (reserved) or 1 (node-local).
146 */
147 mld6_all_nodes_linklocal.s6_addr16[1] =
148 htons(in6m->in6m_ifp->if_index); /* XXX */
149 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld6_all_nodes_linklocal) ||
150 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
151 in6m->in6m_timer = 0;
152 in6m->in6m_state = MLD6_OTHERLISTENER;
153 } else {
154 mld6_sendpkt(in6m, MLD6_LISTENER_REPORT, NULL);
155 in6m->in6m_timer = MLD6_RANDOM_DELAY(
156 MLD6_UNSOLICITED_REPORT_INTERVAL * PR_FASTHZ);
157 in6m->in6m_state = MLD6_IREPORTEDLAST;
158 mld6_timers_are_running = 1;
159 }
160 splx(s);
161 }
162
163 void
164 mld6_stop_listening(in6m)
165 struct in6_multi *in6m;
166 {
167 mld6_all_nodes_linklocal.s6_addr16[1] =
168 htons(in6m->in6m_ifp->if_index); /* XXX */
169 mld6_all_routers_linklocal.s6_addr16[1] =
170 htons(in6m->in6m_ifp->if_index); /* XXX: necessary when mrouting */
171
172 if (in6m->in6m_state == MLD6_IREPORTEDLAST &&
173 (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld6_all_nodes_linklocal)) &&
174 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > IPV6_ADDR_SCOPE_NODELOCAL)
175 mld6_sendpkt(in6m, MLD6_LISTENER_DONE,
176 &mld6_all_routers_linklocal);
177 }
178
179 void
180 mld6_input(m, off)
181 struct mbuf *m;
182 int off;
183 {
184 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
185 struct mld6_hdr *mldh = (struct mld6_hdr *)(mtod(m, caddr_t) + off);
186 struct ifnet *ifp = m->m_pkthdr.rcvif;
187 struct in6_multi *in6m;
188 struct in6_ifaddr *ia;
189 int timer; /* timer value in the MLD query header */
190
191 /* source address validation */
192 if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
193 log(LOG_ERR,
194 "mld6_input: src %s is not link-local\n",
195 ip6_sprintf(&ip6->ip6_src));
196 /*
197 * spec(draft-ietf-ipngwg-mld) 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 return;
202 }
203
204 /*
205 * In the MLD6 specification, there are 3 states and a flag.
206 *
207 * In Non-Listener state, we simply don't have a membership record.
208 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
209 * In Idle Listener state, our timer is not running (in6m->in6m_timer==0)
210 *
211 * The flag is in6m->in6m_state, it is set to MLD6_OTHERLISTENER if
212 * we have heard a report from another member, or MLD6_IREPORTEDLAST
213 * if we sent the last report.
214 */
215 switch(mldh->mld6_type) {
216 case MLD6_LISTENER_QUERY:
217 if (ifp->if_flags & IFF_LOOPBACK)
218 break;
219
220 if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld6_addr) &&
221 !IN6_IS_ADDR_MULTICAST(&mldh->mld6_addr))
222 break; /* print error or log stat? */
223 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
224 mldh->mld6_addr.s6_addr16[1] =
225 htons(ifp->if_index); /* XXX */
226
227 /*
228 * - Start the timers in all of our membership records
229 * that the query applies to for the interface on
230 * which the query arrived excl. those that belong
231 * to the "all-nodes" group (ff02::1).
232 * - Restart any timer that is already running but has
233 * A value longer than the requested timeout.
234 * - Use the value specified in the query message as
235 * the maximum timeout.
236 */
237 IFP_TO_IA6(ifp, ia);
238 if (ia == NULL)
239 break;
240
241 /*
242 * XXX: System timer resolution is too low to handle Max
243 * Response Delay, so set 1 to the internal timer even if
244 * the calculated value equals to zero when Max Response
245 * Delay is positive.
246 */
247 timer = ntohs(mldh->mld6_maxdelay)*PR_FASTHZ/MLD6_TIMER_SCALE;
248 if (timer == 0 && mldh->mld6_maxdelay)
249 timer = 1;
250 mld6_all_nodes_linklocal.s6_addr16[1] =
251 htons(ifp->if_index); /* XXX */
252
253 for (in6m = ia->ia6_multiaddrs.lh_first;
254 in6m;
255 in6m = in6m->in6m_entry.le_next) {
256 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr,
257 &mld6_all_nodes_linklocal) ||
258 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
259 IPV6_ADDR_SCOPE_LINKLOCAL)
260 continue;
261
262 if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld6_addr) ||
263 IN6_ARE_ADDR_EQUAL(&mldh->mld6_addr,
264 &in6m->in6m_addr)) {
265 if (timer == 0) {
266 /* send a report immediately */
267 mld6_sendpkt(in6m, MLD6_LISTENER_REPORT,
268 NULL);
269 in6m->in6m_timer = 0; /* reset timer */
270 in6m->in6m_state = MLD6_IREPORTEDLAST;
271 }
272 else if (in6m->in6m_timer == 0 || /*idle state*/
273 in6m->in6m_timer > timer) {
274 in6m->in6m_timer =
275 MLD6_RANDOM_DELAY(timer);
276 mld6_timers_are_running = 1;
277 }
278 }
279 }
280
281 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
282 mldh->mld6_addr.s6_addr16[1] = 0; /* XXX */
283 break;
284 case MLD6_LISTENER_REPORT:
285 /*
286 * For fast leave to work, we have to know that we are the
287 * last person to send a report for this group. Reports
288 * can potentially get looped back if we are a multicast
289 * router, so discard reports sourced by me.
290 * Note that it is impossible to check IFF_LOOPBACK flag of
291 * ifp for this purpose, since ip6_mloopback pass the physical
292 * interface to looutput.
293 */
294 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
295 break;
296
297 if (!IN6_IS_ADDR_MULTICAST(&mldh->mld6_addr))
298 break;
299
300 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
301 mldh->mld6_addr.s6_addr16[1] =
302 htons(ifp->if_index); /* XXX */
303 /*
304 * If we belong to the group being reported, stop
305 * our timer for that group.
306 */
307 IN6_LOOKUP_MULTI(mldh->mld6_addr, ifp, in6m);
308 if (in6m) {
309 in6m->in6m_timer = 0; /* transit to idle state */
310 in6m->in6m_state = MLD6_OTHERLISTENER; /* clear flag */
311 }
312
313 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
314 mldh->mld6_addr.s6_addr16[1] = 0; /* XXX */
315 break;
316 default: /* this is impossible */
317 log(LOG_ERR, "mld6_input: illegal type(%d)", mldh->mld6_type);
318 break;
319 }
320 }
321
322 void
323 mld6_fasttimeo()
324 {
325 register struct in6_multi *in6m;
326 struct in6_multistep step;
327 int s;
328
329 /*
330 * Quick check to see if any work needs to be done, in order
331 * to minimize the overhead of fasttimo processing.
332 */
333 if (!mld6_timers_are_running)
334 return;
335
336 s = splnet();
337 mld6_timers_are_running = 0;
338 IN6_FIRST_MULTI(step, in6m);
339 while (in6m != NULL) {
340 if (in6m->in6m_timer == 0) {
341 /* do nothing */
342 } else if (--in6m->in6m_timer == 0) {
343 mld6_sendpkt(in6m, MLD6_LISTENER_REPORT, NULL);
344 in6m->in6m_state = MLD6_IREPORTEDLAST;
345 } else {
346 mld6_timers_are_running = 1;
347 }
348 IN6_NEXT_MULTI(step, in6m);
349 }
350 splx(s);
351 }
352
353 static void
354 mld6_sendpkt(in6m, type, dst)
355 struct in6_multi *in6m;
356 int type;
357 const struct in6_addr *dst;
358 {
359 struct mbuf *mh, *md;
360 struct mld6_hdr *mldh;
361 struct ip6_hdr *ip6;
362 struct ip6_moptions im6o;
363 struct in6_ifaddr *ia;
364 struct ifnet *ifp = in6m->in6m_ifp;
365
366 /*
367 * At first, find a link local address on the outgoing interface
368 * to use as the source address of the MLD packet.
369 */
370 if ((ia = in6ifa_ifpforlinklocal(ifp)) == NULL)
371 return;
372
373 /*
374 * Allocate mbufs to store ip6 header and MLD header.
375 * We allocate 2 mbufs and make chain in advance because
376 * it is more convenient when inserting the hop-by-hop option later.
377 */
378 MGETHDR(mh, M_DONTWAIT, MT_HEADER);
379 if (mh == NULL)
380 return;
381 MGET(md, M_DONTWAIT, MT_DATA);
382 if (md == NULL) {
383 m_free(mh);
384 return;
385 }
386 mh->m_next = md;
387
388 #ifdef IPSEC
389 mh->m_pkthdr.rcvif = NULL;
390 #endif
391 mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld6_hdr);
392 mh->m_len = sizeof(struct ip6_hdr);
393 MH_ALIGN(mh, sizeof(struct ip6_hdr));
394
395 /* fill in the ip6 header */
396 ip6 = mtod(mh, struct ip6_hdr *);
397 ip6->ip6_flow = 0;
398 ip6->ip6_vfc = IPV6_VERSION;
399 /* ip6_plen will be set later */
400 ip6->ip6_nxt = IPPROTO_ICMPV6;
401 /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
402 ip6->ip6_src = ia->ia_addr.sin6_addr;
403 ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
404
405 /* fill in the MLD header */
406 md->m_len = sizeof(struct mld6_hdr);
407 mldh = mtod(md, struct mld6_hdr *);
408 mldh->mld6_type = type;
409 mldh->mld6_code = 0;
410 mldh->mld6_cksum = 0;
411 /* XXX: we assume the function will not be called for query messages */
412 mldh->mld6_maxdelay = 0;
413 mldh->mld6_reserved = 0;
414 mldh->mld6_addr = in6m->in6m_addr;
415 if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
416 mldh->mld6_addr.s6_addr16[1] = 0; /* XXX */
417 mldh->mld6_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
418 sizeof(struct mld6_hdr));
419
420 /* construct multicast option */
421 bzero(&im6o, sizeof(im6o));
422 im6o.im6o_multicast_ifp = ifp;
423 im6o.im6o_multicast_hlim = 1;
424
425 /*
426 * Request loopback of the report if we are acting as a multicast
427 * router, so that the process-level routing daemon can hear it.
428 */
429 im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
430
431 /* increment output statictics */
432 icmp6stat.icp6s_outhist[type]++;
433
434 ip6_output(mh, &ip6_opts, NULL, 0, &im6o);
435 }
436