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