netns xfrm: per-netns xfrm_policy_byidx hash
[safe/jmp/linux-2.6] / net / xfrm / xfrm_policy.c
index 280f8de..700cdd7 100644 (file)
@@ -34,7 +34,7 @@
 
 #include "xfrm_hash.h"
 
-int sysctl_xfrm_larval_drop __read_mostly;
+int sysctl_xfrm_larval_drop __read_mostly = 1;
 
 #ifdef CONFIG_XFRM_STATISTICS
 DEFINE_SNMP_STAT(struct linux_xfrm_mib, xfrm_statistics) __read_mostly;
@@ -54,7 +54,6 @@ 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);
 
@@ -96,25 +95,52 @@ int xfrm_selector_match(struct xfrm_selector *sel, struct flowi *fl,
        return 0;
 }
 
+static inline struct dst_entry *__xfrm_dst_lookup(int tos,
+                                                 xfrm_address_t *saddr,
+                                                 xfrm_address_t *daddr,
+                                                 int family)
+{
+       struct xfrm_policy_afinfo *afinfo;
+       struct dst_entry *dst;
+
+       afinfo = xfrm_policy_get_afinfo(family);
+       if (unlikely(afinfo == NULL))
+               return ERR_PTR(-EAFNOSUPPORT);
+
+       dst = afinfo->dst_lookup(tos, saddr, daddr);
+
+       xfrm_policy_put_afinfo(afinfo);
+
+       return dst;
+}
+
 static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, int tos,
+                                               xfrm_address_t *prev_saddr,
+                                               xfrm_address_t *prev_daddr,
                                                int family)
 {
        xfrm_address_t *saddr = &x->props.saddr;
        xfrm_address_t *daddr = &x->id.daddr;
-       struct xfrm_policy_afinfo *afinfo;
        struct dst_entry *dst;
 
-       if (x->type->flags & XFRM_TYPE_LOCAL_COADDR)
+       if (x->type->flags & XFRM_TYPE_LOCAL_COADDR) {
                saddr = x->coaddr;
-       if (x->type->flags & XFRM_TYPE_REMOTE_COADDR)
+               daddr = prev_daddr;
+       }
+       if (x->type->flags & XFRM_TYPE_REMOTE_COADDR) {
+               saddr = prev_saddr;
                daddr = x->coaddr;
+       }
 
-       afinfo = xfrm_policy_get_afinfo(family);
-       if (unlikely(afinfo == NULL))
-               return ERR_PTR(-EAFNOSUPPORT);
+       dst = __xfrm_dst_lookup(tos, saddr, daddr, family);
+
+       if (!IS_ERR(dst)) {
+               if (prev_saddr != saddr)
+                       memcpy(prev_saddr, saddr,  sizeof(*prev_saddr));
+               if (prev_daddr != daddr)
+                       memcpy(prev_daddr, daddr,  sizeof(*prev_daddr));
+       }
 
-       dst = afinfo->dst_lookup(tos, saddr, daddr);
-       xfrm_policy_put_afinfo(afinfo);
        return dst;
 }
 
@@ -136,7 +162,7 @@ static void xfrm_policy_timer(unsigned long data)
 
        read_lock(&xp->lock);
 
-       if (xp->dead)
+       if (xp->walk.dead)
                goto out;
 
        dir = xfrm_policy_id2dir(xp->index);
@@ -201,13 +227,15 @@ 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);
                rwlock_init(&policy->lock);
@@ -221,19 +249,19 @@ EXPORT_SYMBOL(xfrm_policy_alloc);
 
 /* Destroy xfrm_policy: descendant resources must be released to this moment. */
 
-void __xfrm_policy_destroy(struct xfrm_policy *policy)
+void xfrm_policy_destroy(struct xfrm_policy *policy)
 {
-       BUG_ON(!policy->dead);
+       BUG_ON(!policy->walk.dead);
 
        BUG_ON(policy->bundles);
 
        if (del_timer(&policy->timer))
                BUG();
 
-       security_xfrm_policy_free(policy);
+       security_xfrm_policy_free(policy->security);
        kfree(policy);
 }
