[GFS2] Add nanosecond timestamp feature
[safe/jmp/linux-2.6] / net / xfrm / xfrm_user.c
index d6e6527..c06883b 100644 (file)
@@ -10,6 +10,7 @@
  *
  */
 
+#include <linux/crypto.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/types.h>
 #include <net/xfrm.h>
 #include <net/netlink.h>
 #include <asm/uaccess.h>
-
-struct sock *xfrm_nl;
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+#include <linux/in6.h>
+#endif
+#include <linux/audit.h>
 
 static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
 {
@@ -45,7 +48,7 @@ static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
 
        algp = RTA_DATA(rt);
 
-       len -= (algp->alg_key_len + 7U) / 8; 
+       len -= (algp->alg_key_len + 7U) / 8;
        if (len < 0)
                return -EINVAL;
 
@@ -68,7 +71,7 @@ static int verify_one_alg(struct rtattr **xfrma, enum xfrm_attr_type_t type)
 
        default:
                return -EINVAL;
-       };
+       }
 
        algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
        return 0;
@@ -88,6 +91,22 @@ static int verify_encap_tmpl(struct rtattr **xfrma)
        return 0;
 }
 
+static int verify_one_addr(struct rtattr **xfrma, enum xfrm_attr_type_t type,
+                          xfrm_address_t **addrp)
+{
+       struct rtattr *rt = xfrma[type - 1];
+
+       if (!rt)
+               return 0;
+
+       if ((rt->rta_len - sizeof(*rt)) < sizeof(**addrp))
+               return -EINVAL;
+
+       if (addrp)
+               *addrp = RTA_DATA(rt);
+
+       return 0;
+}
 
 static inline int verify_sec_ctx_len(struct rtattr **xfrma)
 {
@@ -103,9 +122,6 @@ static inline int verify_sec_ctx_len(struct rtattr **xfrma)
 
        uctx = RTA_DATA(rt);
 
-       if (uctx->ctx_len > PAGE_SIZE)
-               return -EINVAL;
-
        len += sizeof(struct xfrm_user_sec_ctx);
        len += uctx->ctx_len;
 
@@ -136,7 +152,7 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
 
        default:
                goto out;
-       };
+       }
 
        err = -EINVAL;
        switch (p->id.proto) {
@@ -161,9 +177,22 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
                        goto out;
                break;
 
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+       case IPPROTO_DSTOPTS:
+       case IPPROTO_ROUTING:
+               if (xfrma[XFRMA_ALG_COMP-1]     ||
+                   xfrma[XFRMA_ALG_AUTH-1]     ||
+                   xfrma[XFRMA_ALG_CRYPT-1]    ||
+                   xfrma[XFRMA_ENCAP-1]        ||
+                   xfrma[XFRMA_SEC_CTX-1]      ||
+                   !xfrma[XFRMA_COADDR-1])
+                       goto out;
+               break;
+#endif
+
        default:
                goto out;
-       };
+       }
 
        if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
                goto out;
@@ -175,16 +204,20 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
                goto out;
        if ((err = verify_sec_ctx_len(xfrma)))
                goto out;
+       if ((err = verify_one_addr(xfrma, XFRMA_COADDR, NULL)))
+               goto out;
 
        err = -EINVAL;
        switch (p->mode) {
-       case 0:
-       case 1:
+       case XFRM_MODE_TRANSPORT:
+       case XFRM_MODE_TUNNEL:
+       case XFRM_MODE_ROUTEOPTIMIZATION:
+       case XFRM_MODE_BEET:
                break;
 
        default:
                goto out;
-       };
+       }
 
        err = 0;
 
@@ -212,11 +245,11 @@ static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
        *props = algo->desc.sadb_alg_id;
 
        len = sizeof(*ualg) + (ualg->alg_key_len + 7U) / 8;
-       p = kmalloc(len, GFP_KERNEL);
+       p = kmemdup(ualg, len, GFP_KERNEL);
        if (!p)
                return -ENOMEM;
 
-       memcpy(p, ualg, len);
+       strcpy(p->alg_name, algo->name);
        *algpp = p;
        return 0;
 }
@@ -230,19 +263,17 @@ static int attach_encap_tmpl(struct xfrm_encap_tmpl **encapp, struct rtattr *u_a
                return 0;
 
        uencap = RTA_DATA(rta);
-       p = kmalloc(sizeof(*p), GFP_KERNEL);
+       p = kmemdup(uencap, sizeof(*p), GFP_KERNEL);
        if (!p)
                return -ENOMEM;
 
-       memcpy(p, uencap, sizeof(*p));
        *encapp = p;
        return 0;
 }
 
 
-static inline int xfrm_user_sec_ctx_size(struct xfrm_policy *xp)
+static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
 {
-       struct xfrm_sec_ctx *xfrm_ctx = xp->security;
        int len = 0;
 
        if (xfrm_ctx) {
@@ -263,6 +294,23 @@ static int attach_sec_ctx(struct xfrm_state *x, struct rtattr *u_arg)
        return security_xfrm_state_alloc(x, uctx);
 }
 
+static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
+{
+       struct rtattr *rta = u_arg;
+       xfrm_address_t *p, *uaddrp;
+
+       if (!rta)
+               return 0;
+
+       uaddrp = RTA_DATA(rta);
+       p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
+       if (!p)
+               return -ENOMEM;
+
+       *addrpp = p;
+       return 0;
+}
+
 static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
 {
        memcpy(&x->id, &p->id, sizeof(x->id));
@@ -272,7 +320,7 @@ static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *
        x->props.replay_window = p->replay_window;
        x->props.reqid = p->reqid;
        x->props.family = p->family;
-       x->props.saddr = p->saddr;
+       memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
        x->props.flags = p->flags;
 }
 
@@ -352,7 +400,8 @@ static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
                goto error;
        if ((err = attach_encap_tmpl(&x->encap, xfrma[XFRMA_ENCAP-1])))
                goto error;
-
+       if ((err = attach_one_addr(&x->coaddr, xfrma[XFRMA_COADDR-1])))
+               goto error;
        err = xfrm_init_state(x);
        if (err)
                goto error;
@@ -384,18 +433,19 @@ error_no_put:
        return NULL;
 }
 
-static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_usersa_info *p = NLMSG_DATA(nlh);
        struct xfrm_state *x;
        int err;
        struct km_event c;
 
