[IPV6] ADDRCONF: Fix array size for sysctls.
[safe/jmp/linux-2.6] / net / ipv6 / addrconf.c
index 38274c2..1103761 100644 (file)
@@ -62,6 +62,7 @@
 #include <linux/notifier.h>
 #include <linux/string.h>
 
+#include <net/net_namespace.h>
 #include <net/sock.h>
 #include <net/snmp.h>
 
@@ -73,6 +74,7 @@
 #include <net/tcp.h>
 #include <net/ip.h>
 #include <net/netlink.h>
+#include <net/pkt_sched.h>
 #include <linux/if_tunnel.h>
 #include <linux/rtnetlink.h>
 
@@ -81,6 +83,7 @@
 #endif
 
 #include <asm/uaccess.h>
+#include <asm/unaligned.h>
 
 #include <linux/proc_fs.h>
 #include <linux/seq_file.h>
 #define TIME_DELTA(a,b) ((unsigned long)((long)(a) - (long)(b)))
 
 #ifdef CONFIG_SYSCTL
-static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p);
-static void addrconf_sysctl_unregister(struct ipv6_devconf *p);
+static void addrconf_sysctl_register(struct inet6_dev *idev);
+static void addrconf_sysctl_unregister(struct inet6_dev *idev);
+#else
+static inline void addrconf_sysctl_register(struct inet6_dev *idev)
+{
+}
+
+static inline void addrconf_sysctl_unregister(struct inet6_dev *idev)
+{
+}
 #endif
 
 #ifdef CONFIG_IPV6_PRIVACY
@@ -138,7 +149,8 @@ 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 int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
+                             struct net_device *dev);
 
 static ATOMIC_NOTIFIER_HEAD(inet6addr_chain);
 
@@ -208,11 +220,15 @@ static struct ipv6_devconf ipv6_devconf_dflt __read_mostly = {
 };
 
 /* IPv6 Wildcard Address and Loopback Address defined by RFC2553 */
-#if 0
 const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
-#endif
 const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT;
 
+/* Check if a valid qdisc is available */
+static inline int addrconf_qdisc_ok(struct net_device *dev)
+{
+       return (dev->qdisc != &noop_qdisc);
+}
+
 static void addrconf_del_timer(struct inet6_ifaddr *ifp)
 {
        if (del_timer(&ifp->timer))
@@ -246,6 +262,35 @@ static void addrconf_mod_timer(struct inet6_ifaddr *ifp,
        add_timer(&ifp->timer);
 }
 
+static int snmp6_alloc_dev(struct inet6_dev *idev)
+{
+       if (snmp_mib_init((void **)idev->stats.ipv6,
+                         sizeof(struct ipstats_mib)) < 0)
+               goto err_ip;
+       if (snmp_mib_init((void **)idev->stats.icmpv6,
+                         sizeof(struct icmpv6_mib)) < 0)
+               goto err_icmp;
+       if (snmp_mib_init((void **)idev->stats.icmpv6msg,
+                         sizeof(struct icmpv6msg_mib)) < 0)
+               goto err_icmpmsg;
+
+       return 0;
+
+err_icmpmsg:
+       snmp_mib_free((void **)idev->stats.icmpv6);
+err_icmp:
+       snmp_mib_free((void **)idev->stats.ipv6);
+err_ip:
+       return -ENOMEM;
+}
+
+static void snmp6_free_dev(struct inet6_dev *idev)
+{
+       snmp_mib_free((void **)idev->stats.icmpv6msg);
+       snmp_mib_free((void **)idev->stats.icmpv6);
+       snmp_mib_free((void **)idev->stats.ipv6);
+}
+
 /* Nobody refers to this device, we may destroy it. */
 
 static void in6_dev_finish_destroy_rcu(struct rcu_head *head)
@@ -290,7 +335,7 @@ static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
 
        rwlock_init(&ndev->lock);
        ndev->dev = dev;
-       memcpy(&ndev->cnf, &ipv6_devconf_dflt, sizeof(ndev->cnf));
+       memcpy(&ndev->cnf, dev_net(dev)->ipv6.devconf_dflt, sizeof(ndev->cnf));
        ndev->cnf.mtu6 = dev->mtu;
        ndev->cnf.sysctl = NULL;
        ndev->nd_parms = neigh_parms_alloc(dev, &nd_tbl);
@@ -304,7 +349,7 @@ static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
        if (snmp6_alloc_dev(ndev) < 0) {
                ADBG((KERN_WARNING
                        "%s(): cannot allocate memory for statistics; dev=%s.\n",
-                       __FUNCTION__, dev->name));
+                       __func__, dev->name));
                neigh_parms_release(&nd_tbl, ndev->nd_parms);
                ndev->dead = 1;
                in6_dev_finish_destroy(ndev);
@@ -314,7 +359,7 @@ static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
        if (snmp6_register_dev(ndev) < 0) {
                ADBG((KERN_WARNING
                        "%s(): cannot create /proc/net/dev_snmp6/%s\n",
-                       __FUNCTION__, dev->name));
+                       __func__, dev->name));
                neigh_parms_release(&nd_tbl, ndev->nd_parms);
                ndev->dead = 1;
                in6_dev_finish_destroy(ndev);
@@ -327,9 +372,7 @@ static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
        in6_dev_hold(ndev);
 
 #ifdef CONFIG_IPV6_PRIVACY
-       init_timer(&ndev->regen_timer);
-       ndev->regen_timer.function = ipv6_regen_rndid;
-       ndev->regen_timer.data = (unsigned long) ndev;
+       setup_timer(&ndev->regen_timer, ipv6_regen_rndid, (unsigned long)ndev);
        if ((dev->flags&IFF_LOOPBACK) ||
            dev->type == ARPHRD_TUNNEL ||
 #if defined(CONFIG_IPV6_SIT) || defined(CONFIG_IPV6_SIT_MODULE)
@@ -340,24 +383,25 @@ static struct inet6_dev * ipv6_add_dev(struct net_device *dev)
                       "%s: Disabled Privacy Extensions\n",
                       dev->name);
                ndev->cnf.use_tempaddr = -1;
+
+               if (dev->type == ARPHRD_SIT && (dev->priv_flags & IFF_ISATAP)) {
+                       printk(KERN_INFO
+                              "%s: Disabled Multicast RS\n",
+                              dev->name);
+                       ndev->cnf.rtr_solicits = 0;
+               }
        } else {
                in6_dev_hold(ndev);
                ipv6_regen_rndid((unsigned long) ndev);
        }
 #endif
 
-       if (netif_running(dev) && netif_carrier_ok(dev))
+       if (netif_running(dev) && addrconf_qdisc_ok(dev))
                ndev->if_flags |= IF_READY;
 
        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,
-                             NULL);
-       addrconf_sysctl_register(ndev, &ndev->cnf);
-#endif
+       addrconf_sysctl_register(ndev);
        /* protected by rtnl_lock */
        rcu_assign_pointer(dev->ip6_ptr, ndev);
 
@@ -413,18 +457,18 @@ static void dev_forward_change(struct inet6_dev *idev)
 }
 
 
