]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - net/xfrm/xfrm_state.c
[XFRM]: Pull xfrm_state_by{spi,src} hash table knowledge out of afinfo.
[mirror_ubuntu-artful-kernel.git] / net / xfrm / xfrm_state.c
index ee62c239a7e39f27f862a427f0c2ddb656c88a31..4a3832f81c374de3f3a77b6d3e4f27bf71b46257 100644 (file)
@@ -38,6 +38,8 @@ EXPORT_SYMBOL(sysctl_xfrm_aevent_rseqth);
 
 static DEFINE_SPINLOCK(xfrm_state_lock);
 
+#define XFRM_DST_HSIZE         1024
+
 /* Hash table to find appropriate SA towards given target (endpoint
  * of tunnel or destination of transport mode) allowed by selector.
  *
@@ -45,8 +47,93 @@ static DEFINE_SPINLOCK(xfrm_state_lock);
  * Also, it can be used by ah/esp icmp error handler to find offending SA.
  */
 static struct list_head xfrm_state_bydst[XFRM_DST_HSIZE];
+static struct list_head xfrm_state_bysrc[XFRM_DST_HSIZE];
 static struct list_head xfrm_state_byspi[XFRM_DST_HSIZE];
 
+static __inline__
+unsigned __xfrm4_dst_hash(xfrm_address_t *addr)
+{
+       unsigned h;
+       h = ntohl(addr->a4);
+       h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
+       return h;
+}
+
+static __inline__
+unsigned __xfrm6_dst_hash(xfrm_address_t *addr)
+{
+       unsigned h;
+       h = ntohl(addr->a6[2]^addr->a6[3]);
+       h = (h ^ (h>>16)) % XFRM_DST_HSIZE;
+       return h;
+}
+
+static __inline__
+unsigned __xfrm4_src_hash(xfrm_address_t *addr)
+{
+       return __xfrm4_dst_hash(addr);
+}
+
+static __inline__
+unsigned __xfrm6_src_hash(xfrm_address_t *addr)
+{
+       return __xfrm6_dst_hash(addr);
+}
+
+static __inline__
+unsigned xfrm_src_hash(xfrm_address_t *addr, unsigned short family)
+{
+       switch (family) {
+       case AF_INET:
+               return __xfrm4_src_hash(addr);
+       case AF_INET6:
+               return __xfrm6_src_hash(addr);
+       }
+       return 0;
+}
+
+static __inline__
+unsigned xfrm_dst_hash(xfrm_address_t *addr, unsigned short family)
+{
+       switch (family) {
+       case AF_INET:
+               return __xfrm4_dst_hash(addr);
+       case AF_INET6:
+               return __xfrm6_dst_hash(addr);
+       }
+       return 0;
+}
+
+static __inline__
+unsigned __xfrm4_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
+{
+       unsigned h;
+       h = ntohl(addr->a4^spi^proto);
+       h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
+       return h;
+}
+
+static __inline__
+unsigned __xfrm6_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto)
+{
+       unsigned h;
+       h = ntohl(addr->a6[2]^addr->a6[3]^spi^proto);
+       h = (h ^ (h>>10) ^ (h>>20)) % XFRM_DST_HSIZE;
+       return h;
+}
+
+static __inline__
+unsigned xfrm_spi_hash(xfrm_address_t *addr, u32 spi, u8 proto, unsigned short family)
+{
+       switch (family) {
+       case AF_INET:
+               return __xfrm4_spi_hash(addr, spi, proto);
+       case AF_INET6:
+               return __xfrm6_spi_hash(addr, spi, proto);
+       }
+       return 0;       /*XXX*/
+}
+
 DECLARE_WAIT_QUEUE_HEAD(km_waitq);
 EXPORT_SYMBOL(km_waitq);
 
@@ -77,6 +164,9 @@ static void xfrm_state_gc_destroy(struct xfrm_state *x)
        kfree(x->ealg);
        kfree(x->calg);
        kfree(x->encap);