-       err = verify_newsa_info(p, (struct rtattr **)xfrma);
+       err = verify_newsa_info(p, xfrma);
        if (err)
                return err;
 
-       x = xfrm_state_construct(p, (struct rtattr **)xfrma, &err);
+       x = xfrm_state_construct(p, xfrma, &err);
        if (!x)
                return err;
 
@@ -405,6 +455,9 @@ static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
        else
                err = xfrm_state_update(x);
 
+       xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                      AUDIT_MAC_IPSEC_ADDSA, err ? 0 : 1, NULL, x);
+
        if (err < 0) {
                x->km.state = XFRM_STATE_DEAD;
                __xfrm_state_put(x);
@@ -421,34 +474,73 @@ out:
        return err;
 }
 
-static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
+                                                struct rtattr **xfrma,
+                                                int *errp)
 {
-       struct xfrm_state *x;
+       struct xfrm_state *x = NULL;
        int err;
+
+       if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
+               err = -ESRCH;
+               x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
+       } else {
+               xfrm_address_t *saddr = NULL;
+
+               err = verify_one_addr(xfrma, XFRMA_SRCADDR, &saddr);
+               if (err)
+                       goto out;
+
+               if (!saddr) {
+                       err = -EINVAL;
+                       goto out;
+               }
+
+               err = -ESRCH;
+               x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
+                                            p->family);
+       }
+
+ out:
+       if (!x && errp)
+               *errp = err;
+       return x;
+}
+
+static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
+{
+       struct xfrm_state *x;
+       int err = -ESRCH;
        struct km_event c;
        struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
 
-       x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
+       x = xfrm_user_state_lookup(p, xfrma, &err);
        if (x == NULL)
-               return -ESRCH;
+               return err;
+
+       if ((err = security_xfrm_state_delete(x)) != 0)
+               goto out;
 
        if (xfrm_state_kern(x)) {
-               xfrm_state_put(x);
-               return -EPERM;
+               err = -EPERM;
+               goto out;
        }
 
        err = xfrm_state_delete(x);
-       if (err < 0) {
-               xfrm_state_put(x);
-               return err;
-       }
+
+       if (err < 0)
+               goto out;
 
        c.seq = nlh->nlmsg_seq;
        c.pid = nlh->nlmsg_pid;
        c.event = nlh->nlmsg_type;
        km_state_notify(x, &c);
-       xfrm_state_put(x);
 
+out:
+       xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                      AUDIT_MAC_IPSEC_DELSA, err ? 0 : 1, NULL, x);
+       xfrm_state_put(x);
        return err;
 }
 
@@ -459,7 +551,7 @@ static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
        memcpy(&p->lft, &x->lft, sizeof(p->lft));
        memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
        memcpy(&p->stats, &x->stats, sizeof(p->stats));
-       p->saddr = x->props.saddr;
+       memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
        p->mode = x->props.mode;
        p->replay_window = x->props.replay_window;
        p->reqid = x->props.reqid;
@@ -484,7 +576,7 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
        struct sk_buff *skb = sp->out_skb;
        struct xfrm_usersa_info *p;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb->tail;
+       unsigned char *b = skb_tail_pointer(skb);
 
        if (sp->this_idx < sp->start_idx)
                goto out;
@@ -522,14 +614,21 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
                uctx->ctx_len = x->security->ctx_len;
                memcpy(uctx + 1, x->security->ctx_str, x->security->ctx_len);
        }
-       nlh->nlmsg_len = skb->tail - b;
+
+       if (x->coaddr)
+               RTA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
+
+       if (x->lastused)
+               RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
+
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
 out:
        sp->this_idx++;
        return 0;
 
 nlmsg_failure:
 rtattr_failure:
-       skb_trim(skb, b - skb->data);
+       nlmsg_trim(skb, b);
        return -1;
 }
 
@@ -543,7 +642,7 @@ static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
        info.nlmsg_flags = NLM_F_MULTI;
        info.this_idx = 0;
        info.start_idx = cb->args[0];
-       (void) xfrm_state_walk(IPSEC_PROTO_ANY, dump_one_state, &info);
+       (void) xfrm_state_walk(0, dump_one_state, &info);
        cb->args[0] = info.this_idx;
 
        return skb->len;
@@ -559,7 +658,6 @@ static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
        if (!skb)
                return ERR_PTR(-ENOMEM);
 
-       NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
        info.in_skb = in_skb;
        info.out_skb = skb;
        info.nlmsg_seq = seq;
@@ -574,15 +672,122 @@ static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
        return skb;
 }
 
-static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
+{
+       struct xfrmk_spdinfo si;
+       struct xfrmu_spdinfo spc;
+       struct xfrmu_spdhinfo sph;
+       struct nlmsghdr *nlh;
+       u32 *f;
+
+       nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
+       if (nlh == NULL) /* shouldnt really happen ... */
+               return -EMSGSIZE;
+
+       f = nlmsg_data(nlh);
+       *f = flags;
+       xfrm_spd_getinfo(&si);
+       spc.incnt = si.incnt;
+       spc.outcnt = si.outcnt;
+       spc.fwdcnt = si.fwdcnt;
+       spc.inscnt = si.inscnt;
+       spc.outscnt = si.outscnt;
+       spc.fwdscnt = si.fwdscnt;
+       sph.spdhcnt = si.spdhcnt;
+       sph.spdhmcnt = si.spdhmcnt;
+
+       NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
+       NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
+
+       return nlmsg_end(skb, nlh);
+
+nla_put_failure:
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
+}
+
+static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
+{
+       struct sk_buff *r_skb;
+       u32 *flags = NLMSG_DATA(nlh);
+       u32 spid = NETLINK_CB(skb).pid;
+       u32 seq = nlh->nlmsg_seq;
+       int len = NLMSG_LENGTH(sizeof(u32));
+
+       len += RTA_SPACE(sizeof(struct xfrmu_spdinfo));
+       len += RTA_SPACE(sizeof(struct xfrmu_spdhinfo));
+
+       r_skb = alloc_skb(len, GFP_ATOMIC);
+       if (r_skb == NULL)
+               return -ENOMEM;
+
+       if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
+               BUG();
+
+       return nlmsg_unicast(xfrm_nl, r_skb, spid);
+}
+
+static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
+{
+       struct xfrmk_sadinfo si;
+       struct xfrmu_sadhinfo sh;
+       struct nlmsghdr *nlh;
+       u32 *f;
+
+       nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
+       if (nlh == NULL) /* shouldnt really happen ... */
+               return -EMSGSIZE;
+
+       f = nlmsg_data(nlh);
+       *f = flags;
+       xfrm_sad_getinfo(&si);
+
+       sh.sadhmcnt = si.sadhmcnt;
+       sh.sadhcnt = si.sadhcnt;
+
+       NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
+       NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
+
+       return nlmsg_end(skb, nlh);
+
+nla_put_failure:
+       nlmsg_cancel(skb, nlh);
+       return -EMSGSIZE;
+}
+
+static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
+{
+       struct sk_buff *r_skb;
+       u32 *flags = NLMSG_DATA(nlh);
+       u32 spid = NETLINK_CB(skb).pid;
+       u32 seq = nlh->nlmsg_seq;
+       int len = NLMSG_LENGTH(sizeof(u32));
+
+       len += RTA_SPACE(sizeof(struct xfrmu_sadhinfo));
+       len += RTA_SPACE(sizeof(u32));
+
+       r_skb = alloc_skb(len, GFP_ATOMIC);
+
+       if (r_skb == NULL)
+               return -ENOMEM;
+
+       if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
+               BUG();
+
+       return nlmsg_unicast(xfrm_nl, r_skb, spid);
+}
+
+static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_usersa_id *p = NLMSG_DATA(nlh);
        struct xfrm_state *x;
        struct sk_buff *resp_skb;
