#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <sys/protosw.h>
#include <sys/syslog.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <netinet/ip6.h>
#include <netinet6/ip6_var.h>
#include <netinet/icmp6.h>
#include <netinet6/mld6_var.h>
#include <net/net_osdep.h>
#define MLD6_TIMER_SCALE 1000
#define MLD6_UNSOLICITED_REPORT_INTERVAL 10
extern lck_mtx_t *nd6_mutex;
static struct ip6_pktopts ip6_opts;
static int mld6_timers_are_running;
static int mld6_init_done = 0 ;
static struct in6_addr mld6_all_nodes_linklocal = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
static struct in6_addr mld6_all_routers_linklocal = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
static void mld6_sendpkt(struct in6_multi *, int, const struct in6_addr *);
void
mld6_init()
{
static u_int8_t hbh_buf[8];
struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
if (mld6_init_done)
return;
mld6_init_done = 1;
mld6_timers_are_running = 0;
hbh->ip6h_len = 0;
hbh_buf[2] = IP6OPT_PADN;
hbh_buf[3] = 0;
hbh_buf[4] = IP6OPT_RTALERT;
hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
init_ip6pktopts(&ip6_opts);
ip6_opts.ip6po_hbh = hbh;
}
void
mld6_start_listening(
struct in6_multi *in6m)
{
int s = splnet();
mld6_all_nodes_linklocal.s6_addr16[1] =
htons(in6m->in6m_ifp->if_index);
if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld6_all_nodes_linklocal) ||
IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
in6m->in6m_timer = 0;
in6m->in6m_state = MLD6_OTHERLISTENER;
} else {
mld6_sendpkt(in6m, MLD6_LISTENER_REPORT, NULL);
in6m->in6m_timer = MLD6_RANDOM_DELAY(
MLD6_UNSOLICITED_REPORT_INTERVAL * PR_FASTHZ);
in6m->in6m_state = MLD6_IREPORTEDLAST;
mld6_timers_are_running = 1;
}
splx(s);
}
void
mld6_stop_listening(
struct in6_multi *in6m)
{
mld6_all_nodes_linklocal.s6_addr16[1] =
htons(in6m->in6m_ifp->if_index);
mld6_all_routers_linklocal.s6_addr16[1] =
htons(in6m->in6m_ifp->if_index);
if (in6m->in6m_state == MLD6_IREPORTEDLAST &&
(!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld6_all_nodes_linklocal)) &&
IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > IPV6_ADDR_SCOPE_NODELOCAL)
mld6_sendpkt(in6m, MLD6_LISTENER_DONE,
&mld6_all_routers_linklocal);
}
void
mld6_input(
struct mbuf *m,
int off)
{
struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
struct mld6_hdr *mldh;
struct ifnet *ifp = m->m_pkthdr.rcvif;
struct in6_multi *in6m;
struct in6_ifaddr *ia;
struct ifmultiaddr *ifma;
int timer;
#ifndef PULLDOWN_TEST
IP6_EXTHDR_CHECK(m, off, sizeof(*mldh), return);
mldh = (struct mld6_hdr *)(mtod(m, caddr_t) + off);
#else
IP6_EXTHDR_GET(mldh, struct mld6_hdr *, m, off, sizeof(*mldh));
if (mldh == NULL) {
icmp6stat.icp6s_tooshort++;
return;
}
#endif
ip6 = mtod(m, struct ip6_hdr *);
if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
log(LOG_ERR,
"mld6_input: src %s is not link-local (grp=%s)\n",
ip6_sprintf(&ip6->ip6_src),
ip6_sprintf(&mldh->mld6_addr));
m_freem(m);
return;
}
switch(mldh->mld6_type) {
case MLD6_LISTENER_QUERY:
if (ifp->if_flags & IFF_LOOPBACK)
break;
if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld6_addr) &&
!IN6_IS_ADDR_MULTICAST(&mldh->mld6_addr))
break;
if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
mldh->mld6_addr.s6_addr16[1] =
htons(ifp->if_index);
ifnet_lock_exclusive(ifp);
IFP_TO_IA6(ifp, ia);
if (ia == NULL)
break;
timer = ntohs(mldh->mld6_maxdelay)*PR_FASTHZ/MLD6_TIMER_SCALE;
if (timer == 0 && mldh->mld6_maxdelay)
timer = 1;
mld6_all_nodes_linklocal.s6_addr16[1] =
htons(ifp->if_index);
LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
{
if (ifma->ifma_addr->sa_family != AF_INET6)
continue;
in6m = (struct in6_multi *)ifma->ifma_protospec;
if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr,
&mld6_all_nodes_linklocal) ||
IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
IPV6_ADDR_SCOPE_LINKLOCAL)
continue;
if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld6_addr) ||
IN6_ARE_ADDR_EQUAL(&mldh->mld6_addr,
&in6m->in6m_addr))
{
if (timer == 0) {
mld6_sendpkt(in6m, MLD6_LISTENER_REPORT,
NULL);
in6m->in6m_timer = 0;
in6m->in6m_state = MLD6_IREPORTEDLAST;
}
else if (in6m->in6m_timer == 0 ||
in6m->in6m_timer > timer) {
in6m->in6m_timer =
MLD6_RANDOM_DELAY(timer);
mld6_timers_are_running = 1;
}
}
}
ifnet_lock_done(ifp);
if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
mldh->mld6_addr.s6_addr16[1] = 0;
break;
case MLD6_LISTENER_REPORT:
if (m->m_flags & M_LOOP)
break;
if (!IN6_IS_ADDR_MULTICAST(&mldh->mld6_addr))
break;
if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
mldh->mld6_addr.s6_addr16[1] =
htons(ifp->if_index);
ifnet_lock_shared(ifp);
IN6_LOOKUP_MULTI(mldh->mld6_addr, ifp, in6m);
if (in6m) {
in6m->in6m_timer = 0;
in6m->in6m_state = MLD6_OTHERLISTENER;
}
ifnet_lock_done(ifp);
if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
mldh->mld6_addr.s6_addr16[1] = 0;
break;
default:
log(LOG_ERR, "mld6_input: illegal type(%d)", mldh->mld6_type);
break;
}
m_freem(m);
}
void
mld6_fasttimeo()
{
struct in6_multi *in6m;
struct in6_multistep step;
if (!mld6_timers_are_running)
return;
lck_mtx_lock(nd6_mutex);
mld6_timers_are_running = 0;
IN6_FIRST_MULTI(step, in6m);
while (in6m != NULL) {
if (in6m->in6m_timer == 0) {
} else if (--in6m->in6m_timer == 0) {
mld6_sendpkt(in6m, MLD6_LISTENER_REPORT, NULL);
in6m->in6m_state = MLD6_IREPORTEDLAST;
} else {
mld6_timers_are_running = 1;
}
IN6_NEXT_MULTI(step, in6m);
}
lck_mtx_unlock(nd6_mutex);
}
static void
mld6_sendpkt(
struct in6_multi *in6m,
int type,
const struct in6_addr *dst)
{
struct mbuf *mh, *md;
struct mld6_hdr *mldh;
struct ip6_hdr *ip6;
struct ip6_moptions im6o;
struct in6_ifaddr *ia;
struct ifnet *ifp = in6m->in6m_ifp;
struct ifnet *outif = NULL;
if ((ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST))
== NULL)
return;
MGETHDR(mh, M_DONTWAIT, MT_HEADER);
if (mh == NULL)
return;
MGET(md, M_DONTWAIT, MT_DATA);
if (md == NULL) {
m_free(mh);
return;
}
mh->m_next = md;
mh->m_pkthdr.rcvif = NULL;
mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld6_hdr);
mh->m_len = sizeof(struct ip6_hdr);
MH_ALIGN(mh, sizeof(struct ip6_hdr));
ip6 = mtod(mh, struct ip6_hdr *);
ip6->ip6_flow = 0;
ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
ip6->ip6_vfc |= IPV6_VERSION;
ip6->ip6_nxt = IPPROTO_ICMPV6;
ip6->ip6_src = ia->ia_addr.sin6_addr;
ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
md->m_len = sizeof(struct mld6_hdr);
mldh = mtod(md, struct mld6_hdr *);
mldh->mld6_type = type;
mldh->mld6_code = 0;
mldh->mld6_cksum = 0;
mldh->mld6_maxdelay = 0;
mldh->mld6_reserved = 0;
mldh->mld6_addr = in6m->in6m_addr;
if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld6_addr))
mldh->mld6_addr.s6_addr16[1] = 0;
mldh->mld6_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
sizeof(struct mld6_hdr));
bzero(&im6o, sizeof(im6o));
im6o.im6o_multicast_ifp = ifp;
im6o.im6o_multicast_hlim = 1;
im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
icmp6stat.icp6s_outhist[type]++;
ip6_output(mh, &ip6_opts, NULL, 0, &im6o, &outif, 0);
if (outif) {
icmp6_ifstat_inc(outif, ifs6_out_msg);
switch (type) {
case MLD6_LISTENER_QUERY:
icmp6_ifstat_inc(outif, ifs6_out_mldquery);
break;
case MLD6_LISTENER_REPORT:
icmp6_ifstat_inc(outif, ifs6_out_mldreport);
break;
case MLD6_LISTENER_DONE:
icmp6_ifstat_inc(outif, ifs6_out_mlddone);
break;
}
}
}