[GFS2] Add nanosecond timestamp feature
[safe/jmp/linux-2.6] / net / xfrm / xfrm_user.c
index d54b3a7..c06883b 100644 (file)
@@ -31,6 +31,7 @@
 #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)
 {
@@ -47,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;
 
@@ -70,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;
@@ -151,7 +152,7 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
 
        default:
                goto out;
-       };
+       }
 
        err = -EINVAL;
        switch (p->id.proto) {
@@ -191,7 +192,7 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
 
        default:
                goto out;
-       };
+       }
 
        if ((err = verify_one_alg(xfrma, XFRMA_ALG_AUTH)))
                goto out;
@@ -216,7 +217,7 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
 
        default:
                goto out;
-       };
+       }
 
        err = 0;
 
@@ -244,11 +245,10 @@ 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;
@@ -263,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) {
@@ -305,11 +303,10 @@ static int attach_one_addr(xfrm_address_t **addrpp, struct rtattr *u_arg)
                return 0;
 
        uaddrp = RTA_DATA(rta);
-       p = kmalloc(sizeof(*p), GFP_KERNEL);
+       p = kmemdup(uaddrp, sizeof(*p), GFP_KERNEL);
        if (!p)
                return -ENOMEM;
 
-       memcpy(p, uaddrp, sizeof(*p));
        *addrpp = p;
        return 0;
 }
@@ -323,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;
 }
 
@@ -436,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;
 
@@ -457,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);
@@ -495,6 +496,7 @@ static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
                        goto out;
                }
 
+               err = -ESRCH;
                x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
                                             p->family);
        }
@@ -505,14 +507,15 @@ static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
        return x;
 }
 
-static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+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_user_state_lookup(p, (struct rtattr **)xfrma, &err);
+       x = xfrm_user_state_lookup(p, xfrma, &err);
        if (x == NULL)
                return err;
 
@@ -525,6 +528,7 @@ static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
        }
 
        err = xfrm_state_delete(x);
+
        if (err < 0)
                goto out;
 
@@ -534,6 +538,8 @@ static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
        km_state_notify(x, &c);
 
 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;
 }
@@ -545,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;
@@ -570,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;
@@ -615,14 +621,14 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
        if (x->lastused)
                RTA_PUT(skb, XFRMA_LASTUSED, sizeof(x->lastused), &x->lastused);
 
-       nlh->nlmsg_len = skb->tail - b;
+       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;
 }
 
@@ -652,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;
@@ -667,14 +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 = -ESRCH;
 
-       x = xfrm_user_state_lookup(p, (struct rtattr **)xfrma, &err);
+       x = xfrm_user_state_lookup(p, xfrma, &err);
        if (x == NULL)
                goto out_noput;
 
@@ -705,7 +818,7 @@ static int verify_userspi_info(struct xfrm_userspi_info *p)
 
        default:
                return -EINVAL;
-       };
+       }
 
        if (p->min > p->max)
                return -EINVAL;
@@ -713,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;
@@ -772,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:
@@ -782,12 +896,12 @@ static int verify_policy_dir(__u8 dir)
 
        default:
                return -EINVAL;
-       };
+       }
 
        return 0;
 }
 
-static int verify_policy_type(__u8 type)
+static int verify_policy_type(u8 type)
 {
        switch (type) {
        case XFRM_POLICY_TYPE_MAIN:
@@ -798,7 +912,7 @@ static int verify_policy_type(__u8 type)
 
        default:
                return -EINVAL;
-       };
+       }
 
        return 0;
 }
@@ -814,7 +928,7 @@ static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
 
        default:
                return -EINVAL;
-       };
+       }
 
        switch (p->action) {
        case XFRM_POLICY_ALLOW:
@@ -823,7 +937,7 @@ static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
 
        default:
                return -EINVAL;
-       };
+       }
 
        switch (p->sel.family) {
        case AF_INET:
@@ -838,7 +952,7 @@ static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
 
        default:
                return -EINVAL;
-       };
+       }
 
        return verify_policy_dir(p->dir);
 }
@@ -874,22 +988,57 @@ 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);
        }
