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