ipsec: Restore larval states and socket policies in dump
[safe/jmp/linux-2.6] / net / xfrm / xfrm_state.c
index dfacb9c..0a8f09c 100644 (file)
@@ -19,9 +19,8 @@
 #include <linux/ipsec.h>
 #include <linux/module.h>
 #include <linux/cache.h>
-#include <asm/uaccess.h>
 #include <linux/audit.h>
-#include <linux/cache.h>
+#include <asm/uaccess.h>
 
 #include "xfrm_hash.h"
 
@@ -51,6 +50,7 @@ static DEFINE_SPINLOCK(xfrm_state_lock);
  * Main use is finding SA after policy selected tunnel or transport mode.
  * Also, it can be used by ah/esp icmp error handler to find offending SA.
  */
+static LIST_HEAD(xfrm_state_all);
 static struct hlist_head *xfrm_state_bydst __read_mostly;
 static struct hlist_head *xfrm_state_bysrc __read_mostly;
 static struct hlist_head *xfrm_state_byspi __read_mostly;
@@ -59,6 +59,16 @@ 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);
+static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo);
+
+#ifdef CONFIG_AUDITSYSCALL
+static void xfrm_audit_state_replay(struct xfrm_state *x,
+                                   struct sk_buff *skb, __be32 net_seq);
+#else
+#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,
                                         xfrm_address_t *saddr,
                                         u32 reqid,
@@ -189,6 +199,185 @@ int __xfrm_state_delete(struct xfrm_state *x);
 int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
 void km_state_expired(struct xfrm_state *x, int hard, u32 pid);
 
