📄 in6.c
字号:
//==========================================================================//// src/sys/netinet6/in6.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: in6.c,v 1.256 2001/12/24 10:39:29 jinmei Exp $ *//* * Copyright (C) 1995, 1996, 1997, and 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) 1982, 1986, 1991, 1993 * The Regents of the University of California. 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. 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. * * @(#)in.c 8.2 (Berkeley) 11/15/93 */#include <sys/param.h>#include <sys/errno.h>#include <sys/malloc.h>#include <sys/socket.h>#include <sys/socketvar.h>#include <sys/sockio.h>#include <net/if.h>#include <net/if_types.h>#include <net/route.h>#include <net/if_dl.h>#include <netinet/in.h>#include <netinet/in_var.h>#ifdef __NetBSD__#include <net/if_ether.h>#else#include <netinet/if_ether.h>#endif#ifndef SCOPEDROUTING#if defined(__OpenBSD__) || (defined(__bsdi__) && _BSDI_VERSION >= 199802) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)#include <netinet/in_systm.h>#include <netinet/ip.h>#include <netinet/in_pcb.h>#endif#endif#include <netinet/ip6.h>#include <netinet6/ip6_var.h>#include <netinet6/nd6.h>#include <netinet6/mld6_var.h>#include <netinet6/ip6_mroute.h>#include <netinet6/in6_ifattach.h>#include <netinet6/scope6_var.h>#ifndef SCOPEDROUTING#if defined(__NetBSD__) || (defined(__bsdi__) && _BSDI_VERSION < 199802) || defined(__FreeBSD__)#include <netinet6/in6_pcb.h>#endif#endif#ifdef MIP6#include <netinet6/mip6.h>#endif /* MIP6 *//* * Definitions of some costant IP6 addresses. */const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;const struct in6_addr in6addr_nodelocal_allnodes = IN6ADDR_NODELOCAL_ALLNODES_INIT;const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;const struct in6_addr in6mask0 = IN6MASK0;const struct in6_addr in6mask32 = IN6MASK32;const struct in6_addr in6mask64 = IN6MASK64;const struct in6_addr in6mask96 = IN6MASK96;const struct in6_addr in6mask128 = IN6MASK128;const struct sockaddr_in6 sa6_any = {sizeof(sa6_any), AF_INET6, 0, 0, IN6ADDR_ANY_INIT, 0};#if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3)static int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, struct ifnet *, struct proc *));#elsestatic int in6_lifaddr_ioctl __P((struct socket *, u_long, caddr_t, struct ifnet *));#endifstatic int in6_ifinit __P((struct ifnet *, struct in6_ifaddr *, struct sockaddr_in6 *, int));static void in6_unlink_ifa __P((struct in6_ifaddr *, struct ifnet *));#if defined(__FreeBSD__) && __FreeBSD__ >= 3struct in6_multihead in6_multihead; /* XXX BSS initialization */#else/* * This structure is used to keep track of in6_multi chains which belong to * deleted interface addresses. */static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */struct multi6_kludge { LIST_ENTRY(multi6_kludge) mk_entry; struct ifnet *mk_ifp; struct in6_multihead mk_head;};#endif#ifdef MEASURE_PERFORMANCEstatic void in6h_delifa __P((struct in6_ifaddr *));static void in6h_addhash __P((struct in6hash *));static void in6h_delhash __P((struct in6hash *));#endif/* * Subroutine for in6_ifaddloop() and in6_ifremloop(). * This routine does actual work. */static voidin6_ifloop_request(int cmd, struct ifaddr *ifa){ struct sockaddr_in6 all1_sa; struct rtentry *nrt = NULL; int e; bzero(&all1_sa, sizeof(all1_sa)); all1_sa.sin6_family = AF_INET6; all1_sa.sin6_len = sizeof(struct sockaddr_in6); all1_sa.sin6_addr = in6mask128; /* * We specify the address itself as the gateway, and set the * RTF_LLINFO flag, so that the corresponding host route would have * the flag, and thus applications that assume traditional behavior * would be happy. Note that we assume the caller of the function * (probably implicitly) set nd6_rtrequest() to ifa->ifa_rtrequest, * which changes the outgoing interface to the loopback interface. */ e = rtrequest(cmd, ifa->ifa_addr, ifa->ifa_addr, (struct sockaddr *)&all1_sa, RTF_UP|RTF_HOST|RTF_LLINFO, &nrt); if (e != 0) { log(LOG_ERR, "in6_ifloop_request: " "%s operation failed for %s (errno=%d)\n", cmd == RTM_ADD ? "ADD" : "DELETE", ip6_sprintf(&((struct in6_ifaddr *)ifa)->ia_addr.sin6_addr), e); } /* * Make sure rt_ifa be equal to IFA, the second argument of the * function. * We need this because when we refer to rt_ifa->ia6_flags in * ip6_input, we assume that the rt_ifa points to the address instead * of the loopback address. */ if (cmd == RTM_ADD && nrt && ifa != nrt->rt_ifa) { IFAFREE(nrt->rt_ifa); IFAREF(ifa); nrt->rt_ifa = ifa; } /* * Report the addition/removal of the address to the routing socket. * XXX: since we called rtinit for a p2p interface with a destination, * we end up reporting twice in such a case. Should we rather * omit the second report? */ if (nrt) { rt_newaddrmsg(cmd, ifa, e, nrt); if (cmd == RTM_DELETE) { if (nrt->rt_refcnt <= 0) { /* XXX: we should free the entry ourselves. */ nrt->rt_refcnt++; rtfree(nrt); } } else { /* the cmd must be RTM_ADD here */ nrt->rt_refcnt--; } }}/* * Add ownaddr as loopback rtentry. We previously add the route only if * necessary (ex. on a p2p link). However, since we now manage addresses * separately from prefixes, we should always add the route. We can't * rely on the cloning mechanism from the corresponding interface route * any more. */static voidin6_ifaddloop(struct ifaddr *ifa){ struct rtentry *rt; /* If there is no loopback entry, allocate one. */ rt = rtalloc1(ifa->ifa_addr, 0#ifdef __FreeBSD__ , 0#endif /* __FreeBSD__ */ ); if (rt == NULL || (rt->rt_flags & RTF_HOST) == 0 || (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) in6_ifloop_request(RTM_ADD, ifa); if (rt) rt->rt_refcnt--;}/* * Remove loopback rtentry of ownaddr generated by in6_ifaddloop(), * if it exists. */static voidin6_ifremloop(struct ifaddr *ifa){ struct in6_ifaddr *ia; struct rtentry *rt; int ia_count = 0; /* * Some of BSD variants do not remove cloned routes * from an interface direct route, when removing the direct route * (see comments in net/net_osdep.h). Even for variants that do remove * cloned routes, they could fail to remove the cloned routes when * we handle multple addresses that share a common prefix. * So, we should remove the route corresponding to the deleted address * regardless of the result of in6_is_ifloop_auto(). */ /* * Delete the entry only if exact one ifa exists. More than one ifa * can exist if we assign a same single address to multiple * (probably p2p) interfaces. * XXX: we should avoid such a configuration in IPv6... */ for (ia = in6_ifaddr; ia; ia = ia->ia_next) { if (IN6_ARE_ADDR_EQUAL(IFA_IN6(ifa), &ia->ia_addr.sin6_addr)) { ia_count++; if (ia_count > 1) break; } } if (ia_count == 1) { /* * Before deleting, check if a corresponding loopbacked host * route surely exists. With this check, we can avoid to * delete an interface direct route whose destination is same * as the address being removed. This can happen when remofing * a subnet-router anycast address on an interface attahced * to a shared medium. */ rt = rtalloc1(ifa->ifa_addr, 0#ifdef __FreeBSD__ , 0#endif /* __FreeBSD__ */ ); if (rt != NULL && (rt->rt_flags & RTF_HOST) != 0 && (rt->rt_ifp->if_flags & IFF_LOOPBACK) != 0) { rt->rt_refcnt--; in6_ifloop_request(RTM_DELETE, ifa); } }}intin6_ifindex2scopeid(idx) int idx;{ struct ifnet *ifp; struct ifaddr *ifa; struct sockaddr_in6 *sin6; if (idx < 0 || if_index < idx) return -1;#if defined(__FreeBSD__) && __FreeBSD__ >= 5 ifp = ifnet_byindex(idx);#else ifp = ifindex2ifnet[idx];#endif if (!ifp) return -1;#if defined(__bsdi__) || (defined(__FreeBSD__) && __FreeBSD__ < 3) for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)#elif defined(__FreeBSD__) && __FreeBSD__ >= 4 TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list)#else for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)#endif { if (ifa->ifa_addr->sa_family != AF_INET6) continue; sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; if (IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr)) return sin6->sin6_scope_id & 0xffff; } return -1;}intin6_mask2len(mask, lim0) struct in6_addr *mask; u_char *lim0;{ int x = 0, y; u_char *lim = lim0, *p; if (lim0 == NULL || lim0 - (u_char *)mask > sizeof(*mask)) /* ignore the scope_id part */ lim = (u_char *)mask + sizeof(*mask); for (p = (u_char *)mask; p < lim; x++, p++) { if (*p != 0xff) break; } y = 0; if (p < lim) { for (y = 0; y < 8; y++) { if ((*p & (0x80 >> y)) == 0) break; } } /* * when the limit pointer is given, do a stricter check on the * remaining bits. */ if (p < lim) { if (y != 0 && (*p & (0x00ff >> y)) != 0) return(-1); for (p = p + 1; p < lim; p++) if (*p != 0) return(-1); } return x * 8 + y;}#define ifa2ia6(ifa) ((struct in6_ifaddr *)(ifa))#define ia62ifa(ia6) (&((ia6)->ia_ifa))int#if !defined(__bsdi__) && !(defined(__FreeBSD__) && __FreeBSD__ < 3)in6_control(so, cmd, data, ifp, p) struct socket *so; u_long cmd; caddr_t data; struct ifnet *ifp; struct proc *p;#elsein6_control(so, cmd, data, ifp) struct socket *so; u_long cmd; caddr_t data; struct ifnet *ifp;#endif{ struct in6_ifreq *ifr = (struct in6_ifreq *)data; struct in6_ifaddr *ia = NULL; struct in6_aliasreq *ifra = (struct in6_aliasreq *)data; struct sockaddr_in6 *sa6;#if !(defined(__FreeBSD__) && __FreeBSD__ >= 3) time_t time_second = (time_t)time.tv_sec;#endif switch (cmd) { case SIOCGETSGCNT_IN6: case SIOCGETMIFCNT_IN6: return (mrt6_ioctl(cmd, data)); } switch(cmd) { case SIOCAADDRCTL_POLICY: case SIOCDADDRCTL_POLICY: return(in6_src_ioctl(cmd, data)); }#ifdef MIP6 switch (cmd) { case SIOCENABLEMN: case SIOCENABLEHA: return(mip6_ioctl(cmd, data)); case SIOCGBC: return(mip6_ioctl(cmd, data)); }#endif /* MIP6 */ if (ifp == NULL) return(EOPNOTSUPP); switch (cmd) { case SIOCSNDFLUSH_IN6: case SIOCSPFXFLUSH_IN6: case SIOCSRTRFLUSH_IN6: case SIOCSDEFIFACE_IN6: case SIOCSIFINFO_FLAGS: /* fall through */ case OSIOCGIFINFO_IN6: case SIOCGIFINFO_IN6: case SIOCGDRLST_IN6: case SIOCGPRLST_IN6: case SIOCGNBRINFO_IN6: case SIOCGDEFIFACE_IN6: return(nd6_ioctl(cmd, data, ifp)); } switch (cmd) { case SIOCSIFPREFIX_IN6: case SIOCDIFPREFIX_IN6: case SIOCAIFPREFIX_IN6:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -