mld6.c

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

C
522
字号
//==========================================================================////      src/sys/netinet6/mld6.c////==========================================================================//####BSDCOPYRIGHTBEGIN####//// -------------------------------------------//// Portions of this software may have been derived from OpenBSD, // FreeBSD or other sources, and are covered by the appropriate// copyright disclaimers included herein.//// Portions created by Red Hat are// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.//// -------------------------------------------////####BSDCOPYRIGHTEND####//==========================================================================/*	$KAME: mld6.c,v 1.35 2001/12/18 02:23:44 itojun Exp $	*//* * Copyright (C) 1998 WIDE Project. * All rights reserved. * * 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. Neither the name of the project 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 PROJECT 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 PROJECT 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. *//* * 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.1 (Berkeley) 7/19/93 */#include <sys/param.h>#include <sys/mbuf.h>#include <sys/socket.h>#include <sys/protosw.h>#ifdef __OpenBSD__#include <dev/rndvar.h>#endif#include <net/if.h>#ifdef NEW_STRUCT_ROUTE#include <net/route.h>#endif#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>/* * Protocol constants *//* denotes that the MLD max response delay field specifies time in milliseconds */#define MLD6_TIMER_SCALE	1000/* * time between repetitions of a node's initial report of interest in a * multicast address(in seconds) */#define MLD6_UNSOLICITED_REPORT_INTERVAL	10static struct ip6_pktopts ip6_opts;static int mld6_timers_are_running;/* XXX: These are necessary for KAME's link-local hack */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 __P((struct in6_multi *, int, const struct in6_addr *));voidmld6_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);	mld6_timers_are_running = 0;	/* ip6h_nxt will be fill in later */	hbh->ip6h_len = 0;	/* (8 >> 3) - 1 */	/* XXX: grotty hard coding... */	hbh_buf[2] = IP6OPT_PADN;	/* 2 byte padding */	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;}voidmld6_start_listening(in6m)	struct in6_multi *in6m;{#ifdef __NetBSD__	int s = splsoftnet();#else	int s = splnet();#endif	/*	 * RFC2710 page 10:	 * The node never sends a Report or Done for the link-scope all-nodes	 * address.	 * MLD messages are never sent for multicast addresses whose scope is 0	 * (reserved) or 1 (node-local).	 */	mld6_all_nodes_linklocal.s6_addr16[1] =		htons(in6m->in6m_ifp->if_index); /* XXX */	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, MLD_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);}voidmld6_stop_listening(in6m)	struct in6_multi *in6m;{	mld6_all_nodes_linklocal.s6_addr16[1] =		htons(in6m->in6m_ifp->if_index); /* XXX */	mld6_all_routers_linklocal.s6_addr16[1] =		htons(in6m->in6m_ifp->if_index); /* XXX: necessary when mrouting */	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_INTFACELOCAL)		mld6_sendpkt(in6m, MLD_LISTENER_DONE,			     &mld6_all_routers_linklocal);}voidmld6_input(m, off)	struct mbuf *m;	int off;{	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);	struct mld_hdr *mldh;	struct ifnet *ifp = m->m_pkthdr.rcvif;	struct in6_multi *in6m;	struct in6_ifaddr *ia;#if defined(__FreeBSD__) && __FreeBSD__ >= 3	struct ifmultiaddr *ifma;#endif	int timer;		/* timer value in the MLD query header */#ifndef PULLDOWN_TEST	IP6_EXTHDR_CHECK(m, off, sizeof(*mldh),);	mldh = (struct mld_hdr *)(mtod(m, caddr_t) + off);#else	IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));	if (mldh == NULL) {		icmp6stat.icp6s_tooshort++;		return;	}#endif	/* source address validation */	ip6 = mtod(m, struct ip6_hdr *); /* in case mpullup */	if (!(IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src) ||	      IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src))) {#if 0 /* do not log in an input path */		log(LOG_INFO,		    "mld6_input: src %s is not link-local (grp=%s)\n",		    ip6_sprintf(&ip6->ip6_src),		    ip6_sprintf(&mldh->mld_addr));#endif		/*		 * spec (RFC2710) does not explicitly		 * specify to discard the packet from a non link-local		 * source address. But we believe it's expected to do so.		 */		m_freem(m);		return;	}	/*	 * In the MLD6 specification, there are 3 states and a flag.	 *	 * In Non-Listener state, we simply don't have a membership record.	 * In Delaying Listener state, our timer is running (in6m->in6m_timer)	 * In Idle Listener state, our timer is not running (in6m->in6m_timer==0)	 *	 * The flag is in6m->in6m_state, it is set to MLD6_OTHERLISTENER if	 * we have heard a report from another member, or MLD6_IREPORTEDLAST	 * if we sent the last report.	 */	switch (mldh->mld_type) {	case MLD_LISTENER_QUERY:

⌨️ 快捷键说明

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