This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / net / ipv6 / addrconf.c
index 870e080..4fb96bc 100644 (file)
@@ -137,7 +137,6 @@ 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 ipv6_ifa_notify(int event, struct inet6_ifaddr *ifa);
 
 static void inet6_prefix_notify(int event, struct inet6_dev *idev, 
@@ -327,7 +326,7 @@ static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
        if (ndev) {
                memset(ndev, 0, sizeof(struct inet6_dev));
 
-               rwlock_init(&ndev->lock);
+               ndev->lock = RW_LOCK_UNLOCKED;
                ndev->dev = dev;
                memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
                ndev->cnf.mtu6 = dev->mtu;
@@ -405,7 +404,6 @@ static struct inet6_dev * ipv6_find_idev(struct net_device *dev)
        return idev;
 }
 
-#ifdef CONFIG_SYSCTL
 static void dev_forward_change(struct inet6_dev *idev)
 {
        struct net_device *dev;
@@ -451,7 +449,7 @@ static void addrconf_forward_change(void)
        }
        read_unlock(&dev_base_lock);
 }
-#endif
+
 
 /* Nobody refers to this ifaddr, destroy it */
 
@@ -487,15 +485,10 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
        struct inet6_ifaddr *ifa = NULL;
        struct rt6_info *rt;
        int hash;
+       static spinlock_t lock = SPIN_LOCK_UNLOCKED;
        int err = 0;
 
-       read_lock_bh(&addrconf_lock);
-       if (idev->dead) {
-               err = -ENODEV;                  /*XXX*/
-               goto out2;
-       }
-
-       write_lock(&addrconf_hash_lock);
+       spin_lock_bh(&lock);
 
        /* Ignore adding duplicate addresses on an interface */
        if (ipv6_chk_same_addr(addr, idev->dev)) {
@@ -529,6 +522,13 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int 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);
@@ -538,30 +538,35 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
        /* 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(&addrconf_hash_lock);
+       write_unlock_bh(&addrconf_hash_lock);
 
-       write_lock(&idev->lock);
+       write_lock_bh(&idev->lock);
        /* Add to inet6_dev unicast addr list. */
        ifa->if_next = idev->addr_list;
        idev->addr_list = ifa;
 
 #ifdef CONFIG_IPV6_PRIVACY
+       ifa->regen_count = 0;
        if (ifa->flags&IFA_F_TEMPORARY) {
                ifa->tmp_next = idev->tempaddr_list;
                idev->tempaddr_list = ifa;
                in6_ifa_hold(ifa);
+       } else {
+               ifa->tmp_next = NULL;
        }
 #endif
 
        ifa->rt = rt;
 
        in6_ifa_hold(ifa);
-       write_unlock(&idev->lock);
-out2:
-       read_unlock_bh(&addrconf_lock);
+       write_unlock_bh(&idev->lock);
+       read_unlock(&addrconf_lock);
+out:
+       spin_unlock_bh(&lock);
 
        if (unlikely(err == 0))
                notifier_call_chain(&inet6addr_chain, NETDEV_UP, ifa);
@@ -571,9 +576,6 @@ out2:
        }
 
        return ifa;
-out:
-       write_unlock(&addrconf_hash_lock);
-       goto out2;
 }
 
 /* This function wants to get referenced ifp and releases it before return */
