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
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>
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;
41 static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
44 struct crypto_alg *q, *alg = NULL;
46 down_read(&crypto_alg_sem);
48 if (list_empty(&crypto_alg_list))
51 list_for_each_entry(q, &crypto_alg_list, cra_list) {
53 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
56 if (strlen(p->cru_driver_name))
57 match = !strcmp(q->cra_driver_name,
60 match = !strcmp(q->cra_name, p->cru_name);
68 up_read(&crypto_alg_sem);
73 static int crypto_report_one(struct crypto_alg *alg,
74 struct crypto_user_alg *ualg, struct sk_buff *skb)
76 memcpy(&ualg->cru_name, &alg->cra_name, sizeof(ualg->cru_name));
77 memcpy(&ualg->cru_driver_name, &alg->cra_driver_name,
78 sizeof(ualg->cru_driver_name));
79 memcpy(&ualg->cru_module_name, module_name(alg->cra_module),
82 ualg->cru_flags = alg->cra_flags;
83 ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
85 NLA_PUT_U32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority);
87 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
88 struct crypto_report_larval rl;
90 snprintf(rl.type, CRYPTO_MAX_ALG_NAME, "%s", "larval");
92 NLA_PUT(skb, CRYPTOCFGA_REPORT_LARVAL,
93 sizeof(struct crypto_report_larval), &rl);
98 if (alg->cra_type && alg->cra_type->report) {
99 if (alg->cra_type->report(skb, alg))
100 goto nla_put_failure;
110 static int crypto_report_alg(struct crypto_alg *alg,
111 struct crypto_dump_info *info)
113 struct sk_buff *in_skb = info->in_skb;
114 struct sk_buff *skb = info->out_skb;
115 struct nlmsghdr *nlh;
116 struct crypto_user_alg *ualg;
119 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, info->nlmsg_seq,
120 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
126 ualg = nlmsg_data(nlh);
128 err = crypto_report_one(alg, ualg, skb);
130 nlmsg_cancel(skb, nlh);
140 static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
141 struct nlattr **attrs)
143 struct crypto_user_alg *p = nlmsg_data(in_nlh);
144 struct crypto_alg *alg;
146 struct crypto_dump_info info;
149 if (!p->cru_driver_name)
152 alg = crypto_alg_match(p, 1);
156 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
160 info.in_skb = in_skb;
162 info.nlmsg_seq = in_nlh->nlmsg_seq;
163 info.nlmsg_flags = 0;
165 err = crypto_report_alg(alg, &info);
169 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).pid);
172 static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
174 struct crypto_alg *alg;
175 struct crypto_dump_info info;
183 info.in_skb = cb->skb;
185 info.nlmsg_seq = cb->nlh->nlmsg_seq;
186 info.nlmsg_flags = NLM_F_MULTI;
188 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
189 err = crypto_report_alg(alg, &info);
200 static int crypto_dump_report_done(struct netlink_callback *cb)
205 static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
206 struct nlattr **attrs)
208 struct crypto_alg *alg;
209 struct crypto_user_alg *p = nlmsg_data(nlh);
210 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
213 if (priority && !strlen(p->cru_driver_name))
216 alg = crypto_alg_match(p, 1);
220 down_write(&crypto_alg_sem);
222 crypto_remove_spawns(alg, &list, NULL);
225 alg->cra_priority = nla_get_u32(priority);
227 up_write(&crypto_alg_sem);
229 crypto_remove_final(&list);
234 static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
235 struct nlattr **attrs)
237 struct crypto_alg *alg;
238 struct crypto_user_alg *p = nlmsg_data(nlh);
240 alg = crypto_alg_match(p, 1);
244 /* We can not unregister core algorithms such as aes-generic.
245 * We would loose the reference in the crypto_alg_list to this algorithm
246 * if we try to unregister. Unregistering such an algorithm without
247 * removing the module is not possible, so we restrict to crypto
248 * instances that are build from templates. */
249 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
252 if (atomic_read(&alg->cra_refcnt) != 1)
255 return crypto_unregister_alg(alg);
258 static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
259 struct nlattr **attrs)
263 struct crypto_alg *alg;
264 struct crypto_user_alg *p = nlmsg_data(nlh);
265 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
267 if (strlen(p->cru_driver_name))
270 if (priority && !exact)
273 alg = crypto_alg_match(p, exact);
277 if (strlen(p->cru_driver_name))
278 name = p->cru_driver_name;
282 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
286 down_write(&crypto_alg_sem);
289 alg->cra_priority = nla_get_u32(priority);
291 up_write(&crypto_alg_sem);
298 #define MSGSIZE(type) sizeof(struct type)
300 static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
301 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
302 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
303 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
304 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
307 static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
308 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
313 static struct crypto_link {
314 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
315 int (*dump)(struct sk_buff *, struct netlink_callback *);
316 int (*done)(struct netlink_callback *);
317 } crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
318 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
319 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
320 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
321 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
322 .dump = crypto_dump_report,
323 .done = crypto_dump_report_done},
326 static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
328 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
329 struct crypto_link *link;
332 type = nlh->nlmsg_type;
333 if (type > CRYPTO_MSG_MAX)
336 type -= CRYPTO_MSG_BASE;
337 link = &crypto_dispatch[type];
339 if (security_netlink_recv(skb, CAP_NET_ADMIN))
342 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
343 (nlh->nlmsg_flags & NLM_F_DUMP))) {
344 if (link->dump == NULL)
347 return netlink_dump_start(crypto_nlsk, skb, nlh,
348 link->dump, link->done, 0);
351 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
356 if (link->doit == NULL)
359 return link->doit(skb, nlh, attrs);
362 static void crypto_netlink_rcv(struct sk_buff *skb)
364 mutex_lock(&crypto_cfg_mutex);
365 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
366 mutex_unlock(&crypto_cfg_mutex);
369 static int __init crypto_user_init(void)
371 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO,
372 0, crypto_netlink_rcv,
380 static void __exit crypto_user_exit(void)
382 netlink_kernel_release(crypto_nlsk);
385 module_init(crypto_user_init);
386 module_exit(crypto_user_exit);
387 MODULE_LICENSE("GPL");
388 MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
389 MODULE_DESCRIPTION("Crypto userspace configuration API");