www.pudn.com > vxworks_tcpip_code.rar > igmp.c
/* igmp.c - internet group management protocol routines */
/* Copyright 1984-1996 Wind River Systems, Inc. */
/*
* Copyright (c) 1988 Stephen Deering.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Stephen Deering of Stanford University.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)igmp.c 8.2 (Berkeley) 5/3/95
*/
/*
modification history
--------------------
01e,05oct97,vin changes for multicast hashing.
01d,08mar97,vin added mCastRouteFwdHook to access mcast routing code.
01c,31jan97,vin changed declaration according to prototype decl in protosw.h
01b,11nov96,vin added cluster support, changed m_gethdr mHdlClGet.
01a,03mar96,vin created from BSD4.4 stuff.
*/
/* Internet Group Management Protocol (IGMP) routines. */
#include "vxWorks.h"
#include "errno.h"
#include "net/mbuf.h"
#include "sys/socket.h"
#include "net/protosw.h"
#include "net/if.h"
#include "net/route.h"
#include "netinet/in.h"
#include "netinet/in_var.h"
#include "netinet/in_systm.h"
#include "netinet/ip.h"
#include "netinet/ip_var.h"
#include "netinet/igmp.h"
#include "netinet/igmp_var.h"
#include "net/systm.h"
/* externs */
extern struct ifnet loif;
extern VOIDFUNCPTR _igmpJoinGrpHook;
extern VOIDFUNCPTR _igmpLeaveGrpHook;
extern FUNCPTR _mCastRouteFwdHook; /* WRS mcast forward command hook */
/* globals */
struct igmpstat igmpstat;
static int igmp_timers_are_running = 0;
static u_long igmp_all_hosts_group;
static void igmp_sendreport (struct in_multi *);
static void igmp_joingroup (struct in_multi *inm);
static void igmp_leavegroup (struct in_multi *inm);
void
igmp_init()
{
/*
* To avoid byte-swapping the same value over and over again.
*/
igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
_igmpJoinGrpHook = igmp_joingroup;
_igmpLeaveGrpHook = igmp_leavegroup;
return;
}
void
igmp_input(m, iphlen)
register struct mbuf *m;
register int iphlen;
{
register struct igmp *igmp;
register struct ip *ip;
register int igmplen;
register struct ifnet *ifp = m->m_pkthdr.rcvif;
register int minlen;
register struct in_multi *inm;
register struct in_ifaddr *ia;
++igmpstat.igps_rcv_total;
ip = mtod(m, struct ip *);
igmplen = ip->ip_len;
/*
* Validate lengths
*/
if (igmplen < IGMP_MINLEN) {
++igmpstat.igps_rcv_tooshort;
m_freem(m);
return;
}
minlen = iphlen + IGMP_MINLEN;
if ((m->m_flags & M_EXT || m->m_len < minlen) &&
(m = m_pullup(m, minlen)) == 0) {
++igmpstat.igps_rcv_tooshort;
return;
}
/*
* Validate checksum
*/
m->m_data += iphlen;
m->m_len -= iphlen;
igmp = mtod(m, struct igmp *);
if (in_cksum(m, igmplen)) {
++igmpstat.igps_rcv_badsum;
m_freem(m);
return;
}
m->m_data -= iphlen;
m->m_len += iphlen;
ip = mtod(m, struct ip *);
switch (igmp->igmp_type) {
case IGMP_HOST_MEMBERSHIP_QUERY:
++igmpstat.igps_rcv_queries;
if (ifp == &loif)
break;
if (ip->ip_dst.s_addr != igmp_all_hosts_group) {
++igmpstat.igps_rcv_badqueries;
m_freem(m);
return;
}
{
IN_MULTI_HEAD * inMultiHead;
int ix;
for (ix = 0; ix <= mCastHashInfo.hashMask; ix++)
{
inMultiHead = &mCastHashInfo.hashBase [ix];
if (inMultiHead != NULL)
{
for (inm = inMultiHead->lh_first;
inm != NULL;
inm = inm->inm_hash.le_next)
{
if (inm->inm_ifp == ifp && inm->inm_timer == 0 &&
inm->inm_addr.s_addr != igmp_all_hosts_group)
{
inm->inm_timer =
IGMP_RANDOM_DELAY(inm->inm_addr);
igmp_timers_are_running = 1;
}
}
}
}
}
break;
case IGMP_HOST_MEMBERSHIP_REPORT:
++igmpstat.igps_rcv_reports;
if (ifp == &loif)
break;
if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr)) ||
igmp->igmp_group.s_addr != ip->ip_dst.s_addr) {
++igmpstat.igps_rcv_badreports;
m_freem(m);
return;
}
/*
* KLUDGE: if the IP source address of the report has an
* unspecified (i.e., zero) subnet number, as is allowed for
* a booting host, replace it with the correct subnet number
* so that a process-level multicast routing demon can
* determine which subnet it arrived from. This is necessary
* to compensate for the lack of any way for a process to
* determine the arrival interface of an incoming packet.
*/
if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
IFP_TO_IA(ifp, ia);
if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
}
/*
* If we belong to the group being reported, stop
* our timer for that group.
*/
IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
if (inm != NULL) {
inm->inm_timer = 0;
++igmpstat.igps_rcv_ourreports;
}
break;
}
/*
* Pass all valid IGMP packets up to any process(es) listening
* on a raw IGMP socket.
*/
rip_input(m);
return;
}
static void
igmp_joingroup(inm)
struct in_multi *inm;
{
register int s = splnet();
if (inm->inm_addr.s_addr == igmp_all_hosts_group ||
inm->inm_ifp == &loif)
inm->inm_timer = 0;
else {
igmp_sendreport(inm);
inm->inm_timer = IGMP_RANDOM_DELAY(inm->inm_addr);
igmp_timers_are_running = 1;
}
splx(s);
}
static void
igmp_leavegroup(inm)
struct in_multi *inm;
{
/*
* No action required on leaving a group.
*/
}
void
igmp_fasttimo()
{
register struct in_multi *inm;
register int s;
/*
* Quick check to see if any work needs to be done, in order
* to minimize the overhead of fasttimo processing.
*/
if (!igmp_timers_are_running)
return;
s = splnet();
igmp_timers_are_running = 0;
{
IN_MULTI_HEAD * inMultiHead;
int ix;
for (ix = 0; ix <= mCastHashInfo.hashMask; ix++)
{
inMultiHead = &mCastHashInfo.hashBase [ix];
if (inMultiHead != NULL)
{
for (inm = inMultiHead->lh_first;
inm != NULL;
inm = inm->inm_hash.le_next)
{
if (inm->inm_timer == 0)
{
/* do nothing */
}
else if (--inm->inm_timer == 0)
{
igmp_sendreport(inm);
}
else
{
igmp_timers_are_running = 1;
}
}
}
}
}
splx(s);
return;
}
static void
igmp_sendreport(inm)
register struct in_multi *inm;
{
register struct mbuf *m;
register struct igmp *igmp;
register struct ip *ip;
register struct ip_moptions *imo;
struct ip_moptions simo;
m= mHdrClGet(M_DONTWAIT, MT_HEADER, CL_SIZE_128, TRUE);
if (m == NULL)
return;
/*
* Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
* is smaller than mbuf size returned by MGETHDR.
*/
m->m_data += max_linkhdr;
m->m_len = sizeof(struct ip) + IGMP_MINLEN;
m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
ip = mtod(m, struct ip *);
ip->ip_tos = 0;
ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
ip->ip_off = 0;
ip->ip_p = IPPROTO_IGMP;
ip->ip_src.s_addr = INADDR_ANY;
ip->ip_dst = inm->inm_addr;
m->m_data += sizeof(struct ip);
m->m_len -= sizeof(struct ip);
igmp = mtod(m, struct igmp *);
igmp->igmp_type = IGMP_HOST_MEMBERSHIP_REPORT;
igmp->igmp_code = 0;
igmp->igmp_group = inm->inm_addr;
igmp->igmp_cksum = 0;
igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
m->m_data -= sizeof(struct ip);
m->m_len += sizeof(struct ip);
imo = &simo;
bzero((caddr_t)imo, sizeof(*imo));
imo->imo_multicast_ifp = inm->inm_ifp;
imo->imo_multicast_ttl = 1;
/*
* Request loopback of the report if we are acting as a multicast
* router, so that the process-level routing demon can hear it.
*/
imo->imo_multicast_loop = (_mCastRouteFwdHook != NULL);
ip_output(m, NULL, NULL, 0, imo);
++igmpstat.igps_snd_reports;
}