@@ -900,7 +1049,7 @@ 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;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err;
 
        if (rt) {
@@ -973,7 +1122,8 @@ static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p,
        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;
@@ -984,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;
 
@@ -998,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);
@@ -1027,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;
@@ -1059,7 +1212,7 @@ static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
        uctx->ctx_alg = s->ctx_alg;
        uctx->ctx_len = s->ctx_len;
        memcpy(uctx + 1, s->ctx_str, s->ctx_len);
-       return 0;
+       return 0;
 
  rtattr_failure:
        return -1;
@@ -1082,12 +1235,12 @@ static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *s
 }
 
 #ifdef CONFIG_XFRM_SUB_POLICY
-static int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb)
+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 = xp->type;
+       upt.type = type;
 
        RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
 
@@ -1098,7 +1251,7 @@ rtattr_failure:
 }
 
 #else
-static inline int copy_to_user_policy_type(struct xfrm_policy *xp, struct sk_buff *skb)
+static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
 {
        return 0;
 }
@@ -1111,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;
@@ -1127,16 +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, skb) < 0)
+       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;
 }
 
@@ -1170,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;
@@ -1185,11 +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;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err;
        struct km_event c;
        int delete;
@@ -1197,7 +1350,7 @@ 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, (struct rtattr **)xfrma);
+       err = copy_from_user_policy_type(&type, xfrma);
        if (err)
                return err;
 
@@ -1206,13 +1359,12 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
                return err;
 
        if (p->index)
-               xp = xfrm_policy_byid(type, 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;
 
@@ -1223,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(type, 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)
@@ -1241,8 +1394,12 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
                                              MSG_DONTWAIT);
                }
        } else {
-               if ((err = security_xfrm_policy_delete(xp)) != 0)
+               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;
@@ -1250,18 +1407,24 @@ static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfr
                km_policy_notify(xp, p->dir, &c);
        }
 
-       xfrm_pol_put(xp);
-
 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;
@@ -1277,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);
@@ -1307,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;
@@ -1341,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;
        }
 
@@ -1364,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;
@@ -1388,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;
@@ -1404,17 +1571,23 @@ 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;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err;
+       struct xfrm_audit audit_info;
 
-       err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
+       err = copy_from_user_policy_type(&type, xfrma);
        if (err)
                return err;
 
-       xfrm_policy_flush(type);
+       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;
@@ -1423,26 +1596,26 @@ static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh, void **x
        return 0;
 }
 
-static int xfrm_add_pol_expire(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;
+       u8 type = XFRM_POLICY_TYPE_MAIN;
        int err = -ENOENT;
 
-       err = copy_from_user_policy_type(&type, (struct rtattr **)xfrma);
+       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);
+               xp = xfrm_policy_byid(type, p->dir, p->index, 0, &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;
 
@@ -1453,13 +1626,14 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void *
                        if ((err = security_xfrm_policy_alloc(&tmp, uctx)))
                                return err;
                }
-               xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security, 0);
+               xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, tmp.security,
+                                          0, &err);
                security_xfrm_policy_free(&tmp);
        }
 
        if (xp == NULL)
-               return err;
-                                                                                       read_lock(&xp->lock);
+               return -ENOENT;
+       read_lock(&xp->lock);
        if (xp->dead) {
                read_unlock(&xp->lock);
                goto out;
@@ -1469,6 +1643,9 @@ static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void *
        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");
@@ -1480,7 +1657,8 @@ out:
        return err;
 }
 
-static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **xfrma)
+static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_state *x;
        int err;
@@ -1488,27 +1666,31 @@ static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh, void **
        struct xfrm_usersa_info *p = &ue->state;
 
        x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
-               err = -ENOENT;
 
+       err = -ENOENT;
        if (x == NULL)
                return err;
 
-       err = -EINVAL;
-
        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)
+       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, void **xfrma)
+static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
+               struct rtattr **xfrma)
 {
        struct xfrm_policy *xp;
        struct xfrm_user_tmpl *ut;
@@ -1530,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;
        }
@@ -1560,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))
 
@@ -1581,12 +1934,15 @@ static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
        [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        },
@@ -1607,55 +1963,41 @@ static struct xfrm_link {
        [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, CAP_NET_ADMIN)) {
-               *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);
@@ -1665,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);
@@ -1673,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)
@@ -1699,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));
@@ -1709,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;
 }
 
@@ -1756,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);
@@ -1781,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)
@@ -1802,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;
 
@@ -1886,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,
@@ -1907,14 +2244,14 @@ static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
                goto nlmsg_failure;
        if (copy_to_user_state_sec_ctx(x, skb))
                goto nlmsg_failure;
-       if (copy_to_user_policy_type(xp, skb) < 0)
+       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;
 }
 
@@ -1926,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;
@@ -1976,7 +2316,7 @@ static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, 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)
@@ -1992,15 +2332,6 @@ static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
        xp->type = XFRM_POLICY_TYPE_MAIN;
        copy_templates(xp, ut, nr);
 
-       if (!xp->security) {
-               int err = security_xfrm_sock_policy_alloc(xp, sk);
-               if (err) {
-                       kfree(xp);
-                       *dir = err;
-                       return NULL;
-               }
-       }
-
        *dir = p->dir;
 
        return xp;
@@ -2012,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);
@@ -2023,15 +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, skb) < 0)
+       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;
 }
 
@@ -2042,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;
@@ -2060,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;
 
@@ -2069,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);
@@ -2096,7 +2433,7 @@ 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, skb) < 0)
+       if (copy_to_user_policy_type(xp->type, skb) < 0)
                goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
@@ -2114,11 +2451,12 @@ static int xfrm_notify_policy_flush(struct km_event *c)
 {
        struct nlmsghdr *nlh;
        struct sk_buff *skb;
-       unsigned char *b;
+       sk_buff_data_t b;
+       int len = 0;
 #ifdef CONFIG_XFRM_SUB_POLICY
-       struct xfrm_userpolicy_type upt;
+       len += RTA_SPACE(sizeof(struct xfrm_userpolicy_type));
 #endif
-       int len = NLMSG_LENGTH(0);
+       len += NLMSG_LENGTH(0);
 
        skb = alloc_skb(len, GFP_ATOMIC);
        if (skb == NULL)
@@ -2128,12 +2466,8 @@ 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;
-
-#ifdef CONFIG_XFRM_SUB_POLICY
-       memset(&upt, 0, sizeof(upt));
-       upt.type = c->data.type;
-       RTA_PUT(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
-#endif
+       if (copy_to_user_policy_type(c->data.type, skb) < 0)
+               goto nlmsg_failure;
 
        nlh->nlmsg_len = skb->tail - b;
 
@@ -2141,9 +2475,6 @@ static int xfrm_notify_policy_flush(struct km_event *c)
        return netlink_broadcast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
 
 nlmsg_failure:
-#ifdef CONFIG_XFRM_SUB_POLICY
-rtattr_failure:
-#endif
        kfree_skb(skb);
        return -1;
 }
@@ -2173,7 +2504,7 @@ static int build_report(struct sk_buff *skb, u8 proto,
 {
        struct xfrm_user_report *ur;
        struct nlmsghdr *nlh;
-       unsigned char *b = skb->tail;
+       unsigned char *b = skb_tail_pointer(skb);
 
        nlh = NLMSG_PUT(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur));
        ur = NLMSG_DATA(nlh);
@@ -2185,12 +2516,12 @@ static int build_report(struct sk_buff *skb, u8 proto,
        if (addr)
                RTA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
 
-       nlh->nlmsg_len = skb->tail - b;
+       nlh->nlmsg_len = skb_tail_pointer(skb) - b;
        return skb->len;
 
 nlmsg_failure:
 rtattr_failure:
-       skb_trim(skb, b - skb->data);
+       nlmsg_trim(skb, b);
        return -1;
 }
 
@@ -2219,6 +2550,7 @@ static struct xfrm_mgr netlink_mgr = {
        .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)
@@ -2228,7 +2560,7 @@ static int __init xfrm_user_init(void)
        printk(KERN_INFO "Initializing XFRM netlink socket\n");
 
        nlsk = netlink_kernel_create(NETLINK_XFRM, XFRMNLGRP_MAX,
-                                    xfrm_netlink_rcv, THIS_MODULE);
+                                    xfrm_netlink_rcv, NULL, THIS_MODULE);
        if (nlsk == NULL)
                return -ENOMEM;
        rcu_assign_pointer(xfrm_nl, nlsk);