-       int err;
+       int err = -ESRCH;
 
-       x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
-       err = -ESRCH;
+       x = xfrm_user_state_lookup(p, xfrma, &err);
        if (x == NULL)
                goto out_noput;
 
@@ -613,7 +818,7 @@ static int verify_userspi_info(struct xfrm_userspi_info *p)
 
        default:
                return -EINVAL;
-       };
+       }
 
        if (p->min > p->max)
                return -EINVAL;
@@ -621,7 +826,8 @@ static int verify_userspi_info(struct xfrm_userspi_info *p)
        return 0;
 }
 
-static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_state *x;
        struct xfrm_userspi_info *p;
@@ -680,7 +886,7 @@ out_noput:
        return err;
 }
 
-static int verify_policy_dir(__u8 dir)
+static int verify_policy_dir(u8 dir)
 {
        switch (dir) {
        case XFRM_POLICY_IN:
@@ -690,7 +896,23 @@ static int verify_policy_dir(__u8 dir)
 
        default:
                return -EINVAL;
-       };
+       }
+
+       return 0;
+}
+
+static int verify_policy_type(u8 type)
+{
+       switch (type) {
+       case XFRM_POLICY_TYPE_MAIN:
+#ifdef CONFIG_XFRM_SUB_POLICY
+       case XFRM_POLICY_TYPE_SUB:
+#endif
+               break;
+
+       default:
+               return -EINVAL;
+       }
 
        return 0;
 }
@@ -706,7 +928,7 @@ static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
 
        default:
                return -EINVAL;
-       };
+       }
 
        switch (p->action) {
        case XFRM_POLICY_ALLOW:
@@ -715,7 +937,7 @@ static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
 
        default:
                return -EINVAL;
-       };
+       }
 
        switch (p->sel.family) {
        case AF_INET:
@@ -730,7 +952,7 @@ static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
 
        default:
                return -EINVAL;
-       };
+       }
 
        return verify_policy_dir(p->dir);
 }
@@ -766,28 +988,86 @@ static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
                t->aalgos = ut->aalgos;
                t->ealgos = ut->ealgos;
                t->calgos = ut->calgos;
+               t->encap_family = ut->family;
        }
 }
 
+static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
+{
+       int i;
+
+       if (nr > XFRM_MAX_DEPTH)
+               return -EINVAL;
+
+       for (i = 0; i < nr; i++) {
+               /* We never validated the ut->family value, so many
+                * applications simply leave it at zero.  The check was
+                * never made and ut->family was ignored because all
+                * templates could be assumed to have the same family as
+                * the policy itself.  Now that we will have ipv4-in-ipv6
+                * and ipv6-in-ipv4 tunnels, this is no longer true.
+                */
+               if (!ut[i].family)
+                       ut[i].family = family;
+
+               switch (ut[i].family) {
+               case AF_INET:
+                       break;
+#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
+               case AF_INET6:
+                       break;
+#endif
+               default:
+                       return -EINVAL;
+               }
+       }
+
+       return 0;
+}
+
 static int copy_from_user_tmpl(struct xfrm_policy *pol, struct rtattr **xfrma)
 {
        struct rtattr *rt = xfrma[XFRMA_TMPL-1];
-       struct xfrm_user_tmpl *utmpl;
-       int nr;
 
        if (!rt) {
                pol->xfrm_nr = 0;
        } else {
-               nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
+               struct xfrm_user_tmpl *utmpl = RTA_DATA(rt);
+               int nr = (rt->rta_len - sizeof(*rt)) / sizeof(*utmpl);
+               int err;
 
-               if (nr > XFRM_MAX_DEPTH)
-                       return -EINVAL;
+               err = validate_tmpl(nr, utmpl, pol->family);
+               if (err)
+                       return err;
 
                copy_templates(pol, RTA_DATA(rt), nr);
        }
        return 0;
 }
 
+static int copy_from_user_policy_type(u8 *tp, struct rtattr **xfrma)
+{
+       struct rtattr *rt = xfrma[XFRMA_POLICY_TYPE-1];
+       struct xfrm_userpolicy_type *upt;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
+       int err;
+
+       if (rt) {
+               if (rt->rta_len < sizeof(*upt))
+                       return -EINVAL;
+
+               upt = RTA_DATA(rt);
+               type = upt->type;
+       }
+
+       err = verify_policy_type(type);
+       if (err)
+               return err;
+
+       *tp = type;
+       return 0;
+}
+
 static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
 {
        xp->priority = p->priority;
@@ -826,19 +1106,24 @@ static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p,
 
        copy_from_user_policy(xp, p);
 
+       err = copy_from_user_policy_type(&xp->type, xfrma);
+       if (err)
+               goto error;
+
        if (!(err = copy_from_user_tmpl(xp, xfrma)))
                err = copy_from_user_sec_ctx(xp, xfrma);
-
-       if (err) {
-               *errp = err;
-               kfree(xp);
-               xp = NULL;
-       }
+       if (err)
+               goto error;
 
        return xp;
+ error:
+       *errp = err;
+       kfree(xp);
+       return NULL;
 }
 