-static void addrconf_forward_change(void)
+static void addrconf_forward_change(struct net *net, __s32 newf)
 {
        struct net_device *dev;
        struct inet6_dev *idev;
 
        read_lock(&dev_base_lock);
-       for (dev=dev_base; dev; dev=dev->next) {
+       for_each_netdev(net, dev) {
                rcu_read_lock();
                idev = __in6_dev_get(dev);
                if (idev) {
-                       int changed = (!idev->cnf.forwarding) ^ (!ipv6_devconf.forwarding);
-                       idev->cnf.forwarding = ipv6_devconf.forwarding;
+                       int changed = (!idev->cnf.forwarding) ^ (!newf);
+                       idev->cnf.forwarding = newf;
                        if (changed)
                                dev_forward_change(idev);
                }
@@ -432,6 +476,25 @@ static void addrconf_forward_change(void)
        }
        read_unlock(&dev_base_lock);
 }
+
+static void addrconf_fixup_forwarding(struct ctl_table *table, int *p, int old)
+{
+       struct net *net;
+
+       net = (struct net *)table->extra2;
+       if (p == &net->ipv6.devconf_dflt->forwarding)
+               return;
+
+       if (p == &net->ipv6.devconf_all->forwarding) {
+               __s32 newf = net->ipv6.devconf_all->forwarding;
+               net->ipv6.devconf_dflt->forwarding = newf;
+               addrconf_forward_change(net, newf);
+       } else if ((!*p) ^ (!old))
+               dev_forward_change((struct inet6_dev *)table->extra1);
+
+       if (*p)
+               rt6_purge_dflt_routers(net);
+}
 #endif
 
 /* Nobody refers to this ifaddr, destroy it */
@@ -498,7 +561,7 @@ ipv6_add_addr(struct inet6_dev *idev, const struct in6_addr *addr, int pfxlen,
        write_lock(&addrconf_hash_lock);
 
        /* Ignore adding duplicate addresses on an interface */
-       if (ipv6_chk_same_addr(addr, idev->dev)) {
+       if (ipv6_chk_same_addr(dev_net(idev->dev), addr, idev->dev)) {
                ADBG(("ipv6_add_addr: already assigned\n"));
                err = -EEXIST;
                goto out;
@@ -688,9 +751,9 @@ static void ipv6_del_addr(struct inet6_ifaddr *ifp)
        if ((ifp->flags & IFA_F_PERMANENT) && onlink < 1) {
                struct in6_addr prefix;
                struct rt6_info *rt;
-
+               struct net *net = dev_net(ifp->idev->dev);
                ipv6_addr_prefix(&prefix, &ifp->addr, ifp->prefix_len);
-               rt = rt6_lookup(&prefix, NULL, ifp->idev->dev->ifindex, 1);
+               rt = rt6_lookup(net, &prefix, NULL, ifp->idev->dev->ifindex, 1);
 
                if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
                        if (onlink == 0) {
@@ -713,6 +776,7 @@ static int ipv6_create_tempaddr(struct inet6_ifaddr *ifp, struct inet6_ifaddr *i
        struct inet6_dev *idev = ifp->idev;
        struct in6_addr addr, *tmpaddr;
        unsigned long tmp_prefered_lft, tmp_valid_lft, tmp_cstamp, tmp_tstamp;
+       unsigned long regen_advance;
        int tmp_plen;
        int ret = 0;
        int max_addresses;
@@ -773,8 +837,23 @@ retry:
        tmp_tstamp = ifp->tstamp;
        spin_unlock_bh(&ifp->lock);
 
+       regen_advance = idev->cnf.regen_max_retry *
+                       idev->cnf.dad_transmits *
+                       idev->nd_parms->retrans_time / HZ;
        write_unlock(&idev->lock);
 
+       /* A temporary address is created only if this calculated Preferred
+        * Lifetime is greater than REGEN_ADVANCE time units.  In particular,
+        * an implementation must not create a temporary address with a zero
+        * Preferred Lifetime.
+        */
+       if (tmp_prefered_lft <= regen_advance) {
+               in6_ifa_put(ifp);
+               in6_dev_put(idev);
+               ret = -1;
+               goto out;
+       }
+
        addr_flags = IFA_F_TEMPORARY;
        /* set in addrconf_prefix_rcv() */
        if (ifp->flags & IFA_F_OPTIMISTIC)
@@ -814,20 +893,40 @@ out:
 /*
  *     Choose an appropriate source address (RFC3484)
  */
+enum {
+       IPV6_SADDR_RULE_INIT = 0,
+       IPV6_SADDR_RULE_LOCAL,
+       IPV6_SADDR_RULE_SCOPE,
+       IPV6_SADDR_RULE_PREFERRED,
+#ifdef CONFIG_IPV6_MIP6
+       IPV6_SADDR_RULE_HOA,
+#endif
+       IPV6_SADDR_RULE_OIF,
+       IPV6_SADDR_RULE_LABEL,
+#ifdef CONFIG_IPV6_PRIVACY
+       IPV6_SADDR_RULE_PRIVACY,
+#endif
+       IPV6_SADDR_RULE_ORCHID,
+       IPV6_SADDR_RULE_PREFIX,
+       IPV6_SADDR_RULE_MAX
+};
+
 struct ipv6_saddr_score {
-       int             addr_type;
-       unsigned int    attrs;
-       int             matchlen;
-       int             scope;
-       unsigned int    rule;
+       int                     rule;
+       int                     addr_type;
+       struct inet6_ifaddr     *ifa;
+       DECLARE_BITMAP(scorebits, IPV6_SADDR_RULE_MAX);
+       int                     scopedist;
+       int                     matchlen;
 };
 
-#define IPV6_SADDR_SCORE_LOCAL         0x0001
-#define IPV6_SADDR_SCORE_PREFERRED     0x0004
-#define IPV6_SADDR_SCORE_HOA           0x0008
-#define IPV6_SADDR_SCORE_OIF           0x0010
-#define IPV6_SADDR_SCORE_LABEL         0x0020
-#define IPV6_SADDR_SCORE_PRIVACY       0x0040
+struct ipv6_saddr_dst {
+       struct in6_addr *addr;
+       int ifindex;
+       int scope;
+       int label;
+       unsigned int prefs;
+};
 
 static inline int ipv6_saddr_preferred(int type)
 {
@@ -837,55 +936,152 @@ static inline int ipv6_saddr_preferred(int type)
        return 0;
 }
 
-/* static matching label */
-static inline int ipv6_saddr_label(const struct in6_addr *addr, int type)
+static int ipv6_get_saddr_eval(struct ipv6_saddr_score *score,
+                              struct ipv6_saddr_dst *dst,
+                              int i)
 {
- /*
-  *    prefix (longest match)  label
-  *    -----------------------------
-  *    ::1/128                 0
-  *    ::/0                    1
-  *    2002::/16               2
-  *    ::/96                   3
-  *    ::ffff:0:0/96           4
-  *    fc00::/7                5
-  *    2001::/32               6
-  */
-       if (type & IPV6_ADDR_LOOPBACK)
-               return 0;
-       else if (type & IPV6_ADDR_COMPATv4)
-               return 3;
-       else if (type & IPV6_ADDR_MAPPED)
-               return 4;
-       else if (addr->s6_addr32[0] == htonl(0x20010000))
-               return 6;
-       else if (addr->s6_addr16[0] == htons(0x2002))
-               return 2;
-       else if ((addr->s6_addr[0] & 0xfe) == 0xfc)
-               return 5;
-       return 1;
+       int ret;
+
+       if (i <= score->rule) {
+               switch (i) {
+               case IPV6_SADDR_RULE_SCOPE:
+                       ret = score->scopedist;
+                       break;
+               case IPV6_SADDR_RULE_PREFIX:
+                       ret = score->matchlen;
+                       break;
+               default:
+                       ret = !!test_bit(i, score->scorebits);
+               }
+               goto out;
+       }
+
+       switch (i) {
+       case IPV6_SADDR_RULE_INIT:
+               /* Rule 0: remember if hiscore is not ready yet */
+               ret = !!score->ifa;
+               break;
+       case IPV6_SADDR_RULE_LOCAL:
+               /* Rule 1: Prefer same address */
+               ret = ipv6_addr_equal(&score->ifa->addr, dst->addr);
+               break;
+       case IPV6_SADDR_RULE_SCOPE:
+               /* Rule 2: Prefer appropriate scope
+                *
+                *      ret
+                *       ^
+                *    -1 |  d 15
+                *    ---+--+-+---> scope
+                *       |
+                *       |             d is scope of the destination.
+                *  B-d  |  \
+                *       |   \      <- smaller scope is better if
+                *  B-15 |    \        if scope is enough for destinaion.
+                *       |             ret = B - scope (-1 <= scope >= d <= 15).
+                * d-C-1 | /
+                *       |/         <- greater is better
+                *   -C  /             if scope is not enough for destination.
+                *      /|             ret = scope - C (-1 <= d < scope <= 15).
+                *
+                * d - C - 1 < B -15 (for all -1 <= d <= 15).
+                * C > d + 14 - B >= 15 + 14 - B = 29 - B.
+                * Assume B = 0 and we get C > 29.
+                */
+               ret = __ipv6_addr_src_scope(score->addr_type);
+               if (ret >= dst->scope)
+                       ret = -ret;
+               else
+                       ret -= 128;     /* 30 is enough */
+               score->scopedist = ret;
+               break;
+       case IPV6_SADDR_RULE_PREFERRED:
+               /* Rule 3: Avoid deprecated and optimistic addresses */
+               ret = ipv6_saddr_preferred(score->addr_type) ||
+                     !(score->ifa->flags & (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC));
+               break;
+#ifdef CONFIG_IPV6_MIP6
+       case IPV6_SADDR_RULE_HOA:
+           {
+               /* Rule 4: Prefer home address */
+               int prefhome = !(dst->prefs & IPV6_PREFER_SRC_COA);
+               ret = !(score->ifa->flags & IFA_F_HOMEADDRESS) ^ prefhome;
+               break;
+           }
+#endif
+       case IPV6_SADDR_RULE_OIF:
+               /* Rule 5: Prefer outgoing interface */
+               ret = (!dst->ifindex ||
+                      dst->ifindex == score->ifa->idev->dev->ifindex);
+               break;
+       case IPV6_SADDR_RULE_LABEL:
+               /* Rule 6: Prefer matching label */
+               ret = ipv6_addr_label(&score->ifa->addr, score->addr_type,
+                                     score->ifa->idev->dev->ifindex) == dst->label;
+               break;
+#ifdef CONFIG_IPV6_PRIVACY
+       case IPV6_SADDR_RULE_PRIVACY:
+           {
+               /* Rule 7: Prefer public address
+                * Note: prefer temprary address if use_tempaddr >= 2
+                */
+               int preftmp = dst->prefs & (IPV6_PREFER_SRC_PUBLIC|IPV6_PREFER_SRC_TMP) ?
+                               !!(dst->prefs & IPV6_PREFER_SRC_TMP) :
+                               score->ifa->idev->cnf.use_tempaddr >= 2;
+               ret = (!(score->ifa->flags & IFA_F_TEMPORARY)) ^ preftmp;
+               break;
+           }
+#endif
+       case IPV6_SADDR_RULE_ORCHID:
+               /* Rule 8-: Prefer ORCHID vs ORCHID or
+                *          non-ORCHID vs non-ORCHID
+                */
+               ret = !(ipv6_addr_orchid(&score->ifa->addr) ^
+                       ipv6_addr_orchid(dst->addr));
+               break;
+       case IPV6_SADDR_RULE_PREFIX:
+               /* Rule 8: Use longest matching prefix */
+               score->matchlen = ret = ipv6_addr_diff(&score->ifa->addr,
+                                                      dst->addr);
+               break;
+       default:
+               ret = 0;
+       }
+
+       if (ret)
+               __set_bit(i, score->scorebits);
+       score->rule = i;
+out:
+       return ret;
 }
 
-int ipv6_dev_get_saddr(struct net_device *daddr_dev,
-                      struct in6_addr *daddr, struct in6_addr *saddr)
+int ipv6_dev_get_saddr(struct net_device *dst_dev,
+                      struct in6_addr *daddr, unsigned int prefs,
+                      struct in6_addr *saddr)
 {
-       struct ipv6_saddr_score hiscore;
-       struct inet6_ifaddr *ifa_result = NULL;
-       int daddr_type = __ipv6_addr_type(daddr);
-       int daddr_scope = __ipv6_addr_src_scope(daddr_type);
-       u32 daddr_label = ipv6_saddr_label(daddr, daddr_type);
+       struct ipv6_saddr_score scores[2],
+                               *score = &scores[0], *hiscore = &scores[1];
+       struct net *net = dev_net(dst_dev);
+       struct ipv6_saddr_dst dst;
        struct net_device *dev;
+       int dst_type;
 
-       memset(&hiscore, 0, sizeof(hiscore));
+       dst_type = __ipv6_addr_type(daddr);
+       dst.addr = daddr;
+       dst.ifindex = dst_dev ? dst_dev->ifindex : 0;
+       dst.scope = __ipv6_addr_src_scope(dst_type);
+       dst.label = ipv6_addr_label(daddr, dst_type, dst.ifindex);
+       dst.prefs = prefs;
+
+       hiscore->rule = -1;
+       hiscore->ifa = NULL;
 
        read_lock(&dev_base_lock);
        rcu_read_lock();
 
-       for (dev = dev_base; dev; dev=dev->next) {
+       for_each_netdev(net, dev) {
                struct inet6_dev *idev;
-               struct inet6_ifaddr *ifa;
 
-               /* Rule 0: Candidate Source Address (section 4)
+               /* Candidate Source Address (section 4)
                 *  - multicast and link-local destination address,
                 *    the set of candidate source address MUST only
                 *    include addresses assigned to interfaces
@@ -897,9 +1093,9 @@ int ipv6_dev_get_saddr(struct net_device *daddr_dev,
                 *    belonging to the same site as the outgoing
                 *    interface.)
                 */
-               if ((daddr_type & IPV6_ADDR_MULTICAST ||
-                    daddr_scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
-                   daddr_dev && dev != daddr_dev)
+               if (((dst_type & IPV6_ADDR_MULTICAST) ||
+                    dst.scope <= IPV6_ADDR_SCOPE_LINKLOCAL) &&
+                   dst.ifindex && dev->ifindex != dst.ifindex)
                        continue;
 
                idev = __in6_dev_get(dev);
@@ -907,12 +1103,10 @@ int ipv6_dev_get_saddr(struct net_device *daddr_dev,
                        continue;
 
                read_lock_bh(&idev->lock);
-               for (ifa = idev->addr_list; ifa; ifa = ifa->if_next) {
-                       struct ipv6_saddr_score score;
-
-                       score.addr_type = __ipv6_addr_type(&ifa->addr);
+               for (score->ifa = idev->addr_list; score->ifa; score->ifa = score->ifa->if_next) {
+                       int i;
 
-                       /* Rule 0:
+                       /*
                         * - Tentative Address (RFC2462 section 5.4)
                         *  - A tentative address is not considered
                         *    "assigned to an interface" in the traditional
@@ -922,215 +1116,78 @@ int ipv6_dev_get_saddr(struct net_device *daddr_dev,
                         *    addresses, and the unspecified address MUST
                         *    NOT be included in a candidate set.
                         */
-                       if ((ifa->flags & IFA_F_TENTATIVE) &&
-                           (!(ifa->flags & IFA_F_OPTIMISTIC)))
+                       if ((score->ifa->flags & IFA_F_TENTATIVE) &&
+                           (!(score->ifa->flags & IFA_F_OPTIMISTIC)))
                                continue;
-                       if (unlikely(score.addr_type == IPV6_ADDR_ANY ||
-                                    score.addr_type & IPV6_ADDR_MULTICAST)) {
+
+                       score->addr_type = __ipv6_addr_type(&score->ifa->addr);
+
+                       if (unlikely(score->addr_type == IPV6_ADDR_ANY ||
+                                    score->addr_type & IPV6_ADDR_MULTICAST)) {
                                LIMIT_NETDEBUG(KERN_DEBUG
-                                              "ADDRCONF: unspecified / multicast address"
+                                              "ADDRCONF: unspecified / multicast address "
                                               "assigned as unicast address on %s",
                                               dev->name);
                                continue;
                        }
 
-                       score.attrs = 0;
-                       score.matchlen = 0;
-                       score.scope = 0;
-                       score.rule = 0;
-
-                       if (ifa_result == NULL) {
-                               /* record it if the first available entry */
-                               goto record_it;
-                       }
-
-                       /* Rule 1: Prefer same address */
-                       if (hiscore.rule < 1) {
-                               if (ipv6_addr_equal(&ifa_result->addr, daddr))
-                                       hiscore.attrs |= IPV6_SADDR_SCORE_LOCAL;
-                               hiscore.rule++;
-                       }
-                       if (ipv6_addr_equal(&ifa->addr, daddr)) {
-                               score.attrs |= IPV6_SADDR_SCORE_LOCAL;
-                               if (!(hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)) {
-                                       score.rule = 1;
-                                       goto record_it;
-                               }
-                       } else {
-                               if (hiscore.attrs & IPV6_SADDR_SCORE_LOCAL)
-                                       continue;
-                       }
-
-                       /* Rule 2: Prefer appropriate scope */
-                       if (hiscore.rule < 2) {
-                               hiscore.scope = __ipv6_addr_src_scope(hiscore.addr_type);
-                               hiscore.rule++;
-                       }
-                       score.scope = __ipv6_addr_src_scope(score.addr_type);
-                       if (hiscore.scope < score.scope) {
-                               if (hiscore.scope < daddr_scope) {
-                                       score.rule = 2;
-                                       goto record_it;
-                               } else
-                                       continue;
-                       } else if (score.scope < hiscore.scope) {
-                               if (score.scope < daddr_scope)
-                                       break; /* addresses sorted by scope */
-                               else {
-                                       score.rule = 2;
-                                       goto record_it;
-                               }
-                       }
+                       score->rule = -1;
+                       bitmap_zero(score->scorebits, IPV6_SADDR_RULE_MAX);
+
+                       for (i = 0; i < IPV6_SADDR_RULE_MAX; i++) {
+                               int minihiscore, miniscore;
+
+                               minihiscore = ipv6_get_saddr_eval(hiscore, &dst, i);
+                               miniscore = ipv6_get_saddr_eval(score, &dst, i);
+
+                               if (minihiscore > miniscore) {
+                                       if (i == IPV6_SADDR_RULE_SCOPE &&
+                                           score->scopedist > 0) {
+                                               /*
+                                                * special case:
+                                                * each remaining entry
+                                                * has too small (not enough)
+                                                * scope, because ifa entries
+                                                * are sorted by their scope
+                                                * values.
+                                                */
+                                               goto try_nextdev;
+                                       }
+                                       break;
+                               } else if (minihiscore < miniscore) {
+                                       struct ipv6_saddr_score *tmp;
 
-                       /* Rule 3: Avoid deprecated and optimistic addresses */
-                       if (hiscore.rule < 3) {
-                               if (ipv6_saddr_preferred(hiscore.addr_type) ||
-                                  (((ifa_result->flags &
-                                   (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0)))
-                                       hiscore.attrs |= IPV6_SADDR_SCORE_PREFERRED;
-                               hiscore.rule++;
-                       }
-                       if (ipv6_saddr_preferred(score.addr_type) ||
-                          (((ifa_result->flags &
-                           (IFA_F_DEPRECATED|IFA_F_OPTIMISTIC)) == 0))) {
-                               score.attrs |= IPV6_SADDR_SCORE_PREFERRED;
-                               if (!(hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)) {
-                                       score.rule = 3;
-                                       goto record_it;
-                               }
-                       } else {
-                               if (hiscore.attrs & IPV6_SADDR_SCORE_PREFERRED)
-                                       continue;
-                       }
+                                       if (hiscore->ifa)
+                                               in6_ifa_put(hiscore->ifa);
 
-                       /* Rule 4: Prefer home address */
-#ifdef CONFIG_IPV6_MIP6
-                       if (hiscore.rule < 4) {
-                               if (ifa_result->flags & IFA_F_HOMEADDRESS)
-                                       hiscore.attrs |= IPV6_SADDR_SCORE_HOA;
-                               hiscore.rule++;
-                       }
-                       if (ifa->flags & IFA_F_HOMEADDRESS) {
-                               score.attrs |= IPV6_SADDR_SCORE_HOA;
-                               if (!(ifa_result->flags & IFA_F_HOMEADDRESS)) {
-                                       score.rule = 4;
-                                       goto record_it;
-                               }
-                       } else {
-                               if (hiscore.attrs & IPV6_SADDR_SCORE_HOA)
-                                       continue;
-                       }
-#else
-                       if (hiscore.rule < 4)
-                               hiscore.rule++;
-#endif
+                                       in6_ifa_hold(score->ifa);
 
-                       /* Rule 5: Prefer outgoing interface */
-                       if (hiscore.rule < 5) {
-                               if (daddr_dev == NULL ||
-                                   daddr_dev == ifa_result->idev->dev)
-                                       hiscore.attrs |= IPV6_SADDR_SCORE_OIF;
-                               hiscore.rule++;
-                       }
-                       if (daddr_dev == NULL ||
-                           daddr_dev == ifa->idev->dev) {
-                               score.attrs |= IPV6_SADDR_SCORE_OIF;
-                               if (!(hiscore.attrs & IPV6_SADDR_SCORE_OIF)) {
-                                       score.rule = 5;
-                                       goto record_it;
-                               }
-                       } else {
-                               if (hiscore.attrs & IPV6_SADDR_SCORE_OIF)
-                                       continue;
-                       }
+                                       tmp = hiscore;
+                                       hiscore = score;
+                                       score = tmp;
 
-                       /* Rule 6: Prefer matching label */
-                       if (hiscore.rule < 6) {
-                               if (ipv6_saddr_label(&ifa_result->addr, hiscore.addr_type) == daddr_label)
-                                       hiscore.attrs |= IPV6_SADDR_SCORE_LABEL;
-                               hiscore.rule++;
-                       }
-                       if (ipv6_saddr_label(&ifa->addr, score.addr_type) == daddr_label) {
-                               score.attrs |= IPV6_SADDR_SCORE_LABEL;
-                               if (!(hiscore.attrs & IPV6_SADDR_SCORE_LABEL)) {
-                                       score.rule = 6;
-                                       goto record_it;
-                               }
-                       } else {
-                               if (hiscore.attrs & IPV6_SADDR_SCORE_LABEL)
-                                       continue;
-                       }
+                                       /* restore our iterator */
+                                       score->ifa = hiscore->ifa;
 
-#ifdef CONFIG_IPV6_PRIVACY
-                       /* Rule 7: Prefer public address
-                        * Note: prefer temprary address if use_tempaddr >= 2
-                        */
-                       if (hiscore.rule < 7) {
-                               if ((!(ifa_result->flags & IFA_F_TEMPORARY)) ^
-                                   (ifa_result->idev->cnf.use_tempaddr >= 2))
-                                       hiscore.attrs |= IPV6_SADDR_SCORE_PRIVACY;
-                               hiscore.rule++;
-                       }
-                       if ((!(ifa->flags & IFA_F_TEMPORARY)) ^
-                           (ifa->idev->cnf.use_tempaddr >= 2)) {
-                               score.attrs |= IPV6_SADDR_SCORE_PRIVACY;
-                               if (!(hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)) {
-                                       score.rule = 7;
-                                       goto record_it;
+                                       break;
                                }
-                       } else {
-                               if (hiscore.attrs & IPV6_SADDR_SCORE_PRIVACY)
-                                       continue;
-                       }
-#else
-                       if (hiscore.rule < 7)
-                               hiscore.rule++;
-#endif
-                       /* Rule 8: Use longest matching prefix */
-                       if (hiscore.rule < 8) {
-                               hiscore.matchlen = ipv6_addr_diff(&ifa_result->addr, daddr);
-                               hiscore.rule++;
-                       }
-                       score.matchlen = ipv6_addr_diff(&ifa->addr, daddr);
-                       if (score.matchlen > hiscore.matchlen) {
-                               score.rule = 8;
-                               goto record_it;
                        }
-#if 0
-                       else if (score.matchlen < hiscore.matchlen)
-                               continue;
-#endif
-
-                       /* Final Rule: choose first available one */
-                       continue;
-record_it:
-                       if (ifa_result)
-                               in6_ifa_put(ifa_result);
-                       in6_ifa_hold(ifa);
-                       ifa_result = ifa;
-                       hiscore = score;
                }
+try_nextdev:
                read_unlock_bh(&idev->lock);
        }
        rcu_read_unlock();
        read_unlock(&dev_base_lock);
 
-       if (!ifa_result)
+       if (!hiscore->ifa)
                return -EADDRNOTAVAIL;
 
-       ipv6_addr_copy(saddr, &ifa_result->addr);
-       in6_ifa_put(ifa_result);
+       ipv6_addr_copy(saddr, &hiscore->ifa->addr);
+       in6_ifa_put(hiscore->ifa);
        return 0;
 }
 
-
-int ipv6_get_saddr(struct dst_entry *dst,
-                  struct in6_addr *daddr, struct in6_addr *saddr)
-{
-       return ipv6_dev_get_saddr(dst ? ip6_dst_idev(dst)->dev : NULL, daddr, saddr);
-}
-
-EXPORT_SYMBOL(ipv6_get_saddr);
+EXPORT_SYMBOL(ipv6_dev_get_saddr);
 
 int ipv6_get_lladdr(struct net_device *dev, struct in6_addr *addr,
                    unsigned char banned_flags)
@@ -1168,13 +1225,16 @@ static int ipv6_count_addresses(struct inet6_dev *idev)
        return cnt;
 }
 
-int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
+int ipv6_chk_addr(struct net *net, struct in6_addr *addr,
+                 struct net_device *dev, int strict)
 {
        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 (!net_eq(dev_net(ifp->idev->dev), net))
+                       continue;
                if (ipv6_addr_equal(&ifp->addr, addr) &&
                    !(ifp->flags&IFA_F_TENTATIVE)) {
                        if (dev == NULL || ifp->idev->dev == dev ||
@@ -1185,16 +1245,18 @@ int ipv6_chk_addr(struct in6_addr *addr, struct net_device *dev, int strict)
        read_unlock_bh(&addrconf_hash_lock);
        return ifp != NULL;
 }
-
 EXPORT_SYMBOL(ipv6_chk_addr);
 
 static
-int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
+int ipv6_chk_same_addr(struct net *net, const struct in6_addr *addr,
+                      struct net_device *dev)
 {
        struct inet6_ifaddr * ifp;
        u8 hash = ipv6_addr_hash(addr);
 
        for(ifp = inet6_addr_lst[hash]; ifp; ifp=ifp->lst_next) {
+               if (!net_eq(dev_net(ifp->idev->dev), net))
+                       continue;
                if (ipv6_addr_equal(&ifp->addr, addr)) {
                        if (dev == NULL || ifp->idev->dev == dev)
                                break;
@@ -1203,13 +1265,16 @@ int ipv6_chk_same_addr(const struct in6_addr *addr, struct net_device *dev)
        return ifp != NULL;
 }
 
-struct inet6_ifaddr * ipv6_get_ifaddr(struct in6_addr *addr, struct net_device *dev, int strict)
+struct inet6_ifaddr *ipv6_get_ifaddr(struct net *net, struct in6_addr *addr,
+                                    struct net_device *dev, int strict)
 {
        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 (!net_eq(dev_net(ifp->idev->dev), net))
+                       continue;
                if (ipv6_addr_equal(&ifp->addr, addr)) {
                        if (dev == NULL || ifp->idev->dev == dev ||
                            !(ifp->scope&(IFA_LINK|IFA_HOST) || strict)) {
@@ -1396,6 +1461,9 @@ static int ipv6_generate_eui64(u8 *eui, struct net_device *dev)
                return addrconf_ifid_arcnet(eui, dev);
        case ARPHRD_INFINIBAND:
                return addrconf_ifid_infiniband(eui, dev);
+       case ARPHRD_SIT:
+               if (dev->priv_flags & IFF_ISATAP)
+                       return ipv6_isatap_eui64(eui, *(__be32 *)dev->dev_addr);
        }
        return -1;
 }
@@ -1431,7 +1499,7 @@ regen:
         *
         *  - Reserved subnet anycast (RFC 2526)
         *      11111101 11....11 1xxxxxxx
-        *  - ISATAP (draft-ietf-ngtrans-isatap-13.txt) 5.1
+        *  - ISATAP (RFC4214) 6.1
         *      00-00-5E-FE-xx-xx-xx-xx
         *  - value 0
         *  - XXX: already assigned to an address on the device
@@ -1507,6 +1575,7 @@ addrconf_prefix_route(struct in6_addr *pfx, int plen, struct net_device *dev,
                .fc_expires = expires,
                .fc_dst_len = plen,
                .fc_flags = RTF_UP | flags,
+               .fc_nlinfo.nl_net = dev_net(dev),
        };
 
        ipv6_addr_copy(&cfg.fc_dst, pfx);
@@ -1533,6 +1602,7 @@ static void addrconf_add_mroute(struct net_device *dev)
                .fc_ifindex = dev->ifindex,
                .fc_dst_len = 8,
                .fc_flags = RTF_UP,
+               .fc_nlinfo.nl_net = dev_net(dev),
        };
 
        ipv6_addr_set(&cfg.fc_dst, htonl(0xFF000000), 0, 0, 0);
@@ -1549,6 +1619,7 @@ static void sit_route_add(struct net_device *dev)
                .fc_ifindex = dev->ifindex,
                .fc_dst_len = 96,
                .fc_flags = RTF_UP | RTF_NONEXTHOP,
+               .fc_nlinfo.nl_net = dev_net(dev),
        };
 
        /* prefix length - 96 bits "::d.d.d.d" */
@@ -1649,7 +1720,8 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
 
        if (pinfo->onlink) {
                struct rt6_info *rt;
-               rt = rt6_lookup(&pinfo->prefix, NULL, dev->ifindex, 1);
+               rt = rt6_lookup(dev_net(dev), &pinfo->prefix, NULL,
+                               dev->ifindex, 1);
 
                if (rt && ((rt->rt6i_flags & (RTF_GATEWAY | RTF_DEFAULT)) == 0)) {
                        if (rt->rt6i_flags&RTF_EXPIRES) {
@@ -1692,7 +1764,7 @@ void addrconf_prefix_rcv(struct net_device *dev, u8 *opt, int len)
 
 ok:
 
-               ifp = ipv6_get_ifaddr(&addr, dev, 1);
+               ifp = ipv6_get_ifaddr(dev_net(dev), &addr, dev, 1);
 
                if (ifp == NULL && valid_lft) {
                        int max_addresses = in6_dev->cnf.max_addresses;
@@ -1778,6 +1850,9 @@ ok:
                                 * lifetimes of an existing temporary address
                                 * when processing a Prefix Information Option.
                                 */
+                               if (ifp != ift->ifpub)
+                                       continue;
+
                                spin_lock(&ift->lock);
                                flags = ift->flags;
                                if (ift->valid_lft > valid_lft &&
@@ -1815,7 +1890,7 @@ ok:
  *     Special case for SIT interfaces where we create a new "virtual"
  *     device.
  */
-int addrconf_set_dstaddr(void __user *arg)
+int addrconf_set_dstaddr(struct net *net, void __user *arg)
 {
        struct in6_ifreq ireq;
        struct net_device *dev;
@@ -1827,7 +1902,7 @@ int addrconf_set_dstaddr(void __user *arg)
        if (copy_from_user(&ireq, arg, sizeof(struct in6_ifreq)))
                goto err_exit;
 
-       dev = __dev_get_by_index(ireq.ifr6_ifindex);
+       dev = __dev_get_by_index(net, ireq.ifr6_ifindex);
 
        err = -ENODEV;
        if (dev == NULL)
@@ -1850,7 +1925,7 @@ int addrconf_set_dstaddr(void __user *arg)
                p.iph.ihl = 5;
                p.iph.protocol = IPPROTO_IPV6;
                p.iph.ttl = 64;
-               ifr.ifr_ifru.ifru_data = (void __user *)&p;
+               ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
 
                oldfs = get_fs(); set_fs(KERNEL_DS);
                err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
@@ -1858,7 +1933,8 @@ int addrconf_set_dstaddr(void __user *arg)
 
                if (err == 0) {
                        err = -ENOBUFS;
-                       if ((dev = __dev_get_by_name(p.name)) == NULL)
+                       dev = __dev_get_by_name(net, p.name);
+                       if (!dev)
                                goto err_exit;
                        err = dev_open(dev);
                }
@@ -1873,8 +1949,9 @@ err_exit:
 /*
  *     Manual configuration of address on an interface
  */
-static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
-                         __u8 ifa_flags, __u32 prefered_lft, __u32 valid_lft)
+static int inet6_addr_add(struct net *net, int ifindex, struct in6_addr *pfx,
+                         int plen, __u8 ifa_flags, __u32 prefered_lft,
+                         __u32 valid_lft)
 {
        struct inet6_ifaddr *ifp;
        struct inet6_dev *idev;
@@ -1888,7 +1965,8 @@ static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
        if (!valid_lft || prefered_lft > valid_lft)
                return -EINVAL;
 
-       if ((dev = __dev_get_by_index(ifindex)) == NULL)
+       dev = __dev_get_by_index(net, ifindex);
+       if (!dev)
                return -ENODEV;
 
        if ((idev = addrconf_add_dev(dev)) == NULL)
@@ -1933,13 +2011,15 @@ static int inet6_addr_add(int ifindex, struct in6_addr *pfx, int plen,
        return PTR_ERR(ifp);
 }
 
-static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
+static int inet6_addr_del(struct net *net, int ifindex, struct in6_addr *pfx,
+                         int plen)
 {
        struct inet6_ifaddr *ifp;
        struct inet6_dev *idev;
        struct net_device *dev;
 
-       if ((dev = __dev_get_by_index(ifindex)) == NULL)
+       dev = __dev_get_by_index(net, ifindex);
+       if (!dev)
                return -ENODEV;
 
        if ((idev = __in6_dev_get(dev)) == NULL)
@@ -1967,7 +2047,7 @@ static int inet6_addr_del(int ifindex, struct in6_addr *pfx, int plen)
 }
 
 
-int addrconf_add_ifaddr(void __user *arg)
+int addrconf_add_ifaddr(struct net *net, void __user *arg)
 {
        struct in6_ifreq ireq;
        int err;
@@ -1979,13 +2059,14 @@ int addrconf_add_ifaddr(void __user *arg)
                return -EFAULT;
 
        rtnl_lock();
-       err = inet6_addr_add(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen,
-                            IFA_F_PERMANENT, INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
+       err = inet6_addr_add(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
+                            ireq.ifr6_prefixlen, IFA_F_PERMANENT,
+                            INFINITY_LIFE_TIME, INFINITY_LIFE_TIME);
        rtnl_unlock();
        return err;
 }
 
-int addrconf_del_ifaddr(void __user *arg)
+int addrconf_del_ifaddr(struct net *net, void __user *arg)
 {
        struct in6_ifreq ireq;
        int err;
@@ -1997,7 +2078,8 @@ int addrconf_del_ifaddr(void __user *arg)
                return -EFAULT;
 
        rtnl_lock();
-       err = inet6_addr_del(ireq.ifr6_ifindex, &ireq.ifr6_addr, ireq.ifr6_prefixlen);
+       err = inet6_addr_del(net, ireq.ifr6_ifindex, &ireq.ifr6_addr,
+                            ireq.ifr6_prefixlen);
        rtnl_unlock();
        return err;
 }
@@ -2008,6 +2090,7 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
        struct inet6_ifaddr * ifp;
        struct in6_addr addr;
        struct net_device *dev;
+       struct net *net = dev_net(idev->dev);
        int scope;
 
        ASSERT_RTNL();
@@ -2034,7 +2117,7 @@ static void sit_add_v4_addrs(struct inet6_dev *idev)
                return;
        }
 
-       for (dev = dev_base; dev != NULL; dev = dev->next) {
+       for_each_netdev(net, dev) {
                struct in_device * in_dev = __in_dev_get_rtnl(dev);
                if (in_dev && (dev->flags & IFF_UP)) {
                        struct in_ifaddr * ifa;
@@ -2162,6 +2245,16 @@ static void addrconf_sit_config(struct net_device *dev)
                return;
        }
 
+       if (dev->priv_flags & IFF_ISATAP) {
+               struct in6_addr addr;
+
+               ipv6_addr_set(&addr,  htonl(0xFE800000), 0, 0, 0);
+               addrconf_prefix_route(&addr, 64, dev, 0, 0);
+               if (!ipv6_generate_eui64(addr.s6_addr + 8, dev))
+                       addrconf_add_linklocal(idev, &addr);
+               return;
+       }
+
        sit_add_v4_addrs(idev);
 
        if (dev->flags&IFF_POINTOPOINT) {
@@ -2187,15 +2280,16 @@ ipv6_inherit_linklocal(struct inet6_dev *idev, struct net_device *link_dev)
 static void ip6_tnl_add_linklocal(struct inet6_dev *idev)
 {
        struct net_device *link_dev;
+       struct net *net = dev_net(idev->dev);
 
        /* first try to inherit the link-local address from the link device */
        if (idev->dev->iflink &&
-           (link_dev = __dev_get_by_index(idev->dev->iflink))) {
+           (link_dev = __dev_get_by_index(net, idev->dev->iflink))) {
                if (!ipv6_inherit_linklocal(idev, link_dev))
                        return;
        }
        /* then try to inherit it from any device */
-       for (link_dev = dev_base; link_dev; link_dev = link_dev->next) {
+       for_each_netdev(net, link_dev) {
                if (!ipv6_inherit_linklocal(idev, link_dev))
                        return;
        }
@@ -2226,20 +2320,23 @@ static int addrconf_notify(struct notifier_block *this, unsigned long event,
        struct net_device *dev = (struct net_device *) data;
        struct inet6_dev *idev = __in6_dev_get(dev);
        int run_pending = 0;
+       int err;
 
        switch(event) {
        case NETDEV_REGISTER:
-               if (!idev) {
+               if (!idev && dev->mtu >= IPV6_MIN_MTU) {
                        idev = ipv6_add_dev(dev);
                        if (!idev)
-                               printk(KERN_WARNING "IPv6: add_dev failed for %s\n",
-                                       dev->name);
+                               return notifier_from_errno(-ENOMEM);
                }
                break;
        case NETDEV_UP:
        case NETDEV_CHANGE:
+               if (dev->flags & IFF_SLAVE)
+                       break;
+
                if (event == NETDEV_UP) {
-                       if (!netif_carrier_ok(dev)) {
+                       if (!addrconf_qdisc_ok(dev)) {
                                /* device is not ready yet. */
                                printk(KERN_INFO
                                        "ADDRCONF(NETDEV_UP): %s: "
@@ -2248,10 +2345,13 @@ static int addrconf_notify(struct notifier_block *this, unsigned long event,
                                break;
                        }
 
+                       if (!idev && dev->mtu >= IPV6_MIN_MTU)
+                               idev = ipv6_add_dev(dev);
+
                        if (idev)
                                idev->if_flags |= IF_READY;
                } else {
-                       if (!netif_carrier_ok(dev)) {
+                       if (!addrconf_qdisc_ok(dev)) {
                                /* device is still not ready. */
                                break;
                        }
@@ -2288,7 +2388,7 @@ static int addrconf_notify(struct notifier_block *this, unsigned long event,
                default:
                        addrconf_dev_config(dev);
                        break;
-               };
+               }
                if (idev) {
                        if (run_pending)
                                addrconf_dad_run(idev);
@@ -2312,12 +2412,18 @@ static int addrconf_notify(struct notifier_block *this, unsigned long event,
                break;
 
        case NETDEV_CHANGEMTU:
-               if ( idev && dev->mtu >= IPV6_MIN_MTU) {
+               if (idev && dev->mtu >= IPV6_MIN_MTU) {
                        rt6_mtu_change(dev, dev->mtu);
                        idev->cnf.mtu6 = dev->mtu;
                        break;
                }
 
+               if (!idev && dev->mtu >= IPV6_MIN_MTU) {
+                       idev = ipv6_add_dev(dev);
+                       if (idev)
+                               break;
+               }
+
                /* MTU falled under IPV6_MIN_MTU. Stop IPv6 on this interface. */
 
        case NETDEV_DOWN:
@@ -2329,19 +2435,16 @@ static int addrconf_notify(struct notifier_block *this, unsigned long event,
                break;
 
        case NETDEV_CHANGENAME:
-#ifdef CONFIG_SYSCTL
                if (idev) {
-                       addrconf_sysctl_unregister(&idev->cnf);
-                       neigh_sysctl_unregister(idev->nd_parms);
-                       neigh_sysctl_register(dev, idev->nd_parms,
-                                             NET_IPV6, NET_IPV6_NEIGH, "ipv6",
-                                             &ndisc_ifinfo_sysctl_change,
-                                             NULL);
-                       addrconf_sysctl_register(idev, &idev->cnf);
+                       snmp6_unregister_dev(idev);
+                       addrconf_sysctl_unregister(idev);
+                       addrconf_sysctl_register(idev);
+                       err = snmp6_register_dev(idev);
+                       if (err)
+                               return notifier_from_errno(err);
                }
-#endif
                break;
-       };
+       }
 
        return NOTIFY_OK;
 }
@@ -2358,14 +2461,15 @@ static int addrconf_ifdown(struct net_device *dev, int how)
 {
        struct inet6_dev *idev;
        struct inet6_ifaddr *ifa, **bifa;
+       struct net *net = dev_net(dev);
        int i;
 
        ASSERT_RTNL();
 
-       if (dev == &loopback_dev && how == 1)
+       if (dev == init_net.loopback_dev && how == 1)
                how = 0;
 
-       rt6_ifdown(dev);
+       rt6_ifdown(net, dev);
        neigh_ifdown(&nd_tbl, dev);
 
        idev = __in6_dev_get(dev);
@@ -2440,6 +2544,7 @@ static int addrconf_ifdown(struct net_device *dev, int how)
                write_unlock_bh(&idev->lock);
 
                __ipv6_ifa_notify(RTM_DELADDR, ifa);
+               atomic_notifier_call_chain(&inet6addr_chain, NETDEV_DOWN, ifa);
                in6_ifa_put(ifa);
 
                write_lock_bh(&idev->lock);
@@ -2453,17 +2558,12 @@ static int addrconf_ifdown(struct net_device *dev, int how)
        else
                ipv6_mc_down(idev);
 
-       /* Step 5: netlink notification of this interface */
        idev->tstamp = jiffies;
-       inet6_ifinfo_notify(RTM_DELLINK, idev);
 
        /* Shot the device (if unregistered) */
 
        if (how == 1) {
-#ifdef CONFIG_SYSCTL
-               addrconf_sysctl_unregister(&idev->cnf);
-               neigh_sysctl_unregister(idev->nd_parms);
-#endif
+               addrconf_sysctl_unregister(idev);
                neigh_parms_release(&nd_tbl, idev->nd_parms);
                neigh_ifdown(&nd_tbl, dev);
                in6_dev_put(idev);
@@ -2677,6 +2777,7 @@ static void addrconf_dad_run(struct inet6_dev *idev) {
 
 #ifdef CONFIG_PROC_FS
 struct if6_iter_state {
+       struct seq_net_private p;
        int bucket;
 };
 
@@ -2684,9 +2785,13 @@ static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
 {
        struct inet6_ifaddr *ifa = NULL;
        struct if6_iter_state *state = seq->private;
+       struct net *net = seq_file_net(seq);
 
        for (state->bucket = 0; state->bucket < IN6_ADDR_HSIZE; ++state->bucket) {
                ifa = inet6_addr_lst[state->bucket];
+
+               while (ifa && !net_eq(dev_net(ifa->idev->dev), net))
+                       ifa = ifa->lst_next;
                if (ifa)
                        break;
        }
@@ -2696,13 +2801,22 @@ static struct inet6_ifaddr *if6_get_first(struct seq_file *seq)
 static struct inet6_ifaddr *if6_get_next(struct seq_file *seq, struct inet6_ifaddr *ifa)
 {
        struct if6_iter_state *state = seq->private;
+       struct net *net = seq_file_net(seq);
 
        ifa = ifa->lst_next;
 try_again:
+       if (ifa) {
+               if (!net_eq(dev_net(ifa->idev->dev), net)) {
+                       ifa = ifa->lst_next;
+                       goto try_again;
+               }
+       }
+
        if (!ifa && ++state->bucket < IN6_ADDR_HSIZE) {
                ifa = inet6_addr_lst[state->bucket];
                goto try_again;
        }
+
        return ifa;
 }
 
@@ -2717,6 +2831,7 @@ static struct inet6_ifaddr *if6_get_idx(struct seq_file *seq, loff_t pos)
 }
 
 static void *if6_seq_start(struct seq_file *seq, loff_t *pos)
+       __acquires(addrconf_hash_lock)
 {
        read_lock_bh(&addrconf_hash_lock);
        return if6_get_idx(seq, *pos);
@@ -2732,6 +2847,7 @@ static void *if6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
 }
 
 static void if6_seq_stop(struct seq_file *seq, void *v)
+       __releases(addrconf_hash_lock)
 {
        read_unlock_bh(&addrconf_hash_lock);
 }
@@ -2750,7 +2866,7 @@ static int if6_seq_show(struct seq_file *seq, void *v)
        return 0;
 }
 
-static struct seq_operations if6_seq_ops = {
+static const struct seq_operations if6_seq_ops = {
        .start  = if6_seq_start,
        .next   = if6_seq_next,
        .show   = if6_seq_show,
@@ -2759,24 +2875,8 @@ static struct seq_operations if6_seq_ops = {
 
 static int if6_seq_open(struct inode *inode, struct file *file)
 {
-       struct seq_file *seq;
-       int rc = -ENOMEM;
-       struct if6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
-
-       if (!s)
-               goto out;
-
-       rc = seq_open(file, &if6_seq_ops);
-       if (rc)
-               goto out_kfree;
-
-       seq = file->private_data;
-       seq->private = s;
-out:
-       return rc;
-out_kfree:
-       kfree(s);
-       goto out;
+       return seq_open_net(inode, file, &if6_seq_ops,
+                           sizeof(struct if6_iter_state));
 }
 
 static const struct file_operations if6_fops = {
@@ -2784,31 +2884,48 @@ static const struct file_operations if6_fops = {
        .open           = if6_seq_open,
        .read           = seq_read,
        .llseek         = seq_lseek,
-       .release        = seq_release_private,
+       .release        = seq_release_net,
 };
 
-int __init if6_proc_init(void)
+static int if6_proc_net_init(struct net *net)
 {
-       if (!proc_net_fops_create("if_inet6", S_IRUGO, &if6_fops))
+       if (!proc_net_fops_create(net, "if_inet6", S_IRUGO, &if6_fops))
                return -ENOMEM;
        return 0;
 }
 
+static void if6_proc_net_exit(struct net *net)
+{
+       proc_net_remove(net, "if_inet6");
+}
+
+static struct pernet_operations if6_proc_net_ops = {
+       .init = if6_proc_net_init,
+       .exit = if6_proc_net_exit,
+};
+
+int __init if6_proc_init(void)
+{
+       return register_pernet_subsys(&if6_proc_net_ops);
+}
+
 void if6_proc_exit(void)
 {
-       proc_net_remove("if_inet6");
+       unregister_pernet_subsys(&if6_proc_net_ops);
 }
 #endif /* CONFIG_PROC_FS */
 
-#ifdef CONFIG_IPV6_MIP6
+#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
 /* Check if address is a home address configured on any interface. */
-int ipv6_chk_home_addr(struct in6_addr *addr)
+int ipv6_chk_home_addr(struct net *net, struct in6_addr *addr)
 {
        int ret = 0;
        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 (!net_eq(dev_net(ifp->idev->dev), net))
+                       continue;
                if (ipv6_addr_cmp(&ifp->addr, addr) == 0 &&
                    (ifp->flags & IFA_F_HOMEADDRESS)) {
                        ret = 1;
@@ -2947,7 +3064,7 @@ static struct in6_addr *extract_addr(struct nlattr *addr, struct nlattr *local)
        return pfx;
 }
 
-static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
+static const struct nla_policy ifa_ipv6_policy[IFA_MAX+1] = {
        [IFA_ADDRESS]           = { .len = sizeof(struct in6_addr) },
        [IFA_LOCAL]             = { .len = sizeof(struct in6_addr) },
        [IFA_CACHEINFO]         = { .len = sizeof(struct ifa_cacheinfo) },
@@ -2956,6 +3073,7 @@ static struct nla_policy ifa_ipv6_policy[IFA_MAX+1] __read_mostly = {
 static int
 inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
 {
+       struct net *net = sock_net(skb->sk);
        struct ifaddrmsg *ifm;
        struct nlattr *tb[IFA_MAX+1];
        struct in6_addr *pfx;
@@ -2970,7 +3088,7 @@ inet6_rtm_deladdr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
        if (pfx == NULL)
                return -EINVAL;
 
-       return inet6_addr_del(ifm->ifa_index, pfx, ifm->ifa_prefixlen);
+       return inet6_addr_del(net, ifm->ifa_index, pfx, ifm->ifa_prefixlen);
 }
 
 static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
@@ -3013,6 +3131,7 @@ static int inet6_addr_modify(struct inet6_ifaddr *ifp, u8 ifa_flags,
 static int
 inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
 {
+       struct net *net = sock_net(skb->sk);
        struct ifaddrmsg *ifm;
        struct nlattr *tb[IFA_MAX+1];
        struct in6_addr *pfx;
@@ -3042,21 +3161,22 @@ inet6_rtm_newaddr(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
                valid_lft = INFINITY_LIFE_TIME;
        }
 
-       dev =  __dev_get_by_index(ifm->ifa_index);
+       dev =  __dev_get_by_index(net, ifm->ifa_index);
        if (dev == NULL)
                return -ENODEV;
 
        /* We ignore other flags so far. */
        ifa_flags = ifm->ifa_flags & (IFA_F_NODAD | IFA_F_HOMEADDRESS);
 
-       ifa = ipv6_get_ifaddr(pfx, dev, 1);
+       ifa = ipv6_get_ifaddr(net, pfx, dev, 1);
        if (ifa == NULL) {
                /*
                 * It would be best to check for !NLM_F_CREATE here but
                 * userspace alreay relies on not having to provide this.
                 */
-               return inet6_addr_add(ifm->ifa_index, pfx, ifm->ifa_prefixlen,
-                                     ifa_flags, preferred_lft, valid_lft);
+               return inet6_addr_add(net, ifm->ifa_index, pfx,
+                                     ifm->ifa_prefixlen, ifa_flags,
+                                     preferred_lft, valid_lft);
        }
 
        if (nlh->nlmsg_flags & NLM_F_EXCL ||
@@ -3221,19 +3341,20 @@ static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
        struct inet6_ifaddr *ifa;
        struct ifmcaddr6 *ifmca;
        struct ifacaddr6 *ifaca;
+       struct net *net = sock_net(skb->sk);
 
        s_idx = cb->args[0];
        s_ip_idx = ip_idx = cb->args[1];
-       read_lock(&dev_base_lock);
 
-       for (dev = dev_base, idx = 0; dev; dev = dev->next, idx++) {
+       idx = 0;
+       for_each_netdev(net, dev) {
                if (idx < s_idx)
-                       continue;
+                       goto cont;
                if (idx > s_idx)
                        s_ip_idx = 0;
                ip_idx = 0;
                if ((idev = in6_dev_get(dev)) == NULL)
-                       continue;
+                       goto cont;
                read_lock_bh(&idev->lock);
                switch (type) {
                case UNICAST_ADDR:
@@ -3242,11 +3363,11 @@ static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
                             ifa = ifa->if_next, ip_idx++) {
                                if (ip_idx < s_ip_idx)
                                        continue;
-                               if ((err = inet6_fill_ifaddr(skb, ifa,
-                                   NETLINK_CB(cb->skb).pid,
-                                   cb->nlh->nlmsg_seq, RTM_NEWADDR,
-                                   NLM_F_MULTI)) <= 0)
-                                       goto done;
+                               err = inet6_fill_ifaddr(skb, ifa,
+                                                       NETLINK_CB(cb->skb).pid,
+                                                       cb->nlh->nlmsg_seq,
+                                                       RTM_NEWADDR,
+                                                       NLM_F_MULTI);
                        }
                        break;
                case MULTICAST_ADDR:
@@ -3255,11 +3376,11 @@ static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
                             ifmca = ifmca->next, ip_idx++) {
                                if (ip_idx < s_ip_idx)
                                        continue;
-                               if ((err = inet6_fill_ifmcaddr(skb, ifmca,
-                                   NETLINK_CB(cb->skb).pid,
-                                   cb->nlh->nlmsg_seq, RTM_GETMULTICAST,
-                                   NLM_F_MULTI)) <= 0)
-                                       goto done;
+                               err = inet6_fill_ifmcaddr(skb, ifmca,
+                                                         NETLINK_CB(cb->skb).pid,
+                                                         cb->nlh->nlmsg_seq,
+                                                         RTM_GETMULTICAST,
+                                                         NLM_F_MULTI);
                        }
                        break;
                case ANYCAST_ADDR:
@@ -3268,11 +3389,11 @@ static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
                             ifaca = ifaca->aca_next, ip_idx++) {
                                if (ip_idx < s_ip_idx)
                                        continue;
-                               if ((err = inet6_fill_ifacaddr(skb, ifaca,
-                                   NETLINK_CB(cb->skb).pid,
-                                   cb->nlh->nlmsg_seq, RTM_GETANYCAST,
-                                   NLM_F_MULTI)) <= 0)
-                                       goto done;
+                               err = inet6_fill_ifacaddr(skb, ifaca,
+                                                         NETLINK_CB(cb->skb).pid,
+                                                         cb->nlh->nlmsg_seq,
+                                                         RTM_GETANYCAST,
+                                                         NLM_F_MULTI);
                        }
                        break;
                default:
@@ -3280,13 +3401,12 @@ static int inet6_dump_addr(struct sk_buff *skb, struct netlink_callback *cb,
                }
                read_unlock_bh(&idev->lock);
                in6_dev_put(idev);
+
+               if (err <= 0)
+                       break;
+cont:
+               idx++;
        }
-done:
-       if (err <= 0) {
-               read_unlock_bh(&idev->lock);
-               in6_dev_put(idev);
-       }
-       read_unlock(&dev_base_lock);
        cb->args[0] = idx;
        cb->args[1] = ip_idx;
        return skb->len;
@@ -3295,12 +3415,14 @@ done:
 static int inet6_dump_ifaddr(struct sk_buff *skb, struct netlink_callback *cb)
 {
        enum addr_type_t type = UNICAST_ADDR;
+
        return inet6_dump_addr(skb, cb, type);
 }
 
 static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
 {
        enum addr_type_t type = MULTICAST_ADDR;
+
        return inet6_dump_addr(skb, cb, type);
 }
 
@@ -3308,12 +3430,14 @@ static int inet6_dump_ifmcaddr(struct sk_buff *skb, struct netlink_callback *cb)
 static int inet6_dump_ifacaddr(struct sk_buff *skb, struct netlink_callback *cb)
 {
        enum addr_type_t type = ANYCAST_ADDR;
+
        return inet6_dump_addr(skb, cb, type);
 }
 
 static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
                             void *arg)
 {
+       struct net *net = sock_net(in_skb->sk);
        struct ifaddrmsg *ifm;
        struct nlattr *tb[IFA_MAX+1];
        struct in6_addr *addr = NULL;
@@ -3334,9 +3458,9 @@ static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
 
        ifm = nlmsg_data(nlh);
        if (ifm->ifa_index)
-               dev = __dev_get_by_index(ifm->ifa_index);
+               dev = __dev_get_by_index(net, ifm->ifa_index);
 
-       if ((ifa = ipv6_get_ifaddr(addr, dev, 1)) == NULL) {
+       if ((ifa = ipv6_get_ifaddr(net, addr, dev, 1)) == NULL) {
                err = -EADDRNOTAVAIL;
                goto errout;
        }
@@ -3354,7 +3478,7 @@ static int inet6_rtm_getaddr(struct sk_buff *in_skb, struct nlmsghdr* nlh,
                kfree_skb(skb);
                goto errout_ifa;
        }
-       err = rtnl_unicast(skb, NETLINK_CB(in_skb).pid);
+       err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).pid);
 errout_ifa:
        in6_ifa_put(ifa);
 errout:
@@ -3364,6 +3488,7 @@ errout:
 static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
 {
        struct sk_buff *skb;
+       struct net *net = dev_net(ifa->idev->dev);
        int err = -ENOBUFS;
 
        skb = nlmsg_new(inet6_ifaddr_msgsize(), GFP_ATOMIC);
@@ -3377,10 +3502,10 @@ static void inet6_ifa_notify(int event, struct inet6_ifaddr *ifa)
                kfree_skb(skb);
                goto errout;
        }
-       err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
+       err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
 errout:
        if (err < 0)
-               rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
+               rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
 }
 
 static inline void ipv6_store_devconf(struct ipv6_devconf *cnf,
@@ -3435,14 +3560,44 @@ static inline size_t inet6_if_nlmsg_size(void)
                        nla_total_size(4) /* IFLA_INET6_FLAGS */
                        + nla_total_size(sizeof(struct ifla_cacheinfo))
                        + nla_total_size(DEVCONF_MAX * 4) /* IFLA_INET6_CONF */
+                       + nla_total_size(IPSTATS_MIB_MAX * 8) /* IFLA_INET6_STATS */
+                       + nla_total_size(ICMP6_MIB_MAX * 8) /* IFLA_INET6_ICMP6STATS */
                 );
 }
 
+static inline void __snmp6_fill_stats(u64 *stats, void **mib, int items,
+                                     int bytes)
+{
+       int i;
+       int pad = bytes - sizeof(u64) * items;
+       BUG_ON(pad < 0);
+
+       /* Use put_unaligned() because stats may not be aligned for u64. */
+       put_unaligned(items, &stats[0]);
+       for (i = 1; i < items; i++)
+               put_unaligned(snmp_fold_field(mib, i), &stats[i]);
+
+       memset(&stats[items], 0, pad);
+}
+
+static void snmp6_fill_stats(u64 *stats, struct inet6_dev *idev, int attrtype,
+                            int bytes)
+{
+       switch(attrtype) {
+       case IFLA_INET6_STATS:
+               __snmp6_fill_stats(stats, (void **)idev->stats.ipv6, IPSTATS_MIB_MAX, bytes);
+               break;
+       case IFLA_INET6_ICMP6STATS:
+               __snmp6_fill_stats(stats, (void **)idev->stats.icmpv6, ICMP6_MIB_MAX, bytes);
+               break;
+       }
+}
+
 static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
                             u32 pid, u32 seq, int event, unsigned int flags)
 {
        struct net_device *dev = idev->dev;
-       struct nlattr *conf;
+       struct nlattr *nla;
        struct ifinfomsg *hdr;
        struct nlmsghdr *nlh;
        void *protoinfo;
@@ -3482,12 +3637,22 @@ static int inet6_fill_ifinfo(struct sk_buff *skb, struct inet6_dev *idev,
        ci.retrans_time = idev->nd_parms->retrans_time;
        NLA_PUT(skb, IFLA_INET6_CACHEINFO, sizeof(ci), &ci);
 
-       conf = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
-       if (conf == NULL)
+       nla = nla_reserve(skb, IFLA_INET6_CONF, DEVCONF_MAX * sizeof(s32));
+       if (nla == NULL)
+               goto nla_put_failure;
+       ipv6_store_devconf(&idev->cnf, nla_data(nla), nla_len(nla));
+
+       /* XXX - MC not implemented */
+
+       nla = nla_reserve(skb, IFLA_INET6_STATS, IPSTATS_MIB_MAX * sizeof(u64));
+       if (nla == NULL)
                goto nla_put_failure;
-       ipv6_store_devconf(&idev->cnf, nla_data(conf), nla_len(conf));
+       snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_STATS, nla_len(nla));
 
-       /* XXX - Statistics/MC not implemented */
+       nla = nla_reserve(skb, IFLA_INET6_ICMP6STATS, ICMP6_MIB_MAX * sizeof(u64));
+       if (nla == NULL)
+               goto nla_put_failure;
+       snmp6_fill_stats(nla_data(nla), idev, IFLA_INET6_ICMP6STATS, nla_len(nla));
 
        nla_nest_end(skb, protoinfo);
        return nlmsg_end(skb, nlh);
@@ -3499,22 +3664,26 @@ nla_put_failure:
 
 static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 {
+       struct net *net = sock_net(skb->sk);
        int idx, err;
        int s_idx = cb->args[0];
        struct net_device *dev;
        struct inet6_dev *idev;
 
        read_lock(&dev_base_lock);
-       for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
+       idx = 0;
+       for_each_netdev(net, dev) {
                if (idx < s_idx)
-                       continue;
+                       goto cont;
                if ((idev = in6_dev_get(dev)) == NULL)
-                       continue;
+                       goto cont;
                err = inet6_fill_ifinfo(skb, idev, NETLINK_CB(cb->skb).pid,
                                cb->nlh->nlmsg_seq, RTM_NEWLINK, NLM_F_MULTI);
                in6_dev_put(idev);
                if (err <= 0)
                        break;
+cont:
+               idx++;
        }
        read_unlock(&dev_base_lock);
        cb->args[0] = idx;
@@ -3525,6 +3694,7 @@ static int inet6_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
 void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
 {
        struct sk_buff *skb;
+       struct net *net = dev_net(idev->dev);
        int err = -ENOBUFS;
 
        skb = nlmsg_new(inet6_if_nlmsg_size(), GFP_ATOMIC);
@@ -3538,10 +3708,10 @@ void inet6_ifinfo_notify(int event, struct inet6_dev *idev)
                kfree_skb(skb);
                goto errout;
        }
-       err = rtnl_notify(skb, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
+       err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_IFADDR, NULL, GFP_ATOMIC);
 errout:
        if (err < 0)
-               rtnl_set_sk_err(RTNLGRP_IPV6_IFADDR, err);
+               rtnl_set_sk_err(net, RTNLGRP_IPV6_IFADDR, err);
 }
 
 static inline size_t inet6_prefix_nlmsg_size(void)
@@ -3594,6 +3764,7 @@ static void inet6_prefix_notify(int event, struct inet6_dev *idev,
                         struct prefix_info *pinfo)
 {
        struct sk_buff *skb;
+       struct net *net = dev_net(idev->dev);
        int err = -ENOBUFS;
 
        skb = nlmsg_new(inet6_prefix_nlmsg_size(), GFP_ATOMIC);
@@ -3607,29 +3778,12 @@ static void inet6_prefix_notify(int event, struct inet6_dev *idev,
                kfree_skb(skb);
                goto errout;
        }
-       err = rtnl_notify(skb, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
+       err = rtnl_notify(skb, net, 0, RTNLGRP_IPV6_PREFIX, NULL, GFP_ATOMIC);
 errout:
        if (err < 0)
-               rtnl_set_sk_err(RTNLGRP_IPV6_PREFIX, err);
+               rtnl_set_sk_err(net, RTNLGRP_IPV6_PREFIX, err);
 }
 
-static struct rtnetlink_link inet6_rtnetlink_table[RTM_NR_MSGTYPES] = {
-       [RTM_GETLINK - RTM_BASE] = { .dumpit    = inet6_dump_ifinfo, },
-       [RTM_NEWADDR - RTM_BASE] = { .doit      = inet6_rtm_newaddr, },
-       [RTM_DELADDR - RTM_BASE] = { .doit      = inet6_rtm_deladdr, },
-       [RTM_GETADDR - RTM_BASE] = { .doit      = inet6_rtm_getaddr,
-                                    .dumpit    = inet6_dump_ifaddr, },
-       [RTM_GETMULTICAST - RTM_BASE] = { .dumpit = inet6_dump_ifmcaddr, },
-       [RTM_GETANYCAST - RTM_BASE] = { .dumpit = inet6_dump_ifacaddr, },
-       [RTM_NEWROUTE - RTM_BASE] = { .doit     = inet6_rtm_newroute, },
-       [RTM_DELROUTE - RTM_BASE] = { .doit     = inet6_rtm_delroute, },
-       [RTM_GETROUTE - RTM_BASE] = { .doit     = inet6_rtm_getroute,
-                                     .dumpit   = inet6_dump_fib, },
-#ifdef CONFIG_IPV6_MULTIPLE_TABLES
-       [RTM_GETRULE  - RTM_BASE] = { .dumpit   = fib6_rules_dump,   },
-#endif
-};
-
 static void __ipv6_ifa_notify(int event, struct inet6_ifaddr *ifp)
 {
        inet6_ifa_notify(event ? : RTM_NEWADDR, ifp);
@@ -3678,22 +3832,8 @@ int addrconf_sysctl_forward(ctl_table *ctl, int write, struct file * filp,
 
        ret = proc_dointvec(ctl, write, filp, buffer, lenp, ppos);
 
-       if (write && valp != &ipv6_devconf_dflt.forwarding) {
-               if (valp != &ipv6_devconf.forwarding) {
-                       if ((!*valp) ^ (!val)) {
-                               struct inet6_dev *idev = (struct inet6_dev *)ctl->extra1;
-                               if (idev == NULL)
-                                       return ret;
-                               dev_forward_change(idev);
-                       }
-               } else {
-                       ipv6_devconf_dflt.forwarding = ipv6_devconf.forwarding;
-                       addrconf_forward_change();
-               }
-               if (*valp)
-                       rt6_purge_dflt_routers();
-       }
-
+       if (write)
+               addrconf_fixup_forwarding(ctl, valp, val);
        return ret;
 }
 
@@ -3704,6 +3844,7 @@ static int addrconf_sysctl_forward_strategy(ctl_table *table,
                                            void __user *newval, size_t newlen)
 {
        int *valp = table->data;
+       int val = *valp;
        int new;
 
        if (!newval || !newlen)
@@ -3728,37 +3869,16 @@ static int addrconf_sysctl_forward_strategy(ctl_table *table,
                }
        }
 
-       if (valp != &ipv6_devconf_dflt.forwarding) {
-               if (valp != &ipv6_devconf.forwarding) {
-                       struct inet6_dev *idev = (struct inet6_dev *)table->extra1;
-                       int changed;
-                       if (unlikely(idev == NULL))
-                               return -ENODEV;
-                       changed = (!*valp) ^ (!new);
-                       *valp = new;
-                       if (changed)
-                               dev_forward_change(idev);
-               } else {
-                       *valp = new;
-                       addrconf_forward_change();
-               }
-
-               if (*valp)
-                       rt6_purge_dflt_routers();
-       } else
-               *valp = new;
-
+       *valp = new;
+       addrconf_fixup_forwarding(table, valp, val);
        return 1;
 }
 
 static struct addrconf_sysctl_table
 {
        struct ctl_table_header *sysctl_header;
-       ctl_table addrconf_vars[__NET_IPV6_MAX];
-       ctl_table addrconf_dev[2];
-       ctl_table addrconf_conf_dir[2];
-       ctl_table addrconf_proto_dir[2];
-       ctl_table addrconf_root_dir[2];
+       ctl_table addrconf_vars[DEVCONF_MAX+1];
+       char *dev_name;
 } addrconf_sysctl __read_mostly = {
        .sysctl_header = NULL,
        .addrconf_vars = {
@@ -3979,72 +4099,33 @@ static struct addrconf_sysctl_table
                        .ctl_name       =       0,      /* sentinel */
                }
        },
-       .addrconf_dev = {
-               {
-                       .ctl_name       =       NET_PROTO_CONF_ALL,
-                       .procname       =       "all",
-                       .mode           =       0555,
-                       .child          =       addrconf_sysctl.addrconf_vars,
-               },
-               {
-                       .ctl_name       =       0,      /* sentinel */
-               }
-       },
-       .addrconf_conf_dir = {
-               {
-                       .ctl_name       =       NET_IPV6_CONF,
-                       .procname       =       "conf",
-                       .mode           =       0555,
-                       .child          =       addrconf_sysctl.addrconf_dev,
-               },
-               {
-                       .ctl_name       =       0,      /* sentinel */
-               }
-       },
-       .addrconf_proto_dir = {
-               {
-                       .ctl_name       =       NET_IPV6,
-                       .procname       =       "ipv6",
-                       .mode           =       0555,
-                       .child          =       addrconf_sysctl.addrconf_conf_dir,
-               },
-               {
-                       .ctl_name       =       0,      /* sentinel */
-               }
-       },
-       .addrconf_root_dir = {
-               {
-                       .ctl_name       =       CTL_NET,
-                       .procname       =       "net",
-                       .mode           =       0555,
-                       .child          =       addrconf_sysctl.addrconf_proto_dir,
-               },
-               {
-                       .ctl_name       =       0,      /* sentinel */
-               }
-       },
 };
 
-static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf *p)
+static int __addrconf_sysctl_register(struct net *net, char *dev_name,
+               int ctl_name, struct inet6_dev *idev, struct ipv6_devconf *p)
 {
        int i;
-       struct net_device *dev = idev ? idev->dev : NULL;
        struct addrconf_sysctl_table *t;
-       char *dev_name = NULL;
+
+#define ADDRCONF_CTL_PATH_DEV  3
+
+       struct ctl_path addrconf_ctl_path[] = {
+               { .procname = "net", .ctl_name = CTL_NET, },
+               { .procname = "ipv6", .ctl_name = NET_IPV6, },
+               { .procname = "conf", .ctl_name = NET_IPV6_CONF, },
+               { /* to be set */ },
+               { },
+       };
+
 
        t = kmemdup(&addrconf_sysctl, sizeof(*t), GFP_KERNEL);
        if (t == NULL)
-               return;
+               goto out;
+
        for (i=0; t->addrconf_vars[i].data; i++) {
                t->addrconf_vars[i].data += (char*)p - (char*)&ipv6_devconf;
                t->addrconf_vars[i].extra1 = idev; /* embedded; no ref */
-       }
-       if (dev) {
-               dev_name = dev->name;
-               t->addrconf_dev[0].ctl_name = dev->ifindex;
-       } else {
-               dev_name = "default";
-               t->addrconf_dev[0].ctl_name = NET_PROTO_CONF_DEFAULT;
+               t->addrconf_vars[i].extra2 = net;
        }
 
        /*
@@ -4052,47 +4133,126 @@ static void addrconf_sysctl_register(struct inet6_dev *idev, struct ipv6_devconf
         * by sysctl and we wouldn't want anyone to change it under our feet
         * (see SIOCSIFNAME).
         */
-       dev_name = kstrdup(dev_name, GFP_KERNEL);
-       if (!dev_name)
-           goto free;
-
-       t->addrconf_dev[0].procname = dev_name;
+       t->dev_name = kstrdup(dev_name, GFP_KERNEL);
+       if (!t->dev_name)
+               goto free;
 
-       t->addrconf_dev[0].child = t->addrconf_vars;
-       t->addrconf_conf_dir[0].child = t->addrconf_dev;
-       t->addrconf_proto_dir[0].child = t->addrconf_conf_dir;
-       t->addrconf_root_dir[0].child = t->addrconf_proto_dir;
+       addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].procname = t->dev_name;
+       addrconf_ctl_path[ADDRCONF_CTL_PATH_DEV].ctl_name = ctl_name;
 
-       t->sysctl_header = register_sysctl_table(t->addrconf_root_dir);
+       t->sysctl_header = register_net_sysctl_table(net, addrconf_ctl_path,
+                       t->addrconf_vars);
        if (t->sysctl_header == NULL)
                goto free_procname;
-       else
-               p->sysctl = t;
-       return;
 
-       /* error path */
- free_procname:
-       kfree(dev_name);
- free:
+       p->sysctl = t;
+       return 0;
+
+free_procname:
+       kfree(t->dev_name);
+free:
        kfree(t);
+out:
+       return -ENOBUFS;
+}
 
-       return;
+static void __addrconf_sysctl_unregister(struct ipv6_devconf *p)
+{
+       struct addrconf_sysctl_table *t;
+
+       if (p->sysctl == NULL)
+               return;
+
+       t = p->sysctl;
+       p->sysctl = NULL;
+       unregister_sysctl_table(t->sysctl_header);
+       kfree(t->dev_name);
+       kfree(t);
 }
 
-static void addrconf_sysctl_unregister(struct ipv6_devconf *p)
+static void addrconf_sysctl_register(struct inet6_dev *idev)
 {
-       if (p->sysctl) {
-               struct addrconf_sysctl_table *t = p->sysctl;
-               p->sysctl = NULL;
-               unregister_sysctl_table(t->sysctl_header);
-               kfree(t->addrconf_dev[0].procname);
-               kfree(t);
-       }
+       neigh_sysctl_register(idev->dev, idev->nd_parms, NET_IPV6,
+                             NET_IPV6_NEIGH, "ipv6",
+                             &ndisc_ifinfo_sysctl_change,
+                             NULL);
+       __addrconf_sysctl_register(dev_net(idev->dev), idev->dev->name,
+                       idev->dev->ifindex, idev, &idev->cnf);
+}
+
+static void addrconf_sysctl_unregister(struct inet6_dev *idev)
+{
+       __addrconf_sysctl_unregister(&idev->cnf);
+       neigh_sysctl_unregister(idev->nd_parms);
 }
 
 
 #endif
 
+static int addrconf_init_net(struct net *net)
+{
+       int err;
+       struct ipv6_devconf *all, *dflt;
+
+       err = -ENOMEM;
+       all = &ipv6_devconf;
+       dflt = &ipv6_devconf_dflt;
+
+       if (net != &init_net) {
+               all = kmemdup(all, sizeof(ipv6_devconf), GFP_KERNEL);
+               if (all == NULL)
+                       goto err_alloc_all;
+
+               dflt = kmemdup(dflt, sizeof(ipv6_devconf_dflt), GFP_KERNEL);
+               if (dflt == NULL)
+                       goto err_alloc_dflt;
+       }
+
+       net->ipv6.devconf_all = all;
+       net->ipv6.devconf_dflt = dflt;
+
+#ifdef CONFIG_SYSCTL
+       err = __addrconf_sysctl_register(net, "all", NET_PROTO_CONF_ALL,
+                       NULL, all);
+       if (err < 0)
+               goto err_reg_all;
+
+       err = __addrconf_sysctl_register(net, "default", NET_PROTO_CONF_DEFAULT,
+                       NULL, dflt);
+       if (err < 0)
+               goto err_reg_dflt;
+#endif
+       return 0;
+
+#ifdef CONFIG_SYSCTL
+err_reg_dflt:
+       __addrconf_sysctl_unregister(all);
+err_reg_all:
+       kfree(dflt);
+#endif
+err_alloc_dflt:
+       kfree(all);
+err_alloc_all:
+       return err;
+}
+
+static void addrconf_exit_net(struct net *net)
+{
+#ifdef CONFIG_SYSCTL
+       __addrconf_sysctl_unregister(net->ipv6.devconf_dflt);
+       __addrconf_sysctl_unregister(net->ipv6.devconf_all);
+#endif
+       if (net != &init_net) {
+               kfree(net->ipv6.devconf_dflt);
+               kfree(net->ipv6.devconf_all);
+       }
+}
+
+static struct pernet_operations addrconf_ops = {
+       .init = addrconf_init_net,
+       .exit = addrconf_exit_net,
+};
+
 /*
  *      Device notifier
  */
@@ -4111,13 +4271,47 @@ int unregister_inet6addr_notifier(struct notifier_block *nb)
 
 EXPORT_SYMBOL(unregister_inet6addr_notifier);
 
+
+static int addrconf_net_init(struct net *net)
+{
+       return 0;
+}
+
+static void addrconf_net_exit(struct net *net)
+{
+       struct net_device *dev;
+
+       rtnl_lock();
+       /* clean dev list */
+       for_each_netdev(net, dev) {
+               if (__in6_dev_get(dev) == NULL)
+                       continue;
+               addrconf_ifdown(dev, 1);
+       }
+       addrconf_ifdown(net->loopback_dev, 2);
+       rtnl_unlock();
+}
+
+static struct pernet_operations addrconf_net_ops = {
+       .init = addrconf_net_init,
+       .exit = addrconf_net_exit,
+};
+
 /*
  *     Init / cleanup code
  */
 
 int __init addrconf_init(void)
 {
-       int err = 0;
+       int err;
+
+       if ((err = ipv6_addr_label_init()) < 0) {
+               printk(KERN_CRIT "IPv6 Addrconf: cannot initialize default policy table: %d.\n",
+                       err);
+               return err;
+       }
+
+       register_pernet_subsys(&addrconf_ops);
 
        /* The addrconf netdev notifier requires that loopback_dev
         * has it's ipv6 private information allocated and setup
@@ -4138,59 +4332,57 @@ int __init addrconf_init(void)
         * device and it being up should be removed.
         */
        rtnl_lock();
-       if (!ipv6_add_dev(&loopback_dev))
+       if (!ipv6_add_dev(init_net.loopback_dev))
                err = -ENOMEM;
        rtnl_unlock();
        if (err)
-               return err;
+               goto errlo;
 
-       ip6_null_entry.rt6i_idev = in6_dev_get(&loopback_dev);
+       err = register_pernet_device(&addrconf_net_ops);
+       if (err)
+               return err;
 
        register_netdevice_notifier(&ipv6_dev_notf);
 
        addrconf_verify(0);
-       rtnetlink_links[PF_INET6] = inet6_rtnetlink_table;
-#ifdef CONFIG_SYSCTL
-       addrconf_sysctl.sysctl_header =
-               register_sysctl_table(addrconf_sysctl.addrconf_root_dir);
-       addrconf_sysctl_register(NULL, &ipv6_devconf_dflt);
-#endif
+
+       err = __rtnl_register(PF_INET6, RTM_GETLINK, NULL, inet6_dump_ifinfo);
+       if (err < 0)
+               goto errout;
+
+       /* Only the first call to __rtnl_register can fail */
+       __rtnl_register(PF_INET6, RTM_NEWADDR, inet6_rtm_newaddr, NULL);
+       __rtnl_register(PF_INET6, RTM_DELADDR, inet6_rtm_deladdr, NULL);
+       __rtnl_register(PF_INET6, RTM_GETADDR, inet6_rtm_getaddr, inet6_dump_ifaddr);
+       __rtnl_register(PF_INET6, RTM_GETMULTICAST, NULL, inet6_dump_ifmcaddr);
+       __rtnl_register(PF_INET6, RTM_GETANYCAST, NULL, inet6_dump_ifacaddr);
+
+       ipv6_addr_label_rtnl_register();
 
        return 0;
+errout:
+       unregister_netdevice_notifier(&ipv6_dev_notf);
+errlo:
+       unregister_pernet_subsys(&addrconf_ops);
+
+       return err;
 }
 
-void __exit addrconf_cleanup(void)
+void addrconf_cleanup(void)
 {
-       struct net_device *dev;
-       struct inet6_dev *idev;
        struct inet6_ifaddr *ifa;
        int i;
 
        unregister_netdevice_notifier(&ipv6_dev_notf);
+       unregister_pernet_device(&addrconf_net_ops);
 
-       rtnetlink_links[PF_INET6] = NULL;
-#ifdef CONFIG_SYSCTL
-       addrconf_sysctl_unregister(&ipv6_devconf_dflt);
-       addrconf_sysctl_unregister(&ipv6_devconf);
-#endif
+       unregister_pernet_subsys(&addrconf_ops);
 
        rtnl_lock();
 
        /*
-        *      clean dev list.
-        */
-
-       for (dev=dev_base; dev; dev=dev->next) {
-               if ((idev = __in6_dev_get(dev)) == NULL)
-                       continue;
-               addrconf_ifdown(dev, 1);
-       }
-       addrconf_ifdown(&loopback_dev, 2);
-
-       /*
         *      Check hash table.
         */
-
        write_lock_bh(&addrconf_hash_lock);
        for (i=0; i < IN6_ADDR_HSIZE; i++) {
                for (ifa=inet6_addr_lst[i]; ifa; ) {
@@ -4207,10 +4399,7 @@ void __exit addrconf_cleanup(void)
        write_unlock_bh(&addrconf_hash_lock);
 
        del_timer(&addr_chk_timer);
-
        rtnl_unlock();
 
-#ifdef CONFIG_PROC_FS
-       proc_net_remove("if_inet6");
-#endif
+       unregister_pernet_subsys(&addrconf_net_ops);
 }