1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 2007-2009 Bruce Simpson.
5 * Copyright (c) 1988 Stephen Deering.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
8 *
9 * This code is derived from software contributed to Berkeley by
10 * Stephen Deering of Stanford University.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
37 */
38
39 /*
40 * Internet Group Management Protocol (IGMP) routines.
41 * [RFC1112, RFC2236, RFC3376]
42 *
43 * Written by Steve Deering, Stanford, May 1988.
44 * Modified by Rosen Sharma, Stanford, Aug 1994.
45 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
46 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
47 * Significantly rewritten for IGMPv3, VIMAGE, and SMP by Bruce Simpson.
48 *
49 * MULTICAST Revision: 3.5.1.4
50 */
51
52 #include <sys/cdefs.h>
53 #include "opt_ddb.h"
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/module.h>
58 #include <sys/malloc.h>
59 #include <sys/mbuf.h>
60 #include <sys/socket.h>
61 #include <sys/kernel.h>
62 #include <sys/lock.h>
63 #include <sys/sysctl.h>
64 #include <sys/ktr.h>
65 #include <sys/condvar.h>
66
67 #ifdef DDB
68 #include <ddb/ddb.h>
69 #endif
70
71 #include <net/if.h>
72 #include <net/if_var.h>
73 #include <net/if_private.h>
74 #include <net/netisr.h>
75 #include <net/vnet.h>
76
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/ip.h>
81 #include <netinet/ip_var.h>
82 #include <netinet/ip_options.h>
83 #include <netinet/igmp.h>
84 #include <netinet/igmp_var.h>
85
86 #include <machine/in_cksum.h>
87
88 #include <security/mac/mac_framework.h>
89
90 #ifndef KTR_IGMPV3
91 #define KTR_IGMPV3 KTR_INET
92 #endif
93
94 #define IGMP_SLOWHZ 2 /* 2 slow timeouts per second */
95 #define IGMP_FASTHZ 5 /* 5 fast timeouts per second */
96 #define IGMP_RESPONSE_BURST_INTERVAL (IGMP_FASTHZ / 2)
97
98 static struct igmp_ifsoftc *
99 igi_alloc_locked(struct ifnet *);
100 static void igi_delete_locked(const struct ifnet *);
101 static void igmp_dispatch_queue(struct mbufq *, int, const int);
102 static void igmp_fasttimo_vnet(void);
103 static void igmp_final_leave(struct in_multi *, struct igmp_ifsoftc *);
104 static int igmp_handle_state_change(struct in_multi *,
105 struct igmp_ifsoftc *);
106 static int igmp_initial_join(struct in_multi *, struct igmp_ifsoftc *);
107 static int igmp_input_v1_query(struct ifnet *, const struct ip *,
108 const struct igmp *);
109 static int igmp_input_v2_query(struct ifnet *, const struct ip *,
110 const struct igmp *);
111 static int igmp_input_v3_query(struct ifnet *, const struct ip *,
112 /*const*/ struct igmpv3 *);
113 static int igmp_input_v3_group_query(struct in_multi *,
114 struct igmp_ifsoftc *, int, /*const*/ struct igmpv3 *);
115 static int igmp_input_v1_report(struct ifnet *, /*const*/ struct ip *,
116 /*const*/ struct igmp *);
117 static int igmp_input_v2_report(struct ifnet *, /*const*/ struct ip *,
118 /*const*/ struct igmp *);
119 static void igmp_intr(struct mbuf *);
120 static int igmp_isgroupreported(const struct in_addr);
121 static struct mbuf *
122 igmp_ra_alloc(void);
123 #ifdef KTR
124 static char * igmp_rec_type_to_str(const int);
125 #endif
126 static void igmp_set_version(struct igmp_ifsoftc *, const int);
127 static void igmp_slowtimo_vnet(void);
128 static int igmp_v1v2_queue_report(struct in_multi *, const int);
129 static void igmp_v1v2_process_group_timer(struct in_multi *, const int);
130 static void igmp_v1v2_process_querier_timers(struct igmp_ifsoftc *);
131 static void igmp_v2_update_group(struct in_multi *, const int);
132 static void igmp_v3_cancel_link_timers(struct igmp_ifsoftc *);
133 static void igmp_v3_dispatch_general_query(struct igmp_ifsoftc *);
134 static struct mbuf *
135 igmp_v3_encap_report(struct ifnet *, struct mbuf *);
136 static int igmp_v3_enqueue_group_record(struct mbufq *,
137 struct in_multi *, const int, const int, const int);
138 static int igmp_v3_enqueue_filter_change(struct mbufq *,
139 struct in_multi *);
140 static void igmp_v3_process_group_timers(struct in_multi_head *,
141 struct mbufq *, struct mbufq *, struct in_multi *,
142 const int);
143 static int igmp_v3_merge_state_changes(struct in_multi *,
144 struct mbufq *);
145 static void igmp_v3_suppress_group_record(struct in_multi *);
146 static int sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS);
147 static int sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS);
148 static int sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS);
149 static int sysctl_igmp_stat(SYSCTL_HANDLER_ARGS);
150
151 static const struct netisr_handler igmp_nh = {
152 .nh_name = "igmp",
153 .nh_handler = igmp_intr,
154 .nh_proto = NETISR_IGMP,
155 .nh_policy = NETISR_POLICY_SOURCE,
156 };
157
158 /*
159 * System-wide globals.
160 *
161 * Unlocked access to these is OK, except for the global IGMP output
162 * queue. The IGMP subsystem lock ends up being system-wide for the moment,
163 * because all VIMAGEs have to share a global output queue, as netisrs
164 * themselves are not virtualized.
165 *
166 * Locking:
167 * * The permitted lock order is: IN_MULTI_LIST_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
168 * Any may be taken independently; if any are held at the same
169 * time, the above lock order must be followed.
170 * * All output is delegated to the netisr.
171 * * IN_MULTI_LIST_LOCK covers in_multi.
172 * * IGMP_LOCK covers igmp_ifsoftc and any global variables in this file,
173 * including the output queue.
174 * * IF_ADDR_LOCK covers if_multiaddrs, which is used for a variety of
175 * per-link state iterators.
176 * * igmp_ifsoftc is valid as long as PF_INET is attached to the interface,
177 * therefore it is not refcounted.
178 * We allow unlocked reads of igmp_ifsoftc when accessed via in_multi.
179 *
180 * Reference counting
181 * * IGMP acquires its own reference every time an in_multi is passed to
182 * it and the group is being joined for the first time.
183 * * IGMP releases its reference(s) on in_multi in a deferred way,
184 * because the operations which process the release run as part of
185 * a loop whose control variables are directly affected by the release
186 * (that, and not recursing on the IF_ADDR_LOCK).
187 *
188 * VIMAGE: Each in_multi corresponds to an ifp, and each ifp corresponds
189 * to a vnet in ifp->if_vnet.
190 *
191 * SMPng: XXX We may potentially race operations on ifma_protospec.
192 * The problem is that we currently lack a clean way of taking the
193 * IF_ADDR_LOCK() between the ifnet and in layers w/o recursing,
194 * as anything which modifies ifma needs to be covered by that lock.
195 * So check for ifma_protospec being NULL before proceeding.
196 */
197 struct mtx igmp_mtx;
198
199 struct mbuf *m_raopt; /* Router Alert option */
200 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
201
202 /*
203 * VIMAGE-wide globals.
204 *
205 * The IGMPv3 timers themselves need to run per-image, however, for
206 * historical reasons, timers run globally. This needs to be improved.
207 * An ifnet can only be in one vimage at a time, and the loopback
208 * ifnet, loif, is itself virtualized.
209 * It would otherwise be possible to seriously hose IGMP state,
210 * and create inconsistencies in upstream multicast routing, if you have
211 * multiple VIMAGEs running on the same link joining different multicast
212 * groups, UNLESS the "primary IP address" is different. This is because
213 * IGMP for IPv4 does not force link-local addresses to be used for each
214 * node, unlike MLD for IPv6.
215 * Obviously the IGMPv3 per-interface state has per-vimage granularity
216 * also as a result.
217 *
218 * FUTURE: Stop using IFP_TO_IA/INADDR_ANY, and use source address selection
219 * policy to control the address used by IGMP on the link.
220 */
221 VNET_DEFINE_STATIC(int, interface_timers_running); /* IGMPv3 general
222 * query response */
223 VNET_DEFINE_STATIC(int, state_change_timers_running); /* IGMPv3 state-change
224 * retransmit */
225 VNET_DEFINE_STATIC(int, current_state_timers_running); /* IGMPv1/v2 host
226 * report; IGMPv3 g/sg
227 * query response */
228
229 #define V_interface_timers_running VNET(interface_timers_running)
230 #define V_state_change_timers_running VNET(state_change_timers_running)
231 #define V_current_state_timers_running VNET(current_state_timers_running)
232
233 VNET_PCPUSTAT_DEFINE(struct igmpstat, igmpstat);
234 VNET_PCPUSTAT_SYSINIT(igmpstat);
235 VNET_PCPUSTAT_SYSUNINIT(igmpstat);
236
237 VNET_DEFINE_STATIC(LIST_HEAD(, igmp_ifsoftc), igi_head) =
238 LIST_HEAD_INITIALIZER(igi_head);
239 VNET_DEFINE_STATIC(struct timeval, igmp_gsrdelay) = {10, 0};
240
241 #define V_igi_head VNET(igi_head)
242 #define V_igmp_gsrdelay VNET(igmp_gsrdelay)
243
244 VNET_DEFINE_STATIC(int, igmp_recvifkludge) = 1;
245 VNET_DEFINE_STATIC(int, igmp_sendra) = 1;
246 VNET_DEFINE_STATIC(int, igmp_sendlocal) = 1;
247 VNET_DEFINE_STATIC(int, igmp_v1enable) = 1;
248 VNET_DEFINE_STATIC(int, igmp_v2enable) = 1;
249 VNET_DEFINE_STATIC(int, igmp_legacysupp);
250 VNET_DEFINE_STATIC(int, igmp_default_version) = IGMP_VERSION_3;
251
252 #define V_igmp_recvifkludge VNET(igmp_recvifkludge)
253 #define V_igmp_sendra VNET(igmp_sendra)
254 #define V_igmp_sendlocal VNET(igmp_sendlocal)
255 #define V_igmp_v1enable VNET(igmp_v1enable)
256 #define V_igmp_v2enable VNET(igmp_v2enable)
257 #define V_igmp_legacysupp VNET(igmp_legacysupp)
258 #define V_igmp_default_version VNET(igmp_default_version)
259
260 /*
261 * Virtualized sysctls.
262 */
263 SYSCTL_PROC(_net_inet_igmp, IGMPCTL_STATS, stats,
264 CTLFLAG_VNET | CTLTYPE_STRUCT | CTLFLAG_RW | CTLFLAG_MPSAFE,
265 &VNET_NAME(igmpstat), 0, sysctl_igmp_stat, "S,igmpstat",
266 "IGMP statistics (struct igmpstat, netinet/igmp_var.h)");
267 SYSCTL_INT(_net_inet_igmp, OID_AUTO, recvifkludge, CTLFLAG_VNET | CTLFLAG_RW,
268 &VNET_NAME(igmp_recvifkludge), 0,
269 "Rewrite IGMPv1/v2 reports from 0.0.0.0 to contain subnet address");
270 SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendra, CTLFLAG_VNET | CTLFLAG_RW,
271 &VNET_NAME(igmp_sendra), 0,
272 "Send IP Router Alert option in IGMPv2/v3 messages");
273 SYSCTL_INT(_net_inet_igmp, OID_AUTO, sendlocal, CTLFLAG_VNET | CTLFLAG_RW,
274 &VNET_NAME(igmp_sendlocal), 0,
275 "Send IGMP membership reports for 224.0.0.0/24 groups");
276 SYSCTL_INT(_net_inet_igmp, OID_AUTO, v1enable, CTLFLAG_VNET | CTLFLAG_RW,
277 &VNET_NAME(igmp_v1enable), 0,
278 "Enable backwards compatibility with IGMPv1");
279 SYSCTL_INT(_net_inet_igmp, OID_AUTO, v2enable, CTLFLAG_VNET | CTLFLAG_RW,
280 &VNET_NAME(igmp_v2enable), 0,
281 "Enable backwards compatibility with IGMPv2");
282 SYSCTL_INT(_net_inet_igmp, OID_AUTO, legacysupp, CTLFLAG_VNET | CTLFLAG_RW,
283 &VNET_NAME(igmp_legacysupp), 0,
284 "Allow v1/v2 reports to suppress v3 group responses");
285 SYSCTL_PROC(_net_inet_igmp, OID_AUTO, default_version,
286 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
287 &VNET_NAME(igmp_default_version), 0, sysctl_igmp_default_version, "I",
288 "Default version of IGMP to run on each interface");
289 SYSCTL_PROC(_net_inet_igmp, OID_AUTO, gsrdelay,
290 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
291 &VNET_NAME(igmp_gsrdelay.tv_sec), 0, sysctl_igmp_gsr, "I",
292 "Rate limit for IGMPv3 Group-and-Source queries in seconds");
293
294 /*
295 * Non-virtualized sysctls.
296 */
297 static SYSCTL_NODE(_net_inet_igmp, OID_AUTO, ifinfo,
298 CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_igmp_ifinfo,
299 "Per-interface IGMPv3 state");
300
301 static __inline void
igmp_save_context(struct mbuf * m,struct ifnet * ifp)302 igmp_save_context(struct mbuf *m, struct ifnet *ifp)
303 {
304
305 #ifdef VIMAGE
306 m->m_pkthdr.PH_loc.ptr = ifp->if_vnet;
307 #endif /* VIMAGE */
308 m->m_pkthdr.rcvif = ifp;
309 m->m_pkthdr.flowid = ifp->if_index;
310 }
311
312 static __inline void
igmp_scrub_context(struct mbuf * m)313 igmp_scrub_context(struct mbuf *m)
314 {
315
316 m->m_pkthdr.PH_loc.ptr = NULL;
317 m->m_pkthdr.flowid = 0;
318 }
319
320 /*
321 * Restore context from a queued IGMP output chain.
322 * Return saved ifindex.
323 *
324 * VIMAGE: The assertion is there to make sure that we
325 * actually called CURVNET_SET() with what's in the mbuf chain.
326 */
327 static __inline uint32_t
igmp_restore_context(struct mbuf * m)328 igmp_restore_context(struct mbuf *m)
329 {
330
331 #ifdef notyet
332 #if defined(VIMAGE) && defined(INVARIANTS)
333 KASSERT(curvnet == (m->m_pkthdr.PH_loc.ptr),
334 ("%s: called when curvnet was not restored", __func__));
335 #endif
336 #endif
337 return (m->m_pkthdr.flowid);
338 }
339
340 /*
341 * IGMP statistics.
342 */
343 static int
sysctl_igmp_stat(SYSCTL_HANDLER_ARGS)344 sysctl_igmp_stat(SYSCTL_HANDLER_ARGS)
345 {
346 struct igmpstat igps0;
347 int error;
348 char *p;
349
350 error = sysctl_wire_old_buffer(req, sizeof(struct igmpstat));
351 if (error)
352 return (error);
353
354 if (req->oldptr != NULL) {
355 if (req->oldlen < sizeof(struct igmpstat))
356 error = ENOMEM;
357 else {
358 /*
359 * Copy the counters, and explicitly set the struct's
360 * version and length fields.
361 */
362 COUNTER_ARRAY_COPY(VNET(igmpstat), &igps0,
363 sizeof(struct igmpstat) / sizeof(uint64_t));
364 igps0.igps_version = IGPS_VERSION_3;
365 igps0.igps_len = IGPS_VERSION3_LEN;
366 error = SYSCTL_OUT(req, &igps0,
367 sizeof(struct igmpstat));
368 }
369 } else
370 req->validlen = sizeof(struct igmpstat);
371 if (error)
372 goto out;
373 if (req->newptr != NULL) {
374 if (req->newlen < sizeof(struct igmpstat))
375 error = ENOMEM;
376 else
377 error = SYSCTL_IN(req, &igps0,
378 sizeof(igps0));
379 if (error)
380 goto out;
381 /*
382 * igps0 must be "all zero".
383 */
384 p = (char *)&igps0;
385 while (p < (char *)&igps0 + sizeof(igps0) && *p == '\0')
386 p++;
387 if (p != (char *)&igps0 + sizeof(igps0)) {
388 error = EINVAL;
389 goto out;
390 }
391 COUNTER_ARRAY_ZERO(VNET(igmpstat),
392 sizeof(struct igmpstat) / sizeof(uint64_t));
393 }
394 out:
395 return (error);
396 }
397
398 /*
399 * Retrieve or set default IGMP version.
400 *
401 * VIMAGE: Assume curvnet set by caller.
402 * SMPng: NOTE: Serialized by IGMP lock.
403 */
404 static int
sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)405 sysctl_igmp_default_version(SYSCTL_HANDLER_ARGS)
406 {
407 int error;
408 int new;
409
410 error = sysctl_wire_old_buffer(req, sizeof(int));
411 if (error)
412 return (error);
413
414 IGMP_LOCK();
415
416 new = V_igmp_default_version;
417
418 error = sysctl_handle_int(oidp, &new, 0, req);
419 if (error || !req->newptr)
420 goto out_locked;
421
422 if (new < IGMP_VERSION_1 || new > IGMP_VERSION_3) {
423 error = EINVAL;
424 goto out_locked;
425 }
426
427 CTR2(KTR_IGMPV3, "change igmp_default_version from %d to %d",
428 V_igmp_default_version, new);
429
430 V_igmp_default_version = new;
431
432 out_locked:
433 IGMP_UNLOCK();
434 return (error);
435 }
436
437 /*
438 * Retrieve or set threshold between group-source queries in seconds.
439 *
440 * VIMAGE: Assume curvnet set by caller.
441 * SMPng: NOTE: Serialized by IGMP lock.
442 */
443 static int
sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)444 sysctl_igmp_gsr(SYSCTL_HANDLER_ARGS)
445 {
446 int error;
447 int i;
448
449 error = sysctl_wire_old_buffer(req, sizeof(int));
450 if (error)
451 return (error);
452
453 IGMP_LOCK();
454
455 i = V_igmp_gsrdelay.tv_sec;
456
457 error = sysctl_handle_int(oidp, &i, 0, req);
458 if (error || !req->newptr)
459 goto out_locked;
460
461 if (i < -1 || i >= 60) {
462 error = EINVAL;
463 goto out_locked;
464 }
465
466 CTR2(KTR_IGMPV3, "change igmp_gsrdelay from %d to %d",
467 V_igmp_gsrdelay.tv_sec, i);
468 V_igmp_gsrdelay.tv_sec = i;
469
470 out_locked:
471 IGMP_UNLOCK();
472 return (error);
473 }
474
475 /*
476 * Expose struct igmp_ifsoftc to userland, keyed by ifindex.
477 * For use by ifmcstat(8).
478 *
479 * SMPng: NOTE: Does an unlocked ifindex space read.
480 * VIMAGE: Assume curvnet set by caller. The node handler itself
481 * is not directly virtualized.
482 */
483 static int
sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)484 sysctl_igmp_ifinfo(SYSCTL_HANDLER_ARGS)
485 {
486 struct epoch_tracker et;
487 int *name;
488 int error;
489 u_int namelen;
490 struct ifnet *ifp;
491 struct igmp_ifsoftc *igi;
492
493 name = (int *)arg1;
494 namelen = arg2;
495
496 if (req->newptr != NULL)
497 return (EPERM);
498
499 if (namelen != 1)
500 return (EINVAL);
501
502 error = sysctl_wire_old_buffer(req, sizeof(struct igmp_ifinfo));
503 if (error)
504 return (error);
505
506 IN_MULTI_LIST_LOCK();
507 IGMP_LOCK();
508
509 error = ENOENT;
510
511 NET_EPOCH_ENTER(et);
512 ifp = ifnet_byindex(name[0]);
513 NET_EPOCH_EXIT(et);
514 if (ifp == NULL)
515 goto out_locked;
516
517 LIST_FOREACH(igi, &V_igi_head, igi_link) {
518 if (ifp == igi->igi_ifp) {
519 struct igmp_ifinfo info;
520
521 info.igi_version = igi->igi_version;
522 info.igi_v1_timer = igi->igi_v1_timer;
523 info.igi_v2_timer = igi->igi_v2_timer;
524 info.igi_v3_timer = igi->igi_v3_timer;
525 info.igi_flags = igi->igi_flags;
526 info.igi_rv = igi->igi_rv;
527 info.igi_qi = igi->igi_qi;
528 info.igi_qri = igi->igi_qri;
529 info.igi_uri = igi->igi_uri;
530 error = SYSCTL_OUT(req, &info, sizeof(info));
531 break;
532 }
533 }
534
535 out_locked:
536 IGMP_UNLOCK();
537 IN_MULTI_LIST_UNLOCK();
538 return (error);
539 }
540
541 /*
542 * Dispatch an entire queue of pending packet chains
543 * using the netisr.
544 * VIMAGE: Assumes the vnet pointer has been set.
545 */
546 static void
igmp_dispatch_queue(struct mbufq * mq,int limit,const int loop)547 igmp_dispatch_queue(struct mbufq *mq, int limit, const int loop)
548 {
549 struct epoch_tracker et;
550 struct mbuf *m;
551
552 NET_EPOCH_ENTER(et);
553 while ((m = mbufq_dequeue(mq)) != NULL) {
554 CTR3(KTR_IGMPV3, "%s: dispatch %p from %p", __func__, mq, m);
555 if (loop)
556 m->m_flags |= M_IGMP_LOOP;
557 netisr_dispatch(NETISR_IGMP, m);
558 if (--limit == 0)
559 break;
560 }
561 NET_EPOCH_EXIT(et);
562 }
563
564 /*
565 * Filter outgoing IGMP report state by group.
566 *
567 * Reports are ALWAYS suppressed for ALL-HOSTS (224.0.0.1).
568 * If the net.inet.igmp.sendlocal sysctl is 0, then IGMP reports are
569 * disabled for all groups in the 224.0.0.0/24 link-local scope. However,
570 * this may break certain IGMP snooping switches which rely on the old
571 * report behaviour.
572 *
573 * Return zero if the given group is one for which IGMP reports
574 * should be suppressed, or non-zero if reports should be issued.
575 */
576 static __inline int
igmp_isgroupreported(const struct in_addr addr)577 igmp_isgroupreported(const struct in_addr addr)
578 {
579
580 if (in_allhosts(addr) ||
581 ((!V_igmp_sendlocal && IN_LOCAL_GROUP(ntohl(addr.s_addr)))))
582 return (0);
583
584 return (1);
585 }
586
587 /*
588 * Construct a Router Alert option to use in outgoing packets.
589 */
590 static struct mbuf *
igmp_ra_alloc(void)591 igmp_ra_alloc(void)
592 {
593 struct mbuf *m;
594 struct ipoption *p;
595
596 m = m_get(M_WAITOK, MT_DATA);
597 p = mtod(m, struct ipoption *);
598 p->ipopt_dst.s_addr = INADDR_ANY;
599 p->ipopt_list[0] = (char)IPOPT_RA; /* Router Alert Option */
600 p->ipopt_list[1] = 0x04; /* 4 bytes long */
601 p->ipopt_list[2] = IPOPT_EOL; /* End of IP option list */
602 p->ipopt_list[3] = 0x00; /* pad byte */
603 m->m_len = sizeof(p->ipopt_dst) + p->ipopt_list[1];
604
605 return (m);
606 }
607
608 /*
609 * Attach IGMP when PF_INET is attached to an interface.
610 */
611 struct igmp_ifsoftc *
igmp_domifattach(struct ifnet * ifp)612 igmp_domifattach(struct ifnet *ifp)
613 {
614 struct igmp_ifsoftc *igi;
615
616 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
617 __func__, ifp, ifp->if_xname);
618
619 IGMP_LOCK();
620
621 igi = igi_alloc_locked(ifp);
622 if (!(ifp->if_flags & IFF_MULTICAST))
623 igi->igi_flags |= IGIF_SILENT;
624
625 IGMP_UNLOCK();
626
627 return (igi);
628 }
629
630 /*
631 * VIMAGE: assume curvnet set by caller.
632 */
633 static struct igmp_ifsoftc *
igi_alloc_locked(struct ifnet * ifp)634 igi_alloc_locked(/*const*/ struct ifnet *ifp)
635 {
636 struct igmp_ifsoftc *igi;
637
638 IGMP_LOCK_ASSERT();
639
640 igi = malloc(sizeof(struct igmp_ifsoftc), M_IGMP, M_NOWAIT|M_ZERO);
641 if (igi == NULL)
642 goto out;
643
644 igi->igi_ifp = ifp;
645 igi->igi_version = V_igmp_default_version;
646 igi->igi_flags = 0;
647 igi->igi_rv = IGMP_RV_INIT;
648 igi->igi_qi = IGMP_QI_INIT;
649 igi->igi_qri = IGMP_QRI_INIT;
650 igi->igi_uri = IGMP_URI_INIT;
651 mbufq_init(&igi->igi_gq, IGMP_MAX_RESPONSE_PACKETS);
652
653 LIST_INSERT_HEAD(&V_igi_head, igi, igi_link);
654
655 CTR2(KTR_IGMPV3, "allocate igmp_ifsoftc for ifp %p(%s)",
656 ifp, ifp->if_xname);
657
658 out:
659 return (igi);
660 }
661
662 /*
663 * Hook for ifdetach.
664 *
665 * NOTE: Some finalization tasks need to run before the protocol domain
666 * is detached, but also before the link layer does its cleanup.
667 *
668 * SMPNG: igmp_ifdetach() needs to take IF_ADDR_LOCK().
669 * XXX This is also bitten by unlocked ifma_protospec access.
670 */
671 void
igmp_ifdetach(struct ifnet * ifp)672 igmp_ifdetach(struct ifnet *ifp)
673 {
674 struct epoch_tracker et;
675 struct igmp_ifsoftc *igi;
676 struct ifmultiaddr *ifma;
677 struct in_multi *inm;
678 struct in_multi_head inm_free_tmp;
679 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)", __func__, ifp,
680 ifp->if_xname);
681
682 SLIST_INIT(&inm_free_tmp);
683 IGMP_LOCK();
684
685 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
686 if (igi->igi_version == IGMP_VERSION_3) {
687 IF_ADDR_WLOCK(ifp);
688 NET_EPOCH_ENTER(et);
689 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
690 inm = inm_ifmultiaddr_get_inm(ifma);
691 if (inm == NULL)
692 continue;
693 if (inm->inm_state == IGMP_LEAVING_MEMBER)
694 inm_rele_locked(&inm_free_tmp, inm);
695 inm_clear_recorded(inm);
696 }
697 NET_EPOCH_EXIT(et);
698 IF_ADDR_WUNLOCK(ifp);
699 inm_release_list_deferred(&inm_free_tmp);
700 }
701 IGMP_UNLOCK();
702
703 }
704
705 /*
706 * Hook for domifdetach.
707 */
708 void
igmp_domifdetach(struct ifnet * ifp)709 igmp_domifdetach(struct ifnet *ifp)
710 {
711
712 CTR3(KTR_IGMPV3, "%s: called for ifp %p(%s)",
713 __func__, ifp, ifp->if_xname);
714
715 IGMP_LOCK();
716 igi_delete_locked(ifp);
717 IGMP_UNLOCK();
718 }
719
720 static void
igi_delete_locked(const struct ifnet * ifp)721 igi_delete_locked(const struct ifnet *ifp)
722 {
723 struct igmp_ifsoftc *igi, *tigi;
724
725 CTR3(KTR_IGMPV3, "%s: freeing igmp_ifsoftc for ifp %p(%s)",
726 __func__, ifp, ifp->if_xname);
727
728 IGMP_LOCK_ASSERT();
729
730 LIST_FOREACH_SAFE(igi, &V_igi_head, igi_link, tigi) {
731 if (igi->igi_ifp == ifp) {
732 /*
733 * Free deferred General Query responses.
734 */
735 mbufq_drain(&igi->igi_gq);
736
737 LIST_REMOVE(igi, igi_link);
738 free(igi, M_IGMP);
739 return;
740 }
741 }
742 }
743
744 /*
745 * Process a received IGMPv1 query.
746 * Return non-zero if the message should be dropped.
747 *
748 * VIMAGE: The curvnet pointer is derived from the input ifp.
749 */
750 static int
igmp_input_v1_query(struct ifnet * ifp,const struct ip * ip,const struct igmp * igmp)751 igmp_input_v1_query(struct ifnet *ifp, const struct ip *ip,
752 const struct igmp *igmp)
753 {
754 struct ifmultiaddr *ifma;
755 struct igmp_ifsoftc *igi;
756 struct in_multi *inm;
757
758 NET_EPOCH_ASSERT();
759
760 /*
761 * IGMPv1 Host Mmembership Queries SHOULD always be addressed to
762 * 224.0.0.1. They are always treated as General Queries.
763 * igmp_group is always ignored. Do not drop it as a userland
764 * daemon may wish to see it.
765 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
766 */
767 if (!in_allhosts(ip->ip_dst) || !in_nullhost(igmp->igmp_group)) {
768 IGMPSTAT_INC(igps_rcv_badqueries);
769 return (0);
770 }
771 IGMPSTAT_INC(igps_rcv_gen_queries);
772
773 IN_MULTI_LIST_LOCK();
774 IGMP_LOCK();
775
776 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
777 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
778
779 if (igi->igi_flags & IGIF_LOOPBACK) {
780 CTR2(KTR_IGMPV3, "ignore v1 query on IGIF_LOOPBACK ifp %p(%s)",
781 ifp, ifp->if_xname);
782 goto out_locked;
783 }
784
785 /*
786 * Switch to IGMPv1 host compatibility mode.
787 */
788 igmp_set_version(igi, IGMP_VERSION_1);
789
790 CTR2(KTR_IGMPV3, "process v1 query on ifp %p(%s)", ifp, ifp->if_xname);
791
792 /*
793 * Start the timers in all of our group records
794 * for the interface on which the query arrived,
795 * except those which are already running.
796 */
797 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
798 inm = inm_ifmultiaddr_get_inm(ifma);
799 if (inm == NULL)
800 continue;
801 if (inm->inm_timer != 0)
802 continue;
803 switch (inm->inm_state) {
804 case IGMP_NOT_MEMBER:
805 case IGMP_SILENT_MEMBER:
806 break;
807 case IGMP_G_QUERY_PENDING_MEMBER:
808 case IGMP_SG_QUERY_PENDING_MEMBER:
809 case IGMP_REPORTING_MEMBER:
810 case IGMP_IDLE_MEMBER:
811 case IGMP_LAZY_MEMBER:
812 case IGMP_SLEEPING_MEMBER:
813 case IGMP_AWAKENING_MEMBER:
814 inm->inm_state = IGMP_REPORTING_MEMBER;
815 inm->inm_timer = IGMP_RANDOM_DELAY(
816 IGMP_V1V2_MAX_RI * IGMP_FASTHZ);
817 V_current_state_timers_running = 1;
818 break;
819 case IGMP_LEAVING_MEMBER:
820 break;
821 }
822 }
823
824 out_locked:
825 IGMP_UNLOCK();
826 IN_MULTI_LIST_UNLOCK();
827
828 return (0);
829 }
830
831 /*
832 * Process a received IGMPv2 general or group-specific query.
833 */
834 static int
igmp_input_v2_query(struct ifnet * ifp,const struct ip * ip,const struct igmp * igmp)835 igmp_input_v2_query(struct ifnet *ifp, const struct ip *ip,
836 const struct igmp *igmp)
837 {
838 struct ifmultiaddr *ifma;
839 struct igmp_ifsoftc *igi;
840 struct in_multi *inm;
841 int is_general_query;
842 uint16_t timer;
843
844 NET_EPOCH_ASSERT();
845
846 is_general_query = 0;
847
848 /*
849 * Validate address fields upfront.
850 * XXX SMPng: unlocked increments in igmpstat assumed atomic.
851 */
852 if (in_nullhost(igmp->igmp_group)) {
853 /*
854 * IGMPv2 General Query.
855 * If this was not sent to the all-hosts group, ignore it.
856 */
857 if (!in_allhosts(ip->ip_dst))
858 return (0);
859 IGMPSTAT_INC(igps_rcv_gen_queries);
860 is_general_query = 1;
861 } else {
862 /* IGMPv2 Group-Specific Query. */
863 IGMPSTAT_INC(igps_rcv_group_queries);
864 }
865
866 IN_MULTI_LIST_LOCK();
867 IGMP_LOCK();
868
869 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
870 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
871
872 if (igi->igi_flags & IGIF_LOOPBACK) {
873 CTR2(KTR_IGMPV3, "ignore v2 query on IGIF_LOOPBACK ifp %p(%s)",
874 ifp, ifp->if_xname);
875 goto out_locked;
876 }
877
878 /*
879 * Ignore v2 query if in v1 Compatibility Mode.
880 */
881 if (igi->igi_version == IGMP_VERSION_1)
882 goto out_locked;
883
884 igmp_set_version(igi, IGMP_VERSION_2);
885
886 timer = igmp->igmp_code * IGMP_FASTHZ / IGMP_TIMER_SCALE;
887 if (timer == 0)
888 timer = 1;
889
890 if (is_general_query) {
891 /*
892 * For each reporting group joined on this
893 * interface, kick the report timer.
894 */
895 CTR2(KTR_IGMPV3, "process v2 general query on ifp %p(%s)",
896 ifp, ifp->if_xname);
897 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
898 inm = inm_ifmultiaddr_get_inm(ifma);
899 if (inm == NULL)
900 continue;
901 igmp_v2_update_group(inm, timer);
902 }
903 } else {
904 /*
905 * Group-specific IGMPv2 query, we need only
906 * look up the single group to process it.
907 */
908 inm = inm_lookup(ifp, igmp->igmp_group);
909 if (inm != NULL) {
910 CTR3(KTR_IGMPV3,
911 "process v2 query 0x%08x on ifp %p(%s)",
912 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
913 igmp_v2_update_group(inm, timer);
914 }
915 }
916
917 out_locked:
918 IGMP_UNLOCK();
919 IN_MULTI_LIST_UNLOCK();
920
921 return (0);
922 }
923
924 /*
925 * Update the report timer on a group in response to an IGMPv2 query.
926 *
927 * If we are becoming the reporting member for this group, start the timer.
928 * If we already are the reporting member for this group, and timer is
929 * below the threshold, reset it.
930 *
931 * We may be updating the group for the first time since we switched
932 * to IGMPv3. If we are, then we must clear any recorded source lists,
933 * and transition to REPORTING state; the group timer is overloaded
934 * for group and group-source query responses.
935 *
936 * Unlike IGMPv3, the delay per group should be jittered
937 * to avoid bursts of IGMPv2 reports.
938 */
939 static void
igmp_v2_update_group(struct in_multi * inm,const int timer)940 igmp_v2_update_group(struct in_multi *inm, const int timer)
941 {
942
943 CTR4(KTR_IGMPV3, "0x%08x: %s/%s timer=%d", __func__,
944 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname, timer);
945
946 IN_MULTI_LIST_LOCK_ASSERT();
947
948 switch (inm->inm_state) {
949 case IGMP_NOT_MEMBER:
950 case IGMP_SILENT_MEMBER:
951 break;
952 case IGMP_REPORTING_MEMBER:
953 if (inm->inm_timer != 0 &&
954 inm->inm_timer <= timer) {
955 CTR1(KTR_IGMPV3, "%s: REPORTING and timer running, "
956 "skipping.", __func__);
957 break;
958 }
959 /* FALLTHROUGH */
960 case IGMP_SG_QUERY_PENDING_MEMBER:
961 case IGMP_G_QUERY_PENDING_MEMBER:
962 case IGMP_IDLE_MEMBER:
963 case IGMP_LAZY_MEMBER:
964 case IGMP_AWAKENING_MEMBER:
965 CTR1(KTR_IGMPV3, "%s: ->REPORTING", __func__);
966 inm->inm_state = IGMP_REPORTING_MEMBER;
967 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
968 V_current_state_timers_running = 1;
969 break;
970 case IGMP_SLEEPING_MEMBER:
971 CTR1(KTR_IGMPV3, "%s: ->AWAKENING", __func__);
972 inm->inm_state = IGMP_AWAKENING_MEMBER;
973 break;
974 case IGMP_LEAVING_MEMBER:
975 break;
976 }
977 }
978
979 /*
980 * Process a received IGMPv3 general, group-specific or
981 * group-and-source-specific query.
982 * Assumes m has already been pulled up to the full IGMP message length.
983 * Return 0 if successful, otherwise an appropriate error code is returned.
984 */
985 static int
igmp_input_v3_query(struct ifnet * ifp,const struct ip * ip,struct igmpv3 * igmpv3)986 igmp_input_v3_query(struct ifnet *ifp, const struct ip *ip,
987 /*const*/ struct igmpv3 *igmpv3)
988 {
989 struct igmp_ifsoftc *igi;
990 struct in_multi *inm;
991 int is_general_query;
992 uint32_t maxresp, nsrc, qqi;
993 uint16_t timer;
994 uint8_t qrv;
995
996 is_general_query = 0;
997
998 CTR2(KTR_IGMPV3, "process v3 query on ifp %p(%s)", ifp, ifp->if_xname);
999
1000 maxresp = igmpv3->igmp_code; /* in 1/10ths of a second */
1001 if (maxresp >= 128) {
1002 maxresp = IGMP_MANT(igmpv3->igmp_code) <<
1003 (IGMP_EXP(igmpv3->igmp_code) + 3);
1004 }
1005
1006 /*
1007 * Robustness must never be less than 2 for on-wire IGMPv3.
1008 * FUTURE: Check if ifp has IGIF_LOOPBACK set, as we will make
1009 * an exception for interfaces whose IGMPv3 state changes
1010 * are redirected to loopback (e.g. MANET).
1011 */
1012 qrv = IGMP_QRV(igmpv3->igmp_misc);
1013 if (qrv < 2) {
1014 CTR3(KTR_IGMPV3, "%s: clamping qrv %d to %d", __func__,
1015 qrv, IGMP_RV_INIT);
1016 qrv = IGMP_RV_INIT;
1017 }
1018
1019 qqi = igmpv3->igmp_qqi;
1020 if (qqi >= 128) {
1021 qqi = IGMP_MANT(igmpv3->igmp_qqi) <<
1022 (IGMP_EXP(igmpv3->igmp_qqi) + 3);
1023 }
1024
1025 timer = maxresp * IGMP_FASTHZ / IGMP_TIMER_SCALE;
1026 if (timer == 0)
1027 timer = 1;
1028
1029 nsrc = ntohs(igmpv3->igmp_numsrc);
1030
1031 /*
1032 * Validate address fields and versions upfront before
1033 * accepting v3 query.
1034 * XXX SMPng: Unlocked access to igmpstat counters here.
1035 */
1036 if (in_nullhost(igmpv3->igmp_group)) {
1037 /*
1038 * IGMPv3 General Query.
1039 *
1040 * General Queries SHOULD be directed to 224.0.0.1.
1041 * A general query with a source list has undefined
1042 * behaviour; discard it.
1043 */
1044 IGMPSTAT_INC(igps_rcv_gen_queries);
1045 if (!in_allhosts(ip->ip_dst) || nsrc > 0) {
1046 IGMPSTAT_INC(igps_rcv_badqueries);
1047 return (0);
1048 }
1049 is_general_query = 1;
1050 } else {
1051 /* Group or group-source specific query. */
1052 if (nsrc == 0)
1053 IGMPSTAT_INC(igps_rcv_group_queries);
1054 else
1055 IGMPSTAT_INC(igps_rcv_gsr_queries);
1056 }
1057
1058 IN_MULTI_LIST_LOCK();
1059 IGMP_LOCK();
1060
1061 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
1062 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
1063
1064 if (igi->igi_flags & IGIF_LOOPBACK) {
1065 CTR2(KTR_IGMPV3, "ignore v3 query on IGIF_LOOPBACK ifp %p(%s)",
1066 ifp, ifp->if_xname);
1067 goto out_locked;
1068 }
1069
1070 /*
1071 * Discard the v3 query if we're in Compatibility Mode.
1072 * The RFC is not obviously worded that hosts need to stay in
1073 * compatibility mode until the Old Version Querier Present
1074 * timer expires.
1075 */
1076 if (igi->igi_version != IGMP_VERSION_3) {
1077 CTR3(KTR_IGMPV3, "ignore v3 query in v%d mode on ifp %p(%s)",
1078 igi->igi_version, ifp, ifp->if_xname);
1079 goto out_locked;
1080 }
1081
1082 igmp_set_version(igi, IGMP_VERSION_3);
1083 igi->igi_rv = qrv;
1084 igi->igi_qi = qqi;
1085 igi->igi_qri = maxresp;
1086
1087 CTR4(KTR_IGMPV3, "%s: qrv %d qi %d qri %d", __func__, qrv, qqi,
1088 maxresp);
1089
1090 if (is_general_query) {
1091 /*
1092 * Schedule a current-state report on this ifp for
1093 * all groups, possibly containing source lists.
1094 * If there is a pending General Query response
1095 * scheduled earlier than the selected delay, do
1096 * not schedule any other reports.
1097 * Otherwise, reset the interface timer.
1098 */
1099 CTR2(KTR_IGMPV3, "process v3 general query on ifp %p(%s)",
1100 ifp, ifp->if_xname);
1101 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer) {
1102 igi->igi_v3_timer = IGMP_RANDOM_DELAY(timer);
1103 V_interface_timers_running = 1;
1104 }
1105 } else {
1106 /*
1107 * Group-source-specific queries are throttled on
1108 * a per-group basis to defeat denial-of-service attempts.
1109 * Queries for groups we are not a member of on this
1110 * link are simply ignored.
1111 */
1112 inm = inm_lookup(ifp, igmpv3->igmp_group);
1113 if (inm == NULL)
1114 goto out_locked;
1115 if (nsrc > 0) {
1116 if (!ratecheck(&inm->inm_lastgsrtv,
1117 &V_igmp_gsrdelay)) {
1118 CTR1(KTR_IGMPV3, "%s: GS query throttled.",
1119 __func__);
1120 IGMPSTAT_INC(igps_drop_gsr_queries);
1121 goto out_locked;
1122 }
1123 }
1124 CTR3(KTR_IGMPV3, "process v3 0x%08x query on ifp %p(%s)",
1125 ntohl(igmpv3->igmp_group.s_addr), ifp, ifp->if_xname);
1126 /*
1127 * If there is a pending General Query response
1128 * scheduled sooner than the selected delay, no
1129 * further report need be scheduled.
1130 * Otherwise, prepare to respond to the
1131 * group-specific or group-and-source query.
1132 */
1133 if (igi->igi_v3_timer == 0 || igi->igi_v3_timer >= timer)
1134 igmp_input_v3_group_query(inm, igi, timer, igmpv3);
1135 }
1136
1137 out_locked:
1138 IGMP_UNLOCK();
1139 IN_MULTI_LIST_UNLOCK();
1140
1141 return (0);
1142 }
1143
1144 /*
1145 * Process a received IGMPv3 group-specific or group-and-source-specific
1146 * query.
1147 * Return <0 if any error occurred. Currently this is ignored.
1148 */
1149 static int
igmp_input_v3_group_query(struct in_multi * inm,struct igmp_ifsoftc * igi,int timer,struct igmpv3 * igmpv3)1150 igmp_input_v3_group_query(struct in_multi *inm, struct igmp_ifsoftc *igi,
1151 int timer, /*const*/ struct igmpv3 *igmpv3)
1152 {
1153 int retval;
1154 uint16_t nsrc;
1155
1156 IN_MULTI_LIST_LOCK_ASSERT();
1157 IGMP_LOCK_ASSERT();
1158
1159 retval = 0;
1160
1161 switch (inm->inm_state) {
1162 case IGMP_NOT_MEMBER:
1163 case IGMP_SILENT_MEMBER:
1164 case IGMP_SLEEPING_MEMBER:
1165 case IGMP_LAZY_MEMBER:
1166 case IGMP_AWAKENING_MEMBER:
1167 case IGMP_IDLE_MEMBER:
1168 case IGMP_LEAVING_MEMBER:
1169 return (retval);
1170 break;
1171 case IGMP_REPORTING_MEMBER:
1172 case IGMP_G_QUERY_PENDING_MEMBER:
1173 case IGMP_SG_QUERY_PENDING_MEMBER:
1174 break;
1175 }
1176
1177 nsrc = ntohs(igmpv3->igmp_numsrc);
1178
1179 /*
1180 * Deal with group-specific queries upfront.
1181 * If any group query is already pending, purge any recorded
1182 * source-list state if it exists, and schedule a query response
1183 * for this group-specific query.
1184 */
1185 if (nsrc == 0) {
1186 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
1187 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER) {
1188 inm_clear_recorded(inm);
1189 timer = min(inm->inm_timer, timer);
1190 }
1191 inm->inm_state = IGMP_G_QUERY_PENDING_MEMBER;
1192 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1193 V_current_state_timers_running = 1;
1194 return (retval);
1195 }
1196
1197 /*
1198 * Deal with the case where a group-and-source-specific query has
1199 * been received but a group-specific query is already pending.
1200 */
1201 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER) {
1202 timer = min(inm->inm_timer, timer);
1203 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1204 V_current_state_timers_running = 1;
1205 return (retval);
1206 }
1207
1208 /*
1209 * Finally, deal with the case where a group-and-source-specific
1210 * query has been received, where a response to a previous g-s-r
1211 * query exists, or none exists.
1212 * In this case, we need to parse the source-list which the Querier
1213 * has provided us with and check if we have any source list filter
1214 * entries at T1 for these sources. If we do not, there is no need
1215 * schedule a report and the query may be dropped.
1216 * If we do, we must record them and schedule a current-state
1217 * report for those sources.
1218 * FIXME: Handling source lists larger than 1 mbuf requires that
1219 * we pass the mbuf chain pointer down to this function, and use
1220 * m_getptr() to walk the chain.
1221 */
1222 if (inm->inm_nsrc > 0) {
1223 const struct in_addr *ap;
1224 int i, nrecorded;
1225
1226 ap = (const struct in_addr *)(igmpv3 + 1);
1227 nrecorded = 0;
1228 for (i = 0; i < nsrc; i++, ap++) {
1229 retval = inm_record_source(inm, ap->s_addr);
1230 if (retval < 0)
1231 break;
1232 nrecorded += retval;
1233 }
1234 if (nrecorded > 0) {
1235 CTR1(KTR_IGMPV3,
1236 "%s: schedule response to SG query", __func__);
1237 inm->inm_state = IGMP_SG_QUERY_PENDING_MEMBER;
1238 inm->inm_timer = IGMP_RANDOM_DELAY(timer);
1239 V_current_state_timers_running = 1;
1240 }
1241 }
1242
1243 return (retval);
1244 }
1245
1246 /*
1247 * Process a received IGMPv1 host membership report.
1248 *
1249 * NOTE: 0.0.0.0 workaround breaks const correctness.
1250 */
1251 static int
igmp_input_v1_report(struct ifnet * ifp,struct ip * ip,struct igmp * igmp)1252 igmp_input_v1_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1253 /*const*/ struct igmp *igmp)
1254 {
1255 struct in_ifaddr *ia;
1256 struct in_multi *inm;
1257
1258 IGMPSTAT_INC(igps_rcv_reports);
1259
1260 if (ifp->if_flags & IFF_LOOPBACK)
1261 return (0);
1262
1263 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1264 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1265 IGMPSTAT_INC(igps_rcv_badreports);
1266 return (EINVAL);
1267 }
1268
1269 /*
1270 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1271 * Booting clients may use the source address 0.0.0.0. Some
1272 * IGMP daemons may not know how to use IP_RECVIF to determine
1273 * the interface upon which this message was received.
1274 * Replace 0.0.0.0 with the subnet address if told to do so.
1275 */
1276 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1277 IFP_TO_IA(ifp, ia);
1278 if (ia != NULL)
1279 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1280 }
1281
1282 CTR3(KTR_IGMPV3, "process v1 report 0x%08x on ifp %p(%s)",
1283 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1284
1285 /*
1286 * IGMPv1 report suppression.
1287 * If we are a member of this group, and our membership should be
1288 * reported, stop our group timer and transition to the 'lazy' state.
1289 */
1290 IN_MULTI_LIST_LOCK();
1291 inm = inm_lookup(ifp, igmp->igmp_group);
1292 if (inm != NULL) {
1293 struct igmp_ifsoftc *igi;
1294
1295 igi = inm->inm_igi;
1296 if (igi == NULL) {
1297 KASSERT(igi != NULL,
1298 ("%s: no igi for ifp %p", __func__, ifp));
1299 goto out_locked;
1300 }
1301
1302 IGMPSTAT_INC(igps_rcv_ourreports);
1303
1304 /*
1305 * If we are in IGMPv3 host mode, do not allow the
1306 * other host's IGMPv1 report to suppress our reports
1307 * unless explicitly configured to do so.
1308 */
1309 if (igi->igi_version == IGMP_VERSION_3) {
1310 if (V_igmp_legacysupp)
1311 igmp_v3_suppress_group_record(inm);
1312 goto out_locked;
1313 }
1314
1315 inm->inm_timer = 0;
1316
1317 switch (inm->inm_state) {
1318 case IGMP_NOT_MEMBER:
1319 case IGMP_SILENT_MEMBER:
1320 break;
1321 case IGMP_IDLE_MEMBER:
1322 case IGMP_LAZY_MEMBER:
1323 case IGMP_AWAKENING_MEMBER:
1324 CTR3(KTR_IGMPV3,
1325 "report suppressed for 0x%08x on ifp %p(%s)",
1326 ntohl(igmp->igmp_group.s_addr), ifp,
1327 ifp->if_xname);
1328 case IGMP_SLEEPING_MEMBER:
1329 inm->inm_state = IGMP_SLEEPING_MEMBER;
1330 break;
1331 case IGMP_REPORTING_MEMBER:
1332 CTR3(KTR_IGMPV3,
1333 "report suppressed for 0x%08x on ifp %p(%s)",
1334 ntohl(igmp->igmp_group.s_addr), ifp,
1335 ifp->if_xname);
1336 if (igi->igi_version == IGMP_VERSION_1)
1337 inm->inm_state = IGMP_LAZY_MEMBER;
1338 else if (igi->igi_version == IGMP_VERSION_2)
1339 inm->inm_state = IGMP_SLEEPING_MEMBER;
1340 break;
1341 case IGMP_G_QUERY_PENDING_MEMBER:
1342 case IGMP_SG_QUERY_PENDING_MEMBER:
1343 case IGMP_LEAVING_MEMBER:
1344 break;
1345 }
1346 }
1347
1348 out_locked:
1349 IN_MULTI_LIST_UNLOCK();
1350
1351 return (0);
1352 }
1353
1354 /*
1355 * Process a received IGMPv2 host membership report.
1356 *
1357 * NOTE: 0.0.0.0 workaround breaks const correctness.
1358 */
1359 static int
igmp_input_v2_report(struct ifnet * ifp,struct ip * ip,struct igmp * igmp)1360 igmp_input_v2_report(struct ifnet *ifp, /*const*/ struct ip *ip,
1361 /*const*/ struct igmp *igmp)
1362 {
1363 struct in_ifaddr *ia;
1364 struct in_multi *inm;
1365
1366 /*
1367 * Make sure we don't hear our own membership report. Fast
1368 * leave requires knowing that we are the only member of a
1369 * group.
1370 */
1371 IFP_TO_IA(ifp, ia);
1372 if (ia != NULL && in_hosteq(ip->ip_src, IA_SIN(ia)->sin_addr)) {
1373 return (0);
1374 }
1375
1376 IGMPSTAT_INC(igps_rcv_reports);
1377
1378 if (ifp->if_flags & IFF_LOOPBACK) {
1379 return (0);
1380 }
1381
1382 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
1383 !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
1384 IGMPSTAT_INC(igps_rcv_badreports);
1385 return (EINVAL);
1386 }
1387
1388 /*
1389 * RFC 3376, Section 4.2.13, 9.2, 9.3:
1390 * Booting clients may use the source address 0.0.0.0. Some
1391 * IGMP daemons may not know how to use IP_RECVIF to determine
1392 * the interface upon which this message was received.
1393 * Replace 0.0.0.0 with the subnet address if told to do so.
1394 */
1395 if (V_igmp_recvifkludge && in_nullhost(ip->ip_src)) {
1396 if (ia != NULL)
1397 ip->ip_src.s_addr = htonl(ia->ia_subnet);
1398 }
1399
1400 CTR3(KTR_IGMPV3, "process v2 report 0x%08x on ifp %p(%s)",
1401 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1402
1403 /*
1404 * IGMPv2 report suppression.
1405 * If we are a member of this group, and our membership should be
1406 * reported, and our group timer is pending or about to be reset,
1407 * stop our group timer by transitioning to the 'lazy' state.
1408 */
1409 IN_MULTI_LIST_LOCK();
1410 inm = inm_lookup(ifp, igmp->igmp_group);
1411 if (inm != NULL) {
1412 struct igmp_ifsoftc *igi;
1413
1414 igi = inm->inm_igi;
1415 KASSERT(igi != NULL, ("%s: no igi for ifp %p", __func__, ifp));
1416
1417 IGMPSTAT_INC(igps_rcv_ourreports);
1418
1419 /*
1420 * If we are in IGMPv3 host mode, do not allow the
1421 * other host's IGMPv1 report to suppress our reports
1422 * unless explicitly configured to do so.
1423 */
1424 if (igi->igi_version == IGMP_VERSION_3) {
1425 if (V_igmp_legacysupp)
1426 igmp_v3_suppress_group_record(inm);
1427 goto out_locked;
1428 }
1429
1430 inm->inm_timer = 0;
1431
1432 switch (inm->inm_state) {
1433 case IGMP_NOT_MEMBER:
1434 case IGMP_SILENT_MEMBER:
1435 case IGMP_SLEEPING_MEMBER:
1436 break;
1437 case IGMP_REPORTING_MEMBER:
1438 case IGMP_IDLE_MEMBER:
1439 case IGMP_AWAKENING_MEMBER:
1440 CTR3(KTR_IGMPV3,
1441 "report suppressed for 0x%08x on ifp %p(%s)",
1442 ntohl(igmp->igmp_group.s_addr), ifp, ifp->if_xname);
1443 case IGMP_LAZY_MEMBER:
1444 inm->inm_state = IGMP_LAZY_MEMBER;
1445 break;
1446 case IGMP_G_QUERY_PENDING_MEMBER:
1447 case IGMP_SG_QUERY_PENDING_MEMBER:
1448 case IGMP_LEAVING_MEMBER:
1449 break;
1450 }
1451 }
1452
1453 out_locked:
1454 IN_MULTI_LIST_UNLOCK();
1455
1456 return (0);
1457 }
1458
1459 int
igmp_input(struct mbuf ** mp,int * offp,int proto)1460 igmp_input(struct mbuf **mp, int *offp, int proto)
1461 {
1462 int iphlen;
1463 struct ifnet *ifp;
1464 struct igmp *igmp;
1465 struct ip *ip;
1466 struct mbuf *m;
1467 int igmplen;
1468 int minlen;
1469 int queryver;
1470
1471 CTR3(KTR_IGMPV3, "%s: called w/mbuf (%p,%d)", __func__, *mp, *offp);
1472
1473 m = *mp;
1474 ifp = m->m_pkthdr.rcvif;
1475 *mp = NULL;
1476
1477 IGMPSTAT_INC(igps_rcv_total);
1478
1479 ip = mtod(m, struct ip *);
1480 iphlen = *offp;
1481 igmplen = ntohs(ip->ip_len) - iphlen;
1482
1483 /*
1484 * Validate lengths.
1485 */
1486 if (igmplen < IGMP_MINLEN) {
1487 IGMPSTAT_INC(igps_rcv_tooshort);
1488 m_freem(m);
1489 return (IPPROTO_DONE);
1490 }
1491
1492 /*
1493 * Always pullup to the minimum size for v1/v2 or v3
1494 * to amortize calls to m_pullup().
1495 */
1496 minlen = iphlen;
1497 if (igmplen >= IGMP_V3_QUERY_MINLEN)
1498 minlen += IGMP_V3_QUERY_MINLEN;
1499 else
1500 minlen += IGMP_MINLEN;
1501 if ((!M_WRITABLE(m) || m->m_len < minlen) &&
1502 (m = m_pullup(m, minlen)) == NULL) {
1503 IGMPSTAT_INC(igps_rcv_tooshort);
1504 return (IPPROTO_DONE);
1505 }
1506 ip = mtod(m, struct ip *);
1507
1508 /*
1509 * Validate checksum.
1510 */
1511 m->m_data += iphlen;
1512 m->m_len -= iphlen;
1513 igmp = mtod(m, struct igmp *);
1514 if (in_cksum(m, igmplen)) {
1515 IGMPSTAT_INC(igps_rcv_badsum);
1516 m_freem(m);
1517 return (IPPROTO_DONE);
1518 }
1519 m->m_data -= iphlen;
1520 m->m_len += iphlen;
1521
1522 /*
1523 * IGMP control traffic is link-scope, and must have a TTL of 1.
1524 * DVMRP traffic (e.g. mrinfo, mtrace) is an exception;
1525 * probe packets may come from beyond the LAN.
1526 */
1527 if (igmp->igmp_type != IGMP_DVMRP && ip->ip_ttl != 1) {
1528 IGMPSTAT_INC(igps_rcv_badttl);
1529 m_freem(m);
1530 return (IPPROTO_DONE);
1531 }
1532
1533 switch (igmp->igmp_type) {
1534 case IGMP_HOST_MEMBERSHIP_QUERY:
1535 if (igmplen == IGMP_MINLEN) {
1536 if (igmp->igmp_code == 0)
1537 queryver = IGMP_VERSION_1;
1538 else
1539 queryver = IGMP_VERSION_2;
1540 } else if (igmplen >= IGMP_V3_QUERY_MINLEN) {
1541 queryver = IGMP_VERSION_3;
1542 } else {
1543 IGMPSTAT_INC(igps_rcv_tooshort);
1544 m_freem(m);
1545 return (IPPROTO_DONE);
1546 }
1547
1548 switch (queryver) {
1549 case IGMP_VERSION_1:
1550 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1551 if (!V_igmp_v1enable)
1552 break;
1553 if (igmp_input_v1_query(ifp, ip, igmp) != 0) {
1554 m_freem(m);
1555 return (IPPROTO_DONE);
1556 }
1557 break;
1558
1559 case IGMP_VERSION_2:
1560 IGMPSTAT_INC(igps_rcv_v1v2_queries);
1561 if (!V_igmp_v2enable)
1562 break;
1563 if (igmp_input_v2_query(ifp, ip, igmp) != 0) {
1564 m_freem(m);
1565 return (IPPROTO_DONE);
1566 }
1567 break;
1568
1569 case IGMP_VERSION_3: {
1570 struct igmpv3 *igmpv3;
1571 uint16_t igmpv3len;
1572 uint16_t nsrc;
1573
1574 IGMPSTAT_INC(igps_rcv_v3_queries);
1575 igmpv3 = (struct igmpv3 *)igmp;
1576 /*
1577 * Validate length based on source count.
1578 */
1579 nsrc = ntohs(igmpv3->igmp_numsrc);
1580 if (nsrc * sizeof(in_addr_t) >
1581 UINT16_MAX - iphlen - IGMP_V3_QUERY_MINLEN) {
1582 IGMPSTAT_INC(igps_rcv_tooshort);
1583 m_freem(m);
1584 return (IPPROTO_DONE);
1585 }
1586 /*
1587 * m_pullup() may modify m, so pullup in
1588 * this scope.
1589 */
1590 igmpv3len = iphlen + IGMP_V3_QUERY_MINLEN +
1591 sizeof(struct in_addr) * nsrc;
1592 if ((!M_WRITABLE(m) ||
1593 m->m_len < igmpv3len) &&
1594 (m = m_pullup(m, igmpv3len)) == NULL) {
1595 IGMPSTAT_INC(igps_rcv_tooshort);
1596 return (IPPROTO_DONE);
1597 }
1598 igmpv3 = (struct igmpv3 *)(mtod(m, uint8_t *)
1599 + iphlen);
1600 if (igmp_input_v3_query(ifp, ip, igmpv3) != 0) {
1601 m_freem(m);
1602 return (IPPROTO_DONE);
1603 }
1604 }
1605 break;
1606 }
1607 break;
1608
1609 case IGMP_v1_HOST_MEMBERSHIP_REPORT:
1610 if (!V_igmp_v1enable)
1611 break;
1612 if (igmp_input_v1_report(ifp, ip, igmp) != 0) {
1613 m_freem(m);
1614 return (IPPROTO_DONE);
1615 }
1616 break;
1617
1618 case IGMP_v2_HOST_MEMBERSHIP_REPORT:
1619 if (!V_igmp_v2enable)
1620 break;
1621 if (!ip_checkrouteralert(m))
1622 IGMPSTAT_INC(igps_rcv_nora);
1623 if (igmp_input_v2_report(ifp, ip, igmp) != 0) {
1624 m_freem(m);
1625 return (IPPROTO_DONE);
1626 }
1627 break;
1628
1629 case IGMP_v3_HOST_MEMBERSHIP_REPORT:
1630 /*
1631 * Hosts do not need to process IGMPv3 membership reports,
1632 * as report suppression is no longer required.
1633 */
1634 if (!ip_checkrouteralert(m))
1635 IGMPSTAT_INC(igps_rcv_nora);
1636 break;
1637
1638 default:
1639 break;
1640 }
1641
1642 /*
1643 * Pass all valid IGMP packets up to any process(es) listening on a
1644 * raw IGMP socket.
1645 */
1646 *mp = m;
1647 return (rip_input(mp, offp, proto));
1648 }
1649
1650 /*
1651 * Fast timeout handler (global).
1652 * VIMAGE: Timeout handlers are expected to service all vimages.
1653 */
1654 static struct callout igmpfast_callout;
1655 static void
igmp_fasttimo(void * arg __unused)1656 igmp_fasttimo(void *arg __unused)
1657 {
1658 struct epoch_tracker et;
1659 VNET_ITERATOR_DECL(vnet_iter);
1660
1661 NET_EPOCH_ENTER(et);
1662 VNET_LIST_RLOCK_NOSLEEP();
1663 VNET_FOREACH(vnet_iter) {
1664 CURVNET_SET(vnet_iter);
1665 igmp_fasttimo_vnet();
1666 CURVNET_RESTORE();
1667 }
1668 VNET_LIST_RUNLOCK_NOSLEEP();
1669 NET_EPOCH_EXIT(et);
1670
1671 callout_reset(&igmpfast_callout, hz / IGMP_FASTHZ, igmp_fasttimo, NULL);
1672 }
1673
1674 /*
1675 * Fast timeout handler (per-vnet).
1676 *
1677 * VIMAGE: Assume caller has set up our curvnet.
1678 */
1679 static void
igmp_fasttimo_vnet(void)1680 igmp_fasttimo_vnet(void)
1681 {
1682 struct mbufq scq; /* State-change packets */
1683 struct mbufq qrq; /* Query response packets */
1684 struct ifnet *ifp;
1685 struct igmp_ifsoftc *igi;
1686 struct ifmultiaddr *ifma;
1687 struct in_multi *inm;
1688 struct in_multi_head inm_free_tmp;
1689 int loop, uri_fasthz;
1690
1691 loop = 0;
1692 uri_fasthz = 0;
1693
1694 /*
1695 * Quick check to see if any work needs to be done, in order to
1696 * minimize the overhead of fasttimo processing.
1697 * SMPng: XXX Unlocked reads.
1698 */
1699 if (!V_current_state_timers_running &&
1700 !V_interface_timers_running &&
1701 !V_state_change_timers_running)
1702 return;
1703
1704 SLIST_INIT(&inm_free_tmp);
1705 IN_MULTI_LIST_LOCK();
1706 IGMP_LOCK();
1707
1708 /*
1709 * IGMPv3 General Query response timer processing.
1710 */
1711 if (V_interface_timers_running) {
1712 CTR1(KTR_IGMPV3, "%s: interface timers running", __func__);
1713
1714 V_interface_timers_running = 0;
1715 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1716 if (igi->igi_v3_timer == 0) {
1717 /* Do nothing. */
1718 } else if (--igi->igi_v3_timer == 0) {
1719 igmp_v3_dispatch_general_query(igi);
1720 } else {
1721 V_interface_timers_running = 1;
1722 }
1723 }
1724 }
1725
1726 if (!V_current_state_timers_running &&
1727 !V_state_change_timers_running)
1728 goto out_locked;
1729
1730 V_current_state_timers_running = 0;
1731 V_state_change_timers_running = 0;
1732
1733 CTR1(KTR_IGMPV3, "%s: state change timers running", __func__);
1734
1735 /*
1736 * IGMPv1/v2/v3 host report and state-change timer processing.
1737 * Note: Processing a v3 group timer may remove a node.
1738 */
1739 LIST_FOREACH(igi, &V_igi_head, igi_link) {
1740 ifp = igi->igi_ifp;
1741
1742 if (igi->igi_version == IGMP_VERSION_3) {
1743 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
1744 uri_fasthz = IGMP_RANDOM_DELAY(igi->igi_uri *
1745 IGMP_FASTHZ);
1746 mbufq_init(&qrq, IGMP_MAX_G_GS_PACKETS);
1747 mbufq_init(&scq, IGMP_MAX_STATE_CHANGE_PACKETS);
1748 }
1749
1750 IF_ADDR_WLOCK(ifp);
1751 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1752 inm = inm_ifmultiaddr_get_inm(ifma);
1753 if (inm == NULL)
1754 continue;
1755 switch (igi->igi_version) {
1756 case IGMP_VERSION_1:
1757 case IGMP_VERSION_2:
1758 igmp_v1v2_process_group_timer(inm,
1759 igi->igi_version);
1760 break;
1761 case IGMP_VERSION_3:
1762 igmp_v3_process_group_timers(&inm_free_tmp, &qrq,
1763 &scq, inm, uri_fasthz);
1764 break;
1765 }
1766 }
1767 IF_ADDR_WUNLOCK(ifp);
1768
1769 if (igi->igi_version == IGMP_VERSION_3) {
1770 igmp_dispatch_queue(&qrq, 0, loop);
1771 igmp_dispatch_queue(&scq, 0, loop);
1772
1773 /*
1774 * Free the in_multi reference(s) for this
1775 * IGMP lifecycle.
1776 */
1777 inm_release_list_deferred(&inm_free_tmp);
1778 }
1779 }
1780
1781 out_locked:
1782 IGMP_UNLOCK();
1783 IN_MULTI_LIST_UNLOCK();
1784 }
1785
1786 /*
1787 * Update host report group timer for IGMPv1/v2.
1788 * Will update the global pending timer flags.
1789 */
1790 static void
igmp_v1v2_process_group_timer(struct in_multi * inm,const int version)1791 igmp_v1v2_process_group_timer(struct in_multi *inm, const int version)
1792 {
1793 int report_timer_expired;
1794
1795 IN_MULTI_LIST_LOCK_ASSERT();
1796 IGMP_LOCK_ASSERT();
1797
1798 if (inm->inm_timer == 0) {
1799 report_timer_expired = 0;
1800 } else if (--inm->inm_timer == 0) {
1801 report_timer_expired = 1;
1802 } else {
1803 V_current_state_timers_running = 1;
1804 return;
1805 }
1806
1807 switch (inm->inm_state) {
1808 case IGMP_NOT_MEMBER:
1809 case IGMP_SILENT_MEMBER:
1810 case IGMP_IDLE_MEMBER:
1811 case IGMP_LAZY_MEMBER:
1812 case IGMP_SLEEPING_MEMBER:
1813 case IGMP_AWAKENING_MEMBER:
1814 break;
1815 case IGMP_REPORTING_MEMBER:
1816 if (report_timer_expired) {
1817 inm->inm_state = IGMP_IDLE_MEMBER;
1818 (void)igmp_v1v2_queue_report(inm,
1819 (version == IGMP_VERSION_2) ?
1820 IGMP_v2_HOST_MEMBERSHIP_REPORT :
1821 IGMP_v1_HOST_MEMBERSHIP_REPORT);
1822 }
1823 break;
1824 case IGMP_G_QUERY_PENDING_MEMBER:
1825 case IGMP_SG_QUERY_PENDING_MEMBER:
1826 case IGMP_LEAVING_MEMBER:
1827 break;
1828 }
1829 }
1830
1831 /*
1832 * Update a group's timers for IGMPv3.
1833 * Will update the global pending timer flags.
1834 * Note: Unlocked read from igi.
1835 */
1836 static void
igmp_v3_process_group_timers(struct in_multi_head * inmh,struct mbufq * qrq,struct mbufq * scq,struct in_multi * inm,const int uri_fasthz)1837 igmp_v3_process_group_timers(struct in_multi_head *inmh,
1838 struct mbufq *qrq, struct mbufq *scq,
1839 struct in_multi *inm, const int uri_fasthz)
1840 {
1841 int query_response_timer_expired;
1842 int state_change_retransmit_timer_expired;
1843
1844 IN_MULTI_LIST_LOCK_ASSERT();
1845 IGMP_LOCK_ASSERT();
1846
1847 query_response_timer_expired = 0;
1848 state_change_retransmit_timer_expired = 0;
1849
1850 /*
1851 * During a transition from v1/v2 compatibility mode back to v3,
1852 * a group record in REPORTING state may still have its group
1853 * timer active. This is a no-op in this function; it is easier
1854 * to deal with it here than to complicate the slow-timeout path.
1855 */
1856 if (inm->inm_timer == 0) {
1857 query_response_timer_expired = 0;
1858 } else if (--inm->inm_timer == 0) {
1859 query_response_timer_expired = 1;
1860 } else {
1861 V_current_state_timers_running = 1;
1862 }
1863
1864 if (inm->inm_sctimer == 0) {
1865 state_change_retransmit_timer_expired = 0;
1866 } else if (--inm->inm_sctimer == 0) {
1867 state_change_retransmit_timer_expired = 1;
1868 } else {
1869 V_state_change_timers_running = 1;
1870 }
1871
1872 /* We are in fasttimo, so be quick about it. */
1873 if (!state_change_retransmit_timer_expired &&
1874 !query_response_timer_expired)
1875 return;
1876
1877 switch (inm->inm_state) {
1878 case IGMP_NOT_MEMBER:
1879 case IGMP_SILENT_MEMBER:
1880 case IGMP_SLEEPING_MEMBER:
1881 case IGMP_LAZY_MEMBER:
1882 case IGMP_AWAKENING_MEMBER:
1883 case IGMP_IDLE_MEMBER:
1884 break;
1885 case IGMP_G_QUERY_PENDING_MEMBER:
1886 case IGMP_SG_QUERY_PENDING_MEMBER:
1887 /*
1888 * Respond to a previously pending Group-Specific
1889 * or Group-and-Source-Specific query by enqueueing
1890 * the appropriate Current-State report for
1891 * immediate transmission.
1892 */
1893 if (query_response_timer_expired) {
1894 int retval __unused;
1895
1896 retval = igmp_v3_enqueue_group_record(qrq, inm, 0, 1,
1897 (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER));
1898 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
1899 __func__, retval);
1900 inm->inm_state = IGMP_REPORTING_MEMBER;
1901 /* XXX Clear recorded sources for next time. */
1902 inm_clear_recorded(inm);
1903 }
1904 /* FALLTHROUGH */
1905 case IGMP_REPORTING_MEMBER:
1906 case IGMP_LEAVING_MEMBER:
1907 if (state_change_retransmit_timer_expired) {
1908 /*
1909 * State-change retransmission timer fired.
1910 * If there are any further pending retransmissions,
1911 * set the global pending state-change flag, and
1912 * reset the timer.
1913 */
1914 if (--inm->inm_scrv > 0) {
1915 inm->inm_sctimer = uri_fasthz;
1916 V_state_change_timers_running = 1;
1917 }
1918 /*
1919 * Retransmit the previously computed state-change
1920 * report. If there are no further pending
1921 * retransmissions, the mbuf queue will be consumed.
1922 * Update T0 state to T1 as we have now sent
1923 * a state-change.
1924 */
1925 (void)igmp_v3_merge_state_changes(inm, scq);
1926
1927 inm_commit(inm);
1928 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
1929 ntohl(inm->inm_addr.s_addr),
1930 inm->inm_ifp->if_xname);
1931
1932 /*
1933 * If we are leaving the group for good, make sure
1934 * we release IGMP's reference to it.
1935 * This release must be deferred using a SLIST,
1936 * as we are called from a loop which traverses
1937 * the in_ifmultiaddr TAILQ.
1938 */
1939 if (inm->inm_state == IGMP_LEAVING_MEMBER &&
1940 inm->inm_scrv == 0) {
1941 inm->inm_state = IGMP_NOT_MEMBER;
1942 inm_rele_locked(inmh, inm);
1943 }
1944 }
1945 break;
1946 }
1947 }
1948
1949 /*
1950 * Suppress a group's pending response to a group or source/group query.
1951 *
1952 * Do NOT suppress state changes. This leads to IGMPv3 inconsistency.
1953 * Do NOT update ST1/ST0 as this operation merely suppresses
1954 * the currently pending group record.
1955 * Do NOT suppress the response to a general query. It is possible but
1956 * it would require adding another state or flag.
1957 */
1958 static void
igmp_v3_suppress_group_record(struct in_multi * inm)1959 igmp_v3_suppress_group_record(struct in_multi *inm)
1960 {
1961
1962 IN_MULTI_LIST_LOCK_ASSERT();
1963
1964 KASSERT(inm->inm_igi->igi_version == IGMP_VERSION_3,
1965 ("%s: not IGMPv3 mode on link", __func__));
1966
1967 if (inm->inm_state != IGMP_G_QUERY_PENDING_MEMBER ||
1968 inm->inm_state != IGMP_SG_QUERY_PENDING_MEMBER)
1969 return;
1970
1971 if (inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
1972 inm_clear_recorded(inm);
1973
1974 inm->inm_timer = 0;
1975 inm->inm_state = IGMP_REPORTING_MEMBER;
1976 }
1977
1978 /*
1979 * Switch to a different IGMP version on the given interface,
1980 * as per Section 7.2.1.
1981 */
1982 static void
igmp_set_version(struct igmp_ifsoftc * igi,const int version)1983 igmp_set_version(struct igmp_ifsoftc *igi, const int version)
1984 {
1985 int old_version_timer;
1986
1987 IGMP_LOCK_ASSERT();
1988
1989 CTR4(KTR_IGMPV3, "%s: switching to v%d on ifp %p(%s)", __func__,
1990 version, igi->igi_ifp, igi->igi_ifp->if_xname);
1991
1992 if (version == IGMP_VERSION_1 || version == IGMP_VERSION_2) {
1993 /*
1994 * Compute the "Older Version Querier Present" timer as per
1995 * Section 8.12.
1996 */
1997 old_version_timer = igi->igi_rv * igi->igi_qi + igi->igi_qri;
1998 old_version_timer *= IGMP_SLOWHZ;
1999
2000 if (version == IGMP_VERSION_1) {
2001 igi->igi_v1_timer = old_version_timer;
2002 igi->igi_v2_timer = 0;
2003 } else if (version == IGMP_VERSION_2) {
2004 igi->igi_v1_timer = 0;
2005 igi->igi_v2_timer = old_version_timer;
2006 }
2007 }
2008
2009 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2010 if (igi->igi_version != IGMP_VERSION_2) {
2011 igi->igi_version = IGMP_VERSION_2;
2012 igmp_v3_cancel_link_timers(igi);
2013 }
2014 } else if (igi->igi_v1_timer > 0) {
2015 if (igi->igi_version != IGMP_VERSION_1) {
2016 igi->igi_version = IGMP_VERSION_1;
2017 igmp_v3_cancel_link_timers(igi);
2018 }
2019 }
2020 }
2021
2022 /*
2023 * Cancel pending IGMPv3 timers for the given link and all groups
2024 * joined on it; state-change, general-query, and group-query timers.
2025 *
2026 * Only ever called on a transition from v3 to Compatibility mode. Kill
2027 * the timers stone dead (this may be expensive for large N groups), they
2028 * will be restarted if Compatibility Mode deems that they must be due to
2029 * query processing.
2030 */
2031 static void
igmp_v3_cancel_link_timers(struct igmp_ifsoftc * igi)2032 igmp_v3_cancel_link_timers(struct igmp_ifsoftc *igi)
2033 {
2034 struct ifmultiaddr *ifma;
2035 struct ifnet *ifp;
2036 struct in_multi *inm;
2037 struct in_multi_head inm_free_tmp;
2038
2039 CTR3(KTR_IGMPV3, "%s: cancel v3 timers on ifp %p(%s)", __func__,
2040 igi->igi_ifp, igi->igi_ifp->if_xname);
2041
2042 IN_MULTI_LIST_LOCK_ASSERT();
2043 IGMP_LOCK_ASSERT();
2044 NET_EPOCH_ASSERT();
2045
2046 SLIST_INIT(&inm_free_tmp);
2047
2048 /*
2049 * Stop the v3 General Query Response on this link stone dead.
2050 * If fasttimo is woken up due to V_interface_timers_running,
2051 * the flag will be cleared if there are no pending link timers.
2052 */
2053 igi->igi_v3_timer = 0;
2054
2055 /*
2056 * Now clear the current-state and state-change report timers
2057 * for all memberships scoped to this link.
2058 */
2059 ifp = igi->igi_ifp;
2060 IF_ADDR_WLOCK(ifp);
2061 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2062 inm = inm_ifmultiaddr_get_inm(ifma);
2063 if (inm == NULL)
2064 continue;
2065 switch (inm->inm_state) {
2066 case IGMP_NOT_MEMBER:
2067 case IGMP_SILENT_MEMBER:
2068 case IGMP_IDLE_MEMBER:
2069 case IGMP_LAZY_MEMBER:
2070 case IGMP_SLEEPING_MEMBER:
2071 case IGMP_AWAKENING_MEMBER:
2072 /*
2073 * These states are either not relevant in v3 mode,
2074 * or are unreported. Do nothing.
2075 */
2076 break;
2077 case IGMP_LEAVING_MEMBER:
2078 /*
2079 * If we are leaving the group and switching to
2080 * compatibility mode, we need to release the final
2081 * reference held for issuing the INCLUDE {}, and
2082 * transition to REPORTING to ensure the host leave
2083 * message is sent upstream to the old querier --
2084 * transition to NOT would lose the leave and race.
2085 */
2086 inm_rele_locked(&inm_free_tmp, inm);
2087 /* FALLTHROUGH */
2088 case IGMP_G_QUERY_PENDING_MEMBER:
2089 case IGMP_SG_QUERY_PENDING_MEMBER:
2090 inm_clear_recorded(inm);
2091 /* FALLTHROUGH */
2092 case IGMP_REPORTING_MEMBER:
2093 inm->inm_state = IGMP_REPORTING_MEMBER;
2094 break;
2095 }
2096 /*
2097 * Always clear state-change and group report timers.
2098 * Free any pending IGMPv3 state-change records.
2099 */
2100 inm->inm_sctimer = 0;
2101 inm->inm_timer = 0;
2102 mbufq_drain(&inm->inm_scq);
2103 }
2104 IF_ADDR_WUNLOCK(ifp);
2105
2106 inm_release_list_deferred(&inm_free_tmp);
2107 }
2108
2109 /*
2110 * Update the Older Version Querier Present timers for a link.
2111 * See Section 7.2.1 of RFC 3376.
2112 */
2113 static void
igmp_v1v2_process_querier_timers(struct igmp_ifsoftc * igi)2114 igmp_v1v2_process_querier_timers(struct igmp_ifsoftc *igi)
2115 {
2116
2117 IGMP_LOCK_ASSERT();
2118
2119 if (igi->igi_v1_timer == 0 && igi->igi_v2_timer == 0) {
2120 /*
2121 * IGMPv1 and IGMPv2 Querier Present timers expired.
2122 *
2123 * Revert to IGMPv3.
2124 */
2125 if (igi->igi_version != IGMP_VERSION_3) {
2126 CTR5(KTR_IGMPV3,
2127 "%s: transition from v%d -> v%d on %p(%s)",
2128 __func__, igi->igi_version, IGMP_VERSION_3,
2129 igi->igi_ifp, igi->igi_ifp->if_xname);
2130 igi->igi_version = IGMP_VERSION_3;
2131 }
2132 } else if (igi->igi_v1_timer == 0 && igi->igi_v2_timer > 0) {
2133 /*
2134 * IGMPv1 Querier Present timer expired,
2135 * IGMPv2 Querier Present timer running.
2136 * If IGMPv2 was disabled since last timeout,
2137 * revert to IGMPv3.
2138 * If IGMPv2 is enabled, revert to IGMPv2.
2139 */
2140 if (!V_igmp_v2enable) {
2141 CTR5(KTR_IGMPV3,
2142 "%s: transition from v%d -> v%d on %p(%s)",
2143 __func__, igi->igi_version, IGMP_VERSION_3,
2144 igi->igi_ifp, igi->igi_ifp->if_xname);
2145 igi->igi_v2_timer = 0;
2146 igi->igi_version = IGMP_VERSION_3;
2147 } else {
2148 --igi->igi_v2_timer;
2149 if (igi->igi_version != IGMP_VERSION_2) {
2150 CTR5(KTR_IGMPV3,
2151 "%s: transition from v%d -> v%d on %p(%s)",
2152 __func__, igi->igi_version, IGMP_VERSION_2,
2153 igi->igi_ifp, igi->igi_ifp->if_xname);
2154 igi->igi_version = IGMP_VERSION_2;
2155 igmp_v3_cancel_link_timers(igi);
2156 }
2157 }
2158 } else if (igi->igi_v1_timer > 0) {
2159 /*
2160 * IGMPv1 Querier Present timer running.
2161 * Stop IGMPv2 timer if running.
2162 *
2163 * If IGMPv1 was disabled since last timeout,
2164 * revert to IGMPv3.
2165 * If IGMPv1 is enabled, reset IGMPv2 timer if running.
2166 */
2167 if (!V_igmp_v1enable) {
2168 CTR5(KTR_IGMPV3,
2169 "%s: transition from v%d -> v%d on %p(%s)",
2170 __func__, igi->igi_version, IGMP_VERSION_3,
2171 igi->igi_ifp, igi->igi_ifp->if_xname);
2172 igi->igi_v1_timer = 0;
2173 igi->igi_version = IGMP_VERSION_3;
2174 } else {
2175 --igi->igi_v1_timer;
2176 }
2177 if (igi->igi_v2_timer > 0) {
2178 CTR3(KTR_IGMPV3,
2179 "%s: cancel v2 timer on %p(%s)",
2180 __func__, igi->igi_ifp, igi->igi_ifp->if_xname);
2181 igi->igi_v2_timer = 0;
2182 }
2183 }
2184 }
2185
2186 /*
2187 * Global slowtimo handler.
2188 * VIMAGE: Timeout handlers are expected to service all vimages.
2189 */
2190 static struct callout igmpslow_callout;
2191 static void
igmp_slowtimo(void * arg __unused)2192 igmp_slowtimo(void *arg __unused)
2193 {
2194 struct epoch_tracker et;
2195 VNET_ITERATOR_DECL(vnet_iter);
2196
2197 NET_EPOCH_ENTER(et);
2198 VNET_LIST_RLOCK_NOSLEEP();
2199 VNET_FOREACH(vnet_iter) {
2200 CURVNET_SET(vnet_iter);
2201 igmp_slowtimo_vnet();
2202 CURVNET_RESTORE();
2203 }
2204 VNET_LIST_RUNLOCK_NOSLEEP();
2205 NET_EPOCH_EXIT(et);
2206
2207 callout_reset(&igmpslow_callout, hz / IGMP_SLOWHZ, igmp_slowtimo, NULL);
2208 }
2209
2210 /*
2211 * Per-vnet slowtimo handler.
2212 */
2213 static void
igmp_slowtimo_vnet(void)2214 igmp_slowtimo_vnet(void)
2215 {
2216 struct igmp_ifsoftc *igi;
2217
2218 IGMP_LOCK();
2219
2220 LIST_FOREACH(igi, &V_igi_head, igi_link) {
2221 igmp_v1v2_process_querier_timers(igi);
2222 }
2223
2224 IGMP_UNLOCK();
2225 }
2226
2227 /*
2228 * Dispatch an IGMPv1/v2 host report or leave message.
2229 * These are always small enough to fit inside a single mbuf.
2230 */
2231 static int
igmp_v1v2_queue_report(struct in_multi * inm,const int type)2232 igmp_v1v2_queue_report(struct in_multi *inm, const int type)
2233 {
2234 struct epoch_tracker et;
2235 struct ifnet *ifp;
2236 struct igmp *igmp;
2237 struct ip *ip;
2238 struct mbuf *m;
2239
2240 IN_MULTI_LIST_LOCK_ASSERT();
2241 IGMP_LOCK_ASSERT();
2242
2243 ifp = inm->inm_ifp;
2244
2245 m = m_gethdr(M_NOWAIT, MT_DATA);
2246 if (m == NULL)
2247 return (ENOMEM);
2248 M_ALIGN(m, sizeof(struct ip) + sizeof(struct igmp));
2249
2250 m->m_pkthdr.len = sizeof(struct ip) + sizeof(struct igmp);
2251
2252 m->m_data += sizeof(struct ip);
2253 m->m_len = sizeof(struct igmp);
2254
2255 igmp = mtod(m, struct igmp *);
2256 igmp->igmp_type = type;
2257 igmp->igmp_code = 0;
2258 igmp->igmp_group = inm->inm_addr;
2259 igmp->igmp_cksum = 0;
2260 igmp->igmp_cksum = in_cksum(m, sizeof(struct igmp));
2261
2262 m->m_data -= sizeof(struct ip);
2263 m->m_len += sizeof(struct ip);
2264
2265 ip = mtod(m, struct ip *);
2266 ip->ip_tos = 0;
2267 ip->ip_len = htons(sizeof(struct ip) + sizeof(struct igmp));
2268 ip->ip_off = 0;
2269 ip->ip_p = IPPROTO_IGMP;
2270 ip->ip_src.s_addr = INADDR_ANY;
2271
2272 if (type == IGMP_HOST_LEAVE_MESSAGE)
2273 ip->ip_dst.s_addr = htonl(INADDR_ALLRTRS_GROUP);
2274 else
2275 ip->ip_dst = inm->inm_addr;
2276
2277 igmp_save_context(m, ifp);
2278
2279 m->m_flags |= M_IGMPV2;
2280 if (inm->inm_igi->igi_flags & IGIF_LOOPBACK)
2281 m->m_flags |= M_IGMP_LOOP;
2282
2283 CTR2(KTR_IGMPV3, "%s: netisr_dispatch(NETISR_IGMP, %p)", __func__, m);
2284 NET_EPOCH_ENTER(et);
2285 netisr_dispatch(NETISR_IGMP, m);
2286 NET_EPOCH_EXIT(et);
2287
2288 return (0);
2289 }
2290
2291 /*
2292 * Process a state change from the upper layer for the given IPv4 group.
2293 *
2294 * Each socket holds a reference on the in_multi in its own ip_moptions.
2295 * The socket layer will have made the necessary updates to.the group
2296 * state, it is now up to IGMP to issue a state change report if there
2297 * has been any change between T0 (when the last state-change was issued)
2298 * and T1 (now).
2299 *
2300 * We use the IGMPv3 state machine at group level. The IGMP module
2301 * however makes the decision as to which IGMP protocol version to speak.
2302 * A state change *from* INCLUDE {} always means an initial join.
2303 * A state change *to* INCLUDE {} always means a final leave.
2304 *
2305 * FUTURE: If IGIF_V3LITE is enabled for this interface, then we can
2306 * save ourselves a bunch of work; any exclusive mode groups need not
2307 * compute source filter lists.
2308 *
2309 * VIMAGE: curvnet should have been set by caller, as this routine
2310 * is called from the socket option handlers.
2311 */
2312 int
igmp_change_state(struct in_multi * inm)2313 igmp_change_state(struct in_multi *inm)
2314 {
2315 struct igmp_ifsoftc *igi;
2316 struct ifnet *ifp;
2317 int error;
2318
2319 error = 0;
2320 IN_MULTI_LOCK_ASSERT();
2321 /*
2322 * Try to detect if the upper layer just asked us to change state
2323 * for an interface which has now gone away.
2324 */
2325 KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
2326 ifp = inm->inm_ifma->ifma_ifp;
2327 if (ifp == NULL)
2328 return (0);
2329 /*
2330 * Sanity check that netinet's notion of ifp is the
2331 * same as net's.
2332 */
2333 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
2334
2335 IGMP_LOCK();
2336
2337 igi = ((struct in_ifinfo *)ifp->if_afdata[AF_INET])->ii_igmp;
2338 KASSERT(igi != NULL, ("%s: no igmp_ifsoftc for ifp %p", __func__, ifp));
2339
2340 /*
2341 * If we detect a state transition to or from MCAST_UNDEFINED
2342 * for this group, then we are starting or finishing an IGMP
2343 * life cycle for this group.
2344 */
2345 if (inm->inm_st[1].iss_fmode != inm->inm_st[0].iss_fmode) {
2346 CTR3(KTR_IGMPV3, "%s: inm transition %d -> %d", __func__,
2347 inm->inm_st[0].iss_fmode, inm->inm_st[1].iss_fmode);
2348 if (inm->inm_st[0].iss_fmode == MCAST_UNDEFINED) {
2349 CTR1(KTR_IGMPV3, "%s: initial join", __func__);
2350 error = igmp_initial_join(inm, igi);
2351 goto out_locked;
2352 } else if (inm->inm_st[1].iss_fmode == MCAST_UNDEFINED) {
2353 CTR1(KTR_IGMPV3, "%s: final leave", __func__);
2354 igmp_final_leave(inm, igi);
2355 goto out_locked;
2356 }
2357 } else {
2358 CTR1(KTR_IGMPV3, "%s: filter set change", __func__);
2359 }
2360
2361 error = igmp_handle_state_change(inm, igi);
2362
2363 out_locked:
2364 IGMP_UNLOCK();
2365 return (error);
2366 }
2367
2368 /*
2369 * Perform the initial join for an IGMP group.
2370 *
2371 * When joining a group:
2372 * If the group should have its IGMP traffic suppressed, do nothing.
2373 * IGMPv1 starts sending IGMPv1 host membership reports.
2374 * IGMPv2 starts sending IGMPv2 host membership reports.
2375 * IGMPv3 will schedule an IGMPv3 state-change report containing the
2376 * initial state of the membership.
2377 */
2378 static int
igmp_initial_join(struct in_multi * inm,struct igmp_ifsoftc * igi)2379 igmp_initial_join(struct in_multi *inm, struct igmp_ifsoftc *igi)
2380 {
2381 struct ifnet *ifp;
2382 struct mbufq *mq;
2383 int error, retval, syncstates;
2384
2385 CTR4(KTR_IGMPV3, "%s: initial join 0x%08x on ifp %p(%s)", __func__,
2386 ntohl(inm->inm_addr.s_addr), inm->inm_ifp, inm->inm_ifp->if_xname);
2387
2388 error = 0;
2389 syncstates = 1;
2390
2391 ifp = inm->inm_ifp;
2392
2393 IN_MULTI_LOCK_ASSERT();
2394 IGMP_LOCK_ASSERT();
2395
2396 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2397
2398 /*
2399 * Groups joined on loopback or marked as 'not reported',
2400 * e.g. 224.0.0.1, enter the IGMP_SILENT_MEMBER state and
2401 * are never reported in any IGMP protocol exchanges.
2402 * All other groups enter the appropriate IGMP state machine
2403 * for the version in use on this link.
2404 * A link marked as IGIF_SILENT causes IGMP to be completely
2405 * disabled for the link.
2406 */
2407 if ((ifp->if_flags & IFF_LOOPBACK) ||
2408 (igi->igi_flags & IGIF_SILENT) ||
2409 !igmp_isgroupreported(inm->inm_addr)) {
2410 CTR1(KTR_IGMPV3,
2411 "%s: not kicking state machine for silent group", __func__);
2412 inm->inm_state = IGMP_SILENT_MEMBER;
2413 inm->inm_timer = 0;
2414 } else {
2415 /*
2416 * Deal with overlapping in_multi lifecycle.
2417 * If this group was LEAVING, then make sure
2418 * we drop the reference we picked up to keep the
2419 * group around for the final INCLUDE {} enqueue.
2420 */
2421 if (igi->igi_version == IGMP_VERSION_3 &&
2422 inm->inm_state == IGMP_LEAVING_MEMBER) {
2423 MPASS(inm->inm_refcount > 1);
2424 inm_rele_locked(NULL, inm);
2425 }
2426 inm->inm_state = IGMP_REPORTING_MEMBER;
2427
2428 switch (igi->igi_version) {
2429 case IGMP_VERSION_1:
2430 case IGMP_VERSION_2:
2431 inm->inm_state = IGMP_IDLE_MEMBER;
2432 error = igmp_v1v2_queue_report(inm,
2433 (igi->igi_version == IGMP_VERSION_2) ?
2434 IGMP_v2_HOST_MEMBERSHIP_REPORT :
2435 IGMP_v1_HOST_MEMBERSHIP_REPORT);
2436 if (error == 0) {
2437 inm->inm_timer = IGMP_RANDOM_DELAY(
2438 IGMP_V1V2_MAX_RI * IGMP_FASTHZ);
2439 V_current_state_timers_running = 1;
2440 }
2441 break;
2442
2443 case IGMP_VERSION_3:
2444 /*
2445 * Defer update of T0 to T1, until the first copy
2446 * of the state change has been transmitted.
2447 */
2448 syncstates = 0;
2449
2450 /*
2451 * Immediately enqueue a State-Change Report for
2452 * this interface, freeing any previous reports.
2453 * Don't kick the timers if there is nothing to do,
2454 * or if an error occurred.
2455 */
2456 mq = &inm->inm_scq;
2457 mbufq_drain(mq);
2458 retval = igmp_v3_enqueue_group_record(mq, inm, 1,
2459 0, 0);
2460 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
2461 __func__, retval);
2462 if (retval <= 0) {
2463 error = retval * -1;
2464 break;
2465 }
2466
2467 /*
2468 * Schedule transmission of pending state-change
2469 * report up to RV times for this link. The timer
2470 * will fire at the next igmp_fasttimo (~200ms),
2471 * giving us an opportunity to merge the reports.
2472 */
2473 if (igi->igi_flags & IGIF_LOOPBACK) {
2474 inm->inm_scrv = 1;
2475 } else {
2476 KASSERT(igi->igi_rv > 1,
2477 ("%s: invalid robustness %d", __func__,
2478 igi->igi_rv));
2479 inm->inm_scrv = igi->igi_rv;
2480 }
2481 inm->inm_sctimer = 1;
2482 V_state_change_timers_running = 1;
2483
2484 error = 0;
2485 break;
2486 }
2487 }
2488
2489 /*
2490 * Only update the T0 state if state change is atomic,
2491 * i.e. we don't need to wait for a timer to fire before we
2492 * can consider the state change to have been communicated.
2493 */
2494 if (syncstates) {
2495 inm_commit(inm);
2496 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2497 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2498 }
2499
2500 return (error);
2501 }
2502
2503 /*
2504 * Issue an intermediate state change during the IGMP life-cycle.
2505 */
2506 static int
igmp_handle_state_change(struct in_multi * inm,struct igmp_ifsoftc * igi)2507 igmp_handle_state_change(struct in_multi *inm, struct igmp_ifsoftc *igi)
2508 {
2509 struct ifnet *ifp;
2510 int retval;
2511
2512 CTR4(KTR_IGMPV3, "%s: state change for 0x%08x on ifp %p(%s)", __func__,
2513 ntohl(inm->inm_addr.s_addr), inm->inm_ifp, inm->inm_ifp->if_xname);
2514
2515 ifp = inm->inm_ifp;
2516
2517 IN_MULTI_LIST_LOCK_ASSERT();
2518 IGMP_LOCK_ASSERT();
2519
2520 KASSERT(igi && igi->igi_ifp == ifp, ("%s: inconsistent ifp", __func__));
2521
2522 if ((ifp->if_flags & IFF_LOOPBACK) ||
2523 (igi->igi_flags & IGIF_SILENT) ||
2524 !igmp_isgroupreported(inm->inm_addr) ||
2525 (igi->igi_version != IGMP_VERSION_3)) {
2526 if (!igmp_isgroupreported(inm->inm_addr)) {
2527 CTR1(KTR_IGMPV3,
2528 "%s: not kicking state machine for silent group", __func__);
2529 }
2530 CTR1(KTR_IGMPV3, "%s: nothing to do", __func__);
2531 inm_commit(inm);
2532 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2533 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2534 return (0);
2535 }
2536
2537 mbufq_drain(&inm->inm_scq);
2538
2539 retval = igmp_v3_enqueue_group_record(&inm->inm_scq, inm, 1, 0, 0);
2540 CTR2(KTR_IGMPV3, "%s: enqueue record = %d", __func__, retval);
2541 if (retval <= 0)
2542 return (-retval);
2543
2544 /*
2545 * If record(s) were enqueued, start the state-change
2546 * report timer for this group.
2547 */
2548 inm->inm_scrv = ((igi->igi_flags & IGIF_LOOPBACK) ? 1 : igi->igi_rv);
2549 inm->inm_sctimer = 1;
2550 V_state_change_timers_running = 1;
2551
2552 return (0);
2553 }
2554
2555 /*
2556 * Perform the final leave for an IGMP group.
2557 *
2558 * When leaving a group:
2559 * IGMPv1 does nothing.
2560 * IGMPv2 sends a host leave message, if and only if we are the reporter.
2561 * IGMPv3 enqueues a state-change report containing a transition
2562 * to INCLUDE {} for immediate transmission.
2563 */
2564 static void
igmp_final_leave(struct in_multi * inm,struct igmp_ifsoftc * igi)2565 igmp_final_leave(struct in_multi *inm, struct igmp_ifsoftc *igi)
2566 {
2567 int syncstates;
2568
2569 syncstates = 1;
2570
2571 CTR4(KTR_IGMPV3, "%s: final leave 0x%08x on ifp %p(%s)",
2572 __func__, ntohl(inm->inm_addr.s_addr), inm->inm_ifp,
2573 inm->inm_ifp->if_xname);
2574
2575 IN_MULTI_LIST_LOCK_ASSERT();
2576 IGMP_LOCK_ASSERT();
2577
2578 switch (inm->inm_state) {
2579 case IGMP_NOT_MEMBER:
2580 case IGMP_SILENT_MEMBER:
2581 case IGMP_LEAVING_MEMBER:
2582 /* Already leaving or left; do nothing. */
2583 CTR1(KTR_IGMPV3,
2584 "%s: not kicking state machine for silent group", __func__);
2585 break;
2586 case IGMP_REPORTING_MEMBER:
2587 case IGMP_IDLE_MEMBER:
2588 case IGMP_G_QUERY_PENDING_MEMBER:
2589 case IGMP_SG_QUERY_PENDING_MEMBER:
2590 if (igi->igi_version == IGMP_VERSION_2) {
2591 #ifdef INVARIANTS
2592 if (inm->inm_state == IGMP_G_QUERY_PENDING_MEMBER ||
2593 inm->inm_state == IGMP_SG_QUERY_PENDING_MEMBER)
2594 panic("%s: IGMPv3 state reached, not IGMPv3 mode",
2595 __func__);
2596 #endif
2597 igmp_v1v2_queue_report(inm, IGMP_HOST_LEAVE_MESSAGE);
2598 inm->inm_state = IGMP_NOT_MEMBER;
2599 } else if (igi->igi_version == IGMP_VERSION_3) {
2600 /*
2601 * Stop group timer and all pending reports.
2602 * Immediately enqueue a state-change report
2603 * TO_IN {} to be sent on the next fast timeout,
2604 * giving us an opportunity to merge reports.
2605 */
2606 mbufq_drain(&inm->inm_scq);
2607 inm->inm_timer = 0;
2608 if (igi->igi_flags & IGIF_LOOPBACK) {
2609 inm->inm_scrv = 1;
2610 } else {
2611 inm->inm_scrv = igi->igi_rv;
2612 }
2613 CTR4(KTR_IGMPV3, "%s: Leaving 0x%08x/%s with %d "
2614 "pending retransmissions.", __func__,
2615 ntohl(inm->inm_addr.s_addr),
2616 inm->inm_ifp->if_xname, inm->inm_scrv);
2617 if (inm->inm_scrv == 0) {
2618 inm->inm_state = IGMP_NOT_MEMBER;
2619 inm->inm_sctimer = 0;
2620 } else {
2621 int retval __unused;
2622
2623 inm_acquire_locked(inm);
2624
2625 retval = igmp_v3_enqueue_group_record(
2626 &inm->inm_scq, inm, 1, 0, 0);
2627 KASSERT(retval != 0,
2628 ("%s: enqueue record = %d", __func__,
2629 retval));
2630
2631 inm->inm_state = IGMP_LEAVING_MEMBER;
2632 inm->inm_sctimer = 1;
2633 V_state_change_timers_running = 1;
2634 syncstates = 0;
2635 }
2636 break;
2637 }
2638 break;
2639 case IGMP_LAZY_MEMBER:
2640 case IGMP_SLEEPING_MEMBER:
2641 case IGMP_AWAKENING_MEMBER:
2642 /* Our reports are suppressed; do nothing. */
2643 break;
2644 }
2645
2646 if (syncstates) {
2647 inm_commit(inm);
2648 CTR3(KTR_IGMPV3, "%s: T1 -> T0 for 0x%08x/%s", __func__,
2649 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2650 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
2651 CTR3(KTR_IGMPV3, "%s: T1 now MCAST_UNDEFINED for 0x%08x/%s",
2652 __func__, ntohl(inm->inm_addr.s_addr),
2653 inm->inm_ifp->if_xname);
2654 }
2655 }
2656
2657 /*
2658 * Enqueue an IGMPv3 group record to the given output queue.
2659 *
2660 * XXX This function could do with having the allocation code
2661 * split out, and the multiple-tree-walks coalesced into a single
2662 * routine as has been done in igmp_v3_enqueue_filter_change().
2663 *
2664 * If is_state_change is zero, a current-state record is appended.
2665 * If is_state_change is non-zero, a state-change report is appended.
2666 *
2667 * If is_group_query is non-zero, an mbuf packet chain is allocated.
2668 * If is_group_query is zero, and if there is a packet with free space
2669 * at the tail of the queue, it will be appended to providing there
2670 * is enough free space.
2671 * Otherwise a new mbuf packet chain is allocated.
2672 *
2673 * If is_source_query is non-zero, each source is checked to see if
2674 * it was recorded for a Group-Source query, and will be omitted if
2675 * it is not both in-mode and recorded.
2676 *
2677 * The function will attempt to allocate leading space in the packet
2678 * for the IP/IGMP header to be prepended without fragmenting the chain.
2679 *
2680 * If successful the size of all data appended to the queue is returned,
2681 * otherwise an error code less than zero is returned, or zero if
2682 * no record(s) were appended.
2683 */
2684 static int
igmp_v3_enqueue_group_record(struct mbufq * mq,struct in_multi * inm,const int is_state_change,const int is_group_query,const int is_source_query)2685 igmp_v3_enqueue_group_record(struct mbufq *mq, struct in_multi *inm,
2686 const int is_state_change, const int is_group_query,
2687 const int is_source_query)
2688 {
2689 struct igmp_grouprec ig;
2690 struct igmp_grouprec *pig;
2691 struct ifnet *ifp;
2692 struct ip_msource *ims, *nims;
2693 struct mbuf *m0, *m, *md;
2694 int is_filter_list_change;
2695 int minrec0len, m0srcs, msrcs, nbytes, off;
2696 int record_has_sources;
2697 int now;
2698 int type;
2699 in_addr_t naddr;
2700 uint8_t mode;
2701
2702 IN_MULTI_LIST_LOCK_ASSERT();
2703
2704 ifp = inm->inm_ifp;
2705 is_filter_list_change = 0;
2706 m = NULL;
2707 m0 = NULL;
2708 m0srcs = 0;
2709 msrcs = 0;
2710 nbytes = 0;
2711 nims = NULL;
2712 record_has_sources = 1;
2713 pig = NULL;
2714 type = IGMP_DO_NOTHING;
2715 mode = inm->inm_st[1].iss_fmode;
2716
2717 /*
2718 * If we did not transition out of ASM mode during t0->t1,
2719 * and there are no source nodes to process, we can skip
2720 * the generation of source records.
2721 */
2722 if (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0 &&
2723 inm->inm_nsrc == 0)
2724 record_has_sources = 0;
2725
2726 if (is_state_change) {
2727 /*
2728 * Queue a state change record.
2729 * If the mode did not change, and there are non-ASM
2730 * listeners or source filters present,
2731 * we potentially need to issue two records for the group.
2732 * If we are transitioning to MCAST_UNDEFINED, we need
2733 * not send any sources.
2734 * If there are ASM listeners, and there was no filter
2735 * mode transition of any kind, do nothing.
2736 */
2737 if (mode != inm->inm_st[0].iss_fmode) {
2738 if (mode == MCAST_EXCLUDE) {
2739 CTR1(KTR_IGMPV3, "%s: change to EXCLUDE",
2740 __func__);
2741 type = IGMP_CHANGE_TO_EXCLUDE_MODE;
2742 } else {
2743 CTR1(KTR_IGMPV3, "%s: change to INCLUDE",
2744 __func__);
2745 type = IGMP_CHANGE_TO_INCLUDE_MODE;
2746 if (mode == MCAST_UNDEFINED)
2747 record_has_sources = 0;
2748 }
2749 } else {
2750 if (record_has_sources) {
2751 is_filter_list_change = 1;
2752 } else {
2753 type = IGMP_DO_NOTHING;
2754 }
2755 }
2756 } else {
2757 /*
2758 * Queue a current state record.
2759 */
2760 if (mode == MCAST_EXCLUDE) {
2761 type = IGMP_MODE_IS_EXCLUDE;
2762 } else if (mode == MCAST_INCLUDE) {
2763 type = IGMP_MODE_IS_INCLUDE;
2764 KASSERT(inm->inm_st[1].iss_asm == 0,
2765 ("%s: inm %p is INCLUDE but ASM count is %d",
2766 __func__, inm, inm->inm_st[1].iss_asm));
2767 }
2768 }
2769
2770 /*
2771 * Generate the filter list changes using a separate function.
2772 */
2773 if (is_filter_list_change)
2774 return (igmp_v3_enqueue_filter_change(mq, inm));
2775
2776 if (type == IGMP_DO_NOTHING) {
2777 CTR3(KTR_IGMPV3, "%s: nothing to do for 0x%08x/%s", __func__,
2778 ntohl(inm->inm_addr.s_addr), inm->inm_ifp->if_xname);
2779 return (0);
2780 }
2781
2782 /*
2783 * If any sources are present, we must be able to fit at least
2784 * one in the trailing space of the tail packet's mbuf,
2785 * ideally more.
2786 */
2787 minrec0len = sizeof(struct igmp_grouprec);
2788 if (record_has_sources)
2789 minrec0len += sizeof(in_addr_t);
2790
2791 CTR4(KTR_IGMPV3, "%s: queueing %s for 0x%08x/%s", __func__,
2792 igmp_rec_type_to_str(type), ntohl(inm->inm_addr.s_addr),
2793 inm->inm_ifp->if_xname);
2794
2795 /*
2796 * Check if we have a packet in the tail of the queue for this
2797 * group into which the first group record for this group will fit.
2798 * Otherwise allocate a new packet.
2799 * Always allocate leading space for IP+RA_OPT+IGMP+REPORT.
2800 * Note: Group records for G/GSR query responses MUST be sent
2801 * in their own packet.
2802 */
2803 m0 = mbufq_last(mq);
2804 if (!is_group_query &&
2805 m0 != NULL &&
2806 (m0->m_pkthdr.vt_nrecs + 1 <= IGMP_V3_REPORT_MAXRECS) &&
2807 (m0->m_pkthdr.len + minrec0len) <
2808 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
2809 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
2810 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2811 m = m0;
2812 CTR1(KTR_IGMPV3, "%s: use existing packet", __func__);
2813 } else {
2814 if (mbufq_full(mq)) {
2815 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2816 return (-ENOMEM);
2817 }
2818 m = NULL;
2819 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2820 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2821 if (!is_state_change && !is_group_query) {
2822 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2823 if (m)
2824 m->m_data += IGMP_LEADINGSPACE;
2825 }
2826 if (m == NULL) {
2827 m = m_gethdr(M_NOWAIT, MT_DATA);
2828 if (m)
2829 M_ALIGN(m, IGMP_LEADINGSPACE);
2830 }
2831 if (m == NULL)
2832 return (-ENOMEM);
2833
2834 igmp_save_context(m, ifp);
2835
2836 CTR1(KTR_IGMPV3, "%s: allocated first packet", __func__);
2837 }
2838
2839 /*
2840 * Append group record.
2841 * If we have sources, we don't know how many yet.
2842 */
2843 ig.ig_type = type;
2844 ig.ig_datalen = 0;
2845 ig.ig_numsrc = 0;
2846 ig.ig_group = inm->inm_addr;
2847 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2848 if (m != m0)
2849 m_freem(m);
2850 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2851 return (-ENOMEM);
2852 }
2853 nbytes += sizeof(struct igmp_grouprec);
2854
2855 /*
2856 * Append as many sources as will fit in the first packet.
2857 * If we are appending to a new packet, the chain allocation
2858 * may potentially use clusters; use m_getptr() in this case.
2859 * If we are appending to an existing packet, we need to obtain
2860 * a pointer to the group record after m_append(), in case a new
2861 * mbuf was allocated.
2862 * Only append sources which are in-mode at t1. If we are
2863 * transitioning to MCAST_UNDEFINED state on the group, do not
2864 * include source entries.
2865 * Only report recorded sources in our filter set when responding
2866 * to a group-source query.
2867 */
2868 if (record_has_sources) {
2869 if (m == m0) {
2870 md = m_last(m);
2871 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2872 md->m_len - nbytes);
2873 } else {
2874 md = m_getptr(m, 0, &off);
2875 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) +
2876 off);
2877 }
2878 msrcs = 0;
2879 RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, nims) {
2880 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2881 ims->ims_haddr);
2882 now = ims_get_mode(inm, ims, 1);
2883 CTR2(KTR_IGMPV3, "%s: node is %d", __func__, now);
2884 if ((now != mode) ||
2885 (now == mode && mode == MCAST_UNDEFINED)) {
2886 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2887 continue;
2888 }
2889 if (is_source_query && ims->ims_stp == 0) {
2890 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2891 __func__);
2892 continue;
2893 }
2894 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2895 naddr = htonl(ims->ims_haddr);
2896 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2897 if (m != m0)
2898 m_freem(m);
2899 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2900 __func__);
2901 return (-ENOMEM);
2902 }
2903 nbytes += sizeof(in_addr_t);
2904 ++msrcs;
2905 if (msrcs == m0srcs)
2906 break;
2907 }
2908 CTR2(KTR_IGMPV3, "%s: msrcs is %d this packet", __func__,
2909 msrcs);
2910 pig->ig_numsrc = htons(msrcs);
2911 nbytes += (msrcs * sizeof(in_addr_t));
2912 }
2913
2914 if (is_source_query && msrcs == 0) {
2915 CTR1(KTR_IGMPV3, "%s: no recorded sources to report", __func__);
2916 if (m != m0)
2917 m_freem(m);
2918 return (0);
2919 }
2920
2921 /*
2922 * We are good to go with first packet.
2923 */
2924 if (m != m0) {
2925 CTR1(KTR_IGMPV3, "%s: enqueueing first packet", __func__);
2926 m->m_pkthdr.vt_nrecs = 1;
2927 mbufq_enqueue(mq, m);
2928 } else
2929 m->m_pkthdr.vt_nrecs++;
2930
2931 /*
2932 * No further work needed if no source list in packet(s).
2933 */
2934 if (!record_has_sources)
2935 return (nbytes);
2936
2937 /*
2938 * Whilst sources remain to be announced, we need to allocate
2939 * a new packet and fill out as many sources as will fit.
2940 * Always try for a cluster first.
2941 */
2942 while (nims != NULL) {
2943 if (mbufq_full(mq)) {
2944 CTR1(KTR_IGMPV3, "%s: outbound queue full", __func__);
2945 return (-ENOMEM);
2946 }
2947 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2948 if (m)
2949 m->m_data += IGMP_LEADINGSPACE;
2950 if (m == NULL) {
2951 m = m_gethdr(M_NOWAIT, MT_DATA);
2952 if (m)
2953 M_ALIGN(m, IGMP_LEADINGSPACE);
2954 }
2955 if (m == NULL)
2956 return (-ENOMEM);
2957 igmp_save_context(m, ifp);
2958 md = m_getptr(m, 0, &off);
2959 pig = (struct igmp_grouprec *)(mtod(md, uint8_t *) + off);
2960 CTR1(KTR_IGMPV3, "%s: allocated next packet", __func__);
2961
2962 if (!m_append(m, sizeof(struct igmp_grouprec), (void *)&ig)) {
2963 if (m != m0)
2964 m_freem(m);
2965 CTR1(KTR_IGMPV3, "%s: m_append() failed.", __func__);
2966 return (-ENOMEM);
2967 }
2968 m->m_pkthdr.vt_nrecs = 1;
2969 nbytes += sizeof(struct igmp_grouprec);
2970
2971 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
2972 sizeof(struct igmp_grouprec)) / sizeof(in_addr_t);
2973
2974 msrcs = 0;
2975 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
2976 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
2977 ims->ims_haddr);
2978 now = ims_get_mode(inm, ims, 1);
2979 if ((now != mode) ||
2980 (now == mode && mode == MCAST_UNDEFINED)) {
2981 CTR1(KTR_IGMPV3, "%s: skip node", __func__);
2982 continue;
2983 }
2984 if (is_source_query && ims->ims_stp == 0) {
2985 CTR1(KTR_IGMPV3, "%s: skip unrecorded node",
2986 __func__);
2987 continue;
2988 }
2989 CTR1(KTR_IGMPV3, "%s: append node", __func__);
2990 naddr = htonl(ims->ims_haddr);
2991 if (!m_append(m, sizeof(in_addr_t), (void *)&naddr)) {
2992 if (m != m0)
2993 m_freem(m);
2994 CTR1(KTR_IGMPV3, "%s: m_append() failed.",
2995 __func__);
2996 return (-ENOMEM);
2997 }
2998 ++msrcs;
2999 if (msrcs == m0srcs)
3000 break;
3001 }
3002 pig->ig_numsrc = htons(msrcs);
3003 nbytes += (msrcs * sizeof(in_addr_t));
3004
3005 CTR1(KTR_IGMPV3, "%s: enqueueing next packet", __func__);
3006 mbufq_enqueue(mq, m);
3007 }
3008
3009 return (nbytes);
3010 }
3011
3012 /*
3013 * Type used to mark record pass completion.
3014 * We exploit the fact we can cast to this easily from the
3015 * current filter modes on each ip_msource node.
3016 */
3017 typedef enum {
3018 REC_NONE = 0x00, /* MCAST_UNDEFINED */
3019 REC_ALLOW = 0x01, /* MCAST_INCLUDE */
3020 REC_BLOCK = 0x02, /* MCAST_EXCLUDE */
3021 REC_FULL = REC_ALLOW | REC_BLOCK
3022 } rectype_t;
3023
3024 /*
3025 * Enqueue an IGMPv3 filter list change to the given output queue.
3026 *
3027 * Source list filter state is held in an RB-tree. When the filter list
3028 * for a group is changed without changing its mode, we need to compute
3029 * the deltas between T0 and T1 for each source in the filter set,
3030 * and enqueue the appropriate ALLOW_NEW/BLOCK_OLD records.
3031 *
3032 * As we may potentially queue two record types, and the entire R-B tree
3033 * needs to be walked at once, we break this out into its own function
3034 * so we can generate a tightly packed queue of packets.
3035 *
3036 * XXX This could be written to only use one tree walk, although that makes
3037 * serializing into the mbuf chains a bit harder. For now we do two walks
3038 * which makes things easier on us, and it may or may not be harder on
3039 * the L2 cache.
3040 *
3041 * If successful the size of all data appended to the queue is returned,
3042 * otherwise an error code less than zero is returned, or zero if
3043 * no record(s) were appended.
3044 */
3045 static int
igmp_v3_enqueue_filter_change(struct mbufq * mq,struct in_multi * inm)3046 igmp_v3_enqueue_filter_change(struct mbufq *mq, struct in_multi *inm)
3047 {
3048 static const int MINRECLEN =
3049 sizeof(struct igmp_grouprec) + sizeof(in_addr_t);
3050 struct ifnet *ifp;
3051 struct igmp_grouprec ig;
3052 struct igmp_grouprec *pig;
3053 struct ip_msource *ims, *nims;
3054 struct mbuf *m, *m0, *md;
3055 in_addr_t naddr;
3056 int m0srcs, nbytes, npbytes, off, rsrcs, schanged;
3057 #ifdef KTR
3058 int nallow, nblock;
3059 #endif
3060 uint8_t mode, now, then;
3061 rectype_t crt, drt, nrt;
3062
3063 IN_MULTI_LIST_LOCK_ASSERT();
3064
3065 if (inm->inm_nsrc == 0 ||
3066 (inm->inm_st[0].iss_asm > 0 && inm->inm_st[1].iss_asm > 0))
3067 return (0);
3068
3069 ifp = inm->inm_ifp; /* interface */
3070 mode = inm->inm_st[1].iss_fmode; /* filter mode at t1 */
3071 crt = REC_NONE; /* current group record type */
3072 drt = REC_NONE; /* mask of completed group record types */
3073 nrt = REC_NONE; /* record type for current node */
3074 m0srcs = 0; /* # source which will fit in current mbuf chain */
3075 nbytes = 0; /* # of bytes appended to group's state-change queue */
3076 npbytes = 0; /* # of bytes appended this packet */
3077 rsrcs = 0; /* # sources encoded in current record */
3078 schanged = 0; /* # nodes encoded in overall filter change */
3079 #ifdef KTR
3080 nallow = 0; /* # of source entries in ALLOW_NEW */
3081 nblock = 0; /* # of source entries in BLOCK_OLD */
3082 #endif
3083 nims = NULL; /* next tree node pointer */
3084
3085 /*
3086 * For each possible filter record mode.
3087 * The first kind of source we encounter tells us which
3088 * is the first kind of record we start appending.
3089 * If a node transitioned to UNDEFINED at t1, its mode is treated
3090 * as the inverse of the group's filter mode.
3091 */
3092 while (drt != REC_FULL) {
3093 do {
3094 m0 = mbufq_last(mq);
3095 if (m0 != NULL &&
3096 (m0->m_pkthdr.vt_nrecs + 1 <=
3097 IGMP_V3_REPORT_MAXRECS) &&
3098 (m0->m_pkthdr.len + MINRECLEN) <
3099 (ifp->if_mtu - IGMP_LEADINGSPACE)) {
3100 m = m0;
3101 m0srcs = (ifp->if_mtu - m0->m_pkthdr.len -
3102 sizeof(struct igmp_grouprec)) /
3103 sizeof(in_addr_t);
3104 CTR1(KTR_IGMPV3,
3105 "%s: use previous packet", __func__);
3106 } else {
3107 m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3108 if (m)
3109 m->m_data += IGMP_LEADINGSPACE;
3110 if (m == NULL) {
3111 m = m_gethdr(M_NOWAIT, MT_DATA);
3112 if (m)
3113 M_ALIGN(m, IGMP_LEADINGSPACE);
3114 }
3115 if (m == NULL) {
3116 CTR1(KTR_IGMPV3,
3117 "%s: m_get*() failed", __func__);
3118 return (-ENOMEM);
3119 }
3120 m->m_pkthdr.vt_nrecs = 0;
3121 igmp_save_context(m, ifp);
3122 m0srcs = (ifp->if_mtu - IGMP_LEADINGSPACE -
3123 sizeof(struct igmp_grouprec)) /
3124 sizeof(in_addr_t);
3125 npbytes = 0;
3126 CTR1(KTR_IGMPV3,
3127 "%s: allocated new packet", __func__);
3128 }
3129 /*
3130 * Append the IGMP group record header to the
3131 * current packet's data area.
3132 * Recalculate pointer to free space for next
3133 * group record, in case m_append() allocated
3134 * a new mbuf or cluster.
3135 */
3136 memset(&ig, 0, sizeof(ig));
3137 ig.ig_group = inm->inm_addr;
3138 if (!m_append(m, sizeof(ig), (void *)&ig)) {
3139 if (m != m0)
3140 m_freem(m);
3141 CTR1(KTR_IGMPV3,
3142 "%s: m_append() failed", __func__);
3143 return (-ENOMEM);
3144 }
3145 npbytes += sizeof(struct igmp_grouprec);
3146 if (m != m0) {
3147 /* new packet; offset in c hain */
3148 md = m_getptr(m, npbytes -
3149 sizeof(struct igmp_grouprec), &off);
3150 pig = (struct igmp_grouprec *)(mtod(md,
3151 uint8_t *) + off);
3152 } else {
3153 /* current packet; offset from last append */
3154 md = m_last(m);
3155 pig = (struct igmp_grouprec *)(mtod(md,
3156 uint8_t *) + md->m_len -
3157 sizeof(struct igmp_grouprec));
3158 }
3159 /*
3160 * Begin walking the tree for this record type
3161 * pass, or continue from where we left off
3162 * previously if we had to allocate a new packet.
3163 * Only report deltas in-mode at t1.
3164 * We need not report included sources as allowed
3165 * if we are in inclusive mode on the group,
3166 * however the converse is not true.
3167 */
3168 rsrcs = 0;
3169 if (nims == NULL)
3170 nims = RB_MIN(ip_msource_tree, &inm->inm_srcs);
3171 RB_FOREACH_FROM(ims, ip_msource_tree, nims) {
3172 CTR2(KTR_IGMPV3, "%s: visit node 0x%08x",
3173 __func__, ims->ims_haddr);
3174 now = ims_get_mode(inm, ims, 1);
3175 then = ims_get_mode(inm, ims, 0);
3176 CTR3(KTR_IGMPV3, "%s: mode: t0 %d, t1 %d",
3177 __func__, then, now);
3178 if (now == then) {
3179 CTR1(KTR_IGMPV3,
3180 "%s: skip unchanged", __func__);
3181 continue;
3182 }
3183 if (mode == MCAST_EXCLUDE &&
3184 now == MCAST_INCLUDE) {
3185 CTR1(KTR_IGMPV3,
3186 "%s: skip IN src on EX group",
3187 __func__);
3188 continue;
3189 }
3190 nrt = (rectype_t)now;
3191 if (nrt == REC_NONE)
3192 nrt = (rectype_t)(~mode & REC_FULL);
3193 if (schanged++ == 0) {
3194 crt = nrt;
3195 } else if (crt != nrt)
3196 continue;
3197 naddr = htonl(ims->ims_haddr);
3198 if (!m_append(m, sizeof(in_addr_t),
3199 (void *)&naddr)) {
3200 if (m != m0)
3201 m_freem(m);
3202 CTR1(KTR_IGMPV3,
3203 "%s: m_append() failed", __func__);
3204 return (-ENOMEM);
3205 }
3206 #ifdef KTR
3207 nallow += !!(crt == REC_ALLOW);
3208 nblock += !!(crt == REC_BLOCK);
3209 #endif
3210 if (++rsrcs == m0srcs)
3211 break;
3212 }
3213 /*
3214 * If we did not append any tree nodes on this
3215 * pass, back out of allocations.
3216 */
3217 if (rsrcs == 0) {
3218 npbytes -= sizeof(struct igmp_grouprec);
3219 if (m != m0) {
3220 CTR1(KTR_IGMPV3,
3221 "%s: m_free(m)", __func__);
3222 m_freem(m);
3223 } else {
3224 CTR1(KTR_IGMPV3,
3225 "%s: m_adj(m, -ig)", __func__);
3226 m_adj(m, -((int)sizeof(
3227 struct igmp_grouprec)));
3228 }
3229 continue;
3230 }
3231 npbytes += (rsrcs * sizeof(in_addr_t));
3232 if (crt == REC_ALLOW)
3233 pig->ig_type = IGMP_ALLOW_NEW_SOURCES;
3234 else if (crt == REC_BLOCK)
3235 pig->ig_type = IGMP_BLOCK_OLD_SOURCES;
3236 pig->ig_numsrc = htons(rsrcs);
3237 /*
3238 * Count the new group record, and enqueue this
3239 * packet if it wasn't already queued.
3240 */
3241 m->m_pkthdr.vt_nrecs++;
3242 if (m != m0)
3243 mbufq_enqueue(mq, m);
3244 nbytes += npbytes;
3245 } while (nims != NULL);
3246 drt |= crt;
3247 crt = (~crt & REC_FULL);
3248 }
3249
3250 CTR3(KTR_IGMPV3, "%s: queued %d ALLOW_NEW, %d BLOCK_OLD", __func__,
3251 nallow, nblock);
3252
3253 return (nbytes);
3254 }
3255
3256 static int
igmp_v3_merge_state_changes(struct in_multi * inm,struct mbufq * scq)3257 igmp_v3_merge_state_changes(struct in_multi *inm, struct mbufq *scq)
3258 {
3259 struct mbufq *gq;
3260 struct mbuf *m; /* pending state-change */
3261 struct mbuf *m0; /* copy of pending state-change */
3262 struct mbuf *mt; /* last state-change in packet */
3263 int docopy, domerge;
3264 u_int recslen;
3265
3266 docopy = 0;
3267 domerge = 0;
3268 recslen = 0;
3269
3270 IN_MULTI_LIST_LOCK_ASSERT();
3271 IGMP_LOCK_ASSERT();
3272
3273 /*
3274 * If there are further pending retransmissions, make a writable
3275 * copy of each queued state-change message before merging.
3276 */
3277 if (inm->inm_scrv > 0)
3278 docopy = 1;
3279
3280 gq = &inm->inm_scq;
3281 #ifdef KTR
3282 if (mbufq_first(gq) == NULL) {
3283 CTR2(KTR_IGMPV3, "%s: WARNING: queue for inm %p is empty",
3284 __func__, inm);
3285 }
3286 #endif
3287
3288 m = mbufq_first(gq);
3289 while (m != NULL) {
3290 /*
3291 * Only merge the report into the current packet if
3292 * there is sufficient space to do so; an IGMPv3 report
3293 * packet may only contain 65,535 group records.
3294 * Always use a simple mbuf chain concatentation to do this,
3295 * as large state changes for single groups may have
3296 * allocated clusters.
3297 */
3298 domerge = 0;
3299 mt = mbufq_last(scq);
3300 if (mt != NULL) {
3301 recslen = m_length(m, NULL);
3302
3303 if ((mt->m_pkthdr.vt_nrecs +
3304 m->m_pkthdr.vt_nrecs <=
3305 IGMP_V3_REPORT_MAXRECS) &&
3306 (mt->m_pkthdr.len + recslen <=
3307 (inm->inm_ifp->if_mtu - IGMP_LEADINGSPACE)))
3308 domerge = 1;
3309 }
3310
3311 if (!domerge && mbufq_full(gq)) {
3312 CTR2(KTR_IGMPV3,
3313 "%s: outbound queue full, skipping whole packet %p",
3314 __func__, m);
3315 mt = m->m_nextpkt;
3316 if (!docopy)
3317 m_freem(m);
3318 m = mt;
3319 continue;
3320 }
3321
3322 if (!docopy) {
3323 CTR2(KTR_IGMPV3, "%s: dequeueing %p", __func__, m);
3324 m0 = mbufq_dequeue(gq);
3325 m = m0->m_nextpkt;
3326 } else {
3327 CTR2(KTR_IGMPV3, "%s: copying %p", __func__, m);
3328 m0 = m_dup(m, M_NOWAIT);
3329 if (m0 == NULL)
3330 return (ENOMEM);
3331 m0->m_nextpkt = NULL;
3332 m = m->m_nextpkt;
3333 }
3334
3335 if (!domerge) {
3336 CTR3(KTR_IGMPV3, "%s: queueing %p to scq %p)",
3337 __func__, m0, scq);
3338 mbufq_enqueue(scq, m0);
3339 } else {
3340 struct mbuf *mtl; /* last mbuf of packet mt */
3341
3342 CTR3(KTR_IGMPV3, "%s: merging %p with scq tail %p)",
3343 __func__, m0, mt);
3344
3345 mtl = m_last(mt);
3346 m0->m_flags &= ~M_PKTHDR;
3347 mt->m_pkthdr.len += recslen;
3348 mt->m_pkthdr.vt_nrecs +=
3349 m0->m_pkthdr.vt_nrecs;
3350
3351 mtl->m_next = m0;
3352 }
3353 }
3354
3355 return (0);
3356 }
3357
3358 /*
3359 * Respond to a pending IGMPv3 General Query.
3360 */
3361 static void
igmp_v3_dispatch_general_query(struct igmp_ifsoftc * igi)3362 igmp_v3_dispatch_general_query(struct igmp_ifsoftc *igi)
3363 {
3364 struct ifmultiaddr *ifma;
3365 struct ifnet *ifp;
3366 struct in_multi *inm;
3367 int retval __unused, loop;
3368
3369 IN_MULTI_LIST_LOCK_ASSERT();
3370 IGMP_LOCK_ASSERT();
3371 NET_EPOCH_ASSERT();
3372
3373 KASSERT(igi->igi_version == IGMP_VERSION_3,
3374 ("%s: called when version %d", __func__, igi->igi_version));
3375
3376 /*
3377 * Check that there are some packets queued. If so, send them first.
3378 * For large number of groups the reply to general query can take
3379 * many packets, we should finish sending them before starting of
3380 * queuing the new reply.
3381 */
3382 if (!mbufq_empty(&igi->igi_gq))
3383 goto send;
3384
3385 ifp = igi->igi_ifp;
3386
3387 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
3388 inm = inm_ifmultiaddr_get_inm(ifma);
3389 if (inm == NULL)
3390 continue;
3391 KASSERT(ifp == inm->inm_ifp,
3392 ("%s: inconsistent ifp", __func__));
3393
3394 switch (inm->inm_state) {
3395 case IGMP_NOT_MEMBER:
3396 case IGMP_SILENT_MEMBER:
3397 break;
3398 case IGMP_REPORTING_MEMBER:
3399 case IGMP_IDLE_MEMBER:
3400 case IGMP_LAZY_MEMBER:
3401 case IGMP_SLEEPING_MEMBER:
3402 case IGMP_AWAKENING_MEMBER:
3403 inm->inm_state = IGMP_REPORTING_MEMBER;
3404 retval = igmp_v3_enqueue_group_record(&igi->igi_gq,
3405 inm, 0, 0, 0);
3406 CTR2(KTR_IGMPV3, "%s: enqueue record = %d",
3407 __func__, retval);
3408 break;
3409 case IGMP_G_QUERY_PENDING_MEMBER:
3410 case IGMP_SG_QUERY_PENDING_MEMBER:
3411 case IGMP_LEAVING_MEMBER:
3412 break;
3413 }
3414 }
3415
3416 send:
3417 loop = (igi->igi_flags & IGIF_LOOPBACK) ? 1 : 0;
3418 igmp_dispatch_queue(&igi->igi_gq, IGMP_MAX_RESPONSE_BURST, loop);
3419
3420 /*
3421 * Slew transmission of bursts over 500ms intervals.
3422 */
3423 if (mbufq_first(&igi->igi_gq) != NULL) {
3424 igi->igi_v3_timer = 1 + IGMP_RANDOM_DELAY(
3425 IGMP_RESPONSE_BURST_INTERVAL);
3426 V_interface_timers_running = 1;
3427 }
3428 }
3429
3430 /*
3431 * Transmit the next pending IGMP message in the output queue.
3432 *
3433 * We get called from netisr_processqueue(). A mutex private to igmpoq
3434 * will be acquired and released around this routine.
3435 *
3436 * VIMAGE: Needs to store/restore vnet pointer on a per-mbuf-chain basis.
3437 * MRT: Nothing needs to be done, as IGMP traffic is always local to
3438 * a link and uses a link-scope multicast address.
3439 */
3440 static void
igmp_intr(struct mbuf * m)3441 igmp_intr(struct mbuf *m)
3442 {
3443 struct ip_moptions imo;
3444 struct ifnet *ifp;
3445 struct mbuf *ipopts, *m0;
3446 int error;
3447 uint32_t ifindex;
3448
3449 CTR2(KTR_IGMPV3, "%s: transmit %p", __func__, m);
3450
3451 /*
3452 * Set VNET image pointer from enqueued mbuf chain
3453 * before doing anything else. Whilst we use interface
3454 * indexes to guard against interface detach, they are
3455 * unique to each VIMAGE and must be retrieved.
3456 */
3457 CURVNET_SET((struct vnet *)(m->m_pkthdr.PH_loc.ptr));
3458 ifindex = igmp_restore_context(m);
3459
3460 /*
3461 * Check if the ifnet still exists. This limits the scope of
3462 * any race in the absence of a global ifp lock for low cost
3463 * (an array lookup).
3464 */
3465 ifp = ifnet_byindex(ifindex);
3466 if (ifp == NULL) {
3467 CTR3(KTR_IGMPV3, "%s: dropped %p as ifindex %u went away.",
3468 __func__, m, ifindex);
3469 m_freem(m);
3470 IPSTAT_INC(ips_noroute);
3471 goto out;
3472 }
3473
3474 ipopts = V_igmp_sendra ? m_raopt : NULL;
3475
3476 imo.imo_multicast_ttl = 1;
3477 imo.imo_multicast_vif = -1;
3478 imo.imo_multicast_loop = (V_ip_mrouter != NULL);
3479
3480 /*
3481 * If the user requested that IGMP traffic be explicitly
3482 * redirected to the loopback interface (e.g. they are running a
3483 * MANET interface and the routing protocol needs to see the
3484 * updates), handle this now.
3485 */
3486 if (m->m_flags & M_IGMP_LOOP)
3487 imo.imo_multicast_ifp = V_loif;
3488 else
3489 imo.imo_multicast_ifp = ifp;
3490
3491 if (m->m_flags & M_IGMPV2) {
3492 m0 = m;
3493 } else {
3494 m0 = igmp_v3_encap_report(ifp, m);
3495 if (m0 == NULL) {
3496 CTR2(KTR_IGMPV3, "%s: dropped %p", __func__, m);
3497 m_freem(m);
3498 IPSTAT_INC(ips_odropped);
3499 goto out;
3500 }
3501 }
3502
3503 igmp_scrub_context(m0);
3504 m_clrprotoflags(m);
3505 m0->m_pkthdr.rcvif = V_loif;
3506 #ifdef MAC
3507 mac_netinet_igmp_send(ifp, m0);
3508 #endif
3509 error = ip_output(m0, ipopts, NULL, 0, &imo, NULL);
3510 if (error) {
3511 CTR3(KTR_IGMPV3, "%s: ip_output(%p) = %d", __func__, m0, error);
3512 goto out;
3513 }
3514
3515 IGMPSTAT_INC(igps_snd_reports);
3516
3517 out:
3518 /*
3519 * We must restore the existing vnet pointer before
3520 * continuing as we are run from netisr context.
3521 */
3522 CURVNET_RESTORE();
3523 }
3524
3525 /*
3526 * Encapsulate an IGMPv3 report.
3527 *
3528 * The internal mbuf flag M_IGMPV3_HDR is used to indicate that the mbuf
3529 * chain has already had its IP/IGMPv3 header prepended. In this case
3530 * the function will not attempt to prepend; the lengths and checksums
3531 * will however be re-computed.
3532 *
3533 * Returns a pointer to the new mbuf chain head, or NULL if the
3534 * allocation failed.
3535 */
3536 static struct mbuf *
igmp_v3_encap_report(struct ifnet * ifp,struct mbuf * m)3537 igmp_v3_encap_report(struct ifnet *ifp, struct mbuf *m)
3538 {
3539 struct igmp_report *igmp;
3540 struct ip *ip;
3541 int hdrlen, igmpreclen;
3542
3543 KASSERT((m->m_flags & M_PKTHDR),
3544 ("%s: mbuf chain %p is !M_PKTHDR", __func__, m));
3545
3546 igmpreclen = m_length(m, NULL);
3547 hdrlen = sizeof(struct ip) + sizeof(struct igmp_report);
3548
3549 if (m->m_flags & M_IGMPV3_HDR) {
3550 igmpreclen -= hdrlen;
3551 } else {
3552 M_PREPEND(m, hdrlen, M_NOWAIT);
3553 if (m == NULL)
3554 return (NULL);
3555 m->m_flags |= M_IGMPV3_HDR;
3556 }
3557
3558 CTR2(KTR_IGMPV3, "%s: igmpreclen is %d", __func__, igmpreclen);
3559
3560 m->m_data += sizeof(struct ip);
3561 m->m_len -= sizeof(struct ip);
3562
3563 igmp = mtod(m, struct igmp_report *);
3564 igmp->ir_type = IGMP_v3_HOST_MEMBERSHIP_REPORT;
3565 igmp->ir_rsv1 = 0;
3566 igmp->ir_rsv2 = 0;
3567 igmp->ir_numgrps = htons(m->m_pkthdr.vt_nrecs);
3568 igmp->ir_cksum = 0;
3569 igmp->ir_cksum = in_cksum(m, sizeof(struct igmp_report) + igmpreclen);
3570 m->m_pkthdr.vt_nrecs = 0;
3571
3572 m->m_data -= sizeof(struct ip);
3573 m->m_len += sizeof(struct ip);
3574
3575 ip = mtod(m, struct ip *);
3576 ip->ip_tos = IPTOS_PREC_INTERNETCONTROL;
3577 ip->ip_len = htons(hdrlen + igmpreclen);
3578 ip->ip_off = htons(IP_DF);
3579 ip->ip_p = IPPROTO_IGMP;
3580 ip->ip_sum = 0;
3581
3582 ip->ip_src.s_addr = INADDR_ANY;
3583
3584 if (m->m_flags & M_IGMP_LOOP) {
3585 struct in_ifaddr *ia;
3586
3587 IFP_TO_IA(ifp, ia);
3588 if (ia != NULL)
3589 ip->ip_src = ia->ia_addr.sin_addr;
3590 }
3591
3592 ip->ip_dst.s_addr = htonl(INADDR_ALLRPTS_GROUP);
3593
3594 return (m);
3595 }
3596
3597 #ifdef KTR
3598 static char *
igmp_rec_type_to_str(const int type)3599 igmp_rec_type_to_str(const int type)
3600 {
3601
3602 switch (type) {
3603 case IGMP_CHANGE_TO_EXCLUDE_MODE:
3604 return "TO_EX";
3605 break;
3606 case IGMP_CHANGE_TO_INCLUDE_MODE:
3607 return "TO_IN";
3608 break;
3609 case IGMP_MODE_IS_EXCLUDE:
3610 return "MODE_EX";
3611 break;
3612 case IGMP_MODE_IS_INCLUDE:
3613 return "MODE_IN";
3614 break;
3615 case IGMP_ALLOW_NEW_SOURCES:
3616 return "ALLOW_NEW";
3617 break;
3618 case IGMP_BLOCK_OLD_SOURCES:
3619 return "BLOCK_OLD";
3620 break;
3621 default:
3622 break;
3623 }
3624 return "unknown";
3625 }
3626 #endif
3627
3628 #ifdef VIMAGE
3629 static void
vnet_igmp_init(const void * unused __unused)3630 vnet_igmp_init(const void *unused __unused)
3631 {
3632
3633 netisr_register_vnet(&igmp_nh);
3634 }
3635 VNET_SYSINIT(vnet_igmp_init, SI_SUB_PROTO_MC, SI_ORDER_ANY,
3636 vnet_igmp_init, NULL);
3637
3638 static void
vnet_igmp_uninit(const void * unused __unused)3639 vnet_igmp_uninit(const void *unused __unused)
3640 {
3641
3642 /* This can happen when we shutdown the entire network stack. */
3643 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3644
3645 netisr_unregister_vnet(&igmp_nh);
3646 }
3647 VNET_SYSUNINIT(vnet_igmp_uninit, SI_SUB_PROTO_MC, SI_ORDER_ANY,
3648 vnet_igmp_uninit, NULL);
3649 #endif
3650
3651 #ifdef DDB
DB_SHOW_COMMAND(igi_list,db_show_igi_list)3652 DB_SHOW_COMMAND(igi_list, db_show_igi_list)
3653 {
3654 struct igmp_ifsoftc *igi, *tigi;
3655 LIST_HEAD(_igi_list, igmp_ifsoftc) *igi_head;
3656
3657 if (!have_addr) {
3658 db_printf("usage: show igi_list <addr>\n");
3659 return;
3660 }
3661 igi_head = (struct _igi_list *)addr;
3662
3663 LIST_FOREACH_SAFE(igi, igi_head, igi_link, tigi) {
3664 db_printf("igmp_ifsoftc %p:\n", igi);
3665 db_printf(" ifp %p\n", igi->igi_ifp);
3666 db_printf(" version %u\n", igi->igi_version);
3667 db_printf(" v1_timer %u\n", igi->igi_v1_timer);
3668 db_printf(" v2_timer %u\n", igi->igi_v2_timer);
3669 db_printf(" v3_timer %u\n", igi->igi_v3_timer);
3670 db_printf(" flags %#x\n", igi->igi_flags);
3671 db_printf(" rv %u\n", igi->igi_rv);
3672 db_printf(" qi %u\n", igi->igi_qi);
3673 db_printf(" qri %u\n", igi->igi_qri);
3674 db_printf(" uri %u\n", igi->igi_uri);
3675 /* struct mbufq igi_gq; */
3676 db_printf("\n");
3677 }
3678 }
3679 #endif
3680
3681 static int
igmp_modevent(module_t mod,int type,void * unused __unused)3682 igmp_modevent(module_t mod, int type, void *unused __unused)
3683 {
3684
3685 switch (type) {
3686 case MOD_LOAD:
3687 CTR1(KTR_IGMPV3, "%s: initializing", __func__);
3688 IGMP_LOCK_INIT();
3689 m_raopt = igmp_ra_alloc();
3690 netisr_register(&igmp_nh);
3691 callout_init(&igmpslow_callout, 1);
3692 callout_reset(&igmpslow_callout, hz / IGMP_SLOWHZ,
3693 igmp_slowtimo, NULL);
3694 callout_init(&igmpfast_callout, 1);
3695 callout_reset(&igmpfast_callout, hz / IGMP_FASTHZ,
3696 igmp_fasttimo, NULL);
3697 break;
3698 case MOD_UNLOAD:
3699 CTR1(KTR_IGMPV3, "%s: tearing down", __func__);
3700 netisr_unregister(&igmp_nh);
3701 m_free(m_raopt);
3702 m_raopt = NULL;
3703 IGMP_LOCK_DESTROY();
3704 break;
3705 default:
3706 return (EOPNOTSUPP);
3707 }
3708 return (0);
3709 }
3710
3711 static moduledata_t igmp_mod = {
3712 "igmp",
3713 igmp_modevent,
3714 0
3715 };
3716 DECLARE_MODULE(igmp, igmp_mod, SI_SUB_PROTO_MC, SI_ORDER_MIDDLE);
3717