@@ -642,14 +644,13 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
 #ifdef CONFIG_IPV6_PRIVACY
 static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *ift)
 {
-       struct inet6_dev *idev = ifp->idev;
+       struct inet6_dev *idev;
        struct in6_addr addr, *tmpaddr;
-       unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
+       unsigned long tmp_prefered_lft, tmp_valid_lft;
        int tmp_plen;
        int ret = 0;
        int max_addresses;
 
-       write_lock(&idev->lock);
        if (ift) {
                spin_lock_bh(&ift->lock);
                memcpy(&addr.s6_addr[8], &ift->addr.s6_addr[8], 8);
@@ -659,35 +660,40 @@ static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *i
                tmpaddr = NULL;
        }
 retry:
+       spin_lock_bh(&ifp->lock);
+       in6_ifa_hold(ifp);
+       idev = ifp->idev;
        in6_dev_hold(idev);
+       memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
+       write_lock(&idev->lock);
        if (idev->cnf.use_tempaddr <= 0) {
                write_unlock(&idev->lock);
+               spin_unlock_bh(&ifp->lock);
                printk(KERN_INFO
                        "ipv6_create_tempaddr(): use_tempaddr is disabled.\n");
                in6_dev_put(idev);
+               in6_ifa_put(ifp);
                ret = -1;
                goto out;
        }
-       spin_lock_bh(&ifp->lock);
        if (ifp->regen_count++ >= idev->cnf.regen_max_retry) {
                idev->cnf.use_tempaddr = -1;    /*XXX*/
-               spin_unlock_bh(&ifp->lock);
                write_unlock(&idev->lock);
+               spin_unlock_bh(&ifp->lock);
                printk(KERN_WARNING
                        "ipv6_create_tempaddr(): regeneration time exceeded. disabled temporary address support.\n");
                in6_dev_put(idev);
+               in6_ifa_put(ifp);
                ret = -1;
                goto out;
        }
-       in6_ifa_hold(ifp);
-       memcpy(addr.s6_addr, ifp->addr.s6_addr, 8);
        if (__ipv6_try_regen_rndid(idev, tmpaddr) < 0) {
-               spin_unlock_bh(&ifp->lock);
                write_unlock(&idev->lock);
+               spin_unlock_bh(&ifp->lock);
                printk(KERN_WARNING
                        "ipv6_create_tempaddr(): regeneration of randomized interface id failed.\n");
-               in6_ifa_put(ifp);
                in6_dev_put(idev);
+               in6_ifa_put(ifp);
                ret = -1;
                goto out;
        }
@@ -700,33 +706,27 @@ retry:
                                 idev->cnf.temp_prefered_lft - desync_factor / HZ);
        tmp_plen = ifp->prefix_len;
        max_addresses = idev->cnf.max_addresses;
-       tmp_cstamp = ifp->cstamp;
-       tmp_tstamp = ifp->tstamp;
-       spin_unlock_bh(&ifp->lock);
-
        write_unlock(&idev->lock);
+       spin_unlock_bh(&ifp->lock);
        ift = !max_addresses ||
              ipv6_count_addresses(idev) < max_addresses ? 
                ipv6_add_addr(idev, &addr, tmp_plen,
                              ipv6_addr_type(&addr)&IPV6_ADDR_SCOPE_MASK, IFA_F_TEMPORARY) : NULL;
        if (!ift || IS_ERR(ift)) {
-               in6_ifa_put(ifp);
                in6_dev_put(idev);
+               in6_ifa_put(ifp);
                printk(KERN_INFO
                        "ipv6_create_tempaddr(): retry temporary address regeneration.\n");
                tmpaddr = &addr;
-               write_lock(&idev->lock);
                goto retry;
        }
-
        spin_lock_bh(&ift->lock);
        ift->ifpub = ifp;
        ift->valid_lft = tmp_valid_lft;
        ift->prefered_lft = tmp_prefered_lft;
-       ift->cstamp = tmp_cstamp;
-       ift->tstamp = tmp_tstamp;
+       ift->cstamp = ifp->cstamp;
+       ift->tstamp = ifp->tstamp;
        spin_unlock_bh(&ift->lock);
-
        addrconf_dad_start(ift, 0);
        in6_ifa_put(ift);
        in6_dev_put(idev);