-static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_userpolicy_info *p = NLMSG_DATA(nlh);
        struct xfrm_policy *xp;
@@ -849,11 +1134,11 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
        err = verify_newpolicy_info(p);
        if (err)
                return err;
-       err = verify_sec_ctx_len((struct rtattr **)xfrma);
+       err = verify_sec_ctx_len(xfrma);
        if (err)
                return err;
 
-       xp = xfrm_policy_construct(p, (struct rtattr **)xfrma, &err);
+       xp = xfrm_policy_construct(p, xfrma, &err);
        if (!xp)
                return err;
 
@@ -863,6 +1148,9 @@ static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
         * a type XFRM_MSG_UPDPOLICY - JHS */
        excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
        err = xfrm_policy_insert(p->dir, xp, excl);
+       xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                      AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
+
        if (err) {
                security_xfrm_policy_free(xp);
                kfree(xp);
@@ -892,7 +1180,7 @@ static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
                struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
 
                memcpy(&up->id, &kp->id, sizeof(up->id));
-               up->family = xp->family;
+               up->family = kp->encap_family;
                memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
                up->reqid = kp->reqid;
                up->mode = kp->mode;
@@ -912,27 +1200,63 @@ rtattr_failure:
        return -1;
 }
 
-static int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
+static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
 {
-       if (xp->security) {
-               int ctx_size = sizeof(struct xfrm_sec_ctx) +
-                               xp->security->ctx_len;
-               struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
-               struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
+       int ctx_size = sizeof(struct xfrm_sec_ctx) + s->ctx_len;
+       struct rtattr *rt = __RTA_PUT(skb, XFRMA_SEC_CTX, ctx_size);
+       struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
+
+       uctx->exttype = XFRMA_SEC_CTX;
+       uctx->len = ctx_size;
+       uctx->ctx_doi = s->ctx_doi;
+       uctx->ctx_alg = s->ctx_alg;
+       uctx->ctx_len = s->ctx_len;
+       memcpy(uctx + 1, s->ctx_str, s->ctx_len);
+       return 0;
 
-               uctx->exttype = XFRMA_SEC_CTX;
-               uctx->len = ctx_size;
-               uctx->ctx_doi = xp->security->ctx_doi;
-               uctx->ctx_alg = xp->security->ctx_alg;
-               uctx->ctx_len = xp->security->ctx_len;
-               memcpy(uctx + 1, xp->security->ctx_str, xp->security->ctx_len);
+ rtattr_failure:
+       return -1;
+}
+
+static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
+{
+       if (x->security) {
+               return copy_sec_ctx(x->security, skb);
        }
        return 0;
+}
 
- rtattr_failure:
+static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
+{
+       if (xp->security) {
+               return copy_sec_ctx(xp->security, skb);
+       }
+       return 0;
+}
+
+#ifdef CONFIG_XFRM_SUB_POLICY
+static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
+{
+       struct xfrm_userpolicy_type upt;
+
+       memset(&upt, 0, sizeof(upt));
+       upt.type = type;
+
+       RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
+
+       return 0;
+
+rtattr_failure:
        return -1;
 }
 
+#else
+static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
+{
+       return 0;
+}
+#endif
+
 static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
 {
        struct xfrm_dump_info *sp = ptr;
@@ -940,7 +1264,7 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
        struct sk_buff *in_skb = sp->in_skb;
        struct sk_buff *skb = sp->out_skb;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb->tail;
+       unsigned char *b = skb_tail_pointer(skb);
 
        if (sp->this_idx < sp->start_idx)
                goto out;
@@ -956,14 +1280,16 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
                goto nlmsg_failure;
        if (copy_to_user_sec_ctx(xp, skb))
                goto nlmsg_failure;
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
+               goto nlmsg_failure;
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
 out:
        sp->this_idx++;
        return 0;
 
 nlmsg_failure:
-       skb_trim(skb, b - skb->data);
+       nlmsg_trim(skb, b);
        return -1;
 }
 
@@ -977,7 +1303,10 @@ static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
        info.nlmsg_flags = NLM_F_MULTI;
        info.this_idx = 0;
        info.start_idx = cb->args[0];
-       (void) xfrm_policy_walk(dump_one_policy, &info);
+       (void) xfrm_policy_walk(XFRM_POLICY_TYPE_MAIN, dump_one_policy, &info);
+#ifdef CONFIG_XFRM_SUB_POLICY
+       (void) xfrm_policy_walk(XFRM_POLICY_TYPE_SUB, dump_one_policy, &info);
+#endif
        cb->args[0] = info.this_idx;
 
        return skb->len;
@@ -994,7 +1323,6 @@ static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
        if (!skb)
                return ERR_PTR(-ENOMEM);
 
-       NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
        info.in_skb = in_skb;
        info.out_skb = skb;
        info.nlmsg_seq = seq;
@@ -1009,10 +1337,12 @@ static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
        return skb;
 }
 
-static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_policy *xp;
        struct xfrm_userpolicy_id *p;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err;
        struct km_event c;
        int delete;
@@ -1020,18 +1350,21 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
        p = NLMSG_DATA(nlh);
        delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
 
+       err = copy_from_user_policy_type(&type, xfrma);
+       if (err)
+               return err;
+
        err = verify_policy_dir(p->dir);
        if (err)
                return err;
 
        if (p->index)
-               xp = xfrm_policy_byid(p->dir, p->index, delete);
+               xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
        else {
-               struct rtattr **rtattrs = (struct rtattr **)xfrma;
-               struct rtattr *rt = rtattrs[XFRMA_SEC_CTX-1];
+               struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
                struct xfrm_policy tmp;
 
-               err = verify_sec_ctx_len(rtattrs);
+               err = verify_sec_ctx_len(xfrma);
                if (err)
                        return err;
 
@@ -1042,7 +1375,8 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
                        if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
                                return err;
                }
-               xp = xfrm_policy_bysel_ctx(p->dir, &p->sel, tmp.security, delete);
+               xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
+                                          delete, &err);
                security_xfrm_policy_free(&tmp);
        }
        if (xp == NULL)