+static struct xfrm_state_afinfo *xfrm_state_lock_afinfo(unsigned int family)
+{
+       struct xfrm_state_afinfo *afinfo;
+       if (unlikely(family >= NPROTO))
+               return NULL;
+       write_lock_bh(&xfrm_state_afinfo_lock);
+       afinfo = xfrm_state_afinfo[family];
+       if (unlikely(!afinfo))
+               write_unlock_bh(&xfrm_state_afinfo_lock);
+       return afinfo;
+}
+
+static void xfrm_state_unlock_afinfo(struct xfrm_state_afinfo *afinfo)
+       __releases(xfrm_state_afinfo_lock)
+{
+       write_unlock_bh(&xfrm_state_afinfo_lock);
+}
+
+int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
+{
+       struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
+       const struct xfrm_type **typemap;
+       int err = 0;
+
+       if (unlikely(afinfo == NULL))
+               return -EAFNOSUPPORT;
+       typemap = afinfo->type_map;
+
+       if (likely(typemap[type->proto] == NULL))
+               typemap[type->proto] = type;
+       else
+               err = -EEXIST;
+       xfrm_state_unlock_afinfo(afinfo);
+       return err;
+}
+EXPORT_SYMBOL(xfrm_register_type);
+
+int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
+{
+       struct xfrm_state_afinfo *afinfo = xfrm_state_lock_afinfo(family);
+       const struct xfrm_type **typemap;
+       int err = 0;
+
+       if (unlikely(afinfo == NULL))
+               return -EAFNOSUPPORT;
+       typemap = afinfo->type_map;
+
+       if (unlikely(typemap[type->proto] != type))
+               err = -ENOENT;
+       else
+               typemap[type->proto] = NULL;
+       xfrm_state_unlock_afinfo(afinfo);
+       return err;
+}
+EXPORT_SYMBOL(xfrm_unregister_type);
+
+static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
+{
+       struct xfrm_state_afinfo *afinfo;
+       const struct xfrm_type **typemap;
+       const struct xfrm_type *type;
+       int modload_attempted = 0;
+
+retry:
+       afinfo = xfrm_state_get_afinfo(family);
+       if (unlikely(afinfo == NULL))
+               return NULL;
+       typemap = afinfo->type_map;
+
+       type = typemap[proto];
+       if (unlikely(type && !try_module_get(type->owner)))
+               type = NULL;
+       if (!type && !modload_attempted) {
+               xfrm_state_put_afinfo(afinfo);
+               request_module("xfrm-type-%d-%d", family, proto);
+               modload_attempted = 1;
+               goto retry;
+       }
+
+       xfrm_state_put_afinfo(afinfo);
+       return type;
+}
+
+static void xfrm_put_type(const struct xfrm_type *type)
+{
+       module_put(type->owner);
+}
+
+int xfrm_register_mode(struct xfrm_mode *mode, int family)
+{
+       struct xfrm_state_afinfo *afinfo;
+       struct xfrm_mode **modemap;
+       int err;
+
+       if (unlikely(mode->encap >= XFRM_MODE_MAX))
+               return -EINVAL;
+
+       afinfo = xfrm_state_lock_afinfo(family);
+       if (unlikely(afinfo == NULL))
+               return -EAFNOSUPPORT;
+
+       err = -EEXIST;
+       modemap = afinfo->mode_map;
+       if (modemap[mode->encap])
+               goto out;
+
+       err = -ENOENT;
+       if (!try_module_get(afinfo->owner))
+               goto out;
+
+       mode->afinfo = afinfo;
+       modemap[mode->encap] = mode;
+       err = 0;
+
+out:
+       xfrm_state_unlock_afinfo(afinfo);
+       return err;
+}
+EXPORT_SYMBOL(xfrm_register_mode);
+
+int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
+{
+       struct xfrm_state_afinfo *afinfo;
+       struct xfrm_mode **modemap;
+       int err;
+
+       if (unlikely(mode->encap >= XFRM_MODE_MAX))
+               return -EINVAL;
+
+       afinfo = xfrm_state_lock_afinfo(family);
+       if (unlikely(afinfo == NULL))
+               return -EAFNOSUPPORT;
+
+       err = -ENOENT;
+       modemap = afinfo->mode_map;
+       if (likely(modemap[mode->encap] == mode)) {
+               modemap[mode->encap] = NULL;
+               module_put(mode->afinfo->owner);
+               err = 0;
+       }
+
+       xfrm_state_unlock_afinfo(afinfo);
+       return err;
+}
+EXPORT_SYMBOL(xfrm_unregister_mode);
+
+static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
+{
+       struct xfrm_state_afinfo *afinfo;
+       struct xfrm_mode *mode;
+       int modload_attempted = 0;
+
+       if (unlikely(encap >= XFRM_MODE_MAX))
+               return NULL;
+
+retry:
+       afinfo = xfrm_state_get_afinfo(family);
+       if (unlikely(afinfo == NULL))
+               return NULL;
+
+       mode = afinfo->mode_map[encap];
+       if (unlikely(mode && !try_module_get(mode->owner)))
+               mode = NULL;
+       if (!mode && !modload_attempted) {
+               xfrm_state_put_afinfo(afinfo);
+               request_module("xfrm-mode-%d-%d", family, encap);
+               modload_attempted = 1;
+               goto retry;
+       }
+
+       xfrm_state_put_afinfo(afinfo);
+       return mode;
+}
+
+static void xfrm_put_mode(struct xfrm_mode *mode)
+{
+       module_put(mode->owner);
+}
+
 static void xfrm_state_gc_destroy(struct xfrm_state *x)
 {
        del_timer_sync(&x->timer);
@@ -198,8 +387,12 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)
        kfree(x->calg);
        kfree(x->encap);
        kfree(x->coaddr);