@@ -760,7 +760,7 @@ static int inline ipv6_saddr_pref(const struct inet6_ifaddr *ifp, u8 invpref)
 #endif
 
 int ipv6_dev_get_saddr(struct net_device *dev,
-                      struct in6_addr *daddr, struct in6_addr *saddr)
+                  struct in6_addr *daddr, struct in6_addr *saddr, int onlink)
 {
        struct inet6_ifaddr *ifp = NULL;
        struct inet6_ifaddr *match = NULL;
@@ -769,7 +769,10 @@ int ipv6_dev_get_saddr(struct net_device *dev,
        int err;
        int hiscore = -1, score;
 
-       scope = ipv6_addr_scope(daddr);
+       if (!onlink)
+               scope = ipv6_addr_scope(daddr);
+       else
+               scope = IFA_LINK;
 
        /*
         *      known dev
@@ -874,7 +877,17 @@ out:
 int ipv6_get_saddr(struct dst_entry *dst,
                   struct in6_addr *daddr, struct in6_addr *saddr)
 {
-       return ipv6_dev_get_saddr(dst ? dst->dev : NULL, daddr, saddr);
+       struct rt6_info *rt;
+       struct net_device *dev = NULL;
+       int onlink;
+
+       rt = (struct rt6_info *) dst;
+       if (rt)
+               dev = rt->rt6i_dev;
+
+       onlink = (rt && (rt->rt6i_flags & RTF_ALLONLINK));
+
+       return ipv6_dev_get_saddr(dev, daddr, saddr, onlink);
 }
 
 
@@ -920,7 +933,7 @@ int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
 
        read_lock_bh(&addrconf_hash_lock);
        for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
-               if (ipv6_addr_equal(&ifp->addr, addr) &&
+               if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
                    !(ifp->flags&IFA_F_TENTATIVE)) {
                        if (dev == NULL || ifp->idev->dev == dev ||
                            !(ifp->scope&(IFA_LINK|IFA_HOST) || strict))
@@ -937,12 +950,14 @@ 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_equal(&ifp->addr, addr)) {
+               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;
 }
 
@@ -953,7 +968,7 @@ struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *
 
        read_lock_bh(&addrconf_hash_lock);
        for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
-               if (ipv6_addr_equal(&ifp->addr, addr)) {
+               if (ipv6_addr_cmp(&ifp->addr, addr) == 0) {
                        if (dev == NULL || ifp->idev->dev == dev ||
                            !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
                                in6_ifa_hold(ifp);
@@ -989,7 +1004,7 @@ int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
                return 1;
 
        if (sk2_rcv_saddr6 &&
-           ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
+           !ipv6_addr_cmp(sk_rcv_saddr6, sk2_rcv_saddr6))
                return 1;
 
        if (addr_type == IPV6_ADDR_MAPPED &&
@@ -1627,7 +1642,7 @@ static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
        read_lock_bh(&idev->lock);
        for (ifp = idev->addr_list; ifp; ifp=ifp->if_next) {
                if (ifp->prefix_len == plen &&
-                   ipv6_addr_equal(pfx, &ifp->addr)) {
+                   (!memcmp(pfx, &ifp->addr, sizeof(struct in6_addr)))) {
                        in6_ifa_hold(ifp);
                        read_unlock_bh(&idev->lock);
                        
@@ -2046,7 +2061,7 @@ static int addrconf_ifdown(struct net_device *dev, int how)
                addrconf_del_timer(ifa);
                write_unlock_bh(&idev->lock);
 
-               __ipv6_ifa_notify(RTM_DELADDR, ifa);
+               ipv6_ifa_notify(RTM_DELADDR, ifa);
                in6_ifa_put(ifa);
 
                write_lock_bh(&idev->lock);
@@ -2126,10 +2141,11 @@ out:
  */
 static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
 {
-       struct inet6_dev *idev = ifp->idev;
-       struct net_device *dev = idev->dev;
+       struct net_device *dev;
        unsigned long rand_num;
 
+       dev = ifp->idev->dev;
+
        addrconf_join_solict(dev, &ifp->addr);
 
        if (ifp->prefix_len != 128 && (ifp->flags&IFA_F_PERMANENT))
@@ -2137,43 +2153,31 @@ static void addrconf_dad_start(struct inet6_ifaddr *ifp, int flags)
                                        flags);
 
        net_srandom(ifp->addr.s6_addr32[3]);
-       rand_num = net_random() % (idev->cnf.rtr_solicit_delay ? : 1);
+       rand_num = net_random() % (ifp->idev->cnf.rtr_solicit_delay ? : 1);
 
-       read_lock_bh(&idev->lock);
-       if (ifp->dead)
-               goto out;
        spin_lock_bh(&ifp->lock);
 
        if (dev->flags&(IFF_NOARP|IFF_LOOPBACK) ||
            !(ifp->flags&IFA_F_TENTATIVE)) {
                ifp->flags &= ~IFA_F_TENTATIVE;
                spin_unlock_bh(&ifp->lock);
-               read_unlock_bh(&idev->lock);
 
                addrconf_dad_completed(ifp);
                return;
        }
 
-       ifp->probes = idev->cnf.dad_transmits;
+       ifp->probes = ifp->idev->cnf.dad_transmits;
        addrconf_mod_timer(ifp, AC_DAD, rand_num);
 
        spin_unlock_bh(&ifp->lock);
-out:
-       read_unlock_bh(&idev->lock);
 }
 
 static void addrconf_dad_timer(unsigned long data)
 {
        struct inet6_ifaddr *ifp = (struct inet6_ifaddr *) data;
-       struct inet6_dev *idev = ifp->idev;
        struct in6_addr unspec;
        struct in6_addr mcaddr;
 
-       read_lock_bh(&idev->lock);
-       if (idev->dead) {
-               read_unlock_bh(&idev->lock);
-               goto out;
-       }
        spin_lock_bh(&ifp->lock);
        if (ifp->probes == 0) {
                /*
@@ -2182,23 +2186,22 @@ static void addrconf_dad_timer(unsigned long data)
 
                ifp->flags &= ~IFA_F_TENTATIVE;
                spin_unlock_bh(&ifp->lock);
-               read_unlock_bh(&idev->lock);
 
                addrconf_dad_completed(ifp);
 
-               goto out;
+               in6_ifa_put(ifp);
+               return;
        }
 
        ifp->probes--;
        addrconf_mod_timer(ifp, AC_DAD, ifp->idev->nd_parms->retrans_time);
        spin_unlock_bh(&ifp->lock);
-       read_unlock_bh(&idev->lock);
 
        /* send a neighbour solicitation for our addr */
        memset(&unspec, 0, sizeof(unspec));
        addrconf_addr_solict_mult(&ifp->addr, &mcaddr);
        ndisc_send_ns(ifp->idev->dev, NULL, &ifp->addr, &mcaddr, &unspec);
-out:
+
        in6_ifa_put(ifp);
 }
 
@@ -2997,7 +3000,7 @@ static struct rtnetlink_link inet6_rtnetlink_table[RTM_MAX - RTM_BASE + 1] = {
                                      .dumpit   = inet6_dump_fib, },
 };
 
-static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
+static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
 {
        inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
 
@@ -3022,14 +3025,6 @@ static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
        }
 }
 
