]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/in_mcast.c
in(6)_mcast: Expand out vnet set / restore macro so that they work in a conditional...
[FreeBSD/FreeBSD.git] / sys / netinet / in_mcast.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2007-2009 Bruce Simpson.
5  * Copyright (c) 2005 Robert N. M. Watson.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote
17  *    products derived from this software without specific prior written
18  *    permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 /*
34  * IPv4 multicast socket, group, and socket option processing module.
35  */
36
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/protosw.h>
47 #include <sys/rmlock.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/protosw.h>
51 #include <sys/sysctl.h>
52 #include <sys/ktr.h>
53 #include <sys/taskqueue.h>
54 #include <sys/gtaskqueue.h>
55 #include <sys/tree.h>
56
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_dl.h>
60 #include <net/route.h>
61 #include <net/vnet.h>
62
63 #include <net/ethernet.h>
64
65 #include <netinet/in.h>
66 #include <netinet/in_systm.h>
67 #include <netinet/in_fib.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_var.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/igmp_var.h>
72
73 #ifndef KTR_IGMPV3
74 #define KTR_IGMPV3 KTR_INET
75 #endif
76
77 #ifndef __SOCKUNION_DECLARED
78 union sockunion {
79         struct sockaddr_storage ss;
80         struct sockaddr         sa;
81         struct sockaddr_dl      sdl;
82         struct sockaddr_in      sin;
83 };
84 typedef union sockunion sockunion_t;
85 #define __SOCKUNION_DECLARED
86 #endif /* __SOCKUNION_DECLARED */
87
88 static MALLOC_DEFINE(M_INMFILTER, "in_mfilter",
89     "IPv4 multicast PCB-layer source filter");
90 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "IPv4 multicast group");
91 static MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "IPv4 multicast options");
92 static MALLOC_DEFINE(M_IPMSOURCE, "ip_msource",
93     "IPv4 multicast IGMP-layer source filter");
94
95 /*
96  * Locking:
97  * - Lock order is: Giant, INP_WLOCK, IN_MULTI_LIST_LOCK, IGMP_LOCK, IF_ADDR_LOCK.
98  * - The IF_ADDR_LOCK is implicitly taken by inm_lookup() earlier, however
99  *   it can be taken by code in net/if.c also.
100  * - ip_moptions and in_mfilter are covered by the INP_WLOCK.
101  *
102  * struct in_multi is covered by IN_MULTI_LIST_LOCK. There isn't strictly
103  * any need for in_multi itself to be virtualized -- it is bound to an ifp
104  * anyway no matter what happens.
105  */
106 struct mtx in_multi_list_mtx;
107 MTX_SYSINIT(in_multi_mtx, &in_multi_list_mtx, "in_multi_list_mtx", MTX_DEF);
108
109 struct mtx in_multi_free_mtx;
110 MTX_SYSINIT(in_multi_free_mtx, &in_multi_free_mtx, "in_multi_free_mtx", MTX_DEF);
111
112 struct sx in_multi_sx;
113 SX_SYSINIT(in_multi_sx, &in_multi_sx, "in_multi_sx");
114
115 int ifma_restart;
116
117 /*
118  * Functions with non-static linkage defined in this file should be
119  * declared in in_var.h:
120  *  imo_multi_filter()
121  *  in_addmulti()
122  *  in_delmulti()
123  *  in_joingroup()
124  *  in_joingroup_locked()
125  *  in_leavegroup()
126  *  in_leavegroup_locked()
127  * and ip_var.h:
128  *  inp_freemoptions()
129  *  inp_getmoptions()
130  *  inp_setmoptions()
131  *
132  * XXX: Both carp and pf need to use the legacy (*,G) KPIs in_addmulti()
133  * and in_delmulti().
134  */
135 static void     imf_commit(struct in_mfilter *);
136 static int      imf_get_source(struct in_mfilter *imf,
137                     const struct sockaddr_in *psin,
138                     struct in_msource **);
139 static struct in_msource *
140                 imf_graft(struct in_mfilter *, const uint8_t,
141                     const struct sockaddr_in *);
142 static void     imf_leave(struct in_mfilter *);
143 static int      imf_prune(struct in_mfilter *, const struct sockaddr_in *);
144 static void     imf_purge(struct in_mfilter *);
145 static void     imf_rollback(struct in_mfilter *);
146 static void     imf_reap(struct in_mfilter *);
147 static int      imo_grow(struct ip_moptions *);
148 static size_t   imo_match_group(const struct ip_moptions *,
149                     const struct ifnet *, const struct sockaddr *);
150 static struct in_msource *
151                 imo_match_source(const struct ip_moptions *, const size_t,
152                     const struct sockaddr *);
153 static void     ims_merge(struct ip_msource *ims,
154                     const struct in_msource *lims, const int rollback);
155 static int      in_getmulti(struct ifnet *, const struct in_addr *,
156                     struct in_multi **);
157 static int      inm_get_source(struct in_multi *inm, const in_addr_t haddr,
158                     const int noalloc, struct ip_msource **pims);
159 #ifdef KTR
160 static int      inm_is_ifp_detached(const struct in_multi *);
161 #endif
162 static int      inm_merge(struct in_multi *, /*const*/ struct in_mfilter *);
163 static void     inm_purge(struct in_multi *);
164 static void     inm_reap(struct in_multi *);
165 static void inm_release(struct in_multi *);
166 static struct ip_moptions *
167                 inp_findmoptions(struct inpcb *);
168 static int      inp_get_source_filters(struct inpcb *, struct sockopt *);
169 static int      inp_join_group(struct inpcb *, struct sockopt *);
170 static int      inp_leave_group(struct inpcb *, struct sockopt *);
171 static struct ifnet *
172                 inp_lookup_mcast_ifp(const struct inpcb *,
173                     const struct sockaddr_in *, const struct in_addr);
174 static int      inp_block_unblock_source(struct inpcb *, struct sockopt *);
175 static int      inp_set_multicast_if(struct inpcb *, struct sockopt *);
176 static int      inp_set_source_filters(struct inpcb *, struct sockopt *);
177 static int      sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS);
178
179 static SYSCTL_NODE(_net_inet_ip, OID_AUTO, mcast, CTLFLAG_RW, 0,
180     "IPv4 multicast");
181
182 static u_long in_mcast_maxgrpsrc = IP_MAX_GROUP_SRC_FILTER;
183 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxgrpsrc,
184     CTLFLAG_RWTUN, &in_mcast_maxgrpsrc, 0,
185     "Max source filters per group");
186
187 static u_long in_mcast_maxsocksrc = IP_MAX_SOCK_SRC_FILTER;
188 SYSCTL_ULONG(_net_inet_ip_mcast, OID_AUTO, maxsocksrc,
189     CTLFLAG_RWTUN, &in_mcast_maxsocksrc, 0,
190     "Max source filters per socket");
191
192 int in_mcast_loop = IP_DEFAULT_MULTICAST_LOOP;
193 SYSCTL_INT(_net_inet_ip_mcast, OID_AUTO, loop, CTLFLAG_RWTUN,
194     &in_mcast_loop, 0, "Loopback multicast datagrams by default");
195
196 static SYSCTL_NODE(_net_inet_ip_mcast, OID_AUTO, filters,
197     CTLFLAG_RD | CTLFLAG_MPSAFE, sysctl_ip_mcast_filters,
198     "Per-interface stack-wide source filters");
199
200 #ifdef KTR
201 /*
202  * Inline function which wraps assertions for a valid ifp.
203  * The ifnet layer will set the ifma's ifp pointer to NULL if the ifp
204  * is detached.
205  */
206 static int __inline
207 inm_is_ifp_detached(const struct in_multi *inm)
208 {
209         struct ifnet *ifp;
210
211         KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
212         ifp = inm->inm_ifma->ifma_ifp;
213         if (ifp != NULL) {
214                 /*
215                  * Sanity check that netinet's notion of ifp is the
216                  * same as net's.
217                  */
218                 KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
219         }
220
221         return (ifp == NULL);
222 }
223 #endif
224
225 static struct grouptask free_gtask;
226 static struct in_multi_head inm_free_list;
227 static void inm_release_task(void *arg __unused);
228 static void inm_init(void)
229 {
230         SLIST_INIT(&inm_free_list);
231         taskqgroup_config_gtask_init(NULL, &free_gtask, inm_release_task, "inm release task");
232 }
233
234 SYSINIT(inm_init, SI_SUB_SMP + 1, SI_ORDER_FIRST,
235         inm_init, NULL);
236
237
238 void
239 inm_release_list_deferred(struct in_multi_head *inmh)
240 {
241
242         if (SLIST_EMPTY(inmh))
243                 return;
244         mtx_lock(&in_multi_free_mtx);
245         SLIST_CONCAT(&inm_free_list, inmh, in_multi, inm_nrele);
246         mtx_unlock(&in_multi_free_mtx);
247         GROUPTASK_ENQUEUE(&free_gtask);
248 }
249
250 void
251 inm_disconnect(struct in_multi *inm)
252 {
253         struct ifnet *ifp;
254         struct ifmultiaddr *ifma, *ll_ifma;
255
256         ifp = inm->inm_ifp;
257         IF_ADDR_WLOCK_ASSERT(ifp);
258         ifma = inm->inm_ifma;
259
260         if_ref(ifp);
261         CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifmultiaddr, ifma_link);
262         MCDPRINTF("removed ifma: %p from %s\n", ifma, ifp->if_xname);
263         if ((ll_ifma = ifma->ifma_llifma) != NULL) {
264                 MPASS(ifma != ll_ifma);
265                 ifma->ifma_llifma = NULL;
266                 MPASS(ll_ifma->ifma_llifma == NULL);
267                 MPASS(ll_ifma->ifma_ifp == ifp);
268                 if (--ll_ifma->ifma_refcount == 0) {
269                         CK_STAILQ_REMOVE(&ifp->if_multiaddrs, ll_ifma, ifmultiaddr, ifma_link);
270                         MCDPRINTF("removed ll_ifma: %p from %s\n", ll_ifma, ifp->if_xname);
271                         if_freemulti(ll_ifma);
272                         ifma_restart = true;
273                 }
274         }
275 }
276
277 void
278 inm_release_deferred(struct in_multi *inm)
279 {
280         struct in_multi_head tmp;
281
282         IN_MULTI_LIST_LOCK_ASSERT();
283         MPASS(inm->inm_refcount > 0);
284         if (--inm->inm_refcount == 0) {
285                 SLIST_INIT(&tmp);
286                 inm_disconnect(inm);
287                 inm->inm_ifma->ifma_protospec = NULL;
288                 SLIST_INSERT_HEAD(&tmp, inm, inm_nrele);
289                 inm_release_list_deferred(&tmp);
290         }
291 }
292
293 static void
294 inm_release_task(void *arg __unused)
295 {
296         struct in_multi_head inm_free_tmp;
297         struct in_multi *inm, *tinm;
298
299         SLIST_INIT(&inm_free_tmp);
300         mtx_lock(&in_multi_free_mtx);
301         SLIST_CONCAT(&inm_free_tmp, &inm_free_list, in_multi, inm_nrele);
302         mtx_unlock(&in_multi_free_mtx);
303         IN_MULTI_LOCK();
304         SLIST_FOREACH_SAFE(inm, &inm_free_tmp, inm_nrele, tinm) {
305                 SLIST_REMOVE_HEAD(&inm_free_tmp, inm_nrele);
306                 MPASS(inm);
307                 inm_release(inm);
308         }
309         IN_MULTI_UNLOCK();
310 }
311
312 /*
313  * Initialize an in_mfilter structure to a known state at t0, t1
314  * with an empty source filter list.
315  */
316 static __inline void
317 imf_init(struct in_mfilter *imf, const int st0, const int st1)
318 {
319         memset(imf, 0, sizeof(struct in_mfilter));
320         RB_INIT(&imf->imf_sources);
321         imf->imf_st[0] = st0;
322         imf->imf_st[1] = st1;
323 }
324
325 /*
326  * Function for looking up an in_multi record for an IPv4 multicast address
327  * on a given interface. ifp must be valid. If no record found, return NULL.
328  * The IN_MULTI_LIST_LOCK and IF_ADDR_LOCK on ifp must be held.
329  */
330 struct in_multi *
331 inm_lookup_locked(struct ifnet *ifp, const struct in_addr ina)
332 {
333         struct ifmultiaddr *ifma;
334         struct in_multi *inm;
335
336         IN_MULTI_LIST_LOCK_ASSERT();
337         IF_ADDR_LOCK_ASSERT(ifp);
338
339         inm = NULL;
340         CK_STAILQ_FOREACH(ifma, &((ifp)->if_multiaddrs), ifma_link) {
341                 if (ifma->ifma_addr->sa_family != AF_INET ||
342                         ifma->ifma_protospec == NULL)
343                         continue;
344                 inm = (struct in_multi *)ifma->ifma_protospec;
345                 if (inm->inm_addr.s_addr == ina.s_addr)
346                         break;
347                 inm = NULL;
348         }
349         return (inm);
350 }
351
352 /*
353  * Wrapper for inm_lookup_locked().
354  * The IF_ADDR_LOCK will be taken on ifp and released on return.
355  */
356 struct in_multi *
357 inm_lookup(struct ifnet *ifp, const struct in_addr ina)
358 {
359         struct in_multi *inm;
360
361         IN_MULTI_LIST_LOCK_ASSERT();
362         IF_ADDR_RLOCK(ifp);
363         inm = inm_lookup_locked(ifp, ina);
364         IF_ADDR_RUNLOCK(ifp);
365
366         return (inm);
367 }
368
369 /*
370  * Resize the ip_moptions vector to the next power-of-two minus 1.
371  * May be called with locks held; do not sleep.
372  */
373 static int
374 imo_grow(struct ip_moptions *imo)
375 {
376         struct in_multi         **nmships;
377         struct in_multi         **omships;
378         struct in_mfilter        *nmfilters;
379         struct in_mfilter        *omfilters;
380         size_t                    idx;
381         size_t                    newmax;
382         size_t                    oldmax;
383
384         nmships = NULL;
385         nmfilters = NULL;
386         omships = imo->imo_membership;
387         omfilters = imo->imo_mfilters;
388         oldmax = imo->imo_max_memberships;
389         newmax = ((oldmax + 1) * 2) - 1;
390
391         if (newmax <= IP_MAX_MEMBERSHIPS) {
392                 nmships = (struct in_multi **)realloc(omships,
393                     sizeof(struct in_multi *) * newmax, M_IPMOPTS, M_NOWAIT);
394                 nmfilters = (struct in_mfilter *)realloc(omfilters,
395                     sizeof(struct in_mfilter) * newmax, M_INMFILTER, M_NOWAIT);
396                 if (nmships != NULL && nmfilters != NULL) {
397                         /* Initialize newly allocated source filter heads. */
398                         for (idx = oldmax; idx < newmax; idx++) {
399                                 imf_init(&nmfilters[idx], MCAST_UNDEFINED,
400                                     MCAST_EXCLUDE);
401                         }
402                         imo->imo_max_memberships = newmax;
403                         imo->imo_membership = nmships;
404                         imo->imo_mfilters = nmfilters;
405                 }
406         }
407
408         if (nmships == NULL || nmfilters == NULL) {
409                 if (nmships != NULL)
410                         free(nmships, M_IPMOPTS);
411                 if (nmfilters != NULL)
412                         free(nmfilters, M_INMFILTER);
413                 return (ETOOMANYREFS);
414         }
415
416         return (0);
417 }
418
419 /*
420  * Find an IPv4 multicast group entry for this ip_moptions instance
421  * which matches the specified group, and optionally an interface.
422  * Return its index into the array, or -1 if not found.
423  */
424 static size_t
425 imo_match_group(const struct ip_moptions *imo, const struct ifnet *ifp,
426     const struct sockaddr *group)
427 {
428         const struct sockaddr_in *gsin;
429         struct in_multi **pinm;
430         int               idx;
431         int               nmships;
432
433         gsin = (const struct sockaddr_in *)group;
434
435         /* The imo_membership array may be lazy allocated. */
436         if (imo->imo_membership == NULL || imo->imo_num_memberships == 0)
437                 return (-1);
438
439         nmships = imo->imo_num_memberships;
440         pinm = &imo->imo_membership[0];
441         for (idx = 0; idx < nmships; idx++, pinm++) {
442                 if (*pinm == NULL)
443                         continue;
444                 if ((ifp == NULL || ((*pinm)->inm_ifp == ifp)) &&
445                     in_hosteq((*pinm)->inm_addr, gsin->sin_addr)) {
446                         break;
447                 }
448         }
449         if (idx >= nmships)
450                 idx = -1;
451
452         return (idx);
453 }
454
455 /*
456  * Find an IPv4 multicast source entry for this imo which matches
457  * the given group index for this socket, and source address.
458  *
459  * NOTE: This does not check if the entry is in-mode, merely if
460  * it exists, which may not be the desired behaviour.
461  */
462 static struct in_msource *
463 imo_match_source(const struct ip_moptions *imo, const size_t gidx,
464     const struct sockaddr *src)
465 {
466         struct ip_msource        find;
467         struct in_mfilter       *imf;
468         struct ip_msource       *ims;
469         const sockunion_t       *psa;
470
471         KASSERT(src->sa_family == AF_INET, ("%s: !AF_INET", __func__));
472         KASSERT(gidx != -1 && gidx < imo->imo_num_memberships,
473             ("%s: invalid index %d\n", __func__, (int)gidx));
474
475         /* The imo_mfilters array may be lazy allocated. */
476         if (imo->imo_mfilters == NULL)
477                 return (NULL);
478         imf = &imo->imo_mfilters[gidx];
479
480         /* Source trees are keyed in host byte order. */
481         psa = (const sockunion_t *)src;
482         find.ims_haddr = ntohl(psa->sin.sin_addr.s_addr);
483         ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
484
485         return ((struct in_msource *)ims);
486 }
487
488 /*
489  * Perform filtering for multicast datagrams on a socket by group and source.
490  *
491  * Returns 0 if a datagram should be allowed through, or various error codes
492  * if the socket was not a member of the group, or the source was muted, etc.
493  */
494 int
495 imo_multi_filter(const struct ip_moptions *imo, const struct ifnet *ifp,
496     const struct sockaddr *group, const struct sockaddr *src)
497 {
498         size_t gidx;
499         struct in_msource *ims;
500         int mode;
501
502         KASSERT(ifp != NULL, ("%s: null ifp", __func__));
503
504         gidx = imo_match_group(imo, ifp, group);
505         if (gidx == -1)
506                 return (MCAST_NOTGMEMBER);
507
508         /*
509          * Check if the source was included in an (S,G) join.
510          * Allow reception on exclusive memberships by default,
511          * reject reception on inclusive memberships by default.
512          * Exclude source only if an in-mode exclude filter exists.
513          * Include source only if an in-mode include filter exists.
514          * NOTE: We are comparing group state here at IGMP t1 (now)
515          * with socket-layer t0 (since last downcall).
516          */
517         mode = imo->imo_mfilters[gidx].imf_st[1];
518         ims = imo_match_source(imo, gidx, src);
519
520         if ((ims == NULL && mode == MCAST_INCLUDE) ||
521             (ims != NULL && ims->imsl_st[0] != mode))
522                 return (MCAST_NOTSMEMBER);
523
524         return (MCAST_PASS);
525 }
526
527 /*
528  * Find and return a reference to an in_multi record for (ifp, group),
529  * and bump its reference count.
530  * If one does not exist, try to allocate it, and update link-layer multicast
531  * filters on ifp to listen for group.
532  * Assumes the IN_MULTI lock is held across the call.
533  * Return 0 if successful, otherwise return an appropriate error code.
534  */
535 static int
536 in_getmulti(struct ifnet *ifp, const struct in_addr *group,
537     struct in_multi **pinm)
538 {
539         struct sockaddr_in       gsin;
540         struct ifmultiaddr      *ifma;
541         struct in_ifinfo        *ii;
542         struct in_multi         *inm;
543         int error;
544
545         IN_MULTI_LOCK_ASSERT();
546
547         ii = (struct in_ifinfo *)ifp->if_afdata[AF_INET];
548         IN_MULTI_LIST_LOCK();
549         inm = inm_lookup(ifp, *group);
550         if (inm != NULL) {
551                 /*
552                  * If we already joined this group, just bump the
553                  * refcount and return it.
554                  */
555                 KASSERT(inm->inm_refcount >= 1,
556                     ("%s: bad refcount %d", __func__, inm->inm_refcount));
557                 inm_acquire_locked(inm);
558                 *pinm = inm;
559         }
560         IN_MULTI_LIST_UNLOCK();
561         if (inm != NULL)
562                 return (0);
563         
564         memset(&gsin, 0, sizeof(gsin));
565         gsin.sin_family = AF_INET;
566         gsin.sin_len = sizeof(struct sockaddr_in);
567         gsin.sin_addr = *group;
568
569         /*
570          * Check if a link-layer group is already associated
571          * with this network-layer group on the given ifnet.
572          */
573         error = if_addmulti(ifp, (struct sockaddr *)&gsin, &ifma);
574         if (error != 0)
575                 return (error);
576
577         /* XXX ifma_protospec must be covered by IF_ADDR_LOCK */
578         IN_MULTI_LIST_LOCK();
579         IF_ADDR_WLOCK(ifp);
580
581         /*
582          * If something other than netinet is occupying the link-layer
583          * group, print a meaningful error message and back out of
584          * the allocation.
585          * Otherwise, bump the refcount on the existing network-layer
586          * group association and return it.
587          */
588         if (ifma->ifma_protospec != NULL) {
589                 inm = (struct in_multi *)ifma->ifma_protospec;
590 #ifdef INVARIANTS
591                 KASSERT(ifma->ifma_addr != NULL, ("%s: no ifma_addr",
592                     __func__));
593                 KASSERT(ifma->ifma_addr->sa_family == AF_INET,
594                     ("%s: ifma not AF_INET", __func__));
595                 KASSERT(inm != NULL, ("%s: no ifma_protospec", __func__));
596                 if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
597                     !in_hosteq(inm->inm_addr, *group)) {
598                         char addrbuf[INET_ADDRSTRLEN];
599
600                         panic("%s: ifma %p is inconsistent with %p (%s)",
601                             __func__, ifma, inm, inet_ntoa_r(*group, addrbuf));
602                 }
603 #endif
604                 inm_acquire_locked(inm);
605                 *pinm = inm;
606                 goto out_locked;
607         }
608
609         IF_ADDR_WLOCK_ASSERT(ifp);
610
611         /*
612          * A new in_multi record is needed; allocate and initialize it.
613          * We DO NOT perform an IGMP join as the in_ layer may need to
614          * push an initial source list down to IGMP to support SSM.
615          *
616          * The initial source filter state is INCLUDE, {} as per the RFC.
617          */
618         inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
619         if (inm == NULL) {
620                 IF_ADDR_WUNLOCK(ifp);
621                 IN_MULTI_LIST_UNLOCK();
622                 if_delmulti_ifma(ifma);
623                 return (ENOMEM);
624         }
625         inm->inm_addr = *group;
626         inm->inm_ifp = ifp;
627         inm->inm_igi = ii->ii_igmp;
628         inm->inm_ifma = ifma;
629         inm->inm_refcount = 1;
630         inm->inm_state = IGMP_NOT_MEMBER;
631         mbufq_init(&inm->inm_scq, IGMP_MAX_STATE_CHANGES);
632         inm->inm_st[0].iss_fmode = MCAST_UNDEFINED;
633         inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
634         RB_INIT(&inm->inm_srcs);
635
636         ifma->ifma_protospec = inm;
637
638         *pinm = inm;
639  out_locked:
640         IF_ADDR_WUNLOCK(ifp);
641         IN_MULTI_LIST_UNLOCK();
642         return (0);
643 }
644
645 /*
646  * Drop a reference to an in_multi record.
647  *
648  * If the refcount drops to 0, free the in_multi record and
649  * delete the underlying link-layer membership.
650  */
651 static void
652 inm_release(struct in_multi *inm)
653 {
654         struct ifmultiaddr *ifma;
655         struct ifnet *ifp;
656         struct vnet *saved_vnet;
657
658         CTR2(KTR_IGMPV3, "%s: refcount is %d", __func__, inm->inm_refcount);
659         MPASS(inm->inm_refcount == 0);
660         CTR2(KTR_IGMPV3, "%s: freeing inm %p", __func__, inm);
661
662         ifma = inm->inm_ifma;
663         ifp = inm->inm_ifp;
664
665         /* XXX this access is not covered by IF_ADDR_LOCK */
666         CTR2(KTR_IGMPV3, "%s: purging ifma %p", __func__, ifma);
667         if (ifp) {
668                 saved_vnet = curvnet;
669                 curvnet = ifp->if_vnet;
670         }
671         inm_purge(inm);
672         free(inm, M_IPMADDR);
673
674         if_delmulti_ifma_flags(ifma, 1);
675         if (ifp) {
676                 curvnet = saved_vnet;
677                 if_rele(ifp);
678         }
679 }
680
681 /*
682  * Clear recorded source entries for a group.
683  * Used by the IGMP code. Caller must hold the IN_MULTI lock.
684  * FIXME: Should reap.
685  */
686 void
687 inm_clear_recorded(struct in_multi *inm)
688 {
689         struct ip_msource       *ims;
690
691         IN_MULTI_LIST_LOCK_ASSERT();
692
693         RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
694                 if (ims->ims_stp) {
695                         ims->ims_stp = 0;
696                         --inm->inm_st[1].iss_rec;
697                 }
698         }
699         KASSERT(inm->inm_st[1].iss_rec == 0,
700             ("%s: iss_rec %d not 0", __func__, inm->inm_st[1].iss_rec));
701 }
702
703 /*
704  * Record a source as pending for a Source-Group IGMPv3 query.
705  * This lives here as it modifies the shared tree.
706  *
707  * inm is the group descriptor.
708  * naddr is the address of the source to record in network-byte order.
709  *
710  * If the net.inet.igmp.sgalloc sysctl is non-zero, we will
711  * lazy-allocate a source node in response to an SG query.
712  * Otherwise, no allocation is performed. This saves some memory
713  * with the trade-off that the source will not be reported to the
714  * router if joined in the window between the query response and
715  * the group actually being joined on the local host.
716  *
717  * VIMAGE: XXX: Currently the igmp_sgalloc feature has been removed.
718  * This turns off the allocation of a recorded source entry if
719  * the group has not been joined.
720  *
721  * Return 0 if the source didn't exist or was already marked as recorded.
722  * Return 1 if the source was marked as recorded by this function.
723  * Return <0 if any error occurred (negated errno code).
724  */
725 int
726 inm_record_source(struct in_multi *inm, const in_addr_t naddr)
727 {
728         struct ip_msource        find;
729         struct ip_msource       *ims, *nims;
730
731         IN_MULTI_LIST_LOCK_ASSERT();
732
733         find.ims_haddr = ntohl(naddr);
734         ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
735         if (ims && ims->ims_stp)
736                 return (0);
737         if (ims == NULL) {
738                 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
739                         return (-ENOSPC);
740                 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
741                     M_NOWAIT | M_ZERO);
742                 if (nims == NULL)
743                         return (-ENOMEM);
744                 nims->ims_haddr = find.ims_haddr;
745                 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
746                 ++inm->inm_nsrc;
747                 ims = nims;
748         }
749
750         /*
751          * Mark the source as recorded and update the recorded
752          * source count.
753          */
754         ++ims->ims_stp;
755         ++inm->inm_st[1].iss_rec;
756
757         return (1);
758 }
759
760 /*
761  * Return a pointer to an in_msource owned by an in_mfilter,
762  * given its source address.
763  * Lazy-allocate if needed. If this is a new entry its filter state is
764  * undefined at t0.
765  *
766  * imf is the filter set being modified.
767  * haddr is the source address in *host* byte-order.
768  *
769  * SMPng: May be called with locks held; malloc must not block.
770  */
771 static int
772 imf_get_source(struct in_mfilter *imf, const struct sockaddr_in *psin,
773     struct in_msource **plims)
774 {
775         struct ip_msource        find;
776         struct ip_msource       *ims, *nims;
777         struct in_msource       *lims;
778         int                      error;
779
780         error = 0;
781         ims = NULL;
782         lims = NULL;
783
784         /* key is host byte order */
785         find.ims_haddr = ntohl(psin->sin_addr.s_addr);
786         ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
787         lims = (struct in_msource *)ims;
788         if (lims == NULL) {
789                 if (imf->imf_nsrc == in_mcast_maxsocksrc)
790                         return (ENOSPC);
791                 nims = malloc(sizeof(struct in_msource), M_INMFILTER,
792                     M_NOWAIT | M_ZERO);
793                 if (nims == NULL)
794                         return (ENOMEM);
795                 lims = (struct in_msource *)nims;
796                 lims->ims_haddr = find.ims_haddr;
797                 lims->imsl_st[0] = MCAST_UNDEFINED;
798                 RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
799                 ++imf->imf_nsrc;
800         }
801
802         *plims = lims;
803
804         return (error);
805 }
806
807 /*
808  * Graft a source entry into an existing socket-layer filter set,
809  * maintaining any required invariants and checking allocations.
810  *
811  * The source is marked as being in the new filter mode at t1.
812  *
813  * Return the pointer to the new node, otherwise return NULL.
814  */
815 static struct in_msource *
816 imf_graft(struct in_mfilter *imf, const uint8_t st1,
817     const struct sockaddr_in *psin)
818 {
819         struct ip_msource       *nims;
820         struct in_msource       *lims;
821
822         nims = malloc(sizeof(struct in_msource), M_INMFILTER,
823             M_NOWAIT | M_ZERO);
824         if (nims == NULL)
825                 return (NULL);
826         lims = (struct in_msource *)nims;
827         lims->ims_haddr = ntohl(psin->sin_addr.s_addr);
828         lims->imsl_st[0] = MCAST_UNDEFINED;
829         lims->imsl_st[1] = st1;
830         RB_INSERT(ip_msource_tree, &imf->imf_sources, nims);
831         ++imf->imf_nsrc;
832
833         return (lims);
834 }
835
836 /*
837  * Prune a source entry from an existing socket-layer filter set,
838  * maintaining any required invariants and checking allocations.
839  *
840  * The source is marked as being left at t1, it is not freed.
841  *
842  * Return 0 if no error occurred, otherwise return an errno value.
843  */
844 static int
845 imf_prune(struct in_mfilter *imf, const struct sockaddr_in *psin)
846 {
847         struct ip_msource        find;
848         struct ip_msource       *ims;
849         struct in_msource       *lims;
850
851         /* key is host byte order */
852         find.ims_haddr = ntohl(psin->sin_addr.s_addr);
853         ims = RB_FIND(ip_msource_tree, &imf->imf_sources, &find);
854         if (ims == NULL)
855                 return (ENOENT);
856         lims = (struct in_msource *)ims;
857         lims->imsl_st[1] = MCAST_UNDEFINED;
858         return (0);
859 }
860
861 /*
862  * Revert socket-layer filter set deltas at t1 to t0 state.
863  */
864 static void
865 imf_rollback(struct in_mfilter *imf)
866 {
867         struct ip_msource       *ims, *tims;
868         struct in_msource       *lims;
869
870         RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
871                 lims = (struct in_msource *)ims;
872                 if (lims->imsl_st[0] == lims->imsl_st[1]) {
873                         /* no change at t1 */
874                         continue;
875                 } else if (lims->imsl_st[0] != MCAST_UNDEFINED) {
876                         /* revert change to existing source at t1 */
877                         lims->imsl_st[1] = lims->imsl_st[0];
878                 } else {
879                         /* revert source added t1 */
880                         CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
881                         RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
882                         free(ims, M_INMFILTER);
883                         imf->imf_nsrc--;
884                 }
885         }
886         imf->imf_st[1] = imf->imf_st[0];
887 }
888
889 /*
890  * Mark socket-layer filter set as INCLUDE {} at t1.
891  */
892 static void
893 imf_leave(struct in_mfilter *imf)
894 {
895         struct ip_msource       *ims;
896         struct in_msource       *lims;
897
898         RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
899                 lims = (struct in_msource *)ims;
900                 lims->imsl_st[1] = MCAST_UNDEFINED;
901         }
902         imf->imf_st[1] = MCAST_INCLUDE;
903 }
904
905 /*
906  * Mark socket-layer filter set deltas as committed.
907  */
908 static void
909 imf_commit(struct in_mfilter *imf)
910 {
911         struct ip_msource       *ims;
912         struct in_msource       *lims;
913
914         RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
915                 lims = (struct in_msource *)ims;
916                 lims->imsl_st[0] = lims->imsl_st[1];
917         }
918         imf->imf_st[0] = imf->imf_st[1];
919 }
920
921 /*
922  * Reap unreferenced sources from socket-layer filter set.
923  */
924 static void
925 imf_reap(struct in_mfilter *imf)
926 {
927         struct ip_msource       *ims, *tims;
928         struct in_msource       *lims;
929
930         RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
931                 lims = (struct in_msource *)ims;
932                 if ((lims->imsl_st[0] == MCAST_UNDEFINED) &&
933                     (lims->imsl_st[1] == MCAST_UNDEFINED)) {
934                         CTR2(KTR_IGMPV3, "%s: free lims %p", __func__, ims);
935                         RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
936                         free(ims, M_INMFILTER);
937                         imf->imf_nsrc--;
938                 }
939         }
940 }
941
942 /*
943  * Purge socket-layer filter set.
944  */
945 static void
946 imf_purge(struct in_mfilter *imf)
947 {
948         struct ip_msource       *ims, *tims;
949
950         RB_FOREACH_SAFE(ims, ip_msource_tree, &imf->imf_sources, tims) {
951                 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
952                 RB_REMOVE(ip_msource_tree, &imf->imf_sources, ims);
953                 free(ims, M_INMFILTER);
954                 imf->imf_nsrc--;
955         }
956         imf->imf_st[0] = imf->imf_st[1] = MCAST_UNDEFINED;
957         KASSERT(RB_EMPTY(&imf->imf_sources),
958             ("%s: imf_sources not empty", __func__));
959 }
960
961 /*
962  * Look up a source filter entry for a multicast group.
963  *
964  * inm is the group descriptor to work with.
965  * haddr is the host-byte-order IPv4 address to look up.
966  * noalloc may be non-zero to suppress allocation of sources.
967  * *pims will be set to the address of the retrieved or allocated source.
968  *
969  * SMPng: NOTE: may be called with locks held.
970  * Return 0 if successful, otherwise return a non-zero error code.
971  */
972 static int
973 inm_get_source(struct in_multi *inm, const in_addr_t haddr,
974     const int noalloc, struct ip_msource **pims)
975 {
976         struct ip_msource        find;
977         struct ip_msource       *ims, *nims;
978
979         find.ims_haddr = haddr;
980         ims = RB_FIND(ip_msource_tree, &inm->inm_srcs, &find);
981         if (ims == NULL && !noalloc) {
982                 if (inm->inm_nsrc == in_mcast_maxgrpsrc)
983                         return (ENOSPC);
984                 nims = malloc(sizeof(struct ip_msource), M_IPMSOURCE,
985                     M_NOWAIT | M_ZERO);
986                 if (nims == NULL)
987                         return (ENOMEM);
988                 nims->ims_haddr = haddr;
989                 RB_INSERT(ip_msource_tree, &inm->inm_srcs, nims);
990                 ++inm->inm_nsrc;
991                 ims = nims;
992 #ifdef KTR
993                 CTR3(KTR_IGMPV3, "%s: allocated 0x%08x as %p", __func__,
994                     haddr, ims);
995 #endif
996         }
997
998         *pims = ims;
999         return (0);
1000 }
1001
1002 /*
1003  * Merge socket-layer source into IGMP-layer source.
1004  * If rollback is non-zero, perform the inverse of the merge.
1005  */
1006 static void
1007 ims_merge(struct ip_msource *ims, const struct in_msource *lims,
1008     const int rollback)
1009 {
1010         int n = rollback ? -1 : 1;
1011
1012         if (lims->imsl_st[0] == MCAST_EXCLUDE) {
1013                 CTR3(KTR_IGMPV3, "%s: t1 ex -= %d on 0x%08x",
1014                     __func__, n, ims->ims_haddr);
1015                 ims->ims_st[1].ex -= n;
1016         } else if (lims->imsl_st[0] == MCAST_INCLUDE) {
1017                 CTR3(KTR_IGMPV3, "%s: t1 in -= %d on 0x%08x",
1018                     __func__, n, ims->ims_haddr);
1019                 ims->ims_st[1].in -= n;
1020         }
1021
1022         if (lims->imsl_st[1] == MCAST_EXCLUDE) {
1023                 CTR3(KTR_IGMPV3, "%s: t1 ex += %d on 0x%08x",
1024                     __func__, n, ims->ims_haddr);
1025                 ims->ims_st[1].ex += n;
1026         } else if (lims->imsl_st[1] == MCAST_INCLUDE) {
1027                 CTR3(KTR_IGMPV3, "%s: t1 in += %d on 0x%08x",
1028                     __func__, n, ims->ims_haddr);
1029                 ims->ims_st[1].in += n;
1030         }
1031 }
1032
1033 /*
1034  * Atomically update the global in_multi state, when a membership's
1035  * filter list is being updated in any way.
1036  *
1037  * imf is the per-inpcb-membership group filter pointer.
1038  * A fake imf may be passed for in-kernel consumers.
1039  *
1040  * XXX This is a candidate for a set-symmetric-difference style loop
1041  * which would eliminate the repeated lookup from root of ims nodes,
1042  * as they share the same key space.
1043  *
1044  * If any error occurred this function will back out of refcounts
1045  * and return a non-zero value.
1046  */
1047 static int
1048 inm_merge(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1049 {
1050         struct ip_msource       *ims, *nims;
1051         struct in_msource       *lims;
1052         int                      schanged, error;
1053         int                      nsrc0, nsrc1;
1054
1055         schanged = 0;
1056         error = 0;
1057         nsrc1 = nsrc0 = 0;
1058         IN_MULTI_LIST_LOCK_ASSERT();
1059
1060         /*
1061          * Update the source filters first, as this may fail.
1062          * Maintain count of in-mode filters at t0, t1. These are
1063          * used to work out if we transition into ASM mode or not.
1064          * Maintain a count of source filters whose state was
1065          * actually modified by this operation.
1066          */
1067         RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1068                 lims = (struct in_msource *)ims;
1069                 if (lims->imsl_st[0] == imf->imf_st[0]) nsrc0++;
1070                 if (lims->imsl_st[1] == imf->imf_st[1]) nsrc1++;
1071                 if (lims->imsl_st[0] == lims->imsl_st[1]) continue;
1072                 error = inm_get_source(inm, lims->ims_haddr, 0, &nims);
1073                 ++schanged;
1074                 if (error)
1075                         break;
1076                 ims_merge(nims, lims, 0);
1077         }
1078         if (error) {
1079                 struct ip_msource *bims;
1080
1081                 RB_FOREACH_REVERSE_FROM(ims, ip_msource_tree, nims) {
1082                         lims = (struct in_msource *)ims;
1083                         if (lims->imsl_st[0] == lims->imsl_st[1])
1084                                 continue;
1085                         (void)inm_get_source(inm, lims->ims_haddr, 1, &bims);
1086                         if (bims == NULL)
1087                                 continue;
1088                         ims_merge(bims, lims, 1);
1089                 }
1090                 goto out_reap;
1091         }
1092
1093         CTR3(KTR_IGMPV3, "%s: imf filters in-mode: %d at t0, %d at t1",
1094             __func__, nsrc0, nsrc1);
1095
1096         /* Handle transition between INCLUDE {n} and INCLUDE {} on socket. */
1097         if (imf->imf_st[0] == imf->imf_st[1] &&
1098             imf->imf_st[1] == MCAST_INCLUDE) {
1099                 if (nsrc1 == 0) {
1100                         CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1101                         --inm->inm_st[1].iss_in;
1102                 }
1103         }
1104
1105         /* Handle filter mode transition on socket. */
1106         if (imf->imf_st[0] != imf->imf_st[1]) {
1107                 CTR3(KTR_IGMPV3, "%s: imf transition %d to %d",
1108                     __func__, imf->imf_st[0], imf->imf_st[1]);
1109
1110                 if (imf->imf_st[0] == MCAST_EXCLUDE) {
1111                         CTR1(KTR_IGMPV3, "%s: --ex on inm at t1", __func__);
1112                         --inm->inm_st[1].iss_ex;
1113                 } else if (imf->imf_st[0] == MCAST_INCLUDE) {
1114                         CTR1(KTR_IGMPV3, "%s: --in on inm at t1", __func__);
1115                         --inm->inm_st[1].iss_in;
1116                 }
1117
1118                 if (imf->imf_st[1] == MCAST_EXCLUDE) {
1119                         CTR1(KTR_IGMPV3, "%s: ex++ on inm at t1", __func__);
1120                         inm->inm_st[1].iss_ex++;
1121                 } else if (imf->imf_st[1] == MCAST_INCLUDE && nsrc1 > 0) {
1122                         CTR1(KTR_IGMPV3, "%s: in++ on inm at t1", __func__);
1123                         inm->inm_st[1].iss_in++;
1124                 }
1125         }
1126
1127         /*
1128          * Track inm filter state in terms of listener counts.
1129          * If there are any exclusive listeners, stack-wide
1130          * membership is exclusive.
1131          * Otherwise, if only inclusive listeners, stack-wide is inclusive.
1132          * If no listeners remain, state is undefined at t1,
1133          * and the IGMP lifecycle for this group should finish.
1134          */
1135         if (inm->inm_st[1].iss_ex > 0) {
1136                 CTR1(KTR_IGMPV3, "%s: transition to EX", __func__);
1137                 inm->inm_st[1].iss_fmode = MCAST_EXCLUDE;
1138         } else if (inm->inm_st[1].iss_in > 0) {
1139                 CTR1(KTR_IGMPV3, "%s: transition to IN", __func__);
1140                 inm->inm_st[1].iss_fmode = MCAST_INCLUDE;
1141         } else {
1142                 CTR1(KTR_IGMPV3, "%s: transition to UNDEF", __func__);
1143                 inm->inm_st[1].iss_fmode = MCAST_UNDEFINED;
1144         }
1145
1146         /* Decrement ASM listener count on transition out of ASM mode. */
1147         if (imf->imf_st[0] == MCAST_EXCLUDE && nsrc0 == 0) {
1148                 if ((imf->imf_st[1] != MCAST_EXCLUDE) ||
1149                     (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 > 0)) {
1150                         CTR1(KTR_IGMPV3, "%s: --asm on inm at t1", __func__);
1151                         --inm->inm_st[1].iss_asm;
1152                 }
1153         }
1154
1155         /* Increment ASM listener count on transition to ASM mode. */
1156         if (imf->imf_st[1] == MCAST_EXCLUDE && nsrc1 == 0) {
1157                 CTR1(KTR_IGMPV3, "%s: asm++ on inm at t1", __func__);
1158                 inm->inm_st[1].iss_asm++;
1159         }
1160
1161         CTR3(KTR_IGMPV3, "%s: merged imf %p to inm %p", __func__, imf, inm);
1162         inm_print(inm);
1163
1164 out_reap:
1165         if (schanged > 0) {
1166                 CTR1(KTR_IGMPV3, "%s: sources changed; reaping", __func__);
1167                 inm_reap(inm);
1168         }
1169         return (error);
1170 }
1171
1172 /*
1173  * Mark an in_multi's filter set deltas as committed.
1174  * Called by IGMP after a state change has been enqueued.
1175  */
1176 void
1177 inm_commit(struct in_multi *inm)
1178 {
1179         struct ip_msource       *ims;
1180
1181         CTR2(KTR_IGMPV3, "%s: commit inm %p", __func__, inm);
1182         CTR1(KTR_IGMPV3, "%s: pre commit:", __func__);
1183         inm_print(inm);
1184
1185         RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
1186                 ims->ims_st[0] = ims->ims_st[1];
1187         }
1188         inm->inm_st[0] = inm->inm_st[1];
1189 }
1190
1191 /*
1192  * Reap unreferenced nodes from an in_multi's filter set.
1193  */
1194 static void
1195 inm_reap(struct in_multi *inm)
1196 {
1197         struct ip_msource       *ims, *tims;
1198
1199         RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1200                 if (ims->ims_st[0].ex > 0 || ims->ims_st[0].in > 0 ||
1201                     ims->ims_st[1].ex > 0 || ims->ims_st[1].in > 0 ||
1202                     ims->ims_stp != 0)
1203                         continue;
1204                 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1205                 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1206                 free(ims, M_IPMSOURCE);
1207                 inm->inm_nsrc--;
1208         }
1209 }
1210
1211 /*
1212  * Purge all source nodes from an in_multi's filter set.
1213  */
1214 static void
1215 inm_purge(struct in_multi *inm)
1216 {
1217         struct ip_msource       *ims, *tims;
1218
1219         RB_FOREACH_SAFE(ims, ip_msource_tree, &inm->inm_srcs, tims) {
1220                 CTR2(KTR_IGMPV3, "%s: free ims %p", __func__, ims);
1221                 RB_REMOVE(ip_msource_tree, &inm->inm_srcs, ims);
1222                 free(ims, M_IPMSOURCE);
1223                 inm->inm_nsrc--;
1224         }
1225 }
1226
1227 /*
1228  * Join a multicast group; unlocked entry point.
1229  *
1230  * SMPng: XXX: in_joingroup() is called from in_control() when Giant
1231  * is not held. Fortunately, ifp is unlikely to have been detached
1232  * at this point, so we assume it's OK to recurse.
1233  */
1234 int
1235 in_joingroup(struct ifnet *ifp, const struct in_addr *gina,
1236     /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1237 {
1238         int error;
1239
1240         IN_MULTI_LOCK();
1241         error = in_joingroup_locked(ifp, gina, imf, pinm);
1242         IN_MULTI_UNLOCK();
1243
1244         return (error);
1245 }
1246
1247 /*
1248  * Join a multicast group; real entry point.
1249  *
1250  * Only preserves atomicity at inm level.
1251  * NOTE: imf argument cannot be const due to sys/tree.h limitations.
1252  *
1253  * If the IGMP downcall fails, the group is not joined, and an error
1254  * code is returned.
1255  */
1256 int
1257 in_joingroup_locked(struct ifnet *ifp, const struct in_addr *gina,
1258     /*const*/ struct in_mfilter *imf, struct in_multi **pinm)
1259 {
1260         struct in_mfilter        timf;
1261         struct in_multi         *inm;
1262         int                      error;
1263
1264         IN_MULTI_LOCK_ASSERT();
1265         IN_MULTI_LIST_UNLOCK_ASSERT();
1266
1267         CTR4(KTR_IGMPV3, "%s: join 0x%08x on %p(%s))", __func__,
1268             ntohl(gina->s_addr), ifp, ifp->if_xname);
1269
1270         error = 0;
1271         inm = NULL;
1272
1273         /*
1274          * If no imf was specified (i.e. kernel consumer),
1275          * fake one up and assume it is an ASM join.
1276          */
1277         if (imf == NULL) {
1278                 imf_init(&timf, MCAST_UNDEFINED, MCAST_EXCLUDE);
1279                 imf = &timf;
1280         }
1281
1282         error = in_getmulti(ifp, gina, &inm);
1283         if (error) {
1284                 CTR1(KTR_IGMPV3, "%s: in_getmulti() failure", __func__);
1285                 return (error);
1286         }
1287         IN_MULTI_LIST_LOCK();
1288         CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1289         error = inm_merge(inm, imf);
1290         if (error) {
1291                 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1292                 goto out_inm_release;
1293         }
1294
1295         CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1296         error = igmp_change_state(inm);
1297         if (error) {
1298                 CTR1(KTR_IGMPV3, "%s: failed to update source", __func__);
1299                 goto out_inm_release;
1300         }
1301
1302  out_inm_release:
1303         if (error) {
1304
1305                 CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1306                 inm_release_deferred(inm);
1307         } else {
1308                 *pinm = inm;
1309         }
1310         IN_MULTI_LIST_UNLOCK();
1311
1312         return (error);
1313 }
1314
1315 /*
1316  * Leave a multicast group; unlocked entry point.
1317  */
1318 int
1319 in_leavegroup(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1320 {
1321         int error;
1322
1323         IN_MULTI_LOCK();
1324         error = in_leavegroup_locked(inm, imf);
1325         IN_MULTI_UNLOCK();
1326
1327         return (error);
1328 }
1329
1330 /*
1331  * Leave a multicast group; real entry point.
1332  * All source filters will be expunged.
1333  *
1334  * Only preserves atomicity at inm level.
1335  *
1336  * Holding the write lock for the INP which contains imf
1337  * is highly advisable. We can't assert for it as imf does not
1338  * contain a back-pointer to the owning inp.
1339  *
1340  * Note: This is not the same as inm_release(*) as this function also
1341  * makes a state change downcall into IGMP.
1342  */
1343 int
1344 in_leavegroup_locked(struct in_multi *inm, /*const*/ struct in_mfilter *imf)
1345 {
1346         struct in_mfilter        timf;
1347         int                      error;
1348
1349         error = 0;
1350
1351         IN_MULTI_LOCK_ASSERT();
1352         IN_MULTI_LIST_UNLOCK_ASSERT();
1353
1354         CTR5(KTR_IGMPV3, "%s: leave inm %p, 0x%08x/%s, imf %p", __func__,
1355             inm, ntohl(inm->inm_addr.s_addr),
1356             (inm_is_ifp_detached(inm) ? "null" : inm->inm_ifp->if_xname),
1357             imf);
1358
1359         /*
1360          * If no imf was specified (i.e. kernel consumer),
1361          * fake one up and assume it is an ASM join.
1362          */
1363         if (imf == NULL) {
1364                 imf_init(&timf, MCAST_EXCLUDE, MCAST_UNDEFINED);
1365                 imf = &timf;
1366         }
1367
1368         /*
1369          * Begin state merge transaction at IGMP layer.
1370          *
1371          * As this particular invocation should not cause any memory
1372          * to be allocated, and there is no opportunity to roll back
1373          * the transaction, it MUST NOT fail.
1374          */
1375         CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1376         IN_MULTI_LIST_LOCK();
1377         error = inm_merge(inm, imf);
1378         KASSERT(error == 0, ("%s: failed to merge inm state", __func__));
1379
1380         CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1381         CURVNET_SET(inm->inm_ifp->if_vnet);
1382         error = igmp_change_state(inm);
1383         IF_ADDR_WLOCK(inm->inm_ifp);
1384         inm_release_deferred(inm);
1385         IF_ADDR_WUNLOCK(inm->inm_ifp);
1386         IN_MULTI_LIST_UNLOCK();
1387         CURVNET_RESTORE();
1388         if (error)
1389                 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1390
1391         CTR2(KTR_IGMPV3, "%s: dropping ref on %p", __func__, inm);
1392
1393         return (error);
1394 }
1395
1396 /*#ifndef BURN_BRIDGES*/
1397 /*
1398  * Join an IPv4 multicast group in (*,G) exclusive mode.
1399  * The group must be a 224.0.0.0/24 link-scope group.
1400  * This KPI is for legacy kernel consumers only.
1401  */
1402 struct in_multi *
1403 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1404 {
1405         struct in_multi *pinm;
1406         int error;
1407 #ifdef INVARIANTS
1408         char addrbuf[INET_ADDRSTRLEN];
1409 #endif
1410
1411         KASSERT(IN_LOCAL_GROUP(ntohl(ap->s_addr)),
1412             ("%s: %s not in 224.0.0.0/24", __func__,
1413             inet_ntoa_r(*ap, addrbuf)));
1414
1415         error = in_joingroup(ifp, ap, NULL, &pinm);
1416         if (error != 0)
1417                 pinm = NULL;
1418
1419         return (pinm);
1420 }
1421
1422 /*
1423  * Block or unblock an ASM multicast source on an inpcb.
1424  * This implements the delta-based API described in RFC 3678.
1425  *
1426  * The delta-based API applies only to exclusive-mode memberships.
1427  * An IGMP downcall will be performed.
1428  *
1429  * SMPng: NOTE: Must take Giant as a join may create a new ifma.
1430  *
1431  * Return 0 if successful, otherwise return an appropriate error code.
1432  */
1433 static int
1434 inp_block_unblock_source(struct inpcb *inp, struct sockopt *sopt)
1435 {
1436         struct group_source_req          gsr;
1437         sockunion_t                     *gsa, *ssa;
1438         struct ifnet                    *ifp;
1439         struct in_mfilter               *imf;
1440         struct ip_moptions              *imo;
1441         struct in_msource               *ims;
1442         struct in_multi                 *inm;
1443         size_t                           idx;
1444         uint16_t                         fmode;
1445         int                              error, doblock;
1446
1447         ifp = NULL;
1448         error = 0;
1449         doblock = 0;
1450
1451         memset(&gsr, 0, sizeof(struct group_source_req));
1452         gsa = (sockunion_t *)&gsr.gsr_group;
1453         ssa = (sockunion_t *)&gsr.gsr_source;
1454
1455         switch (sopt->sopt_name) {
1456         case IP_BLOCK_SOURCE:
1457         case IP_UNBLOCK_SOURCE: {
1458                 struct ip_mreq_source    mreqs;
1459
1460                 error = sooptcopyin(sopt, &mreqs,
1461                     sizeof(struct ip_mreq_source),
1462                     sizeof(struct ip_mreq_source));
1463                 if (error)
1464                         return (error);
1465
1466                 gsa->sin.sin_family = AF_INET;
1467                 gsa->sin.sin_len = sizeof(struct sockaddr_in);
1468                 gsa->sin.sin_addr = mreqs.imr_multiaddr;
1469
1470                 ssa->sin.sin_family = AF_INET;
1471                 ssa->sin.sin_len = sizeof(struct sockaddr_in);
1472                 ssa->sin.sin_addr = mreqs.imr_sourceaddr;
1473
1474                 if (!in_nullhost(mreqs.imr_interface))
1475                         INADDR_TO_IFP(mreqs.imr_interface, ifp);
1476
1477                 if (sopt->sopt_name == IP_BLOCK_SOURCE)
1478                         doblock = 1;
1479
1480                 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
1481                     __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
1482                 break;
1483             }
1484
1485         case MCAST_BLOCK_SOURCE:
1486         case MCAST_UNBLOCK_SOURCE:
1487                 error = sooptcopyin(sopt, &gsr,
1488                     sizeof(struct group_source_req),
1489                     sizeof(struct group_source_req));
1490                 if (error)
1491                         return (error);
1492
1493                 if (gsa->sin.sin_family != AF_INET ||
1494                     gsa->sin.sin_len != sizeof(struct sockaddr_in))
1495                         return (EINVAL);
1496
1497                 if (ssa->sin.sin_family != AF_INET ||
1498                     ssa->sin.sin_len != sizeof(struct sockaddr_in))
1499                         return (EINVAL);
1500
1501                 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
1502                         return (EADDRNOTAVAIL);
1503
1504                 ifp = ifnet_byindex(gsr.gsr_interface);
1505
1506                 if (sopt->sopt_name == MCAST_BLOCK_SOURCE)
1507                         doblock = 1;
1508                 break;
1509
1510         default:
1511                 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
1512                     __func__, sopt->sopt_name);
1513                 return (EOPNOTSUPP);
1514                 break;
1515         }
1516
1517         if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
1518                 return (EINVAL);
1519
1520         /*
1521          * Check if we are actually a member of this group.
1522          */
1523         imo = inp_findmoptions(inp);
1524         idx = imo_match_group(imo, ifp, &gsa->sa);
1525         if (idx == -1 || imo->imo_mfilters == NULL) {
1526                 error = EADDRNOTAVAIL;
1527                 goto out_inp_locked;
1528         }
1529
1530         KASSERT(imo->imo_mfilters != NULL,
1531             ("%s: imo_mfilters not allocated", __func__));
1532         imf = &imo->imo_mfilters[idx];
1533         inm = imo->imo_membership[idx];
1534
1535         /*
1536          * Attempting to use the delta-based API on an
1537          * non exclusive-mode membership is an error.
1538          */
1539         fmode = imf->imf_st[0];
1540         if (fmode != MCAST_EXCLUDE) {
1541                 error = EINVAL;
1542                 goto out_inp_locked;
1543         }
1544
1545         /*
1546          * Deal with error cases up-front:
1547          *  Asked to block, but already blocked; or
1548          *  Asked to unblock, but nothing to unblock.
1549          * If adding a new block entry, allocate it.
1550          */
1551         ims = imo_match_source(imo, idx, &ssa->sa);
1552         if ((ims != NULL && doblock) || (ims == NULL && !doblock)) {
1553                 CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent", __func__,
1554                     ntohl(ssa->sin.sin_addr.s_addr), doblock ? "" : "not ");
1555                 error = EADDRNOTAVAIL;
1556                 goto out_inp_locked;
1557         }
1558
1559         INP_WLOCK_ASSERT(inp);
1560
1561         /*
1562          * Begin state merge transaction at socket layer.
1563          */
1564         if (doblock) {
1565                 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
1566                 ims = imf_graft(imf, fmode, &ssa->sin);
1567                 if (ims == NULL)
1568                         error = ENOMEM;
1569         } else {
1570                 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
1571                 error = imf_prune(imf, &ssa->sin);
1572         }
1573
1574         if (error) {
1575                 CTR1(KTR_IGMPV3, "%s: merge imf state failed", __func__);
1576                 goto out_imf_rollback;
1577         }
1578
1579         /*
1580          * Begin state merge transaction at IGMP layer.
1581          */
1582         IN_MULTI_LOCK();
1583         IN_MULTI_LIST_LOCK();
1584         CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
1585         error = inm_merge(inm, imf);
1586         if (error) {
1587                 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
1588                 goto out_in_multi_locked;
1589         }
1590
1591         CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
1592         error = igmp_change_state(inm);
1593         if (error)
1594                 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
1595
1596 out_in_multi_locked:
1597
1598         IN_MULTI_UNLOCK();
1599         IN_MULTI_UNLOCK();
1600 out_imf_rollback:
1601         if (error)
1602                 imf_rollback(imf);
1603         else
1604                 imf_commit(imf);
1605
1606         imf_reap(imf);
1607
1608 out_inp_locked:
1609         INP_WUNLOCK(inp);
1610         return (error);
1611 }
1612
1613 /*
1614  * Given an inpcb, return its multicast options structure pointer.  Accepts
1615  * an unlocked inpcb pointer, but will return it locked.  May sleep.
1616  *
1617  * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
1618  * SMPng: NOTE: Returns with the INP write lock held.
1619  */
1620 static struct ip_moptions *
1621 inp_findmoptions(struct inpcb *inp)
1622 {
1623         struct ip_moptions       *imo;
1624         struct in_multi         **immp;
1625         struct in_mfilter        *imfp;
1626         size_t                    idx;
1627
1628         INP_WLOCK(inp);
1629         if (inp->inp_moptions != NULL)
1630                 return (inp->inp_moptions);
1631
1632         INP_WUNLOCK(inp);
1633
1634         imo = malloc(sizeof(*imo), M_IPMOPTS, M_WAITOK);
1635         immp = malloc(sizeof(*immp) * IP_MIN_MEMBERSHIPS, M_IPMOPTS,
1636             M_WAITOK | M_ZERO);
1637         imfp = malloc(sizeof(struct in_mfilter) * IP_MIN_MEMBERSHIPS,
1638             M_INMFILTER, M_WAITOK);
1639
1640         imo->imo_multicast_ifp = NULL;
1641         imo->imo_multicast_addr.s_addr = INADDR_ANY;
1642         imo->imo_multicast_vif = -1;
1643         imo->imo_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1644         imo->imo_multicast_loop = in_mcast_loop;
1645         imo->imo_num_memberships = 0;
1646         imo->imo_max_memberships = IP_MIN_MEMBERSHIPS;
1647         imo->imo_membership = immp;
1648
1649         /* Initialize per-group source filters. */
1650         for (idx = 0; idx < IP_MIN_MEMBERSHIPS; idx++)
1651                 imf_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE);
1652         imo->imo_mfilters = imfp;
1653
1654         INP_WLOCK(inp);
1655         if (inp->inp_moptions != NULL) {
1656                 free(imfp, M_INMFILTER);
1657                 free(immp, M_IPMOPTS);
1658                 free(imo, M_IPMOPTS);
1659                 return (inp->inp_moptions);
1660         }
1661         inp->inp_moptions = imo;
1662         return (imo);
1663 }
1664
1665 static void
1666 inp_gcmoptions(epoch_context_t ctx)
1667 {
1668         struct ip_moptions *imo;
1669         struct in_mfilter       *imf;
1670         struct in_multi *inm;
1671         struct ifnet *ifp;
1672         struct vnet *saved_vnet;
1673         size_t                   idx, nmships;
1674
1675         imo =  __containerof(ctx, struct ip_moptions, imo_epoch_ctx);
1676
1677         nmships = imo->imo_num_memberships;
1678         for (idx = 0; idx < nmships; ++idx) {
1679                 imf = imo->imo_mfilters ? &imo->imo_mfilters[idx] : NULL;
1680                 if (imf)
1681                         imf_leave(imf);
1682                 inm = imo->imo_membership[idx];
1683                 ifp = inm->inm_ifp;
1684                 if (ifp) {
1685                         saved_vnet = curvnet;
1686                         curvnet = ifp->if_vnet;
1687                 }
1688                 (void)in_leavegroup(inm, imf);
1689                 if (ifp)
1690                         curvnet = saved_vnet;
1691                 if (imf)
1692                         imf_purge(imf);
1693         }
1694
1695         if (imo->imo_mfilters)
1696                 free(imo->imo_mfilters, M_INMFILTER);
1697         free(imo->imo_membership, M_IPMOPTS);
1698         free(imo, M_IPMOPTS);
1699 }
1700
1701 /*
1702  * Discard the IP multicast options (and source filters).  To minimize
1703  * the amount of work done while holding locks such as the INP's
1704  * pcbinfo lock (which is used in the receive path), the free
1705  * operation is deferred to the epoch callback task.
1706  */
1707 void
1708 inp_freemoptions(struct ip_moptions *imo)
1709 {
1710         if (imo == NULL)
1711                 return;
1712         epoch_call(net_epoch_preempt, &imo->imo_epoch_ctx, inp_gcmoptions);
1713 }
1714
1715 /*
1716  * Atomically get source filters on a socket for an IPv4 multicast group.
1717  * Called with INP lock held; returns with lock released.
1718  */
1719 static int
1720 inp_get_source_filters(struct inpcb *inp, struct sockopt *sopt)
1721 {
1722         struct __msfilterreq     msfr;
1723         sockunion_t             *gsa;
1724         struct ifnet            *ifp;
1725         struct ip_moptions      *imo;
1726         struct in_mfilter       *imf;
1727         struct ip_msource       *ims;
1728         struct in_msource       *lims;
1729         struct sockaddr_in      *psin;
1730         struct sockaddr_storage *ptss;
1731         struct sockaddr_storage *tss;
1732         int                      error;
1733         size_t                   idx, nsrcs, ncsrcs;
1734
1735         INP_WLOCK_ASSERT(inp);
1736
1737         imo = inp->inp_moptions;
1738         KASSERT(imo != NULL, ("%s: null ip_moptions", __func__));
1739
1740         INP_WUNLOCK(inp);
1741
1742         error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
1743             sizeof(struct __msfilterreq));
1744         if (error)
1745                 return (error);
1746
1747         if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
1748                 return (EINVAL);
1749
1750         ifp = ifnet_byindex(msfr.msfr_ifindex);
1751         if (ifp == NULL)
1752                 return (EINVAL);
1753
1754         INP_WLOCK(inp);
1755
1756         /*
1757          * Lookup group on the socket.
1758          */
1759         gsa = (sockunion_t *)&msfr.msfr_group;
1760         idx = imo_match_group(imo, ifp, &gsa->sa);
1761         if (idx == -1 || imo->imo_mfilters == NULL) {
1762                 INP_WUNLOCK(inp);
1763                 return (EADDRNOTAVAIL);
1764         }
1765         imf = &imo->imo_mfilters[idx];
1766
1767         /*
1768          * Ignore memberships which are in limbo.
1769          */
1770         if (imf->imf_st[1] == MCAST_UNDEFINED) {
1771                 INP_WUNLOCK(inp);
1772                 return (EAGAIN);
1773         }
1774         msfr.msfr_fmode = imf->imf_st[1];
1775
1776         /*
1777          * If the user specified a buffer, copy out the source filter
1778          * entries to userland gracefully.
1779          * We only copy out the number of entries which userland
1780          * has asked for, but we always tell userland how big the
1781          * buffer really needs to be.
1782          */
1783         if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
1784                 msfr.msfr_nsrcs = in_mcast_maxsocksrc;
1785         tss = NULL;
1786         if (msfr.msfr_srcs != NULL && msfr.msfr_nsrcs > 0) {
1787                 tss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
1788                     M_TEMP, M_NOWAIT | M_ZERO);
1789                 if (tss == NULL) {
1790                         INP_WUNLOCK(inp);
1791                         return (ENOBUFS);
1792                 }
1793         }
1794
1795         /*
1796          * Count number of sources in-mode at t0.
1797          * If buffer space exists and remains, copy out source entries.
1798          */
1799         nsrcs = msfr.msfr_nsrcs;
1800         ncsrcs = 0;
1801         ptss = tss;
1802         RB_FOREACH(ims, ip_msource_tree, &imf->imf_sources) {
1803                 lims = (struct in_msource *)ims;
1804                 if (lims->imsl_st[0] == MCAST_UNDEFINED ||
1805                     lims->imsl_st[0] != imf->imf_st[0])
1806                         continue;
1807                 ++ncsrcs;
1808                 if (tss != NULL && nsrcs > 0) {
1809                         psin = (struct sockaddr_in *)ptss;
1810                         psin->sin_family = AF_INET;
1811                         psin->sin_len = sizeof(struct sockaddr_in);
1812                         psin->sin_addr.s_addr = htonl(lims->ims_haddr);
1813                         psin->sin_port = 0;
1814                         ++ptss;
1815                         --nsrcs;
1816                 }
1817         }
1818
1819         INP_WUNLOCK(inp);
1820
1821         if (tss != NULL) {
1822                 error = copyout(tss, msfr.msfr_srcs,
1823                     sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
1824                 free(tss, M_TEMP);
1825                 if (error)
1826                         return (error);
1827         }
1828
1829         msfr.msfr_nsrcs = ncsrcs;
1830         error = sooptcopyout(sopt, &msfr, sizeof(struct __msfilterreq));
1831
1832         return (error);
1833 }
1834
1835 /*
1836  * Return the IP multicast options in response to user getsockopt().
1837  */
1838 int
1839 inp_getmoptions(struct inpcb *inp, struct sockopt *sopt)
1840 {
1841         struct rm_priotracker    in_ifa_tracker;
1842         struct ip_mreqn          mreqn;
1843         struct ip_moptions      *imo;
1844         struct ifnet            *ifp;
1845         struct in_ifaddr        *ia;
1846         int                      error, optval;
1847         u_char                   coptval;
1848
1849         INP_WLOCK(inp);
1850         imo = inp->inp_moptions;
1851         /*
1852          * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
1853          * or is a divert socket, reject it.
1854          */
1855         if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
1856             (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
1857             inp->inp_socket->so_proto->pr_type != SOCK_DGRAM)) {
1858                 INP_WUNLOCK(inp);
1859                 return (EOPNOTSUPP);
1860         }
1861
1862         error = 0;
1863         switch (sopt->sopt_name) {
1864         case IP_MULTICAST_VIF:
1865                 if (imo != NULL)
1866                         optval = imo->imo_multicast_vif;
1867                 else
1868                         optval = -1;
1869                 INP_WUNLOCK(inp);
1870                 error = sooptcopyout(sopt, &optval, sizeof(int));
1871                 break;
1872
1873         case IP_MULTICAST_IF:
1874                 memset(&mreqn, 0, sizeof(struct ip_mreqn));
1875                 if (imo != NULL) {
1876                         ifp = imo->imo_multicast_ifp;
1877                         if (!in_nullhost(imo->imo_multicast_addr)) {
1878                                 mreqn.imr_address = imo->imo_multicast_addr;
1879                         } else if (ifp != NULL) {
1880                                 mreqn.imr_ifindex = ifp->if_index;
1881                                 IFP_TO_IA(ifp, ia, &in_ifa_tracker);
1882                                 if (ia != NULL) {
1883                                         mreqn.imr_address =
1884                                             IA_SIN(ia)->sin_addr;
1885                                         ifa_free(&ia->ia_ifa);
1886                                 }
1887                         }
1888                 }
1889                 INP_WUNLOCK(inp);
1890                 if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
1891                         error = sooptcopyout(sopt, &mreqn,
1892                             sizeof(struct ip_mreqn));
1893                 } else {
1894                         error = sooptcopyout(sopt, &mreqn.imr_address,
1895                             sizeof(struct in_addr));
1896                 }
1897                 break;
1898
1899         case IP_MULTICAST_TTL:
1900                 if (imo == NULL)
1901                         optval = coptval = IP_DEFAULT_MULTICAST_TTL;
1902                 else
1903                         optval = coptval = imo->imo_multicast_ttl;
1904                 INP_WUNLOCK(inp);
1905                 if (sopt->sopt_valsize == sizeof(u_char))
1906                         error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1907                 else
1908                         error = sooptcopyout(sopt, &optval, sizeof(int));
1909                 break;
1910
1911         case IP_MULTICAST_LOOP:
1912                 if (imo == NULL)
1913                         optval = coptval = IP_DEFAULT_MULTICAST_LOOP;
1914                 else
1915                         optval = coptval = imo->imo_multicast_loop;
1916                 INP_WUNLOCK(inp);
1917                 if (sopt->sopt_valsize == sizeof(u_char))
1918                         error = sooptcopyout(sopt, &coptval, sizeof(u_char));
1919                 else
1920                         error = sooptcopyout(sopt, &optval, sizeof(int));
1921                 break;
1922
1923         case IP_MSFILTER:
1924                 if (imo == NULL) {
1925                         error = EADDRNOTAVAIL;
1926                         INP_WUNLOCK(inp);
1927                 } else {
1928                         error = inp_get_source_filters(inp, sopt);
1929                 }
1930                 break;
1931
1932         default:
1933                 INP_WUNLOCK(inp);
1934                 error = ENOPROTOOPT;
1935                 break;
1936         }
1937
1938         INP_UNLOCK_ASSERT(inp);
1939
1940         return (error);
1941 }
1942
1943 /*
1944  * Look up the ifnet to use for a multicast group membership,
1945  * given the IPv4 address of an interface, and the IPv4 group address.
1946  *
1947  * This routine exists to support legacy multicast applications
1948  * which do not understand that multicast memberships are scoped to
1949  * specific physical links in the networking stack, or which need
1950  * to join link-scope groups before IPv4 addresses are configured.
1951  *
1952  * If inp is non-NULL, use this socket's current FIB number for any
1953  * required FIB lookup.
1954  * If ina is INADDR_ANY, look up the group address in the unicast FIB,
1955  * and use its ifp; usually, this points to the default next-hop.
1956  *
1957  * If the FIB lookup fails, attempt to use the first non-loopback
1958  * interface with multicast capability in the system as a
1959  * last resort. The legacy IPv4 ASM API requires that we do
1960  * this in order to allow groups to be joined when the routing
1961  * table has not yet been populated during boot.
1962  *
1963  * Returns NULL if no ifp could be found.
1964  *
1965  * SMPng: TODO: Acquire the appropriate locks for INADDR_TO_IFP.
1966  * FUTURE: Implement IPv4 source-address selection.
1967  */
1968 static struct ifnet *
1969 inp_lookup_mcast_ifp(const struct inpcb *inp,
1970     const struct sockaddr_in *gsin, const struct in_addr ina)
1971 {
1972         struct rm_priotracker in_ifa_tracker;
1973         struct ifnet *ifp;
1974         struct nhop4_basic nh4;
1975         uint32_t fibnum;
1976
1977         KASSERT(gsin->sin_family == AF_INET, ("%s: not AF_INET", __func__));
1978         KASSERT(IN_MULTICAST(ntohl(gsin->sin_addr.s_addr)),
1979             ("%s: not multicast", __func__));
1980
1981         ifp = NULL;
1982         if (!in_nullhost(ina)) {
1983                 INADDR_TO_IFP(ina, ifp);
1984         } else {
1985                 fibnum = inp ? inp->inp_inc.inc_fibnum : 0;
1986                 if (fib4_lookup_nh_basic(fibnum, gsin->sin_addr, 0, 0, &nh4)==0)
1987                         ifp = nh4.nh_ifp;
1988                 else {
1989                         struct in_ifaddr *ia;
1990                         struct ifnet *mifp;
1991
1992                         mifp = NULL;
1993                         IN_IFADDR_RLOCK(&in_ifa_tracker);
1994                         CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1995                                 mifp = ia->ia_ifp;
1996                                 if (!(mifp->if_flags & IFF_LOOPBACK) &&
1997                                      (mifp->if_flags & IFF_MULTICAST)) {
1998                                         ifp = mifp;
1999                                         break;
2000                                 }
2001                         }
2002                         IN_IFADDR_RUNLOCK(&in_ifa_tracker);
2003                 }
2004         }
2005
2006         return (ifp);
2007 }
2008
2009 /*
2010  * Join an IPv4 multicast group, possibly with a source.
2011  */
2012 static int
2013 inp_join_group(struct inpcb *inp, struct sockopt *sopt)
2014 {
2015         struct group_source_req          gsr;
2016         sockunion_t                     *gsa, *ssa;
2017         struct ifnet                    *ifp;
2018         struct in_mfilter               *imf;
2019         struct ip_moptions              *imo;
2020         struct in_multi                 *inm;
2021         struct in_msource               *lims;
2022         size_t                           idx;
2023         int                              error, is_new;
2024
2025         ifp = NULL;
2026         imf = NULL;
2027         lims = NULL;
2028         error = 0;
2029         is_new = 0;
2030
2031         memset(&gsr, 0, sizeof(struct group_source_req));
2032         gsa = (sockunion_t *)&gsr.gsr_group;
2033         gsa->ss.ss_family = AF_UNSPEC;
2034         ssa = (sockunion_t *)&gsr.gsr_source;
2035         ssa->ss.ss_family = AF_UNSPEC;
2036
2037         switch (sopt->sopt_name) {
2038         case IP_ADD_MEMBERSHIP:
2039         case IP_ADD_SOURCE_MEMBERSHIP: {
2040                 struct ip_mreq_source    mreqs;
2041
2042                 if (sopt->sopt_name == IP_ADD_MEMBERSHIP) {
2043                         error = sooptcopyin(sopt, &mreqs,
2044                             sizeof(struct ip_mreq),
2045                             sizeof(struct ip_mreq));
2046                         /*
2047                          * Do argument switcharoo from ip_mreq into
2048                          * ip_mreq_source to avoid using two instances.
2049                          */
2050                         mreqs.imr_interface = mreqs.imr_sourceaddr;
2051                         mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2052                 } else if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
2053                         error = sooptcopyin(sopt, &mreqs,
2054                             sizeof(struct ip_mreq_source),
2055                             sizeof(struct ip_mreq_source));
2056                 }
2057                 if (error)
2058                         return (error);
2059
2060                 gsa->sin.sin_family = AF_INET;
2061                 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2062                 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2063
2064                 if (sopt->sopt_name == IP_ADD_SOURCE_MEMBERSHIP) {
2065                         ssa->sin.sin_family = AF_INET;
2066                         ssa->sin.sin_len = sizeof(struct sockaddr_in);
2067                         ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2068                 }
2069
2070                 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2071                         return (EINVAL);
2072
2073                 ifp = inp_lookup_mcast_ifp(inp, &gsa->sin,
2074                     mreqs.imr_interface);
2075                 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2076                     __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2077                 break;
2078         }
2079
2080         case MCAST_JOIN_GROUP:
2081         case MCAST_JOIN_SOURCE_GROUP:
2082                 if (sopt->sopt_name == MCAST_JOIN_GROUP) {
2083                         error = sooptcopyin(sopt, &gsr,
2084                             sizeof(struct group_req),
2085                             sizeof(struct group_req));
2086                 } else if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2087                         error = sooptcopyin(sopt, &gsr,
2088                             sizeof(struct group_source_req),
2089                             sizeof(struct group_source_req));
2090                 }
2091                 if (error)
2092                         return (error);
2093
2094                 if (gsa->sin.sin_family != AF_INET ||
2095                     gsa->sin.sin_len != sizeof(struct sockaddr_in))
2096                         return (EINVAL);
2097
2098                 /*
2099                  * Overwrite the port field if present, as the sockaddr
2100                  * being copied in may be matched with a binary comparison.
2101                  */
2102                 gsa->sin.sin_port = 0;
2103                 if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) {
2104                         if (ssa->sin.sin_family != AF_INET ||
2105                             ssa->sin.sin_len != sizeof(struct sockaddr_in))
2106                                 return (EINVAL);
2107                         ssa->sin.sin_port = 0;
2108                 }
2109
2110                 if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2111                         return (EINVAL);
2112
2113                 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2114                         return (EADDRNOTAVAIL);
2115                 ifp = ifnet_byindex(gsr.gsr_interface);
2116                 break;
2117
2118         default:
2119                 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2120                     __func__, sopt->sopt_name);
2121                 return (EOPNOTSUPP);
2122                 break;
2123         }
2124
2125         if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0)
2126                 return (EADDRNOTAVAIL);
2127
2128         imo = inp_findmoptions(inp);
2129         idx = imo_match_group(imo, ifp, &gsa->sa);
2130         if (idx == -1) {
2131                 is_new = 1;
2132         } else {
2133                 inm = imo->imo_membership[idx];
2134                 imf = &imo->imo_mfilters[idx];
2135                 if (ssa->ss.ss_family != AF_UNSPEC) {
2136                         /*
2137                          * MCAST_JOIN_SOURCE_GROUP on an exclusive membership
2138                          * is an error. On an existing inclusive membership,
2139                          * it just adds the source to the filter list.
2140                          */
2141                         if (imf->imf_st[1] != MCAST_INCLUDE) {
2142                                 error = EINVAL;
2143                                 goto out_inp_locked;
2144                         }
2145                         /*
2146                          * Throw out duplicates.
2147                          *
2148                          * XXX FIXME: This makes a naive assumption that
2149                          * even if entries exist for *ssa in this imf,
2150                          * they will be rejected as dupes, even if they
2151                          * are not valid in the current mode (in-mode).
2152                          *
2153                          * in_msource is transactioned just as for anything
2154                          * else in SSM -- but note naive use of inm_graft()
2155                          * below for allocating new filter entries.
2156                          *
2157                          * This is only an issue if someone mixes the
2158                          * full-state SSM API with the delta-based API,
2159                          * which is discouraged in the relevant RFCs.
2160                          */
2161                         lims = imo_match_source(imo, idx, &ssa->sa);
2162                         if (lims != NULL /*&&
2163                             lims->imsl_st[1] == MCAST_INCLUDE*/) {
2164                                 error = EADDRNOTAVAIL;
2165                                 goto out_inp_locked;
2166                         }
2167                 } else {
2168                         /*
2169                          * MCAST_JOIN_GROUP on an existing exclusive
2170                          * membership is an error; return EADDRINUSE
2171                          * to preserve 4.4BSD API idempotence, and
2172                          * avoid tedious detour to code below.
2173                          * NOTE: This is bending RFC 3678 a bit.
2174                          *
2175                          * On an existing inclusive membership, this is also
2176                          * an error; if you want to change filter mode,
2177                          * you must use the userland API setsourcefilter().
2178                          * XXX We don't reject this for imf in UNDEFINED
2179                          * state at t1, because allocation of a filter
2180                          * is atomic with allocation of a membership.
2181                          */
2182                         error = EINVAL;
2183                         if (imf->imf_st[1] == MCAST_EXCLUDE)
2184                                 error = EADDRINUSE;
2185                         goto out_inp_locked;
2186                 }
2187         }
2188
2189         /*
2190          * Begin state merge transaction at socket layer.
2191          */
2192         INP_WLOCK_ASSERT(inp);
2193
2194         if (is_new) {
2195                 if (imo->imo_num_memberships == imo->imo_max_memberships) {
2196                         error = imo_grow(imo);
2197                         if (error)
2198                                 goto out_inp_locked;
2199                 }
2200                 /*
2201                  * Allocate the new slot upfront so we can deal with
2202                  * grafting the new source filter in same code path
2203                  * as for join-source on existing membership.
2204                  */
2205                 idx = imo->imo_num_memberships;
2206                 imo->imo_membership[idx] = NULL;
2207                 imo->imo_num_memberships++;
2208                 KASSERT(imo->imo_mfilters != NULL,
2209                     ("%s: imf_mfilters vector was not allocated", __func__));
2210                 imf = &imo->imo_mfilters[idx];
2211                 KASSERT(RB_EMPTY(&imf->imf_sources),
2212                     ("%s: imf_sources not empty", __func__));
2213         }
2214
2215         /*
2216          * Graft new source into filter list for this inpcb's
2217          * membership of the group. The in_multi may not have
2218          * been allocated yet if this is a new membership, however,
2219          * the in_mfilter slot will be allocated and must be initialized.
2220          *
2221          * Note: Grafting of exclusive mode filters doesn't happen
2222          * in this path.
2223          * XXX: Should check for non-NULL lims (node exists but may
2224          * not be in-mode) for interop with full-state API.
2225          */
2226         if (ssa->ss.ss_family != AF_UNSPEC) {
2227                 /* Membership starts in IN mode */
2228                 if (is_new) {
2229                         CTR1(KTR_IGMPV3, "%s: new join w/source", __func__);
2230                         imf_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE);
2231                 } else {
2232                         CTR2(KTR_IGMPV3, "%s: %s source", __func__, "allow");
2233                 }
2234                 lims = imf_graft(imf, MCAST_INCLUDE, &ssa->sin);
2235                 if (lims == NULL) {
2236                         CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2237                             __func__);
2238                         error = ENOMEM;
2239                         goto out_imo_free;
2240                 }
2241         } else {
2242                 /* No address specified; Membership starts in EX mode */
2243                 if (is_new) {
2244                         CTR1(KTR_IGMPV3, "%s: new join w/o source", __func__);
2245                         imf_init(imf, MCAST_UNDEFINED, MCAST_EXCLUDE);
2246                 }
2247         }
2248
2249         /*
2250          * Begin state merge transaction at IGMP layer.
2251          */
2252         in_pcbref(inp);
2253         INP_WUNLOCK(inp);
2254         IN_MULTI_LOCK();
2255
2256         if (is_new) {
2257                 error = in_joingroup_locked(ifp, &gsa->sin.sin_addr, imf,
2258                     &inm);
2259                 if (error) {
2260                         CTR1(KTR_IGMPV3, "%s: in_joingroup_locked failed", 
2261                             __func__);
2262                         IN_MULTI_LIST_UNLOCK();
2263                         goto out_imo_free;
2264                 }
2265                 imo->imo_membership[idx] = inm;
2266         } else {
2267                 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2268                 IN_MULTI_LIST_LOCK();
2269                 error = inm_merge(inm, imf);
2270                 if (error) {
2271                         CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2272                                  __func__);
2273                         IN_MULTI_LIST_UNLOCK();
2274                         goto out_in_multi_locked;
2275                 }
2276                 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2277                 error = igmp_change_state(inm);
2278                 IN_MULTI_LIST_UNLOCK();
2279                 if (error) {
2280                         CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2281                             __func__);
2282                         goto out_in_multi_locked;
2283                 }
2284         }
2285
2286 out_in_multi_locked:
2287
2288         IN_MULTI_UNLOCK();
2289         INP_WLOCK(inp);
2290         if (in_pcbrele_wlocked(inp))
2291                 return (ENXIO);
2292         if (error) {
2293                 imf_rollback(imf);
2294                 if (is_new)
2295                         imf_purge(imf);
2296                 else
2297                         imf_reap(imf);
2298         } else {
2299                 imf_commit(imf);
2300         }
2301
2302 out_imo_free:
2303         if (error && is_new) {
2304                 imo->imo_membership[idx] = NULL;
2305                 --imo->imo_num_memberships;
2306         }
2307
2308 out_inp_locked:
2309         INP_WUNLOCK(inp);
2310         return (error);
2311 }
2312
2313 /*
2314  * Leave an IPv4 multicast group on an inpcb, possibly with a source.
2315  */
2316 static int
2317 inp_leave_group(struct inpcb *inp, struct sockopt *sopt)
2318 {
2319         struct group_source_req          gsr;
2320         struct ip_mreq_source            mreqs;
2321         sockunion_t                     *gsa, *ssa;
2322         struct ifnet                    *ifp;
2323         struct in_mfilter               *imf;
2324         struct ip_moptions              *imo;
2325         struct in_msource               *ims;
2326         struct in_multi                 *inm;
2327         size_t                           idx;
2328         int                              error, is_final;
2329
2330         ifp = NULL;
2331         error = 0;
2332         is_final = 1;
2333
2334         memset(&gsr, 0, sizeof(struct group_source_req));
2335         gsa = (sockunion_t *)&gsr.gsr_group;
2336         gsa->ss.ss_family = AF_UNSPEC;
2337         ssa = (sockunion_t *)&gsr.gsr_source;
2338         ssa->ss.ss_family = AF_UNSPEC;
2339
2340         switch (sopt->sopt_name) {
2341         case IP_DROP_MEMBERSHIP:
2342         case IP_DROP_SOURCE_MEMBERSHIP:
2343                 if (sopt->sopt_name == IP_DROP_MEMBERSHIP) {
2344                         error = sooptcopyin(sopt, &mreqs,
2345                             sizeof(struct ip_mreq),
2346                             sizeof(struct ip_mreq));
2347                         /*
2348                          * Swap interface and sourceaddr arguments,
2349                          * as ip_mreq and ip_mreq_source are laid
2350                          * out differently.
2351                          */
2352                         mreqs.imr_interface = mreqs.imr_sourceaddr;
2353                         mreqs.imr_sourceaddr.s_addr = INADDR_ANY;
2354                 } else if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2355                         error = sooptcopyin(sopt, &mreqs,
2356                             sizeof(struct ip_mreq_source),
2357                             sizeof(struct ip_mreq_source));
2358                 }
2359                 if (error)
2360                         return (error);
2361
2362                 gsa->sin.sin_family = AF_INET;
2363                 gsa->sin.sin_len = sizeof(struct sockaddr_in);
2364                 gsa->sin.sin_addr = mreqs.imr_multiaddr;
2365
2366                 if (sopt->sopt_name == IP_DROP_SOURCE_MEMBERSHIP) {
2367                         ssa->sin.sin_family = AF_INET;
2368                         ssa->sin.sin_len = sizeof(struct sockaddr_in);
2369                         ssa->sin.sin_addr = mreqs.imr_sourceaddr;
2370                 }
2371
2372                 /*
2373                  * Attempt to look up hinted ifp from interface address.
2374                  * Fallthrough with null ifp iff lookup fails, to
2375                  * preserve 4.4BSD mcast API idempotence.
2376                  * XXX NOTE WELL: The RFC 3678 API is preferred because
2377                  * using an IPv4 address as a key is racy.
2378                  */
2379                 if (!in_nullhost(mreqs.imr_interface))
2380                         INADDR_TO_IFP(mreqs.imr_interface, ifp);
2381
2382                 CTR3(KTR_IGMPV3, "%s: imr_interface = 0x%08x, ifp = %p",
2383                     __func__, ntohl(mreqs.imr_interface.s_addr), ifp);
2384
2385                 break;
2386
2387         case MCAST_LEAVE_GROUP:
2388         case MCAST_LEAVE_SOURCE_GROUP:
2389                 if (sopt->sopt_name == MCAST_LEAVE_GROUP) {
2390                         error = sooptcopyin(sopt, &gsr,
2391                             sizeof(struct group_req),
2392                             sizeof(struct group_req));
2393                 } else if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2394                         error = sooptcopyin(sopt, &gsr,
2395                             sizeof(struct group_source_req),
2396                             sizeof(struct group_source_req));
2397                 }
2398                 if (error)
2399                         return (error);
2400
2401                 if (gsa->sin.sin_family != AF_INET ||
2402                     gsa->sin.sin_len != sizeof(struct sockaddr_in))
2403                         return (EINVAL);
2404
2405                 if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) {
2406                         if (ssa->sin.sin_family != AF_INET ||
2407                             ssa->sin.sin_len != sizeof(struct sockaddr_in))
2408                                 return (EINVAL);
2409                 }
2410
2411                 if (gsr.gsr_interface == 0 || V_if_index < gsr.gsr_interface)
2412                         return (EADDRNOTAVAIL);
2413
2414                 ifp = ifnet_byindex(gsr.gsr_interface);
2415
2416                 if (ifp == NULL)
2417                         return (EADDRNOTAVAIL);
2418                 break;
2419
2420         default:
2421                 CTR2(KTR_IGMPV3, "%s: unknown sopt_name %d",
2422                     __func__, sopt->sopt_name);
2423                 return (EOPNOTSUPP);
2424                 break;
2425         }
2426
2427         if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2428                 return (EINVAL);
2429
2430         /*
2431          * Find the membership in the membership array.
2432          */
2433         imo = inp_findmoptions(inp);
2434         idx = imo_match_group(imo, ifp, &gsa->sa);
2435         if (idx == -1) {
2436                 error = EADDRNOTAVAIL;
2437                 goto out_inp_locked;
2438         }
2439         inm = imo->imo_membership[idx];
2440         imf = &imo->imo_mfilters[idx];
2441
2442         if (ssa->ss.ss_family != AF_UNSPEC)
2443                 is_final = 0;
2444
2445         /*
2446          * Begin state merge transaction at socket layer.
2447          */
2448         INP_WLOCK_ASSERT(inp);
2449
2450         /*
2451          * If we were instructed only to leave a given source, do so.
2452          * MCAST_LEAVE_SOURCE_GROUP is only valid for inclusive memberships.
2453          */
2454         if (is_final) {
2455                 imf_leave(imf);
2456         } else {
2457                 if (imf->imf_st[0] == MCAST_EXCLUDE) {
2458                         error = EADDRNOTAVAIL;
2459                         goto out_inp_locked;
2460                 }
2461                 ims = imo_match_source(imo, idx, &ssa->sa);
2462                 if (ims == NULL) {
2463                         CTR3(KTR_IGMPV3, "%s: source 0x%08x %spresent",
2464                             __func__, ntohl(ssa->sin.sin_addr.s_addr), "not ");
2465                         error = EADDRNOTAVAIL;
2466                         goto out_inp_locked;
2467                 }
2468                 CTR2(KTR_IGMPV3, "%s: %s source", __func__, "block");
2469                 error = imf_prune(imf, &ssa->sin);
2470                 if (error) {
2471                         CTR1(KTR_IGMPV3, "%s: merge imf state failed",
2472                             __func__);
2473                         goto out_inp_locked;
2474                 }
2475         }
2476
2477         /*
2478          * Begin state merge transaction at IGMP layer.
2479          */
2480         in_pcbref(inp);
2481         INP_WUNLOCK(inp);
2482         IN_MULTI_LOCK();
2483
2484         if (is_final) {
2485                 /*
2486                  * Give up the multicast address record to which
2487                  * the membership points.
2488                  */
2489                 (void)in_leavegroup_locked(inm, imf);
2490         } else {
2491                 CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2492                 IN_MULTI_LIST_LOCK();
2493                 error = inm_merge(inm, imf);
2494                 if (error) {
2495                         CTR1(KTR_IGMPV3, "%s: failed to merge inm state",
2496                             __func__);
2497                         goto out_in_multi_locked;
2498                 }
2499
2500                 CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2501                 error = igmp_change_state(inm);
2502                 IN_MULTI_LIST_UNLOCK();
2503                 if (error) {
2504                         CTR1(KTR_IGMPV3, "%s: failed igmp downcall",
2505                             __func__);
2506                 }
2507         }
2508
2509 out_in_multi_locked:
2510
2511         IN_MULTI_UNLOCK();
2512         INP_WLOCK(inp);
2513         if (in_pcbrele_wlocked(inp))
2514                 return (ENXIO);
2515
2516         if (error)
2517                 imf_rollback(imf);
2518         else
2519                 imf_commit(imf);
2520
2521         imf_reap(imf);
2522
2523         if (is_final) {
2524                 /* Remove the gap in the membership and filter array. */
2525                 for (++idx; idx < imo->imo_num_memberships; ++idx) {
2526                         imo->imo_membership[idx-1] = imo->imo_membership[idx];
2527                         imo->imo_mfilters[idx-1] = imo->imo_mfilters[idx];
2528                 }
2529                 imo->imo_num_memberships--;
2530         }
2531
2532 out_inp_locked:
2533         INP_WUNLOCK(inp);
2534         return (error);
2535 }
2536
2537 /*
2538  * Select the interface for transmitting IPv4 multicast datagrams.
2539  *
2540  * Either an instance of struct in_addr or an instance of struct ip_mreqn
2541  * may be passed to this socket option. An address of INADDR_ANY or an
2542  * interface index of 0 is used to remove a previous selection.
2543  * When no interface is selected, one is chosen for every send.
2544  */
2545 static int
2546 inp_set_multicast_if(struct inpcb *inp, struct sockopt *sopt)
2547 {
2548         struct in_addr           addr;
2549         struct ip_mreqn          mreqn;
2550         struct ifnet            *ifp;
2551         struct ip_moptions      *imo;
2552         int                      error;
2553
2554         if (sopt->sopt_valsize == sizeof(struct ip_mreqn)) {
2555                 /*
2556                  * An interface index was specified using the
2557                  * Linux-derived ip_mreqn structure.
2558                  */
2559                 error = sooptcopyin(sopt, &mreqn, sizeof(struct ip_mreqn),
2560                     sizeof(struct ip_mreqn));
2561                 if (error)
2562                         return (error);
2563
2564                 if (mreqn.imr_ifindex < 0 || V_if_index < mreqn.imr_ifindex)
2565                         return (EINVAL);
2566
2567                 if (mreqn.imr_ifindex == 0) {
2568                         ifp = NULL;
2569                 } else {
2570                         ifp = ifnet_byindex(mreqn.imr_ifindex);
2571                         if (ifp == NULL)
2572                                 return (EADDRNOTAVAIL);
2573                 }
2574         } else {
2575                 /*
2576                  * An interface was specified by IPv4 address.
2577                  * This is the traditional BSD usage.
2578                  */
2579                 error = sooptcopyin(sopt, &addr, sizeof(struct in_addr),
2580                     sizeof(struct in_addr));
2581                 if (error)
2582                         return (error);
2583                 if (in_nullhost(addr)) {
2584                         ifp = NULL;
2585                 } else {
2586                         INADDR_TO_IFP(addr, ifp);
2587                         if (ifp == NULL)
2588                                 return (EADDRNOTAVAIL);
2589                 }
2590                 CTR3(KTR_IGMPV3, "%s: ifp = %p, addr = 0x%08x", __func__, ifp,
2591                     ntohl(addr.s_addr));
2592         }
2593
2594         /* Reject interfaces which do not support multicast. */
2595         if (ifp != NULL && (ifp->if_flags & IFF_MULTICAST) == 0)
2596                 return (EOPNOTSUPP);
2597
2598         imo = inp_findmoptions(inp);
2599         imo->imo_multicast_ifp = ifp;
2600         imo->imo_multicast_addr.s_addr = INADDR_ANY;
2601         INP_WUNLOCK(inp);
2602
2603         return (0);
2604 }
2605
2606 /*
2607  * Atomically set source filters on a socket for an IPv4 multicast group.
2608  *
2609  * SMPng: NOTE: Potentially calls malloc(M_WAITOK) with Giant held.
2610  */
2611 static int
2612 inp_set_source_filters(struct inpcb *inp, struct sockopt *sopt)
2613 {
2614         struct __msfilterreq     msfr;
2615         sockunion_t             *gsa;
2616         struct ifnet            *ifp;
2617         struct in_mfilter       *imf;
2618         struct ip_moptions      *imo;
2619         struct in_multi         *inm;
2620         size_t                   idx;
2621         int                      error;
2622
2623         error = sooptcopyin(sopt, &msfr, sizeof(struct __msfilterreq),
2624             sizeof(struct __msfilterreq));
2625         if (error)
2626                 return (error);
2627
2628         if (msfr.msfr_nsrcs > in_mcast_maxsocksrc)
2629                 return (ENOBUFS);
2630
2631         if ((msfr.msfr_fmode != MCAST_EXCLUDE &&
2632              msfr.msfr_fmode != MCAST_INCLUDE))
2633                 return (EINVAL);
2634
2635         if (msfr.msfr_group.ss_family != AF_INET ||
2636             msfr.msfr_group.ss_len != sizeof(struct sockaddr_in))
2637                 return (EINVAL);
2638
2639         gsa = (sockunion_t *)&msfr.msfr_group;
2640         if (!IN_MULTICAST(ntohl(gsa->sin.sin_addr.s_addr)))
2641                 return (EINVAL);
2642
2643         gsa->sin.sin_port = 0;  /* ignore port */
2644
2645         if (msfr.msfr_ifindex == 0 || V_if_index < msfr.msfr_ifindex)
2646                 return (EADDRNOTAVAIL);
2647
2648         ifp = ifnet_byindex(msfr.msfr_ifindex);
2649         if (ifp == NULL)
2650                 return (EADDRNOTAVAIL);
2651
2652         /*
2653          * Take the INP write lock.
2654          * Check if this socket is a member of this group.
2655          */
2656         imo = inp_findmoptions(inp);
2657         idx = imo_match_group(imo, ifp, &gsa->sa);
2658         if (idx == -1 || imo->imo_mfilters == NULL) {
2659                 error = EADDRNOTAVAIL;
2660                 goto out_inp_locked;
2661         }
2662         inm = imo->imo_membership[idx];
2663         imf = &imo->imo_mfilters[idx];
2664
2665         /*
2666          * Begin state merge transaction at socket layer.
2667          */
2668         INP_WLOCK_ASSERT(inp);
2669
2670         imf->imf_st[1] = msfr.msfr_fmode;
2671
2672         /*
2673          * Apply any new source filters, if present.
2674          * Make a copy of the user-space source vector so
2675          * that we may copy them with a single copyin. This
2676          * allows us to deal with page faults up-front.
2677          */
2678         if (msfr.msfr_nsrcs > 0) {
2679                 struct in_msource       *lims;
2680                 struct sockaddr_in      *psin;
2681                 struct sockaddr_storage *kss, *pkss;
2682                 int                      i;
2683
2684                 INP_WUNLOCK(inp);
2685  
2686                 CTR2(KTR_IGMPV3, "%s: loading %lu source list entries",
2687                     __func__, (unsigned long)msfr.msfr_nsrcs);
2688                 kss = malloc(sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs,
2689                     M_TEMP, M_WAITOK);
2690                 error = copyin(msfr.msfr_srcs, kss,
2691                     sizeof(struct sockaddr_storage) * msfr.msfr_nsrcs);
2692                 if (error) {
2693                         free(kss, M_TEMP);
2694                         return (error);
2695                 }
2696
2697                 INP_WLOCK(inp);
2698
2699                 /*
2700                  * Mark all source filters as UNDEFINED at t1.
2701                  * Restore new group filter mode, as imf_leave()
2702                  * will set it to INCLUDE.
2703                  */
2704                 imf_leave(imf);
2705                 imf->imf_st[1] = msfr.msfr_fmode;
2706
2707                 /*
2708                  * Update socket layer filters at t1, lazy-allocating
2709                  * new entries. This saves a bunch of memory at the
2710                  * cost of one RB_FIND() per source entry; duplicate
2711                  * entries in the msfr_nsrcs vector are ignored.
2712                  * If we encounter an error, rollback transaction.
2713                  *
2714                  * XXX This too could be replaced with a set-symmetric
2715                  * difference like loop to avoid walking from root
2716                  * every time, as the key space is common.
2717                  */
2718                 for (i = 0, pkss = kss; i < msfr.msfr_nsrcs; i++, pkss++) {
2719                         psin = (struct sockaddr_in *)pkss;
2720                         if (psin->sin_family != AF_INET) {
2721                                 error = EAFNOSUPPORT;
2722                                 break;
2723                         }
2724                         if (psin->sin_len != sizeof(struct sockaddr_in)) {
2725                                 error = EINVAL;
2726                                 break;
2727                         }
2728                         error = imf_get_source(imf, psin, &lims);
2729                         if (error)
2730                                 break;
2731                         lims->imsl_st[1] = imf->imf_st[1];
2732                 }
2733                 free(kss, M_TEMP);
2734         }
2735
2736         if (error)
2737                 goto out_imf_rollback;
2738
2739         INP_WLOCK_ASSERT(inp);
2740         IN_MULTI_LOCK();
2741         IN_MULTI_LIST_LOCK();
2742
2743         /*
2744          * Begin state merge transaction at IGMP layer.
2745          */
2746         CTR1(KTR_IGMPV3, "%s: merge inm state", __func__);
2747         error = inm_merge(inm, imf);
2748         if (error) {
2749                 CTR1(KTR_IGMPV3, "%s: failed to merge inm state", __func__);
2750                 IN_MULTI_LIST_UNLOCK();
2751                 goto out_in_multi_locked;
2752         }
2753
2754         CTR1(KTR_IGMPV3, "%s: doing igmp downcall", __func__);
2755         error = igmp_change_state(inm);
2756         IN_MULTI_LIST_UNLOCK();
2757         if (error)
2758                 CTR1(KTR_IGMPV3, "%s: failed igmp downcall", __func__);
2759
2760 out_in_multi_locked:
2761
2762         IN_MULTI_UNLOCK();
2763
2764 out_imf_rollback:
2765         if (error)
2766                 imf_rollback(imf);
2767         else
2768                 imf_commit(imf);
2769
2770         imf_reap(imf);
2771
2772 out_inp_locked:
2773         INP_WUNLOCK(inp);
2774         return (error);
2775 }
2776
2777 /*
2778  * Set the IP multicast options in response to user setsockopt().
2779  *
2780  * Many of the socket options handled in this function duplicate the
2781  * functionality of socket options in the regular unicast API. However,
2782  * it is not possible to merge the duplicate code, because the idempotence
2783  * of the IPv4 multicast part of the BSD Sockets API must be preserved;
2784  * the effects of these options must be treated as separate and distinct.
2785  *
2786  * SMPng: XXX: Unlocked read of inp_socket believed OK.
2787  * FUTURE: The IP_MULTICAST_VIF option may be eliminated if MROUTING
2788  * is refactored to no longer use vifs.
2789  */
2790 int
2791 inp_setmoptions(struct inpcb *inp, struct sockopt *sopt)
2792 {
2793         struct ip_moptions      *imo;
2794         int                      error;
2795
2796         error = 0;
2797
2798         /*
2799          * If socket is neither of type SOCK_RAW or SOCK_DGRAM,
2800          * or is a divert socket, reject it.
2801          */
2802         if (inp->inp_socket->so_proto->pr_protocol == IPPROTO_DIVERT ||
2803             (inp->inp_socket->so_proto->pr_type != SOCK_RAW &&
2804              inp->inp_socket->so_proto->pr_type != SOCK_DGRAM))
2805                 return (EOPNOTSUPP);
2806
2807         switch (sopt->sopt_name) {
2808         case IP_MULTICAST_VIF: {
2809                 int vifi;
2810                 /*
2811                  * Select a multicast VIF for transmission.
2812                  * Only useful if multicast forwarding is active.
2813                  */
2814                 if (legal_vif_num == NULL) {
2815                         error = EOPNOTSUPP;
2816                         break;
2817                 }
2818                 error = sooptcopyin(sopt, &vifi, sizeof(int), sizeof(int));
2819                 if (error)
2820                         break;
2821                 if (!legal_vif_num(vifi) && (vifi != -1)) {
2822                         error = EINVAL;
2823                         break;
2824                 }
2825                 imo = inp_findmoptions(inp);
2826                 imo->imo_multicast_vif = vifi;
2827                 INP_WUNLOCK(inp);
2828                 break;
2829         }
2830
2831         case IP_MULTICAST_IF:
2832                 error = inp_set_multicast_if(inp, sopt);
2833                 break;
2834
2835         case IP_MULTICAST_TTL: {
2836                 u_char ttl;
2837
2838                 /*
2839                  * Set the IP time-to-live for outgoing multicast packets.
2840                  * The original multicast API required a char argument,
2841                  * which is inconsistent with the rest of the socket API.
2842                  * We allow either a char or an int.
2843                  */
2844                 if (sopt->sopt_valsize == sizeof(u_char)) {
2845                         error = sooptcopyin(sopt, &ttl, sizeof(u_char),
2846                             sizeof(u_char));
2847                         if (error)
2848                                 break;
2849                 } else {
2850                         u_int ittl;
2851
2852                         error = sooptcopyin(sopt, &ittl, sizeof(u_int),
2853                             sizeof(u_int));
2854                         if (error)
2855                                 break;
2856                         if (ittl > 255) {
2857                                 error = EINVAL;
2858                                 break;
2859                         }
2860                         ttl = (u_char)ittl;
2861                 }
2862                 imo = inp_findmoptions(inp);
2863                 imo->imo_multicast_ttl = ttl;
2864                 INP_WUNLOCK(inp);
2865                 break;
2866         }
2867
2868         case IP_MULTICAST_LOOP: {
2869                 u_char loop;
2870
2871                 /*
2872                  * Set the loopback flag for outgoing multicast packets.
2873                  * Must be zero or one.  The original multicast API required a
2874                  * char argument, which is inconsistent with the rest
2875                  * of the socket API.  We allow either a char or an int.
2876                  */
2877                 if (sopt->sopt_valsize == sizeof(u_char)) {
2878                         error = sooptcopyin(sopt, &loop, sizeof(u_char),
2879                             sizeof(u_char));
2880                         if (error)
2881                                 break;
2882                 } else {
2883                         u_int iloop;
2884
2885                         error = sooptcopyin(sopt, &iloop, sizeof(u_int),
2886                                             sizeof(u_int));
2887                         if (error)
2888                                 break;
2889                         loop = (u_char)iloop;
2890                 }
2891                 imo = inp_findmoptions(inp);
2892                 imo->imo_multicast_loop = !!loop;
2893                 INP_WUNLOCK(inp);
2894                 break;
2895         }
2896
2897         case IP_ADD_MEMBERSHIP:
2898         case IP_ADD_SOURCE_MEMBERSHIP:
2899         case MCAST_JOIN_GROUP:
2900         case MCAST_JOIN_SOURCE_GROUP:
2901                 error = inp_join_group(inp, sopt);
2902                 break;
2903
2904         case IP_DROP_MEMBERSHIP:
2905         case IP_DROP_SOURCE_MEMBERSHIP:
2906         case MCAST_LEAVE_GROUP:
2907         case MCAST_LEAVE_SOURCE_GROUP:
2908                 error = inp_leave_group(inp, sopt);
2909                 break;
2910
2911         case IP_BLOCK_SOURCE:
2912         case IP_UNBLOCK_SOURCE:
2913         case MCAST_BLOCK_SOURCE:
2914         case MCAST_UNBLOCK_SOURCE:
2915                 error = inp_block_unblock_source(inp, sopt);
2916                 break;
2917
2918         case IP_MSFILTER:
2919                 error = inp_set_source_filters(inp, sopt);
2920                 break;
2921
2922         default:
2923                 error = EOPNOTSUPP;
2924                 break;
2925         }
2926
2927         INP_UNLOCK_ASSERT(inp);
2928
2929         return (error);
2930 }
2931
2932 /*
2933  * Expose IGMP's multicast filter mode and source list(s) to userland,
2934  * keyed by (ifindex, group).
2935  * The filter mode is written out as a uint32_t, followed by
2936  * 0..n of struct in_addr.
2937  * For use by ifmcstat(8).
2938  * SMPng: NOTE: unlocked read of ifindex space.
2939  */
2940 static int
2941 sysctl_ip_mcast_filters(SYSCTL_HANDLER_ARGS)
2942 {
2943         struct in_addr                   src, group;
2944         struct ifnet                    *ifp;
2945         struct ifmultiaddr              *ifma;
2946         struct in_multi                 *inm;
2947         struct ip_msource               *ims;
2948         int                             *name;
2949         int                              retval;
2950         u_int                            namelen;
2951         uint32_t                         fmode, ifindex;
2952
2953         name = (int *)arg1;
2954         namelen = arg2;
2955
2956         if (req->newptr != NULL)
2957                 return (EPERM);
2958
2959         if (namelen != 2)
2960                 return (EINVAL);
2961
2962         ifindex = name[0];
2963         if (ifindex <= 0 || ifindex > V_if_index) {
2964                 CTR2(KTR_IGMPV3, "%s: ifindex %u out of range",
2965                     __func__, ifindex);
2966                 return (ENOENT);
2967         }
2968
2969         group.s_addr = name[1];
2970         if (!IN_MULTICAST(ntohl(group.s_addr))) {
2971                 CTR2(KTR_IGMPV3, "%s: group 0x%08x is not multicast",
2972                     __func__, ntohl(group.s_addr));
2973                 return (EINVAL);
2974         }
2975
2976         ifp = ifnet_byindex(ifindex);
2977         if (ifp == NULL) {
2978                 CTR2(KTR_IGMPV3, "%s: no ifp for ifindex %u",
2979                     __func__, ifindex);
2980                 return (ENOENT);
2981         }
2982
2983         retval = sysctl_wire_old_buffer(req,
2984             sizeof(uint32_t) + (in_mcast_maxgrpsrc * sizeof(struct in_addr)));
2985         if (retval)
2986                 return (retval);
2987
2988         IN_MULTI_LIST_LOCK();
2989
2990         IF_ADDR_RLOCK(ifp);
2991         CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2992                 if (ifma->ifma_addr->sa_family != AF_INET ||
2993                     ifma->ifma_protospec == NULL)
2994                         continue;
2995                 inm = (struct in_multi *)ifma->ifma_protospec;
2996                 if (!in_hosteq(inm->inm_addr, group))
2997                         continue;
2998                 fmode = inm->inm_st[1].iss_fmode;
2999                 retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t));
3000                 if (retval != 0)
3001                         break;
3002                 RB_FOREACH(ims, ip_msource_tree, &inm->inm_srcs) {
3003                         CTR2(KTR_IGMPV3, "%s: visit node 0x%08x", __func__,
3004                             ims->ims_haddr);
3005                         /*
3006                          * Only copy-out sources which are in-mode.
3007                          */
3008                         if (fmode != ims_get_mode(inm, ims, 1)) {
3009                                 CTR1(KTR_IGMPV3, "%s: skip non-in-mode",
3010                                     __func__);
3011                                 continue;
3012                         }
3013                         src.s_addr = htonl(ims->ims_haddr);
3014                         retval = SYSCTL_OUT(req, &src, sizeof(struct in_addr));
3015                         if (retval != 0)
3016                                 break;
3017                 }
3018         }
3019         IF_ADDR_RUNLOCK(ifp);
3020
3021         IN_MULTI_LIST_UNLOCK();
3022
3023         return (retval);
3024 }
3025
3026 #if defined(KTR) && (KTR_COMPILE & KTR_IGMPV3)
3027
3028 static const char *inm_modestrs[] = { "un", "in", "ex" };
3029
3030 static const char *
3031 inm_mode_str(const int mode)
3032 {
3033
3034         if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE)
3035                 return (inm_modestrs[mode]);
3036         return ("??");
3037 }
3038
3039 static const char *inm_statestrs[] = {
3040         "not-member",
3041         "silent",
3042         "idle",
3043         "lazy",
3044         "sleeping",
3045         "awakening",
3046         "query-pending",
3047         "sg-query-pending",
3048         "leaving"
3049 };
3050
3051 static const char *
3052 inm_state_str(const int state)
3053 {
3054
3055         if (state >= IGMP_NOT_MEMBER && state <= IGMP_LEAVING_MEMBER)
3056                 return (inm_statestrs[state]);
3057         return ("??");
3058 }
3059
3060 /*
3061  * Dump an in_multi structure to the console.
3062  */
3063 void
3064 inm_print(const struct in_multi *inm)
3065 {
3066         int t;
3067         char addrbuf[INET_ADDRSTRLEN];
3068
3069         if ((ktr_mask & KTR_IGMPV3) == 0)
3070                 return;
3071
3072         printf("%s: --- begin inm %p ---\n", __func__, inm);
3073         printf("addr %s ifp %p(%s) ifma %p\n",
3074             inet_ntoa_r(inm->inm_addr, addrbuf),
3075             inm->inm_ifp,
3076             inm->inm_ifp->if_xname,
3077             inm->inm_ifma);
3078         printf("timer %u state %s refcount %u scq.len %u\n",
3079             inm->inm_timer,
3080             inm_state_str(inm->inm_state),
3081             inm->inm_refcount,
3082             inm->inm_scq.mq_len);
3083         printf("igi %p nsrc %lu sctimer %u scrv %u\n",
3084             inm->inm_igi,
3085             inm->inm_nsrc,
3086             inm->inm_sctimer,
3087             inm->inm_scrv);
3088         for (t = 0; t < 2; t++) {
3089                 printf("t%d: fmode %s asm %u ex %u in %u rec %u\n", t,
3090                     inm_mode_str(inm->inm_st[t].iss_fmode),
3091                     inm->inm_st[t].iss_asm,
3092                     inm->inm_st[t].iss_ex,
3093                     inm->inm_st[t].iss_in,
3094                     inm->inm_st[t].iss_rec);
3095         }
3096         printf("%s: --- end inm %p ---\n", __func__, inm);
3097 }
3098
3099 #else /* !KTR || !(KTR_COMPILE & KTR_IGMPV3) */
3100
3101 void
3102 inm_print(const struct in_multi *inm)
3103 {
3104
3105 }
3106
3107 #endif /* KTR && (KTR_COMPILE & KTR_IGMPV3) */
3108
3109 RB_GENERATE(ip_msource_tree, ip_msource, ims_link, ip_msource_cmp);