blob: f847c181a39c8f6cadad7a403b0a9a8b75e24585 [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 Biggers015c2052019-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
Eric Biggersa0f044f2018-11-03 14:56:00 -070089 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
Eric Biggersa0f044f2018-11-03 14:56:00 -0700108 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
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +0100118static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)
119{
120 struct crypto_report_acomp racomp;
121
Eric Biggersa0f044f2018-11-03 14:56:00 -0700122 strncpy(racomp.type, "acomp", sizeof(racomp.type));
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +0100123
124 if (nla_put(skb, CRYPTOCFGA_REPORT_ACOMP,
125 sizeof(struct crypto_report_acomp), &racomp))
126 goto nla_put_failure;
127 return 0;
128
129nla_put_failure:
130 return -EMSGSIZE;
131}
132
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700133static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)
134{
135 struct crypto_report_akcipher rakcipher;
136
Eric Biggersa0f044f2018-11-03 14:56:00 -0700137 strncpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700138
139 if (nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
140 sizeof(struct crypto_report_akcipher), &rakcipher))
141 goto nla_put_failure;
142 return 0;
143
144nla_put_failure:
145 return -EMSGSIZE;
146}
147
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +0100148static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)
149{
150 struct crypto_report_kpp rkpp;
151
Eric Biggersa0f044f2018-11-03 14:56:00 -0700152 strncpy(rkpp.type, "kpp", sizeof(rkpp.type));
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +0100153
154 if (nla_put(skb, CRYPTOCFGA_REPORT_KPP,
155 sizeof(struct crypto_report_kpp), &rkpp))
156 goto nla_put_failure;
157 return 0;
158
159nla_put_failure:
160 return -EMSGSIZE;
161}
162
Steffen Klasserta38f7902011-09-27 07:23:50 +0200163static int crypto_report_one(struct crypto_alg *alg,
164 struct crypto_user_alg *ualg, struct sk_buff *skb)
165{
Eric Biggersa0f044f2018-11-03 14:56:00 -0700166 strncpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
167 strncpy(ualg->cru_driver_name, alg->cra_driver_name,
Mathias Krause9a5467b2013-02-05 18:19:13 +0100168 sizeof(ualg->cru_driver_name));
Eric Biggersa0f044f2018-11-03 14:56:00 -0700169 strncpy(ualg->cru_module_name, module_name(alg->cra_module),
Mathias Krause9a5467b2013-02-05 18:19:13 +0100170 sizeof(ualg->cru_module_name));
Steffen Klasserta38f7902011-09-27 07:23:50 +0200171
Mathias Krause9a5467b2013-02-05 18:19:13 +0100172 ualg->cru_type = 0;
173 ualg->cru_mask = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200174 ualg->cru_flags = alg->cra_flags;
Eric Biggersce8614a2017-12-29 10:00:46 -0600175 ualg->cru_refcnt = refcount_read(&alg->cra_refcnt);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200176
David S. Miller6662df32012-04-01 20:19:05 -0400177 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
178 goto nla_put_failure;
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200179 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
180 struct crypto_report_larval rl;
181
Eric Biggersa0f044f2018-11-03 14:56:00 -0700182 strncpy(rl.type, "larval", sizeof(rl.type));
David S. Miller6662df32012-04-01 20:19:05 -0400183 if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL,
184 sizeof(struct crypto_report_larval), &rl))
185 goto nla_put_failure;
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200186 goto out;
187 }
188
Steffen Klassertb6aa63c2011-09-27 07:24:29 +0200189 if (alg->cra_type && alg->cra_type->report) {
190 if (alg->cra_type->report(skb, alg))
191 goto nla_put_failure;
Steffen Klassert07a5fa42011-09-27 07:48:01 +0200192
193 goto out;
194 }
195
196 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
197 case CRYPTO_ALG_TYPE_CIPHER:
198 if (crypto_report_cipher(skb, alg))
199 goto nla_put_failure;
200
201 break;
Steffen Klassert540b97c2011-09-27 07:48:48 +0200202 case CRYPTO_ALG_TYPE_COMPRESS:
203 if (crypto_report_comp(skb, alg))
204 goto nla_put_failure;
205
206 break;
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +0100207 case CRYPTO_ALG_TYPE_ACOMPRESS:
208 if (crypto_report_acomp(skb, alg))
209 goto nla_put_failure;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700210
Giovanni Cabiddu2ebda742016-10-21 13:19:47 +0100211 break;
Tadeusz Struk3c339ab2015-06-16 10:30:55 -0700212 case CRYPTO_ALG_TYPE_AKCIPHER:
213 if (crypto_report_akcipher(skb, alg))
214 goto nla_put_failure;
215
216 break;
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +0100217 case CRYPTO_ALG_TYPE_KPP:
218 if (crypto_report_kpp(skb, alg))
219 goto nla_put_failure;
220 break;
Steffen Klassertb6aa63c2011-09-27 07:24:29 +0200221 }
222
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200223out:
Steffen Klasserta38f7902011-09-27 07:23:50 +0200224 return 0;
225
226nla_put_failure:
227 return -EMSGSIZE;
228}
229
230static int crypto_report_alg(struct crypto_alg *alg,
231 struct crypto_dump_info *info)
232{
233 struct sk_buff *in_skb = info->in_skb;
234 struct sk_buff *skb = info->out_skb;
235 struct nlmsghdr *nlh;
236 struct crypto_user_alg *ualg;
237 int err = 0;
238
Eric W. Biederman15e47302012-09-07 20:12:54 +0000239 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
Steffen Klasserta38f7902011-09-27 07:23:50 +0200240 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
241 if (!nlh) {
242 err = -EMSGSIZE;
243 goto out;
244 }
245
246 ualg = nlmsg_data(nlh);
247
248 err = crypto_report_one(alg, ualg, skb);
249 if (err) {
250 nlmsg_cancel(skb, nlh);
251 goto out;
252 }
253
254 nlmsg_end(skb, nlh);
255
256out:
257 return err;
258}
259
260static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
261 struct nlattr **attrs)
262{
263 struct crypto_user_alg *p = nlmsg_data(in_nlh);
264 struct crypto_alg *alg;
265 struct sk_buff *skb;
266 struct crypto_dump_info info;
267 int err;
268
Mathias Krause8fd61d32013-02-05 18:19:15 +0100269 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
270 return -EINVAL;
271
Herbert Xu5d4a5e72014-11-20 12:44:32 +0800272 alg = crypto_alg_match(p, 0);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200273 if (!alg)
274 return -ENOENT;
275
Herbert Xu016baaa2015-04-07 21:27:01 +0800276 err = -ENOMEM;
Jia-Ju Bai9a69b7a2018-01-25 18:06:02 +0800277 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200278 if (!skb)
Herbert Xu016baaa2015-04-07 21:27:01 +0800279 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200280
281 info.in_skb = in_skb;
282 info.out_skb = skb;
283 info.nlmsg_seq = in_nlh->nlmsg_seq;
284 info.nlmsg_flags = 0;
285
286 err = crypto_report_alg(alg, &info);
Herbert Xu016baaa2015-04-07 21:27:01 +0800287
288drop_alg:
289 crypto_mod_put(alg);
290
Navid Emamdoost351a5672019-10-04 14:29:16 -0500291 if (err) {
292 kfree_skb(skb);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200293 return err;
Navid Emamdoost351a5672019-10-04 14:29:16 -0500294 }
Steffen Klasserta38f7902011-09-27 07:23:50 +0200295
Eric W. Biederman15e47302012-09-07 20:12:54 +0000296 return nlmsg_unicast(crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200297}
298
299static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
300{
Eric Biggers48d37cc2018-12-06 15:55:41 -0800301 const size_t start_pos = cb->args[0];
302 size_t pos = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200303 struct crypto_dump_info info;
Eric Biggers48d37cc2018-12-06 15:55:41 -0800304 struct crypto_alg *alg;
305 int res;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200306
307 info.in_skb = cb->skb;
308 info.out_skb = skb;
309 info.nlmsg_seq = cb->nlh->nlmsg_seq;
310 info.nlmsg_flags = NLM_F_MULTI;
311
Eric Biggers48d37cc2018-12-06 15:55:41 -0800312 down_read(&crypto_alg_sem);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200313 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
Eric Biggers48d37cc2018-12-06 15:55:41 -0800314 if (pos >= start_pos) {
315 res = crypto_report_alg(alg, &info);
316 if (res == -EMSGSIZE)
317 break;
318 if (res)
319 goto out;
320 }
321 pos++;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200322 }
Eric Biggers48d37cc2018-12-06 15:55:41 -0800323 cb->args[0] = pos;
324 res = skb->len;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200325out:
Eric Biggers48d37cc2018-12-06 15:55:41 -0800326 up_read(&crypto_alg_sem);
327 return res;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200328}
329
330static int crypto_dump_report_done(struct netlink_callback *cb)
331{
332 return 0;
333}
334
335static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
336 struct nlattr **attrs)
337{
338 struct crypto_alg *alg;
339 struct crypto_user_alg *p = nlmsg_data(nlh);
340 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
341 LIST_HEAD(list);
342
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700343 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800344 return -EPERM;
345
Mathias Krause8fd61d32013-02-05 18:19:15 +0100346 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
347 return -EINVAL;
348
Steffen Klasserta38f7902011-09-27 07:23:50 +0200349 if (priority && !strlen(p->cru_driver_name))
350 return -EINVAL;
351
352 alg = crypto_alg_match(p, 1);
353 if (!alg)
354 return -ENOENT;
355
356 down_write(&crypto_alg_sem);
357
358 crypto_remove_spawns(alg, &list, NULL);
359
360 if (priority)
361 alg->cra_priority = nla_get_u32(priority);
362
363 up_write(&crypto_alg_sem);
364
Herbert Xu016baaa2015-04-07 21:27:01 +0800365 crypto_mod_put(alg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200366 crypto_remove_final(&list);
367
368 return 0;
369}
370
371static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
372 struct nlattr **attrs)
373{
374 struct crypto_alg *alg;
375 struct crypto_user_alg *p = nlmsg_data(nlh);
Herbert Xu016baaa2015-04-07 21:27:01 +0800376 int err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200377
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700378 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800379 return -EPERM;
380
Mathias Krause8fd61d32013-02-05 18:19:15 +0100381 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
382 return -EINVAL;
383
Steffen Klasserta38f7902011-09-27 07:23:50 +0200384 alg = crypto_alg_match(p, 1);
385 if (!alg)
386 return -ENOENT;
387
388 /* We can not unregister core algorithms such as aes-generic.
389 * We would loose the reference in the crypto_alg_list to this algorithm
390 * if we try to unregister. Unregistering such an algorithm without
391 * removing the module is not possible, so we restrict to crypto
392 * instances that are build from templates. */
Herbert Xu016baaa2015-04-07 21:27:01 +0800393 err = -EINVAL;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200394 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
Herbert Xu016baaa2015-04-07 21:27:01 +0800395 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200396
Herbert Xu016baaa2015-04-07 21:27:01 +0800397 err = -EBUSY;
Eric Biggersce8614a2017-12-29 10:00:46 -0600398 if (refcount_read(&alg->cra_refcnt) > 2)
Herbert Xu016baaa2015-04-07 21:27:01 +0800399 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200400
Herbert Xu016baaa2015-04-07 21:27:01 +0800401 err = crypto_unregister_instance((struct crypto_instance *)alg);
402
403drop_alg:
404 crypto_mod_put(alg);
405 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200406}
407
408static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
409 struct nlattr **attrs)
410{
Jesper Juhl0cfdec72012-01-29 23:39:22 +0100411 int exact = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200412 const char *name;
413 struct crypto_alg *alg;
414 struct crypto_user_alg *p = nlmsg_data(nlh);
415 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
416
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700417 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800418 return -EPERM;
419
Mathias Krause8fd61d32013-02-05 18:19:15 +0100420 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
421 return -EINVAL;
422
Steffen Klasserta38f7902011-09-27 07:23:50 +0200423 if (strlen(p->cru_driver_name))
424 exact = 1;
425
426 if (priority && !exact)
427 return -EINVAL;
428
429 alg = crypto_alg_match(p, exact);
Herbert Xu016baaa2015-04-07 21:27:01 +0800430 if (alg) {
431 crypto_mod_put(alg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200432 return -EEXIST;
Herbert Xu016baaa2015-04-07 21:27:01 +0800433 }
Steffen Klasserta38f7902011-09-27 07:23:50 +0200434
435 if (strlen(p->cru_driver_name))
436 name = p->cru_driver_name;
437 else
438 name = p->cru_name;
439
Herbert Xu6cf80a22016-07-12 13:17:49 +0800440 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200441 if (IS_ERR(alg))
442 return PTR_ERR(alg);
443
444 down_write(&crypto_alg_sem);
445
446 if (priority)
447 alg->cra_priority = nla_get_u32(priority);
448
449 up_write(&crypto_alg_sem);
450
451 crypto_mod_put(alg);
452
453 return 0;
454}
455
Herbert Xu9aa867e2015-06-21 19:11:45 +0800456static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
457 struct nlattr **attrs)
458{
459 if (!netlink_capable(skb, CAP_NET_ADMIN))
460 return -EPERM;
461 return crypto_del_default_rng();
462}
463
Steffen Klasserta38f7902011-09-27 07:23:50 +0200464#define MSGSIZE(type) sizeof(struct type)
465
466static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
467 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
468 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
469 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Mathias Krause055ddaa2016-06-22 20:29:37 +0200470 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Herbert Xu9aa867e2015-06-21 19:11:45 +0800471 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
Steffen Klasserta38f7902011-09-27 07:23:50 +0200472};
473
474static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
475 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
476};
477
478#undef MSGSIZE
479
Mathias Krausea84fb792013-02-24 14:09:12 +0100480static const struct crypto_link {
Steffen Klasserta38f7902011-09-27 07:23:50 +0200481 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
482 int (*dump)(struct sk_buff *, struct netlink_callback *);
483 int (*done)(struct netlink_callback *);
484} crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
485 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
486 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
487 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
488 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
489 .dump = crypto_dump_report,
490 .done = crypto_dump_report_done},
Herbert Xu9aa867e2015-06-21 19:11:45 +0800491 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
Steffen Klasserta38f7902011-09-27 07:23:50 +0200492};
493
Johannes Berg2d4bc932017-04-12 14:34:04 +0200494static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
495 struct netlink_ext_ack *extack)
Steffen Klasserta38f7902011-09-27 07:23:50 +0200496{
497 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
Mathias Krausea84fb792013-02-24 14:09:12 +0100498 const struct crypto_link *link;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200499 int type, err;
500
501 type = nlh->nlmsg_type;
502 if (type > CRYPTO_MSG_MAX)
503 return -EINVAL;
504
505 type -= CRYPTO_MSG_BASE;
506 link = &crypto_dispatch[type];
507
Steffen Klasserta38f7902011-09-27 07:23:50 +0200508 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
509 (nlh->nlmsg_flags & NLM_F_DUMP))) {
Steffen Klassert5219a532012-03-29 09:04:46 +0200510 struct crypto_alg *alg;
Eric Biggers48d37cc2018-12-06 15:55:41 -0800511 unsigned long dump_alloc = 0;
Steffen Klassert5219a532012-03-29 09:04:46 +0200512
Steffen Klasserta38f7902011-09-27 07:23:50 +0200513 if (link->dump == NULL)
514 return -EINVAL;
Steffen Klassert5219a532012-03-29 09:04:46 +0200515
Mathias Krause63e41eb2016-02-01 14:27:30 +0100516 down_read(&crypto_alg_sem);
Steffen Klassert5219a532012-03-29 09:04:46 +0200517 list_for_each_entry(alg, &crypto_alg_list, cra_list)
518 dump_alloc += CRYPTO_REPORT_MAXSIZE;
Eric Biggers48d37cc2018-12-06 15:55:41 -0800519 up_read(&crypto_alg_sem);
Steffen Klassert5219a532012-03-29 09:04:46 +0200520
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000521 {
522 struct netlink_dump_control c = {
523 .dump = link->dump,
524 .done = link->done,
Eric Biggers48d37cc2018-12-06 15:55:41 -0800525 .min_dump_alloc = min(dump_alloc, 65535UL),
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000526 };
Mathias Krause63e41eb2016-02-01 14:27:30 +0100527 err = netlink_dump_start(crypto_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000528 }
Mathias Krause63e41eb2016-02-01 14:27:30 +0100529
530 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200531 }
532
Herbert Xufd2efd92016-06-23 18:06:02 +0800533 err = nlmsg_parse(nlh, crypto_msg_min[type], attrs, CRYPTOCFGA_MAX,
Johannes Bergfe521452017-04-12 14:34:08 +0200534 crypto_policy, extack);
Herbert Xufd2efd92016-06-23 18:06:02 +0800535 if (err < 0)
536 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200537
538 if (link->doit == NULL)
539 return -EINVAL;
540
541 return link->doit(skb, nlh, attrs);
542}
543
544static void crypto_netlink_rcv(struct sk_buff *skb)
545{
546 mutex_lock(&crypto_cfg_mutex);
547 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
548 mutex_unlock(&crypto_cfg_mutex);
549}
550
551static int __init crypto_user_init(void)
552{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000553 struct netlink_kernel_cfg cfg = {
554 .input = crypto_netlink_rcv,
555 };
556
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000557 crypto_nlsk = netlink_kernel_create(&init_net, NETLINK_CRYPTO, &cfg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200558 if (!crypto_nlsk)
559 return -ENOMEM;
560
561 return 0;
562}
563
564static void __exit crypto_user_exit(void)
565{
566 netlink_kernel_release(crypto_nlsk);
567}
568
569module_init(crypto_user_init);
570module_exit(crypto_user_exit);
571MODULE_LICENSE("GPL");
572MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
573MODULE_DESCRIPTION("Crypto userspace configuration API");
Stephan Mueller476c7fe2014-11-24 17:12:45 +0100574MODULE_ALIAS("net-pf-16-proto-21");