@@ -1060,6 +1394,12 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
                                              MSG_DONTWAIT);
                }
        } else {
+               xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                              AUDIT_MAC_IPSEC_DELSPD, err ? 0 : 1, xp, NULL);
+
+               if (err != 0)
+                       goto out;
+
                c.data.byid = p->index;
                c.event = nlh->nlmsg_type;
                c.seq = nlh->nlmsg_seq;
@@ -1067,17 +1407,24 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
                km_policy_notify(xp, p->dir, &c);
        }
 
+out:
        xfrm_pol_put(xp);
-
        return err;
 }
 
-static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct km_event c;
        struct xfrm_usersa_flush *p = NLMSG_DATA(nlh);
+       struct xfrm_audit audit_info;
+       int err;
 
-       xfrm_state_flush(p->proto);
+       audit_info.loginuid = NETLINK_CB(skb).loginuid;
+       audit_info.secid = NETLINK_CB(skb).sid;
+       err = xfrm_state_flush(p->proto, &audit_info);
+       if (err)
+               return err;
        c.data.proto = p->proto;
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
@@ -1093,16 +1440,18 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
        struct xfrm_aevent_id *id;
        struct nlmsghdr *nlh;
        struct xfrm_lifetime_cur ltime;
-       unsigned char *b = skb->tail;
+       unsigned char *b = skb_tail_pointer(skb);
 
        nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id));
        id = NLMSG_DATA(nlh);
        nlh->nlmsg_flags = 0;
 
-       id->sa_id.daddr = x->id.daddr;
+       memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
        id->sa_id.spi = x->id.spi;
        id->sa_id.family = x->props.family;
        id->sa_id.proto = x->id.proto;
+       memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
+       id->reqid = x->props.reqid;
        id->flags = c->data.aevent;
 
        RTA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
@@ -1123,16 +1472,17 @@ static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
                RTA_PUT(skb,XFRMA_ETIMER_THRESH,sizeof(u32),&etimer);
        }
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
        return skb->len;
 
 rtattr_failure:
 nlmsg_failure:
-       skb_trim(skb, b - skb->data);
+       nlmsg_trim(skb, b);
        return -1;
 }
 
-static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_state *x;
        struct sk_buff *r_skb;
@@ -1157,7 +1507,7 @@ static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
 
        x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
        if (x == NULL) {
-               kfree(r_skb);
+               kfree_skb(r_skb);
                return -ESRCH;
        }
 
@@ -1180,7 +1530,8 @@ static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
        return err;
 }
 
-static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_state *x;
        struct km_event c;
@@ -1204,7 +1555,7 @@ static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
                goto out;
 
        spin_lock_bh(&x->lock);
-       err = xfrm_update_ae_params(x,(struct rtattr **)xfrma);
+       err = xfrm_update_ae_params(x, xfrma);
        spin_unlock_bh(&x->lock);
        if (err < 0)
                goto out;
@@ -1220,11 +1571,24 @@ out:
        return err;
 }
 
-static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct km_event c;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
+       int err;
+       struct xfrm_audit audit_info;
+
+       err = copy_from_user_policy_type(&type, xfrma);
+       if (err)
+               return err;
 
-       xfrm_policy_flush();
+       audit_info.loginuid = NETLINK_CB(skb).loginuid;
+       audit_info.secid = NETLINK_CB(skb).sid;
+       err = xfrm_policy_flush(type, &audit_info);
+       if (err)
+               return err;
+       c.data.type = type;
        c.event = nlh->nlmsg_type;
        c.seq = nlh->nlmsg_seq;
        c.pid = nlh->nlmsg_pid;
@@ -1232,7 +1596,101 @@ static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **x
        return 0;
 }
 
-static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
+{
+       struct xfrm_policy *xp;
+       struct xfrm_user_polexpire *up = NLMSG_DATA(nlh);
+       struct xfrm_userpolicy_info *p = &up->pol;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
+       int err = -ENOENT;
+
+       err = copy_from_user_policy_type(&type, xfrma);
+       if (err)
+               return err;
+
+       if (p->index)
+               xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
+       else {
+               struct rtattr *rt = xfrma[XFRMA_SEC_CTX-1];
+               struct xfrm_policy tmp;
+
+               err = verify_sec_ctx_len(xfrma);
+               if (err)
+                       return err;
+
+               memset(&tmp, 0, sizeof(struct xfrm_policy));
+               if (rt) {
+                       struct xfrm_user_sec_ctx *uctx = RTA_DATA(rt);
+
+                       if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
+                               return err;
+               }
+               xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
+                                          0, &err);
+               security_xfrm_policy_free(&tmp);
+       }
+
+       if (xp == NULL)
+               return -ENOENT;
+       read_lock(&xp->lock);
+       if (xp->dead) {
+               read_unlock(&xp->lock);
+               goto out;
+       }
+
+       read_unlock(&xp->lock);
+       err = 0;
+       if (up->hard) {
+               xfrm_policy_delete(xp, p->dir);
+               xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                               AUDIT_MAC_IPSEC_DELSPD, 1, xp, NULL);
+
+       } else {
+               // reset the timers here?
+               printk("Dont know what to do with soft policy expire\n");
+       }
+       km_policy_expired(xp, p->dir, up->hard, current->pid);
+
+out:
+       xfrm_pol_put(xp);
+       return err;
+}
+
+static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
+{
+       struct xfrm_state *x;
+       int err;
+       struct xfrm_user_expire *ue = NLMSG_DATA(nlh);
+       struct xfrm_usersa_info *p = &ue->state;
+
+       x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
+
+       err = -ENOENT;
+       if (x == NULL)
+               return err;
+
+       spin_lock_bh(&x->lock);
+       err = -EINVAL;
+       if (x->km.state != XFRM_STATE_VALID)
+               goto out;
+       km_state_expired(x, ue->hard, current->pid);
+
+       if (ue->hard) {
+               __xfrm_state_delete(x);
+               xfrm_audit_log(NETLINK_CB(skb).loginuid, NETLINK_CB(skb).sid,
+                              AUDIT_MAC_IPSEC_DELSA, 1, NULL, x);
+       }
+       err = 0;
+out:
+       spin_unlock_bh(&x->lock);
+       xfrm_state_put(x);
+       return err;
+}
+
+static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_policy *xp;
        struct xfrm_user_tmpl *ut;
@@ -1254,7 +1712,8 @@ static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xf
        }
 
        /*   build an XP */
