netns xfrm: state walking in netns
[safe/jmp/linux-2.6] / net / xfrm / xfrm_state.c
index 52d828b..ea340bb 100644 (file)
@@ -44,10 +44,7 @@ u32 sysctl_xfrm_acq_expires __read_mostly = 30;
 
 static DEFINE_SPINLOCK(xfrm_state_lock);
 
-static struct hlist_head *xfrm_state_byspi __read_mostly;
-static unsigned int xfrm_state_hmask __read_mostly;
 static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
-static unsigned int xfrm_state_num;
 static unsigned int xfrm_state_genid;
 
 static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
@@ -60,25 +57,27 @@ static void xfrm_audit_state_replay(struct xfrm_state *x,
 #define xfrm_audit_state_replay(x, s, sq)      do { ; } while (0)
 #endif /* CONFIG_AUDITSYSCALL */
 
-static inline unsigned int xfrm_dst_hash(xfrm_address_t *daddr,
+static inline unsigned int xfrm_dst_hash(struct net *net,
+                                        xfrm_address_t *daddr,
                                         xfrm_address_t *saddr,
                                         u32 reqid,
                                         unsigned short family)
 {
-       return __xfrm_dst_hash(daddr, saddr, reqid, family, xfrm_state_hmask);
+       return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
 }
 
-static inline unsigned int xfrm_src_hash(xfrm_address_t *daddr,
+static inline unsigned int xfrm_src_hash(struct net *net,
+                                        xfrm_address_t *daddr,
                                         xfrm_address_t *saddr,
                                         unsigned short family)
 {
-       return __xfrm_src_hash(daddr, saddr, family, xfrm_state_hmask);
+       return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
 }
 
 static inline unsigned int
-xfrm_spi_hash(xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
+xfrm_spi_hash(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
 {
-       return __xfrm_spi_hash(daddr, spi, proto, family, xfrm_state_hmask);
+       return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
 }
 
 static void xfrm_hash_transfer(struct hlist_head *list,
@@ -112,16 +111,16 @@ static void xfrm_hash_transfer(struct hlist_head *list,
        }
 }
 
-static unsigned long xfrm_hash_new_size(void)
+static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
 {
-       return ((xfrm_state_hmask + 1) << 1) *
-               sizeof(struct hlist_head);
+       return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
 }
 
 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.state_hash_work);
        struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
        unsigned long nsize, osize;
        unsigned int nhashmask, ohashmask;
@@ -129,7 +128,7 @@ static void xfrm_hash_resize(struct work_struct *__unused)
 
        mutex_lock(&hash_resize_mutex);
 
-       nsize = xfrm_hash_new_size();
+       nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
        ndst = xfrm_hash_alloc(nsize);
        if (!ndst)
                goto out_unlock;
@@ -148,19 +147,19 @@ static void xfrm_hash_resize(struct work_struct *__unused)
        spin_lock_bh(&xfrm_state_lock);
 
        nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
-       for (i = xfrm_state_hmask; i >= 0; i--)
-               xfrm_hash_transfer(init_net.xfrm.state_bydst+i, ndst, nsrc, nspi,
+       for (i = net->xfrm.state_hmask; i >= 0; i--)
+               xfrm_hash_transfer(net->xfrm.state_bydst+i, ndst, nsrc, nspi,
                                   nhashmask);
 
-       odst = init_net.xfrm.state_bydst;
-       osrc = init_net.xfrm.state_bysrc;
-       ospi = xfrm_state_byspi;
-       ohashmask = xfrm_state_hmask;
+       odst = net->xfrm.state_bydst;
+       osrc = net->xfrm.state_bysrc;
+       ospi = net->xfrm.state_byspi;
+       ohashmask = net->xfrm.state_hmask;
 
-       init_net.xfrm.state_bydst = ndst;
-       init_net.xfrm.state_bysrc = nsrc;
-       xfrm_state_byspi = nspi;
-       xfrm_state_hmask = nhashmask;
+       net->xfrm.state_bydst = ndst;
+       net->xfrm.state_bysrc = nsrc;
+       net->xfrm.state_byspi = nspi;
+       net->xfrm.state_hmask = nhashmask;
 
        spin_unlock_bh(&xfrm_state_lock);
 
@@ -173,16 +172,9 @@ out_unlock:
        mutex_unlock(&hash_resize_mutex);
 }
 
-static DECLARE_WORK(xfrm_hash_work, xfrm_hash_resize);
-
-DECLARE_WAIT_QUEUE_HEAD(km_waitq);
-EXPORT_SYMBOL(km_waitq);
-
 static DEFINE_RWLOCK(xfrm_state_afinfo_lock);
 static struct xfrm_state_afinfo *xfrm_state_afinfo[NPROTO];
 
-static struct work_struct xfrm_state_gc_work;
-static HLIST_HEAD(xfrm_state_gc_list);
 static DEFINE_SPINLOCK(xfrm_state_gc_lock);
 
 int __xfrm_state_delete(struct xfrm_state *x);
@@ -392,20 +384,21 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)
        kfree(x);
 }
 
-static void xfrm_state_gc_task(struct work_struct *data)
+static void xfrm_state_gc_task(struct work_struct *work)
 {
+       struct net *net = container_of(work, struct net, xfrm.state_gc_work);
        struct xfrm_state *x;
        struct hlist_node *entry, *tmp;
        struct hlist_head gc_list;
 
        spin_lock_bh(&xfrm_state_gc_lock);
-       hlist_move_list(&xfrm_state_gc_list, &gc_list);
+       hlist_move_list(&net->xfrm.state_gc_list, &gc_list);
        spin_unlock_bh(&xfrm_state_gc_lock);
 
        hlist_for_each_entry_safe(x, entry, tmp, &gc_list, gclist)
                xfrm_state_gc_destroy(x);
 
-       wake_up(&km_waitq);
+       wake_up(&net->xfrm.km_waitq);
 }
 
 static inline unsigned long make_jiffies(long secs)
@@ -419,6 +412,7 @@ static inline unsigned long make_jiffies(long secs)
 static void xfrm_timer_handler(unsigned long data)
 {
        struct xfrm_state *x = (struct xfrm_state*)data;
+       struct net *net = xs_net(x);
        unsigned long now = get_seconds();
        long next = LONG_MAX;
        int warn = 0;
@@ -476,7 +470,7 @@ resched:
 expired:
        if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0) {
                x->km.state = XFRM_STATE_EXPIRED;
-               wake_up(&km_waitq);
+               wake_up(&net->xfrm.km_waitq);
                next = 2;
                goto resched;
        }
@@ -529,17 +523,20 @@ EXPORT_SYMBOL(xfrm_state_alloc);
 
 void __xfrm_state_destroy(struct xfrm_state *x)
 {
+       struct net *net = xs_net(x);
+
        WARN_ON(x->km.state != XFRM_STATE_DEAD);
 
        spin_lock_bh(&xfrm_state_gc_lock);
-       hlist_add_head(&x->gclist, &xfrm_state_gc_list);
+       hlist_add_head(&x->gclist, &net->xfrm.state_gc_list);
        spin_unlock_bh(&xfrm_state_gc_lock);
-       schedule_work(&xfrm_state_gc_work);
+       schedule_work(&net->xfrm.state_gc_work);
 }
 EXPORT_SYMBOL(__xfrm_state_destroy);
 
 int __xfrm_state_delete(struct xfrm_state *x)
 {
+       struct net *net = xs_net(x);
        int err = -ESRCH;
 
        if (x->km.state != XFRM_STATE_DEAD) {
@@ -550,7 +547,7 @@ int __xfrm_state_delete(struct xfrm_state *x)
                hlist_del(&x->bysrc);
                if (x->id.spi)
                        hlist_del(&x->byspi);
-               xfrm_state_num--;
+               net->xfrm.state_num--;
                spin_unlock(&xfrm_state_lock);
 
                /* All xfrm_state objects are created by xfrm_state_alloc.
@@ -579,15 +576,15 @@ EXPORT_SYMBOL(xfrm_state_delete);
 
 #ifdef CONFIG_SECURITY_NETWORK_XFRM
 static inline int
-xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
+xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
 {
        int i, err = 0;
 
-       for (i = 0; i <= xfrm_state_hmask; i++) {
+       for (i = 0; i <= net->xfrm.state_hmask; i++) {
                struct hlist_node *entry;
                struct xfrm_state *x;
 
-               hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+i, bydst) {
+               hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
                        if (xfrm_id_proto_match(x->id.proto, proto) &&
                           (err = security_xfrm_state_delete(x)) != 0) {
                                xfrm_audit_state_delete(x, 0,
@@ -603,26 +600,26 @@ xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
 }
 #else
 static inline int
-xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
+xfrm_state_flush_secctx_check(struct net *net, u8 proto, struct xfrm_audit *audit_info)
 {
        return 0;
 }
 #endif
 
-int xfrm_state_flush(u8 proto, struct xfrm_audit *audit_info)
+int xfrm_state_flush(struct net *net, u8 proto, struct xfrm_audit *audit_info)
 {
        int i, err = 0;
 
        spin_lock_bh(&xfrm_state_lock);
-       err = xfrm_state_flush_secctx_check(proto, audit_info);
+       err = xfrm_state_flush_secctx_check(net, proto, audit_info);
        if (err)
                goto out;
 
-       for (i = 0; i <= xfrm_state_hmask; i++) {
+       for (i = 0; i <= net->xfrm.state_hmask; i++) {
                struct hlist_node *entry;
                struct xfrm_state *x;
 restart:
-               hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+i, bydst) {
+               hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
                        if (!xfrm_state_kern(x) &&
                            xfrm_id_proto_match(x->id.proto, proto)) {
                                xfrm_state_hold(x);
@@ -644,7 +641,7 @@ restart:
 
 out:
        spin_unlock_bh(&xfrm_state_lock);
-       wake_up(&km_waitq);
+       wake_up(&net->xfrm.km_waitq);
        return err;
 }
 EXPORT_SYMBOL(xfrm_state_flush);
@@ -652,8 +649,8 @@ EXPORT_SYMBOL(xfrm_state_flush);
 void xfrm_sad_getinfo(struct xfrmk_sadinfo *si)
 {
        spin_lock_bh(&xfrm_state_lock);
-       si->sadcnt = xfrm_state_num;
-       si->sadhcnt = xfrm_state_hmask;
+       si->sadcnt = init_net.xfrm.state_num;
+       si->sadhcnt = init_net.xfrm.state_hmask;
        si->sadhmcnt = xfrm_state_hashmax;
        spin_unlock_bh(&xfrm_state_lock);
 }
@@ -673,13 +670,13 @@ xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
        return 0;
 }
 
-static struct xfrm_state *__xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
+static struct xfrm_state *__xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto, unsigned short family)
 {
-       unsigned int h = xfrm_spi_hash(daddr, spi, proto, family);
+       unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
        struct xfrm_state *x;
        struct hlist_node *entry;
 
-       hlist_for_each_entry(x, entry, xfrm_state_byspi+h, byspi) {
+       hlist_for_each_entry(x, entry, net->xfrm.state_byspi+h, byspi) {
                if (x->props.family != family ||
                    x->id.spi       != spi ||
                    x->id.proto     != proto)
@@ -705,13 +702,13 @@ static struct xfrm_state *__xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi,
        return NULL;
 }
 
-static struct xfrm_state *__xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
+static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
 {
-       unsigned int h = xfrm_src_hash(daddr, saddr, family);
+       unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
        struct xfrm_state *x;
        struct hlist_node *entry;
 
-       hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
+       hlist_for_each_entry(x, entry, net->xfrm.state_bysrc+h, bysrc) {
                if (x->props.family != family ||
                    x->id.proto     != proto)
                        continue;
@@ -743,21 +740,23 @@ static struct xfrm_state *__xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm
 static inline struct xfrm_state *
 __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
 {
+       struct net *net = xs_net(x);
+
        if (use_spi)
-               return __xfrm_state_lookup(&x->id.daddr, x->id.spi,
+               return __xfrm_state_lookup(net, &x->id.daddr, x->id.spi,
                                           x->id.proto, family);
        else
-               return __xfrm_state_lookup_byaddr(&x->id.daddr,
+               return __xfrm_state_lookup_byaddr(net, &x->id.daddr,
                                                  &x->props.saddr,
                                                  x->id.proto, family);
 }
 
-static void xfrm_hash_grow_check(int have_hash_collision)
+static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
 {
        if (have_hash_collision &&
-           (xfrm_state_hmask + 1) < xfrm_state_hashmax &&
-           xfrm_state_num > xfrm_state_hmask)
-               schedule_work(&xfrm_hash_work);
+           (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
+           net->xfrm.state_num > net->xfrm.state_hmask)
+               schedule_work(&net->xfrm.state_hash_work);
 }
 
 struct xfrm_state *
@@ -766,6 +765,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                struct xfrm_policy *pol, int *err,
                unsigned short family)
 {
+       struct net *net = xp_net(pol);
        unsigned int h;
        struct hlist_node *entry;
        struct xfrm_state *x, *x0, *to_put;
@@ -776,8 +776,8 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
        to_put = NULL;
 
        spin_lock_bh(&xfrm_state_lock);
-       h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family);
-       hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
+       h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, family);
+       hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
                if (x->props.family == family &&
                    x->props.reqid == tmpl->reqid &&
                    !(x->props.flags & XFRM_STATE_WILDRECV) &&
@@ -821,13 +821,13 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
        x = best;
        if (!x && !error && !acquire_in_progress) {
                if (tmpl->id.spi &&
-                   (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi,
+                   (x0 = __xfrm_state_lookup(net, daddr, tmpl->id.spi,
                                              tmpl->id.proto, family)) != NULL) {
                        to_put = x0;
                        error = -EEXIST;
                        goto out;
                }
-               x = xfrm_state_alloc(&init_net);
+               x = xfrm_state_alloc(net);
                if (x == NULL) {
                        error = -ENOMEM;
                        goto out;
@@ -846,19 +846,19 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
 
                if (km_query(x, tmpl, pol) == 0) {
                        x->km.state = XFRM_STATE_ACQ;
-                       list_add(&x->km.all, &init_net.xfrm.state_all);
-                       hlist_add_head(&x->bydst, init_net.xfrm.state_bydst+h);
-                       h = xfrm_src_hash(daddr, saddr, family);
-                       hlist_add_head(&x->bysrc, init_net.xfrm.state_bysrc+h);
+                       list_add(&x->km.all, &net->xfrm.state_all);
+                       hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
+                       h = xfrm_src_hash(net, daddr, saddr, family);
+                       hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
                        if (x->id.spi) {
-                               h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, family);
-                               hlist_add_head(&x->byspi, xfrm_state_byspi+h);
+                               h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, family);
+                               hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
                        }
                        x->lft.hard_add_expires_seconds = sysctl_xfrm_acq_expires;
                        x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ;
                        add_timer(&x->timer);
-                       xfrm_state_num++;
-                       xfrm_hash_grow_check(x->bydst.next != NULL);
+                       net->xfrm.state_num++;
+                       xfrm_hash_grow_check(net, x->bydst.next != NULL);
                } else {
                        x->km.state = XFRM_STATE_DEAD;
                        to_put = x;
@@ -878,7 +878,8 @@ out:
 }
 
 struct xfrm_state *
-xfrm_stateonly_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
+xfrm_stateonly_find(struct net *net,
+                   xfrm_address_t *daddr, xfrm_address_t *saddr,
                    unsigned short family, u8 mode, u8 proto, u32 reqid)
 {
        unsigned int h;
@@ -886,8 +887,8 @@ xfrm_stateonly_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
        struct hlist_node *entry;
 
        spin_lock(&xfrm_state_lock);
-       h = xfrm_dst_hash(daddr, saddr, reqid, family);
-       hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
+       h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
+       hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
                if (x->props.family == family &&
                    x->props.reqid == reqid &&
                    !(x->props.flags & XFRM_STATE_WILDRECV) &&
@@ -911,48 +912,50 @@ EXPORT_SYMBOL(xfrm_stateonly_find);
 
 static void __xfrm_state_insert(struct xfrm_state *x)
 {
+       struct net *net = xs_net(x);
        unsigned int h;
 
        x->genid = ++xfrm_state_genid;
 
-       list_add(&x->km.all, &init_net.xfrm.state_all);
+       list_add(&x->km.all, &net->xfrm.state_all);
 
-       h = xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
+       h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
                          x->props.reqid, x->props.family);
-       hlist_add_head(&x->bydst, init_net.xfrm.state_bydst+h);
+       hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
 
-       h = xfrm_src_hash(&x->id.daddr, &x->props.saddr, x->props.family);
-       hlist_add_head(&x->bysrc, init_net.xfrm.state_bysrc+h);
+       h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
+       hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
 
        if (x->id.spi) {
-               h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto,
+               h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
                                  x->props.family);
 
-               hlist_add_head(&x->byspi, xfrm_state_byspi+h);
+               hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
        }
 
        mod_timer(&x->timer, jiffies + HZ);
        if (x->replay_maxage)
                mod_timer(&x->rtimer, jiffies + x->replay_maxage);
 
-       wake_up(&km_waitq);
+       wake_up(&net->xfrm.km_waitq);
 
-       xfrm_state_num++;
+       net->xfrm.state_num++;
 
-       xfrm_hash_grow_check(x->bydst.next != NULL);
+       xfrm_hash_grow_check(net, x->bydst.next != NULL);
 }
 
 /* xfrm_state_lock is held */
 static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
 {
+       struct net *net = xs_net(xnew);
        unsigned short family = xnew->props.family;
        u32 reqid = xnew->props.reqid;
        struct xfrm_state *x;
        struct hlist_node *entry;
        unsigned int h;
 
-       h = xfrm_dst_hash(&xnew->id.daddr, &xnew->props.saddr, reqid, family);
-       hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
+       h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
+       hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
                if (x->props.family     == family &&
                    x->props.reqid      == reqid &&
                    !xfrm_addr_cmp(&x->id.daddr, &xnew->id.daddr, family) &&
@@ -971,13 +974,13 @@ void xfrm_state_insert(struct xfrm_state *x)
 EXPORT_SYMBOL(xfrm_state_insert);
 
 /* xfrm_state_lock is held */
-static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
+static struct xfrm_state *__find_acq_core(struct net *net, unsigned short family, u8 mode, u32 reqid, u8 proto, xfrm_address_t *daddr, xfrm_address_t *saddr, int create)
 {
-       unsigned int h = xfrm_dst_hash(daddr, saddr, reqid, family);
+       unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
        struct hlist_node *entry;
        struct xfrm_state *x;
 
-       hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
+       hlist_for_each_entry(x, entry, net->xfrm.state_bydst+h, bydst) {
                if (x->props.reqid  != reqid ||
                    x->props.mode   != mode ||
                    x->props.family != family ||
@@ -1009,7 +1012,7 @@ static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 re
        if (!create)
                return NULL;
 
-       x = xfrm_state_alloc(&init_net);
+       x = xfrm_state_alloc(net);
        if (likely(x)) {
                switch (family) {
                case AF_INET:
@@ -1044,23 +1047,24 @@ static struct xfrm_state *__find_acq_core(unsigned short family, u8 mode, u32 re
                xfrm_state_hold(x);
                x->timer.expires = jiffies + sysctl_xfrm_acq_expires*HZ;
                add_timer(&x->timer);
-               list_add(&x->km.all, &init_net.xfrm.state_all);
-               hlist_add_head(&x->bydst, init_net.xfrm.state_bydst+h);
-               h = xfrm_src_hash(daddr, saddr, family);
-               hlist_add_head(&x->bysrc, init_net.xfrm.state_bysrc+h);
+               list_add(&x->km.all, &net->xfrm.state_all);
+               hlist_add_head(&x->bydst, net->xfrm.state_bydst+h);
+               h = xfrm_src_hash(net, daddr, saddr, family);
+               hlist_add_head(&x->bysrc, net->xfrm.state_bysrc+h);
 
-               xfrm_state_num++;
+               net->xfrm.state_num++;
 
-               xfrm_hash_grow_check(x->bydst.next != NULL);
+               xfrm_hash_grow_check(net, x->bydst.next != NULL);
        }
 
        return x;
 }
 
-static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq);
+static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq);
 
 int xfrm_state_add(struct xfrm_state *x)
 {
+       struct net *net = xs_net(x);
        struct xfrm_state *x1, *to_put;
        int family;
        int err;
@@ -1081,7 +1085,7 @@ int xfrm_state_add(struct xfrm_state *x)
        }
 
        if (use_spi && x->km.seq) {
-               x1 = __xfrm_find_acq_byseq(x->km.seq);
+               x1 = __xfrm_find_acq_byseq(net, x->km.seq);
                if (x1 && ((x1->id.proto != x->id.proto) ||
                    xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
                        to_put = x1;
@@ -1090,7 +1094,7 @@ int xfrm_state_add(struct xfrm_state *x)
        }
 
        if (use_spi && !x1)
-               x1 = __find_acq_core(family, x->props.mode, x->props.reqid,
+               x1 = __find_acq_core(net, family, x->props.mode, x->props.reqid,
                                     x->id.proto,
                                     &x->id.daddr, &x->props.saddr, 0);
 
@@ -1116,8 +1120,9 @@ EXPORT_SYMBOL(xfrm_state_add);
 #ifdef CONFIG_XFRM_MIGRATE
 static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
 {
+       struct net *net = xs_net(orig);
        int err = -ENOMEM;
-       struct xfrm_state *x = xfrm_state_alloc(&init_net);
+       struct xfrm_state *x = xfrm_state_alloc(net);
        if (!x)
                goto error;
 
@@ -1198,7 +1203,7 @@ struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
        struct hlist_node *entry;
 
        if (m->reqid) {
-               h = xfrm_dst_hash(&m->old_daddr, &m->old_saddr,
+               h = xfrm_dst_hash(&init_net, &m->old_daddr, &m->old_saddr,
                                  m->reqid, m->old_family);
                hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+h, bydst) {
                        if (x->props.mode != m->mode ||
@@ -1215,7 +1220,7 @@ struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
                        return x;
                }
        } else {
-               h = xfrm_src_hash(&m->old_daddr, &m->old_saddr,
+               h = xfrm_src_hash(&init_net, &m->old_daddr, &m->old_saddr,
                                  m->old_family);
                hlist_for_each_entry(x, entry, init_net.xfrm.state_bysrc+h, bysrc) {
                        if (x->props.mode != m->mode ||
@@ -1361,40 +1366,41 @@ int xfrm_state_check_expire(struct xfrm_state *x)
 EXPORT_SYMBOL(xfrm_state_check_expire);
 
 struct xfrm_state *
-xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto,
+xfrm_state_lookup(struct net *net, xfrm_address_t *daddr, __be32 spi, u8 proto,
                  unsigned short family)
 {
        struct xfrm_state *x;
 
        spin_lock_bh(&xfrm_state_lock);
-       x = __xfrm_state_lookup(daddr, spi, proto, family);
+       x = __xfrm_state_lookup(net, daddr, spi, proto, family);
        spin_unlock_bh(&xfrm_state_lock);
        return x;
 }
 EXPORT_SYMBOL(xfrm_state_lookup);
 
 struct xfrm_state *
-xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr,
+xfrm_state_lookup_byaddr(struct net *net,
+                        xfrm_address_t *daddr, xfrm_address_t *saddr,
                         u8 proto, unsigned short family)
 {
        struct xfrm_state *x;
 
        spin_lock_bh(&xfrm_state_lock);
-       x = __xfrm_state_lookup_byaddr(daddr, saddr, proto, family);
+       x = __xfrm_state_lookup_byaddr(net, daddr, saddr, proto, family);
        spin_unlock_bh(&xfrm_state_lock);
        return x;
 }
 EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
 
 struct xfrm_state *
-xfrm_find_acq(u8 mode, u32 reqid, u8 proto,
+xfrm_find_acq(struct net *net, u8 mode, u32 reqid, u8 proto,
              xfrm_address_t *daddr, xfrm_address_t *saddr,
              int create, unsigned short family)
 {
        struct xfrm_state *x;
 
        spin_lock_bh(&xfrm_state_lock);
-       x = __find_acq_core(family, mode, reqid, proto, daddr, saddr, create);
+       x = __find_acq_core(net, family, mode, reqid, proto, daddr, saddr, create);
        spin_unlock_bh(&xfrm_state_lock);
 
        return x;
@@ -1441,15 +1447,15 @@ EXPORT_SYMBOL(xfrm_state_sort);
 
 /* Silly enough, but I'm lazy to build resolution list */
 
-static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq)
+static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 seq)
 {
        int i;
 
-       for (i = 0; i <= xfrm_state_hmask; i++) {
+       for (i = 0; i <= net->xfrm.state_hmask; i++) {
                struct hlist_node *entry;
                struct xfrm_state *x;
 
-               hlist_for_each_entry(x, entry, init_net.xfrm.state_bydst+i, bydst) {
+               hlist_for_each_entry(x, entry, net->xfrm.state_bydst+i, bydst) {
                        if (x->km.seq == seq &&
                            x->km.state == XFRM_STATE_ACQ) {
                                xfrm_state_hold(x);
@@ -1460,12 +1466,12 @@ static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq)
        return NULL;
 }
 
-struct xfrm_state *xfrm_find_acq_byseq(u32 seq)
+struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 seq)
 {
        struct xfrm_state *x;
 
        spin_lock_bh(&xfrm_state_lock);
-       x = __xfrm_find_acq_byseq(seq);
+       x = __xfrm_find_acq_byseq(net, seq);
        spin_unlock_bh(&xfrm_state_lock);
        return x;
 }
@@ -1486,6 +1492,7 @@ EXPORT_SYMBOL(xfrm_get_acqseq);
 
 int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
 {
+       struct net *net = xs_net(x);
        unsigned int h;
        struct xfrm_state *x0;
        int err = -ENOENT;
@@ -1503,7 +1510,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
        err = -ENOENT;
 
        if (minspi == maxspi) {
-               x0 = xfrm_state_lookup(&x->id.daddr, minspi, x->id.proto, x->props.family);
+               x0 = xfrm_state_lookup(net, &x->id.daddr, minspi, x->id.proto, x->props.family);
                if (x0) {
                        xfrm_state_put(x0);
                        goto unlock;
@@ -1513,7 +1520,7 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
                u32 spi = 0;
                for (h=0; h<high-low+1; h++) {
                        spi = low + net_random()%(high-low+1);
-                       x0 = xfrm_state_lookup(&x->id.daddr, htonl(spi), x->id.proto, x->props.family);
+                       x0 = xfrm_state_lookup(net, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
                        if (x0 == NULL) {
                                x->id.spi = htonl(spi);
                                break;
@@ -1523,8 +1530,8 @@ int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
        }
        if (x->id.spi) {
                spin_lock_bh(&xfrm_state_lock);
-               h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, x->props.family);
-               hlist_add_head(&x->byspi, xfrm_state_byspi+h);
+               h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
+               hlist_add_head(&x->byspi, net->xfrm.state_byspi+h);
                spin_unlock_bh(&xfrm_state_lock);
 
                err = 0;
@@ -1537,7 +1544,7 @@ unlock:
 }
 EXPORT_SYMBOL(xfrm_alloc_spi);
 
-int xfrm_state_walk(struct xfrm_state_walk *walk,
+int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
                    int (*func)(struct xfrm_state *, int, void*),
                    void *data)
 {
@@ -1550,10 +1557,10 @@ int xfrm_state_walk(struct xfrm_state_walk *walk,
 
        spin_lock_bh(&xfrm_state_lock);
        if (list_empty(&walk->all))
-               x = list_first_entry(&init_net.xfrm.state_all, struct xfrm_state_walk, all);
+               x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
        else
                x = list_entry(&walk->all, struct xfrm_state_walk, all);
-       list_for_each_entry_from(x, &init_net.xfrm.state_all, all) {
+       list_for_each_entry_from(x, &net->xfrm.state_all, all) {
                if (x->state == XFRM_STATE_DEAD)
                        continue;
                state = container_of(x, struct xfrm_state, km);
@@ -1741,6 +1748,7 @@ EXPORT_SYMBOL(km_state_notify);
 
 void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
 {
+       struct net *net = xs_net(x);
        struct km_event c;
 
        c.data.hard = hard;
@@ -1749,7 +1757,7 @@ void km_state_expired(struct xfrm_state *x, int hard, u32 pid)
        km_state_notify(x, &c);
 
        if (hard)
-               wake_up(&km_waitq);
+               wake_up(&net->xfrm.km_waitq);
 }
 
 EXPORT_SYMBOL(km_state_expired);
@@ -1792,6 +1800,7 @@ EXPORT_SYMBOL(km_new_mapping);
 
 void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
 {
+       struct net *net = xp_net(pol);
        struct km_event c;
 
        c.data.hard = hard;
@@ -1800,7 +1809,7 @@ void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
        km_policy_notify(pol, dir, &c);
 
        if (hard)
-               wake_up(&km_waitq);
+               wake_up(&net->xfrm.km_waitq);
 }
 EXPORT_SYMBOL(km_policy_expired);
 
@@ -2086,12 +2095,20 @@ int __net_init xfrm_state_init(struct net *net)
        net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
        if (!net->xfrm.state_bysrc)
                goto out_bysrc;
-       xfrm_state_byspi = xfrm_hash_alloc(sz);
-       xfrm_state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
-
-       INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task);
+       net->xfrm.state_byspi = xfrm_hash_alloc(sz);
+       if (!net->xfrm.state_byspi)
+               goto out_byspi;
+       net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
+
+       net->xfrm.state_num = 0;
+       INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
+       INIT_HLIST_HEAD(&net->xfrm.state_gc_list);
+       INIT_WORK(&net->xfrm.state_gc_work, xfrm_state_gc_task);
+       init_waitqueue_head(&net->xfrm.km_waitq);
        return 0;
 
+out_byspi:
+       xfrm_hash_free(net->xfrm.state_bysrc, sz);
 out_bysrc:
        xfrm_hash_free(net->xfrm.state_bydst, sz);
 out_bydst:
@@ -2104,7 +2121,9 @@ void xfrm_state_fini(struct net *net)
 
        WARN_ON(!list_empty(&net->xfrm.state_all));
 
-       sz = (xfrm_state_hmask + 1) * sizeof(struct hlist_head);
+       sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
+       WARN_ON(!hlist_empty(net->xfrm.state_byspi));
+       xfrm_hash_free(net->xfrm.state_byspi, sz);
        WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
        xfrm_hash_free(net->xfrm.state_bysrc, sz);
        WARN_ON(!hlist_empty(net->xfrm.state_bydst));