-EXPORT_SYMBOL(__xfrm_policy_destroy);
+EXPORT_SYMBOL(xfrm_policy_destroy);
 
 static void xfrm_policy_gc_kill(struct xfrm_policy *policy)
 {
@@ -267,6 +295,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.
@@ -277,8 +306,8 @@ static void xfrm_policy_kill(struct xfrm_policy *policy)
        int dead;
 
        write_lock_bh(&policy->lock);
-       dead = policy->dead;
-       policy->dead = 1;
+       dead = policy->walk.dead;
+       policy->walk.dead = 1;
        write_unlock_bh(&policy->lock);
 
        if (unlikely(dead)) {
@@ -286,9 +315,9 @@ static void xfrm_policy_kill(struct xfrm_policy *policy)
                return;
        }
 
-       spin_lock(&xfrm_policy_gc_lock);
+       spin_lock_bh(&xfrm_policy_gc_lock);
        hlist_add_head(&policy->bydst, &xfrm_policy_gc_list);
-       spin_unlock(&xfrm_policy_gc_lock);
+       spin_unlock_bh(&xfrm_policy_gc_lock);
 
        schedule_work(&xfrm_policy_gc_work);
 }
@@ -300,7 +329,6 @@ struct xfrm_policy_hash {
 
 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;
 
@@ -331,15 +359,31 @@ static void xfrm_dst_hash_transfer(struct hlist_head *list,
                                   struct hlist_head *ndsttable,
                                   unsigned int nhashmask)
 {
-       struct hlist_node *entry, *tmp;
+       struct hlist_node *entry, *tmp, *entry0 = NULL;
        struct xfrm_policy *pol;
+       unsigned int h0 = 0;
 
+redo:
        hlist_for_each_entry_safe(pol, entry, tmp, list, bydst) {
                unsigned int h;
 
                h = __addr_hash(&pol->selector.daddr, &pol->selector.saddr,
                                pol->family, nhashmask);
-               hlist_add_head(&pol->bydst, ndsttable+h);
+               if (!entry0) {
+                       hlist_del(entry);
+                       hlist_add_head(&pol->bydst, ndsttable+h);
+                       h0 = h;
+               } else {
+                       if (h != h0)
+                               continue;
+                       hlist_del(entry);
+                       hlist_add_after(entry0, &pol->bydst);
+               }
+               entry0 = entry;
+       }
+       if (!hlist_empty(list)) {
+               entry0 = NULL;
+               goto redo;
        }
 }
 
@@ -393,7 +437,7 @@ static void xfrm_byidx_resize(int total)
        unsigned int hmask = xfrm_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 = init_net.xfrm.policy_byidx;
        struct hlist_head *nidx = xfrm_hash_alloc(nsize);
        int i;
 
@@ -405,7 +449,7 @@ 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;
+       init_net.xfrm.policy_byidx = nidx;
        xfrm_idx_hmask = nhashmask;
 
        write_unlock_bh(&xfrm_policy_lock);
@@ -476,7 +520,7 @@ 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;
 
@@ -491,7 +535,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(idx);
                found = 0;
                hlist_for_each_entry(p, entry, list, byidx) {
                        if (p->index == idx) {
@@ -560,14 +604,16 @@ int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl)
        if (delpol) {
                hlist_del(&delpol->bydst);
                hlist_del(&delpol->byidx);
+               list_del(&delpol->walk.all);
                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(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, &init_net.xfrm.policy_all);
        write_unlock_bh(&xfrm_policy_lock);
 
        if (delpol)
@@ -626,13 +672,15 @@ struct xfrm_policy *xfrm_policy_bysel_ctx(u8 type, int dir,
                    xfrm_sec_ctx_match(ctx, pol->security)) {
                        xfrm_pol_hold(pol);
                        if (delete) {
-                               *err = security_xfrm_policy_delete(pol);
+                               *err = security_xfrm_policy_delete(
+                                                               pol->security);
                                if (*err) {
                                        write_unlock_bh(&xfrm_policy_lock);
                                        return pol;
                                }
                                hlist_del(&pol->bydst);
                                hlist_del(&pol->byidx);
+                               list_del(&pol->walk.all);
                                xfrm_policy_count[dir]--;
                        }
                        ret = pol;
@@ -662,19 +710,21 @@ 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(id);
        ret = NULL;
        hlist_for_each_entry(pol, entry, chain, byidx) {
                if (pol->type == type && pol->index == id) {
                        xfrm_pol_hold(pol);
                        if (delete) {
-                               *err = security_xfrm_policy_delete(pol);
+                               *err = security_xfrm_policy_delete(
+                                                               pol->security);
                                if (*err) {
                                        write_unlock_bh(&xfrm_policy_lock);
                                        return pol;
                                }
                                hlist_del(&pol->bydst);
                                hlist_del(&pol->byidx);
+                               list_del(&pol->walk.all);
                                xfrm_policy_count[dir]--;
                        }
                        ret = pol;
@@ -706,10 +756,11 @@ xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
                                     &xfrm_policy_inexact[dir], bydst) {
                        if (pol->type != type)
                                continue;
-                       err = security_xfrm_policy_delete(pol);
+                       err = security_xfrm_policy_delete(pol->security);
                        if (err) {
                                xfrm_audit_policy_delete(pol, 0,
                                                         audit_info->loginuid,
+                                                        audit_info->sessionid,
                                                         audit_info->secid);
                                return err;
                        }
@@ -720,10 +771,12 @@ xfrm_policy_flush_secctx_check(u8 type, struct xfrm_audit *audit_info)
                                             bydst) {
                                if (pol->type != type)
                                        continue;
-                               err = security_xfrm_policy_delete(pol);
+                               err = security_xfrm_policy_delete(
+                                                               pol->security);
                                if (err) {
                                        xfrm_audit_policy_delete(pol, 0,
                                                        audit_info->loginuid,
+                                                       audit_info->sessionid,
                                                        audit_info->secid);
                                        return err;
                                }
@@ -766,6 +819,7 @@ int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
                        write_unlock_bh(&xfrm_policy_lock);
 
                        xfrm_audit_policy_delete(pol, 1, audit_info->loginuid,
+                                                audit_info->sessionid,
                                                 audit_info->secid);
 
                        xfrm_policy_kill(pol);
@@ -784,10 +838,12 @@ int xfrm_policy_flush(u8 type, struct xfrm_audit *audit_info)
                                        continue;
                                hlist_del(&pol->bydst);
                                hlist_del(&pol->byidx);
+                               list_del(&pol->walk.all);
                                write_unlock_bh(&xfrm_policy_lock);
 
                                xfrm_audit_policy_delete(pol, 1,
                                                         audit_info->loginuid,
+                                                        audit_info->sessionid,
                                                         audit_info->secid);
                                xfrm_policy_kill(pol);
                                killed++;
@@ -806,61 +862,72 @@ out:
 }
 EXPORT_SYMBOL(xfrm_policy_flush);
 
-int xfrm_policy_walk(u8 type, int (*func)(struct xfrm_policy *, int, int, void*),
+int xfrm_policy_walk(struct xfrm_policy_walk *walk,
+                    int (*func)(struct xfrm_policy *, int, int, void*),
                     void *data)
 {
-       struct xfrm_policy *pol, *last = NULL;
-       struct hlist_node *entry;
-       int dir, last_dir = 0, count, error;
+       struct xfrm_policy *pol;
+       struct xfrm_policy_walk_entry *x;
+       int error = 0;
 
-       read_lock_bh(&xfrm_policy_lock);
-       count = 0;
+       if (walk->type >= XFRM_POLICY_TYPE_MAX &&
+           walk->type != XFRM_POLICY_TYPE_ANY)
+               return -EINVAL;
 
-       for (dir = 0; dir < 2*XFRM_POLICY_MAX; dir++) {
-               struct hlist_head *table = xfrm_policy_bydst[dir].table;
-               int i;
+       if (list_empty(&walk->walk.all) && walk->seq != 0)
+               return 0;
 
-               hlist_for_each_entry(pol, entry,
-                                    &xfrm_policy_inexact[dir], bydst) {
-                       if (pol->type != type)
-                               continue;
-                       if (last) {
-                               error = func(last, last_dir % XFRM_POLICY_MAX,
-                                            count, data);
-                               if (error)
-                                       goto out;
-                       }
-                       last = pol;
-                       last_dir = dir;
-                       count++;
-               }
-               for (i = xfrm_policy_bydst[dir].hmask; i >= 0; i--) {
-                       hlist_for_each_entry(pol, entry, table + i, bydst) {
-                               if (pol->type != type)
-                                       continue;
-                               if (last) {
-                                       error = func(last, last_dir % XFRM_POLICY_MAX,
-                                                    count, data);
-                                       if (error)
-                                               goto out;
-                               }
-                               last = pol;
-                               last_dir = dir;
-                               count++;
-                       }
+       write_lock_bh(&xfrm_policy_lock);
+       if (list_empty(&walk->walk.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, &init_net.xfrm.policy_all, all) {
+               if (x->dead)
+                       continue;
+               pol = container_of(x, struct xfrm_policy, walk);
+               if (walk->type != XFRM_POLICY_TYPE_ANY &&
+                   walk->type != pol->type)
+                       continue;
+               error = func(pol, xfrm_policy_id2dir(pol->index),
+                            walk->seq, data);
+               if (error) {
+                       list_move_tail(&walk->walk.all, &x->all);
+                       goto out;
                }
+               walk->seq++;
        }
-       if (count == 0) {
+       if (walk->seq == 0) {
                error = -ENOENT;
                goto out;
        }
-       error = func(last, last_dir % XFRM_POLICY_MAX, 0, data);
+       list_del_init(&walk->walk.all);
 out:
-       read_unlock_bh(&xfrm_policy_lock);
+       write_unlock_bh(&xfrm_policy_lock);
        return error;
 }
 EXPORT_SYMBOL(xfrm_policy_walk);
 
+void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type)
+{
+       INIT_LIST_HEAD(&walk->walk.all);
+       walk->walk.dead = 1;
+       walk->type = type;
+       walk->seq = 0;
+}
+EXPORT_SYMBOL(xfrm_policy_walk_init);
+
+void xfrm_policy_walk_done(struct xfrm_policy_walk *walk)
+{
+       if (list_empty(&walk->walk.all))
+               return;
+
+       write_lock_bh(&xfrm_policy_lock);
+       list_del(&walk->walk.all);
+       write_unlock_bh(&xfrm_policy_lock);
+}
+EXPORT_SYMBOL(xfrm_policy_walk_done);
+
 /*
  * Find policy to apply to this flow.
  *
@@ -878,7 +945,8 @@ static int xfrm_policy_match(struct xfrm_policy *pol, struct flowi *fl,
 
        match = xfrm_selector_match(sel, fl, family);
        if (match)
-               ret = security_xfrm_policy_lookup(pol, fl->secid, dir);
+               ret = security_xfrm_policy_lookup(pol->security, fl->secid,
+                                                 dir);
 
        return ret;
 }
@@ -995,8 +1063,9 @@ static struct xfrm_policy *xfrm_sk_policy_lookup(struct sock *sk, int dir, struc
                int err = 0;
 
                if (match) {
-                       err = security_xfrm_policy_lookup(pol, fl->secid,
-                                       policy_to_flow_dir(dir));
+                       err = security_xfrm_policy_lookup(pol->security,
+                                                     fl->secid,
+                                                     policy_to_flow_dir(dir));
                        if (!err)
                                xfrm_pol_hold(pol);
                        else if (err == -ESRCH)
@@ -1015,8 +1084,9 @@ static void __xfrm_policy_link(struct xfrm_policy *pol, int dir)
        struct hlist_head *chain = policy_hash_bysel(&pol->selector,
                                                     pol->family, dir);
 
+       list_add(&pol->walk.all, &init_net.xfrm.policy_all);
        hlist_add_head(&pol->bydst, chain);
-       hlist_add_head(&pol->byidx, xfrm_policy_byidx+idx_hash(pol->index));
+       hlist_add_head(&pol->byidx, init_net.xfrm.policy_byidx+idx_hash(pol->index));
        xfrm_policy_count[dir]++;
        xfrm_pol_hold(pol);
 
@@ -1032,6 +1102,7 @@ static struct xfrm_policy *__xfrm_policy_unlink(struct xfrm_policy *pol,
 
        hlist_del(&pol->bydst);
        hlist_del(&pol->byidx);
+       list_del(&pol->walk.all);
        xfrm_policy_count[dir]--;
 
        return pol;
@@ -1066,7 +1137,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)
@@ -1081,11 +1152,12 @@ 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;
-               if (security_xfrm_policy_clone(old, newp)) {
+               if (security_xfrm_policy_clone(old->security,
+                                              &newp->security)) {
                        kfree(newp);
                        return NULL;  /* ENOMEM */
                }
@@ -1178,6 +1250,8 @@ xfrm_tmpl_resolve_one(struct xfrm_policy *policy, struct flowi *fl,
                                 -EINVAL : -EAGAIN);
                        xfrm_state_put(x);
                }
+               else if (error == -ESRCH)
+                       error = -EAGAIN;
 
                if (!tmpl->optional)
                        goto fail;
@@ -1328,6 +1402,9 @@ static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
        int trailer_len = 0;
        int tos;
        int family = policy->selector.family;
+       xfrm_address_t saddr, daddr;
+
+       xfrm_flowi_addr_get(fl, &saddr, &daddr, family);
 
        tos = xfrm_get_tos(fl, family);
        err = tos;
@@ -1358,7 +1435,8 @@ static struct dst_entry *xfrm_bundle_create(struct xfrm_policy *policy,
 
                if (xfrm[i]->props.mode != XFRM_MODE_TRANSPORT) {
                        family = xfrm[i]->props.family;
-                       dst = xfrm_dst_lookup(xfrm[i], tos, family);
+                       dst = xfrm_dst_lookup(xfrm[i], tos, &saddr, &daddr,
+                                             family);
                        err = PTR_ERR(dst);
                        if (IS_ERR(dst))
                                goto put_states;
@@ -1510,8 +1588,10 @@ restart:
                policy = flow_cache_lookup(fl, dst_orig->ops->family,
                                           dir, xfrm_policy_lookup);
                err = PTR_ERR(policy);
-               if (IS_ERR(policy))
+               if (IS_ERR(policy)) {
+                       XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLERROR);
                        goto dropdst;
+               }
        }
 
        if (!policy)
@@ -1603,17 +1683,18 @@ restart:
                                /* EREMOTE tells the caller to generate
                                 * a one-shot blackhole route.
                                 */
+                               XFRM_INC_STATS(LINUX_MIB_XFRMOUTNOSTATES);
                                xfrm_pol_put(policy);
                                return -EREMOTE;
                        }
                        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);
 
@@ -1649,7 +1730,7 @@ restart:
 
                for (pi = 0; pi < npols; pi++) {
                        read_lock_bh(&pols[pi]->lock);
-                       pol_dead |= pols[pi]->dead;
+                       pol_dead |= pols[pi]->walk.dead;
                        read_unlock_bh(&pols[pi]->lock);
                }
 
@@ -1661,8 +1742,7 @@ restart:
                         * We can't enlist stable bundles either.
                         */
                        write_unlock_bh(&policy->lock);
-                       if (dst)
-                               dst_free(dst);
+                       dst_free(dst);
 
                        if (pol_dead)
                                XFRM_INC_STATS(LINUX_MIB_XFRMOUTPOLDEAD);
@@ -1678,8 +1758,7 @@ restart:
                        err = xfrm_dst_update_origin(dst, fl);
                if (unlikely(err)) {
                        write_unlock_bh(&policy->lock);
-                       if (dst)
-                               dst_free(dst);
+                       dst_free(dst);
                        XFRM_INC_STATS(LINUX_MIB_XFRMOUTBUNDLECHECKERROR);
                        goto error;
                }
@@ -1753,7 +1832,7 @@ xfrm_state_ok(struct xfrm_tmpl *tmpl, struct xfrm_state *x,
                (x->id.spi == tmpl->id.spi || !tmpl->id.spi) &&
                (x->props.reqid == tmpl->reqid || !tmpl->reqid) &&
                x->props.mode == tmpl->mode &&
-               ((tmpl->aalgos & (1<<x->props.aalgo)) ||
+               (tmpl->allalgs || (tmpl->aalgos & (1<<x->props.aalgo)) ||
                 !(xfrm_id_proto_match(tmpl->id.proto, IPSEC_PROTO_ANY))) &&
                !(x->props.mode != XFRM_MODE_TRANSPORT &&
                  xfrm_state_addr_cmp(tmpl, x, family));
@@ -2019,7 +2098,7 @@ static int stale_bundle(struct dst_entry *dst)
 void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev)
 {
        while ((dst = dst->child) && dst->xfrm && dst->dev == dev) {
-               dst->dev = dev->nd_net->loopback_dev;
+               dst->dev = dev_net(dev)->loopback_dev;
                dev_hold(dst->dev);
                dev_put(dev);
        }
@@ -2290,7 +2369,7 @@ static int xfrm_dev_event(struct notifier_block *this, unsigned long event, void
 {
        struct net_device *dev = ptr;
 
-       if (dev->nd_net != &init_net)
+       if (!net_eq(dev_net(dev), &init_net))
                return NOTIFY_DONE;
 
        switch (event) {
@@ -2301,9 +2380,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
@@ -2316,12 +2393,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);
@@ -2329,10 +2407,10 @@ static void __init xfrm_policy_init(void)
        hmask = 8 - 1;
        sz = (hmask+1) * sizeof(struct hlist_head);
 
-       xfrm_policy_byidx = xfrm_hash_alloc(sz);
+       net->xfrm.policy_byidx = xfrm_hash_alloc(sz);
+       if (!net->xfrm.policy_byidx)
+               goto out_byidx;
        xfrm_idx_hmask = hmask;
-       if (!xfrm_policy_byidx)
-               panic("XFRM: failed to allocate byidx hash\n");
 
        for (dir = 0; dir < XFRM_POLICY_MAX * 2; dir++) {
                struct xfrm_policy_hash *htab;
@@ -2346,17 +2424,61 @@ static void __init xfrm_policy_init(void)
                        panic("XFRM: failed to allocate bydst hash\n");
        }
 
-       INIT_WORK(&xfrm_policy_gc_work, xfrm_policy_gc_task);
-       register_netdevice_notifier(&xfrm_dev_notifier);
+       INIT_LIST_HEAD(&net->xfrm.policy_all);
+       if (net_eq(net, &init_net))
+               register_netdevice_notifier(&xfrm_dev_notifier);
+       return 0;
+
+out_byidx:
+       return -ENOMEM;
+}
+
+static void xfrm_policy_fini(struct net *net)
+{
+       unsigned int sz;
+
+       WARN_ON(!list_empty(&net->xfrm.policy_all));
+
+       sz = (xfrm_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();
@@ -2364,8 +2486,8 @@ void __init xfrm_init(void)
 }
 
 #ifdef CONFIG_AUDITSYSCALL
-static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
-                                               struct audit_buffer *audit_buf)
+static void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
+                                        struct audit_buffer *audit_buf)
 {
        struct xfrm_sec_ctx *ctx = xp->security;
        struct xfrm_selector *sel = &xp->selector;
@@ -2376,25 +2498,21 @@ static inline 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);
@@ -2403,14 +2521,14 @@ static inline void xfrm_audit_common_policyinfo(struct xfrm_policy *xp,
 }
 
 void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
-                          u32 auid, u32 secid)
+                          uid_t auid, u32 sessionid, u32 secid)
 {
        struct audit_buffer *audit_buf;
 
        audit_buf = xfrm_audit_start("SPD-add");
        if (audit_buf == NULL)
                return;
-       xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
+       xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
        audit_log_format(audit_buf, " res=%u", result);
        xfrm_audit_common_policyinfo(xp, audit_buf);
        audit_log_end(audit_buf);
@@ -2418,14 +2536,14 @@ void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
 EXPORT_SYMBOL_GPL(xfrm_audit_policy_add);
 
 void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
-                             u32 auid, u32 secid)
+                             uid_t auid, u32 sessionid, u32 secid)
 {
        struct audit_buffer *audit_buf;
 
        audit_buf = xfrm_audit_start("SPD-delete");
        if (audit_buf == NULL)
                return;
-       xfrm_audit_helper_usrinfo(auid, secid, audit_buf);
+       xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
        audit_log_format(audit_buf, " res=%u", result);
        xfrm_audit_common_policyinfo(xp, audit_buf);
        audit_log_end(audit_buf);
@@ -2529,7 +2647,7 @@ static int xfrm_policy_migrate(struct xfrm_policy *pol,
        int i, j, n = 0;
 
        write_lock_bh(&pol->lock);
-       if (unlikely(pol->dead)) {
+       if (unlikely(pol->walk.dead)) {
                /* target policy has been deleted */
                write_unlock_bh(&pol->lock);
                return -ENOENT;
@@ -2600,7 +2718,8 @@ static int xfrm_migrate_check(struct xfrm_migrate *m, int num_migrate)
 }
 
 int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
-                struct xfrm_migrate *m, int num_migrate)
+                struct xfrm_migrate *m, int num_migrate,
+                struct xfrm_kmaddress *k)
 {
        int i, err, nx_cur = 0, nx_new = 0;
        struct xfrm_policy *pol = NULL;
@@ -2644,7 +2763,7 @@ int xfrm_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
        }
 
        /* Stage 5 - announce */
-       km_migrate(sel, dir, type, m, num_migrate);
+       km_migrate(sel, dir, type, m, num_migrate, k);
 
        xfrm_pol_put(pol);