X-Git-Url: https://git.saurik.com/apple/xnu.git/blobdiff_plain/39236c6e673c41db228275375ab7fdb0f837b292..f427ee49d309d8fc33ebf3042c3a775f2f530ded:/bsd/netinet6/in6_mcast.c diff --git a/bsd/netinet6/in6_mcast.c b/bsd/netinet6/in6_mcast.c index a76ce4fe0..0f6330fa2 100644 --- a/bsd/netinet6/in6_mcast.c +++ b/bsd/netinet6/in6_mcast.c @@ -1,8 +1,8 @@ /* - * Copyright (c) 2010-2013 Apple Inc. All rights reserved. + * Copyright (c) 2010-2020 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ - * + * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in @@ -11,10 +11,10 @@ * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. - * + * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. - * + * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, @@ -22,7 +22,7 @@ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. - * + * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* @@ -80,6 +80,7 @@ #include #include +#include #include #include @@ -96,71 +97,60 @@ #include #include -#ifndef __SOCKUNION_DECLARED -union sockunion { - struct sockaddr_storage ss; - struct sockaddr sa; - struct sockaddr_dl sdl; - struct sockaddr_in6 sin6; -}; -typedef union sockunion sockunion_t; -#define __SOCKUNION_DECLARED -#endif /* __SOCKUNION_DECLARED */ - -static void im6f_commit(struct in6_mfilter *); -static int im6f_get_source(struct in6_mfilter *imf, - const struct sockaddr_in6 *psin, - struct in6_msource **); +static void im6f_commit(struct in6_mfilter *); +static int im6f_get_source(struct in6_mfilter *imf, + const struct sockaddr_in6 *psin, + struct in6_msource **); static struct in6_msource * - im6f_graft(struct in6_mfilter *, const uint8_t, - const struct sockaddr_in6 *); -static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *); -static void im6f_rollback(struct in6_mfilter *); -static void im6f_reap(struct in6_mfilter *); -static int im6o_grow(struct ip6_moptions *, size_t); -static size_t im6o_match_group(const struct ip6_moptions *, - const struct ifnet *, const struct sockaddr *); +im6f_graft(struct in6_mfilter *, const uint8_t, + const struct sockaddr_in6 *); +static int im6f_prune(struct in6_mfilter *, const struct sockaddr_in6 *); +static void im6f_rollback(struct in6_mfilter *); +static void im6f_reap(struct in6_mfilter *); +static int im6o_grow(struct ip6_moptions *); +static size_t im6o_match_group(const struct ip6_moptions *, + const struct ifnet *, const struct sockaddr_in6 *); static struct in6_msource * - im6o_match_source(const struct ip6_moptions *, const size_t, - const struct sockaddr *); -static void im6s_merge(struct ip6_msource *ims, - const struct in6_msource *lims, const int rollback); -static int in6_mc_get(struct ifnet *, const struct in6_addr *, - struct in6_multi **); -static int in6m_get_source(struct in6_multi *inm, - const struct in6_addr *addr, const int noalloc, - struct ip6_msource **pims); -static int in6m_is_ifp_detached(const struct in6_multi *); -static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *); -static void in6m_reap(struct in6_multi *); +im6o_match_source(const struct ip6_moptions *, + const size_t, const struct sockaddr_in6 *); +static void im6s_merge(struct ip6_msource *ims, + const struct in6_msource *lims, const int rollback); +static int in6_mc_get(struct ifnet *, const struct in6_addr *, + struct in6_multi **); +static int in6m_get_source(struct in6_multi *inm, + const struct in6_addr *addr, const int noalloc, + struct ip6_msource **pims); +static int in6m_is_ifp_detached(const struct in6_multi *); +static int in6m_merge(struct in6_multi *, /*const*/ struct in6_mfilter *); +static void in6m_reap(struct in6_multi *); static struct ip6_moptions * - in6p_findmoptions(struct inpcb *); -static int in6p_get_source_filters(struct inpcb *, struct sockopt *); -static int in6p_lookup_v4addr(struct ipv6_mreq *, struct ip_mreq *); -static int in6p_join_group(struct inpcb *, struct sockopt *); -static int in6p_leave_group(struct inpcb *, struct sockopt *); +in6p_findmoptions(struct inpcb *); +static int in6p_get_source_filters(struct inpcb *, struct sockopt *); +static int in6p_lookup_v4addr(struct ipv6_mreq *, struct ip_mreq *); +static int in6p_join_group(struct inpcb *, struct sockopt *); +static int in6p_leave_group(struct inpcb *, struct sockopt *); static struct ifnet * - in6p_lookup_mcast_ifp(const struct inpcb *, - const struct sockaddr_in6 *); -static int in6p_block_unblock_source(struct inpcb *, struct sockopt *); -static int in6p_set_multicast_if(struct inpcb *, struct sockopt *); -static int in6p_set_source_filters(struct inpcb *, struct sockopt *); -static int sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS; +in6p_lookup_mcast_ifp(const struct inpcb *, + const struct sockaddr_in6 *); +static int in6p_block_unblock_source(struct inpcb *, struct sockopt *); +static int in6p_set_multicast_if(struct inpcb *, struct sockopt *); +static int in6p_set_source_filters(struct inpcb *, struct sockopt *); +static int sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS; static __inline__ int ip6_msource_cmp(const struct ip6_msource *, - const struct ip6_msource *); + const struct ip6_msource *); -SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */ +SYSCTL_DECL(_net_inet6_ip6); /* XXX Not in any common header. */ SYSCTL_NODE(_net_inet6_ip6, OID_AUTO, mcast, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "IPv6 multicast"); static unsigned long in6_mcast_maxgrpsrc = IPV6_MAX_GROUP_SRC_FILTER; SYSCTL_LONG(_net_inet6_ip6_mcast, OID_AUTO, maxgrpsrc, - CTLFLAG_RW | CTLFLAG_LOCKED, &in6_mcast_maxgrpsrc, + CTLFLAG_RW | CTLFLAG_LOCKED, &in6_mcast_maxgrpsrc, "Max source filters per group"); static unsigned long in6_mcast_maxsocksrc = IPV6_MAX_SOCK_SRC_FILTER; SYSCTL_LONG(_net_inet6_ip6_mcast, OID_AUTO, maxsocksrc, - CTLFLAG_RW | CTLFLAG_LOCKED, &in6_mcast_maxsocksrc, + CTLFLAG_RW | CTLFLAG_LOCKED, &in6_mcast_maxsocksrc, "Max source filters per socket"); int in6_mcast_loop = IPV6_DEFAULT_MULTICAST_LOOP; @@ -173,20 +163,20 @@ SYSCTL_NODE(_net_inet6_ip6_mcast, OID_AUTO, filters, RB_GENERATE_PREV(ip6_msource_tree, ip6_msource, im6s_link, ip6_msource_cmp); -#define IN6M_TRACE_HIST_SIZE 32 /* size of trace history */ +#define IN6M_TRACE_HIST_SIZE 32 /* size of trace history */ /* For gdb */ __private_extern__ unsigned int in6m_trace_hist_size = IN6M_TRACE_HIST_SIZE; struct in6_multi_dbg { - struct in6_multi in6m; /* in6_multi */ - u_int16_t in6m_refhold_cnt; /* # of ref */ - u_int16_t in6m_refrele_cnt; /* # of rele */ + struct in6_multi in6m; /* in6_multi */ + u_int16_t in6m_refhold_cnt; /* # of ref */ + u_int16_t in6m_refrele_cnt; /* # of rele */ /* * Circular lists of in6m_addref and in6m_remref callers. */ - ctrace_t in6m_refhold[IN6M_TRACE_HIST_SIZE]; - ctrace_t in6m_refrele[IN6M_TRACE_HIST_SIZE]; + ctrace_t in6m_refhold[IN6M_TRACE_HIST_SIZE]; + ctrace_t in6m_refrele[IN6M_TRACE_HIST_SIZE]; /* * Trash list linkage */ @@ -198,52 +188,40 @@ static TAILQ_HEAD(, in6_multi_dbg) in6m_trash_head; static decl_lck_mtx_data(, in6m_trash_lock); #if DEBUG -static unsigned int in6m_debug = 1; /* debugging (enabled) */ +static unsigned int in6m_debug = 1; /* debugging (enabled) */ #else -static unsigned int in6m_debug; /* debugging (disabled) */ +static unsigned int in6m_debug; /* debugging (disabled) */ #endif /* !DEBUG */ -static unsigned int in6m_size; /* size of zone element */ -static struct zone *in6m_zone; /* zone for in6_multi */ - -#define IN6M_ZONE_MAX 64 /* maximum elements in zone */ -#define IN6M_ZONE_NAME "in6_multi" /* zone name */ - -static unsigned int imm_size; /* size of zone element */ -static struct zone *imm_zone; /* zone for in6_multi_mship */ - -#define IMM_ZONE_MAX 64 /* maximum elements in zone */ -#define IMM_ZONE_NAME "in6_multi_mship" /* zone name */ +static struct zone *in6m_zone; /* zone for in6_multi */ +#define IN6M_ZONE_NAME "in6_multi" /* zone name */ -#define IP6MS_ZONE_MAX 64 /* maximum elements in zone */ -#define IP6MS_ZONE_NAME "ip6_msource" /* zone name */ +static ZONE_DECLARE(imm_zone, "in6_multi_mship", + sizeof(struct in6_multi_mship), ZC_ZFREE_CLEARMEM); -static unsigned int ip6ms_size; /* size of zone element */ -static struct zone *ip6ms_zone; /* zone for ip6_msource */ +static ZONE_DECLARE(ip6ms_zone, "ip6_msource", + sizeof(struct ip6_msource), ZC_ZFREE_CLEARMEM); -#define IN6MS_ZONE_MAX 64 /* maximum elements in zone */ -#define IN6MS_ZONE_NAME "in6_msource" /* zone name */ - -static unsigned int in6ms_size; /* size of zone element */ -static struct zone *in6ms_zone; /* zone for in6_msource */ +static ZONE_DECLARE(in6ms_zone, "in6_msource", + sizeof(struct in6_msource), ZC_ZFREE_CLEARMEM); /* Lock group and attribute for in6_multihead_lock lock */ -static lck_attr_t *in6_multihead_lock_attr; -static lck_grp_t *in6_multihead_lock_grp; -static lck_grp_attr_t *in6_multihead_lock_grp_attr; +static lck_attr_t *in6_multihead_lock_attr; +static lck_grp_t *in6_multihead_lock_grp; +static lck_grp_attr_t *in6_multihead_lock_grp_attr; static decl_lck_rw_data(, in6_multihead_lock); struct in6_multihead in6_multihead; -static struct in6_multi *in6_multi_alloc(int); +static struct in6_multi *in6_multi_alloc(zalloc_flags_t); static void in6_multi_free(struct in6_multi *); static void in6_multi_attach(struct in6_multi *); -static struct in6_multi_mship *in6_multi_mship_alloc(int); +static struct in6_multi_mship *in6_multi_mship_alloc(zalloc_flags_t); static void in6_multi_mship_free(struct in6_multi_mship *); static void in6m_trace(struct in6_multi *, int); -static struct ip6_msource *ip6ms_alloc(int); +static struct ip6_msource *ip6ms_alloc(zalloc_flags_t); static void ip6ms_free(struct ip6_msource *); -static struct in6_msource *in6ms_alloc(int); +static struct in6_msource *in6ms_alloc(zalloc_flags_t); static void in6ms_free(struct in6_msource *); /* @@ -255,7 +233,7 @@ static void in6ms_free(struct in6_msource *); static __inline int ip6_msource_cmp(const struct ip6_msource *a, const struct ip6_msource *b) { - return (memcmp(&a->im6s_addr, &b->im6s_addr, sizeof(struct in6_addr))); + return memcmp(&a->im6s_addr, &b->im6s_addr, sizeof(struct in6_addr)); } /* @@ -267,7 +245,7 @@ in6m_is_ifp_detached(const struct in6_multi *inm) VERIFY(inm->in6m_ifma != NULL); VERIFY(inm->in6m_ifp == inm->in6m_ifma->ifma_ifp); - return (!ifnet_is_attached(inm->in6m_ifp, 0)); + return !ifnet_is_attached(inm->in6m_ifp, 0); } /* @@ -275,7 +253,7 @@ in6m_is_ifp_detached(const struct in6_multi *inm) * with an empty source filter list. */ static __inline__ void -im6f_init(struct in6_mfilter *imf, const int st0, const int st1) +im6f_init(struct in6_mfilter *imf, const uint8_t st0, const uint8_t st1) { memset(imf, 0, sizeof(struct in6_mfilter)); RB_INIT(&imf->im6f_sources); @@ -287,14 +265,15 @@ im6f_init(struct in6_mfilter *imf, const int st0, const int st1) * Resize the ip6_moptions vector to the next power-of-two minus 1. */ static int -im6o_grow(struct ip6_moptions *imo, size_t newmax) +im6o_grow(struct ip6_moptions *imo) { - struct in6_multi **nmships; - struct in6_multi **omships; - struct in6_mfilter *nmfilters; - struct in6_mfilter *omfilters; - size_t idx; - size_t oldmax; + struct in6_multi **nmships; + struct in6_multi **omships; + struct in6_mfilter *nmfilters; + struct in6_mfilter *omfilters; + size_t idx; + size_t oldmax; + size_t newmax; IM6O_LOCK_ASSERT_HELD(imo); @@ -303,33 +282,36 @@ im6o_grow(struct ip6_moptions *imo, size_t newmax) omships = imo->im6o_membership; omfilters = imo->im6o_mfilters; oldmax = imo->im6o_max_memberships; - if (newmax == 0) - newmax = ((oldmax + 1) * 2) - 1; + newmax = ((oldmax + 1) * 2) - 1; - if (newmax > IPV6_MAX_MEMBERSHIPS) - return (ETOOMANYREFS); + if (newmax > IPV6_MAX_MEMBERSHIPS) { + return ETOOMANYREFS; + } if ((nmships = (struct in6_multi **)_REALLOC(omships, - sizeof (struct in6_multi *) * newmax, M_IP6MOPTS, - M_WAITOK | M_ZERO)) == NULL) - return (ENOMEM); + sizeof(struct in6_multi *) * newmax, M_IP6MOPTS, + M_WAITOK | M_ZERO)) == NULL) { + return ENOMEM; + } imo->im6o_membership = nmships; if ((nmfilters = (struct in6_mfilter *)_REALLOC(omfilters, - sizeof (struct in6_mfilter) * newmax, M_IN6MFILTER, - M_WAITOK | M_ZERO)) == NULL) - return (ENOMEM); + sizeof(struct in6_mfilter) * newmax, M_IN6MFILTER, + M_WAITOK | M_ZERO)) == NULL) { + return ENOMEM; + } imo->im6o_mfilters = nmfilters; /* Initialize newly allocated source filter heads. */ - for (idx = oldmax; idx < newmax; idx++) + for (idx = oldmax; idx < newmax; idx++) { im6f_init(&nmfilters[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); + } - imo->im6o_max_memberships = newmax; + imo->im6o_max_memberships = (u_short)newmax; - return (0); + return 0; } /* @@ -339,26 +321,28 @@ im6o_grow(struct ip6_moptions *imo, size_t newmax) */ static size_t im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp, - const struct sockaddr *group) + const struct sockaddr_in6 *group) { const struct sockaddr_in6 *gsin6; struct in6_multi *pinm; - int idx; - int nmships; + int idx; + int nmships; IM6O_LOCK_ASSERT_HELD(__DECONST(struct ip6_moptions *, imo)); - gsin6 = (struct sockaddr_in6 *)(uintptr_t)(size_t)group; + gsin6 = group; /* The im6o_membership array may be lazy allocated. */ - if (imo->im6o_membership == NULL || imo->im6o_num_memberships == 0) - return (-1); + if (imo->im6o_membership == NULL || imo->im6o_num_memberships == 0) { + return -1; + } nmships = imo->im6o_num_memberships; for (idx = 0; idx < nmships; idx++) { pinm = imo->im6o_membership[idx]; - if (pinm == NULL) + if (pinm == NULL) { continue; + } IN6M_LOCK(pinm); if ((ifp == NULL || (pinm->in6m_ifp == ifp)) && IN6_ARE_ADDR_EQUAL(&pinm->in6m_addr, @@ -368,10 +352,11 @@ im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp, } IN6M_UNLOCK(pinm); } - if (idx >= nmships) + if (idx >= nmships) { idx = -1; + } - return (idx); + return idx; } /* @@ -387,29 +372,30 @@ im6o_match_group(const struct ip6_moptions *imo, const struct ifnet *ifp, */ static struct in6_msource * im6o_match_source(const struct ip6_moptions *imo, const size_t gidx, - const struct sockaddr *src) + const struct sockaddr_in6 *src) { - struct ip6_msource find; - struct in6_mfilter *imf; - struct ip6_msource *ims; - const sockunion_t *psa; + struct ip6_msource find; + struct in6_mfilter *imf; + struct ip6_msource *ims; + const struct sockaddr_in6 *psa; IM6O_LOCK_ASSERT_HELD(__DECONST(struct ip6_moptions *, imo)); - VERIFY(src->sa_family == AF_INET6); + VERIFY(src->sin6_family == AF_INET6); VERIFY(gidx != (size_t)-1 && gidx < imo->im6o_num_memberships); /* The im6o_mfilters array may be lazy allocated. */ - if (imo->im6o_mfilters == NULL) - return (NULL); + if (imo->im6o_mfilters == NULL) { + return NULL; + } imf = &imo->im6o_mfilters[gidx]; - psa = (sockunion_t *)(uintptr_t)(size_t)src; - find.im6s_addr = psa->sin6.sin6_addr; - in6_clearscope(&find.im6s_addr); /* XXX */ + psa = src; + find.im6s_addr = psa->sin6_addr; + in6_clearscope(&find.im6s_addr); /* XXX */ ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find); - return ((struct in6_msource *)ims); + return (struct in6_msource *)ims; } /* @@ -420,7 +406,7 @@ im6o_match_source(const struct ip6_moptions *imo, const size_t gidx, */ int im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp, - const struct sockaddr *group, const struct sockaddr *src) + const struct sockaddr_in6 *group, const struct sockaddr_in6 *src) { size_t gidx; struct in6_msource *ims; @@ -430,8 +416,9 @@ im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp, VERIFY(ifp != NULL); gidx = im6o_match_group(imo, ifp, group); - if (gidx == (size_t)-1) - return (MCAST_NOTGMEMBER); + if (gidx == (size_t)-1) { + return MCAST_NOTGMEMBER; + } /* * Check if the source was included in an (S,G) join. @@ -446,10 +433,11 @@ im6o_mc_filter(const struct ip6_moptions *imo, const struct ifnet *ifp, ims = im6o_match_source(imo, gidx, src); if ((ims == NULL && mode == MCAST_INCLUDE) || - (ims != NULL && ims->im6sl_st[0] != mode)) - return (MCAST_NOTSMEMBER); + (ims != NULL && ims->im6sl_st[0] != mode)) { + return MCAST_NOTSMEMBER; + } - return (MCAST_PASS); + return MCAST_PASS; } /* @@ -464,10 +452,10 @@ static int in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, struct in6_multi **pinm) { - struct sockaddr_in6 gsin6; - struct ifmultiaddr *ifma; - struct in6_multi *inm; - int error; + struct sockaddr_in6 gsin6; + struct ifmultiaddr *ifma; + struct in6_multi *inm; + int error; *pinm = NULL; @@ -485,7 +473,7 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, * We already joined this group; return the in6m * with a refcount held (via lookup) for caller. */ - return (0); + return 0; } in6_multihead_lock_done(); @@ -499,8 +487,9 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, * with this network-layer group on the given ifnet. */ error = if_addmulti(ifp, (struct sockaddr *)&gsin6, &ifma); - if (error != 0) - return (error); + if (error != 0) { + return error; + } /* * See comments in in6m_remref() for access to ifma_protospec. @@ -510,7 +499,7 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, if ((inm = ifma->ifma_protospec) != NULL) { VERIFY(ifma->ifma_addr != NULL); VERIFY(ifma->ifma_addr->sa_family == AF_INET6); - IN6M_ADDREF(inm); /* for caller */ + IN6M_ADDREF(inm); /* for caller */ IFMA_UNLOCK(ifma); IN6M_LOCK(inm); VERIFY(inm->in6m_ifma == ifma); @@ -530,7 +519,7 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, * been joined; return the inm with a refcount * held for caller. */ - return (0); + return 0; } /* * We lost the race with another thread doing in6_delmulti(); @@ -545,7 +534,7 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, IN6M_UNLOCK(inm); in6_multihead_lock_done(); IFMA_REMREF(ifma); - return (0); + return 0; } IFMA_UNLOCK(ifma); @@ -557,19 +546,15 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, * The initial source filter state is INCLUDE, {} as per the RFC. * Pending state-changes per group are subject to a bounds check. */ - inm = in6_multi_alloc(M_WAITOK); - if (inm == NULL) { - in6_multihead_lock_done(); - IFMA_REMREF(ifma); - return (ENOMEM); - } + inm = in6_multi_alloc(Z_WAITOK); + IN6M_LOCK(inm); inm->in6m_addr = *group; inm->in6m_ifp = ifp; inm->in6m_mli = MLD_IFINFO(ifp); VERIFY(inm->in6m_mli != NULL); MLI_ADDREF(inm->in6m_mli); - inm->in6m_ifma = ifma; /* keep refcount from if_addmulti() */ + inm->in6m_ifma = ifma; /* keep refcount from if_addmulti() */ inm->in6m_state = MLD_NOT_MEMBER; /* * Pending state-changes per group are subject to a bounds check. @@ -582,7 +567,7 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, in6_multi_attach(inm); VERIFY((inm->in6m_debug & (IFD_ATTACHED | IFD_TRASHED)) == IFD_ATTACHED); - IN6M_ADDREF_LOCKED(inm); /* for caller */ + IN6M_ADDREF_LOCKED(inm); /* for caller */ IN6M_UNLOCK(inm); IFMA_LOCK(ifma); @@ -591,7 +576,7 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, IFMA_UNLOCK(ifma); in6_multihead_lock_done(); - return (0); + return 0; } /* @@ -602,7 +587,7 @@ in6_mc_get(struct ifnet *ifp, const struct in6_addr *group, void in6m_clear_recorded(struct in6_multi *inm) { - struct ip6_msource *ims; + struct ip6_msource *ims; IN6M_LOCK_ASSERT_HELD(inm); @@ -640,21 +625,21 @@ in6m_clear_recorded(struct in6_multi *inm) int in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr) { - struct ip6_msource find; - struct ip6_msource *ims, *nims; + struct ip6_msource find; + struct ip6_msource *ims, *nims; IN6M_LOCK_ASSERT_HELD(inm); find.im6s_addr = *addr; ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find); - if (ims && ims->im6s_stp) - return (0); + if (ims && ims->im6s_stp) { + return 0; + } if (ims == NULL) { - if (inm->in6m_nsrc == in6_mcast_maxgrpsrc) - return (-ENOSPC); - nims = ip6ms_alloc(M_WAITOK); - if (nims == NULL) - return (-ENOMEM); + if (inm->in6m_nsrc == in6_mcast_maxgrpsrc) { + return -ENOSPC; + } + nims = ip6ms_alloc(Z_WAITOK); nims->im6s_addr = find.im6s_addr; RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims); ++inm->in6m_nsrc; @@ -668,7 +653,7 @@ in6m_record_source(struct in6_multi *inm, const struct in6_addr *addr) ++ims->im6s_stp; ++inm->in6m_st[1].iss_rec; - return (1); + return 1; } /* @@ -686,10 +671,10 @@ static int im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin, struct in6_msource **plims) { - struct ip6_msource find; - struct ip6_msource *ims; - struct in6_msource *lims; - int error; + struct ip6_msource find; + struct ip6_msource *ims; + struct in6_msource *lims; + int error; error = 0; ims = NULL; @@ -699,11 +684,10 @@ im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin, ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find); lims = (struct in6_msource *)ims; if (lims == NULL) { - if (imf->im6f_nsrc == in6_mcast_maxsocksrc) - return (ENOSPC); - lims = in6ms_alloc(M_WAITOK); - if (lims == NULL) - return (ENOMEM); + if (imf->im6f_nsrc == in6_mcast_maxsocksrc) { + return ENOSPC; + } + lims = in6ms_alloc(Z_WAITOK); lims->im6s_addr = find.im6s_addr; lims->im6sl_st[0] = MCAST_UNDEFINED; RB_INSERT(ip6_msource_tree, &imf->im6f_sources, @@ -713,7 +697,7 @@ im6f_get_source(struct in6_mfilter *imf, const struct sockaddr_in6 *psin, *plims = lims; - return (error); + return error; } /* @@ -730,11 +714,9 @@ static struct in6_msource * im6f_graft(struct in6_mfilter *imf, const uint8_t st1, const struct sockaddr_in6 *psin) { - struct in6_msource *lims; + struct in6_msource *lims; - lims = in6ms_alloc(M_WAITOK); - if (lims == NULL) - return (NULL); + lims = in6ms_alloc(Z_WAITOK); lims->im6s_addr = psin->sin6_addr; lims->im6sl_st[0] = MCAST_UNDEFINED; lims->im6sl_st[1] = st1; @@ -742,7 +724,7 @@ im6f_graft(struct in6_mfilter *imf, const uint8_t st1, (struct ip6_msource *)lims); ++imf->im6f_nsrc; - return (lims); + return lims; } /* @@ -758,17 +740,18 @@ im6f_graft(struct in6_mfilter *imf, const uint8_t st1, static int im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin) { - struct ip6_msource find; - struct ip6_msource *ims; - struct in6_msource *lims; + struct ip6_msource find; + struct ip6_msource *ims; + struct in6_msource *lims; find.im6s_addr = psin->sin6_addr; ims = RB_FIND(ip6_msource_tree, &imf->im6f_sources, &find); - if (ims == NULL) - return (ENOENT); + if (ims == NULL) { + return ENOENT; + } lims = (struct in6_msource *)ims; lims->im6sl_st[1] = MCAST_UNDEFINED; - return (0); + return 0; } /* @@ -779,8 +762,8 @@ im6f_prune(struct in6_mfilter *imf, const struct sockaddr_in6 *psin) static void im6f_rollback(struct in6_mfilter *imf) { - struct ip6_msource *ims, *tims; - struct in6_msource *lims; + struct ip6_msource *ims, *tims; + struct in6_msource *lims; RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) { lims = (struct in6_msource *)ims; @@ -810,8 +793,8 @@ im6f_rollback(struct in6_mfilter *imf) void im6f_leave(struct in6_mfilter *imf) { - struct ip6_msource *ims; - struct in6_msource *lims; + struct ip6_msource *ims; + struct in6_msource *lims; RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) { lims = (struct in6_msource *)ims; @@ -828,8 +811,8 @@ im6f_leave(struct in6_mfilter *imf) static void im6f_commit(struct in6_mfilter *imf) { - struct ip6_msource *ims; - struct in6_msource *lims; + struct ip6_msource *ims; + struct in6_msource *lims; RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) { lims = (struct in6_msource *)ims; @@ -846,8 +829,8 @@ im6f_commit(struct in6_mfilter *imf) static void im6f_reap(struct in6_mfilter *imf) { - struct ip6_msource *ims, *tims; - struct in6_msource *lims; + struct ip6_msource *ims, *tims; + struct in6_msource *lims; RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) { lims = (struct in6_msource *)ims; @@ -870,8 +853,8 @@ im6f_reap(struct in6_mfilter *imf) void im6f_purge(struct in6_mfilter *imf) { - struct ip6_msource *ims, *tims; - struct in6_msource *lims; + struct ip6_msource *ims, *tims; + struct in6_msource *lims; RB_FOREACH_SAFE(ims, ip6_msource_tree, &imf->im6f_sources, tims) { lims = (struct in6_msource *)ims; @@ -899,19 +882,18 @@ static int in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr, const int noalloc, struct ip6_msource **pims) { - struct ip6_msource find; - struct ip6_msource *ims, *nims; + struct ip6_msource find; + struct ip6_msource *ims, *nims; IN6M_LOCK_ASSERT_HELD(inm); find.im6s_addr = *addr; ims = RB_FIND(ip6_msource_tree, &inm->in6m_srcs, &find); if (ims == NULL && !noalloc) { - if (inm->in6m_nsrc == in6_mcast_maxgrpsrc) - return (ENOSPC); - nims = ip6ms_alloc(M_WAITOK); - if (nims == NULL) - return (ENOMEM); + if (inm->in6m_nsrc == in6_mcast_maxgrpsrc) { + return ENOSPC; + } + nims = ip6ms_alloc(Z_WAITOK); nims->im6s_addr = *addr; RB_INSERT(ip6_msource_tree, &inm->in6m_srcs, nims); ++inm->in6m_nsrc; @@ -921,7 +903,7 @@ in6m_get_source(struct in6_multi *inm, const struct in6_addr *addr, } *pims = ims; - return (0); + return 0; } /* @@ -940,11 +922,12 @@ im6s_get_mode(const struct in6_multi *inm, const struct ip6_msource *ims, t = !!t; if (inm->in6m_st[t].iss_ex > 0 && - inm->in6m_st[t].iss_ex == ims->im6s_st[t].ex) - return (MCAST_EXCLUDE); - else if (ims->im6s_st[t].in > 0 && ims->im6s_st[t].ex == 0) - return (MCAST_INCLUDE); - return (MCAST_UNDEFINED); + inm->in6m_st[t].iss_ex == ims->im6s_st[t].ex) { + return MCAST_EXCLUDE; + } else if (ims->im6s_st[t].in > 0 && ims->im6s_st[t].ex == 0) { + return MCAST_INCLUDE; + } + return MCAST_UNDEFINED; } /* @@ -995,10 +978,10 @@ im6s_merge(struct ip6_msource *ims, const struct in6_msource *lims, static int in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) { - struct ip6_msource *ims, *nims; - struct in6_msource *lims; - int schanged, error; - int nsrc0, nsrc1; + struct ip6_msource *ims, *nims = NULL; + struct in6_msource *lims; + int schanged, error; + int nsrc0, nsrc1; IN6M_LOCK_ASSERT_HELD(inm); @@ -1015,13 +998,20 @@ in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) */ RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) { lims = (struct in6_msource *)ims; - if (lims->im6sl_st[0] == imf->im6f_st[0]) nsrc0++; - if (lims->im6sl_st[1] == imf->im6f_st[1]) nsrc1++; - if (lims->im6sl_st[0] == lims->im6sl_st[1]) continue; + if (lims->im6sl_st[0] == imf->im6f_st[0]) { + nsrc0++; + } + if (lims->im6sl_st[1] == imf->im6f_st[1]) { + nsrc1++; + } + if (lims->im6sl_st[0] == lims->im6sl_st[1]) { + continue; + } error = in6m_get_source(inm, &lims->im6s_addr, 0, &nims); ++schanged; - if (error) + if (error) { break; + } im6s_merge(nims, lims, 0); } if (error) { @@ -1029,11 +1019,13 @@ in6m_merge(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) RB_FOREACH_REVERSE_FROM(ims, ip6_msource_tree, nims) { lims = (struct in6_msource *)ims; - if (lims->im6sl_st[0] == lims->im6sl_st[1]) + if (lims->im6sl_st[0] == lims->im6sl_st[1]) { continue; + } (void) in6m_get_source(inm, &lims->im6s_addr, 1, &bims); - if (bims == NULL) + if (bims == NULL) { continue; + } im6s_merge(bims, lims, 1); } goto out_reap; @@ -1117,7 +1109,7 @@ out_reap: MLD_PRINTF(("%s: sources changed; reaping\n", __func__)); in6m_reap(inm); } - return (error); + return error; } /* @@ -1127,7 +1119,7 @@ out_reap: void in6m_commit(struct in6_multi *inm) { - struct ip6_msource *ims; + struct ip6_msource *ims; IN6M_LOCK_ASSERT_HELD(inm); @@ -1148,15 +1140,16 @@ in6m_commit(struct in6_multi *inm) static void in6m_reap(struct in6_multi *inm) { - struct ip6_msource *ims, *tims; + struct ip6_msource *ims, *tims; IN6M_LOCK_ASSERT_HELD(inm); RB_FOREACH_SAFE(ims, ip6_msource_tree, &inm->in6m_srcs, tims) { if (ims->im6s_st[0].ex > 0 || ims->im6s_st[0].in > 0 || ims->im6s_st[1].ex > 0 || ims->im6s_st[1].in > 0 || - ims->im6s_stp != 0) + ims->im6s_stp != 0) { continue; + } MLD_PRINTF(("%s: free ims 0x%llx\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(ims))); RB_REMOVE(ip6_msource_tree, &inm->in6m_srcs, ims); @@ -1171,7 +1164,7 @@ in6m_reap(struct in6_multi *inm) void in6m_purge(struct in6_multi *inm) { - struct ip6_msource *ims, *tims; + struct ip6_msource *ims, *tims; IN6M_LOCK_ASSERT_HELD(inm); @@ -1198,20 +1191,16 @@ in6_joingroup(struct ifnet *ifp, struct in6_addr *mcaddr, *errorp = 0; - imm = in6_multi_mship_alloc(M_WAITOK); - if (imm == NULL) { - *errorp = ENOBUFS; - return (NULL); - } + imm = in6_multi_mship_alloc(Z_WAITOK); error = in6_mc_join(ifp, mcaddr, NULL, &imm->i6mm_maddr, delay); if (error) { *errorp = error; in6_multi_mship_free(imm); - return (NULL); + return NULL; } - return (imm); + return imm; } /* @@ -1244,10 +1233,10 @@ in6_mc_join(struct ifnet *ifp, const struct in6_addr *mcaddr, /*const*/ struct in6_mfilter *imf, struct in6_multi **pinm, const int delay) { - struct in6_mfilter timf; - struct in6_multi *inm = NULL; - int error = 0; - struct mld_tparams mtp; + struct in6_mfilter timf; + struct in6_multi *inm = NULL; + int error = 0; + struct mld_tparams mtp; /* * Sanity: Check scope zone ID was set for ifp, if and @@ -1263,7 +1252,7 @@ in6_mc_join(struct ifnet *ifp, const struct in6_addr *mcaddr, ip6_sprintf(mcaddr), (uint64_t)VM_KERNEL_ADDRPERM(ifp), if_name(ifp))); - bzero(&mtp, sizeof (mtp)); + bzero(&mtp, sizeof(mtp)); *pinm = NULL; /* @@ -1278,7 +1267,7 @@ in6_mc_join(struct ifnet *ifp, const struct in6_addr *mcaddr, error = in6_mc_get(ifp, mcaddr, &inm); if (error) { MLD_PRINTF(("%s: in6_mc_get() failure\n", __func__)); - return (error); + return error; } MLD_PRINTF(("%s: merge inm state\n", __func__)); @@ -1306,13 +1295,13 @@ out_in6m_release: IN6M_REMREF(inm); } else { IN6M_UNLOCK(inm); - *pinm = inm; /* keep refcount from in6_mc_get() */ + *pinm = inm; /* keep refcount from in6_mc_get() */ } /* schedule timer now that we've dropped the lock(s) */ mld_set_timeout(&mtp); - return (error); + return error; } /* @@ -1331,11 +1320,11 @@ out_in6m_release: int in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) { - struct in6_mfilter timf; - int error, lastref; - struct mld_tparams mtp; + struct in6_mfilter timf; + int error, lastref; + struct mld_tparams mtp; - bzero(&mtp, sizeof (mtp)); + bzero(&mtp, sizeof(mtp)); error = 0; IN6M_LOCK_ASSERT_NOTHELD(inm); @@ -1372,8 +1361,9 @@ in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) MLD_PRINTF(("%s: doing mld downcall\n", __func__)); error = mld_change_state(inm, &mtp, 0); #if MLD_DEBUG - if (error) + if (error) { MLD_PRINTF(("%s: failed mld downcall\n", __func__)); + } #endif lastref = in6_multi_detach(inm); VERIFY(!lastref || (!(inm->in6m_debug & IFD_ATTACHED) && @@ -1381,13 +1371,13 @@ in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) IN6M_UNLOCK(inm); in6_multihead_lock_done(); - if (lastref) - IN6M_REMREF(inm); /* for in6_multihead list */ - + if (lastref) { + IN6M_REMREF(inm); /* for in6_multihead list */ + } /* schedule timer now that we've dropped the lock(s) */ mld_set_timeout(&mtp); - return (error); + return error; } /* @@ -1402,26 +1392,26 @@ in6_mc_leave(struct in6_multi *inm, /*const*/ struct in6_mfilter *imf) static int in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) { - struct group_source_req gsr; - sockunion_t *gsa, *ssa; - struct ifnet *ifp; - struct in6_mfilter *imf; - struct ip6_moptions *imo; - struct in6_msource *ims; - struct in6_multi *inm; - size_t idx; - uint16_t fmode; - int error, doblock; - struct mld_tparams mtp; - - bzero(&mtp, sizeof (mtp)); + struct group_source_req gsr; + struct sockaddr_in6 *gsa, *ssa; + struct ifnet *ifp; + struct in6_mfilter *imf; + struct ip6_moptions *imo; + struct in6_msource *ims; + struct in6_multi *inm; + size_t idx; + uint8_t fmode; + int error, doblock; + struct mld_tparams mtp; + + bzero(&mtp, sizeof(mtp)); ifp = NULL; error = 0; doblock = 0; memset(&gsr, 0, sizeof(struct group_source_req)); - gsa = (sockunion_t *)&gsr.gsr_group; - ssa = (sockunion_t *)&gsr.gsr_source; + gsa = (struct sockaddr_in6 *)&gsr.gsr_group; + ssa = (struct sockaddr_in6 *)&gsr.gsr_source; switch (sopt->sopt_name) { case MCAST_BLOCK_SOURCE: @@ -1429,55 +1419,61 @@ in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) error = sooptcopyin(sopt, &gsr, sizeof(struct group_source_req), sizeof(struct group_source_req)); - if (error) - return (error); + if (error) { + return error; + } - if (gsa->sin6.sin6_family != AF_INET6 || - gsa->sin6.sin6_len != sizeof(struct sockaddr_in6)) - return (EINVAL); + if (gsa->sin6_family != AF_INET6 || + gsa->sin6_len != sizeof(struct sockaddr_in6)) { + return EINVAL; + } - if (ssa->sin6.sin6_family != AF_INET6 || - ssa->sin6.sin6_len != sizeof(struct sockaddr_in6)) - return (EINVAL); + if (ssa->sin6_family != AF_INET6 || + ssa->sin6_len != sizeof(struct sockaddr_in6)) { + return EINVAL; + } ifnet_head_lock_shared(); if (gsr.gsr_interface == 0 || (u_int)if_index < gsr.gsr_interface) { ifnet_head_done(); - return (EADDRNOTAVAIL); + return EADDRNOTAVAIL; } ifp = ifindex2ifnet[gsr.gsr_interface]; ifnet_head_done(); - if (ifp == NULL) - return (EADDRNOTAVAIL); + if (ifp == NULL) { + return EADDRNOTAVAIL; + } - if (sopt->sopt_name == MCAST_BLOCK_SOURCE) + if (sopt->sopt_name == MCAST_BLOCK_SOURCE) { doblock = 1; + } break; default: MLD_PRINTF(("%s: unknown sopt_name %d\n", __func__, sopt->sopt_name)); - return (EOPNOTSUPP); - break; + return EOPNOTSUPP; } - if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) - return (EINVAL); + if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6_addr)) { + return EINVAL; + } - (void) in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); + (void) in6_setscope(&gsa->sin6_addr, ifp, NULL); /* * Check if we are actually a member of this group. */ imo = in6p_findmoptions(inp); - if (imo == NULL) - return (ENOMEM); + if (imo == NULL) { + return ENOMEM; + } IM6O_LOCK(imo); - idx = im6o_match_group(imo, ifp, &gsa->sa); + idx = im6o_match_group(imo, ifp, gsa); if (idx == (size_t)-1 || imo->im6o_mfilters == NULL) { error = EADDRNOTAVAIL; goto out_imo_locked; @@ -1503,10 +1499,10 @@ in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) * Asked to unblock, but nothing to unblock. * If adding a new block entry, allocate it. */ - ims = im6o_match_source(imo, idx, &ssa->sa); + ims = im6o_match_source(imo, idx, ssa); if ((ims != NULL && doblock) || (ims == NULL && !doblock)) { MLD_PRINTF(("%s: source %s %spresent\n", __func__, - ip6_sprintf(&ssa->sin6.sin6_addr), + ip6_sprintf(&ssa->sin6_addr), doblock ? "" : "not ")); error = EADDRNOTAVAIL; goto out_imo_locked; @@ -1517,12 +1513,13 @@ in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) */ if (doblock) { MLD_PRINTF(("%s: %s source\n", __func__, "block")); - ims = im6f_graft(imf, fmode, &ssa->sin6); - if (ims == NULL) + ims = im6f_graft(imf, fmode, ssa); + if (ims == NULL) { error = ENOMEM; + } } else { MLD_PRINTF(("%s: %s source\n", __func__, "allow")); - error = im6f_prune(imf, &ssa->sin6); + error = im6f_prune(imf, ssa); } if (error) { @@ -1546,26 +1543,28 @@ in6p_block_unblock_source(struct inpcb *inp, struct sockopt *sopt) error = mld_change_state(inm, &mtp, 0); IN6M_UNLOCK(inm); #if MLD_DEBUG - if (error) + if (error) { MLD_PRINTF(("%s: failed mld downcall\n", __func__)); + } #endif out_im6f_rollback: - if (error) + if (error) { im6f_rollback(imf); - else + } else { im6f_commit(imf); + } im6f_reap(imf); out_imo_locked: IM6O_UNLOCK(imo); - IM6O_REMREF(imo); /* from in6p_findmoptions() */ + IM6O_REMREF(imo); /* from in6p_findmoptions() */ /* schedule timer now that we've dropped the lock(s) */ mld_set_timeout(&mtp); - return (error); + return error; } /* @@ -1576,51 +1575,53 @@ out_imo_locked: static struct ip6_moptions * in6p_findmoptions(struct inpcb *inp) { - struct ip6_moptions *imo; - struct in6_multi **immp; - struct in6_mfilter *imfp; - size_t idx; + struct ip6_moptions *imo; + struct in6_multi **immp; + struct in6_mfilter *imfp; + size_t idx; if ((imo = inp->in6p_moptions) != NULL) { - IM6O_ADDREF(imo); /* for caller */ - return (imo); + IM6O_ADDREF(imo); /* for caller */ + return imo; } - imo = ip6_allocmoptions(M_WAITOK); - if (imo == NULL) - return (NULL); + imo = ip6_allocmoptions(Z_WAITOK); + if (imo == NULL) { + return NULL; + } - immp = _MALLOC(sizeof (*immp) * IPV6_MIN_MEMBERSHIPS, M_IP6MOPTS, + immp = _MALLOC(sizeof(*immp) * IPV6_MIN_MEMBERSHIPS, M_IP6MOPTS, M_WAITOK | M_ZERO); if (immp == NULL) { IM6O_REMREF(imo); - return (NULL); + return NULL; } - imfp = _MALLOC(sizeof (struct in6_mfilter) * IPV6_MIN_MEMBERSHIPS, + imfp = _MALLOC(sizeof(struct in6_mfilter) * IPV6_MIN_MEMBERSHIPS, M_IN6MFILTER, M_WAITOK | M_ZERO); if (imfp == NULL) { _FREE(immp, M_IP6MOPTS); IM6O_REMREF(imo); - return (NULL); + return NULL; } imo->im6o_multicast_ifp = NULL; - imo->im6o_multicast_hlim = ip6_defmcasthlim; - imo->im6o_multicast_loop = in6_mcast_loop; + imo->im6o_multicast_hlim = (u_char)ip6_defmcasthlim; + imo->im6o_multicast_loop = (u_char)in6_mcast_loop; imo->im6o_num_memberships = 0; imo->im6o_max_memberships = IPV6_MIN_MEMBERSHIPS; imo->im6o_membership = immp; /* Initialize per-group source filters. */ - for (idx = 0; idx < IPV6_MIN_MEMBERSHIPS; idx++) + for (idx = 0; idx < IPV6_MIN_MEMBERSHIPS; idx++) { im6f_init(&imfp[idx], MCAST_UNDEFINED, MCAST_EXCLUDE); + } imo->im6o_mfilters = imfp; inp->in6p_moptions = imo; /* keep reference from ip6_allocmoptions() */ - IM6O_ADDREF(imo); /* for caller */ + IM6O_ADDREF(imo); /* for caller */ - return (imo); + return imo; } /* @@ -1630,20 +1631,20 @@ in6p_findmoptions(struct inpcb *inp) static int in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) { - struct __msfilterreq64 msfr, msfr64; - struct __msfilterreq32 msfr32; - sockunion_t *gsa; - struct ifnet *ifp; - struct ip6_moptions *imo; - struct in6_mfilter *imf; - struct ip6_msource *ims; - struct in6_msource *lims; - struct sockaddr_in6 *psin; - struct sockaddr_storage *ptss; - struct sockaddr_storage *tss; - int error; - size_t idx, nsrcs, ncsrcs; - user_addr_t tmp_ptr; + struct __msfilterreq64 msfr = {}, msfr64; + struct __msfilterreq32 msfr32; + struct sockaddr_in6 *gsa; + struct ifnet *ifp; + struct ip6_moptions *imo; + struct in6_mfilter *imf; + struct ip6_msource *ims; + struct in6_msource *lims; + struct sockaddr_in6 *psin; + struct sockaddr_storage *ptss; + struct sockaddr_storage *tss; + int error; + size_t idx, nsrcs, ncsrcs; + user_addr_t tmp_ptr; imo = inp->in6p_moptions; VERIFY(imo != NULL); @@ -1652,56 +1653,63 @@ in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) error = sooptcopyin(sopt, &msfr64, sizeof(struct __msfilterreq64), sizeof(struct __msfilterreq64)); - if (error) - return (error); + if (error) { + return error; + } /* we never use msfr.msfr_srcs; */ - memcpy(&msfr, &msfr64, sizeof(msfr)); + memcpy(&msfr, &msfr64, sizeof(msfr64)); } else { error = sooptcopyin(sopt, &msfr32, sizeof(struct __msfilterreq32), sizeof(struct __msfilterreq32)); - if (error) - return (error); + if (error) { + return error; + } /* we never use msfr.msfr_srcs; */ - memcpy(&msfr, &msfr32, sizeof(msfr)); + memcpy(&msfr, &msfr32, sizeof(msfr32)); } if (msfr.msfr_group.ss_family != AF_INET6 || - msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6)) - return (EINVAL); + msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6)) { + return EINVAL; + } - gsa = (sockunion_t *)&msfr.msfr_group; - if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) - return (EINVAL); + gsa = (struct sockaddr_in6 *)&msfr.msfr_group; + if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6_addr)) { + return EINVAL; + } ifnet_head_lock_shared(); if (msfr.msfr_ifindex == 0 || (u_int)if_index < msfr.msfr_ifindex) { ifnet_head_done(); - return (EADDRNOTAVAIL); + return EADDRNOTAVAIL; } ifp = ifindex2ifnet[msfr.msfr_ifindex]; ifnet_head_done(); - if (ifp == NULL) - return (EADDRNOTAVAIL); + if (ifp == NULL) { + return EADDRNOTAVAIL; + } if ((size_t) msfr.msfr_nsrcs > - UINT32_MAX / sizeof(struct sockaddr_storage)) + UINT32_MAX / sizeof(struct sockaddr_storage)) { msfr.msfr_nsrcs = UINT32_MAX / sizeof(struct sockaddr_storage); + } - if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc) - msfr.msfr_nsrcs = in6_mcast_maxsocksrc; + if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc) { + msfr.msfr_nsrcs = (uint32_t)in6_mcast_maxsocksrc; + } - (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); + (void)in6_setscope(&gsa->sin6_addr, ifp, NULL); IM6O_LOCK(imo); /* * Lookup group on the socket. */ - idx = im6o_match_group(imo, ifp, &gsa->sa); + idx = im6o_match_group(imo, ifp, gsa); if (idx == (size_t)-1 || imo->im6o_mfilters == NULL) { IM6O_UNLOCK(imo); - return (EADDRNOTAVAIL); + return EADDRNOTAVAIL; } imf = &imo->im6o_mfilters[idx]; @@ -1710,7 +1718,7 @@ in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) */ if (imf->im6f_st[1] == MCAST_UNDEFINED) { IM6O_UNLOCK(imo); - return (EAGAIN); + return EAGAIN; } msfr.msfr_fmode = imf->im6f_st[1]; @@ -1723,19 +1731,19 @@ in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) */ tss = NULL; - if (IS_64BIT_PROCESS(current_proc())) - tmp_ptr = msfr64.msfr_srcs; - else + if (IS_64BIT_PROCESS(current_proc())) { + tmp_ptr = (user_addr_t)msfr64.msfr_srcs; + } else { tmp_ptr = CAST_USER_ADDR_T(msfr32.msfr_srcs); + } if (tmp_ptr != USER_ADDR_NULL && msfr.msfr_nsrcs > 0) { tss = _MALLOC((size_t) msfr.msfr_nsrcs * sizeof(*tss), M_TEMP, M_WAITOK | M_ZERO); if (tss == NULL) { IM6O_UNLOCK(imo); - return (ENOBUFS); + return ENOBUFS; } - bzero(tss, (size_t) msfr.msfr_nsrcs * sizeof(*tss)); } /* @@ -1748,8 +1756,9 @@ in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) RB_FOREACH(ims, ip6_msource_tree, &imf->im6f_sources) { lims = (struct in6_msource *)ims; if (lims->im6sl_st[0] == MCAST_UNDEFINED || - lims->im6sl_st[0] != imf->im6f_st[0]) + lims->im6sl_st[0] != imf->im6f_st[0]) { continue; + } if (tss != NULL && nsrcs > 0) { psin = (struct sockaddr_in6 *)ptss; psin->sin6_family = AF_INET6; @@ -1767,11 +1776,12 @@ in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) if (tss != NULL) { error = copyout(tss, tmp_ptr, ncsrcs * sizeof(*tss)); FREE(tss, M_TEMP); - if (error) - return (error); + if (error) { + return error; + } } - msfr.msfr_nsrcs = ncsrcs; + msfr.msfr_nsrcs = (uint32_t)ncsrcs; if (IS_64BIT_PROCESS(current_proc())) { msfr64.msfr_ifindex = msfr.msfr_ifindex; msfr64.msfr_fmode = msfr.msfr_fmode; @@ -1784,13 +1794,13 @@ in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) msfr32.msfr_ifindex = msfr.msfr_ifindex; msfr32.msfr_fmode = msfr.msfr_fmode; msfr32.msfr_nsrcs = msfr.msfr_nsrcs; - memcpy(&msfr64.msfr_group, &msfr.msfr_group, + memcpy(&msfr32.msfr_group, &msfr.msfr_group, sizeof(struct sockaddr_storage)); error = sooptcopyout(sopt, &msfr32, sizeof(struct __msfilterreq32)); } - return (error); + return error; } /* @@ -1799,9 +1809,9 @@ in6p_get_source_filters(struct inpcb *inp, struct sockopt *sopt) int ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt) { - struct ip6_moptions *im6o; - int error; - u_int optval; + struct ip6_moptions *im6o; + int error; + u_int optval; im6o = inp->in6p_moptions; /* @@ -1811,21 +1821,23 @@ ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt) if (SOCK_PROTO(inp->inp_socket) == IPPROTO_DIVERT || (SOCK_TYPE(inp->inp_socket) != SOCK_RAW && SOCK_TYPE(inp->inp_socket) != SOCK_DGRAM)) { - return (EOPNOTSUPP); + return EOPNOTSUPP; } error = 0; switch (sopt->sopt_name) { case IPV6_MULTICAST_IF: - if (im6o != NULL) + if (im6o != NULL) { IM6O_LOCK(im6o); + } if (im6o == NULL || im6o->im6o_multicast_ifp == NULL) { optval = 0; } else { optval = im6o->im6o_multicast_ifp->if_index; } - if (im6o != NULL) + if (im6o != NULL) { IM6O_UNLOCK(im6o); + } error = sooptcopyout(sopt, &optval, sizeof(u_int)); break; @@ -1864,7 +1876,7 @@ ip6_getmoptions(struct inpcb *inp, struct sockopt *sopt) break; } - return (error); + return error; } /* @@ -1886,17 +1898,19 @@ static struct ifnet * in6p_lookup_mcast_ifp(const struct inpcb *in6p, const struct sockaddr_in6 *gsin6) { - struct route_in6 ro6; - struct ifnet *ifp; - unsigned int ifscope = IFSCOPE_NONE; + struct route_in6 ro6; + struct ifnet *ifp; + unsigned int ifscope = IFSCOPE_NONE; VERIFY(in6p == NULL || (in6p->inp_vflag & INP_IPV6)); VERIFY(gsin6->sin6_family == AF_INET6); - if (IN6_IS_ADDR_MULTICAST(&gsin6->sin6_addr) == 0) + if (IN6_IS_ADDR_MULTICAST(&gsin6->sin6_addr) == 0) { return NULL; + } - if (in6p != NULL && (in6p->inp_flags & INP_BOUND_IF)) + if (in6p != NULL && (in6p->inp_flags & INP_BOUND_IF)) { ifscope = in6p->inp_boundifp->if_index; + } ifp = NULL; memset(&ro6, 0, sizeof(struct route_in6)); @@ -1908,7 +1922,7 @@ in6p_lookup_mcast_ifp(const struct inpcb *in6p, } ROUTE_RELEASE(&ro6); - return (ifp); + return ifp; } /* @@ -1928,20 +1942,23 @@ in6p_lookup_v4addr(struct ipv6_mreq *mreq, struct ip_mreq *v4mreq) ifnet_head_lock_shared(); if (mreq->ipv6mr_interface > (unsigned int)if_index) { ifnet_head_done(); - return (EADDRNOTAVAIL); - } else + return EADDRNOTAVAIL; + } else { ifp = ifindex2ifnet[mreq->ipv6mr_interface]; + } ifnet_head_done(); - if (ifp == NULL) - return (EADDRNOTAVAIL); + if (ifp == NULL) { + return EADDRNOTAVAIL; + } ifa = ifa_ifpgetprimary(ifp, AF_INET); - if (ifa == NULL) - return (EADDRNOTAVAIL); + if (ifa == NULL) { + return EADDRNOTAVAIL; + } sin = (struct sockaddr_in *)(uintptr_t)(size_t)ifa->ifa_addr; v4mreq->imr_interface.s_addr = sin->sin_addr.s_addr; IFA_REMREF(ifa); - return (0); + return 0; } /* @@ -1953,29 +1970,27 @@ in6p_lookup_v4addr(struct ipv6_mreq *mreq, struct ip_mreq *v4mreq) static int in6p_join_group(struct inpcb *inp, struct sockopt *sopt) { - struct group_source_req gsr; - sockunion_t *gsa, *ssa; - struct ifnet *ifp; - struct in6_mfilter *imf; - struct ip6_moptions *imo; - struct in6_multi *inm = NULL; - struct in6_msource *lims = NULL; - size_t idx; - int error, is_new; - uint32_t scopeid = 0; - struct mld_tparams mtp; - - bzero(&mtp, sizeof (mtp)); + struct group_source_req gsr; + struct sockaddr_in6 *gsa, *ssa; + struct ifnet *ifp; + struct in6_mfilter *imf; + struct ip6_moptions *imo; + struct in6_multi *inm = NULL; + struct in6_msource *lims = NULL; + size_t idx; + int error, is_new; + uint32_t scopeid = 0; + struct mld_tparams mtp; + + bzero(&mtp, sizeof(mtp)); ifp = NULL; imf = NULL; error = 0; is_new = 0; memset(&gsr, 0, sizeof(struct group_source_req)); - gsa = (sockunion_t *)&gsr.gsr_group; - gsa->ss.ss_family = AF_UNSPEC; - ssa = (sockunion_t *)&gsr.gsr_source; - ssa->ss.ss_family = AF_UNSPEC; + gsa = (struct sockaddr_in6 *)&gsr.gsr_group; + ssa = (struct sockaddr_in6 *)&gsr.gsr_source; /* * Chew everything into struct group_source_req. @@ -1986,52 +2001,52 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) switch (sopt->sopt_name) { case IPV6_JOIN_GROUP: { struct ipv6_mreq mreq; - struct sockaddr_in6 *gsin6; error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq), sizeof(struct ipv6_mreq)); - if (error) - return (error); + if (error) { + return error; + } if (IN6_IS_ADDR_V4MAPPED(&mreq.ipv6mr_multiaddr)) { struct ip_mreq v4mreq; struct sockopt v4sopt; v4mreq.imr_multiaddr.s_addr = mreq.ipv6mr_multiaddr.s6_addr32[3]; - if (mreq.ipv6mr_interface == 0) + if (mreq.ipv6mr_interface == 0) { v4mreq.imr_interface.s_addr = INADDR_ANY; - else + } else { error = in6p_lookup_v4addr(&mreq, &v4mreq); - if (error) - return (error); + } + if (error) { + return error; + } v4sopt.sopt_dir = SOPT_SET; - v4sopt.sopt_level = sopt->sopt_level; + v4sopt.sopt_level = sopt->sopt_level; v4sopt.sopt_name = IP_ADD_MEMBERSHIP; v4sopt.sopt_val = CAST_USER_ADDR_T(&v4mreq); v4sopt.sopt_valsize = sizeof(v4mreq); v4sopt.sopt_p = kernproc; - return (inp_join_group(inp, &v4sopt)); + return inp_join_group(inp, &v4sopt); } - gsa->sin6.sin6_family = AF_INET6; - gsa->sin6.sin6_len = sizeof(struct sockaddr_in6); - gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr; - - gsin6 = &gsa->sin6; + gsa->sin6_family = AF_INET6; + gsa->sin6_len = sizeof(struct sockaddr_in6); + gsa->sin6_addr = mreq.ipv6mr_multiaddr; - /* Only allow IPv6 multicast addresses */ - if (IN6_IS_ADDR_MULTICAST(&gsin6->sin6_addr) == 0) { - return (EINVAL); + /* Only allow IPv6 multicast addresses */ + if (IN6_IS_ADDR_MULTICAST(&gsa->sin6_addr) == 0) { + return EINVAL; } if (mreq.ipv6mr_interface == 0) { - ifp = in6p_lookup_mcast_ifp(inp, gsin6); + ifp = in6p_lookup_mcast_ifp(inp, gsa); } else { ifnet_head_lock_shared(); if ((u_int)if_index < mreq.ipv6mr_interface) { ifnet_head_done(); - return (EADDRNOTAVAIL); - } + return EADDRNOTAVAIL; + } ifp = ifindex2ifnet[mreq.ipv6mr_interface]; ifnet_head_done(); } @@ -2052,34 +2067,38 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) sizeof(struct group_source_req), sizeof(struct group_source_req)); } - if (error) - return (error); + if (error) { + return error; + } - if (gsa->sin6.sin6_family != AF_INET6 || - gsa->sin6.sin6_len != sizeof(struct sockaddr_in6)) - return (EINVAL); + if (gsa->sin6_family != AF_INET6 || + gsa->sin6_len != sizeof(struct sockaddr_in6)) { + return EINVAL; + } if (sopt->sopt_name == MCAST_JOIN_SOURCE_GROUP) { - if (ssa->sin6.sin6_family != AF_INET6 || - ssa->sin6.sin6_len != sizeof(struct sockaddr_in6)) - return (EINVAL); - if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr)) - return (EINVAL); + if (ssa->sin6_family != AF_INET6 || + ssa->sin6_len != sizeof(struct sockaddr_in6)) { + return EINVAL; + } + if (IN6_IS_ADDR_MULTICAST(&ssa->sin6_addr)) { + return EINVAL; + } /* * TODO: Validate embedded scope ID in source * list entry against passed-in ifp, if and only * if source list filter entry is iface or node local. */ - in6_clearscope(&ssa->sin6.sin6_addr); - ssa->sin6.sin6_port = 0; - ssa->sin6.sin6_scope_id = 0; + in6_clearscope(&ssa->sin6_addr); + ssa->sin6_port = 0; + ssa->sin6_scope_id = 0; } ifnet_head_lock_shared(); if (gsr.gsr_interface == 0 || (u_int)if_index < gsr.gsr_interface) { ifnet_head_done(); - return (EADDRNOTAVAIL); + return EADDRNOTAVAIL; } ifp = ifindex2ifnet[gsr.gsr_interface]; ifnet_head_done(); @@ -2088,46 +2107,57 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) default: MLD_PRINTF(("%s: unknown sopt_name %d\n", __func__, sopt->sopt_name)); - return (EOPNOTSUPP); - break; + return EOPNOTSUPP; } - if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) - return (EINVAL); + if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6_addr)) { + return EINVAL; + } - if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) - return (EADDRNOTAVAIL); + if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { + return EADDRNOTAVAIL; + } - gsa->sin6.sin6_port = 0; - gsa->sin6.sin6_scope_id = 0; + INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_mcast_join_total); + /* + * TBD: revisit the criteria for non-OS initiated joins + */ + if (inp->inp_lport == htons(5353)) { + INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_mcast_join_os_total); + } + + gsa->sin6_port = 0; + gsa->sin6_scope_id = 0; /* * Always set the scope zone ID on memberships created from userland. * Use the passed-in ifp to do this. */ - (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, &scopeid); + (void)in6_setscope(&gsa->sin6_addr, ifp, &scopeid); /* * Some addresses are not valid without an embedded scopeid. * This check must be present because otherwise we will later hit * a VERIFY() in in6_mc_join(). */ - if ((IN6_IS_ADDR_MC_LINKLOCAL(&gsa->sin6.sin6_addr) || - IN6_IS_ADDR_MC_INTFACELOCAL(&gsa->sin6.sin6_addr)) && - (scopeid == 0 || gsa->sin6.sin6_addr.s6_addr16[1] == 0)) - return (EINVAL); + if ((IN6_IS_ADDR_MC_LINKLOCAL(&gsa->sin6_addr) || + IN6_IS_ADDR_MC_INTFACELOCAL(&gsa->sin6_addr)) && + (scopeid == 0 || gsa->sin6_addr.s6_addr16[1] == 0)) { + return EINVAL; + } imo = in6p_findmoptions(inp); - if (imo == NULL) - return (ENOMEM); + if (imo == NULL) { + return ENOMEM; + } IM6O_LOCK(imo); - idx = im6o_match_group(imo, ifp, &gsa->sa); + idx = im6o_match_group(imo, ifp, gsa); if (idx == (size_t)-1) { is_new = 1; } else { inm = imo->im6o_membership[idx]; imf = &imo->im6o_mfilters[idx]; - if (ssa->ss.ss_family != AF_UNSPEC) { + if (ssa->sin6_family != AF_UNSPEC) { /* * MCAST_JOIN_SOURCE_GROUP on an exclusive membership * is an error. On an existing inclusive membership, @@ -2153,9 +2183,9 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) * full-state SSM API with the delta-based API, * which is discouraged in the relevant RFCs. */ - lims = im6o_match_source(imo, idx, &ssa->sa); + lims = im6o_match_source(imo, idx, ssa); if (lims != NULL /*&& - lims->im6sl_st[1] == MCAST_INCLUDE*/) { + * lims->im6sl_st[1] == MCAST_INCLUDE*/) { error = EADDRNOTAVAIL; goto out_imo_locked; } @@ -2176,8 +2206,9 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) */ error = EINVAL; /* See comments above for EADDRINUSE */ - if (imf->im6f_st[1] == MCAST_EXCLUDE) + if (imf->im6f_st[1] == MCAST_EXCLUDE) { error = EADDRINUSE; + } goto out_imo_locked; } } @@ -2188,9 +2219,10 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) if (is_new) { if (imo->im6o_num_memberships == imo->im6o_max_memberships) { - error = im6o_grow(imo, 0); - if (error) + error = im6o_grow(imo); + if (error) { goto out_imo_locked; + } } /* * Allocate the new slot upfront so we can deal with @@ -2216,15 +2248,15 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) * XXX: Should check for non-NULL lims (node exists but may * not be in-mode) for interop with full-state API. */ - if (ssa->ss.ss_family != AF_UNSPEC) { + if (ssa->sin6_family != AF_UNSPEC) { /* Membership starts in IN mode */ if (is_new) { MLD_PRINTF(("%s: new join w/source\n", __func__); - im6f_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE)); + im6f_init(imf, MCAST_UNDEFINED, MCAST_INCLUDE)); } else { MLD_PRINTF(("%s: %s source\n", __func__, "allow")); } - lims = im6f_graft(imf, MCAST_INCLUDE, &ssa->sin6); + lims = im6f_graft(imf, MCAST_INCLUDE, ssa); if (lims == NULL) { MLD_PRINTF(("%s: merge imf state failed\n", __func__)); @@ -2244,11 +2276,24 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) */ if (is_new) { + /* + * See inp_join_group() for why we need to unlock + */ + IM6O_ADDREF_LOCKED(imo); + IM6O_UNLOCK(imo); + socket_unlock(inp->inp_socket, 0); + VERIFY(inm == NULL); - error = in6_mc_join(ifp, &gsa->sin6.sin6_addr, imf, &inm, 0); + error = in6_mc_join(ifp, &gsa->sin6_addr, imf, &inm, 0); VERIFY(inm != NULL || error != 0); - if (error) + + socket_lock(inp->inp_socket, 0); + IM6O_REMREF(imo); + IM6O_LOCK(imo); + + if (error) { goto out_im6o_free; + } imo->im6o_membership[idx] = inm; /* from in6_mc_join() */ } else { MLD_PRINTF(("%s: merge inm state\n", __func__)); @@ -2273,10 +2318,11 @@ in6p_join_group(struct inpcb *inp, struct sockopt *sopt) out_im6f_rollback: if (error) { im6f_rollback(imf); - if (is_new) + if (is_new) { im6f_purge(imf); - else + } else { im6f_reap(imf); + } } else { im6f_commit(imf); } @@ -2290,12 +2336,12 @@ out_im6o_free: out_imo_locked: IM6O_UNLOCK(imo); - IM6O_REMREF(imo); /* from in6p_findmoptions() */ + IM6O_REMREF(imo); /* from in6p_findmoptions() */ /* schedule timer now that we've dropped the lock(s) */ mld_set_timeout(&mtp); - return (error); + return error; } /* @@ -2304,29 +2350,27 @@ out_imo_locked: static int in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) { - struct ipv6_mreq mreq; - struct group_source_req gsr; - sockunion_t *gsa, *ssa; - struct ifnet *ifp; - struct in6_mfilter *imf; - struct ip6_moptions *imo; - struct in6_msource *ims; - struct in6_multi *inm = NULL; - uint32_t ifindex = 0; - size_t idx; - int error, is_final; - struct mld_tparams mtp; - - bzero(&mtp, sizeof (mtp)); + struct ipv6_mreq mreq; + struct group_source_req gsr; + struct sockaddr_in6 *gsa, *ssa; + struct ifnet *ifp; + struct in6_mfilter *imf; + struct ip6_moptions *imo; + struct in6_msource *ims; + struct in6_multi *inm = NULL; + uint32_t ifindex = 0; + size_t idx; + int error, is_final; + struct mld_tparams mtp; + + bzero(&mtp, sizeof(mtp)); ifp = NULL; error = 0; is_final = 1; memset(&gsr, 0, sizeof(struct group_source_req)); - gsa = (sockunion_t *)&gsr.gsr_group; - gsa->ss.ss_family = AF_UNSPEC; - ssa = (sockunion_t *)&gsr.gsr_source; - ssa->ss.ss_family = AF_UNSPEC; + gsa = (struct sockaddr_in6 *)&gsr.gsr_group; + ssa = (struct sockaddr_in6 *)&gsr.gsr_source; /* * Chew everything passed in up into a struct group_source_req @@ -2337,43 +2381,43 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) */ switch (sopt->sopt_name) { case IPV6_LEAVE_GROUP: { - struct sockaddr_in6 *gsin6; - error = sooptcopyin(sopt, &mreq, sizeof(struct ipv6_mreq), sizeof(struct ipv6_mreq)); - if (error) - return (error); + if (error) { + return error; + } if (IN6_IS_ADDR_V4MAPPED(&mreq.ipv6mr_multiaddr)) { struct ip_mreq v4mreq; struct sockopt v4sopt; v4mreq.imr_multiaddr.s_addr = mreq.ipv6mr_multiaddr.s6_addr32[3]; - if (mreq.ipv6mr_interface == 0) + if (mreq.ipv6mr_interface == 0) { v4mreq.imr_interface.s_addr = INADDR_ANY; - else + } else { error = in6p_lookup_v4addr(&mreq, &v4mreq); - if (error) - return (error); + } + if (error) { + return error; + } v4sopt.sopt_dir = SOPT_SET; - v4sopt.sopt_level = sopt->sopt_level; + v4sopt.sopt_level = sopt->sopt_level; v4sopt.sopt_name = IP_DROP_MEMBERSHIP; v4sopt.sopt_val = CAST_USER_ADDR_T(&v4mreq); v4sopt.sopt_valsize = sizeof(v4mreq); v4sopt.sopt_p = kernproc; - return (inp_leave_group(inp, &v4sopt)); + return inp_leave_group(inp, &v4sopt); } - gsa->sin6.sin6_family = AF_INET6; - gsa->sin6.sin6_len = sizeof(struct sockaddr_in6); - gsa->sin6.sin6_addr = mreq.ipv6mr_multiaddr; - gsa->sin6.sin6_port = 0; - gsa->sin6.sin6_scope_id = 0; + gsa->sin6_family = AF_INET6; + gsa->sin6_len = sizeof(struct sockaddr_in6); + gsa->sin6_addr = mreq.ipv6mr_multiaddr; + gsa->sin6_port = 0; + gsa->sin6_scope_id = 0; ifindex = mreq.ipv6mr_interface; - gsin6 = &gsa->sin6; - /* Only allow IPv6 multicast addresses */ - if (IN6_IS_ADDR_MULTICAST(&gsin6->sin6_addr) == 0) { - return (EINVAL); + /* Only allow IPv6 multicast addresses */ + if (IN6_IS_ADDR_MULTICAST(&gsa->sin6_addr) == 0) { + return EINVAL; } break; } @@ -2389,39 +2433,43 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) sizeof(struct group_source_req), sizeof(struct group_source_req)); } - if (error) - return (error); + if (error) { + return error; + } - if (gsa->sin6.sin6_family != AF_INET6 || - gsa->sin6.sin6_len != sizeof(struct sockaddr_in6)) - return (EINVAL); + if (gsa->sin6_family != AF_INET6 || + gsa->sin6_len != sizeof(struct sockaddr_in6)) { + return EINVAL; + } if (sopt->sopt_name == MCAST_LEAVE_SOURCE_GROUP) { - if (ssa->sin6.sin6_family != AF_INET6 || - ssa->sin6.sin6_len != sizeof(struct sockaddr_in6)) - return (EINVAL); - if (IN6_IS_ADDR_MULTICAST(&ssa->sin6.sin6_addr)) - return (EINVAL); + if (ssa->sin6_family != AF_INET6 || + ssa->sin6_len != sizeof(struct sockaddr_in6)) { + return EINVAL; + } + if (IN6_IS_ADDR_MULTICAST(&ssa->sin6_addr)) { + return EINVAL; + } /* * TODO: Validate embedded scope ID in source * list entry against passed-in ifp, if and only * if source list filter entry is iface or node local. */ - in6_clearscope(&ssa->sin6.sin6_addr); + in6_clearscope(&ssa->sin6_addr); } - gsa->sin6.sin6_port = 0; - gsa->sin6.sin6_scope_id = 0; + gsa->sin6_port = 0; + gsa->sin6_scope_id = 0; ifindex = gsr.gsr_interface; break; default: MLD_PRINTF(("%s: unknown sopt_name %d\n", __func__, sopt->sopt_name)); - return (EOPNOTSUPP); - break; + return EOPNOTSUPP; } - if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) - return (EINVAL); + if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6_addr)) { + return EINVAL; + } /* * Validate interface index if provided. If no interface index @@ -2434,17 +2482,19 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) ifnet_head_lock_shared(); if ((u_int)if_index < ifindex) { ifnet_head_done(); - return (EADDRNOTAVAIL); + return EADDRNOTAVAIL; } ifp = ifindex2ifnet[ifindex]; ifnet_head_done(); - if (ifp == NULL) - return (EADDRNOTAVAIL); - (void) in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); + if (ifp == NULL) { + return EADDRNOTAVAIL; + } + (void) in6_setscope(&gsa->sin6_addr, ifp, NULL); } else { - error = sa6_embedscope(&gsa->sin6, ip6_use_defzone); - if (error) - return (EADDRNOTAVAIL); + error = sa6_embedscope(gsa, ip6_use_defzone); + if (error) { + return EADDRNOTAVAIL; + } /* * Some badly behaved applications don't pass an ifindex * or a scope ID, which is an API violation. In this case, @@ -2455,19 +2505,23 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) * directly until such time as this implementation is * refactored, assuming the scope IDs are the way to go. */ - ifindex = ntohs(gsa->sin6.sin6_addr.s6_addr16[1]); + ifindex = ntohs(gsa->sin6_addr.s6_addr16[1]); if (ifindex == 0) { MLD_PRINTF(("%s: warning: no ifindex, looking up " "ifp for group %s.\n", __func__, - ip6_sprintf(&gsa->sin6.sin6_addr))); - ifp = in6p_lookup_mcast_ifp(inp, &gsa->sin6); + ip6_sprintf(&gsa->sin6_addr))); + ifp = in6p_lookup_mcast_ifp(inp, gsa); } else { + if (!IF_INDEX_IN_RANGE(ifindex)) { + return EADDRNOTAVAIL; + } ifnet_head_lock_shared(); ifp = ifindex2ifnet[ifindex]; ifnet_head_done(); } - if (ifp == NULL) - return (EADDRNOTAVAIL); + if (ifp == NULL) { + return EADDRNOTAVAIL; + } } VERIFY(ifp != NULL); @@ -2478,11 +2532,12 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) * Find the membership in the membership array. */ imo = in6p_findmoptions(inp); - if (imo == NULL) - return (ENOMEM); + if (imo == NULL) { + return ENOMEM; + } IM6O_LOCK(imo); - idx = im6o_match_group(imo, ifp, &gsa->sa); + idx = im6o_match_group(imo, ifp, gsa); if (idx == (size_t)-1) { error = EADDRNOTAVAIL; goto out_locked; @@ -2490,8 +2545,9 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) inm = imo->im6o_membership[idx]; imf = &imo->im6o_mfilters[idx]; - if (ssa->ss.ss_family != AF_UNSPEC) + if (ssa->sin6_family != AF_UNSPEC) { is_final = 0; + } /* * Begin state merge transaction at socket layer. @@ -2508,16 +2564,16 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) error = EADDRNOTAVAIL; goto out_locked; } - ims = im6o_match_source(imo, idx, &ssa->sa); + ims = im6o_match_source(imo, idx, ssa); if (ims == NULL) { MLD_PRINTF(("%s: source %s %spresent\n", __func__, - ip6_sprintf(&ssa->sin6.sin6_addr), + ip6_sprintf(&ssa->sin6_addr), "not ")); error = EADDRNOTAVAIL; goto out_locked; } MLD_PRINTF(("%s: %s source\n", __func__, "block")); - error = im6f_prune(imf, &ssa->sin6); + error = im6f_prune(imf, ssa); if (error) { MLD_PRINTF(("%s: merge imf state failed\n", __func__)); @@ -2556,10 +2612,11 @@ in6p_leave_group(struct inpcb *inp, struct sockopt *sopt) } out_im6f_rollback: - if (error) + if (error) { im6f_rollback(imf); - else + } else { im6f_commit(imf); + } im6f_reap(imf); @@ -2567,22 +2624,35 @@ out_im6f_rollback: /* Remove the gap in the membership array. */ VERIFY(inm == imo->im6o_membership[idx]); imo->im6o_membership[idx] = NULL; + + /* + * See inp_join_group() for why we need to unlock + */ + IM6O_ADDREF_LOCKED(imo); + IM6O_UNLOCK(imo); + socket_unlock(inp->inp_socket, 0); + IN6M_REMREF(inm); + + socket_lock(inp->inp_socket, 0); + IM6O_REMREF(imo); + IM6O_LOCK(imo); + for (++idx; idx < imo->im6o_num_memberships; ++idx) { - imo->im6o_membership[idx-1] = imo->im6o_membership[idx]; - imo->im6o_mfilters[idx-1] = imo->im6o_mfilters[idx]; + imo->im6o_membership[idx - 1] = imo->im6o_membership[idx]; + imo->im6o_mfilters[idx - 1] = imo->im6o_mfilters[idx]; } imo->im6o_num_memberships--; } out_locked: IM6O_UNLOCK(imo); - IM6O_REMREF(imo); /* from in6p_findmoptions() */ + IM6O_REMREF(imo); /* from in6p_findmoptions() */ /* schedule timer now that we've dropped the lock(s) */ mld_set_timeout(&mtp); - return (error); + return error; } /* @@ -2596,39 +2666,43 @@ out_locked: static int in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt) { - struct ifnet *ifp; - struct ip6_moptions *imo; - u_int ifindex; - int error; + struct ifnet *ifp; + struct ip6_moptions *imo; + u_int ifindex; + int error; - if (sopt->sopt_valsize != sizeof(u_int)) - return (EINVAL); + if (sopt->sopt_valsize != sizeof(u_int)) { + return EINVAL; + } error = sooptcopyin(sopt, &ifindex, sizeof(u_int), sizeof(u_int)); - if (error) - return (error); + if (error) { + return error; + } ifnet_head_lock_shared(); if ((u_int)if_index < ifindex) { ifnet_head_done(); - return (EINVAL); + return EINVAL; } ifp = ifindex2ifnet[ifindex]; ifnet_head_done(); - if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) - return (EADDRNOTAVAIL); + if (ifp == NULL || (ifp->if_flags & IFF_MULTICAST) == 0) { + return EADDRNOTAVAIL; + } imo = in6p_findmoptions(inp); - if (imo == NULL) - return (ENOMEM); + if (imo == NULL) { + return ENOMEM; + } IM6O_LOCK(imo); imo->im6o_multicast_ifp = ifp; IM6O_UNLOCK(imo); - IM6O_REMREF(imo); /* from in6p_findmoptions() */ + IM6O_REMREF(imo); /* from in6p_findmoptions() */ - return (0); + return 0; } /* @@ -2638,81 +2712,90 @@ in6p_set_multicast_if(struct inpcb *inp, struct sockopt *sopt) static int in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt) { - struct __msfilterreq64 msfr, msfr64; - struct __msfilterreq32 msfr32; - sockunion_t *gsa; - struct ifnet *ifp; - struct in6_mfilter *imf; - struct ip6_moptions *imo; - struct in6_multi *inm; - size_t idx; - int error; - user_addr_t tmp_ptr; - struct mld_tparams mtp; - - bzero(&mtp, sizeof (mtp)); + struct __msfilterreq64 msfr = {}, msfr64; + struct __msfilterreq32 msfr32; + struct sockaddr_in6 *gsa; + struct ifnet *ifp; + struct in6_mfilter *imf; + struct ip6_moptions *imo; + struct in6_multi *inm; + size_t idx; + int error; + user_addr_t tmp_ptr; + struct mld_tparams mtp; + + bzero(&mtp, sizeof(mtp)); if (IS_64BIT_PROCESS(current_proc())) { error = sooptcopyin(sopt, &msfr64, sizeof(struct __msfilterreq64), sizeof(struct __msfilterreq64)); - if (error) - return (error); + if (error) { + return error; + } /* we never use msfr.msfr_srcs; */ - memcpy(&msfr, &msfr64, sizeof(msfr)); + memcpy(&msfr, &msfr64, sizeof(msfr64)); } else { error = sooptcopyin(sopt, &msfr32, sizeof(struct __msfilterreq32), sizeof(struct __msfilterreq32)); - if (error) - return (error); + if (error) { + return error; + } /* we never use msfr.msfr_srcs; */ - memcpy(&msfr, &msfr32, sizeof(msfr)); + memcpy(&msfr, &msfr32, sizeof(msfr32)); } if ((size_t) msfr.msfr_nsrcs > - UINT32_MAX / sizeof(struct sockaddr_storage)) + UINT32_MAX / sizeof(struct sockaddr_storage)) { msfr.msfr_nsrcs = UINT32_MAX / sizeof(struct sockaddr_storage); + } - if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc) - return (ENOBUFS); + if (msfr.msfr_nsrcs > in6_mcast_maxsocksrc) { + return ENOBUFS; + } if (msfr.msfr_fmode != MCAST_EXCLUDE && - msfr.msfr_fmode != MCAST_INCLUDE) - return (EINVAL); + msfr.msfr_fmode != MCAST_INCLUDE) { + return EINVAL; + } if (msfr.msfr_group.ss_family != AF_INET6 || - msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6)) - return (EINVAL); + msfr.msfr_group.ss_len != sizeof(struct sockaddr_in6)) { + return EINVAL; + } - gsa = (sockunion_t *)&msfr.msfr_group; - if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6.sin6_addr)) - return (EINVAL); + gsa = (struct sockaddr_in6 *)&msfr.msfr_group; + if (!IN6_IS_ADDR_MULTICAST(&gsa->sin6_addr)) { + return EINVAL; + } - gsa->sin6.sin6_port = 0; /* ignore port */ + gsa->sin6_port = 0; /* ignore port */ ifnet_head_lock_shared(); if (msfr.msfr_ifindex == 0 || (u_int)if_index < msfr.msfr_ifindex) { ifnet_head_done(); - return (EADDRNOTAVAIL); + return EADDRNOTAVAIL; } ifp = ifindex2ifnet[msfr.msfr_ifindex]; ifnet_head_done(); - if (ifp == NULL) - return (EADDRNOTAVAIL); + if (ifp == NULL) { + return EADDRNOTAVAIL; + } - (void)in6_setscope(&gsa->sin6.sin6_addr, ifp, NULL); + (void)in6_setscope(&gsa->sin6_addr, ifp, NULL); /* * Take the INP write lock. * Check if this socket is a member of this group. */ imo = in6p_findmoptions(inp); - if (imo == NULL) - return (ENOMEM); + if (imo == NULL) { + return ENOMEM; + } IM6O_LOCK(imo); - idx = im6o_match_group(imo, ifp, &gsa->sa); + idx = im6o_match_group(imo, ifp, gsa); if (idx == (size_t)-1 || imo->im6o_mfilters == NULL) { error = EADDRNOTAVAIL; goto out_imo_locked; @@ -2724,7 +2807,7 @@ in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt) * Begin state merge transaction at socket layer. */ - imf->im6f_st[1] = msfr.msfr_fmode; + imf->im6f_st[1] = (uint8_t)msfr.msfr_fmode; /* * Apply any new source filters, if present. @@ -2733,15 +2816,16 @@ in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt) * allows us to deal with page faults up-front. */ if (msfr.msfr_nsrcs > 0) { - struct in6_msource *lims; - struct sockaddr_in6 *psin; - struct sockaddr_storage *kss, *pkss; - unsigned int i; - - if (IS_64BIT_PROCESS(current_proc())) - tmp_ptr = msfr64.msfr_srcs; - else + struct in6_msource *lims; + struct sockaddr_in6 *psin; + struct sockaddr_storage *kss, *pkss; + unsigned int i; + + if (IS_64BIT_PROCESS(current_proc())) { + tmp_ptr = (user_addr_t)msfr64.msfr_srcs; + } else { tmp_ptr = CAST_USER_ADDR_T(msfr32.msfr_srcs); + } MLD_PRINTF(("%s: loading %lu source list entries\n", __func__, (unsigned long)msfr.msfr_nsrcs)); @@ -2765,7 +2849,7 @@ in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt) * will set it to INCLUDE. */ im6f_leave(imf); - imf->im6f_st[1] = msfr.msfr_fmode; + imf->im6f_st[1] = (uint8_t)msfr.msfr_fmode; /* * Update socket layer filters at t1, lazy-allocating @@ -2799,15 +2883,17 @@ in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt) */ in6_clearscope(&psin->sin6_addr); error = im6f_get_source(imf, psin, &lims); - if (error) + if (error) { break; + } lims->im6sl_st[1] = imf->im6f_st[1]; } FREE(kss, M_TEMP); } - if (error) + if (error) { goto out_im6f_rollback; + } /* * Begin state merge transaction at MLD layer. @@ -2825,26 +2911,28 @@ in6p_set_source_filters(struct inpcb *inp, struct sockopt *sopt) error = mld_change_state(inm, &mtp, 0); IN6M_UNLOCK(inm); #if MLD_DEBUG - if (error) + if (error) { MLD_PRINTF(("%s: failed mld downcall\n", __func__)); + } #endif out_im6f_rollback: - if (error) + if (error) { im6f_rollback(imf); - else + } else { im6f_commit(imf); + } im6f_reap(imf); out_imo_locked: IM6O_UNLOCK(imo); - IM6O_REMREF(imo); /* from in6p_findmoptions() */ + IM6O_REMREF(imo); /* from in6p_findmoptions() */ /* schedule timer now that we've dropped the lock(s) */ mld_set_timeout(&mtp); - return (error); + return error; } /* @@ -2860,8 +2948,8 @@ out_imo_locked: int ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt) { - struct ip6_moptions *im6o; - int error; + struct ip6_moptions *im6o; + int error; error = 0; @@ -2871,8 +2959,9 @@ ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt) */ if (SOCK_PROTO(inp->inp_socket) == IPPROTO_DIVERT || (SOCK_TYPE(inp->inp_socket) != SOCK_RAW && - SOCK_TYPE(inp->inp_socket) != SOCK_DGRAM)) - return (EOPNOTSUPP); + SOCK_TYPE(inp->inp_socket) != SOCK_DGRAM)) { + return EOPNOTSUPP; + } switch (sopt->sopt_name) { case IPV6_MULTICAST_IF: @@ -2887,9 +2976,10 @@ ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt) break; } error = sooptcopyin(sopt, &hlim, sizeof(hlim), sizeof(int)); - if (error) + if (error) { break; - if (hlim < -1 || hlim > 255) { + } + if (hlim < -1 || hlim > IPV6_MAXHLIM) { error = EINVAL; break; } else if (hlim == -1) { @@ -2901,9 +2991,9 @@ ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt) break; } IM6O_LOCK(im6o); - im6o->im6o_multicast_hlim = hlim; + im6o->im6o_multicast_hlim = (u_char)hlim; IM6O_UNLOCK(im6o); - IM6O_REMREF(im6o); /* from in6p_findmoptions() */ + IM6O_REMREF(im6o); /* from in6p_findmoptions() */ break; } @@ -2919,8 +3009,9 @@ ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt) break; } error = sooptcopyin(sopt, &loop, sizeof(u_int), sizeof(u_int)); - if (error) + if (error) { break; + } if (loop > 1) { error = EINVAL; break; @@ -2931,9 +3022,9 @@ ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt) break; } IM6O_LOCK(im6o); - im6o->im6o_multicast_loop = loop; + im6o->im6o_multicast_loop = (u_char)loop; IM6O_UNLOCK(im6o); - IM6O_REMREF(im6o); /* from in6p_findmoptions() */ + IM6O_REMREF(im6o); /* from in6p_findmoptions() */ break; } @@ -2963,7 +3054,7 @@ ip6_setmoptions(struct inpcb *inp, struct sockopt *sopt) break; } - return (error); + return error; } /* * Expose MLD's multicast filter mode and source list(s) to userland, @@ -2977,26 +3068,28 @@ sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS { #pragma unused(oidp) - struct in6_addr mcaddr; - struct in6_addr src; - struct ifnet *ifp; - struct in6_multi *inm; - struct in6_multistep step; - struct ip6_msource *ims; - int *name; - int retval = 0; - u_int namelen; - uint32_t fmode, ifindex; + struct in6_addr mcaddr; + struct in6_addr src; + struct ifnet *ifp; + struct in6_multi *inm; + struct in6_multistep step; + struct ip6_msource *ims; + int *name; + int retval = 0; + u_int namelen; + uint32_t fmode, ifindex; name = (int *)arg1; namelen = arg2; - if (req->newptr != USER_ADDR_NULL) - return (EPERM); + if (req->newptr != USER_ADDR_NULL) { + return EPERM; + } /* int: ifindex + 4 * 32 bits of IPv6 address */ - if (namelen != 5) - return (EINVAL); + if (namelen != 5) { + return EINVAL; + } ifindex = name[0]; ifnet_head_lock_shared(); @@ -3004,7 +3097,7 @@ sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS MLD_PRINTF(("%s: ifindex %u out of range\n", __func__, ifindex)); ifnet_head_done(); - return (ENOENT); + return ENOENT; } memcpy(&mcaddr, &name[1], sizeof(struct in6_addr)); @@ -3012,14 +3105,14 @@ sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS MLD_PRINTF(("%s: group %s is not multicast\n", __func__, ip6_sprintf(&mcaddr))); ifnet_head_done(); - return (EINVAL); + return EINVAL; } ifp = ifindex2ifnet[ifindex]; ifnet_head_done(); if (ifp == NULL) { MLD_PRINTF(("%s: no ifp for ifindex %u\n", __func__, ifindex)); - return (ENOENT); + return ENOENT; } /* * Internal MLD lookups require that scope/zone ID is set. @@ -3030,17 +3123,19 @@ sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS IN6_FIRST_MULTI(step, inm); while (inm != NULL) { IN6M_LOCK(inm); - if (inm->in6m_ifp != ifp) + if (inm->in6m_ifp != ifp) { goto next; + } - if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr)) + if (!IN6_ARE_ADDR_EQUAL(&inm->in6m_addr, &mcaddr)) { goto next; + } fmode = inm->in6m_st[1].iss_fmode; retval = SYSCTL_OUT(req, &fmode, sizeof(uint32_t)); if (retval != 0) { IN6M_UNLOCK(inm); - break; /* abort */ + break; /* abort */ } RB_FOREACH(ims, ip6_msource_tree, &inm->in6m_srcs) { MLD_PRINTF(("%s: visit node 0x%llx\n", __func__, @@ -3055,8 +3150,9 @@ sysctl_ip6_mcast_filters SYSCTL_HANDLER_ARGS } src = ims->im6s_addr; retval = SYSCTL_OUT(req, &src, sizeof(struct in6_addr)); - if (retval != 0) - break; /* process next inm */ + if (retval != 0) { + break; /* process next inm */ + } } next: IN6M_UNLOCK(inm); @@ -3064,13 +3160,13 @@ next: } in6_multihead_lock_done(); - return (retval); + return retval; } void in6_multi_init(void) { - PE_parse_boot_argn("ifa_debug", &in6m_debug, sizeof (in6m_debug)); + PE_parse_boot_argn("ifa_debug", &in6m_debug, sizeof(in6m_debug)); /* Setup lock group and attribute for in6_multihead */ in6_multihead_lock_grp_attr = lck_grp_attr_alloc_init(); @@ -3084,52 +3180,18 @@ in6_multi_init(void) in6_multihead_lock_attr); TAILQ_INIT(&in6m_trash_head); - in6m_size = (in6m_debug == 0) ? sizeof (struct in6_multi) : - sizeof (struct in6_multi_dbg); - in6m_zone = zinit(in6m_size, IN6M_ZONE_MAX * in6m_size, - 0, IN6M_ZONE_NAME); - if (in6m_zone == NULL) { - panic("%s: failed allocating %s", __func__, IN6M_ZONE_NAME); - /* NOTREACHED */ - } - zone_change(in6m_zone, Z_EXPAND, TRUE); - - imm_size = sizeof (struct in6_multi_mship); - imm_zone = zinit(imm_size, IMM_ZONE_MAX * imm_size, 0, IMM_ZONE_NAME); - if (imm_zone == NULL) { - panic("%s: failed allocating %s", __func__, IMM_ZONE_NAME); - /* NOTREACHED */ - } - zone_change(imm_zone, Z_EXPAND, TRUE); - - ip6ms_size = sizeof (struct ip6_msource); - ip6ms_zone = zinit(ip6ms_size, IP6MS_ZONE_MAX * ip6ms_size, - 0, IP6MS_ZONE_NAME); - if (ip6ms_zone == NULL) { - panic("%s: failed allocating %s", __func__, IP6MS_ZONE_NAME); - /* NOTREACHED */ - } - zone_change(ip6ms_zone, Z_EXPAND, TRUE); - - in6ms_size = sizeof (struct in6_msource); - in6ms_zone = zinit(in6ms_size, IN6MS_ZONE_MAX * in6ms_size, - 0, IN6MS_ZONE_NAME); - if (in6ms_zone == NULL) { - panic("%s: failed allocating %s", __func__, IN6MS_ZONE_NAME); - /* NOTREACHED */ - } - zone_change(in6ms_zone, Z_EXPAND, TRUE); + vm_size_t in6m_size = (in6m_debug == 0) ? sizeof(struct in6_multi) : + sizeof(struct in6_multi_dbg); + in6m_zone = zone_create(IN6M_ZONE_NAME, in6m_size, ZC_ZFREE_CLEARMEM); } static struct in6_multi * -in6_multi_alloc(int how) +in6_multi_alloc(zalloc_flags_t how) { struct in6_multi *in6m; - in6m = (how == M_WAITOK) ? zalloc(in6m_zone) : - zalloc_noblock(in6m_zone); + in6m = zalloc_flags(in6m_zone, how | Z_ZERO); if (in6m != NULL) { - bzero(in6m, in6m_size); lck_mtx_init(&in6m->in6m_lock, in6_multihead_lock_grp, in6_multihead_lock_attr); in6m->in6m_debug |= IFD_ALLOC; @@ -3138,7 +3200,7 @@ in6_multi_alloc(int how) in6m->in6m_trace = in6m_trace; } } - return (in6m); + return in6m; } static void @@ -3190,10 +3252,6 @@ in6_multi_attach(struct in6_multi *in6m) panic("%s: Attempt to attach an already attached in6m=%p", __func__, in6m); /* NOTREACHED */ - } else if (in6m->in6m_debug & IFD_TRASHED) { - panic("%s: Attempt to reattach a detached in6m=%p", - __func__, in6m); - /* NOTREACHED */ } in6m->in6m_reqcnt++; @@ -3230,8 +3288,9 @@ in6_multi_detach(struct in6_multi *in6m) } --in6m->in6m_reqcnt; - if (in6m->in6m_reqcnt > 0) - return (0); + if (in6m->in6m_reqcnt > 0) { + return 0; + } if (!(in6m->in6m_debug & IFD_ATTACHED)) { panic("%s: Attempt to detach an unattached record in6m=%p", @@ -3258,16 +3317,17 @@ in6_multi_detach(struct in6_multi *in6m) in6m->in6m_debug |= IFD_TRASHED; } - return (1); + return 1; } void in6m_addref(struct in6_multi *in6m, int locked) { - if (!locked) + if (!locked) { IN6M_LOCK_SPIN(in6m); - else + } else { IN6M_LOCK_ASSERT_HELD(in6m); + } if (++in6m->in6m_refcount == 0) { panic("%s: in6m=%p wraparound refcnt", __func__, in6m); @@ -3275,8 +3335,9 @@ in6m_addref(struct in6_multi *in6m, int locked) } else if (in6m->in6m_trace != NULL) { (*in6m->in6m_trace)(in6m, TRUE); } - if (!locked) + if (!locked) { IN6M_UNLOCK(in6m); + } } void @@ -3285,10 +3346,11 @@ in6m_remref(struct in6_multi *in6m, int locked) struct ifmultiaddr *ifma; struct mld_ifinfo *mli; - if (!locked) + if (!locked) { IN6M_LOCK_SPIN(in6m); - else + } else { IN6M_LOCK_ASSERT_HELD(in6m); + } if (in6m->in6m_refcount == 0 || (in6m->in6m_refcount == 1 && locked)) { panic("%s: in6m=%p negative refcnt", __func__, in6m); @@ -3299,8 +3361,9 @@ in6m_remref(struct in6_multi *in6m, int locked) --in6m->in6m_refcount; if (in6m->in6m_refcount > 0) { - if (!locked) + if (!locked) { IN6M_UNLOCK(in6m); + } return; } @@ -3325,8 +3388,9 @@ in6m_remref(struct in6_multi *in6m, int locked) IN6M_UNLOCK(in6m); in6_multihead_lock_done(); /* If it was locked, return it as such */ - if (locked) + if (locked) { IN6M_LOCK(in6m); + } return; } in6m_purge(in6m); @@ -3346,8 +3410,9 @@ in6m_remref(struct in6_multi *in6m, int locked) /* Release reference held to the underlying ifmultiaddr */ IFMA_REMREF(ifma); - if (mli != NULL) + if (mli != NULL) { MLI_REMREF(mli); + } } static void @@ -3375,15 +3440,9 @@ in6m_trace(struct in6_multi *in6m, int refhold) } static struct in6_multi_mship * -in6_multi_mship_alloc(int how) +in6_multi_mship_alloc(zalloc_flags_t how) { - struct in6_multi_mship *imm; - - imm = (how == M_WAITOK) ? zalloc(imm_zone) : zalloc_noblock(imm_zone); - if (imm != NULL) - bzero(imm, imm_size); - - return (imm); + return zalloc_flags(imm_zone, how | Z_ZERO); } static void @@ -3411,7 +3470,10 @@ in6_multihead_lock_shared(void) void in6_multihead_lock_assert(int what) { - lck_rw_assert(&in6_multihead_lock, what); +#if !MACH_ASSERT +#pragma unused(what) +#endif + LCK_RW_ASSERT(&in6_multihead_lock, what); } void @@ -3421,16 +3483,9 @@ in6_multihead_lock_done(void) } static struct ip6_msource * -ip6ms_alloc(int how) +ip6ms_alloc(zalloc_flags_t how) { - struct ip6_msource *i6ms; - - i6ms = (how == M_WAITOK) ? zalloc(ip6ms_zone) : - zalloc_noblock(ip6ms_zone); - if (i6ms != NULL) - bzero(i6ms, ip6ms_size); - - return (i6ms); + return zalloc_flags(ip6ms_zone, how | Z_ZERO); } static void @@ -3440,16 +3495,9 @@ ip6ms_free(struct ip6_msource *i6ms) } static struct in6_msource * -in6ms_alloc(int how) +in6ms_alloc(zalloc_flags_t how) { - struct in6_msource *in6ms; - - in6ms = (how == M_WAITOK) ? zalloc(in6ms_zone) : - zalloc_noblock(in6ms_zone); - if (in6ms != NULL) - bzero(in6ms, in6ms_size); - - return (in6ms); + return zalloc_flags(in6ms_zone, how | Z_ZERO); } static void @@ -3465,9 +3513,10 @@ static const char *in6m_modestrs[] = { "un\n", "in", "ex" }; static const char * in6m_mode_str(const int mode) { - if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE) - return (in6m_modestrs[mode]); - return ("??"); + if (mode >= MCAST_UNDEFINED && mode <= MCAST_EXCLUDE) { + return in6m_modestrs[mode]; + } + return "??"; } static const char *in6m_statestrs[] = { @@ -3486,9 +3535,10 @@ static const char *in6m_statestrs[] = { static const char * in6m_state_str(const int state) { - if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER) - return (in6m_statestrs[state]); - return ("??"); + if (state >= MLD_NOT_MEMBER && state <= MLD_LEAVING_MEMBER) { + return in6m_statestrs[state]; + } + return "??"; } /* @@ -3501,8 +3551,9 @@ in6m_print(const struct in6_multi *inm) IN6M_LOCK_ASSERT_HELD(__DECONST(struct in6_multi *, inm)); - if (mld_debug == 0) + if (mld_debug == 0) { return; + } printf("%s: --- begin in6m 0x%llx ---\n", __func__, (uint64_t)VM_KERNEL_ADDRPERM(inm)); @@ -3533,12 +3584,11 @@ in6m_print(const struct in6_multi *inm) (uint64_t)VM_KERNEL_ADDRPERM(inm)); } -#else +#else void in6m_print(__unused const struct in6_multi *inm) { - } #endif