📄 addrconf.c
字号:
/* * IPv6 Address [auto]configuration * Linux INET6 implementation * * Authors: * Pedro Roque <roque@di.fc.ul.pt> * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> * * $Id: addrconf.c,v 1.69 2001/10/31 21:55:54 davem Exp $ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. *//* * Changes: * * Janos Farkas : delete timer on ifdown * <chexum@bankinf.banki.hu> * Andi Kleen : kill doube kfree on module * unload. * Maciej W. Rozycki : FDDI support * sekiya@USAGI : Don't send too many RS * packets. * yoshfuji@USAGI : Fixed interval between DAD * packets. */#include <linux/config.h>#include <linux/errno.h>#include <linux/types.h>#include <linux/socket.h>#include <linux/sockios.h>#include <linux/sched.h>#include <linux/net.h>#include <linux/in6.h>#include <linux/netdevice.h>#include <linux/if_arp.h>#include <linux/route.h>#include <linux/inetdevice.h>#include <linux/init.h>#ifdef CONFIG_SYSCTL#include <linux/sysctl.h>#endif#include <linux/delay.h>#include <linux/notifier.h>#include <linux/proc_fs.h>#include <net/sock.h>#include <net/snmp.h>#include <net/ipv6.h>#include <net/protocol.h>#include <net/ndisc.h>#include <net/ip6_route.h>#include <net/addrconf.h>#include <net/ip.h>#include <linux/if_tunnel.h>#include <linux/rtnetlink.h>#include <asm/uaccess.h>/* Set to 3 to get tracing... */#define ACONF_DEBUG 2#if ACONF_DEBUG >= 3#define ADBG(x) printk x#else#define ADBG(x)#endif#ifdef CONFIG_SYSCTLstatic void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);static void addrconf_sysctl_unregister(struct ipv6_devconf *p);#endifint inet6_dev_count;int inet6_ifa_count;/* * Configured unicast address hash table */static struct inet6_ifaddr *inet6_addr_lst[IN6_ADDR_HSIZE];static rwlock_t addrconf_hash_lock = RW_LOCK_UNLOCKED;/* Protects inet6 devices */rwlock_t addrconf_lock = RW_LOCK_UNLOCKED;void addrconf_verify(unsigned long);static struct timer_list addr_chk_timer = { function: addrconf_verify };static int addrconf_ifdown(struct net_device *dev, int how);static void addrconf_dad_start(struct inet6_ifaddr *ifp);static void addrconf_dad_timer(unsigned long data);static void addrconf_dad_completed(struct inet6_ifaddr *ifp);static void addrconf_rs_timer(unsigned long data);static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);static struct notifier_block *inet6addr_chain;struct ipv6_devconf ipv6_devconf ={ 0, /* forwarding */ IPV6_DEFAULT_HOPLIMIT, /* hop limit */ IPV6_MIN_MTU, /* mtu */ 1, /* accept RAs */ 1, /* accept redirects */ 1, /* autoconfiguration */ 1, /* dad transmits */ MAX_RTR_SOLICITATIONS, /* router solicits */ RTR_SOLICITATION_INTERVAL, /* rtr solicit interval */ MAX_RTR_SOLICITATION_DELAY, /* rtr solicit delay */};static struct ipv6_devconf ipv6_devconf_dflt ={ 0, /* forwarding */ IPV6_DEFAULT_HOPLIMIT, /* hop limit */ IPV6_MIN_MTU, /* mtu */ 1, /* accept RAs */ 1, /* accept redirects */ 1, /* autoconfiguration */ 1, /* dad transmits */ MAX_RTR_SOLICITATIONS, /* router solicits */ RTR_SOLICITATION_INTERVAL, /* rtr solicit interval */ MAX_RTR_SOLICITATION_DELAY, /* rtr solicit delay */};int ipv6_addr_type(struct in6_addr *addr){ u32 st; st = addr->s6_addr32[0]; /* Consider all addresses with the first three bits different of 000 and 111 as unicasts. */ if ((st & __constant_htonl(0xE0000000)) != __constant_htonl(0x00000000) && (st & __constant_htonl(0xE0000000)) != __constant_htonl(0xE0000000)) return IPV6_ADDR_UNICAST; if ((st & __constant_htonl(0xFF000000)) == __constant_htonl(0xFF000000)) { int type = IPV6_ADDR_MULTICAST; switch((st & __constant_htonl(0x00FF0000))) { case __constant_htonl(0x00010000): type |= IPV6_ADDR_LOOPBACK; break; case __constant_htonl(0x00020000): type |= IPV6_ADDR_LINKLOCAL; break; case __constant_htonl(0x00050000): type |= IPV6_ADDR_SITELOCAL; break; }; return type; } if ((st & __constant_htonl(0xFFC00000)) == __constant_htonl(0xFE800000)) return (IPV6_ADDR_LINKLOCAL | IPV6_ADDR_UNICAST); if ((st & __constant_htonl(0xFFC00000)) == __constant_htonl(0xFEC00000)) return (IPV6_ADDR_SITELOCAL | IPV6_ADDR_UNICAST); if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) { if (addr->s6_addr32[2] == 0) { if (addr->in6_u.u6_addr32[3] == 0) return IPV6_ADDR_ANY; if (addr->s6_addr32[3] == __constant_htonl(0x00000001)) return (IPV6_ADDR_LOOPBACK | IPV6_ADDR_UNICAST); return (IPV6_ADDR_COMPATv4 | IPV6_ADDR_UNICAST); } if (addr->s6_addr32[2] == __constant_htonl(0x0000ffff)) return IPV6_ADDR_MAPPED; } return IPV6_ADDR_RESERVED;}static void addrconf_del_timer(struct inet6_ifaddr *ifp){ if (del_timer(&ifp->timer)) __in6_ifa_put(ifp);}enum addrconf_timer_t{ AC_NONE, AC_DAD, AC_RS,};static void addrconf_mod_timer(struct inet6_ifaddr *ifp, enum addrconf_timer_t what, unsigned long when){ if (!del_timer(&ifp->timer)) in6_ifa_hold(ifp); switch (what) { case AC_DAD: ifp->timer.function = addrconf_dad_timer; break; case AC_RS: ifp->timer.function = addrconf_rs_timer; break; default:; } ifp->timer.expires = jiffies + when; add_timer(&ifp->timer);}/* Nobody refers to this device, we may destroy it. */void in6_dev_finish_destroy(struct inet6_dev *idev){ struct net_device *dev = idev->dev; BUG_TRAP(idev->addr_list==NULL); BUG_TRAP(idev->mc_list==NULL);#ifdef NET_REFCNT_DEBUG printk(KERN_DEBUG "in6_dev_finish_destroy: %s\n", dev ? dev->name : "NIL");#endif dev_put(dev); if (!idev->dead) { printk("Freeing alive inet6 device %p\n", idev); return; } inet6_dev_count--; kfree(idev);}static struct inet6_dev * ipv6_add_dev(struct net_device *dev){ struct inet6_dev *ndev; ASSERT_RTNL(); if (dev->mtu < IPV6_MIN_MTU) return NULL; ndev = kmalloc(sizeof(struct inet6_dev), GFP_KERNEL); if (ndev) { memset(ndev, 0, sizeof(struct inet6_dev)); ndev->lock = RW_LOCK_UNLOCKED; ndev->dev = dev; memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf)); ndev->cnf.mtu6 = dev->mtu; ndev->cnf.sysctl = NULL; ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl); if (ndev->nd_parms == NULL) { kfree(ndev); return NULL; } inet6_dev_count++; /* We refer to the device */ dev_hold(dev); write_lock_bh(&addrconf_lock); dev->ip6_ptr = ndev; /* One reference from device */ in6_dev_hold(ndev); write_unlock_bh(&addrconf_lock); ipv6_mc_init_dev(ndev);#ifdef CONFIG_SYSCTL neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6"); addrconf_sysctl_register(ndev, &ndev->cnf);#endif } return ndev;}static struct inet6_dev * ipv6_find_idev(struct net_device *dev){ struct inet6_dev *idev; ASSERT_RTNL(); if ((idev = __in6_dev_get(dev)) == NULL) { if ((idev = ipv6_add_dev(dev)) == NULL) return NULL; } if (dev->flags&IFF_UP) ipv6_mc_up(idev); return idev;}static void addrconf_forward_change(struct inet6_dev *idev){ struct net_device *dev; if (idev) return; read_lock(&dev_base_lock); for (dev=dev_base; dev; dev=dev->next) { read_lock(&addrconf_lock); idev = __in6_dev_get(dev); if (idev) idev->cnf.forwarding = ipv6_devconf.forwarding; read_unlock(&addrconf_lock); } read_unlock(&dev_base_lock);}/* Nobody refers to this ifaddr, destroy it */void inet6_ifa_finish_destroy(struct inet6_ifaddr *ifp){ BUG_TRAP(ifp->if_next==NULL); BUG_TRAP(ifp->lst_next==NULL);#ifdef NET_REFCNT_DEBUG printk(KERN_DEBUG "inet6_ifa_finish_destroy\n");#endif in6_dev_put(ifp->idev); if (del_timer(&ifp->timer)) printk("Timer is still running, when freeing ifa=%p\n", ifp); if (!ifp->dead) { printk("Freeing alive inet6 address %p\n", ifp); return; } inet6_ifa_count--; kfree(ifp);}/* On success it returns ifp with increased reference count */static struct inet6_ifaddr *ipv6_add_addr(struct inet6_dev *idev, struct in6_addr *addr, int pfxlen, int scope, unsigned flags){ struct inet6_ifaddr *ifa; int hash; ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); if (ifa == NULL) { ADBG(("ipv6_add_addr: malloc failed\n")); return NULL; } memset(ifa, 0, sizeof(struct inet6_ifaddr)); ipv6_addr_copy(&ifa->addr, addr); spin_lock_init(&ifa->lock); init_timer(&ifa->timer); ifa->timer.data = (unsigned long) ifa; ifa->scope = scope; ifa->prefix_len = pfxlen; ifa->flags = flags | IFA_F_TENTATIVE; read_lock(&addrconf_lock); if (idev->dead) { read_unlock(&addrconf_lock); kfree(ifa); return NULL; } inet6_ifa_count++; ifa->idev = idev; in6_dev_hold(idev); /* For caller */ in6_ifa_hold(ifa); /* Add to big hash table */ hash = ipv6_addr_hash(addr); write_lock_bh(&addrconf_hash_lock); ifa->lst_next = inet6_addr_lst[hash]; inet6_addr_lst[hash] = ifa; in6_ifa_hold(ifa); write_unlock_bh(&addrconf_hash_lock); write_lock_bh(&idev->lock); /* Add to inet6_dev unicast addr list. */ ifa->if_next = idev->addr_list; idev->addr_list = ifa; in6_ifa_hold(ifa); write_unlock_bh(&idev->lock); read_unlock(&addrconf_lock); notifier_call_chain(&inet6addr_chain,NETDEV_UP,ifa); return ifa;}/* This function wants to get referenced ifp and releases it before return */static void ipv6_del_addr(struct inet6_ifaddr *ifp){ struct inet6_ifaddr *ifa, **ifap; struct inet6_dev *idev = ifp->idev; int hash; hash = ipv6_addr_hash(&ifp->addr); ifp->dead = 1; write_lock_bh(&addrconf_hash_lock); for (ifap = &inet6_addr_lst[hash]; (ifa=*ifap) != NULL; ifap = &ifa->lst_next) { if (ifa == ifp) { *ifap = ifa->lst_next; __in6_ifa_put(ifp); ifa->lst_next = NULL; break; } } write_unlock_bh(&addrconf_hash_lock); write_lock_bh(&idev->lock); for (ifap = &idev->addr_list; (ifa=*ifap) != NULL; ifap = &ifa->if_next) { if (ifa == ifp) { *ifap = ifa->if_next; __in6_ifa_put(ifp); ifa->if_next = NULL; break; } } write_unlock_bh(&idev->lock); ipv6_ifa_notify(RTM_DELADDR, ifp); notifier_call_chain(&inet6addr_chain,NETDEV_DOWN,ifp); addrconf_del_timer(ifp); in6_ifa_put(ifp);}/* * Choose an apropriate source address * should do: * i) get an address with an apropriate scope * ii) see if there is a specific route for the destination and use * an address of the attached interface * iii) don't use deprecated addresses */int ipv6_get_saddr(struct dst_entry *dst, struct in6_addr *daddr, struct in6_addr *saddr){ int scope; struct inet6_ifaddr *ifp = NULL; struct inet6_ifaddr *match = NULL; struct net_device *dev = NULL; struct inet6_dev *idev; struct rt6_info *rt; int err; rt = (struct rt6_info *) dst; if (rt) dev = rt->rt6i_dev; scope = ipv6_addr_scope(daddr); if (rt && (rt->rt6i_flags & RTF_ALLONLINK)) { /* * route for the "all destinations on link" rule * when no routers are present */ scope = IFA_LINK; } /* * known dev * search dev and walk through dev addresses */ if (dev) { if (dev->flags & IFF_LOOPBACK) scope = IFA_HOST; read_lock(&addrconf_lock); idev = __in6_dev_get(dev); if (idev) { read_lock_bh(&idev->lock); for (ifp=idev->addr_list; ifp; ifp=ifp->if_next) { if (ifp->scope == scope) { if (!(ifp->flags & (IFA_F_DEPRECATED|IFA_F_TENTATIVE))) { in6_ifa_hold(ifp); read_unlock_bh(&idev->lock); read_unlock(&addrconf_lock); goto out; } if (!match && !(ifp->flags & IFA_F_TENTATIVE)) { match = ifp; in6_ifa_hold(ifp); } } } read_unlock_bh(&idev->lock); } read_unlock(&addrconf_lock); } if (scope == IFA_LINK) goto out; /* * dev == NULL or search failed for specified dev */ read_lock(&dev_base_lock); read_lock(&addrconf_lock);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -