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