-       if (x->mode)
-               xfrm_put_mode(x->mode);
+       if (x->inner_mode)
+               xfrm_put_mode(x->inner_mode);
+       if (x->inner_mode_iaf)
+               xfrm_put_mode(x->inner_mode_iaf);
+       if (x->outer_mode)
+               xfrm_put_mode(x->outer_mode);
        if (x->type) {
                x->type->destructor(x);
                xfrm_put_type(x->type);
@@ -302,8 +495,9 @@ expired:
        if (!err && x->id.spi)
                km_state_expired(x, 1, 0);
 
-       xfrm_audit_log(audit_get_loginuid(current->audit_context), 0,
-                      AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
+       xfrm_audit_state_delete(x, err ? 0 : 1,
+                               audit_get_loginuid(current),
+                               audit_get_sessionid(current), 0);
 
 out:
        spin_unlock(&x->lock);
@@ -320,15 +514,13 @@ struct xfrm_state *xfrm_state_alloc(void)
        if (x) {
                atomic_set(&x->refcnt, 1);
                atomic_set(&x->tunnel_users, 0);
+               INIT_LIST_HEAD(&x->all);
                INIT_HLIST_NODE(&x->bydst);
                INIT_HLIST_NODE(&x->bysrc);
                INIT_HLIST_NODE(&x->byspi);
-               init_timer(&x->timer);
-               x->timer.function = xfrm_timer_handler;
-               x->timer.data     = (unsigned long)x;
-               init_timer(&x->rtimer);
-               x->rtimer.function = xfrm_replay_timer_handler;
-               x->rtimer.data     = (unsigned long)x;
+               setup_timer(&x->timer, xfrm_timer_handler, (unsigned long)x);
+               setup_timer(&x->rtimer, xfrm_replay_timer_handler,
+                               (unsigned long)x);
                x->curlft.add_time = get_seconds();
                x->lft.soft_byte_limit = XFRM_INF;
                x->lft.soft_packet_limit = XFRM_INF;
@@ -336,6 +528,8 @@ struct xfrm_state *xfrm_state_alloc(void)
                x->lft.hard_packet_limit = XFRM_INF;
                x->replay_maxage = 0;
                x->replay_maxdiff = 0;
+               x->inner_mode = NULL;
+               x->inner_mode_iaf = NULL;
                spin_lock_init(&x->lock);
        }
        return x;
@@ -344,7 +538,11 @@ EXPORT_SYMBOL(xfrm_state_alloc);
 
 void __xfrm_state_destroy(struct xfrm_state *x)
 {
-       BUG_TRAP(x->km.state == XFRM_STATE_DEAD);
+       WARN_ON(x->km.state != XFRM_STATE_DEAD);
+
+       spin_lock_bh(&xfrm_state_lock);
+       list_del(&x->all);
+       spin_unlock_bh(&xfrm_state_lock);
 
        spin_lock_bh(&xfrm_state_gc_lock);
        hlist_add_head(&x->bydst, &xfrm_state_gc_list);
@@ -371,7 +569,7 @@ int __xfrm_state_delete(struct xfrm_state *x)
                 * The xfrm_state_alloc call gives a reference, and that
                 * is what we are dropping here.
                 */
-               __xfrm_state_put(x);
+               xfrm_state_put(x);
                err = 0;
        }
 
@@ -404,11 +602,10 @@ xfrm_state_flush_secctx_check(u8 proto, struct xfrm_audit *audit_info)
                hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
                        if (xfrm_id_proto_match(x->id.proto, proto) &&
                           (err = security_xfrm_state_delete(x)) != 0) {
-                               xfrm_audit_log(audit_info->loginuid,
-                                              audit_info->secid,
-                                              AUDIT_MAC_IPSEC_DELSA,
-                                               0, NULL, x);
-
+                               xfrm_audit_state_delete(x, 0,
+                                                       audit_info->loginuid,
+                                                       audit_info->sessionid,
+                                                       audit_info->secid);
                                return err;
                        }
                }
@@ -444,10 +641,10 @@ restart:
                                spin_unlock_bh(&xfrm_state_lock);
 
                                err = xfrm_state_delete(x);
-                               xfrm_audit_log(audit_info->loginuid,
-                                              audit_info->secid,
-                                              AUDIT_MAC_IPSEC_DELSA,
-                                              err ? 0 : 1, NULL, x);
+                               xfrm_audit_state_delete(x, err ? 0 : 1,
+                                                       audit_info->loginuid,
+                                                       audit_info->sessionid,
+                                                       audit_info->secid);
                                xfrm_state_put(x);
 
                                spin_lock_bh(&xfrm_state_lock);
@@ -581,14 +778,17 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                struct xfrm_policy *pol, int *err,
                unsigned short family)
 {
-       unsigned int h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family);
+       unsigned int h;
        struct hlist_node *entry;
-       struct xfrm_state *x, *x0;
+       struct xfrm_state *x, *x0, *to_put;
        int acquire_in_progress = 0;
        int error = 0;
        struct xfrm_state *best = NULL;
 
+       to_put = NULL;
+
        spin_lock_bh(&xfrm_state_lock);
