netns xfrm: propagate netns into policy byidx hash
[pandora-kernel.git] / net / xfrm / xfrm_policy.c
index 058f04f..aefbb56 100644 (file)
@@ -46,16 +46,11 @@ EXPORT_SYMBOL(xfrm_cfg_mutex);
 
 static DEFINE_RWLOCK(xfrm_policy_lock);
 
-static struct list_head xfrm_policy_all;
-unsigned int xfrm_policy_count[XFRM_POLICY_MAX*2];
-EXPORT_SYMBOL(xfrm_policy_count);
-
 static DEFINE_RWLOCK(xfrm_policy_afinfo_lock);
 static struct xfrm_policy_afinfo *xfrm_policy_afinfo[NPROTO];
 
 static struct kmem_cache *xfrm_dst_cache __read_mostly;
 
-static struct work_struct xfrm_policy_gc_work;
 static HLIST_HEAD(xfrm_policy_gc_list);
 static DEFINE_SPINLOCK(xfrm_policy_gc_lock);
 
@@ -229,13 +224,14 @@ expired:
  * SPD calls.
  */
 
-struct xfrm_policy *xfrm_policy_alloc(gfp_t gfp)
+struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp)
 {
        struct xfrm_policy *policy;
 
        policy = kzalloc(sizeof(struct xfrm_policy), gfp);
 
        if (policy) {
+               write_pnet(&policy->xp_net, net);
                INIT_LIST_HEAD(&policy->walk.all);
                INIT_HLIST_NODE(&policy->bydst);
                INIT_HLIST_NODE(&policy->byidx);
@@ -296,6 +292,7 @@ static void xfrm_policy_gc_task(struct work_struct *work)
        hlist_for_each_entry_safe(policy, entry, tmp, &gc_list, bydst)
                xfrm_policy_gc_kill(policy);
 }
+static DECLARE_WORK(xfrm_policy_gc_work, xfrm_policy_gc_task);
 
 /* Rule must be locked. Release descentant resources, announce
  * entry dead. The rule must be unlinked from lists to the moment.
@@ -322,38 +319,29 @@ static void xfrm_policy_kill(struct xfrm_policy *policy)
        schedule_work(&xfrm_policy_gc_work);
 }
 
-struct xfrm_policy_hash {
-       struct hlist_head       *table;
-       unsigned int            hmask;
-};
-
-static struct hlist_head xfrm_policy_inexact[XFRM_POLICY_MAX*2];
-static struct xfrm_policy_hash xfrm_policy_bydst[XFRM_POLICY_MAX*2] __read_mostly;
-static struct hlist_head *xfrm_policy_byidx __read_mostly;
-static unsigned int xfrm_idx_hmask __read_mostly;
 static unsigned int xfrm_policy_hashmax __read_mostly = 1 * 1024 * 1024;
 
-static inline unsigned int idx_hash(u32 index)
+static inline unsigned int idx_hash(struct net *net, u32 index)
 {
-       return __idx_hash(index, xfrm_idx_hmask);
+       return __idx_hash(index, net->xfrm.policy_idx_hmask);
 }
 
 static struct hlist_head *policy_hash_bysel(struct xfrm_selector *sel, unsigned short family, int dir)
 {
-       unsigned int hmask = xfrm_policy_bydst[dir].hmask;
+       unsigned int hmask = init_net.xfrm.policy_bydst[dir].hmask;
        unsigned int hash = __sel_hash(sel, family, hmask);
 
        return (hash == hmask + 1 ?
-               &xfrm_policy_inexact[dir] :
-               xfrm_policy_bydst[dir].table + hash);
+               &init_net.xfrm.policy_inexact[dir] :
+               init_net.xfrm.policy_bydst[dir].table + hash);
 }
 
 static struct hlist_head *policy_hash_direct(xfrm_address_t *daddr, xfrm_address_t *saddr, unsigned short family, int dir)
 {
-       unsigned int hmask = xfrm_policy_bydst[dir].hmask;
+       unsigned int hmask = init_net.xfrm.policy_bydst[dir].hmask;
        unsigned int hash = __addr_hash(daddr, saddr, family, hmask);
 
-       return xfrm_policy_bydst[dir].table + hash;
+       return init_net.xfrm.policy_bydst[dir].table + hash;
 }
 
 static void xfrm_dst_hash_transfer(struct hlist_head *list,
@@ -408,12 +396,12 @@ static unsigned long xfrm_new_hash_mask(unsigned int old_hmask)
        return ((old_hmask + 1) << 1) - 1;
 }
 
-static void xfrm_bydst_resize(int dir)
+static void xfrm_bydst_resize(struct net *net, int dir)
 {
-       unsigned int hmask = xfrm_policy_bydst[dir].hmask;
+       unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
        unsigned int nhashmask = xfrm_new_hash_mask(hmask);
        unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
-       struct hlist_head *odst = xfrm_policy_bydst[dir].table;
+       struct hlist_head *odst = net->xfrm.policy_bydst[dir].table;
        struct hlist_head *ndst = xfrm_hash_alloc(nsize);
        int i;
 
@@ -425,20 +413,20 @@ static void xfrm_bydst_resize(int dir)
        for (i = hmask; i >= 0; i--)
                xfrm_dst_hash_transfer(odst + i, ndst, nhashmask);
 
-       xfrm_policy_bydst[dir].table = ndst;
-       xfrm_policy_bydst[dir].hmask = nhashmask;
+       net->xfrm.policy_bydst[dir].table = ndst;
+       net->xfrm.policy_bydst[dir].hmask = nhashmask;
 
        write_unlock_bh(&xfrm_policy_lock);
 
        xfrm_hash_free(odst, (hmask + 1) * sizeof(struct hlist_head));
 }
 
-static void xfrm_byidx_resize(int total)
+static void xfrm_byidx_resize(struct net *net, int total)
 {
-       unsigned int hmask = xfrm_idx_hmask;
+       unsigned int hmask = net->xfrm.policy_idx_hmask;
        unsigned int nhashmask = xfrm_new_hash_mask(hmask);
        unsigned int nsize = (nhashmask + 1) * sizeof(struct hlist_head);
-       struct hlist_head *oidx = xfrm_policy_byidx;
+       struct hlist_head *oidx = net->xfrm.policy_byidx;
        struct hlist_head *nidx = xfrm_hash_alloc(nsize);
        int i;
 
@@ -450,18 +438,18 @@ static void xfrm_byidx_resize(int total)
        for (i = hmask; i >= 0; i--)
                xfrm_idx_hash_transfer(oidx + i, nidx, nhashmask);
 
-       xfrm_policy_byidx = nidx;
-       xfrm_idx_hmask = nhashmask;
+       net->xfrm.policy_byidx = nidx;
+       net->xfrm.policy_idx_hmask = nhashmask;
 
        write_unlock_bh(&xfrm_policy_lock);
 
        xfrm_hash_free(oidx, (hmask + 1) * sizeof(struct hlist_head));
 }
 
-static inline int xfrm_bydst_should_resize(int dir, int *total)
+static inline int xfrm_bydst_should_resize(struct net *net, int dir, int *total)
 {
-       unsigned int cnt = xfrm_policy_count[dir];
-       unsigned int hmask = xfrm_policy_bydst[dir].hmask;
+       unsigned int cnt = net->xfrm.policy_count[dir];
+       unsigned int hmask = net->xfrm.policy_bydst[dir].hmask;
 
        if (total)
                *total += cnt;
@@ -473,9 +461,9 @@ static inline int xfrm_bydst_should_resize(int dir, int *total)
        return 0;
 }
 
-static inline int xfrm_byidx_should_resize(int total)
+static inline int xfrm_byidx_should_resize(struct net *net, int total)
 {
-       unsigned int hmask = xfrm_idx_hmask;
+       unsigned int hmask = net->xfrm.policy_idx_hmask;
 
        if ((hmask + 1) < xfrm_policy_hashmax &&
            total > hmask)
@@ -487,41 +475,40 @@ static inline int xfrm_byidx_should_resize(int total)
 void xfrm_spd_getinfo(struct xfrmk_spdinfo *si)
 {
        read_lock_bh(&xfrm_policy_lock);
-       si->incnt = xfrm_policy_count[XFRM_POLICY_IN];
-       si->outcnt = xfrm_policy_count[XFRM_POLICY_OUT];
-       si->fwdcnt = xfrm_policy_count[XFRM_POLICY_FWD];
-       si->inscnt = xfrm_policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
-       si->outscnt = xfrm_policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
-       si->fwdscnt = xfrm_policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
-       si->spdhcnt = xfrm_idx_hmask;
+       si->incnt = init_net.xfrm.policy_count[XFRM_POLICY_IN];
+       si->outcnt = init_net.xfrm.policy_count[XFRM_POLICY_OUT];
+       si->fwdcnt = init_net.xfrm.policy_count[XFRM_POLICY_FWD];
+       si->inscnt = init_net.xfrm.policy_count[XFRM_POLICY_IN+XFRM_POLICY_MAX];
+       si->outscnt = init_net.xfrm.policy_count[XFRM_POLICY_OUT+XFRM_POLICY_MAX];
+       si->fwdscnt = init_net.xfrm.policy_count[XFRM_POLICY_FWD+XFRM_POLICY_MAX];
+       si->spdhcnt = init_net.xfrm.policy_idx_hmask;
        si->spdhmcnt = xfrm_policy_hashmax;
        read_unlock_bh(&xfrm_policy_lock);
 }
 EXPORT_SYMBOL(xfrm_spd_getinfo);
 
 static DEFINE_MUTEX(hash_resize_mutex);
-static void xfrm_hash_resize(struct work_struct *__unused)
+static void xfrm_hash_resize(struct work_struct *work)
 {
+       struct net *net = container_of(work, struct net, xfrm.policy_hash_work);
        int dir, total;
 
        mutex_lock(&hash_resize_mutex);
 
        total = 0;
        for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
-               if (xfrm_bydst_should_resize(dir, &total))
-                       xfrm_bydst_resize(dir);
+               if (xfrm_bydst_should_resize(net, dir, &total))
+                       xfrm_bydst_resize(net, dir);
        }
-       if (xfrm_byidx_should_resize(total))
-               xfrm_byidx_resize(total);
+       if (xfrm_byidx_should_resize(net, total))
+               xfrm_byidx_resize(net, total);
 
        mutex_unlock(&hash_resize_mutex);
 }
 
-static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
-
 /* Generate new index... KAME seems to generate them ordered by cost
  * of an absolute inpredictability of ordering of rules. This will not pass. */
-static u32 xfrm_gen_index(u8 type, int dir)
+static u32 xfrm_gen_index(int dir)
 {
        static u32 idx_generator;
 
@@ -536,7 +523,7 @@ static u32 xfrm_gen_index(u8 type, int dir)
                idx_generator += 8;
                if (idx == 0)
                        idx = 8;
-               list = xfrm_policy_byidx + idx_hash(idx);
+               list = init_net.xfrm.policy_byidx + idx_hash(&init_net, idx);
                found = 0;
                hlist_for_each_entry(p, entry, list, byidx) {
                        if (p->index == idx) {
@@ -600,27 +587,27 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
        else
                hlist_add_head(&policy->bydst, chain);
        xfrm_pol_hold(policy);
-       xfrm_policy_count[dir]++;
+       init_net.xfrm.policy_count[dir]++;
        atomic_inc(&flow_cache_genid);
        if (delpol) {
                hlist_del(&delpol->bydst);
                hlist_del(&delpol->byidx);
                list_del(&delpol->walk.all);
-               xfrm_policy_count[dir]--;
+               init_net.xfrm.policy_count[dir]--;
        }
-       policy->index = delpol ? delpol->index : xfrm_gen_index(policy->type, dir);
-       hlist_add_head(&policy->byidx, xfrm_policy_byidx+idx_hash(policy->index));
+       policy->index = delpol ? delpol->index : xfrm_gen_index(dir);
+       hlist_add_head(&policy->byidx, init_net.xfrm.policy_byidx+idx_hash(&init_net, policy->index));
        policy->curlft.add_time = get_seconds();
        policy->curlft.use_time = 0;
        if (!mod_timer(&policy->timer, jiffies + HZ))
                xfrm_pol_hold(policy);
-       list_add(&policy->walk.all, &xfrm_policy_all);
+       list_add(&policy->walk.all, &init_net.xfrm.policy_all);
        write_unlock_bh(&xfrm_policy_lock);
 
        if (delpol)
                xfrm_policy_kill(delpol);
-       else if (xfrm_bydst_should_resize(dir, NULL))
-               schedule_work(&xfrm_hash_work);
+       else if (xfrm_bydst_should_resize(&init_net, dir, NULL))
+               schedule_work(&init_net.xfrm.policy_hash_work);
 
        read_lock_bh(&xfrm_policy_lock);
        gc_list = NULL;
@@ -682,7 +669,7 @@ struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
                                hlist_del(&pol->bydst);
                                hlist_del(&pol->byidx);
                                list_del(&pol->walk.all);
-                               xfrm_policy_count[dir]--;
+                               init_net.xfrm.policy_count[dir]--;
                        }
                        ret = pol;
                        break;
@@ -711,7 +698,7 @@ struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
 
        *err = 0;
        write_lock_bh(&xfrm_policy_lock);
-       chain = xfrm_policy_byidx + idx_hash(id);
+       chain = init_net.xfrm.policy_byidx + idx_hash(&init_net, id);
        ret = NULL;
        hlist_for_each_entry(pol, entry, chain, byidx) {
                if (pol->type == type && pol->index == id) {
@@ -726,7 +713,7 @@ struct xfrm_policy *xfrm_policy_byid(u8 type, int dir, u32 id, int delete,
                                hlist_del(&pol->bydst);
                                hlist_del(&pol->byidx);
                                list_del(&pol->walk.all);
-                               xfrm_policy_count[dir]--;
+                               init_net.xfrm.policy_count[dir]--;
                        }
                        ret = pol;
                        break;
@@ -754,7 +741,7 @@ xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
                int i;
 
                hlist_for_each_entry(pol, entry,
-                                    &xfrm_policy_inexact[dir], bydst) {
+                                    &init_net.xfrm.policy_inexact[dir], bydst) {
                        if (pol->type != type)
                                continue;
                        err = security_xfrm_policy_delete(pol->security);
@@ -766,9 +753,9 @@ xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
                                return err;
                        }
                }
-               for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
+               for (i = init_net.xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
                        hlist_for_each_entry(pol, entry,
-                                            xfrm_policy_bydst[dir].table + i,
+                                            init_net.xfrm.policy_bydst[dir].table + i,
                                             bydst) {
                                if (pol->type != type)
                                        continue;
@@ -812,7 +799,7 @@ int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
                killed = 0;
        again1:
                hlist_for_each_entry(pol, entry,
-                                    &xfrm_policy_inexact[dir], bydst) {
+                                    &init_net.xfrm.policy_inexact[dir], bydst) {
                        if (pol->type != type)
                                continue;
                        hlist_del(&pol->bydst);
@@ -830,10 +817,10 @@ int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
                        goto again1;
                }
 
-               for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
+               for (i = init_net.xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
        again2:
                        hlist_for_each_entry(pol, entry,
-                                            xfrm_policy_bydst[dir].table + i,
+                                            init_net.xfrm.policy_bydst[dir].table + i,
                                             bydst) {
                                if (pol->type != type)
                                        continue;
@@ -854,7 +841,7 @@ int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
                        }
                }
 
-               xfrm_policy_count[dir] -= killed;
+               init_net.xfrm.policy_count[dir] -= killed;
        }
        atomic_inc(&flow_cache_genid);
 out:
@@ -880,10 +867,10 @@ int xfrm_policy_walk(struct xfrm_policy_walk *walk,
 
        write_lock_bh(&xfrm_policy_lock);
        if (list_empty(&walk->walk.all))
-               x = list_first_entry(&xfrm_policy_all, struct xfrm_policy_walk_entry, all);
+               x = list_first_entry(&init_net.xfrm.policy_all, struct xfrm_policy_walk_entry, all);
        else
                x = list_entry(&walk->walk.all, struct xfrm_policy_walk_entry, all);
-       list_for_each_entry_from(x, &xfrm_policy_all, all) {
+       list_for_each_entry_from(x, &init_net.xfrm.policy_all, all) {
                if (x->dead)
                        continue;
                pol = container_of(x, struct xfrm_policy, walk);
@@ -985,7 +972,7 @@ static struct xfrm_policy *xfrm_policy_lookup_bytype(u8 type, struct flowi *fl,
                        break;
                }
        }
-       chain = &xfrm_policy_inexact[dir];
+       chain = &init_net.xfrm.policy_inexact[dir];
        hlist_for_each_entry(pol, entry, chain, bydst) {
                err = xfrm_policy_match(pol, fl, type, family, dir);
                if (err) {
@@ -1082,29 +1069,32 @@ static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struc
 
 static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
 {
+       struct net *net = xp_net(pol);
        struct hlist_head *chain = policy_hash_bysel(&pol->selector,
                                                     pol->family, dir);
 
-       list_add(&pol->walk.all, &xfrm_policy_all);
+       list_add(&pol->walk.all, &net->xfrm.policy_all);
        hlist_add_head(&pol->bydst, chain);
-       hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
-       xfrm_policy_count[dir]++;
+       hlist_add_head(&pol->byidx, net->xfrm.policy_byidx+idx_hash(net, pol->index));
+       net->xfrm.policy_count[dir]++;
        xfrm_pol_hold(pol);
 
-       if (xfrm_bydst_should_resize(dir, NULL))
-               schedule_work(&xfrm_hash_work);
+       if (xfrm_bydst_should_resize(net, dir, NULL))
+               schedule_work(&net->xfrm.policy_hash_work);
 }
 
 static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
                                                int dir)
 {
+       struct net *net = xp_net(pol);
+
        if (hlist_unhashed(&pol->bydst))
                return NULL;
 
        hlist_del(&pol->bydst);
        hlist_del(&pol->byidx);
        list_del(&pol->walk.all);
-       xfrm_policy_count[dir]--;
+       net->xfrm.policy_count[dir]--;
 
        return pol;
 }
@@ -1138,7 +1128,7 @@ int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
        sk->sk_policy[dir] = pol;
        if (pol) {
                pol->curlft.add_time = get_seconds();
-               pol->index = xfrm_gen_index(pol->type, XFRM_POLICY_MAX+dir);
+               pol->index = xfrm_gen_index(XFRM_POLICY_MAX+dir);
                __xfrm_policy_link(pol, XFRM_POLICY_MAX+dir);
        }
        if (old_pol)
@@ -1153,7 +1143,7 @@ int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol)
 
 static struct xfrm_policy *clone_policy(struct xfrm_policy *old, int dir)
 {
-       struct xfrm_policy *newp = xfrm_policy_alloc(GFP_ATOMIC);
+       struct xfrm_policy *newp = xfrm_policy_alloc(xp_net(old), GFP_ATOMIC);
 
        if (newp) {
                newp->selector = old->selector;
@@ -1583,7 +1573,7 @@ restart:
        if (!policy) {
                /* To accelerate a bit...  */
                if ((dst_orig->flags & DST_NOXFRM) ||
-                   !xfrm_policy_count[XFRM_POLICY_OUT])
+                   !init_net.xfrm.policy_count[XFRM_POLICY_OUT])
                        goto nopol;
 
                policy = flow_cache_lookup(fl, dst_orig->ops->family,
@@ -1691,11 +1681,11 @@ restart:
                        if (err == -EAGAIN && (flags & XFRM_LOOKUP_WAIT)) {
                                DECLARE_WAITQUEUE(wait, current);
 
-                               add_wait_queue(&km_waitq, &wait);
+                               add_wait_queue(&init_net.xfrm.km_waitq, &wait);
                                set_current_state(TASK_INTERRUPTIBLE);
                                schedule();
                                set_current_state(TASK_RUNNING);
-                               remove_wait_queue(&km_waitq, &wait);
+                               remove_wait_queue(&init_net.xfrm.km_waitq, &wait);
 
                                nx = xfrm_tmpl_resolve(pols, npols, fl, xfrm, family);
 
@@ -2154,11 +2144,11 @@ static void xfrm_prune_bundles(int (*func)(struct dst_entry *))
                int i;
 
                hlist_for_each_entry(pol, entry,
-                                    &xfrm_policy_inexact[dir], bydst)
+                                    &init_net.xfrm.policy_inexact[dir], bydst)
                        prune_one_bundle(pol, func, &gc_list);
 
-               table = xfrm_policy_bydst[dir].table;
-               for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
+               table = init_net.xfrm.policy_bydst[dir].table;
+               for (i = init_net.xfrm.policy_bydst[dir].hmask; i >= 0; i--) {
                        hlist_for_each_entry(pol, entry, table + i, bydst)
                                prune_one_bundle(pol, func, &gc_list);
                }
@@ -2381,9 +2371,7 @@ static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void
 }
 
 static struct notifier_block xfrm_dev_notifier = {
-       xfrm_dev_event,
-       NULL,
-       0
+       .notifier_call  = xfrm_dev_event,
 };
 
 #ifdef CONFIG_XFRM_STATISTICS
@@ -2396,12 +2384,13 @@ static int __init xfrm_statistics_init(void)
 }
 #endif
 
-static void __init xfrm_policy_init(void)
+static int __net_init xfrm_policy_init(struct net *net)
 {
        unsigned int hmask, sz;
        int dir;
 
-       xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
+       if (net_eq(net, &init_net))
+               xfrm_dst_cache = kmem_cache_create("xfrm_dst_cache",
                                           sizeof(struct xfrm_dst),
                                           0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
                                           NULL);
@@ -2409,35 +2398,100 @@ static void __init xfrm_policy_init(void)
        hmask = 8 - 1;
        sz = (hmask+1) * sizeof(struct hlist_head);
 
-       xfrm_policy_byidx = xfrm_hash_alloc(sz);
-       xfrm_idx_hmask = hmask;
-       if (!xfrm_policy_byidx)
-               panic("XFRM: failed to allocate byidx hash\n");
+       net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
+       if (!net->xfrm.policy_byidx)
+               goto out_byidx;
+       net->xfrm.policy_idx_hmask = hmask;
 
        for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
                struct xfrm_policy_hash *htab;
 
-               INIT_HLIST_HEAD(&xfrm_policy_inexact[dir]);
+               net->xfrm.policy_count[dir] = 0;
+               INIT_HLIST_HEAD(&net->xfrm.policy_inexact[dir]);
 
-               htab = &xfrm_policy_bydst[dir];
+               htab = &net->xfrm.policy_bydst[dir];
                htab->table = xfrm_hash_alloc(sz);
-               htab->hmask = hmask;
                if (!htab->table)
-                       panic("XFRM: failed to allocate bydst hash\n");
+                       goto out_bydst;
+               htab->hmask = hmask;
        }
 
-       INIT_LIST_HEAD(&xfrm_policy_all);
-       INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
-       register_netdevice_notifier(&xfrm_dev_notifier);
+       INIT_LIST_HEAD(&net->xfrm.policy_all);
+       INIT_WORK(&net->xfrm.policy_hash_work, xfrm_hash_resize);
+       if (net_eq(net, &init_net))
+               register_netdevice_notifier(&xfrm_dev_notifier);
+       return 0;
+
+out_bydst:
+       for (dir--; dir >= 0; dir--) {
+               struct xfrm_policy_hash *htab;
+
+               htab = &net->xfrm.policy_bydst[dir];
+               xfrm_hash_free(htab->table, sz);
+       }
+       xfrm_hash_free(net->xfrm.policy_byidx, sz);
+out_byidx:
+       return -ENOMEM;
 }
 
+static void xfrm_policy_fini(struct net *net)
+{
+       unsigned int sz;
+       int dir;
+
+       WARN_ON(!list_empty(&net->xfrm.policy_all));
+
+       for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
+               struct xfrm_policy_hash *htab;
+
+               WARN_ON(!hlist_empty(&net->xfrm.policy_inexact[dir]));
+
+               htab = &net->xfrm.policy_bydst[dir];
+               sz = (htab->hmask + 1);
+               WARN_ON(!hlist_empty(htab->table));
+               xfrm_hash_free(htab->table, sz);
+       }
+
+       sz = (net->xfrm.policy_idx_hmask + 1) * sizeof(struct hlist_head);
+       WARN_ON(!hlist_empty(net->xfrm.policy_byidx));
+       xfrm_hash_free(net->xfrm.policy_byidx, sz);
+}
+
+static int __net_init xfrm_net_init(struct net *net)
+{
+       int rv;
+
+       rv = xfrm_state_init(net);
+       if (rv < 0)
+               goto out_state;
+       rv = xfrm_policy_init(net);
+       if (rv < 0)
+               goto out_policy;
+       return 0;
+
+out_policy:
+       xfrm_state_fini(net);
+out_state:
+       return rv;
+}
+
+static void __net_exit xfrm_net_exit(struct net *net)
+{
+       xfrm_policy_fini(net);
+       xfrm_state_fini(net);
+}
+
+static struct pernet_operations __net_initdata xfrm_net_ops = {
+       .init = xfrm_net_init,
+       .exit = xfrm_net_exit,
+};
+
 void __init xfrm_init(void)
 {
+       register_pernet_subsys(&xfrm_net_ops);
 #ifdef CONFIG_XFRM_STATISTICS
        xfrm_statistics_init();
 #endif
-       xfrm_state_init();
-       xfrm_policy_init();
        xfrm_input_init();
 #ifdef CONFIG_XFRM_STATISTICS
        xfrm_proc_init();
@@ -2457,25 +2511,21 @@ static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
 
        switch(sel->family) {
        case AF_INET:
-               audit_log_format(audit_buf, " src=" NIPQUAD_FMT,
-                                NIPQUAD(sel->saddr.a4));
+               audit_log_format(audit_buf, " src=%pI4", &sel->saddr.a4);
                if (sel->prefixlen_s != 32)
                        audit_log_format(audit_buf, " src_prefixlen=%d",
                                         sel->prefixlen_s);
-               audit_log_format(audit_buf, " dst=" NIPQUAD_FMT,
-                                NIPQUAD(sel->daddr.a4));
+               audit_log_format(audit_buf, " dst=%pI4", &sel->daddr.a4);
                if (sel->prefixlen_d != 32)
                        audit_log_format(audit_buf, " dst_prefixlen=%d",
                                         sel->prefixlen_d);
                break;
        case AF_INET6:
-               audit_log_format(audit_buf, " src=" NIP6_FMT,
-                                NIP6(*(struct in6_addr *)sel->saddr.a6));
+               audit_log_format(audit_buf, " src=%pI6", sel->saddr.a6);
                if (sel->prefixlen_s != 128)
                        audit_log_format(audit_buf, " src_prefixlen=%d",
                                         sel->prefixlen_s);
-               audit_log_format(audit_buf, " dst=" NIP6_FMT,
-                                NIP6(*(struct in6_addr *)sel->daddr.a6));
+               audit_log_format(audit_buf, " dst=%pI6", sel->daddr.a6);
                if (sel->prefixlen_d != 128)
                        audit_log_format(audit_buf, " dst_prefixlen=%d",
                                         sel->prefixlen_d);
@@ -2554,7 +2604,7 @@ static struct xfrm_policy * xfrm_migrate_policy_find(struct xfrm_selector *sel,
                        break;
                }
        }
-       chain = &xfrm_policy_inexact[dir];
+       chain = &init_net.xfrm.policy_inexact[dir];
        hlist_for_each_entry(pol, entry, chain, bydst) {
                if (xfrm_migrate_selector_match(sel, &pol->selector) &&
                    pol->type == type &&