blob: 810be7a9e3c4ff15b3c2dec5b3962fe9687d2c4f [file] [log] [blame]
Steffen Klasserta38f7902011-09-27 07:23:50 +02001/*
2 * Crypto user configuration API.
3 *
4 * Copyright (C) 2011 secunet Security Networks AG
5 * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com>
6 *
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.
10 *
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.
15 *
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.
19 */
20
21#include <linux/module.h>
22#include <linux/crypto.h>
23#include <linux/cryptouser.h>
Steffen Klassert1e122992012-03-29 09:03:47 +020024#include <linux/sched.h>
Steffen Klasserta38f7902011-09-27 07:23:50 +020025#include <net/netlink.h>
26#include <linux/security.h>
27#include <net/net_namespace.h>
Steffen Klassert1e122992012-03-29 09:03:47 +020028#include <crypto/internal/skcipher.h>
Herbert Xu9aa867e2015-06-21 19:11:45 +080029#include <crypto/internal/rng.h>
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070030#include <crypto/akcipher.h>
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010031#include <crypto/kpp.h>
Steffen Klassert1e122992012-03-29 09:03:47 +020032
Steffen Klasserta38f7902011-09-27 07:23:50 +020033#include "internal.h"
34
Mathias Krause8fd61d32013-02-05 18:19:15 +010035#define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
36
Jussi Kivilinna66ce0b02012-08-28 16:46:54 +030037static DEFINE_MUTEX(crypto_cfg_mutex);
Steffen Klasserta38f7902011-09-27 07:23:50 +020038
39/* The crypto netlink socket */
40static struct sock *crypto_nlsk;
41
42struct crypto_dump_info {
43 struct sk_buff *in_skb;
44 struct sk_buff *out_skb;
45 u32 nlmsg_seq;
46 u16 nlmsg_flags;
47};
48
49static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
50{
Steffen Klasserta38f7902011-09-27 07:23:50 +020051 struct crypto_alg *q, *alg = NULL;
52
53 down_read(&crypto_alg_sem);
54
Steffen Klasserta38f7902011-09-27 07:23:50 +020055 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xue6ea64e2011-10-21 14:37:10 +020056 int match = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +020057
Eric Biggerscd6f2062019-07-02 14:17:00 -070058 if (crypto_is_larval(q))
59 continue;
60
Steffen Klasserta38f7902011-09-27 07:23:50 +020061 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
62 continue;
63
64 if (strlen(p->cru_driver_name))
65 match = !strcmp(q->cra_driver_name,
66 p->cru_driver_name);
67 else if (!exact)
68 match = !strcmp(q->cra_name, p->cru_name);
69
Herbert Xu016baaa2015-04-07 21:27:01 +080070 if (!match)
71 continue;
72
73 if (unlikely(!crypto_mod_get(q)))
74 continue;
75
76 alg = q;
77 break;
Steffen Klasserta38f7902011-09-27 07:23:50 +020078 }
79
80 up_read(&crypto_alg_sem);
81
82 return alg;
83}
84
Steffen Klassert07a5fa42011-09-27 07:48:01 +020085static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
86{
87 struct crypto_report_cipher rcipher;
88
Mathias Krause9a5467b2013-02-05 18:19:13 +010089 strncpy(rcipher.type, "cipher", sizeof(rcipher.type));
Steffen Klassert07a5fa42011-09-27 07:48:01 +020090
91 rcipher.blocksize = alg->cra_blocksize;
92 rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
93 rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
94
David S. Miller6662df32012-04-01 20:19:05 -040095 if (nla_put(skb, CRYPTOCFGA_REPORT_CIPHER,
96 sizeof(struct crypto_report_cipher), &rcipher))
97 goto nla_put_failure;
Steffen Klassert07a5fa42011-09-27 07:48:01 +020098 return 0;
99
100nla_put_failure:
101 return -EMSGSIZE;
102}
103
Steffen Klassert540b97c2011-09-27 07:48:48 +0200104static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
105{
106 struct crypto_report_comp rcomp;
107
Mathias Krause9a5467b2013-02-05 18:19:13 +0100108 strncpy(rcomp.type, "compression", sizeof(rcomp.type));
David S. Miller6662df32012-04-01 20:19:05 -0400109 if (nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
110 sizeof(struct crypto_report_comp), &rcomp))
111 goto nla_put_failure;
Steffen Klassert540b97c2011-09-27 07:48:48 +0200112 return 0;
113
114nla_put_failure:
115 return -EMSGSIZE;
116}
117
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700118static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
119{
120 struct crypto_report_akcipher rakcipher;
121
122 strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
123
124 if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
125 sizeof(struct crypto_report_akcipher), &rakcipher))
126 goto nla_put_failure;
127 return 0;
128
129nla_put_failure:
130 return -EMSGSIZE;
131}
132
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +0100133static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
134{
135 struct crypto_report_kpp rkpp;
136
137 strncpy(rkpp.type, "kpp", sizeof(rkpp.type));
138
139 if (nla_put(skb, CRYPTOCFGA_REPORT_KPP,
140 sizeof(struct crypto_report_kpp), &rkpp))
141 goto nla_put_failure;
142 return 0;
143
144nla_put_failure:
145 return -EMSGSIZE;
146}
147
Steffen Klasserta38f7902011-09-27 07:23:50 +0200148static int crypto_report_one(struct crypto_alg *alg,
149 struct crypto_user_alg *ualg, struct sk_buff *skb)
150{
Mathias Krause9a5467b2013-02-05 18:19:13 +0100151 strncpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
152 strncpy(ualg->cru_driver_name, alg->cra_driver_name,
153 sizeof(ualg->cru_driver_name));
154 strncpy(ualg->cru_module_name, module_name(alg->cra_module),
155 sizeof(ualg->cru_module_name));
Steffen Klasserta38f7902011-09-27 07:23:50 +0200156
Mathias Krause9a5467b2013-02-05 18:19:13 +0100157 ualg->cru_type = 0;
158 ualg->cru_mask = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200159 ualg->cru_flags = alg->cra_flags;
160 ualg->cru_refcnt = atomic_read(&alg->cra_refcnt);
161
David S. Miller6662df32012-04-01 20:19:05 -0400162 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
163 goto nla_put_failure;
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200164 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
165 struct crypto_report_larval rl;
166
Mathias Krause9a5467b2013-02-05 18:19:13 +0100167 strncpy(rl.type, "larval", sizeof(rl.type));
David S. Miller6662df32012-04-01 20:19:05 -0400168 if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL,
169 sizeof(struct crypto_report_larval), &rl))
170 goto nla_put_failure;
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200171 goto out;
172 }
173
Steffen Klassertb6aa63c2011-09-27 07:24:29 +0200174 if (alg->cra_type && alg->cra_type->report) {
175 if (alg->cra_type->report(skb, alg))
176 goto nla_put_failure;
Steffen Klassert07a5fa42011-09-27 07:48:01 +0200177
178 goto out;
179 }
180
181 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
182 case CRYPTO_ALG_TYPE_CIPHER:
183 if (crypto_report_cipher(skb, alg))
184 goto nla_put_failure;
185
186 break;
Steffen Klassert540b97c2011-09-27 07:48:48 +0200187 case CRYPTO_ALG_TYPE_COMPRESS:
188 if (crypto_report_comp(skb, alg))
189 goto nla_put_failure;
190
191 break;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700192
193 case CRYPTO_ALG_TYPE_AKCIPHER:
194 if (crypto_report_akcipher(skb, alg))
195 goto nla_put_failure;
196
197 break;
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +0100198 case CRYPTO_ALG_TYPE_KPP:
199 if (crypto_report_kpp(skb, alg))
200 goto nla_put_failure;
201 break;
Steffen Klassertb6aa63c2011-09-27 07:24:29 +0200202 }
203
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200204out:
Steffen Klasserta38f7902011-09-27 07:23:50 +0200205 return 0;
206
207nla_put_failure:
208 return -EMSGSIZE;
209}
210
211static int crypto_report_alg(struct crypto_alg *alg,
212 struct crypto_dump_info *info)
213{
214 struct sk_buff *in_skb = info->in_skb;
215 struct sk_buff *skb = info->out_skb;
216 struct nlmsghdr *nlh;
217 struct crypto_user_alg *ualg;
218 int err = 0;
219
Eric W. Biederman15e47302012-09-07 20:12:54 +0000220 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
Steffen Klasserta38f7902011-09-27 07:23:50 +0200221 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
222 if (!nlh) {
223 err = -EMSGSIZE;
224 goto out;
225 }
226
227 ualg = nlmsg_data(nlh);
228
229 err = crypto_report_one(alg, ualg, skb);
230 if (err) {
231 nlmsg_cancel(skb, nlh);
232 goto out;
233 }
234
235 nlmsg_end(skb, nlh);
236
237out:
238 return err;
239}
240
241static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
242 struct nlattr **attrs)
243{
244 struct crypto_user_alg *p = nlmsg_data(in_nlh);
245 struct crypto_alg *alg;
246 struct sk_buff *skb;
247 struct crypto_dump_info info;
248 int err;
249
Mathias Krause8fd61d32013-02-05 18:19:15 +0100250 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
251 return -EINVAL;
252
Herbert Xu5d4a5e72014-11-20 12:44:32 +0800253 alg = crypto_alg_match(p, 0);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200254 if (!alg)
255 return -ENOENT;
256
Herbert Xu016baaa2015-04-07 21:27:01 +0800257 err = -ENOMEM;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200258 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
259 if (!skb)
Herbert Xu016baaa2015-04-07 21:27:01 +0800260 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200261
262 info.in_skb = in_skb;
263 info.out_skb = skb;
264 info.nlmsg_seq = in_nlh->nlmsg_seq;
265 info.nlmsg_flags = 0;
266
267 err = crypto_report_alg(alg, &info);
Herbert Xu016baaa2015-04-07 21:27:01 +0800268
269drop_alg:
270 crypto_mod_put(alg);
271
Navid Emamdoostf427e1f2019-10-04 14:29:16 -0500272 if (err) {
273 kfree_skb(skb);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200274 return err;
Navid Emamdoostf427e1f2019-10-04 14:29:16 -0500275 }
Steffen Klasserta38f7902011-09-27 07:23:50 +0200276
Eric W. Biederman15e47302012-09-07 20:12:54 +0000277 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200278}
279
280static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
281{
Eric Biggers5de3963e2018-12-06 15:55:41 -0800282 const size_t start_pos = cb->args[0];
283 size_t pos = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200284 struct crypto_dump_info info;
Eric Biggers5de3963e2018-12-06 15:55:41 -0800285 struct crypto_alg *alg;
286 int res;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200287
288 info.in_skb = cb->skb;
289 info.out_skb = skb;
290 info.nlmsg_seq = cb->nlh->nlmsg_seq;
291 info.nlmsg_flags = NLM_F_MULTI;
292
Eric Biggers5de3963e2018-12-06 15:55:41 -0800293 down_read(&crypto_alg_sem);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200294 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
Eric Biggers5de3963e2018-12-06 15:55:41 -0800295 if (pos >= start_pos) {
296 res = crypto_report_alg(alg, &info);
297 if (res == -EMSGSIZE)
298 break;
299 if (res)
300 goto out;
301 }
302 pos++;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200303 }
Eric Biggers5de3963e2018-12-06 15:55:41 -0800304 cb->args[0] = pos;
305 res = skb->len;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200306out:
Eric Biggers5de3963e2018-12-06 15:55:41 -0800307 up_read(&crypto_alg_sem);
308 return res;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200309}
310
311static int crypto_dump_report_done(struct netlink_callback *cb)
312{
313 return 0;
314}
315
316static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
317 struct nlattr **attrs)
318{
319 struct crypto_alg *alg;
320 struct crypto_user_alg *p = nlmsg_data(nlh);
321 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
322 LIST_HEAD(list);
323
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700324 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800325 return -EPERM;
326
Mathias Krause8fd61d32013-02-05 18:19:15 +0100327 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
328 return -EINVAL;
329
Steffen Klasserta38f7902011-09-27 07:23:50 +0200330 if (priority && !strlen(p->cru_driver_name))
331 return -EINVAL;
332
333 alg = crypto_alg_match(p, 1);
334 if (!alg)
335 return -ENOENT;
336
337 down_write(&crypto_alg_sem);
338
339 crypto_remove_spawns(alg, &list, NULL);
340
341 if (priority)
342 alg->cra_priority = nla_get_u32(priority);
343
344 up_write(&crypto_alg_sem);
345
Herbert Xu016baaa2015-04-07 21:27:01 +0800346 crypto_mod_put(alg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200347 crypto_remove_final(&list);
348
349 return 0;
350}
351
352static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
353 struct nlattr **attrs)
354{
355 struct crypto_alg *alg;
356 struct crypto_user_alg *p = nlmsg_data(nlh);
Herbert Xu016baaa2015-04-07 21:27:01 +0800357 int err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200358
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700359 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800360 return -EPERM;
361
Mathias Krause8fd61d32013-02-05 18:19:15 +0100362 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
363 return -EINVAL;
364
Steffen Klasserta38f7902011-09-27 07:23:50 +0200365 alg = crypto_alg_match(p, 1);
366 if (!alg)
367 return -ENOENT;
368
369 /* We can not unregister core algorithms such as aes-generic.
370 * We would loose the reference in the crypto_alg_list to this algorithm
371 * if we try to unregister. Unregistering such an algorithm without
372 * removing the module is not possible, so we restrict to crypto
373 * instances that are build from templates. */
Herbert Xu016baaa2015-04-07 21:27:01 +0800374 err = -EINVAL;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200375 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
Herbert Xu016baaa2015-04-07 21:27:01 +0800376 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200377
Herbert Xu016baaa2015-04-07 21:27:01 +0800378 err = -EBUSY;
379 if (atomic_read(&alg->cra_refcnt) > 2)
380 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200381
Herbert Xu016baaa2015-04-07 21:27:01 +0800382 err = crypto_unregister_instance((struct crypto_instance *)alg);
383
384drop_alg:
385 crypto_mod_put(alg);
386 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200387}
388
389static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
390 struct nlattr **attrs)
391{
Jesper Juhl0cfdec72012-01-29 23:39:22 +0100392 int exact = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200393 const char *name;
394 struct crypto_alg *alg;
395 struct crypto_user_alg *p = nlmsg_data(nlh);
396 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
397
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700398 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800399 return -EPERM;
400
Mathias Krause8fd61d32013-02-05 18:19:15 +0100401 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
402 return -EINVAL;
403
Steffen Klasserta38f7902011-09-27 07:23:50 +0200404 if (strlen(p->cru_driver_name))
405 exact = 1;
406
407 if (priority && !exact)
408 return -EINVAL;
409
410 alg = crypto_alg_match(p, exact);
Herbert Xu016baaa2015-04-07 21:27:01 +0800411 if (alg) {
412 crypto_mod_put(alg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200413 return -EEXIST;
Herbert Xu016baaa2015-04-07 21:27:01 +0800414 }
Steffen Klasserta38f7902011-09-27 07:23:50 +0200415
416 if (strlen(p->cru_driver_name))
417 name = p->cru_driver_name;
418 else
419 name = p->cru_name;
420
Herbert Xu6cf80a22016-07-12 13:17:49 +0800421 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200422 if (IS_ERR(alg))
423 return PTR_ERR(alg);
424
425 down_write(&crypto_alg_sem);
426
427 if (priority)
428 alg->cra_priority = nla_get_u32(priority);
429
430 up_write(&crypto_alg_sem);
431
432 crypto_mod_put(alg);
433
434 return 0;
435}
436
Herbert Xu9aa867e2015-06-21 19:11:45 +0800437static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
438 struct nlattr **attrs)
439{
440 if (!netlink_capable(skb, CAP_NET_ADMIN))
441 return -EPERM;
442 return crypto_del_default_rng();
443}
444
Steffen Klasserta38f7902011-09-27 07:23:50 +0200445#define MSGSIZE(type) sizeof(struct type)
446
447static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
448 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
449 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
450 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Mathias Krause055ddaa2016-06-22 20:29:37 +0200451 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Herbert Xu9aa867e2015-06-21 19:11:45 +0800452 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
Steffen Klasserta38f7902011-09-27 07:23:50 +0200453};
454
455static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
456 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
457};
458
459#undef MSGSIZE
460
Mathias Krausea84fb792013-02-24 14:09:12 +0100461static const struct crypto_link {
Steffen Klasserta38f7902011-09-27 07:23:50 +0200462 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
463 int (*dump)(struct sk_buff *, struct netlink_callback *);
464 int (*done)(struct netlink_callback *);
465} crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
466 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
467 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
468 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
469 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
470 .dump = crypto_dump_report,
471 .done = crypto_dump_report_done},
Herbert Xu9aa867e2015-06-21 19:11:45 +0800472 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
Steffen Klasserta38f7902011-09-27 07:23:50 +0200473};
474
475static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
476{
477 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
Mathias Krausea84fb792013-02-24 14:09:12 +0100478 const struct crypto_link *link;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200479 int type, err;
480
481 type = nlh->nlmsg_type;
482 if (type > CRYPTO_MSG_MAX)
483 return -EINVAL;
484
485 type -= CRYPTO_MSG_BASE;
486 link = &crypto_dispatch[type];
487
Steffen Klasserta38f7902011-09-27 07:23:50 +0200488 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
489 (nlh->nlmsg_flags & NLM_F_DUMP))) {
Steffen Klassert5219a532012-03-29 09:04:46 +0200490 struct crypto_alg *alg;
Eric Biggers5de3963e2018-12-06 15:55:41 -0800491 unsigned long dump_alloc = 0;
Steffen Klassert5219a532012-03-29 09:04:46 +0200492
Steffen Klasserta38f7902011-09-27 07:23:50 +0200493 if (link->dump == NULL)
494 return -EINVAL;
Steffen Klassert5219a532012-03-29 09:04:46 +0200495
Mathias Krause63e41eb2016-02-01 14:27:30 +0100496 down_read(&crypto_alg_sem);
Steffen Klassert5219a532012-03-29 09:04:46 +0200497 list_for_each_entry(alg, &crypto_alg_list, cra_list)
498 dump_alloc += CRYPTO_REPORT_MAXSIZE;
Eric Biggers5de3963e2018-12-06 15:55:41 -0800499 up_read(&crypto_alg_sem);
Steffen Klassert5219a532012-03-29 09:04:46 +0200500
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000501 {
502 struct netlink_dump_control c = {
503 .dump = link->dump,
504 .done = link->done,
Eric Biggers5de3963e2018-12-06 15:55:41 -0800505 .min_dump_alloc = min(dump_alloc, 65535UL),
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000506 };
Mathias Krause63e41eb2016-02-01 14:27:30 +0100507 err = netlink_dump_start(crypto_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000508 }
Mathias Krause63e41eb2016-02-01 14:27:30 +0100509
510 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200511 }
512
Herbert Xufd2efd92016-06-23 18:06:02 +0800513 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
514 crypto_policy);
515 if (err < 0)
516 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200517
518 if (link->doit == NULL)
519 return -EINVAL;
520
521 return link->doit(skb, nlh, attrs);
522}
523
524static void crypto_netlink_rcv(struct sk_buff *skb)
525{
526 mutex_lock(&crypto_cfg_mutex);
527 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
528 mutex_unlock(&crypto_cfg_mutex);
529}
530
531static int __init crypto_user_init(void)
532{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000533 struct netlink_kernel_cfg cfg = {
534 .input = crypto_netlink_rcv,
535 };
536
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000537 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, &cfg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200538 if (!crypto_nlsk)
539 return -ENOMEM;
540
541 return 0;
542}
543
544static void __exit crypto_user_exit(void)
545{
546 netlink_kernel_release(crypto_nlsk);
547}
548
549module_init(crypto_user_init);
550module_exit(crypto_user_exit);
551MODULE_LICENSE("GPL");
552MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
553MODULE_DESCRIPTION("Crypto userspace configuration API");
Stephan Mueller476c7fe2014-11-24 17:12:45 +0100554MODULE_ALIAS("net-pf-16-proto-21");