Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/davem/net-2.6
[safe/jmp/linux-2.6] / net / xfrm / xfrm_user.c
index d41588d..5578c90 100644 (file)
@@ -31,7 +31,7 @@
 #include <linux/in6.h>
 #endif
 
-static inline int alg_len(struct xfrm_algo *alg)
+static inline int aead_len(struct xfrm_algo_aead *alg)
 {
        return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
 }
@@ -45,7 +45,7 @@ static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
                return 0;
 
        algp = nla_data(rt);
-       if (nla_len(rt) < alg_len(algp))
+       if (nla_len(rt) < xfrm_alg_len(algp))
                return -EINVAL;
 
        switch (type) {
@@ -73,6 +73,22 @@ static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
        return 0;
 }
 
+static int verify_aead(struct nlattr **attrs)
+{
+       struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
+       struct xfrm_algo_aead *algp;
+
+       if (!rt)
+               return 0;
+
+       algp = nla_data(rt);
+       if (nla_len(rt) < aead_len(algp))
+               return -EINVAL;
+
+       algp->alg_name[CRYPTO_MAX_ALG_NAME - 1] = '\0';
+       return 0;
+}
+
 static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
                           xfrm_address_t **addrp)
 {
@@ -124,20 +140,28 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
        switch (p->id.proto) {
        case IPPROTO_AH:
                if (!attrs[XFRMA_ALG_AUTH]      ||
+                   attrs[XFRMA_ALG_AEAD]       ||
                    attrs[XFRMA_ALG_CRYPT]      ||
                    attrs[XFRMA_ALG_COMP])
                        goto out;
                break;
 
        case IPPROTO_ESP:
-               if ((!attrs[XFRMA_ALG_AUTH] &&
-                    !attrs[XFRMA_ALG_CRYPT])   ||
-                   attrs[XFRMA_ALG_COMP])
+               if (attrs[XFRMA_ALG_COMP])
+                       goto out;
+               if (!attrs[XFRMA_ALG_AUTH] &&
+                   !attrs[XFRMA_ALG_CRYPT] &&
+                   !attrs[XFRMA_ALG_AEAD])
+                       goto out;
+               if ((attrs[XFRMA_ALG_AUTH] ||
+                    attrs[XFRMA_ALG_CRYPT]) &&
+                   attrs[XFRMA_ALG_AEAD])
                        goto out;
                break;
 
        case IPPROTO_COMP:
                if (!attrs[XFRMA_ALG_COMP]      ||
+                   attrs[XFRMA_ALG_AEAD]       ||
                    attrs[XFRMA_ALG_AUTH]       ||
                    attrs[XFRMA_ALG_CRYPT])
                        goto out;
@@ -148,6 +172,7 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
        case IPPROTO_ROUTING:
                if (attrs[XFRMA_ALG_COMP]       ||
                    attrs[XFRMA_ALG_AUTH]       ||
+                   attrs[XFRMA_ALG_AEAD]       ||
                    attrs[XFRMA_ALG_CRYPT]      ||
                    attrs[XFRMA_ENCAP]          ||
                    attrs[XFRMA_SEC_CTX]        ||
@@ -160,6 +185,8 @@ static int verify_newsa_info(struct xfrm_usersa_info *p,
                goto out;
        }
 
+       if ((err = verify_aead(attrs)))
+               goto out;
        if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
                goto out;
        if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
@@ -204,7 +231,32 @@ static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
                return -ENOSYS;
        *props = algo->desc.sadb_alg_id;
 
-       p = kmemdup(ualg, alg_len(ualg), GFP_KERNEL);
+       p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
+       if (!p)
+               return -ENOMEM;
+
+       strcpy(p->alg_name, algo->name);
+       *algpp = p;
+       return 0;
+}
+
+static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
+                      struct nlattr *rta)
+{
+       struct xfrm_algo_aead *p, *ualg;
+       struct xfrm_algo_desc *algo;
+
+       if (!rta)
+               return 0;
+
+       ualg = nla_data(rta);
+
+       algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
+       if (!algo)
+               return -ENOSYS;
+       *props = algo->desc.sadb_alg_id;
+
+       p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
        if (!p)
                return -ENOMEM;
 
@@ -236,12 +288,9 @@ static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *
        memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
        x->props.flags = p->flags;
 
-       /*
-        * Set inner address family if the KM left it as zero.
-        * See comment in validate_tmpl.
-        */
-       if (!x->sel.family)
+       if (x->props.mode == XFRM_MODE_TRANSPORT)
                x->sel.family = p->family;
+
 }
 
 /*
@@ -291,6 +340,9 @@ static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
 
        copy_from_user_state(x, p);
 
+       if ((err = attach_aead(&x->aead, &x->props.ealgo,
+                              attrs[XFRMA_ALG_AEAD])))
+               goto error;
        if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
                                   xfrm_aalg_get_byname,
                                   attrs[XFRMA_ALG_AUTH])))
@@ -477,8 +529,6 @@ struct xfrm_dump_info {
        struct sk_buff *out_skb;
        u32 nlmsg_seq;
        u16 nlmsg_flags;
-       int start_idx;
-       int this_idx;
 };
 
 static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
@@ -507,7 +557,6 @@ static int copy_to_user_state_extra(struct xfrm_state *x,
                                    struct xfrm_usersa_info *p,
                                    struct sk_buff *skb)
 {
-       spin_lock_bh(&x->lock);
        copy_to_user_state(x, p);
 
        if (x->coaddr)
@@ -515,12 +564,13 @@ static int copy_to_user_state_extra(struct xfrm_state *x,
 
        if (x->lastused)
                NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
-       spin_unlock_bh(&x->lock);
 
+       if (x->aead)
+               NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
        if (x->aalg)
-               NLA_PUT(skb, XFRMA_ALG_AUTH, alg_len(x->aalg), x->aalg);
+               NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
        if (x->ealg)
-               NLA_PUT(skb, XFRMA_ALG_CRYPT, alg_len(x->ealg), x->ealg);
+               NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
        if (x->calg)
                NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
 
@@ -545,9 +595,6 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
        struct nlmsghdr *nlh;
        int err;
 
-       if (sp->this_idx < sp->start_idx)
-               goto out;
-
        nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
                        XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
        if (nlh == NULL)
@@ -560,8 +607,6 @@ static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
                goto nla_put_failure;
 
        nlmsg_end(skb, nlh);
-out:
-       sp->this_idx++;
        return 0;
 
 nla_put_failure:
@@ -569,18 +614,32 @@ nla_put_failure:
        return err;
 }
 
+static int xfrm_dump_sa_done(struct netlink_callback *cb)
+{
+       struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
+       xfrm_state_walk_done(walk);
+       return 0;
+}
+
 static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
 {
+       struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
        struct xfrm_dump_info info;
 
+       BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
+                    sizeof(cb->args) - sizeof(cb->args[0]));
+
        info.in_skb = cb->skb;
        info.out_skb = skb;
        info.nlmsg_seq = cb->nlh->nlmsg_seq;
        info.nlmsg_flags = NLM_F_MULTI;
-       info.this_idx = 0;
-       info.start_idx = cb->args[0];
-       (void) xfrm_state_walk(0, dump_one_state, &info);
-       cb->args[0] = info.this_idx;
+
+       if (!cb->args[0]) {
+               cb->args[0] = 1;
+               xfrm_state_walk_init(walk, 0);
+       }
+
+       (void) xfrm_state_walk(walk, dump_one_state, &info);
 
        return skb->len;
 }
@@ -599,7 +658,6 @@ static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
        info.out_skb = skb;
        info.nlmsg_seq = seq;
        info.nlmsg_flags = 0;
-       info.this_idx = info.start_idx = 0;
 
        if (dump_one_state(x, 0, &info)) {
                kfree_skb(skb);
@@ -1050,7 +1108,8 @@ static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p,
        return xp;
  error:
        *errp = err;
-       kfree(xp);
+       xp->dead = 1;
+       xfrm_policy_destroy(xp);
        return NULL;
 }
 
@@ -1176,9 +1235,6 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
        struct sk_buff *skb = sp->out_skb;
        struct nlmsghdr *nlh;
 
-       if (sp->this_idx < sp->start_idx)
-               goto out;
-
        nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
                        XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
        if (nlh == NULL)
@@ -1194,8 +1250,6 @@ static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr
                goto nlmsg_failure;
 
        nlmsg_end(skb, nlh);
-out:
-       sp->this_idx++;
        return 0;
 
 nlmsg_failure:
@@ -1203,21 +1257,33 @@ nlmsg_failure:
        return -EMSGSIZE;
 }
 
+static int xfrm_dump_policy_done(struct netlink_callback *cb)
+{
+       struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
+
+       xfrm_policy_walk_done(walk);
+       return 0;
+}
+
 static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
 {
+       struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
        struct xfrm_dump_info info;
 
+       BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
+                    sizeof(cb->args) - sizeof(cb->args[0]));
+
        info.in_skb = cb->skb;
        info.out_skb = skb;
        info.nlmsg_seq = cb->nlh->nlmsg_seq;
        info.nlmsg_flags = NLM_F_MULTI;
-       info.this_idx = 0;
-       info.start_idx = cb->args[0];
-       (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;
+
+       if (!cb->args[0]) {
+               cb->args[0] = 1;
+               xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
+       }
+
+       (void) xfrm_policy_walk(walk, dump_one_policy, &info);
 
        return skb->len;
 }
@@ -1237,7 +1303,6 @@ static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
        info.out_skb = skb;
        info.nlmsg_seq = seq;
        info.nlmsg_flags = 0;
-       info.this_idx = info.start_idx = 0;
 
        if (dump_one_policy(xp, dir, 0, &info) < 0) {
                kfree_skb(skb);
@@ -1815,6 +1880,7 @@ static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
 #undef XMSGSIZE
 
 static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
+       [XFRMA_ALG_AEAD]        = { .len = sizeof(struct xfrm_algo_aead) },
        [XFRMA_ALG_AUTH]        = { .len = sizeof(struct xfrm_algo) },
        [XFRMA_ALG_CRYPT]       = { .len = sizeof(struct xfrm_algo) },
        [XFRMA_ALG_COMP]        = { .len = sizeof(struct xfrm_algo) },
@@ -1834,15 +1900,18 @@ static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
 static struct xfrm_link {
        int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
        int (*dump)(struct sk_buff *, struct netlink_callback *);
+       int (*done)(struct netlink_callback *);
 } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
        [XFRM_MSG_NEWSA       - XFRM_MSG_BASE] = { .doit = xfrm_add_sa        },
        [XFRM_MSG_DELSA       - XFRM_MSG_BASE] = { .doit = xfrm_del_sa        },
        [XFRM_MSG_GETSA       - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
-                                                  .dump = xfrm_dump_sa       },
+                                                  .dump = xfrm_dump_sa,
+                                                  .done = xfrm_dump_sa_done  },
        [XFRM_MSG_NEWPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_add_policy    },
        [XFRM_MSG_DELPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy    },
        [XFRM_MSG_GETPOLICY   - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
-                                                  .dump = xfrm_dump_policy   },
+                                                  .dump = xfrm_dump_policy,
+                                                  .done = xfrm_dump_policy_done },
        [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 },
@@ -1881,7 +1950,7 @@ static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
                if (link->dump == NULL)
                        return -EINVAL;
 
-               return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, NULL);
+               return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
        }
 
        err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
@@ -1979,10 +2048,12 @@ static int xfrm_notify_sa_flush(struct km_event *c)
 static inline size_t xfrm_sa_len(struct xfrm_state *x)
 {
        size_t l = 0;
+       if (x->aead)
+               l += nla_total_size(aead_len(x->aead));
        if (x->aalg)
-               l += nla_total_size(alg_len(x->aalg));
+               l += nla_total_size(xfrm_alg_len(x->aalg));
        if (x->ealg)
-               l += nla_total_size(alg_len(x->ealg));
+               l += nla_total_size(xfrm_alg_len(x->ealg));
        if (x->calg)
                l += nla_total_size(sizeof(*x->calg));
        if (x->encap)
@@ -1993,8 +2064,8 @@ static inline size_t xfrm_sa_len(struct xfrm_state *x)
        if (x->coaddr)
                l += nla_total_size(sizeof(*x->coaddr));
 
-       /* Must count this as this may become non-zero behind our back. */
-       l += nla_total_size(sizeof(x->lastused));
+       /* Must count x->lastused as it may become non-zero behind our back. */
+       l += nla_total_size(sizeof(u64));
 
        return l;
 }
@@ -2427,7 +2498,7 @@ static void __exit xfrm_user_exit(void)
        xfrm_unregister_km(&netlink_mgr);
        rcu_assign_pointer(xfrm_nl, NULL);
        synchronize_rcu();
-       sock_release(nlsk->sk_socket);
+       netlink_kernel_release(nlsk);
 }
 
 module_init(xfrm_user_init);