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