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