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