-       xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);        if (!xp) {
+       xp = xfrm_policy_construct(&ua->policy, (struct rtattr **) xfrma, &err);
+       if (!xp) {
                kfree(x);
                return err;
        }
@@ -1284,6 +1743,176 @@ static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xf
        return 0;
 }
 
+#ifdef CONFIG_XFRM_MIGRATE
+static int verify_user_migrate(struct rtattr **xfrma)
+{
+       struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
+       struct xfrm_user_migrate *um;
+
+       if (!rt)
+               return -EINVAL;
+
+       if ((rt->rta_len - sizeof(*rt)) < sizeof(*um))
+               return -EINVAL;
+
+       return 0;
+}
+
+static int copy_from_user_migrate(struct xfrm_migrate *ma,
+                                 struct rtattr **xfrma, int *num)
+{
+       struct rtattr *rt = xfrma[XFRMA_MIGRATE-1];
+       struct xfrm_user_migrate *um;
+       int i, num_migrate;
+
+       um = RTA_DATA(rt);
+       num_migrate = (rt->rta_len - sizeof(*rt)) / sizeof(*um);
+
+       if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
+               return -EINVAL;
+
+       for (i = 0; i < num_migrate; i++, um++, ma++) {
+               memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
+               memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
+               memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
+               memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
+
+               ma->proto = um->proto;
+               ma->mode = um->mode;
+               ma->reqid = um->reqid;
+
+               ma->old_family = um->old_family;
+               ma->new_family = um->new_family;
+       }
+
+       *num = i;
+       return 0;
+}
+
+static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
+                          struct rtattr **xfrma)
+{
+       struct xfrm_userpolicy_id *pi = NLMSG_DATA(nlh);
+       struct xfrm_migrate m[XFRM_MAX_DEPTH];
+       u8 type;
+       int err;
+       int n = 0;
+
+       err = verify_user_migrate((struct rtattr **)xfrma);
+       if (err)
+               return err;
+
+       err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
+       if (err)
+               return err;
+
+       err = copy_from_user_migrate((struct xfrm_migrate *)m,
+                                    (struct rtattr **)xfrma, &n);
+       if (err)
+               return err;
+
+       if (!n)
+               return 0;
+
+       xfrm_migrate(&pi->sel, pi->dir, type, m, n);
+
+       return 0;
+}
+#else
+static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
+                          struct rtattr **xfrma)
+{
+       return -ENOPROTOOPT;
+}
+#endif
+
+#ifdef CONFIG_XFRM_MIGRATE
+static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
+{
+       struct xfrm_user_migrate um;
+
+       memset(&um, 0, sizeof(um));
+       um.proto = m->proto;
+       um.mode = m->mode;
+       um.reqid = m->reqid;
+       um.old_family = m->old_family;
+       memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
+       memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
+       um.new_family = m->new_family;
+       memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
+       memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
+
+       RTA_PUT(skb, XFRMA_MIGRATE, sizeof(um), &um);
+       return 0;
+
+rtattr_failure:
+       return -1;
+}
+
+static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
+                        int num_migrate, struct xfrm_selector *sel,
+                        u8 dir, u8 type)
+{
+       struct xfrm_migrate *mp;
+       struct xfrm_userpolicy_id *pol_id;
+       struct nlmsghdr *nlh;
+       unsigned char *b = skb_tail_pointer(skb);
+       int i;
+
+       nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id));
+       pol_id = NLMSG_DATA(nlh);
+       nlh->nlmsg_flags = 0;
+
+       /* copy data from selector, dir, and type to the pol_id */
+       memset(pol_id, 0, sizeof(*pol_id));
+       memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
+       pol_id->dir = dir;
+
+       if (copy_to_user_policy_type(type, skb) < 0)
+               goto nlmsg_failure;
+
+       for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
+               if (copy_to_user_migrate(mp, skb) < 0)
+                       goto nlmsg_failure;
+       }
+
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
+       return skb->len;
+nlmsg_failure:
+       nlmsg_trim(skb, b);
+       return -1;
+}
+
+static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
+                            struct xfrm_migrate *m, int num_migrate)
+{
+       struct sk_buff *skb;
+       size_t len;
+
+       len = RTA_SPACE(sizeof(struct xfrm_user_migrate) * num_migrate);
+       len += NLMSG_SPACE(sizeof(struct xfrm_userpolicy_id));
+#ifdef CONFIG_XFRM_SUB_POLICY
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
+#endif
+       skb = alloc_skb(len, GFP_ATOMIC);
+       if (skb == NULL)
+               return -ENOMEM;
+
+       /* build migrate */
+       if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
+               BUG();
+
+       NETLINK_CB(skb).dst_group = XFRMNLGRP_MIGRATE;
+       return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE,
+                                GFP_ATOMIC);
+}
+#else
+static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
+                            struct xfrm_migrate *m, int num_migrate)
+{
+       return -ENOPROTOOPT;
+}
+#endif
 
 #define XMSGSIZE(type) NLMSG_LENGTH(sizeof(struct type))
 
