pch_uart: Fix dma channel unallocated issue
[zen-stable.git] / crypto / crypto_user.c
blob16f8693cc147ff9d8c864cc769b6632d8d105e45
1 /*
2 * Crypto user configuration API.
4 * Copyright (C) 2011 secunet Security Networks AG
5 * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <linux/module.h>
22 #include <linux/crypto.h>
23 #include <linux/cryptouser.h>
24 #include <net/netlink.h>
25 #include <linux/security.h>
26 #include <net/net_namespace.h>
27 #include "internal.h"
29 DEFINE_MUTEX(crypto_cfg_mutex);
31 /* The crypto netlink socket */
32 static struct sock *crypto_nlsk;
34 struct crypto_dump_info {
35 struct sk_buff *in_skb;
36 struct sk_buff *out_skb;
37 u32 nlmsg_seq;
38 u16 nlmsg_flags;
41 static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
43 struct crypto_alg *q, *alg = NULL;
45 down_read(&crypto_alg_sem);
47 list_for_each_entry(q, &crypto_alg_list, cra_list) {
48 int match = 0;
50 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
51 continue;
53 if (strlen(p->cru_driver_name))
54 match = !strcmp(q->cra_driver_name,
55 p->cru_driver_name);
56 else if (!exact)
57 match = !strcmp(q->cra_name, p->cru_name);
59 if (match) {
60 alg = q;
61 break;
65 up_read(&crypto_alg_sem);
67 return alg;
70 static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
72 struct crypto_report_cipher rcipher;
74 snprintf(rcipher.type, CRYPTO_MAX_ALG_NAME, "%s", "cipher");
76 rcipher.blocksize = alg->cra_blocksize;
77 rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
78 rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
80 NLA_PUT(skb, CRYPTOCFGA_REPORT_CIPHER,
81 sizeof(struct crypto_report_cipher), &rcipher);
83 return 0;
85 nla_put_failure:
86 return -EMSGSIZE;
89 static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
91 struct crypto_report_comp rcomp;
93 snprintf(rcomp.type, CRYPTO_MAX_ALG_NAME, "%s", "compression");
95 NLA_PUT(skb, CRYPTOCFGA_REPORT_COMPRESS,
96 sizeof(struct crypto_report_comp), &rcomp);
98 return 0;
100 nla_put_failure:
101 return -EMSGSIZE;
104 static int crypto_report_one(struct crypto_alg *alg,
105 struct crypto_user_alg *ualg, struct sk_buff *skb)
107 memcpy(&ualg->cru_name, &alg->cra_name, sizeof(ualg->cru_name));
108 memcpy(&ualg->cru_driver_name, &alg->cra_driver_name,
109 sizeof(ualg->cru_driver_name));
110 memcpy(&ualg->cru_module_name, module_name(alg->cra_module),
111 CRYPTO_MAX_ALG_NAME);
113 ualg->cru_flags = alg->cra_flags;
114 ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
116 NLA_PUT_U32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority);
118 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
119 struct crypto_report_larval rl;
121 snprintf(rl.type, CRYPTO_MAX_ALG_NAME, "%s", "larval");
123 NLA_PUT(skb, CRYPTOCFGA_REPORT_LARVAL,
124 sizeof(struct crypto_report_larval), &rl);
126 goto out;
129 if (alg->cra_type && alg->cra_type->report) {
130 if (alg->cra_type->report(skb, alg))
131 goto nla_put_failure;
133 goto out;
136 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
137 case CRYPTO_ALG_TYPE_CIPHER:
138 if (crypto_report_cipher(skb, alg))
139 goto nla_put_failure;
141 break;
142 case CRYPTO_ALG_TYPE_COMPRESS:
143 if (crypto_report_comp(skb, alg))
144 goto nla_put_failure;
146 break;
149 out:
150 return 0;
152 nla_put_failure:
153 return -EMSGSIZE;
156 static int crypto_report_alg(struct crypto_alg *alg,
157 struct crypto_dump_info *info)
159 struct sk_buff *in_skb = info->in_skb;
160 struct sk_buff *skb = info->out_skb;
161 struct nlmsghdr *nlh;
162 struct crypto_user_alg *ualg;
163 int err = 0;
165 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, info->nlmsg_seq,
166 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
167 if (!nlh) {
168 err = -EMSGSIZE;
169 goto out;
172 ualg = nlmsg_data(nlh);
174 err = crypto_report_one(alg, ualg, skb);
175 if (err) {
176 nlmsg_cancel(skb, nlh);
177 goto out;
180 nlmsg_end(skb, nlh);
182 out:
183 return err;
186 static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
187 struct nlattr **attrs)
189 struct crypto_user_alg *p = nlmsg_data(in_nlh);
190 struct crypto_alg *alg;
191 struct sk_buff *skb;
192 struct crypto_dump_info info;
193 int err;
195 if (!p->cru_driver_name)
196 return -EINVAL;
198 alg = crypto_alg_match(p, 1);
199 if (!alg)
200 return -ENOENT;
202 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
203 if (!skb)
204 return -ENOMEM;
206 info.in_skb = in_skb;
207 info.out_skb = skb;
208 info.nlmsg_seq = in_nlh->nlmsg_seq;
209 info.nlmsg_flags = 0;
211 err = crypto_report_alg(alg, &info);
212 if (err)
213 return err;
215 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).pid);
218 static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
220 struct crypto_alg *alg;
221 struct crypto_dump_info info;
222 int err;
224 if (cb->args[0])
225 goto out;
227 cb->args[0] = 1;
229 info.in_skb = cb->skb;
230 info.out_skb = skb;
231 info.nlmsg_seq = cb->nlh->nlmsg_seq;
232 info.nlmsg_flags = NLM_F_MULTI;
234 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
235 err = crypto_report_alg(alg, &info);
236 if (err)
237 goto out_err;
240 out:
241 return skb->len;
242 out_err:
243 return err;
246 static int crypto_dump_report_done(struct netlink_callback *cb)
248 return 0;
251 static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
252 struct nlattr **attrs)
254 struct crypto_alg *alg;
255 struct crypto_user_alg *p = nlmsg_data(nlh);
256 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
257 LIST_HEAD(list);
259 if (priority && !strlen(p->cru_driver_name))
260 return -EINVAL;
262 alg = crypto_alg_match(p, 1);
263 if (!alg)
264 return -ENOENT;
266 down_write(&crypto_alg_sem);
268 crypto_remove_spawns(alg, &list, NULL);
270 if (priority)
271 alg->cra_priority = nla_get_u32(priority);
273 up_write(&crypto_alg_sem);
275 crypto_remove_final(&list);
277 return 0;
280 static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
281 struct nlattr **attrs)
283 struct crypto_alg *alg;
284 struct crypto_user_alg *p = nlmsg_data(nlh);
286 alg = crypto_alg_match(p, 1);
287 if (!alg)
288 return -ENOENT;
290 /* We can not unregister core algorithms such as aes-generic.
291 * We would loose the reference in the crypto_alg_list to this algorithm
292 * if we try to unregister. Unregistering such an algorithm without
293 * removing the module is not possible, so we restrict to crypto
294 * instances that are build from templates. */
295 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
296 return -EINVAL;
298 if (atomic_read(&alg->cra_refcnt) != 1)
299 return -EBUSY;
301 return crypto_unregister_instance(alg);
304 static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
305 struct nlattr **attrs)
307 int exact;
308 const char *name;
309 struct crypto_alg *alg;
310 struct crypto_user_alg *p = nlmsg_data(nlh);
311 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
313 if (strlen(p->cru_driver_name))
314 exact = 1;
316 if (priority && !exact)
317 return -EINVAL;
319 alg = crypto_alg_match(p, exact);
320 if (alg)
321 return -EEXIST;
323 if (strlen(p->cru_driver_name))
324 name = p->cru_driver_name;
325 else
326 name = p->cru_name;
328 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
329 if (IS_ERR(alg))
330 return PTR_ERR(alg);
332 down_write(&crypto_alg_sem);
334 if (priority)
335 alg->cra_priority = nla_get_u32(priority);
337 up_write(&crypto_alg_sem);
339 crypto_mod_put(alg);
341 return 0;
344 #define MSGSIZE(type) sizeof(struct type)
346 static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
347 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
348 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
349 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
350 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
353 static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
354 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
357 #undef MSGSIZE
359 static struct crypto_link {
360 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
361 int (*dump)(struct sk_buff *, struct netlink_callback *);
362 int (*done)(struct netlink_callback *);
363 } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
364 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
365 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
366 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
367 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
368 .dump = crypto_dump_report,
369 .done = crypto_dump_report_done},
372 static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
374 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
375 struct crypto_link *link;
376 int type, err;
378 type = nlh->nlmsg_type;
379 if (type > CRYPTO_MSG_MAX)
380 return -EINVAL;
382 type -= CRYPTO_MSG_BASE;
383 link = &crypto_dispatch[type];
385 if (!capable(CAP_NET_ADMIN))
386 return -EPERM;
388 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
389 (nlh->nlmsg_flags & NLM_F_DUMP))) {
390 if (link->dump == NULL)
391 return -EINVAL;
393 return netlink_dump_start(crypto_nlsk, skb, nlh,
394 link->dump, link->done, 0);
397 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
398 crypto_policy);
399 if (err < 0)
400 return err;
402 if (link->doit == NULL)
403 return -EINVAL;
405 return link->doit(skb, nlh, attrs);
408 static void crypto_netlink_rcv(struct sk_buff *skb)
410 mutex_lock(&crypto_cfg_mutex);
411 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
412 mutex_unlock(&crypto_cfg_mutex);
415 static int __init crypto_user_init(void)
417 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO,
418 0, crypto_netlink_rcv,
419 NULL, THIS_MODULE);
420 if (!crypto_nlsk)
421 return -ENOMEM;
423 return 0;
426 static void __exit crypto_user_exit(void)
428 netlink_kernel_release(crypto_nlsk);
431 module_init(crypto_user_init);
432 module_exit(crypto_user_exit);
433 MODULE_LICENSE("GPL");
434 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
435 MODULE_DESCRIPTION("Crypto userspace configuration API");