mld6.c revision 1.44 1 /* $NetBSD: mld6.c,v 1.44 2008/04/15 03:57:04 thorpej 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.44 2008/04/15 03:57:04 thorpej 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 #include <sys/sysctl.h>
116 #include <sys/kernel.h>
117 #include <sys/callout.h>
118
119 #include <net/if.h>
120
121 #include <netinet/in.h>
122 #include <netinet/in_var.h>
123 #include <netinet6/in6_var.h>
124 #include <netinet/ip6.h>
125 #include <netinet6/ip6_var.h>
126 #include <netinet6/scope6_var.h>
127 #include <netinet/icmp6.h>
128 #include <netinet6/icmp6_private.h>
129 #include <netinet6/mld6_var.h>
130
131 #include <net/net_osdep.h>
132
133
134 /*
135 * This structure is used to keep track of in6_multi chains which belong to
136 * deleted interface addresses.
137 */
138 static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
139
140 struct multi6_kludge {
141 LIST_ENTRY(multi6_kludge) mk_entry;
142 struct ifnet *mk_ifp;
143 struct in6_multihead mk_head;
144 };
145
146
147 /*
148 * Protocol constants
149 */
150
151 /*
152 * time between repetitions of a node's initial report of interest in a
153 * multicast address(in seconds)
154 */
155 #define MLD_UNSOLICITED_REPORT_INTERVAL 10
156
157 static struct ip6_pktopts ip6_opts;
158
159 static void mld_start_listening(struct in6_multi *);
160 static void mld_stop_listening(struct in6_multi *);
161
162 static struct mld_hdr * mld_allocbuf(struct mbuf **, int, struct in6_multi *,
163 int);
164 static void mld_sendpkt(struct in6_multi *, int, const struct in6_addr *);
165 static void mld_starttimer(struct in6_multi *);
166 static void mld_stoptimer(struct in6_multi *);
167 static u_long mld_timerresid(struct in6_multi *);
168
169 void
170 mld_init(void)
171 {
172 static u_int8_t hbh_buf[8];
173 struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
174 u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
175
176 /* ip6h_nxt will be fill in later */
177 hbh->ip6h_len = 0; /* (8 >> 3) - 1 */
178
179 /* XXX: grotty hard coding... */
180 hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */
181 hbh_buf[3] = 0;
182 hbh_buf[4] = IP6OPT_RTALERT;
183 hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
184 bcopy((void *)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
185
186 ip6_opts.ip6po_hbh = hbh;
187 /* We will specify the hoplimit by a multicast option. */
188 ip6_opts.ip6po_hlim = -1;
189 }
190
191 static void
192 mld_starttimer(struct in6_multi *in6m)
193 {
194 struct timeval now;
195
196 microtime(&now);
197 in6m->in6m_timer_expire.tv_sec = now.tv_sec + in6m->in6m_timer / hz;
198 in6m->in6m_timer_expire.tv_usec = now.tv_usec +
199 (in6m->in6m_timer % hz) * (1000000 / hz);
200 if (in6m->in6m_timer_expire.tv_usec > 1000000) {
201 in6m->in6m_timer_expire.tv_sec++;
202 in6m->in6m_timer_expire.tv_usec -= 1000000;
203 }
204
205 /* start or restart the timer */
206 callout_schedule(&in6m->in6m_timer_ch, in6m->in6m_timer);
207 }
208
209 static void
210 mld_stoptimer(struct in6_multi *in6m)
211 {
212 if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
213 return;
214
215 callout_stop(&in6m->in6m_timer_ch);
216
217 in6m->in6m_timer = IN6M_TIMER_UNDEF;
218 }
219
220 static void
221 mld_timeo(void *arg)
222 {
223 struct in6_multi *in6m = arg;
224 int s = splsoftnet();
225
226 in6m->in6m_timer = IN6M_TIMER_UNDEF;
227
228 switch (in6m->in6m_state) {
229 case MLD_REPORTPENDING:
230 mld_start_listening(in6m);
231 break;
232 default:
233 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
234 break;
235 }
236
237 splx(s);
238 }
239
240 static u_long
241 mld_timerresid(struct in6_multi *in6m)
242 {
243 struct timeval now, diff;
244
245 microtime(&now);
246
247 if (now.tv_sec > in6m->in6m_timer_expire.tv_sec ||
248 (now.tv_sec == in6m->in6m_timer_expire.tv_sec &&
249 now.tv_usec > in6m->in6m_timer_expire.tv_usec)) {
250 return (0);
251 }
252 diff = in6m->in6m_timer_expire;
253 diff.tv_sec -= now.tv_sec;
254 diff.tv_usec -= now.tv_usec;
255 if (diff.tv_usec < 0) {
256 diff.tv_sec--;
257 diff.tv_usec += 1000000;
258 }
259
260 /* return the remaining time in milliseconds */
261 return (((u_long)(diff.tv_sec * 1000000 + diff.tv_usec)) / 1000);
262 }
263
264 static void
265 mld_start_listening(struct in6_multi *in6m)
266 {
267 struct in6_addr all_in6;
268
269 /*
270 * RFC2710 page 10:
271 * The node never sends a Report or Done for the link-scope all-nodes
272 * address.
273 * MLD messages are never sent for multicast addresses whose scope is 0
274 * (reserved) or 1 (node-local).
275 */
276 all_in6 = in6addr_linklocal_allnodes;
277 if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) {
278 /* XXX: this should not happen! */
279 in6m->in6m_timer = 0;
280 in6m->in6m_state = MLD_OTHERLISTENER;
281 }
282 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
283 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
284 in6m->in6m_timer = IN6M_TIMER_UNDEF;
285 in6m->in6m_state = MLD_OTHERLISTENER;
286 } else {
287 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
288 in6m->in6m_timer = arc4random() %
289 (MLD_UNSOLICITED_REPORT_INTERVAL * hz);
290 in6m->in6m_state = MLD_IREPORTEDLAST;
291
292 mld_starttimer(in6m);
293 }
294 }
295
296 static void
297 mld_stop_listening(struct in6_multi *in6m)
298 {
299 struct in6_addr allnode, allrouter;
300
301 allnode = in6addr_linklocal_allnodes;
302 if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) {
303 /* XXX: this should not happen! */
304 return;
305 }
306 allrouter = in6addr_linklocal_allrouters;
307 if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) {
308 /* XXX impossible */
309 return;
310 }
311
312 if (in6m->in6m_state == MLD_IREPORTEDLAST &&
313 (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode)) &&
314 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
315 IPV6_ADDR_SCOPE_INTFACELOCAL) {
316 mld_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
317 }
318 }
319
320 void
321 mld_input(struct mbuf *m, int off)
322 {
323 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
324 struct mld_hdr *mldh;
325 struct ifnet *ifp = m->m_pkthdr.rcvif;
326 struct in6_multi *in6m = NULL;
327 struct in6_addr mld_addr, all_in6;
328 struct in6_ifaddr *ia;
329 int timer = 0; /* timer value in the MLD query header */
330
331 IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
332 if (mldh == NULL) {
333 ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
334 return;
335 }
336
337 /* source address validation */
338 ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
339 if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
340 /*
341 * RFC3590 allows the IPv6 unspecified address as the source
342 * address of MLD report and done messages. However, as this
343 * same document says, this special rule is for snooping
344 * switches and the RFC requires routers to discard MLD packets
345 * with the unspecified source address. The RFC only talks
346 * about hosts receiving an MLD query or report in Security
347 * Considerations, but this is probably the correct intention.
348 * RFC3590 does not talk about other cases than link-local and
349 * the unspecified source addresses, but we believe the same
350 * rule should be applied.
351 * As a result, we only allow link-local addresses as the
352 * source address; otherwise, simply discard the packet.
353 */
354 #if 0
355 /*
356 * XXX: do not log in an input path to avoid log flooding,
357 * though RFC3590 says "SHOULD log" if the source of a query
358 * is the unspecified address.
359 */
360 log(LOG_INFO,
361 "mld_input: src %s is not link-local (grp=%s)\n",
362 ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&mldh->mld_addr));
363 #endif
364 m_freem(m);
365 return;
366 }
367
368 /*
369 * make a copy for local work (in6_setscope() may modify the 1st arg)
370 */
371 mld_addr = mldh->mld_addr;
372 if (in6_setscope(&mld_addr, ifp, NULL)) {
373 /* XXX: this should not happen! */
374 m_free(m);
375 return;
376 }
377
378 /*
379 * In the MLD specification, there are 3 states and a flag.
380 *
381 * In Non-Listener state, we simply don't have a membership record.
382 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
383 * In Idle Listener state, our timer is not running
384 * (in6m->in6m_timer==IN6M_TIMER_UNDEF)
385 *
386 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
387 * we have heard a report from another member, or MLD_IREPORTEDLAST
388 * if we sent the last report.
389 */
390 switch (mldh->mld_type) {
391 case MLD_LISTENER_QUERY:
392 if (ifp->if_flags & IFF_LOOPBACK)
393 break;
394
395 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
396 !IN6_IS_ADDR_MULTICAST(&mld_addr))
397 break; /* print error or log stat? */
398
399 all_in6 = in6addr_linklocal_allnodes;
400 if (in6_setscope(&all_in6, ifp, NULL)) {
401 /* XXX: this should not happen! */
402 break;
403 }
404
405 /*
406 * - Start the timers in all of our membership records
407 * that the query applies to for the interface on
408 * which the query arrived excl. those that belong
409 * to the "all-nodes" group (ff02::1).
410 * - Restart any timer that is already running but has
411 * a value longer than the requested timeout.
412 * - Use the value specified in the query message as
413 * the maximum timeout.
414 */
415 timer = ntohs(mldh->mld_maxdelay);
416
417 IFP_TO_IA6(ifp, ia);
418 if (ia == NULL)
419 break;
420
421 LIST_FOREACH(in6m, &ia->ia6_multiaddrs, in6m_entry) {
422 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
423 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
424 IPV6_ADDR_SCOPE_LINKLOCAL)
425 continue;
426
427 if (in6m->in6m_state == MLD_REPORTPENDING)
428 continue; /* we are not yet ready */
429
430 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
431 !IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr))
432 continue;
433
434 if (timer == 0) {
435 /* send a report immediately */
436 mld_stoptimer(in6m);
437 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
438 in6m->in6m_state = MLD_IREPORTEDLAST;
439 } else if (in6m->in6m_timer == IN6M_TIMER_UNDEF ||
440 mld_timerresid(in6m) > (u_long)timer) {
441 in6m->in6m_timer = arc4random() %
442 (int)(((long)timer * hz) / 1000);
443 mld_starttimer(in6m);
444 }
445 }
446 break;
447
448 case MLD_LISTENER_REPORT:
449 /*
450 * For fast leave to work, we have to know that we are the
451 * last person to send a report for this group. Reports
452 * can potentially get looped back if we are a multicast
453 * router, so discard reports sourced by me.
454 * Note that it is impossible to check IFF_LOOPBACK flag of
455 * ifp for this purpose, since ip6_mloopback pass the physical
456 * interface to looutput.
457 */
458 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
459 break;
460
461 if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
462 break;
463
464 /*
465 * If we belong to the group being reported, stop
466 * our timer for that group.
467 */
468 IN6_LOOKUP_MULTI(mld_addr, ifp, in6m);
469 if (in6m) {
470 mld_stoptimer(in6m); /* transit to idle state */
471 in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
472 }
473 break;
474 default: /* this is impossible */
475 #if 0
476 /*
477 * this case should be impossible because of filtering in
478 * icmp6_input(). But we explicitly disabled this part
479 * just in case.
480 */
481 log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type);
482 #endif
483 break;
484 }
485
486 m_freem(m);
487 }
488
489 static void
490 mld_sendpkt(struct in6_multi *in6m, int type,
491 const struct in6_addr *dst)
492 {
493 struct mbuf *mh;
494 struct mld_hdr *mldh;
495 struct ip6_hdr *ip6 = NULL;
496 struct ip6_moptions im6o;
497 struct in6_ifaddr *ia = NULL;
498 struct ifnet *ifp = in6m->in6m_ifp;
499 int ignflags;
500
501 /*
502 * At first, find a link local address on the outgoing interface
503 * to use as the source address of the MLD packet.
504 * We do not reject tentative addresses for MLD report to deal with
505 * the case where we first join a link-local address.
506 */
507 ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
508 if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
509 return;
510 if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
511 ia = NULL;
512
513 /* Allocate two mbufs to store IPv6 header and MLD header */
514 mldh = mld_allocbuf(&mh, sizeof(struct mld_hdr), in6m, type);
515 if (mldh == NULL)
516 return;
517
518 /* fill src/dst here */
519 ip6 = mtod(mh, struct ip6_hdr *);
520 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
521 ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
522
523 mldh->mld_addr = in6m->in6m_addr;
524 in6_clearscope(&mldh->mld_addr); /* XXX */
525 mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
526 sizeof(struct mld_hdr));
527
528 /* construct multicast option */
529 memset(&im6o, 0, sizeof(im6o));
530 im6o.im6o_multicast_ifp = ifp;
531 im6o.im6o_multicast_hlim = 1;
532
533 /*
534 * Request loopback of the report if we are acting as a multicast
535 * router, so that the process-level routing daemon can hear it.
536 */
537 im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
538
539 /* increment output statictics */
540 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
541 icmp6_ifstat_inc(ifp, ifs6_out_msg);
542 switch (type) {
543 case MLD_LISTENER_QUERY:
544 icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
545 break;
546 case MLD_LISTENER_REPORT:
547 icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
548 break;
549 case MLD_LISTENER_DONE:
550 icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
551 break;
552 }
553
554 ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
555 &im6o, (struct socket *)NULL, NULL);
556 }
557
558 static struct mld_hdr *
559 mld_allocbuf(struct mbuf **mh, int len, struct in6_multi *in6m,
560 int type)
561 {
562 struct mbuf *md;
563 struct mld_hdr *mldh;
564 struct ip6_hdr *ip6;
565
566 /*
567 * Allocate mbufs to store ip6 header and MLD header.
568 * We allocate 2 mbufs and make chain in advance because
569 * it is more convenient when inserting the hop-by-hop option later.
570 */
571 MGETHDR(*mh, M_DONTWAIT, MT_HEADER);
572 if (*mh == NULL)
573 return NULL;
574 MGET(md, M_DONTWAIT, MT_DATA);
575 if (md == NULL) {
576 m_free(*mh);
577 *mh = NULL;
578 return NULL;
579 }
580 (*mh)->m_next = md;
581 md->m_next = NULL;
582
583 (*mh)->m_pkthdr.rcvif = NULL;
584 (*mh)->m_pkthdr.len = sizeof(struct ip6_hdr) + len;
585 (*mh)->m_len = sizeof(struct ip6_hdr);
586 MH_ALIGN(*mh, sizeof(struct ip6_hdr));
587
588 /* fill in the ip6 header */
589 ip6 = mtod(*mh, struct ip6_hdr *);
590 memset(ip6, 0, sizeof(*ip6));
591 ip6->ip6_flow = 0;
592 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
593 ip6->ip6_vfc |= IPV6_VERSION;
594 /* ip6_plen will be set later */
595 ip6->ip6_nxt = IPPROTO_ICMPV6;
596 /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
597 /* ip6_src/dst will be set by mld_sendpkt() or mld_sendbuf() */
598
599 /* fill in the MLD header as much as possible */
600 md->m_len = len;
601 mldh = mtod(md, struct mld_hdr *);
602 memset(mldh, 0, len);
603 mldh->mld_type = type;
604 return mldh;
605 }
606
607 /*
608 * Add an address to the list of IP6 multicast addresses for a given interface.
609 */
610 struct in6_multi *
611 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp,
612 int *errorp, int timer)
613 {
614 struct in6_ifaddr *ia;
615 struct in6_ifreq ifr;
616 struct in6_multi *in6m;
617 int s = splsoftnet();
618
619 *errorp = 0;
620
621 /*
622 * See if address already in list.
623 */
624 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
625 if (in6m != NULL) {
626 /*
627 * Found it; just increment the refrence count.
628 */
629 in6m->in6m_refcount++;
630 } else {
631 /*
632 * New address; allocate a new multicast record
633 * and link it into the interface's multicast list.
634 */
635 in6m = (struct in6_multi *)
636 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
637 if (in6m == NULL) {
638 splx(s);
639 *errorp = ENOBUFS;
640 return (NULL);
641 }
642
643 memset(in6m, 0, sizeof(*in6m));
644 in6m->in6m_addr = *maddr6;
645 in6m->in6m_ifp = ifp;
646 in6m->in6m_refcount = 1;
647 in6m->in6m_timer = IN6M_TIMER_UNDEF;
648 IFP_TO_IA6(ifp, ia);
649 if (ia == NULL) {
650 free(in6m, M_IPMADDR);
651 splx(s);
652 *errorp = EADDRNOTAVAIL; /* appropriate? */
653 return (NULL);
654 }
655 in6m->in6m_ia = ia;
656 IFAREF(&ia->ia_ifa); /* gain a reference */
657 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
658
659 /*
660 * Ask the network driver to update its multicast reception
661 * filter appropriately for the new address.
662 */
663 sockaddr_in6_init(&ifr.ifr_addr, maddr6, 0, 0, 0);
664 if (ifp->if_ioctl == NULL)
665 *errorp = ENXIO; /* XXX: appropriate? */
666 else
667 *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
668 (void *)&ifr);
669 if (*errorp) {
670 LIST_REMOVE(in6m, in6m_entry);
671 free(in6m, M_IPMADDR);
672 IFAFREE(&ia->ia_ifa);
673 splx(s);
674 return (NULL);
675 }
676
677 callout_init(&in6m->in6m_timer_ch, 0);
678 callout_setfunc(&in6m->in6m_timer_ch, mld_timeo, in6m);
679 in6m->in6m_timer = timer;
680 if (in6m->in6m_timer > 0) {
681 in6m->in6m_state = MLD_REPORTPENDING;
682 mld_starttimer(in6m);
683
684 splx(s);
685 return (in6m);
686 }
687
688 /*
689 * Let MLD6 know that we have joined a new IP6 multicast
690 * group.
691 */
692 mld_start_listening(in6m);
693 }
694 splx(s);
695 return (in6m);
696 }
697
698 /*
699 * Delete a multicast address record.
700 */
701 void
702 in6_delmulti(struct in6_multi *in6m)
703 {
704 struct in6_ifreq ifr;
705 struct in6_ifaddr *ia;
706 int s = splsoftnet();
707
708 mld_stoptimer(in6m);
709
710 if (--in6m->in6m_refcount == 0) {
711 /*
712 * No remaining claims to this record; let MLD6 know
713 * that we are leaving the multicast group.
714 */
715 mld_stop_listening(in6m);
716
717 /*
718 * Unlink from list.
719 */
720 LIST_REMOVE(in6m, in6m_entry);
721 if (in6m->in6m_ia != NULL) {
722 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
723 in6m->in6m_ia = NULL;
724 }
725
726 /*
727 * Delete all references of this multicasting group from
728 * the membership arrays
729 */
730 for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
731 struct in6_multi_mship *imm;
732 LIST_FOREACH(imm, &ia->ia6_memberships, i6mm_chain) {
733 if (imm->i6mm_maddr == in6m)
734 imm->i6mm_maddr = NULL;
735 }
736 }
737
738 /*
739 * Notify the network driver to update its multicast
740 * reception filter.
741 */
742 sockaddr_in6_init(&ifr.ifr_addr, &in6m->in6m_addr, 0, 0, 0);
743 (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
744 SIOCDELMULTI, (void *)&ifr);
745 callout_destroy(&in6m->in6m_timer_ch);
746 free(in6m, M_IPMADDR);
747 }
748 splx(s);
749 }
750
751
752 struct in6_multi_mship *
753 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,
754 int *errorp, int timer)
755 {
756 struct in6_multi_mship *imm;
757
758 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
759 if (!imm) {
760 *errorp = ENOBUFS;
761 return NULL;
762 }
763
764 memset(imm, 0, sizeof(*imm));
765 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, timer);
766 if (!imm->i6mm_maddr) {
767 /* *errorp is already set */
768 free(imm, M_IPMADDR);
769 return NULL;
770 }
771 return imm;
772 }
773
774 int
775 in6_leavegroup(struct in6_multi_mship *imm)
776 {
777
778 if (imm->i6mm_maddr) {
779 in6_delmulti(imm->i6mm_maddr);
780 }
781 free(imm, M_IPMADDR);
782 return 0;
783 }
784
785
786 /*
787 * Multicast address kludge:
788 * If there were any multicast addresses attached to this interface address,
789 * either move them to another address on this interface, or save them until
790 * such time as this interface is reconfigured for IPv6.
791 */
792 void
793 in6_savemkludge(struct in6_ifaddr *oia)
794 {
795 struct in6_ifaddr *ia;
796 struct in6_multi *in6m;
797
798 IFP_TO_IA6(oia->ia_ifp, ia);
799 if (ia) { /* there is another address */
800 KASSERT(ia != oia);
801 while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
802 LIST_REMOVE(in6m, in6m_entry);
803 IFAREF(&ia->ia_ifa);
804 IFAFREE(&in6m->in6m_ia->ia_ifa);
805 in6m->in6m_ia = ia;
806 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
807 }
808 } else { /* last address on this if deleted, save */
809 struct multi6_kludge *mk;
810
811 LIST_FOREACH(mk, &in6_mk, mk_entry) {
812 if (mk->mk_ifp == oia->ia_ifp)
813 break;
814 }
815 if (mk == NULL) /* this should not happen! */
816 panic("in6_savemkludge: no kludge space");
817
818 while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
819 LIST_REMOVE(in6m, in6m_entry);
820 IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
821 in6m->in6m_ia = NULL;
822 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
823 }
824 }
825 }
826
827 /*
828 * Continuation of multicast address hack:
829 * If there was a multicast group list previously saved for this interface,
830 * then we re-attach it to the first address configured on the i/f.
831 */
832 void
833 in6_restoremkludge(struct in6_ifaddr *ia, struct ifnet *ifp)
834 {
835 struct multi6_kludge *mk;
836 struct in6_multi *in6m;
837
838 LIST_FOREACH(mk, &in6_mk, mk_entry) {
839 if (mk->mk_ifp == ifp)
840 break;
841 }
842 if (mk == NULL)
843 return;
844 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) {
845 LIST_REMOVE(in6m, in6m_entry);
846 in6m->in6m_ia = ia;
847 IFAREF(&ia->ia_ifa);
848 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
849 }
850 }
851
852 /*
853 * Allocate space for the kludge at interface initialization time.
854 * Formerly, we dynamically allocated the space in in6_savemkludge() with
855 * malloc(M_WAITOK). However, it was wrong since the function could be called
856 * under an interrupt context (software timer on address lifetime expiration).
857 * Also, we cannot just give up allocating the strucutre, since the group
858 * membership structure is very complex and we need to keep it anyway.
859 * Of course, this function MUST NOT be called under an interrupt context.
860 * Specifically, it is expected to be called only from in6_ifattach(), though
861 * it is a global function.
862 */
863 void
864 in6_createmkludge(struct ifnet *ifp)
865 {
866 struct multi6_kludge *mk;
867
868 LIST_FOREACH(mk, &in6_mk, mk_entry) {
869 /* If we've already had one, do not allocate. */
870 if (mk->mk_ifp == ifp)
871 return;
872 }
873
874 mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
875
876 memset(mk, 0, sizeof(*mk));
877 LIST_INIT(&mk->mk_head);
878 mk->mk_ifp = ifp;
879 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
880 }
881
882 void
883 in6_purgemkludge(struct ifnet *ifp)
884 {
885 struct multi6_kludge *mk;
886 struct in6_multi *in6m, *next;
887
888 LIST_FOREACH(mk, &in6_mk, mk_entry) {
889 if (mk->mk_ifp == ifp)
890 break;
891 }
892 if (mk == NULL)
893 return;
894
895 /* leave from all multicast groups joined */
896 for (in6m = LIST_FIRST(&mk->mk_head); in6m != NULL; in6m = next) {
897 next = LIST_NEXT(in6m, in6m_entry);
898 in6_delmulti(in6m);
899 }
900 LIST_REMOVE(mk, mk_entry);
901 free(mk, M_IPMADDR);
902 }
903