+       h = xfrm_dst_hash(daddr, saddr, tmpl->reqid, family);
        hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
                if (x->props.family == family &&
                    x->props.reqid == tmpl->reqid &&
@@ -611,7 +811,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                              selector.
                         */
                        if (x->km.state == XFRM_STATE_VALID) {
-                               if (!xfrm_selector_match(&x->sel, fl, family) ||
+                               if ((x->sel.family && !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
                                    !security_xfrm_state_pol_flow_match(x, pol, fl))
                                        continue;
                                if (!best ||
@@ -623,7 +823,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                                acquire_in_progress = 1;
                        } else if (x->km.state == XFRM_STATE_ERROR ||
                                   x->km.state == XFRM_STATE_EXPIRED) {
-                               if (xfrm_selector_match(&x->sel, fl, family) &&
+                               if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
                                    security_xfrm_state_pol_flow_match(x, pol, fl))
                                        error = -ESRCH;
                        }
@@ -635,7 +835,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                if (tmpl->id.spi &&
                    (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi,
                                              tmpl->id.proto, family)) != NULL) {
-                       xfrm_state_put(x0);
+                       to_put = x0;
                        error = -EEXIST;
                        goto out;
                }
@@ -651,13 +851,14 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
                if (error) {
                        x->km.state = XFRM_STATE_DEAD;
-                       xfrm_state_put(x);
+                       to_put = x;
                        x = NULL;
                        goto out;
                }
 
                if (km_query(x, tmpl, pol) == 0) {
                        x->km.state = XFRM_STATE_ACQ;
+                       list_add_tail(&x->all, &xfrm_state_all);
                        hlist_add_head(&x->bydst, xfrm_state_bydst+h);
                        h = xfrm_src_hash(daddr, saddr, family);
                        hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
@@ -672,7 +873,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                        xfrm_hash_grow_check(x->bydst.next != NULL);
                } else {
                        x->km.state = XFRM_STATE_DEAD;
-                       xfrm_state_put(x);
+                       to_put = x;
                        x = NULL;
                        error = -ESRCH;
                }
@@ -683,15 +884,51 @@ out:
        else
                *err = acquire_in_progress ? -EAGAIN : error;
        spin_unlock_bh(&xfrm_state_lock);
+       if (to_put)
+               xfrm_state_put(to_put);
        return x;
 }
 
+struct xfrm_state *
+xfrm_stateonly_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
+                   unsigned short family, u8 mode, u8 proto, u32 reqid)
+{
+       unsigned int h;
+       struct xfrm_state *rx = NULL, *x = NULL;
+       struct hlist_node *entry;
+
+       spin_lock(&xfrm_state_lock);
+       h = xfrm_dst_hash(daddr, saddr, reqid, family);
+       hlist_for_each_entry(x, entry, xfrm_state_bydst+h, bydst) {
+               if (x->props.family == family &&
+                   x->props.reqid == reqid &&
+                   !(x->props.flags & XFRM_STATE_WILDRECV) &&
+                   xfrm_state_addr_check(x, daddr, saddr, family) &&
+                   mode == x->props.mode &&
+                   proto == x->id.proto &&
+                   x->km.state == XFRM_STATE_VALID) {
+                       rx = x;
+                       break;
+               }
+       }
+
+       if (rx)
+               xfrm_state_hold(rx);
+       spin_unlock(&xfrm_state_lock);
+
+
+       return rx;
+}
+EXPORT_SYMBOL(xfrm_stateonly_find);
+
 static void __xfrm_state_insert(struct xfrm_state *x)
 {
        unsigned int h;
 
        x->genid = ++xfrm_state_genid;
 
+       list_add_tail(&x->all, &xfrm_state_all);
+
        h = xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
                          x->props.reqid, x->props.family);
        hlist_add_head(&x->bydst, xfrm_state_bydst+h);
@@ -819,10 +1056,10 @@ 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_tail(&x->all, &xfrm_state_all);
                hlist_add_head(&x->bydst, xfrm_state_bydst+h);
                h = xfrm_src_hash(daddr, saddr, family);
                hlist_add_head(&x->bysrc, xfrm_state_bysrc+h);
-               wake_up(&km_waitq);
 
                xfrm_state_num++;
 
@@ -836,18 +1073,20 @@ static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq);
 
 int xfrm_state_add(struct xfrm_state *x)
 {
-       struct xfrm_state *x1;
+       struct xfrm_state *x1, *to_put;
        int family;
        int err;
        int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
 
        family = x->props.family;
 
+       to_put = NULL;
+
        spin_lock_bh(&xfrm_state_lock);
 
        x1 = __xfrm_state_locate(x, use_spi, family);
        if (x1) {
-               xfrm_state_put(x1);
+               to_put = x1;
                x1 = NULL;
                err = -EEXIST;
                goto out;
@@ -857,7 +1096,7 @@ int xfrm_state_add(struct xfrm_state *x)
                x1 = __xfrm_find_acq_byseq(x->km.seq);
                if (x1 && ((x1->id.proto != x->id.proto) ||
                    xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family))) {
-                       xfrm_state_put(x1);
+                       to_put = x1;
                        x1 = NULL;
                }
        }
@@ -879,12 +1118,15 @@ out:
                xfrm_state_put(x1);
        }
 
+       if (to_put)
+               xfrm_state_put(to_put);
+
        return err;
 }
 EXPORT_SYMBOL(xfrm_state_add);
 
 #ifdef CONFIG_XFRM_MIGRATE
-struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
+static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
 {
        int err = -ENOMEM;
        struct xfrm_state *x = xfrm_state_alloc();
@@ -959,7 +1201,6 @@ struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig, int *errp)
        kfree(x);
        return NULL;
 }
-EXPORT_SYMBOL(xfrm_state_clone);
 
 /* xfrm_state_lock is held */
 struct xfrm_state * xfrm_migrate_state_find(struct xfrm_migrate *m)
@@ -1039,10 +1280,12 @@ EXPORT_SYMBOL(xfrm_state_migrate);
 
 int xfrm_state_update(struct xfrm_state *x)
 {
-       struct xfrm_state *x1;
+       struct xfrm_state *x1, *to_put;
        int err;
        int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
 
+       to_put = NULL;
+
        spin_lock_bh(&xfrm_state_lock);
        x1 = __xfrm_state_locate(x, use_spi, x->props.family);
 
@@ -1051,7 +1294,7 @@ int xfrm_state_update(struct xfrm_state *x)
                goto out;
 
        if (xfrm_state_kern(x1)) {
-               xfrm_state_put(x1);
+               to_put = x1;
                err = -EEXIST;
                goto out;
        }
@@ -1065,6 +1308,9 @@ int xfrm_state_update(struct xfrm_state *x)
 out:
        spin_unlock_bh(&xfrm_state_lock);
 
+       if (to_put)
+               xfrm_state_put(to_put);
+
        if (err)
                return err;
 
@@ -1126,29 +1372,6 @@ int xfrm_state_check_expire(struct xfrm_state *x)
 }
 EXPORT_SYMBOL(xfrm_state_check_expire);
 
-static int xfrm_state_check_space(struct xfrm_state *x, struct sk_buff *skb)
-{
-       int nhead = x->props.header_len + LL_RESERVED_SPACE(skb->dst->dev)
-               - skb_headroom(skb);
-
-       if (nhead > 0)
-               return pskb_expand_head(skb, nhead, 0, GFP_ATOMIC);
-
-       /* Check tail too... */
-       return 0;
-}
-
-int xfrm_state_check(struct xfrm_state *x, struct sk_buff *skb)
-{
-       int err = xfrm_state_check_expire(x);
-       if (err < 0)
-               goto err;
-       err = xfrm_state_check_space(x, skb);
-err:
-       return err;
-}
-EXPORT_SYMBOL(xfrm_state_check);
-
 struct xfrm_state *
 xfrm_state_lookup(xfrm_address_t *daddr, __be32 spi, u8 proto,
                  unsigned short family)
@@ -1273,26 +1496,33 @@ u32 xfrm_get_acqseq(void)
 }
 EXPORT_SYMBOL(xfrm_get_acqseq);
 
-void
-xfrm_alloc_spi(struct xfrm_state *x, __be32 minspi, __be32 maxspi)
+int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
 {
        unsigned int h;
        struct xfrm_state *x0;
+       int err = -ENOENT;
+       __be32 minspi = htonl(low);
+       __be32 maxspi = htonl(high);
+
+       spin_lock_bh(&x->lock);
+       if (x->km.state == XFRM_STATE_DEAD)
+               goto unlock;
 
+       err = 0;
        if (x->id.spi)
-               return;
+               goto unlock;
+
+       err = -ENOENT;
 
        if (minspi == maxspi) {
                x0 = xfrm_state_lookup(&x->id.daddr, minspi, x->id.proto, x->props.family);
                if (x0) {
                        xfrm_state_put(x0);
-                       return;
+                       goto unlock;
                }
                x->id.spi = minspi;
        } else {
                u32 spi = 0;
-               u32 low = ntohl(minspi);
-               u32 high = ntohl(maxspi);
                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);
@@ -1308,41 +1538,58 @@ xfrm_alloc_spi(struct xfrm_state *x, __be32 minspi, __be32 maxspi)
                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);
                spin_unlock_bh(&xfrm_state_lock);
-               wake_up(&km_waitq);
+
+               err = 0;
        }
+
+unlock:
+       spin_unlock_bh(&x->lock);
+
+       return err;
 }
 EXPORT_SYMBOL(xfrm_alloc_spi);
 
-int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*),
+int xfrm_state_walk(struct xfrm_state_walk *walk,
+                   int (*func)(struct xfrm_state *, int, void*),
                    void *data)
 {
-       int i;
-       struct xfrm_state *x, *last = NULL;
-       struct hlist_node *entry;
-       int count = 0;
+       struct xfrm_state *old, *x, *last = NULL;
        int err = 0;
 
+       if (walk->state == NULL && walk->count != 0)
+               return 0;
+
+       old = x = walk->state;
+       walk->state = NULL;
        spin_lock_bh(&xfrm_state_lock);
-       for (i = 0; i <= xfrm_state_hmask; i++) {
-               hlist_for_each_entry(x, entry, xfrm_state_bydst+i, bydst) {
-                       if (!xfrm_id_proto_match(x->id.proto, proto))
-                               continue;
-                       if (last) {
-                               err = func(last, count, data);
-                               if (err)
-                                       goto out;
+       if (x == NULL)
+               x = list_first_entry(&xfrm_state_all, struct xfrm_state, all);
+       list_for_each_entry_from(x, &xfrm_state_all, all) {
+               if (x->km.state == XFRM_STATE_DEAD)
+                       continue;
+               if (!xfrm_id_proto_match(x->id.proto, walk->proto))
+                       continue;
+               if (last) {
+                       err = func(last, walk->count, data);
+                       if (err) {
+                               xfrm_state_hold(last);
+                               walk->state = last;
+                               goto out;
                        }
-                       last = x;
-                       count++;
                }
+               last = x;
+               walk->count++;
        }
-       if (count == 0) {
+       if (walk->count == 0) {
                err = -ENOENT;
                goto out;
        }
-       err = func(last, 0, data);
+       if (last)
+               err = func(last, 0, data);
 out:
        spin_unlock_bh(&xfrm_state_lock);
+       if (old != NULL)
+               xfrm_state_put(old);
        return err;
 }
 EXPORT_SYMBOL(xfrm_state_walk);
@@ -1394,7 +1641,6 @@ void xfrm_replay_notify(struct xfrm_state *x, int event)
            !mod_timer(&x->rtimer, jiffies + x->replay_maxage))
                x->xflags &= ~XFRM_TIME_DEFER;
 }
-EXPORT_SYMBOL(xfrm_replay_notify);
 
 static void xfrm_replay_timer_handler(unsigned long data)
 {
@@ -1412,13 +1658,14 @@ static void xfrm_replay_timer_handler(unsigned long data)
        spin_unlock(&x->lock);
 }
 
-int xfrm_replay_check(struct xfrm_state *x, __be32 net_seq)
+int xfrm_replay_check(struct xfrm_state *x,
+                     struct sk_buff *skb, __be32 net_seq)
 {
        u32 diff;
        u32 seq = ntohl(net_seq);
 
        if (unlikely(seq == 0))
-               return -EINVAL;
+               goto err;
 
        if (likely(seq > x->replay.seq))
                return 0;
@@ -1427,16 +1674,19 @@ int xfrm_replay_check(struct xfrm_state *x, __be32 net_seq)
        if (diff >= min_t(unsigned int, x->props.replay_window,
                          sizeof(x->replay.bitmap) * 8)) {
                x->stats.replay_window++;
-               return -EINVAL;
+               goto err;
        }
 
        if (x->replay.bitmap & (1U << diff)) {
                x->stats.replay++;
-               return -EINVAL;
+               goto err;
        }
        return 0;
+
+err:
+       xfrm_audit_state_replay(x, skb, net_seq);
+       return -EINVAL;
 }
-EXPORT_SYMBOL(xfrm_replay_check);
 
 void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
 {
@@ -1458,9 +1708,8 @@ void xfrm_replay_advance(struct xfrm_state *x, __be32 net_seq)
        if (xfrm_aevent_is_on())
                xfrm_replay_notify(x, XFRM_REPLAY_UPDATE);
 }
-EXPORT_SYMBOL(xfrm_replay_advance);
 
-static struct list_head xfrm_km_list = LIST_HEAD_INIT(xfrm_km_list);
+static LIST_HEAD(xfrm_km_list);
 static DEFINE_RWLOCK(xfrm_km_lock);
 
 void km_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
@@ -1552,6 +1801,7 @@ void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
 }
 EXPORT_SYMBOL(km_policy_expired);
 
+#ifdef CONFIG_XFRM_MIGRATE
 int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
               struct xfrm_migrate *m, int num_migrate)
 {
@@ -1571,6 +1821,7 @@ int km_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
        return err;
 }
 EXPORT_SYMBOL(km_migrate);