+       kfree(x->coaddr);
+       if (x->mode)
+               xfrm_put_mode(x->mode);
        if (x->type) {
                x->type->destructor(x);
                xfrm_put_type(x->type);
@@ -192,13 +282,13 @@ struct xfrm_state *xfrm_state_alloc(void)
 {
        struct xfrm_state *x;
 
-       x = kmalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
+       x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
 
        if (x) {
-               memset(x, 0, sizeof(struct xfrm_state));
                atomic_set(&x->refcnt, 1);
                atomic_set(&x->tunnel_users, 0);
                INIT_LIST_HEAD(&x->bydst);
+               INIT_LIST_HEAD(&x->bysrc);
                INIT_LIST_HEAD(&x->byspi);
                init_timer(&x->timer);
                x->timer.function = xfrm_timer_handler;
@@ -239,6 +329,8 @@ int __xfrm_state_delete(struct xfrm_state *x)
                spin_lock(&xfrm_state_lock);
                list_del(&x->bydst);
                __xfrm_state_put(x);
+               list_del(&x->bysrc);
+               __xfrm_state_put(x);
                if (x->id.spi) {
                        list_del(&x->byspi);
                        __xfrm_state_put(x);
@@ -293,7 +385,7 @@ void xfrm_state_flush(u8 proto)
 restart:
                list_for_each_entry(x, xfrm_state_bydst+i, bydst) {
                        if (!xfrm_state_kern(x) &&
-                           (proto == IPSEC_PROTO_ANY || x->id.proto == proto)) {
+                           xfrm_id_proto_match(x->id.proto, proto)) {
                                xfrm_state_hold(x);
                                spin_unlock_bh(&xfrm_state_lock);
 
@@ -324,6 +416,83 @@ xfrm_init_tempsel(struct xfrm_state *x, struct flowi *fl,
        return 0;
 }
 
+static struct xfrm_state *__xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto, unsigned short family)
+{
+       unsigned int h = xfrm_spi_hash(daddr, spi, proto, family);
+       struct xfrm_state *x;
+
+       list_for_each_entry(x, xfrm_state_byspi+h, byspi) {
+               if (x->props.family != family ||
+                   x->id.spi       != spi ||
+                   x->id.proto     != proto)
+                       continue;
+
+               switch (family) {
+               case AF_INET:
+                       if (x->id.daddr.a4 != daddr->a4)
+                               continue;
+                       break;
+               case AF_INET6:
+                       if (!ipv6_addr_equal((struct in6_addr *)daddr,
+                                            (struct in6_addr *)
+                                            x->id.daddr.a6))
+                               continue;
+                       break;
+               };
+
+               xfrm_state_hold(x);
+               return x;
+       }
+
+       return NULL;
+}
+
+static struct xfrm_state *__xfrm_state_lookup_byaddr(xfrm_address_t *daddr, xfrm_address_t *saddr, u8 proto, unsigned short family)
+{
+       unsigned int h = xfrm_src_hash(saddr, family);
+       struct xfrm_state *x;
+
+       list_for_each_entry(x, xfrm_state_bysrc+h, bysrc) {
+               if (x->props.family != family ||
+                   x->id.proto     != proto)
+                       continue;
+
+               switch (family) {
+               case AF_INET:
+                       if (x->id.daddr.a4 != daddr->a4 ||
+                           x->props.saddr.a4 != saddr->a4)
+                               continue;
+                       break;
+               case AF_INET6:
+                       if (!ipv6_addr_equal((struct in6_addr *)daddr,
+                                            (struct in6_addr *)
+                                            x->id.daddr.a6) ||
+                           !ipv6_addr_equal((struct in6_addr *)saddr,
+                                            (struct in6_addr *)
+                                            x->props.saddr.a6))
+                               continue;
+                       break;
+               };
+
+               xfrm_state_hold(x);
+               return x;
+       }
+
+       return NULL;
+}
+
+static inline struct xfrm_state *
+__xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
+{
+       if (use_spi)
+               return __xfrm_state_lookup(&x->id.daddr, x->id.spi,
+                                          x->id.proto, family);
+       else
+               return __xfrm_state_lookup_byaddr(&x->id.daddr,
+                                                 &x->props.saddr,
+                                                 x->id.proto, family);
+}
+
 struct xfrm_state *
 xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr, 
                struct flowi *fl, struct xfrm_tmpl *tmpl,
@@ -335,18 +504,12 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
        int acquire_in_progress = 0;
        int error = 0;
        struct xfrm_state *best = NULL;
-       struct xfrm_state_afinfo *afinfo;
        
-       afinfo = xfrm_state_get_afinfo(family);
-       if (afinfo == NULL) {
-               *err = -EAFNOSUPPORT;
-               return NULL;
-       }
-
        spin_lock_bh(&xfrm_state_lock);
        list_for_each_entry(x, xfrm_state_bydst+h, bydst) {
                if (x->props.family == family &&
                    x->props.reqid == tmpl->reqid &&
+                   !(x->props.flags & XFRM_STATE_WILDRECV) &&
                    xfrm_state_addr_check(x, daddr, saddr, family) &&
                    tmpl->mode == x->props.mode &&
                    tmpl->id.proto == x->id.proto &&
@@ -366,7 +529,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                         */
                        if (x->km.state == XFRM_STATE_VALID) {
                                if (!xfrm_selector_match(&x->sel, fl, family) ||
-                                   !xfrm_sec_ctx_match(pol->security, x->security))
+                                   !security_xfrm_state_pol_flow_match(x, pol, fl))
                                        continue;
                                if (!best ||
                                    best->km.dying > x->km.dying ||
@@ -378,7 +541,7 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                        } else if (x->km.state == XFRM_STATE_ERROR ||
                                   x->km.state == XFRM_STATE_EXPIRED) {
                                if (xfrm_selector_match(&x->sel, fl, family) &&
-                                   xfrm_sec_ctx_match(pol->security, x->security))
+                                   security_xfrm_state_pol_flow_match(x, pol, fl))
                                        error = -ESRCH;
                        }
                }
@@ -387,8 +550,8 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
        x = best;
        if (!x && !error && !acquire_in_progress) {
                if (tmpl->id.spi &&
-                   (x0 = afinfo->state_lookup(daddr, tmpl->id.spi,
-                                              tmpl->id.proto)) != NULL) {
+                   (x0 = __xfrm_state_lookup(daddr, tmpl->id.spi,
+                                             tmpl->id.proto, family)) != NULL) {
                        xfrm_state_put(x0);
                        error = -EEXIST;
                        goto out;
@@ -402,10 +565,20 @@ xfrm_state_find(xfrm_address_t *daddr, xfrm_address_t *saddr,
                 * to current session. */
                xfrm_init_tempsel(x, fl, tmpl, daddr, saddr, family);
 
+               error = security_xfrm_state_alloc_acquire(x, pol->security, fl->secid);
+               if (error) {
+                       x->km.state = XFRM_STATE_DEAD;
+                       xfrm_state_put(x);
+                       x = NULL;
+                       goto out;
+               }
+
                if (km_query(x, tmpl, pol) == 0) {
                        x->km.state = XFRM_STATE_ACQ;
                        list_add_tail(&x->bydst, xfrm_state_bydst+h);
                        xfrm_state_hold(x);
+                       list_add_tail(&x->bysrc, xfrm_state_bysrc+h);
+                       xfrm_state_hold(x);
                        if (x->id.spi) {
                                h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, family);
                                list_add(&x->byspi, xfrm_state_byspi+h);
@@ -428,7 +601,6 @@ out:
        else
                *err = acquire_in_progress ? -EAGAIN : error;
        spin_unlock_bh(&xfrm_state_lock);
-       xfrm_state_put_afinfo(afinfo);
        return x;
 }
 
@@ -439,11 +611,19 @@ static void __xfrm_state_insert(struct xfrm_state *x)
        list_add(&x->bydst, xfrm_state_bydst+h);
        xfrm_state_hold(x);
 
-       h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto, x->props.family);
+       h = xfrm_src_hash(&x->props.saddr, x->props.family);
 
-       list_add(&x->byspi, xfrm_state_byspi+h);
+       list_add(&x->bysrc, xfrm_state_bysrc+h);
        xfrm_state_hold(x);
 
+       if (xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY)) {
+               h = xfrm_spi_hash(&x->id.daddr, x->id.spi, x->id.proto,
+                                 x->props.family);
+
+               list_add(&x->byspi, xfrm_state_byspi+h);
+               xfrm_state_hold(x);
+       }
+
        if (!mod_timer(&x->timer, jiffies + HZ))
                xfrm_state_hold(x);
 
@@ -464,23 +644,103 @@ 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)
+{
+       unsigned int h = xfrm_dst_hash(daddr, family);
+       struct xfrm_state *x;
+
+       list_for_each_entry(x, xfrm_state_bydst+h, bydst) {
+               if (x->props.reqid  != reqid ||
+                   x->props.mode   != mode ||
+                   x->props.family != family ||
+                   x->km.state     != XFRM_STATE_ACQ ||
+                   x->id.spi       != 0)
+                       continue;
+
+               switch (family) {
+               case AF_INET:
+                       if (x->id.daddr.a4    != daddr->a4 ||
+                           x->props.saddr.a4 != saddr->a4)
+                               continue;
+                       break;
+               case AF_INET6:
+                       if (!ipv6_addr_equal((struct in6_addr *)x->id.daddr.a6,
+                                            (struct in6_addr *)daddr) ||
+                           !ipv6_addr_equal((struct in6_addr *)
+                                            x->props.saddr.a6,
+                                            (struct in6_addr *)saddr))
+                               continue;
+                       break;
+               };
+
+               xfrm_state_hold(x);
+               return x;
+       }
+
+       if (!create)
+               return NULL;
+
+       x = xfrm_state_alloc();
+       if (likely(x)) {
+               switch (family) {
+               case AF_INET:
+                       x->sel.daddr.a4 = daddr->a4;
+                       x->sel.saddr.a4 = saddr->a4;
+                       x->sel.prefixlen_d = 32;
+                       x->sel.prefixlen_s = 32;
+                       x->props.saddr.a4 = saddr->a4;
+                       x->id.daddr.a4 = daddr->a4;
+                       break;
+
+               case AF_INET6:
+                       ipv6_addr_copy((struct in6_addr *)x->sel.daddr.a6,
+                                      (struct in6_addr *)daddr);
+                       ipv6_addr_copy((struct in6_addr *)x->sel.saddr.a6,
+                                      (struct in6_addr *)saddr);
+                       x->sel.prefixlen_d = 128;
+                       x->sel.prefixlen_s = 128;
+                       ipv6_addr_copy((struct in6_addr *)x->props.saddr.a6,
+                                      (struct in6_addr *)saddr);
+                       ipv6_addr_copy((struct in6_addr *)x->id.daddr.a6,
+                                      (struct in6_addr *)daddr);
+                       break;
+               };
+
+               x->km.state = XFRM_STATE_ACQ;
+               x->id.proto = proto;
+               x->props.family = family;
+               x->props.mode = mode;
+               x->props.reqid = reqid;
+               x->lft.hard_add_expires_seconds = XFRM_ACQ_EXPIRES;
+               xfrm_state_hold(x);
+               x->timer.expires = jiffies + XFRM_ACQ_EXPIRES*HZ;
+               add_timer(&x->timer);
+               xfrm_state_hold(x);
+               list_add_tail(&x->bydst, xfrm_state_bydst+h);
+               h = xfrm_src_hash(saddr, family);
+               xfrm_state_hold(x);
+               list_add_tail(&x->bysrc, xfrm_state_bysrc+h);
+               wake_up(&km_waitq);
+       }
+
+       return x;
+}
+
 static struct xfrm_state *__xfrm_find_acq_byseq(u32 seq);
 
 int xfrm_state_add(struct xfrm_state *x)
 {
-       struct xfrm_state_afinfo *afinfo;
        struct xfrm_state *x1;
        int family;
        int err;
+       int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
 
        family = x->props.family;
-       afinfo = xfrm_state_get_afinfo(family);
-       if (unlikely(afinfo == NULL))
-               return -EAFNOSUPPORT;
 
        spin_lock_bh(&xfrm_state_lock);
 
-       x1 = afinfo->state_lookup(&x->id.daddr, x->id.spi, x->id.proto);
+       x1 = __xfrm_state_locate(x, use_spi, family);
        if (x1) {
                xfrm_state_put(x1);
                x1 = NULL;
@@ -488,7 +748,7 @@ int xfrm_state_add(struct xfrm_state *x)
                goto out;
        }
 
-       if (x->km.seq) {
+       if (use_spi && x->km.seq) {
                x1 = __xfrm_find_acq_byseq(x->km.seq);
                if (x1 && xfrm_addr_cmp(&x1->id.daddr, &x->id.daddr, family)) {
                        xfrm_state_put(x1);
@@ -496,17 +756,16 @@ int xfrm_state_add(struct xfrm_state *x)
                }
        }
 
-       if (!x1)
-               x1 = afinfo->find_acq(
-                       x->props.mode, x->props.reqid, x->id.proto,
-                       &x->id.daddr, &x->props.saddr, 0);
+       if (use_spi && !x1)
+               x1 = __find_acq_core(family, x->props.mode, x->props.reqid,
+                                    x->id.proto,
+                                    &x->id.daddr, &x->props.saddr, 0);
 
        __xfrm_state_insert(x);
        err = 0;
 
 out:
        spin_unlock_bh(&xfrm_state_lock);
-       xfrm_state_put_afinfo(afinfo);
 
        if (!err)
                xfrm_flush_all_bundles();
@@ -522,16 +781,12 @@ EXPORT_SYMBOL(xfrm_state_add);
 
 int xfrm_state_update(struct xfrm_state *x)
 {
-       struct xfrm_state_afinfo *afinfo;
        struct xfrm_state *x1;
        int err;
-
-       afinfo = xfrm_state_get_afinfo(x->props.family);
-       if (unlikely(afinfo == NULL))
-               return -EAFNOSUPPORT;
+       int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
 
        spin_lock_bh(&xfrm_state_lock);
-       x1 = afinfo->state_lookup(&x->id.daddr, x->id.spi, x->id.proto);
+       x1 = __xfrm_state_locate(x, use_spi, x->props.family);
 
        err = -ESRCH;
        if (!x1)
@@ -551,7 +806,6 @@ int xfrm_state_update(struct xfrm_state *x)
 
 out:
        spin_unlock_bh(&xfrm_state_lock);
-       xfrm_state_put_afinfo(afinfo);
 
        if (err)
                return err;
@@ -567,6 +821,11 @@ out:
        if (likely(x1->km.state == XFRM_STATE_VALID)) {
                if (x->encap && x1->encap)
                        memcpy(x1->encap, x->encap, sizeof(*x1->encap));
+               if (x->coaddr && x1->coaddr) {
+                       memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
+               }
+               if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
+                       memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
                memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
                x1->km.dying = 0;
 
@@ -639,35 +898,79 @@ xfrm_state_lookup(xfrm_address_t *daddr, u32 spi, u8 proto,
                  unsigned short family)
 {
        struct xfrm_state *x;
-       struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
-       if (!afinfo)
-               return NULL;
 
        spin_lock_bh(&xfrm_state_lock);
-       x = afinfo->state_lookup(daddr, spi, proto);
+       x = __xfrm_state_lookup(daddr, spi, proto, family);
        spin_unlock_bh(&xfrm_state_lock);
-       xfrm_state_put_afinfo(afinfo);
        return x;
 }
 EXPORT_SYMBOL(xfrm_state_lookup);
 
+struct xfrm_state *
+xfrm_state_lookup_byaddr(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);
+       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_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);
+       spin_unlock_bh(&xfrm_state_lock);
+
+       return x;
+}
+EXPORT_SYMBOL(xfrm_find_acq);
+
+#ifdef CONFIG_XFRM_SUB_POLICY
+int
+xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
+              unsigned short family)
+{
+       int err = 0;
        struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
        if (!afinfo)
-               return NULL;
+               return -EAFNOSUPPORT;
 
        spin_lock_bh(&xfrm_state_lock);
-       x = afinfo->find_acq(mode, reqid, proto, daddr, saddr, create);
+       if (afinfo->tmpl_sort)
+               err = afinfo->tmpl_sort(dst, src, n);
        spin_unlock_bh(&xfrm_state_lock);
        xfrm_state_put_afinfo(afinfo);
-       return x;
+       return err;
 }
-EXPORT_SYMBOL(xfrm_find_acq);
+EXPORT_SYMBOL(xfrm_tmpl_sort);
+
+int
+xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
+               unsigned short family)
+{
+       int err = 0;
+       struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
+       if (!afinfo)
+               return -EAFNOSUPPORT;
+
+       spin_lock_bh(&xfrm_state_lock);
+       if (afinfo->state_sort)
+               err = afinfo->state_sort(dst, src, n);
+       spin_unlock_bh(&xfrm_state_lock);
+       xfrm_state_put_afinfo(afinfo);
+       return err;
+}
+EXPORT_SYMBOL(xfrm_state_sort);
+#endif
 
 /* Silly enough, but I'm lazy to build resolution list */
 
@@ -763,7 +1066,7 @@ int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*),
        spin_lock_bh(&xfrm_state_lock);
        for (i = 0; i < XFRM_DST_HSIZE; i++) {
                list_for_each_entry(x, xfrm_state_bydst+i, bydst) {
-                       if (proto == IPSEC_PROTO_ANY || x->id.proto == proto)
+                       if (xfrm_id_proto_match(x->id.proto, proto))
                                count++;
                }
        }
@@ -774,7 +1077,7 @@ int xfrm_state_walk(u8 proto, int (*func)(struct xfrm_state *, int, void*),
 
        for (i = 0; i < XFRM_DST_HSIZE; i++) {
                list_for_each_entry(x, xfrm_state_bydst+i, bydst) {
-                       if (proto != IPSEC_PROTO_ANY && x->id.proto != proto)
+                       if (!xfrm_id_proto_match(x->id.proto, proto))
                                continue;
                        err = func(x, --count, data);
                        if (err)
@@ -996,6 +1299,25 @@ void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 pid)
 }
 EXPORT_SYMBOL(km_policy_expired);
 
+int km_report(u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
+{
+       int err = -EINVAL;
+       int ret;
+       struct xfrm_mgr *km;
+
+       read_lock(&xfrm_km_lock);
+       list_for_each_entry(km, &xfrm_km_list, list) {
+               if (km->report) {
+                       ret = km->report(proto, sel, addr);
+                       if (!ret)
+                               err = ret;
+               }
+       }
+       read_unlock(&xfrm_km_lock);
+       return err;
+}
+EXPORT_SYMBOL(km_report);
+
 int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
 {
        int err;
@@ -1017,7 +1339,7 @@ int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen
        err = -EINVAL;
        read_lock(&xfrm_km_lock);
        list_for_each_entry(km, &xfrm_km_list, list) {
-               pol = km->compile_policy(sk->sk_family, optname, data,
+               pol = km->compile_policy(sk, optname, data,
                                         optlen, &err);
                if (err >= 0)
                        break;
@@ -1064,11 +1386,8 @@ int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
        write_lock_bh(&xfrm_state_afinfo_lock);
        if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
                err = -ENOBUFS;
-       else {
-               afinfo->state_bydst = xfrm_state_bydst;
-               afinfo->state_byspi = xfrm_state_byspi;
+       else
                xfrm_state_afinfo[afinfo->family] = afinfo;
-       }
        write_unlock_bh(&xfrm_state_afinfo_lock);
        return err;
 }
@@ -1085,11 +1404,8 @@ int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
        if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
                if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
                        err = -EINVAL;
-               else {
+               else
                        xfrm_state_afinfo[afinfo->family] = NULL;
-                       afinfo->state_byspi = NULL;
-                       afinfo->state_bydst = NULL;
-               }
        }
        write_unlock_bh(&xfrm_state_afinfo_lock);
        return err;
@@ -1162,8 +1478,6 @@ int xfrm_state_mtu(struct xfrm_state *x, int mtu)
        return res;
 }
 
-EXPORT_SYMBOL(xfrm_state_mtu);
-
 int xfrm_init_state(struct xfrm_state *x)
 {
        struct xfrm_state_afinfo *afinfo;
@@ -1193,6 +1507,10 @@ 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)
+               goto error;
+
        x->km.state = XFRM_STATE_VALID;
 
 error:
@@ -1207,6 +1525,7 @@ void __init xfrm_state_init(void)
 
        for (i=0; i<XFRM_DST_HSIZE; i++) {
                INIT_LIST_HEAD(&xfrm_state_bydst[i]);
+               INIT_LIST_HEAD(&xfrm_state_bysrc[i]);
                INIT_LIST_HEAD(&xfrm_state_byspi[i]);
        }
        INIT_WORK(&xfrm_state_gc_work, xfrm_state_gc_task, NULL);