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