📄 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 double 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. * YOSHIFUJI Hideaki @USAGI : improved accuracy of * address validation timer. * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) * support. * Yuji SEKIYA @USAGI : Don't assign a same IPv6 * address on a same interface. * YOSHIFUJI Hideaki @USAGI : ARCnet support * YOSHIFUJI Hideaki @USAGI : convert /proc/net/if_inet6 to * seq_file. */#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/if_arcnet.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 <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/tcp.h>#include <net/ip.h>#include <linux/if_tunnel.h>#include <linux/rtnetlink.h>#ifdef CONFIG_IPV6_PRIVACY#include <linux/random.h>#include <linux/crypto.h>#include <asm/scatterlist.h>#endif#include <asm/uaccess.h>#include <linux/proc_fs.h>#include <linux/seq_file.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#define INFINITY_LIFE_TIME 0xFFFFFFFF#define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))#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;#ifdef CONFIG_IPV6_PRIVACYstatic int __ipv6_regen_rndid(struct inet6_dev *idev);static int __ipv6_try_regen_rndid(struct inet6_dev *idev, struct in6_addr *tmpaddr); static void ipv6_regen_rndid(unsigned long data);static int desync_factor = MAX_DESYNC_FACTOR * HZ;static struct crypto_tfm *md5_tfm;static spinlock_t md5_tfm_lock = SPIN_LOCK_UNLOCKED;#endifstatic int ipv6_count_addresses(struct inet6_dev *idev);/* * 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;static void addrconf_verify(unsigned long);static struct timer_list addr_chk_timer = TIMER_INITIALIZER(addrconf_verify, 0, 0);static spinlock_t addrconf_verify_lock = SPIN_LOCK_UNLOCKED;static void addrconf_join_anycast(struct inet6_ifaddr *ifp);static void addrconf_leave_anycast(struct inet6_ifaddr *ifp);static int addrconf_ifdown(struct net_device *dev, int how);static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags);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 void inet6_prefix_notify(int event, struct inet6_dev *idev, struct prefix_info *pinfo);static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);static struct notifier_block *inet6addr_chain;struct ipv6_devconf ipv6_devconf = { .forwarding = 0, .hop_limit = IPV6_DEFAULT_HOPLIMIT, .mtu6 = IPV6_MIN_MTU, .accept_ra = 1, .accept_redirects = 1, .autoconf = 1, .force_mld_version = 0, .dad_transmits = 1, .rtr_solicits = MAX_RTR_SOLICITATIONS, .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,#ifdef CONFIG_IPV6_PRIVACY .use_tempaddr = 0, .temp_valid_lft = TEMP_VALID_LIFETIME, .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, .regen_max_retry = REGEN_MAX_RETRY, .max_desync_factor = MAX_DESYNC_FACTOR,#endif .max_addresses = IPV6_MAX_ADDRESSES,};static struct ipv6_devconf ipv6_devconf_dflt = { .forwarding = 0, .hop_limit = IPV6_DEFAULT_HOPLIMIT, .mtu6 = IPV6_MIN_MTU, .accept_ra = 1, .accept_redirects = 1, .autoconf = 1, .dad_transmits = 1, .rtr_solicits = MAX_RTR_SOLICITATIONS, .rtr_solicit_interval = RTR_SOLICITATION_INTERVAL, .rtr_solicit_delay = MAX_RTR_SOLICITATION_DELAY,#ifdef CONFIG_IPV6_PRIVACY .use_tempaddr = 0, .temp_valid_lft = TEMP_VALID_LIFETIME, .temp_prefered_lft = TEMP_PREFERRED_LIFETIME, .regen_max_retry = REGEN_MAX_RETRY, .max_desync_factor = MAX_DESYNC_FACTOR,#endif .max_addresses = IPV6_MAX_ADDRESSES,};/* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;int ipv6_addr_type(const struct in6_addr *addr){ int type; u32 st; st = addr->s6_addr32[0]; if ((st & htonl(0xFF000000)) == htonl(0xFF000000)) { type = IPV6_ADDR_MULTICAST; switch((st & 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; } type = IPV6_ADDR_UNICAST; /* Consider all addresses with the first three bits different of 000 and 111 as finished. */ if ((st & htonl(0xE0000000)) != htonl(0x00000000) && (st & htonl(0xE0000000)) != htonl(0xE0000000)) return type; if ((st & htonl(0xFFC00000)) == htonl(0xFE800000)) return (IPV6_ADDR_LINKLOCAL | type); if ((st & htonl(0xFFC00000)) == htonl(0xFEC00000)) return (IPV6_ADDR_SITELOCAL | type); if ((addr->s6_addr32[0] | addr->s6_addr32[1]) == 0) { if (addr->s6_addr32[2] == 0) { if (addr->s6_addr32[3] == 0) return IPV6_ADDR_ANY; if (addr->s6_addr32[3] == htonl(0x00000001)) return (IPV6_ADDR_LOOPBACK | type); return (IPV6_ADDR_COMPATv4 | type); } if (addr->s6_addr32[2] == htonl(0x0000ffff)) return IPV6_ADDR_MAPPED; } st &= htonl(0xFF000000); if (st == 0) return IPV6_ADDR_RESERVED; st &= htonl(0xFE000000); if (st == htonl(0x02000000)) return IPV6_ADDR_RESERVED; /* for NSAP */ if (st == htonl(0x04000000)) return IPV6_ADDR_RESERVED; /* for IPX */ return type;}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; } snmp6_unregister_dev(idev); 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); if (snmp6_register_dev(ndev) < 0) { ADBG((KERN_WARNING "%s(): cannot create /proc/net/dev_snmp6/%s\n", __FUNCTION__, dev->name)); neigh_parms_release(&nd_tbl, ndev->nd_parms); ndev->dead = 1; in6_dev_finish_destroy(ndev); return NULL; } /* One reference from device. We must do this before * we invoke __ipv6_regen_rndid(). */ in6_dev_hold(ndev);#ifdef CONFIG_IPV6_PRIVACY get_random_bytes(ndev->rndid, sizeof(ndev->rndid)); get_random_bytes(ndev->entropy, sizeof(ndev->entropy)); init_timer(&ndev->regen_timer); ndev->regen_timer.function = ipv6_regen_rndid; ndev->regen_timer.data = (unsigned long) ndev; if ((dev->flags&IFF_LOOPBACK) || dev->type == ARPHRD_TUNNEL || dev->type == ARPHRD_SIT) { printk(KERN_INFO "Disabled Privacy Extensions on device %p(%s)\n", dev, dev->name); ndev->cnf.use_tempaddr = -1; } else { in6_dev_hold(ndev); ipv6_regen_rndid((unsigned long) ndev); }#endif write_lock_bh(&addrconf_lock); dev->ip6_ptr = ndev; write_unlock_bh(&addrconf_lock); ipv6_mc_init_dev(ndev); ndev->tstamp = jiffies;#ifdef CONFIG_SYSCTL neigh_sysctl_register(dev, ndev->nd_parms, NET_IPV6, NET_IPV6_NEIGH, "ipv6", &ndisc_ifinfo_sysctl_change); 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 dev_forward_change(struct inet6_dev *idev){ struct net_device *dev; struct inet6_ifaddr *ifa; struct in6_addr addr; if (!idev) return; dev = idev->dev; if (dev && (dev->flags & IFF_MULTICAST)) { ipv6_addr_all_routers(&addr); if (idev->cnf.forwarding) ipv6_dev_mc_inc(dev, &addr); else ipv6_dev_mc_dec(dev, &addr); } for (ifa=idev->addr_list; ifa; ifa=ifa->if_next) { if (idev->cnf.forwarding) addrconf_join_anycast(ifa); else addrconf_leave_anycast(ifa); }}static void addrconf_forward_change(void){ struct net_device *dev; struct inet6_dev *idev; read_lock(&dev_base_lock); for (dev=dev_base; dev; dev=dev->next) { read_lock(&addrconf_lock); idev = __in6_dev_get(dev); if (idev) { int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding); idev->cnf.forwarding = ipv6_devconf.forwarding; if (changed) dev_forward_change(idev); } 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; } dst_release(&ifp->rt->u.dst); 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, const struct in6_addr *addr, int pfxlen, int scope, unsigned flags){ struct inet6_ifaddr *ifa = NULL; struct rt6_info *rt; int hash; static spinlock_t lock = SPIN_LOCK_UNLOCKED; int err = 0; spin_lock_bh(&lock); /* Ignore adding duplicate addresses on an interface */ if (ipv6_chk_same_addr(addr, idev->dev)) { ADBG(("ipv6_add_addr: already assigned\n")); err = -EEXIST; goto out; } ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); if (ifa == NULL) { ADBG(("ipv6_add_addr: malloc failed\n")); err = -ENOBUFS; goto out; } rt = addrconf_dst_alloc(idev, addr, 0); if (IS_ERR(rt)) { err = PTR_ERR(rt); goto out; } 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; ifa->cstamp = ifa->tstamp = jiffies; read_lock(&addrconf_lock); if (idev->dead) { read_unlock(&addrconf_lock); err = -ENODEV; /*XXX*/ goto out; } 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -