Commit 023f3d8b authored by Hideaki Yoshifuji's avatar Hideaki Yoshifuji Committed by David S. Miller

[IPV6]: Don't allow multiple instances of the same IPv6 address on an interface.

parent 47810add
...@@ -30,6 +30,8 @@ ...@@ -30,6 +30,8 @@
* address validation timer. * address validation timer.
* YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041) * YOSHIFUJI Hideaki @USAGI : Privacy Extensions (RFC3041)
* support. * support.
* Yuji SEKIYA @USAGI : Don't assign a same IPv6
* address on a same interface.
*/ */
#include <linux/config.h> #include <linux/config.h>
...@@ -126,6 +128,8 @@ static void addrconf_dad_completed(struct inet6_ifaddr *ifp); ...@@ -126,6 +128,8 @@ static void addrconf_dad_completed(struct inet6_ifaddr *ifp);
static void addrconf_rs_timer(unsigned long data); static void addrconf_rs_timer(unsigned long data);
static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa); static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
static int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev);
static struct notifier_block *inet6addr_chain; static struct notifier_block *inet6addr_chain;
struct ipv6_devconf ipv6_devconf = struct ipv6_devconf ipv6_devconf =
...@@ -492,12 +496,23 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, ...@@ -492,12 +496,23 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
{ {
struct inet6_ifaddr *ifa; struct inet6_ifaddr *ifa;
int hash; int hash;
static spinlock_t lock = SPIN_LOCK_UNLOCKED;
spin_lock_bh(&lock);
/* Ignore adding duplicate addresses on an interface */
if (ipv6_chk_same_addr(addr, idev->dev)) {
spin_unlock_bh(&lock);
ADBG(("ipv6_add_addr: already assigned\n"));
return ERR_PTR(-EEXIST);
}
ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC); ifa = kmalloc(sizeof(struct inet6_ifaddr), GFP_ATOMIC);
if (ifa == NULL) { if (ifa == NULL) {
spin_unlock_bh(&lock);
ADBG(("ipv6_add_addr: malloc failed\n")); ADBG(("ipv6_add_addr: malloc failed\n"));
return NULL; return ERR_PTR(-ENOBUFS);
} }
memset(ifa, 0, sizeof(struct inet6_ifaddr)); memset(ifa, 0, sizeof(struct inet6_ifaddr));
...@@ -513,8 +528,9 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, ...@@ -513,8 +528,9 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
read_lock(&addrconf_lock); read_lock(&addrconf_lock);
if (idev->dead) { if (idev->dead) {
read_unlock(&addrconf_lock); read_unlock(&addrconf_lock);
spin_unlock_bh(&lock);
kfree(ifa); kfree(ifa);
return NULL; return ERR_PTR(-ENODEV); /*XXX*/
} }
inet6_ifa_count++; inet6_ifa_count++;
...@@ -551,6 +567,7 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen, ...@@ -551,6 +567,7 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
in6_ifa_hold(ifa); in6_ifa_hold(ifa);
write_unlock_bh(&idev->lock); write_unlock_bh(&idev->lock);
read_unlock(&addrconf_lock); read_unlock(&addrconf_lock);
spin_unlock_bh(&lock);
notifier_call_chain(&inet6addr_chain,NETDEV_UP,ifa); notifier_call_chain(&inet6addr_chain,NETDEV_UP,ifa);
...@@ -697,7 +714,7 @@ static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *i ...@@ -697,7 +714,7 @@ static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *i
ift = ipv6_count_addresses(idev) < IPV6_MAX_ADDRESSES ? ift = ipv6_count_addresses(idev) < IPV6_MAX_ADDRESSES ?
ipv6_add_addr(idev, &addr, tmp_plen, ipv6_add_addr(idev, &addr, tmp_plen,
ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : 0; ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : 0;
if (!ift) { if (IS_ERR(ift)) {
in6_dev_put(idev); in6_dev_put(idev);
in6_ifa_put(ifp); in6_ifa_put(ifp);
printk(KERN_INFO printk(KERN_INFO
...@@ -928,6 +945,23 @@ int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev) ...@@ -928,6 +945,23 @@ int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev)
return ifp != NULL; return ifp != NULL;
} }
static
int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
{
struct inet6_ifaddr * ifp;
u8 hash = ipv6_addr_hash(addr);
read_lock_bh(&addrconf_hash_lock);
for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
if (dev == NULL || ifp->idev->dev == dev)
break;
}
}
read_unlock_bh(&addrconf_hash_lock);
return ifp != NULL;
}
struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev) struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev)
{ {
struct inet6_ifaddr * ifp; struct inet6_ifaddr * ifp;
...@@ -1344,7 +1378,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len) ...@@ -1344,7 +1378,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len, ifp = ipv6_add_addr(in6_dev, &addr, pinfo->prefix_len,
addr_type&IPV6_ADDR_SCOPE_MASK, 0); addr_type&IPV6_ADDR_SCOPE_MASK, 0);
if (ifp == NULL) { if (IS_ERR(ifp)) {
in6_dev_put(in6_dev); in6_dev_put(in6_dev);
return; return;
} }
...@@ -1499,13 +1533,14 @@ static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen) ...@@ -1499,13 +1533,14 @@ static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen)
scope = ipv6_addr_scope(pfx); scope = ipv6_addr_scope(pfx);
if ((ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT)) != NULL) { ifp = ipv6_add_addr(idev, pfx, plen, scope, IFA_F_PERMANENT);
if (!IS_ERR(ifp)) {
addrconf_dad_start(ifp); addrconf_dad_start(ifp);
in6_ifa_put(ifp); in6_ifa_put(ifp);
return 0; return 0;
} }
return -ENOBUFS; return PTR_ERR(ifp);
} }
static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen) static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
...@@ -1597,7 +1632,7 @@ static void sit_add_v4_addrs(struct inet6_dev *idev) ...@@ -1597,7 +1632,7 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
if (addr.s6_addr32[3]) { if (addr.s6_addr32[3]) {
ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT); ifp = ipv6_add_addr(idev, &addr, 128, scope, IFA_F_PERMANENT);
if (ifp) { if (!IS_ERR(ifp)) {
spin_lock_bh(&ifp->lock); spin_lock_bh(&ifp->lock);
ifp->flags &= ~IFA_F_TENTATIVE; ifp->flags &= ~IFA_F_TENTATIVE;
spin_unlock_bh(&ifp->lock); spin_unlock_bh(&ifp->lock);
...@@ -1633,7 +1668,7 @@ static void sit_add_v4_addrs(struct inet6_dev *idev) ...@@ -1633,7 +1668,7 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
ifp = ipv6_add_addr(idev, &addr, plen, flag, ifp = ipv6_add_addr(idev, &addr, plen, flag,
IFA_F_PERMANENT); IFA_F_PERMANENT);
if (ifp) { if (!IS_ERR(ifp)) {
spin_lock_bh(&ifp->lock); spin_lock_bh(&ifp->lock);
ifp->flags &= ~IFA_F_TENTATIVE; ifp->flags &= ~IFA_F_TENTATIVE;
spin_unlock_bh(&ifp->lock); spin_unlock_bh(&ifp->lock);
...@@ -1660,7 +1695,7 @@ static void init_loopback(struct net_device *dev) ...@@ -1660,7 +1695,7 @@ static void init_loopback(struct net_device *dev)
} }
ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT); ifp = ipv6_add_addr(idev, &in6addr_loopback, 128, IFA_HOST, IFA_F_PERMANENT);
if (ifp) { if (!IS_ERR(ifp)) {
spin_lock_bh(&ifp->lock); spin_lock_bh(&ifp->lock);
ifp->flags &= ~IFA_F_TENTATIVE; ifp->flags &= ~IFA_F_TENTATIVE;
spin_unlock_bh(&ifp->lock); spin_unlock_bh(&ifp->lock);
...@@ -1674,7 +1709,7 @@ static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr ...@@ -1674,7 +1709,7 @@ static void addrconf_add_linklocal(struct inet6_dev *idev, struct in6_addr *addr
struct inet6_ifaddr * ifp; struct inet6_ifaddr * ifp;
ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT); ifp = ipv6_add_addr(idev, addr, 64, IFA_LINK, IFA_F_PERMANENT);
if (ifp) { if (!IS_ERR(ifp)) {
addrconf_dad_start(ifp); addrconf_dad_start(ifp);
in6_ifa_put(ifp); in6_ifa_put(ifp);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment