mld6.c

来自「eCos操作系统源码」· C语言 代码 · 共 522 行 · 第 1/2 页

C
522
字号
		if (ifp->if_flags & IFF_LOOPBACK)			break;		if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) &&		    !IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))			break;	/* print error or log stat? */		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))			mldh->mld_addr.s6_addr16[1] =				htons(ifp->if_index); /* XXX */		/*		 * - Start the timers in all of our membership records		 *   that the query applies to for the interface on		 *   which the query arrived excl. those that belong		 *   to the "all-nodes" group (ff02::1).		 * - Restart any timer that is already running but has		 *   A value longer than the requested timeout.		 * - Use the value specified in the query message as		 *   the maximum timeout.		 */		IFP_TO_IA6(ifp, ia);		if (ia == NULL)			break;		/*		 * XXX: System timer resolution is too low to handle Max		 * Response Delay, so set 1 to the internal timer even if		 * the calculated value equals to zero when Max Response		 * Delay is positive.		 */		timer = ntohs(mldh->mld_maxdelay)*PR_FASTHZ/MLD6_TIMER_SCALE;		if (timer == 0 && mldh->mld_maxdelay)			timer = 1;		mld6_all_nodes_linklocal.s6_addr16[1] =			htons(ifp->if_index); /* XXX */		#if defined(__FreeBSD__) && __FreeBSD__ >= 3		LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)#else		for (in6m = ia->ia6_multiaddrs.lh_first;		     in6m;		     in6m = in6m->in6m_entry.le_next)#endif		{#if defined(__FreeBSD__) && __FreeBSD__ >= 3			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;#else			if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr,						&mld6_all_nodes_linklocal) ||			    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <			    IPV6_ADDR_SCOPE_LINKLOCAL)				continue;#endif			if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) ||			    IN6_ARE_ADDR_EQUAL(&mldh->mld_addr,						&in6m->in6m_addr))			{				if (timer == 0) {					/* send a report immediately */					mld6_sendpkt(in6m, MLD_LISTENER_REPORT,						NULL);					in6m->in6m_timer = 0; /* reset timer */					in6m->in6m_state = MLD6_IREPORTEDLAST;				}				else if (in6m->in6m_timer == 0 || /* idle */					in6m->in6m_timer > timer) {					in6m->in6m_timer =						MLD6_RANDOM_DELAY(timer);					mld6_timers_are_running = 1;				}			}		}		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))			mldh->mld_addr.s6_addr16[1] = 0; /* XXX */		break;	case MLD_LISTENER_REPORT:		/*		 * For fast leave to work, we have to know that we are the		 * last person to send a report for this group.  Reports		 * can potentially get looped back if we are a multicast		 * router, so discard reports sourced by me.		 * Note that it is impossible to check IFF_LOOPBACK flag of		 * ifp for this purpose, since ip6_mloopback pass the physical		 * interface to looutput.		 */		if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */			break;		if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))			break;		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))			mldh->mld_addr.s6_addr16[1] =				htons(ifp->if_index); /* XXX */		/*		 * If we belong to the group being reported, stop		 * our timer for that group.		 */		IN6_LOOKUP_MULTI(mldh->mld_addr, ifp, in6m);		if (in6m) {			in6m->in6m_timer = 0; /* transit to idle state */			in6m->in6m_state = MLD6_OTHERLISTENER; /* clear flag */		}		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))			mldh->mld_addr.s6_addr16[1] = 0; /* XXX */		break;	default:		/* this is impossible */		log(LOG_ERR, "mld6_input: illegal type(%d)", mldh->mld_type);		break;	}	m_freem(m);}voidmld6_fasttimeo(){	struct in6_multi *in6m;	struct in6_multistep step;	int s;	/*	 * Quick check to see if any work needs to be done, in order	 * to minimize the overhead of fasttimo processing.	 */	if (!mld6_timers_are_running)		return;#ifdef __NetBSD__	s = splsoftnet();#else	s = splnet();#endif	mld6_timers_are_running = 0;	IN6_FIRST_MULTI(step, in6m);	while (in6m != NULL) {		if (in6m->in6m_timer == 0) {			/* do nothing */		} else if (--in6m->in6m_timer == 0) {			mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);			in6m->in6m_state = MLD6_IREPORTEDLAST;		} else {			mld6_timers_are_running = 1;		}		IN6_NEXT_MULTI(step, in6m);	}	splx(s);}static voidmld6_sendpkt(in6m, type, dst)	struct in6_multi *in6m;	int type;	const struct in6_addr *dst;{	struct mbuf *mh, *md;	struct mld_hdr *mldh;	struct ip6_hdr *ip6;	struct ip6_moptions im6o;	struct in6_ifaddr *ia;	struct ifnet *ifp = in6m->in6m_ifp;	int ignflags;	/*	 * At first, find a link local address on the outgoing interface	 * to use as the source address of the MLD packet.	 * We do not reject tentative addresses for MLD report to deal with	 * the case where we first join a link-local address.	 */	ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;	if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)		return;	if ((ia->ia6_flags & IN6_IFF_TENTATIVE))		ia = NULL;	/*	 * Allocate mbufs to store ip6 header and MLD header.	 * We allocate 2 mbufs and make chain in advance because	 * it is more convenient when inserting the hop-by-hop option later.	 */	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 mld_hdr);	mh->m_len = sizeof(struct ip6_hdr);	MH_ALIGN(mh, sizeof(struct ip6_hdr));	/* fill in the ip6 header */	ip6 = mtod(mh, struct ip6_hdr *);	ip6->ip6_flow = 0;	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;	ip6->ip6_vfc |= IPV6_VERSION;	/* ip6_plen will be set later */	ip6->ip6_nxt = IPPROTO_ICMPV6;	/* ip6_hlim will be set by im6o.im6o_multicast_hlim */	ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;	ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;	/* fill in the MLD header */	md->m_len = sizeof(struct mld_hdr);	mldh = mtod(md, struct mld_hdr *);	mldh->mld_type = type;	mldh->mld_code = 0;	mldh->mld_cksum = 0;	/* XXX: we assume the function will not be called for query messages */	mldh->mld_maxdelay = 0;	mldh->mld_reserved = 0;	mldh->mld_addr = in6m->in6m_addr;	if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))		mldh->mld_addr.s6_addr16[1] = 0; /* XXX */	mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6,				     sizeof(struct ip6_hdr),				     sizeof(struct mld_hdr));	/* construct multicast option */	bzero(&im6o, sizeof(im6o));	im6o.im6o_multicast_ifp = ifp;	im6o.im6o_multicast_hlim = 1;	/*	 * Request loopback of the report if we are acting as a multicast	 * router, so that the process-level routing daemon can hear it.	 */	im6o.im6o_multicast_loop = (ip6_mrouter != NULL);	/* increment output statictics */	icmp6stat.icp6s_outhist[type]++;	icmp6_ifstat_inc(ifp, ifs6_out_msg);	switch (type) {	case MLD_LISTENER_QUERY:		icmp6_ifstat_inc(ifp, ifs6_out_mldquery);		break;	case MLD_LISTENER_REPORT:		icmp6_ifstat_inc(ifp, ifs6_out_mldreport);		break;	case MLD_LISTENER_DONE:		icmp6_ifstat_inc(ifp, ifs6_out_mlddone);		break;	}	ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC, &im6o, NULL);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?