mld6.c revision 1.61.2.7 1 /* $NetBSD: mld6.c,v 1.61.2.7 2017/02/05 13:40:59 skrll 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.61.2.7 2017/02/05 13:40:59 skrll Exp $");
106
107 #ifdef _KERNEL_OPT
108 #include "opt_inet.h"
109 #include "opt_net_mpsafe.h"
110 #endif
111
112 #include <sys/param.h>
113 #include <sys/systm.h>
114 #include <sys/mbuf.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/syslog.h>
118 #include <sys/sysctl.h>
119 #include <sys/kernel.h>
120 #include <sys/callout.h>
121 #include <sys/cprng.h>
122
123 #include <net/if.h>
124
125 #include <netinet/in.h>
126 #include <netinet/in_var.h>
127 #include <netinet6/in6_var.h>
128 #include <netinet/ip6.h>
129 #include <netinet6/ip6_var.h>
130 #include <netinet6/scope6_var.h>
131 #include <netinet/icmp6.h>
132 #include <netinet6/icmp6_private.h>
133 #include <netinet6/mld6_var.h>
134
135 #include <net/net_osdep.h>
136
137
138 /*
139 * This structure is used to keep track of in6_multi chains which belong to
140 * deleted interface addresses.
141 */
142 static LIST_HEAD(, multi6_kludge) in6_mk = LIST_HEAD_INITIALIZER(in6_mk);
143
144 struct multi6_kludge {
145 LIST_ENTRY(multi6_kludge) mk_entry;
146 struct ifnet *mk_ifp;
147 struct in6_multihead mk_head;
148 };
149
150
151 /*
152 * Protocol constants
153 */
154
155 /*
156 * time between repetitions of a node's initial report of interest in a
157 * multicast address(in seconds)
158 */
159 #define MLD_UNSOLICITED_REPORT_INTERVAL 10
160
161 static struct ip6_pktopts ip6_opts;
162
163 static void mld_start_listening(struct in6_multi *);
164 static void mld_stop_listening(struct in6_multi *);
165
166 static struct mld_hdr * mld_allocbuf(struct mbuf **, int, struct in6_multi *,
167 int);
168 static void mld_sendpkt(struct in6_multi *, int, const struct in6_addr *);
169 static void mld_starttimer(struct in6_multi *);
170 static void mld_stoptimer(struct in6_multi *);
171 static u_long mld_timerresid(struct in6_multi *);
172
173 void
174 mld_init(void)
175 {
176 static u_int8_t hbh_buf[8];
177 struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
178 u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
179
180 /* ip6h_nxt will be fill in later */
181 hbh->ip6h_len = 0; /* (8 >> 3) - 1 */
182
183 /* XXX: grotty hard coding... */
184 hbh_buf[2] = IP6OPT_PADN; /* 2 byte padding */
185 hbh_buf[3] = 0;
186 hbh_buf[4] = IP6OPT_RTALERT;
187 hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
188 memcpy(&hbh_buf[6], (void *)&rtalert_code, sizeof(u_int16_t));
189
190 ip6_opts.ip6po_hbh = hbh;
191 /* We will specify the hoplimit by a multicast option. */
192 ip6_opts.ip6po_hlim = -1;
193 ip6_opts.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER;
194 }
195
196 static void
197 mld_starttimer(struct in6_multi *in6m)
198 {
199 struct timeval now;
200
201 KASSERT(in6m->in6m_timer != IN6M_TIMER_UNDEF);
202
203 microtime(&now);
204 in6m->in6m_timer_expire.tv_sec = now.tv_sec + in6m->in6m_timer / hz;
205 in6m->in6m_timer_expire.tv_usec = now.tv_usec +
206 (in6m->in6m_timer % hz) * (1000000 / hz);
207 if (in6m->in6m_timer_expire.tv_usec > 1000000) {
208 in6m->in6m_timer_expire.tv_sec++;
209 in6m->in6m_timer_expire.tv_usec -= 1000000;
210 }
211
212 /* start or restart the timer */
213 callout_schedule(&in6m->in6m_timer_ch, in6m->in6m_timer);
214 }
215
216 static void
217 mld_stoptimer(struct in6_multi *in6m)
218 {
219 if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
220 return;
221
222 callout_stop(&in6m->in6m_timer_ch);
223
224 in6m->in6m_timer = IN6M_TIMER_UNDEF;
225 }
226
227 static void
228 mld_timeo(void *arg)
229 {
230 struct in6_multi *in6m = arg;
231
232 /* XXX NOMPSAFE still need softnet_lock */
233 mutex_enter(softnet_lock);
234 KERNEL_LOCK(1, NULL);
235
236 if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
237 goto out;
238
239 in6m->in6m_timer = IN6M_TIMER_UNDEF;
240
241 switch (in6m->in6m_state) {
242 case MLD_REPORTPENDING:
243 mld_start_listening(in6m);
244 break;
245 default:
246 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
247 break;
248 }
249
250 out:
251 KERNEL_UNLOCK_ONE(NULL);
252 mutex_exit(softnet_lock);
253 }
254
255 static u_long
256 mld_timerresid(struct in6_multi *in6m)
257 {
258 struct timeval now, diff;
259
260 microtime(&now);
261
262 if (now.tv_sec > in6m->in6m_timer_expire.tv_sec ||
263 (now.tv_sec == in6m->in6m_timer_expire.tv_sec &&
264 now.tv_usec > in6m->in6m_timer_expire.tv_usec)) {
265 return (0);
266 }
267 diff = in6m->in6m_timer_expire;
268 diff.tv_sec -= now.tv_sec;
269 diff.tv_usec -= now.tv_usec;
270 if (diff.tv_usec < 0) {
271 diff.tv_sec--;
272 diff.tv_usec += 1000000;
273 }
274
275 /* return the remaining time in milliseconds */
276 return diff.tv_sec * 1000 + diff.tv_usec / 1000;
277 }
278
279 static void
280 mld_start_listening(struct in6_multi *in6m)
281 {
282 struct in6_addr all_in6;
283
284 /*
285 * RFC2710 page 10:
286 * The node never sends a Report or Done for the link-scope all-nodes
287 * address.
288 * MLD messages are never sent for multicast addresses whose scope is 0
289 * (reserved) or 1 (node-local).
290 */
291 all_in6 = in6addr_linklocal_allnodes;
292 if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) {
293 /* XXX: this should not happen! */
294 in6m->in6m_timer = 0;
295 in6m->in6m_state = MLD_OTHERLISTENER;
296 }
297 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
298 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
299 in6m->in6m_timer = IN6M_TIMER_UNDEF;
300 in6m->in6m_state = MLD_OTHERLISTENER;
301 } else {
302 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
303 in6m->in6m_timer = cprng_fast32() %
304 (MLD_UNSOLICITED_REPORT_INTERVAL * hz);
305 in6m->in6m_state = MLD_IREPORTEDLAST;
306
307 mld_starttimer(in6m);
308 }
309 }
310
311 static void
312 mld_stop_listening(struct in6_multi *in6m)
313 {
314 struct in6_addr allnode, allrouter;
315
316 allnode = in6addr_linklocal_allnodes;
317 if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) {
318 /* XXX: this should not happen! */
319 return;
320 }
321 allrouter = in6addr_linklocal_allrouters;
322 if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) {
323 /* XXX impossible */
324 return;
325 }
326
327 if (in6m->in6m_state == MLD_IREPORTEDLAST &&
328 (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode)) &&
329 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
330 IPV6_ADDR_SCOPE_INTFACELOCAL) {
331 mld_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
332 }
333 }
334
335 void
336 mld_input(struct mbuf *m, int off)
337 {
338 struct ip6_hdr *ip6;
339 struct mld_hdr *mldh;
340 struct ifnet *ifp;
341 struct in6_multi *in6m = NULL;
342 struct in6_addr mld_addr, all_in6;
343 struct in6_ifaddr *ia;
344 u_long timer = 0; /* timer value in the MLD query header */
345 int s;
346
347 ifp = m_get_rcvif(m, &s);
348 IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
349 if (mldh == NULL) {
350 ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
351 goto out_nodrop;
352 }
353
354 /* source address validation */
355 ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
356 if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
357 /*
358 * RFC3590 allows the IPv6 unspecified address as the source
359 * address of MLD report and done messages. However, as this
360 * same document says, this special rule is for snooping
361 * switches and the RFC requires routers to discard MLD packets
362 * with the unspecified source address. The RFC only talks
363 * about hosts receiving an MLD query or report in Security
364 * Considerations, but this is probably the correct intention.
365 * RFC3590 does not talk about other cases than link-local and
366 * the unspecified source addresses, but we believe the same
367 * rule should be applied.
368 * As a result, we only allow link-local addresses as the
369 * source address; otherwise, simply discard the packet.
370 */
371 #if 0
372 /*
373 * XXX: do not log in an input path to avoid log flooding,
374 * though RFC3590 says "SHOULD log" if the source of a query
375 * is the unspecified address.
376 */
377 char ip6bufs[INET6_ADDRSTRLEN];
378 char ip6bufm[INET6_ADDRSTRLEN];
379 log(LOG_INFO,
380 "mld_input: src %s is not link-local (grp=%s)\n",
381 IN6_PRINT(ip6bufs,&ip6->ip6_src),
382 IN6_PRINT(ip6bufm, &mldh->mld_addr));
383 #endif
384 goto out;
385 }
386
387 /*
388 * make a copy for local work (in6_setscope() may modify the 1st arg)
389 */
390 mld_addr = mldh->mld_addr;
391 if (in6_setscope(&mld_addr, ifp, NULL)) {
392 /* XXX: this should not happen! */
393 goto out;
394 }
395
396 /*
397 * In the MLD specification, there are 3 states and a flag.
398 *
399 * In Non-Listener state, we simply don't have a membership record.
400 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
401 * In Idle Listener state, our timer is not running
402 * (in6m->in6m_timer==IN6M_TIMER_UNDEF)
403 *
404 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
405 * we have heard a report from another member, or MLD_IREPORTEDLAST
406 * if we sent the last report.
407 */
408 switch (mldh->mld_type) {
409 case MLD_LISTENER_QUERY: {
410 struct psref psref;
411
412 if (ifp->if_flags & IFF_LOOPBACK)
413 break;
414
415 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
416 !IN6_IS_ADDR_MULTICAST(&mld_addr))
417 break; /* print error or log stat? */
418
419 all_in6 = in6addr_linklocal_allnodes;
420 if (in6_setscope(&all_in6, ifp, NULL)) {
421 /* XXX: this should not happen! */
422 break;
423 }
424
425 /*
426 * - Start the timers in all of our membership records
427 * that the query applies to for the interface on
428 * which the query arrived excl. those that belong
429 * to the "all-nodes" group (ff02::1).
430 * - Restart any timer that is already running but has
431 * a value longer than the requested timeout.
432 * - Use the value specified in the query message as
433 * the maximum timeout.
434 */
435 timer = ntohs(mldh->mld_maxdelay);
436
437 ia = in6_get_ia_from_ifp_psref(ifp, &psref);
438 if (ia == NULL)
439 break;
440
441 /* The following operations may sleep */
442 m_put_rcvif(ifp, &s);
443 ifp = NULL;
444
445 LIST_FOREACH(in6m, &ia->ia6_multiaddrs, in6m_entry) {
446 if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
447 IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
448 IPV6_ADDR_SCOPE_LINKLOCAL)
449 continue;
450
451 if (in6m->in6m_state == MLD_REPORTPENDING)
452 continue; /* we are not yet ready */
453
454 if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
455 !IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr))
456 continue;
457
458 if (timer == 0) {
459 /* send a report immediately */
460 mld_stoptimer(in6m);
461 mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
462 in6m->in6m_state = MLD_IREPORTEDLAST;
463 } else if (in6m->in6m_timer == IN6M_TIMER_UNDEF ||
464 mld_timerresid(in6m) > timer) {
465 in6m->in6m_timer =
466 1 + (cprng_fast32() % timer) * hz / 1000;
467 mld_starttimer(in6m);
468 }
469 }
470 ia6_release(ia, &psref);
471 break;
472 }
473
474 case MLD_LISTENER_REPORT:
475 /*
476 * For fast leave to work, we have to know that we are the
477 * last person to send a report for this group. Reports
478 * can potentially get looped back if we are a multicast
479 * router, so discard reports sourced by me.
480 * Note that it is impossible to check IFF_LOOPBACK flag of
481 * ifp for this purpose, since ip6_mloopback pass the physical
482 * interface to looutput.
483 */
484 if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
485 break;
486
487 if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
488 break;
489
490 /*
491 * If we belong to the group being reported, stop
492 * our timer for that group.
493 */
494 IN6_LOOKUP_MULTI(mld_addr, ifp, in6m);
495 if (in6m) {
496 mld_stoptimer(in6m); /* transit to idle state */
497 in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
498 }
499 break;
500 default: /* this is impossible */
501 #if 0
502 /*
503 * this case should be impossible because of filtering in
504 * icmp6_input(). But we explicitly disabled this part
505 * just in case.
506 */
507 log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type);
508 #endif
509 break;
510 }
511
512 out:
513 m_freem(m);
514 out_nodrop:
515 m_put_rcvif(ifp, &s);
516 }
517
518 static void
519 mld_sendpkt(struct in6_multi *in6m, int type,
520 const struct in6_addr *dst)
521 {
522 struct mbuf *mh;
523 struct mld_hdr *mldh;
524 struct ip6_hdr *ip6 = NULL;
525 struct ip6_moptions im6o;
526 struct in6_ifaddr *ia = NULL;
527 struct ifnet *ifp = in6m->in6m_ifp;
528 int ignflags;
529 struct psref psref;
530 int bound;
531
532 /*
533 * At first, find a link local address on the outgoing interface
534 * to use as the source address of the MLD packet.
535 * We do not reject tentative addresses for MLD report to deal with
536 * the case where we first join a link-local address.
537 */
538 ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
539 bound = curlwp_bind();
540 ia = in6ifa_ifpforlinklocal_psref(ifp, ignflags, &psref);
541 if (ia == NULL) {
542 curlwp_bindx(bound);
543 return;
544 }
545 if ((ia->ia6_flags & IN6_IFF_TENTATIVE)) {
546 ia6_release(ia, &psref);
547 ia = NULL;
548 }
549
550 /* Allocate two mbufs to store IPv6 header and MLD header */
551 mldh = mld_allocbuf(&mh, sizeof(struct mld_hdr), in6m, type);
552 if (mldh == NULL) {
553 ia6_release(ia, &psref);
554 curlwp_bindx(bound);
555 return;
556 }
557
558 /* fill src/dst here */
559 ip6 = mtod(mh, struct ip6_hdr *);
560 ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
561 ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
562 ia6_release(ia, &psref);
563 curlwp_bindx(bound);
564
565 mldh->mld_addr = in6m->in6m_addr;
566 in6_clearscope(&mldh->mld_addr); /* XXX */
567 mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
568 sizeof(struct mld_hdr));
569
570 /* construct multicast option */
571 memset(&im6o, 0, sizeof(im6o));
572 im6o.im6o_multicast_if_index = if_get_index(ifp);
573 im6o.im6o_multicast_hlim = 1;
574
575 /*
576 * Request loopback of the report if we are acting as a multicast
577 * router, so that the process-level routing daemon can hear it.
578 */
579 im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
580
581 /* increment output statictics */
582 ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
583 icmp6_ifstat_inc(ifp, ifs6_out_msg);
584 switch (type) {
585 case MLD_LISTENER_QUERY:
586 icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
587 break;
588 case MLD_LISTENER_REPORT:
589 icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
590 break;
591 case MLD_LISTENER_DONE:
592 icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
593 break;
594 }
595
596 ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
597 &im6o, NULL, NULL);
598 }
599
600 static struct mld_hdr *
601 mld_allocbuf(struct mbuf **mh, int len, struct in6_multi *in6m,
602 int type)
603 {
604 struct mbuf *md;
605 struct mld_hdr *mldh;
606 struct ip6_hdr *ip6;
607
608 /*
609 * Allocate mbufs to store ip6 header and MLD header.
610 * We allocate 2 mbufs and make chain in advance because
611 * it is more convenient when inserting the hop-by-hop option later.
612 */
613 MGETHDR(*mh, M_DONTWAIT, MT_HEADER);
614 if (*mh == NULL)
615 return NULL;
616 MGET(md, M_DONTWAIT, MT_DATA);
617 if (md == NULL) {
618 m_free(*mh);
619 *mh = NULL;
620 return NULL;
621 }
622 (*mh)->m_next = md;
623 md->m_next = NULL;
624
625 m_reset_rcvif((*mh));
626 (*mh)->m_pkthdr.len = sizeof(struct ip6_hdr) + len;
627 (*mh)->m_len = sizeof(struct ip6_hdr);
628 MH_ALIGN(*mh, sizeof(struct ip6_hdr));
629
630 /* fill in the ip6 header */
631 ip6 = mtod(*mh, struct ip6_hdr *);
632 memset(ip6, 0, sizeof(*ip6));
633 ip6->ip6_flow = 0;
634 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
635 ip6->ip6_vfc |= IPV6_VERSION;
636 /* ip6_plen will be set later */
637 ip6->ip6_nxt = IPPROTO_ICMPV6;
638 /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
639 /* ip6_src/dst will be set by mld_sendpkt() or mld_sendbuf() */
640
641 /* fill in the MLD header as much as possible */
642 md->m_len = len;
643 mldh = mtod(md, struct mld_hdr *);
644 memset(mldh, 0, len);
645 mldh->mld_type = type;
646 return mldh;
647 }
648
649 /*
650 * Add an address to the list of IP6 multicast addresses for a given interface.
651 */
652 struct in6_multi *
653 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp,
654 int *errorp, int timer)
655 {
656 struct in6_ifaddr *ia;
657 struct sockaddr_in6 sin6;
658 struct in6_multi *in6m;
659 int s = splsoftnet();
660
661 *errorp = 0;
662
663 /*
664 * See if address already in list.
665 */
666 IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
667 if (in6m != NULL) {
668 /*
669 * Found it; just increment the refrence count.
670 */
671 in6m->in6m_refcount++;
672 } else {
673 int _s;
674 /*
675 * New address; allocate a new multicast record
676 * and link it into the interface's multicast list.
677 */
678 in6m = (struct in6_multi *)
679 malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT|M_ZERO);
680 if (in6m == NULL) {
681 splx(s);
682 *errorp = ENOBUFS;
683 return (NULL);
684 }
685
686 in6m->in6m_addr = *maddr6;
687 in6m->in6m_ifp = ifp;
688 in6m->in6m_refcount = 1;
689 in6m->in6m_timer = IN6M_TIMER_UNDEF;
690 callout_init(&in6m->in6m_timer_ch, CALLOUT_MPSAFE);
691 callout_setfunc(&in6m->in6m_timer_ch, mld_timeo, in6m);
692
693 _s = pserialize_read_enter();
694 ia = in6_get_ia_from_ifp(ifp);
695 if (ia == NULL) {
696 pserialize_read_exit(_s);
697 callout_destroy(&in6m->in6m_timer_ch);
698 free(in6m, M_IPMADDR);
699 splx(s);
700 *errorp = EADDRNOTAVAIL; /* appropriate? */
701 return (NULL);
702 }
703 in6m->in6m_ia = ia;
704 ifaref(&ia->ia_ifa); /* gain a reference */
705 /* FIXME NOMPSAFE: need to lock */
706 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
707 pserialize_read_exit(_s);
708
709 /*
710 * Ask the network driver to update its multicast reception
711 * filter appropriately for the new address.
712 */
713 sockaddr_in6_init(&sin6, maddr6, 0, 0, 0);
714 *errorp = if_mcast_op(ifp, SIOCADDMULTI, sin6tosa(&sin6));
715 if (*errorp) {
716 callout_destroy(&in6m->in6m_timer_ch);
717 LIST_REMOVE(in6m, in6m_entry);
718 free(in6m, M_IPMADDR);
719 ifafree(&ia->ia_ifa);
720 splx(s);
721 return (NULL);
722 }
723
724 in6m->in6m_timer = timer;
725 if (in6m->in6m_timer > 0) {
726 in6m->in6m_state = MLD_REPORTPENDING;
727 mld_starttimer(in6m);
728
729 splx(s);
730 return (in6m);
731 }
732
733 /*
734 * Let MLD6 know that we have joined a new IP6 multicast
735 * group.
736 */
737 mld_start_listening(in6m);
738 }
739 splx(s);
740 return (in6m);
741 }
742
743 /*
744 * Delete a multicast address record.
745 */
746 void
747 in6_delmulti(struct in6_multi *in6m)
748 {
749 struct sockaddr_in6 sin6;
750 struct in6_ifaddr *ia;
751 int s = splsoftnet();
752
753 mld_stoptimer(in6m);
754
755 if (--in6m->in6m_refcount == 0) {
756 int _s;
757
758 /*
759 * No remaining claims to this record; let MLD6 know
760 * that we are leaving the multicast group.
761 */
762 mld_stop_listening(in6m);
763
764 /*
765 * Unlink from list.
766 */
767 LIST_REMOVE(in6m, in6m_entry);
768 if (in6m->in6m_ia != NULL) {
769 ifafree(&in6m->in6m_ia->ia_ifa); /* release reference */
770 in6m->in6m_ia = NULL;
771 }
772
773 /*
774 * Delete all references of this multicasting group from
775 * the membership arrays
776 */
777 _s = pserialize_read_enter();
778 IN6_ADDRLIST_READER_FOREACH(ia) {
779 struct in6_multi_mship *imm;
780 LIST_FOREACH(imm, &ia->ia6_memberships, i6mm_chain) {
781 if (imm->i6mm_maddr == in6m)
782 imm->i6mm_maddr = NULL;
783 }
784 }
785 pserialize_read_exit(_s);
786
787 /*
788 * Notify the network driver to update its multicast
789 * reception filter.
790 */
791 sockaddr_in6_init(&sin6, &in6m->in6m_addr, 0, 0, 0);
792 if_mcast_op(in6m->in6m_ifp, SIOCDELMULTI, sin6tosa(&sin6));
793
794 /* Tell mld_timeo we're halting the timer */
795 in6m->in6m_timer = IN6M_TIMER_UNDEF;
796 if (mutex_owned(softnet_lock))
797 callout_halt(&in6m->in6m_timer_ch, softnet_lock);
798 else
799 callout_halt(&in6m->in6m_timer_ch, NULL);
800 callout_destroy(&in6m->in6m_timer_ch);
801
802 free(in6m, M_IPMADDR);
803 }
804 splx(s);
805 }
806
807
808 struct in6_multi_mship *
809 in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,
810 int *errorp, int timer)
811 {
812 struct in6_multi_mship *imm;
813
814 imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT|M_ZERO);
815 if (imm == NULL) {
816 *errorp = ENOBUFS;
817 return NULL;
818 }
819
820 imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, timer);
821 if (!imm->i6mm_maddr) {
822 /* *errorp is already set */
823 free(imm, M_IPMADDR);
824 return NULL;
825 }
826 return imm;
827 }
828
829 int
830 in6_leavegroup(struct in6_multi_mship *imm)
831 {
832
833 if (imm->i6mm_maddr) {
834 in6_delmulti(imm->i6mm_maddr);
835 }
836 free(imm, M_IPMADDR);
837 return 0;
838 }
839
840
841 /*
842 * Multicast address kludge:
843 * If there were any multicast addresses attached to this interface address,
844 * either move them to another address on this interface, or save them until
845 * such time as this interface is reconfigured for IPv6.
846 */
847 void
848 in6_savemkludge(struct in6_ifaddr *oia)
849 {
850 struct in6_ifaddr *ia;
851 struct in6_multi *in6m;
852 int s;
853
854 s = pserialize_read_enter();
855 ia = in6_get_ia_from_ifp(oia->ia_ifp);
856 if (ia) { /* there is another address */
857 KASSERT(ia != oia);
858 while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
859 LIST_REMOVE(in6m, in6m_entry);
860 ifaref(&ia->ia_ifa);
861 ifafree(&in6m->in6m_ia->ia_ifa);
862 in6m->in6m_ia = ia;
863 /* FIXME NOMPSAFE: need to lock */
864 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
865 }
866 } else { /* last address on this if deleted, save */
867 struct multi6_kludge *mk;
868
869 LIST_FOREACH(mk, &in6_mk, mk_entry) {
870 if (mk->mk_ifp == oia->ia_ifp)
871 break;
872 }
873 if (mk == NULL) /* this should not happen! */
874 panic("in6_savemkludge: no kludge space");
875
876 while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
877 LIST_REMOVE(in6m, in6m_entry);
878 ifafree(&in6m->in6m_ia->ia_ifa); /* release reference */
879 in6m->in6m_ia = NULL;
880 LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
881 }
882 }
883 pserialize_read_exit(s);
884 }
885
886 /*
887 * Continuation of multicast address hack:
888 * If there was a multicast group list previously saved for this interface,
889 * then we re-attach it to the first address configured on the i/f.
890 */
891 void
892 in6_restoremkludge(struct in6_ifaddr *ia, struct ifnet *ifp)
893 {
894 struct multi6_kludge *mk;
895 struct in6_multi *in6m;
896
897 LIST_FOREACH(mk, &in6_mk, mk_entry) {
898 if (mk->mk_ifp == ifp)
899 break;
900 }
901 if (mk == NULL)
902 return;
903 while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) {
904 LIST_REMOVE(in6m, in6m_entry);
905 in6m->in6m_ia = ia;
906 ifaref(&ia->ia_ifa);
907 LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
908 }
909 }
910
911 /*
912 * Allocate space for the kludge at interface initialization time.
913 * Formerly, we dynamically allocated the space in in6_savemkludge() with
914 * malloc(M_WAITOK). However, it was wrong since the function could be called
915 * under an interrupt context (software timer on address lifetime expiration).
916 * Also, we cannot just give up allocating the strucutre, since the group
917 * membership structure is very complex and we need to keep it anyway.
918 * Of course, this function MUST NOT be called under an interrupt context.
919 * Specifically, it is expected to be called only from in6_ifattach(), though
920 * it is a global function.
921 */
922 void
923 in6_createmkludge(struct ifnet *ifp)
924 {
925 struct multi6_kludge *mk;
926
927 LIST_FOREACH(mk, &in6_mk, mk_entry) {
928 /* If we've already had one, do not allocate. */
929 if (mk->mk_ifp == ifp)
930 return;
931 }
932
933 mk = malloc(sizeof(*mk), M_IPMADDR, M_ZERO|M_WAITOK);
934
935 LIST_INIT(&mk->mk_head);
936 mk->mk_ifp = ifp;
937 LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
938 }
939
940 void
941 in6_purgemkludge(struct ifnet *ifp)
942 {
943 struct multi6_kludge *mk;
944 struct in6_multi *in6m, *next;
945
946 LIST_FOREACH(mk, &in6_mk, mk_entry) {
947 if (mk->mk_ifp == ifp)
948 break;
949 }
950 if (mk == NULL)
951 return;
952
953 /* leave from all multicast groups joined */
954 for (in6m = LIST_FIRST(&mk->mk_head); in6m != NULL; in6m = next) {
955 next = LIST_NEXT(in6m, in6m_entry);
956 in6_delmulti(in6m);
957 }
958 LIST_REMOVE(mk, mk_entry);
959 free(mk, M_IPMADDR);
960 }
961
962 static int
963 in6_mkludge_sysctl(SYSCTLFN_ARGS)
964 {
965 struct multi6_kludge *mk;
966 struct in6_multi *in6m;
967 int error;
968 uint32_t tmp;
969 size_t written;
970
971 if (namelen != 1)
972 return EINVAL;
973
974 if (oldp == NULL) {
975 *oldlenp = 0;
976 LIST_FOREACH(mk, &in6_mk, mk_entry) {
977 if (mk->mk_ifp->if_index == name[0])
978 continue;
979 LIST_FOREACH(in6m, &mk->mk_head, in6m_entry) {
980 *oldlenp += sizeof(struct in6_addr) +
981 sizeof(uint32_t);
982 }
983 }
984 return 0;
985 }
986
987 error = 0;
988 written = 0;
989 LIST_FOREACH(mk, &in6_mk, mk_entry) {
990 if (mk->mk_ifp->if_index == name[0])
991 continue;
992 LIST_FOREACH(in6m, &mk->mk_head, in6m_entry) {
993 if (written + sizeof(struct in6_addr) +
994 sizeof(uint32_t) > *oldlenp)
995 goto done;
996 error = sysctl_copyout(l, &in6m->in6m_addr,
997 oldp, sizeof(struct in6_addr));
998 if (error)
999 goto done;
1000 oldp = (char *)oldp + sizeof(struct in6_addr);
1001 written += sizeof(struct in6_addr);
1002 tmp = in6m->in6m_refcount;
1003 error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
1004 if (error)
1005 goto done;
1006 oldp = (char *)oldp + sizeof(tmp);
1007 written += sizeof(tmp);
1008 }
1009 }
1010
1011 done:
1012 *oldlenp = written;
1013 return error;
1014 }
1015
1016 static int
1017 in6_multicast_sysctl(SYSCTLFN_ARGS)
1018 {
1019 struct ifnet *ifp;
1020 struct ifaddr *ifa;
1021 struct in6_ifaddr *ifa6;
1022 struct in6_multi *in6m;
1023 uint32_t tmp;
1024 int error;
1025 size_t written;
1026 struct psref psref, psref_ia;
1027 int bound, s;
1028
1029 if (namelen != 1)
1030 return EINVAL;
1031
1032 bound = curlwp_bind();
1033 ifp = if_get_byindex(name[0], &psref);
1034 if (ifp == NULL) {
1035 curlwp_bindx(bound);
1036 return ENODEV;
1037 }
1038
1039 if (oldp == NULL) {
1040 *oldlenp = 0;
1041 s = pserialize_read_enter();
1042 IFADDR_READER_FOREACH(ifa, ifp) {
1043 if (ifa->ifa_addr->sa_family != AF_INET6)
1044 continue;
1045 ifa6 = (struct in6_ifaddr *)ifa;
1046 LIST_FOREACH(in6m, &ifa6->ia6_multiaddrs, in6m_entry) {
1047 *oldlenp += 2 * sizeof(struct in6_addr) +
1048 sizeof(uint32_t);
1049 }
1050 }
1051 pserialize_read_exit(s);
1052 if_put(ifp, &psref);
1053 curlwp_bindx(bound);
1054 return 0;
1055 }
1056
1057 error = 0;
1058 written = 0;
1059 s = pserialize_read_enter();
1060 IFADDR_READER_FOREACH(ifa, ifp) {
1061 if (ifa->ifa_addr->sa_family != AF_INET6)
1062 continue;
1063
1064 ifa_acquire(ifa, &psref_ia);
1065 pserialize_read_exit(s);
1066
1067 ifa6 = (struct in6_ifaddr *)ifa;
1068 LIST_FOREACH(in6m, &ifa6->ia6_multiaddrs, in6m_entry) {
1069 if (written + 2 * sizeof(struct in6_addr) +
1070 sizeof(uint32_t) > *oldlenp)
1071 goto done;
1072 error = sysctl_copyout(l, &ifa6->ia_addr.sin6_addr,
1073 oldp, sizeof(struct in6_addr));
1074 if (error)
1075 goto done;
1076 oldp = (char *)oldp + sizeof(struct in6_addr);
1077 written += sizeof(struct in6_addr);
1078 error = sysctl_copyout(l, &in6m->in6m_addr,
1079 oldp, sizeof(struct in6_addr));
1080 if (error)
1081 goto done;
1082 oldp = (char *)oldp + sizeof(struct in6_addr);
1083 written += sizeof(struct in6_addr);
1084 tmp = in6m->in6m_refcount;
1085 error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
1086 if (error)
1087 goto done;
1088 oldp = (char *)oldp + sizeof(tmp);
1089 written += sizeof(tmp);
1090 }
1091
1092 s = pserialize_read_enter();
1093 ifa_release(ifa, &psref_ia);
1094 }
1095 pserialize_read_exit(s);
1096 done:
1097 ifa_release(ifa, &psref_ia);
1098 if_put(ifp, &psref);
1099 curlwp_bindx(bound);
1100 *oldlenp = written;
1101 return error;
1102 }
1103
1104 void
1105 in6_sysctl_multicast_setup(struct sysctllog **clog)
1106 {
1107
1108 sysctl_createv(clog, 0, NULL, NULL,
1109 CTLFLAG_PERMANENT,
1110 CTLTYPE_NODE, "inet6", NULL,
1111 NULL, 0, NULL, 0,
1112 CTL_NET, PF_INET6, CTL_EOL);
1113
1114 sysctl_createv(clog, 0, NULL, NULL,
1115 CTLFLAG_PERMANENT,
1116 CTLTYPE_NODE, "multicast",
1117 SYSCTL_DESCR("Multicast information"),
1118 in6_multicast_sysctl, 0, NULL, 0,
1119 CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
1120
1121 sysctl_createv(clog, 0, NULL, NULL,
1122 CTLFLAG_PERMANENT,
1123 CTLTYPE_NODE, "multicast_kludge",
1124 SYSCTL_DESCR("multicast kludge information"),
1125 in6_mkludge_sysctl, 0, NULL, 0,
1126 CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
1127 }
1128