blob: 60cf7d163731b965dc5ab572a5e64e45dd85e1dd [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
Steffen Klasserta38f7902011-09-27 07:23:50 +0200272 if (err)
273 return err;
274
Eric W. Biederman15e47302012-09-07 20:12:54 +0000275 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200276}
277
278static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
279{
Eric Biggers5de3963e2018-12-06 15:55:41 -0800280 const size_t start_pos = cb->args[0];
281 size_t pos = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200282 struct crypto_dump_info info;
Eric Biggers5de3963e2018-12-06 15:55:41 -0800283 struct crypto_alg *alg;
284 int res;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200285
286 info.in_skb = cb->skb;
287 info.out_skb = skb;
288 info.nlmsg_seq = cb->nlh->nlmsg_seq;
289 info.nlmsg_flags = NLM_F_MULTI;
290
Eric Biggers5de3963e2018-12-06 15:55:41 -0800291 down_read(&crypto_alg_sem);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200292 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
Eric Biggers5de3963e2018-12-06 15:55:41 -0800293 if (pos >= start_pos) {
294 res = crypto_report_alg(alg, &info);
295 if (res == -EMSGSIZE)
296 break;
297 if (res)
298 goto out;
299 }
300 pos++;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200301 }
Eric Biggers5de3963e2018-12-06 15:55:41 -0800302 cb->args[0] = pos;
303 res = skb->len;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200304out:
Eric Biggers5de3963e2018-12-06 15:55:41 -0800305 up_read(&crypto_alg_sem);
306 return res;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200307}
308
309static int crypto_dump_report_done(struct netlink_callback *cb)
310{
311 return 0;
312}
313
314static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
315 struct nlattr **attrs)
316{
317 struct crypto_alg *alg;
318 struct crypto_user_alg *p = nlmsg_data(nlh);
319 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
320 LIST_HEAD(list);
321
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700322 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800323 return -EPERM;
324
Mathias Krause8fd61d32013-02-05 18:19:15 +0100325 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
326 return -EINVAL;
327
Steffen Klasserta38f7902011-09-27 07:23:50 +0200328 if (priority && !strlen(p->cru_driver_name))
329 return -EINVAL;
330
331 alg = crypto_alg_match(p, 1);
332 if (!alg)
333 return -ENOENT;
334
335 down_write(&crypto_alg_sem);
336
337 crypto_remove_spawns(alg, &list, NULL);
338
339 if (priority)
340 alg->cra_priority = nla_get_u32(priority);
341
342 up_write(&crypto_alg_sem);
343
Herbert Xu016baaa2015-04-07 21:27:01 +0800344 crypto_mod_put(alg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200345 crypto_remove_final(&list);
346
347 return 0;
348}
349
350static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
351 struct nlattr **attrs)
352{
353 struct crypto_alg *alg;
354 struct crypto_user_alg *p = nlmsg_data(nlh);
Herbert Xu016baaa2015-04-07 21:27:01 +0800355 int err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200356
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700357 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800358 return -EPERM;
359
Mathias Krause8fd61d32013-02-05 18:19:15 +0100360 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
361 return -EINVAL;
362
Steffen Klasserta38f7902011-09-27 07:23:50 +0200363 alg = crypto_alg_match(p, 1);
364 if (!alg)
365 return -ENOENT;
366
367 /* We can not unregister core algorithms such as aes-generic.
368 * We would loose the reference in the crypto_alg_list to this algorithm
369 * if we try to unregister. Unregistering such an algorithm without
370 * removing the module is not possible, so we restrict to crypto
371 * instances that are build from templates. */
Herbert Xu016baaa2015-04-07 21:27:01 +0800372 err = -EINVAL;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200373 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
Herbert Xu016baaa2015-04-07 21:27:01 +0800374 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200375
Herbert Xu016baaa2015-04-07 21:27:01 +0800376 err = -EBUSY;
377 if (atomic_read(&alg->cra_refcnt) > 2)
378 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200379
Herbert Xu016baaa2015-04-07 21:27:01 +0800380 err = crypto_unregister_instance((struct crypto_instance *)alg);
381
382drop_alg:
383 crypto_mod_put(alg);
384 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200385}
386
387static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
388 struct nlattr **attrs)
389{
Jesper Juhl0cfdec72012-01-29 23:39:22 +0100390 int exact = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200391 const char *name;
392 struct crypto_alg *alg;
393 struct crypto_user_alg *p = nlmsg_data(nlh);
394 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
395
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700396 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800397 return -EPERM;
398
Mathias Krause8fd61d32013-02-05 18:19:15 +0100399 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
400 return -EINVAL;
401
Steffen Klasserta38f7902011-09-27 07:23:50 +0200402 if (strlen(p->cru_driver_name))
403 exact = 1;
404
405 if (priority && !exact)
406 return -EINVAL;
407
408 alg = crypto_alg_match(p, exact);
Herbert Xu016baaa2015-04-07 21:27:01 +0800409 if (alg) {
410 crypto_mod_put(alg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200411 return -EEXIST;
Herbert Xu016baaa2015-04-07 21:27:01 +0800412 }
Steffen Klasserta38f7902011-09-27 07:23:50 +0200413
414 if (strlen(p->cru_driver_name))
415 name = p->cru_driver_name;
416 else
417 name = p->cru_name;
418
Herbert Xu6cf80a22016-07-12 13:17:49 +0800419 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200420 if (IS_ERR(alg))
421 return PTR_ERR(alg);
422
423 down_write(&crypto_alg_sem);
424
425 if (priority)
426 alg->cra_priority = nla_get_u32(priority);
427
428 up_write(&crypto_alg_sem);
429
430 crypto_mod_put(alg);
431
432 return 0;
433}
434
Herbert Xu9aa867e2015-06-21 19:11:45 +0800435static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
436 struct nlattr **attrs)
437{
438 if (!netlink_capable(skb, CAP_NET_ADMIN))
439 return -EPERM;
440 return crypto_del_default_rng();
441}
442
Steffen Klasserta38f7902011-09-27 07:23:50 +0200443#define MSGSIZE(type) sizeof(struct type)
444
445static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
446 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
447 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
448 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Mathias Krause055ddaa2016-06-22 20:29:37 +0200449 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Herbert Xu9aa867e2015-06-21 19:11:45 +0800450 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
Steffen Klasserta38f7902011-09-27 07:23:50 +0200451};
452
453static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
454 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
455};
456
457#undef MSGSIZE
458
Mathias Krausea84fb792013-02-24 14:09:12 +0100459static const struct crypto_link {
Steffen Klasserta38f7902011-09-27 07:23:50 +0200460 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
461 int (*dump)(struct sk_buff *, struct netlink_callback *);
462 int (*done)(struct netlink_callback *);
463} crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
464 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
465 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
466 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
467 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
468 .dump = crypto_dump_report,
469 .done = crypto_dump_report_done},
Herbert Xu9aa867e2015-06-21 19:11:45 +0800470 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
Steffen Klasserta38f7902011-09-27 07:23:50 +0200471};
472
473static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
474{
475 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
Mathias Krausea84fb792013-02-24 14:09:12 +0100476 const struct crypto_link *link;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200477 int type, err;
478
479 type = nlh->nlmsg_type;
480 if (type > CRYPTO_MSG_MAX)
481 return -EINVAL;
482
483 type -= CRYPTO_MSG_BASE;
484 link = &crypto_dispatch[type];
485
Steffen Klasserta38f7902011-09-27 07:23:50 +0200486 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
487 (nlh->nlmsg_flags & NLM_F_DUMP))) {
Steffen Klassert5219a532012-03-29 09:04:46 +0200488 struct crypto_alg *alg;
Eric Biggers5de3963e2018-12-06 15:55:41 -0800489 unsigned long dump_alloc = 0;
Steffen Klassert5219a532012-03-29 09:04:46 +0200490
Steffen Klasserta38f7902011-09-27 07:23:50 +0200491 if (link->dump == NULL)
492 return -EINVAL;
Steffen Klassert5219a532012-03-29 09:04:46 +0200493
Mathias Krause63e41eb2016-02-01 14:27:30 +0100494 down_read(&crypto_alg_sem);
Steffen Klassert5219a532012-03-29 09:04:46 +0200495 list_for_each_entry(alg, &crypto_alg_list, cra_list)
496 dump_alloc += CRYPTO_REPORT_MAXSIZE;
Eric Biggers5de3963e2018-12-06 15:55:41 -0800497 up_read(&crypto_alg_sem);
Steffen Klassert5219a532012-03-29 09:04:46 +0200498
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000499 {
500 struct netlink_dump_control c = {
501 .dump = link->dump,
502 .done = link->done,
Eric Biggers5de3963e2018-12-06 15:55:41 -0800503 .min_dump_alloc = min(dump_alloc, 65535UL),
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000504 };
Mathias Krause63e41eb2016-02-01 14:27:30 +0100505 err = netlink_dump_start(crypto_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000506 }
Mathias Krause63e41eb2016-02-01 14:27:30 +0100507
508 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200509 }
510
Herbert Xufd2efd92016-06-23 18:06:02 +0800511 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
512 crypto_policy);
513 if (err < 0)
514 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200515
516 if (link->doit == NULL)
517 return -EINVAL;
518
519 return link->doit(skb, nlh, attrs);
520}
521
522static void crypto_netlink_rcv(struct sk_buff *skb)
523{
524 mutex_lock(&crypto_cfg_mutex);
525 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
526 mutex_unlock(&crypto_cfg_mutex);
527}
528
529static int __init crypto_user_init(void)
530{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000531 struct netlink_kernel_cfg cfg = {
532 .input = crypto_netlink_rcv,
533 };
534
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000535 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, &cfg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200536 if (!crypto_nlsk)
537 return -ENOMEM;
538
539 return 0;
540}
541
542static void __exit crypto_user_exit(void)
543{
544 netlink_kernel_release(crypto_nlsk);
545}
546
547module_init(crypto_user_init);
548module_exit(crypto_user_exit);
549MODULE_LICENSE("GPL");
550MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
551MODULE_DESCRIPTION("Crypto userspace configuration API");
Stephan Mueller476c7fe2014-11-24 17:12:45 +0100552MODULE_ALIAS("net-pf-16-proto-21");