+#endif
 
 int km_report(u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
 {
@@ -1685,7 +1936,7 @@ int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
 }
 EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
 
-struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family)
+static struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
 {
        struct xfrm_state_afinfo *afinfo;
        if (unlikely(family >= NPROTO))
@@ -1697,14 +1948,12 @@ struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned short family)
        return afinfo;
 }
 
-void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
+static void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
+       __releases(xfrm_state_afinfo_lock)
 {
        read_unlock(&xfrm_state_afinfo_lock);
 }
 
-EXPORT_SYMBOL(xfrm_state_get_afinfo);
-EXPORT_SYMBOL(xfrm_state_put_afinfo);
-
 /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
 void xfrm_state_delete_tunnel(struct xfrm_state *x)
 {
@@ -1737,6 +1986,7 @@ int xfrm_state_mtu(struct xfrm_state *x, int mtu)
 int xfrm_init_state(struct xfrm_state *x)
 {
        struct xfrm_state_afinfo *afinfo;
+       struct xfrm_mode *inner_mode;
        int family = x->props.family;
        int err;
 
@@ -1755,6 +2005,49 @@ int xfrm_init_state(struct xfrm_state *x)
                goto error;
 
        err = -EPROTONOSUPPORT;
+
+       if (x->sel.family != AF_UNSPEC) {
+               inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
+               if (inner_mode == NULL)
+                       goto error;
+
+               if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
+                   family != x->sel.family) {
+                       xfrm_put_mode(inner_mode);
+                       goto error;
+               }
+
+               x->inner_mode = inner_mode;
+       } else {
+               struct xfrm_mode *inner_mode_iaf;
+
+               inner_mode = xfrm_get_mode(x->props.mode, AF_INET);
+               if (inner_mode == NULL)
+                       goto error;
+
+               if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
+                       xfrm_put_mode(inner_mode);
+                       goto error;
+               }
+
+               inner_mode_iaf = xfrm_get_mode(x->props.mode, AF_INET6);
+               if (inner_mode_iaf == NULL)
+                       goto error;
+
+               if (!(inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)) {
+                       xfrm_put_mode(inner_mode_iaf);
+                       goto error;
+               }
+
+               if (x->props.family == AF_INET) {
+                       x->inner_mode = inner_mode;
+                       x->inner_mode_iaf = inner_mode_iaf;
+               } else {
+                       x->inner_mode = inner_mode_iaf;
+                       x->inner_mode_iaf = inner_mode;
+               }
+       }
+
        x->type = xfrm_get_type(x->id.proto, family);
        if (x->type == NULL)
                goto error;
@@ -1763,8 +2056,8 @@ int xfrm_init_state(struct xfrm_state *x)
        if (err)
                goto error;
 
-       x->mode = xfrm_get_mode(x->props.mode, family);
-       if (x->mode == NULL)
+       x->outer_mode = xfrm_get_mode(x->props.mode, family);
+       if (x->outer_mode == NULL)
                goto error;
 
        x->km.state = XFRM_STATE_VALID;
@@ -1791,3 +2084,173 @@ void __init xfrm_state_init(void)
        INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task);
 }
 
