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