-static void ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
-{
-       read_lock_bh(&addrconf_lock);
-       if (likely(ifp->idev->dead == 0))
-               __ipv6_ifa_notify(event, ifp);
-       read_unlock_bh(&addrconf_lock);
-}
-
 #ifdef CONFIG_SYSCTL
 
 static
@@ -3055,7 +3050,7 @@ int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
                        addrconf_forward_change();
                }
                if (*valp)
-                       rt6_purge_dflt_routers();
+                       rt6_purge_dflt_routers(0);
        }
 
         return ret;
@@ -3109,7 +3104,7 @@ static int addrconf_sysctl_forward_strategy(ctl_table *table,
                }
 
                if (*valp)
-                       rt6_purge_dflt_routers();
+                       rt6_purge_dflt_routers(0);
        } else
                *valp = new;
 
@@ -3412,29 +3407,6 @@ int unregister_inet6addr_notifier(struct notifier_block *nb)
 
 void __init addrconf_init(void)
 {
-       /* The addrconf netdev notifier requires that loopback_dev
-        * has it's ipv6 private information allocated and setup
-        * before it can bring up and give link-local addresses
-        * to other devices which are up.
-        *
-        * Unfortunately, loopback_dev is not necessarily the first
-        * entry in the global dev_base list of net devices.  In fact,
-        * it is likely to be the very last entry on that list.
-        * So this causes the notifier registry below to try and
-        * give link-local addresses to all devices besides loopback_dev
-        * first, then loopback_dev, which cases all the non-loopback_dev
-        * devices to fail to get a link-local address.
-        *
-        * So, as a temporary fix, register loopback_dev first by hand.
-        * Longer term, all of the dependencies ipv6 has upon the loopback
-        * device and it being up should be removed.
-        */
-       rtnl_lock();
-       addrconf_notify(&ipv6_dev_notf, NETDEV_REGISTER, &loopback_dev);
-       if (loopback_dev.flags & IFF_UP)
-               addrconf_notify(&ipv6_dev_notf, NETDEV_UP, &loopback_dev);
-       rtnl_unlock();
-
        register_netdevice_notifier(&ipv6_dev_notf);
 
 #ifdef CONFIG_IPV6_PRIVACY