+#ifdef CONFIG_AUDITSYSCALL
+static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
+                                    struct audit_buffer *audit_buf)
+{
+       struct xfrm_sec_ctx *ctx = x->security;
+       u32 spi = ntohl(x->id.spi);
+
+       if (ctx)
+               audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
+                                ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
+
+       switch(x->props.family) {
+       case AF_INET:
+               audit_log_format(audit_buf,
+                                " src=" NIPQUAD_FMT " dst=" NIPQUAD_FMT,
+                                NIPQUAD(x->props.saddr.a4),
+                                NIPQUAD(x->id.daddr.a4));
+               break;
+       case AF_INET6:
+               audit_log_format(audit_buf,
+                                " src=" NIP6_FMT " dst=" NIP6_FMT,
+                                NIP6(*(struct in6_addr *)x->props.saddr.a6),
+                                NIP6(*(struct in6_addr *)x->id.daddr.a6));
+               break;
+       }
+
+       audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
+}
+
+static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
+                                     struct audit_buffer *audit_buf)
+{
+       struct iphdr *iph4;
+       struct ipv6hdr *iph6;
+
+       switch (family) {
+       case AF_INET:
+               iph4 = ip_hdr(skb);
+               audit_log_format(audit_buf,
+                                " src=" NIPQUAD_FMT " dst=" NIPQUAD_FMT,
+                                NIPQUAD(iph4->saddr),
+                                NIPQUAD(iph4->daddr));
+               break;
+       case AF_INET6:
+               iph6 = ipv6_hdr(skb);
+               audit_log_format(audit_buf,
+                                " src=" NIP6_FMT " dst=" NIP6_FMT
+                                " flowlbl=0x%x%02x%02x",
+                                NIP6(iph6->saddr),
+                                NIP6(iph6->daddr),
+                                iph6->flow_lbl[0] & 0x0f,
+                                iph6->flow_lbl[1],
+                                iph6->flow_lbl[2]);
+               break;
+       }
+}
+
+void xfrm_audit_state_add(struct xfrm_state *x, int result,
+                         uid_t auid, u32 sessionid, u32 secid)
+{
+       struct audit_buffer *audit_buf;
+
+       audit_buf = xfrm_audit_start("SAD-add");
+       if (audit_buf == NULL)
+               return;
+       xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
+       xfrm_audit_helper_sainfo(x, audit_buf);
+       audit_log_format(audit_buf, " res=%u", result);
+       audit_log_end(audit_buf);
+}
+EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
+
+void xfrm_audit_state_delete(struct xfrm_state *x, int result,
+                            uid_t auid, u32 sessionid, u32 secid)
+{
+       struct audit_buffer *audit_buf;
+
+       audit_buf = xfrm_audit_start("SAD-delete");
+       if (audit_buf == NULL)
+               return;
+       xfrm_audit_helper_usrinfo(auid, sessionid, secid, audit_buf);
+       xfrm_audit_helper_sainfo(x, audit_buf);
+       audit_log_format(audit_buf, " res=%u", result);
+       audit_log_end(audit_buf);
+}
+EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
+
+void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
+                                     struct sk_buff *skb)
+{
+       struct audit_buffer *audit_buf;
+       u32 spi;
+
+       audit_buf = xfrm_audit_start("SA-replay-overflow");
+       if (audit_buf == NULL)
+               return;
+       xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
+       /* don't record the sequence number because it's inherent in this kind
+        * of audit message */
+       spi = ntohl(x->id.spi);
+       audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
+       audit_log_end(audit_buf);
+}
+EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
+
+static void xfrm_audit_state_replay(struct xfrm_state *x,
+                            struct sk_buff *skb, __be32 net_seq)
+{
+       struct audit_buffer *audit_buf;
+       u32 spi;
+
+       audit_buf = xfrm_audit_start("SA-replayed-pkt");
+       if (audit_buf == NULL)
+               return;
+       xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
+       spi = ntohl(x->id.spi);
+       audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
+                        spi, spi, ntohl(net_seq));
+       audit_log_end(audit_buf);
+}
+
+void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
+{
+       struct audit_buffer *audit_buf;
+
+       audit_buf = xfrm_audit_start("SA-notfound");
+       if (audit_buf == NULL)
+               return;
+       xfrm_audit_helper_pktinfo(skb, family, audit_buf);
+       audit_log_end(audit_buf);
+}
+EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
+
+void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
+                              __be32 net_spi, __be32 net_seq)
+{
+       struct audit_buffer *audit_buf;
+       u32 spi;
+
+       audit_buf = xfrm_audit_start("SA-notfound");
+       if (audit_buf == NULL)
+               return;
+       xfrm_audit_helper_pktinfo(skb, family, audit_buf);
+       spi = ntohl(net_spi);
+       audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
+                        spi, spi, ntohl(net_seq));
+       audit_log_end(audit_buf);
+}
+EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
+
+void xfrm_audit_state_icvfail(struct xfrm_state *x,
+                             struct sk_buff *skb, u8 proto)
+{
+       struct audit_buffer *audit_buf;
+       __be32 net_spi;
+       __be32 net_seq;
+
+       audit_buf = xfrm_audit_start("SA-icv-failure");
+       if (audit_buf == NULL)
+               return;
+       xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
+       if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
+               u32 spi = ntohl(net_spi);
+               audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
+                                spi, spi, ntohl(net_seq));
+       }
+       audit_log_end(audit_buf);
+}
+EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
+#endif /* CONFIG_AUDITSYSCALL */