@@ -1296,18 +1925,24 @@ static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
        [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
        [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
        [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
+       [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
        [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
        [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
+       [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
        [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
        [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = NLMSG_LENGTH(0),
        [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
        [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
+       [XFRM_MSG_REPORT      - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
+       [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
+       [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
+       [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = NLMSG_LENGTH(sizeof(u32)),
 };
 
 #undef XMSGSIZE
 
 static struct xfrm_link {
-       int (*doit)(struct sk_buff *, struct nlmsghdr *, void **);
+       int (*doit)(struct sk_buff *, struct nlmsghdr *, struct rtattr **);
        int (*dump)(struct sk_buff *, struct netlink_callback *);
 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
        [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
@@ -1320,61 +1955,49 @@ static struct xfrm_link {
                                                   .dump = xfrm_dump_policy   },
        [XFRM_MSG_ALLOCSPI    - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
        [XFRM_MSG_ACQUIRE     - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire   },
+       [XFRM_MSG_EXPIRE      - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
        [XFRM_MSG_UPDPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
        [XFRM_MSG_UPDSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
+       [XFRM_MSG_POLEXPIRE   - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
        [XFRM_MSG_FLUSHSA     - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa      },
        [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy  },
        [XFRM_MSG_NEWAE       - XFRM_MSG_BASE] = { .doit = xfrm_new_ae  },
        [XFRM_MSG_GETAE       - XFRM_MSG_BASE] = { .doit = xfrm_get_ae  },
+       [XFRM_MSG_MIGRATE     - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate    },
+       [XFRM_MSG_GETSADINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo   },
+       [XFRM_MSG_GETSPDINFO  - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo   },
 };
 
-static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
+static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
 {
        struct rtattr *xfrma[XFRMA_MAX];
        struct xfrm_link *link;
        int type, min_len;
 
-       if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
-               return 0;
-
        type = nlh->nlmsg_type;
-
-       /* A control message: ignore them */
-       if (type < XFRM_MSG_BASE)
-               return 0;
-
-       /* Unknown message: reply with EINVAL */
        if (type > XFRM_MSG_MAX)
-               goto err_einval;
+               return -EINVAL;
 
        type -= XFRM_MSG_BASE;
        link = &xfrm_dispatch[type];
 
        /* All operations require privileges, even GET */
-       if (security_netlink_recv(skb)) {
-               *errp = -EPERM;
-               return -1;
-       }
+       if (security_netlink_recv(skb, CAP_NET_ADMIN))
+               return -EPERM;
 
        if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
             type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
            (nlh->nlmsg_flags & NLM_F_DUMP)) {
                if (link->dump == NULL)
-                       goto err_einval;
-
-               if ((*errp = netlink_dump_start(xfrm_nl, skb, nlh,
-                                               link->dump, NULL)) != 0) {
-                       return -1;
-               }
+                       return -EINVAL;
 
-               netlink_queue_skip(nlh, skb);
-               return -1;
+               return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
        }
 
        memset(xfrma, 0, sizeof(xfrma));
 
        if (nlh->nlmsg_len < (min_len = xfrm_msg_min[type]))
-               goto err_einval;
+               return -EINVAL;
 
        if (nlh->nlmsg_len > min_len) {
                int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
@@ -1384,7 +2007,7 @@ static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *err
                        unsigned short flavor = attr->rta_type;
                        if (flavor) {
                                if (flavor > XFRMA_MAX)
-                                       goto err_einval;
+                                       return -EINVAL;
                                xfrma[flavor - 1] = attr;
                        }
                        attr = RTA_NEXT(attr, attrlen);
@@ -1392,14 +2015,9 @@ static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *err
        }
 
        if (link->doit == NULL)
-               goto err_einval;
-       *errp = link->doit(skb, nlh, (void **) &xfrma);
-
-       return *errp;
+               return -EINVAL;
 
-err_einval:
-       *errp = -EINVAL;
-       return -1;
+       return link->doit(skb, nlh, xfrma);
 }
 
 static void xfrm_netlink_rcv(struct sock *sk, int len)
@@ -1407,9 +2025,9 @@ static void xfrm_netlink_rcv(struct sock *sk, int len)
        unsigned int qlen = 0;
 
        do {
-               down(&xfrm_cfg_sem);
+               mutex_lock(&xfrm_cfg_mutex);
                netlink_run_queue(sk, &qlen, &xfrm_user_rcv_msg);
-               up(&xfrm_cfg_sem);
+               mutex_unlock(&xfrm_cfg_mutex);
 
        } while (qlen);
 }
@@ -1418,7 +2036,7 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
 {
        struct xfrm_user_expire *ue;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb->tail;
+       unsigned char *b = skb_tail_pointer(skb);
 
        nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_EXPIRE,
                        sizeof(*ue));
@@ -1428,11 +2046,11 @@ static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_eve
        copy_to_user_state(x, &ue->state);
        ue->hard = (c->data.hard != 0) ? 1 : 0;
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
        return skb->len;
 
 nlmsg_failure:
-       skb_trim(skb, b - skb->data);
+       nlmsg_trim(skb, b);
        return -1;
 }
 
@@ -1475,7 +2093,7 @@ static int xfrm_notify_sa_flush(struct km_event *c)
        struct xfrm_usersa_flush *p;
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       unsigned char *b;
+       sk_buff_data_t b;
        int len = NLMSG_LENGTH(sizeof(struct xfrm_usersa_flush));
 
        skb = alloc_skb(len, GFP_ATOMIC);
@@ -1500,7 +2118,7 @@ nlmsg_failure:
        return -1;
 }
 
-static int inline xfrm_sa_len(struct xfrm_state *x)
+static inline int xfrm_sa_len(struct xfrm_state *x)
 {
        int l = 0;
        if (x->aalg)
@@ -1521,7 +2139,7 @@ static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
        struct xfrm_usersa_id *id;
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       unsigned char *b;
+       sk_buff_data_t b;
        int len = xfrm_sa_len(x);
        int headlen;
 
@@ -1605,7 +2223,7 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
 {
        struct xfrm_user_acquire *ua;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb->tail;
+       unsigned char *b = skb_tail_pointer(skb);
        __u32 seq = xfrm_get_acqseq();
 
        nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_ACQUIRE,
@@ -1624,14 +2242,16 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
 
        if (copy_to_user_tmpl(xp, skb) < 0)
                goto nlmsg_failure;
-       if (copy_to_user_sec_ctx(xp, skb))
+       if (copy_to_user_state_sec_ctx(x, skb))
+               goto nlmsg_failure;
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
                goto nlmsg_failure;
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
        return skb->len;
 
 nlmsg_failure:
-       skb_trim(skb, b - skb->data);
+       nlmsg_trim(skb, b);
        return -1;
 }
 
@@ -1643,7 +2263,10 @@ static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
 
        len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
        len += NLMSG_SPACE(sizeof(struct xfrm_user_acquire));
-       len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
+       len += RTA_SPACE(xfrm_user_sec_ctx_size(x->security));
+#ifdef CONFIG_XFRM_SUB_POLICY
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
+#endif
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
@@ -1658,7 +2281,7 @@ static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
 /* User gives us xfrm_user_policy_info followed by an array of 0
  * or more templates.
  */
-static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
+static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
                                               u8 *data, int len, int *dir)
 {
        struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
@@ -1666,7 +2289,7 @@ static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
        struct xfrm_policy *xp;
        int nr;
 
-       switch (family) {
+       switch (sk->sk_family) {
        case AF_INET:
                if (opt != IP_XFRM_POLICY) {
                        *dir = -EOPNOTSUPP;
@@ -1693,7 +2316,7 @@ static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
                return NULL;
 
        nr = ((len - sizeof(*p)) / sizeof(*ut));
-       if (nr > XFRM_MAX_DEPTH)
+       if (validate_tmpl(nr, ut, p->sel.family))
                return NULL;
 
        if (p->dir > XFRM_POLICY_OUT)
@@ -1706,6 +2329,7 @@ static struct xfrm_policy *xfrm_compile_policy(u16 family, int opt,
        }
 
        copy_from_user_policy(xp, p);
+       xp->type = XFRM_POLICY_TYPE_MAIN;
        copy_templates(xp, ut, nr);
 
        *dir = p->dir;
@@ -1719,7 +2343,7 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
        struct xfrm_user_polexpire *upe;
        struct nlmsghdr *nlh;
        int hard = c->data.hard;
-       unsigned char *b = skb->tail;
+       unsigned char *b = skb_tail_pointer(skb);
 
        nlh = NLMSG_PUT(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe));
        upe = NLMSG_DATA(nlh);
@@ -1730,13 +2354,15 @@ static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
                goto nlmsg_failure;
        if (copy_to_user_sec_ctx(xp, skb))
                goto nlmsg_failure;
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
+               goto nlmsg_failure;
        upe->hard = !!hard;
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
        return skb->len;
 
 nlmsg_failure:
-       skb_trim(skb, b - skb->data);
+       nlmsg_trim(skb, b);
        return -1;
 }
 
@@ -1747,7 +2373,10 @@ static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_eve
 
        len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
        len += NLMSG_SPACE(sizeof(struct xfrm_user_polexpire));
-       len += RTA_SPACE(xfrm_user_sec_ctx_size(xp));
+       len += RTA_SPACE(xfrm_user_sec_ctx_size(xp->security));
+#ifdef CONFIG_XFRM_SUB_POLICY
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
+#endif
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
                return -ENOMEM;
@@ -1765,7 +2394,7 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
        struct xfrm_userpolicy_id *id;
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       unsigned char *b;
+       sk_buff_data_t b;
        int len = RTA_SPACE(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
        int headlen;
 
@@ -1774,6 +2403,9 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
                len += RTA_SPACE(headlen);
                headlen = sizeof(*id);
        }
+#ifdef CONFIG_XFRM_SUB_POLICY
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
+#endif
        len += NLMSG_SPACE(headlen);
 
        skb = alloc_skb(len, GFP_ATOMIC);
@@ -1801,6 +2433,8 @@ static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *
        copy_to_user_policy(xp, p, dir);
        if (copy_to_user_tmpl(xp, skb) < 0)
                goto nlmsg_failure;
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
+               goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
 
@@ -1817,8 +2451,12 @@ static int xfrm_notify_policy_flush(struct km_event *c)
 {
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       unsigned char *b;
-       int len = NLMSG_LENGTH(0);
+       sk_buff_data_t b;
+       int len = 0;
+#ifdef CONFIG_XFRM_SUB_POLICY
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
+#endif
+       len += NLMSG_LENGTH(0);
 
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
@@ -1827,6 +2465,9 @@ static int xfrm_notify_policy_flush(struct km_event *c)
 
 
        nlh = NLMSG_PUT(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0);
+       nlh->nlmsg_flags = 0;
+       if (copy_to_user_policy_type(c->data.type, skb) < 0)
+               goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
 
@@ -1858,22 +2499,71 @@ static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_ev
 
 }
 
+static int build_report(struct sk_buff *skb, u8 proto,
+                       struct xfrm_selector *sel, xfrm_address_t *addr)
+{
+       struct xfrm_user_report *ur;
+       struct nlmsghdr *nlh;
+       unsigned char *b = skb_tail_pointer(skb);
+
+       nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
+       ur = NLMSG_DATA(nlh);
+       nlh->nlmsg_flags = 0;
+
+       ur->proto = proto;
+       memcpy(&ur->sel, sel, sizeof(ur->sel));
+
+       if (addr)
+               RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
+
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
+       return skb->len;
+
+nlmsg_failure:
+rtattr_failure:
+       nlmsg_trim(skb, b);
+       return -1;
+}
+
+static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
+                           xfrm_address_t *addr)
+{
+       struct sk_buff *skb;
+       size_t len;
+
+       len = NLMSG_ALIGN(NLMSG_LENGTH(sizeof(struct xfrm_user_report)));
+       skb = alloc_skb(len, GFP_ATOMIC);
+       if (skb == NULL)
+               return -ENOMEM;
+
+       if (build_report(skb, proto, sel, addr) < 0)
+               BUG();
+
+       NETLINK_CB(skb).dst_group = XFRMNLGRP_REPORT;
+       return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
+}
+
 static struct xfrm_mgr netlink_mgr = {
        .id             = "netlink",
        .notify         = xfrm_send_state_notify,
        .acquire        = xfrm_send_acquire,
        .compile_policy = xfrm_compile_policy,
        .notify_policy  = xfrm_send_policy_notify,
+       .report         = xfrm_send_report,
+       .migrate        = xfrm_send_migrate,
 };
 
 static int __init xfrm_user_init(void)
 {
-       printk(KERN_INFO "Initializing IPsec netlink socket\n");
+       struct sock *nlsk;
+
+       printk(KERN_INFO "Initializing XFRM netlink socket\n");
 
-       xfrm_nl = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
-                                       xfrm_netlink_rcv, THIS_MODULE);
-       if (xfrm_nl == NULL)
+       nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
+                                    xfrm_netlink_rcv, NULL, THIS_MODULE);
+       if (nlsk == NULL)
                return -ENOMEM;
+       rcu_assign_pointer(xfrm_nl, nlsk);
 
        xfrm_register_km(&netlink_mgr);
 
@@ -1882,13 +2572,16 @@ static int __init xfrm_user_init(void)
 
 static void __exit xfrm_user_exit(void)
 {
+       struct sock *nlsk = xfrm_nl;
+
        xfrm_unregister_km(&netlink_mgr);
-       sock_release(xfrm_nl->sk_socket);
+       rcu_assign_pointer(xfrm_nl, NULL);
+       synchronize_rcu();
+       sock_release(nlsk->sk_socket);
 }
 
 module_init(xfrm_user_init);
 module_exit(xfrm_user_exit);
 MODULE_LICENSE("GPL");
 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);
-EXPORT_SYMBOL(xfrm_nl);