blob: 910e0b46012e37e8097f6578e2a305ddfc75a9b7 [file] [log] [blame]
Thomas Gleixnera61127c2019-05-29 16:57:49 -07001// SPDX-License-Identifier: GPL-2.0-only
Steffen Klasserta38f7902011-09-27 07:23:50 +02002/*
3 * Crypto user configuration API.
4 *
5 * Copyright (C) 2011 secunet Security Networks AG
6 * Copyright (C) 2011 Steffen Klassert <steffen.klassert@secunet.com>
Steffen Klasserta38f7902011-09-27 07:23:50 +02007 */
8
9#include <linux/module.h>
10#include <linux/crypto.h>
11#include <linux/cryptouser.h>
Steffen Klassert1e122992012-03-29 09:03:47 +020012#include <linux/sched.h>
Steffen Klasserta38f7902011-09-27 07:23:50 +020013#include <linux/security.h>
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +020014#include <net/netlink.h>
Steffen Klasserta38f7902011-09-27 07:23:50 +020015#include <net/net_namespace.h>
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +020016#include <net/sock.h>
Steffen Klassert1e122992012-03-29 09:03:47 +020017#include <crypto/internal/skcipher.h>
Herbert Xu9aa867e2015-06-21 19:11:45 +080018#include <crypto/internal/rng.h>
Tadeusz Struk3c339ab2015-06-16 10:30:55 -070019#include <crypto/akcipher.h>
Salvatore Benedetto4e5f2c42016-06-22 17:49:13 +010020#include <crypto/kpp.h>
Corentin Labbecac58182018-09-19 10:10:54 +000021#include <crypto/internal/cryptouser.h>
Steffen Klassert1e122992012-03-29 09:03:47 +020022
Steffen Klasserta38f7902011-09-27 07:23:50 +020023#include "internal.h"
24
Mathias Krause8fd61d32013-02-05 18:19:15 +010025#define null_terminated(x) (strnlen(x, sizeof(x)) < sizeof(x))
26
Jussi Kivilinna66ce0b02012-08-28 16:46:54 +030027static DEFINE_MUTEX(crypto_cfg_mutex);
Steffen Klasserta38f7902011-09-27 07:23:50 +020028
Steffen Klasserta38f7902011-09-27 07:23:50 +020029struct crypto_dump_info {
30 struct sk_buff *in_skb;
31 struct sk_buff *out_skb;
32 u32 nlmsg_seq;
33 u16 nlmsg_flags;
34};
35
Corentin Labbecac58182018-09-19 10:10:54 +000036struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
Steffen Klasserta38f7902011-09-27 07:23:50 +020037{
Steffen Klasserta38f7902011-09-27 07:23:50 +020038 struct crypto_alg *q, *alg = NULL;
39
40 down_read(&crypto_alg_sem);
41
Steffen Klasserta38f7902011-09-27 07:23:50 +020042 list_for_each_entry(q, &crypto_alg_list, cra_list) {
Herbert Xue6ea64e2011-10-21 14:37:10 +020043 int match = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +020044
Eric Biggers21d41202019-07-02 14:17:00 -070045 if (crypto_is_larval(q))
46 continue;
47
Steffen Klasserta38f7902011-09-27 07:23:50 +020048 if ((q->cra_flags ^ p->cru_type) & p->cru_mask)
49 continue;
50
51 if (strlen(p->cru_driver_name))
52 match = !strcmp(q->cra_driver_name,
53 p->cru_driver_name);
54 else if (!exact)
55 match = !strcmp(q->cra_name, p->cru_name);
56
Herbert Xu016baaa2015-04-07 21:27:01 +080057 if (!match)
58 continue;
59
60 if (unlikely(!crypto_mod_get(q)))
61 continue;
62
63 alg = q;
64 break;
Steffen Klasserta38f7902011-09-27 07:23:50 +020065 }
66
67 up_read(&crypto_alg_sem);
68
69 return alg;
70}
71
Steffen Klassert07a5fa42011-09-27 07:48:01 +020072static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
73{
74 struct crypto_report_cipher rcipher;
75
Eric Biggers37db69e2018-11-03 14:56:03 -070076 memset(&rcipher, 0, sizeof(rcipher));
77
78 strscpy(rcipher.type, "cipher", sizeof(rcipher.type));
Steffen Klassert07a5fa42011-09-27 07:48:01 +020079
80 rcipher.blocksize = alg->cra_blocksize;
81 rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
82 rcipher.max_keysize = alg->cra_cipher.cia_max_keysize;
83
Eric Biggers37db69e2018-11-03 14:56:03 -070084 return nla_put(skb, CRYPTOCFGA_REPORT_CIPHER,
85 sizeof(rcipher), &rcipher);
Steffen Klassert07a5fa42011-09-27 07:48:01 +020086}
87
Steffen Klassert540b97c2011-09-27 07:48:48 +020088static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)
89{
90 struct crypto_report_comp rcomp;
91
Eric Biggers37db69e2018-11-03 14:56:03 -070092 memset(&rcomp, 0, sizeof(rcomp));
Steffen Klassert540b97c2011-09-27 07:48:48 +020093
Eric Biggers37db69e2018-11-03 14:56:03 -070094 strscpy(rcomp.type, "compression", sizeof(rcomp.type));
95
96 return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS, sizeof(rcomp), &rcomp);
Steffen Klassert540b97c2011-09-27 07:48:48 +020097}
98
Steffen Klasserta38f7902011-09-27 07:23:50 +020099static int crypto_report_one(struct crypto_alg *alg,
100 struct crypto_user_alg *ualg, struct sk_buff *skb)
101{
Eric Biggers37db69e2018-11-03 14:56:03 -0700102 memset(ualg, 0, sizeof(*ualg));
103
104 strscpy(ualg->cru_name, alg->cra_name, sizeof(ualg->cru_name));
105 strscpy(ualg->cru_driver_name, alg->cra_driver_name,
Mathias Krause9a5467bf2013-02-05 18:19:13 +0100106 sizeof(ualg->cru_driver_name));
Eric Biggers37db69e2018-11-03 14:56:03 -0700107 strscpy(ualg->cru_module_name, module_name(alg->cra_module),
Mathias Krause9a5467bf2013-02-05 18:19:13 +0100108 sizeof(ualg->cru_module_name));
Steffen Klasserta38f7902011-09-27 07:23:50 +0200109
Mathias Krause9a5467bf2013-02-05 18:19:13 +0100110 ualg->cru_type = 0;
111 ualg->cru_mask = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200112 ualg->cru_flags = alg->cra_flags;
Eric Biggersce8614a2017-12-29 10:00:46 -0600113 ualg->cru_refcnt = refcount_read(&alg->cra_refcnt);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200114
David S. Miller6662df32012-04-01 20:19:05 -0400115 if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
116 goto nla_put_failure;
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200117 if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
118 struct crypto_report_larval rl;
119
Eric Biggers37db69e2018-11-03 14:56:03 -0700120 memset(&rl, 0, sizeof(rl));
121 strscpy(rl.type, "larval", sizeof(rl.type));
122 if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL, sizeof(rl), &rl))
David S. Miller6662df32012-04-01 20:19:05 -0400123 goto nla_put_failure;
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200124 goto out;
125 }
126
Steffen Klassertb6aa63c2011-09-27 07:24:29 +0200127 if (alg->cra_type && alg->cra_type->report) {
128 if (alg->cra_type->report(skb, alg))
129 goto nla_put_failure;
Steffen Klassert07a5fa42011-09-27 07:48:01 +0200130
131 goto out;
132 }
133
134 switch (alg->cra_flags & (CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_LARVAL)) {
135 case CRYPTO_ALG_TYPE_CIPHER:
136 if (crypto_report_cipher(skb, alg))
137 goto nla_put_failure;
138
139 break;
Steffen Klassert540b97c2011-09-27 07:48:48 +0200140 case CRYPTO_ALG_TYPE_COMPRESS:
141 if (crypto_report_comp(skb, alg))
142 goto nla_put_failure;
143
144 break;
Steffen Klassertb6aa63c2011-09-27 07:24:29 +0200145 }
146
Steffen Klassert6c5a86f52011-09-27 07:25:05 +0200147out:
Steffen Klasserta38f7902011-09-27 07:23:50 +0200148 return 0;
149
150nla_put_failure:
151 return -EMSGSIZE;
152}
153
154static int crypto_report_alg(struct crypto_alg *alg,
155 struct crypto_dump_info *info)
156{
157 struct sk_buff *in_skb = info->in_skb;
158 struct sk_buff *skb = info->out_skb;
159 struct nlmsghdr *nlh;
160 struct crypto_user_alg *ualg;
161 int err = 0;
162
Eric W. Biederman15e47302012-09-07 20:12:54 +0000163 nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, info->nlmsg_seq,
Steffen Klasserta38f7902011-09-27 07:23:50 +0200164 CRYPTO_MSG_GETALG, sizeof(*ualg), info->nlmsg_flags);
165 if (!nlh) {
166 err = -EMSGSIZE;
167 goto out;
168 }
169
170 ualg = nlmsg_data(nlh);
171
172 err = crypto_report_one(alg, ualg, skb);
173 if (err) {
174 nlmsg_cancel(skb, nlh);
175 goto out;
176 }
177
178 nlmsg_end(skb, nlh);
179
180out:
181 return err;
182}
183
184static int crypto_report(struct sk_buff *in_skb, struct nlmsghdr *in_nlh,
185 struct nlattr **attrs)
186{
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +0200187 struct net *net = sock_net(in_skb->sk);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200188 struct crypto_user_alg *p = nlmsg_data(in_nlh);
189 struct crypto_alg *alg;
190 struct sk_buff *skb;
191 struct crypto_dump_info info;
192 int err;
193
Mathias Krause8fd61d32013-02-05 18:19:15 +0100194 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
195 return -EINVAL;
196
Herbert Xu5d4a5e72014-11-20 12:44:32 +0800197 alg = crypto_alg_match(p, 0);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200198 if (!alg)
199 return -ENOENT;
200
Herbert Xu016baaa2015-04-07 21:27:01 +0800201 err = -ENOMEM;
Jia-Ju Bai9a69b7a2018-01-25 18:06:02 +0800202 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200203 if (!skb)
Herbert Xu016baaa2015-04-07 21:27:01 +0800204 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200205
206 info.in_skb = in_skb;
207 info.out_skb = skb;
208 info.nlmsg_seq = in_nlh->nlmsg_seq;
209 info.nlmsg_flags = 0;
210
211 err = crypto_report_alg(alg, &info);
Herbert Xu016baaa2015-04-07 21:27:01 +0800212
213drop_alg:
214 crypto_mod_put(alg);
215
Steffen Klasserta38f7902011-09-27 07:23:50 +0200216 if (err)
217 return err;
218
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +0200219 return nlmsg_unicast(net->crypto_nlsk, skb, NETLINK_CB(in_skb).portid);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200220}
221
222static int crypto_dump_report(struct sk_buff *skb, struct netlink_callback *cb)
223{
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800224 const size_t start_pos = cb->args[0];
225 size_t pos = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200226 struct crypto_dump_info info;
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800227 struct crypto_alg *alg;
228 int res;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200229
230 info.in_skb = cb->skb;
231 info.out_skb = skb;
232 info.nlmsg_seq = cb->nlh->nlmsg_seq;
233 info.nlmsg_flags = NLM_F_MULTI;
234
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800235 down_read(&crypto_alg_sem);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200236 list_for_each_entry(alg, &crypto_alg_list, cra_list) {
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800237 if (pos >= start_pos) {
238 res = crypto_report_alg(alg, &info);
239 if (res == -EMSGSIZE)
240 break;
241 if (res)
242 goto out;
243 }
244 pos++;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200245 }
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800246 cb->args[0] = pos;
247 res = skb->len;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200248out:
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800249 up_read(&crypto_alg_sem);
250 return res;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200251}
252
253static int crypto_dump_report_done(struct netlink_callback *cb)
254{
255 return 0;
256}
257
258static int crypto_update_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
259 struct nlattr **attrs)
260{
261 struct crypto_alg *alg;
262 struct crypto_user_alg *p = nlmsg_data(nlh);
263 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
264 LIST_HEAD(list);
265
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700266 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800267 return -EPERM;
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
Steffen Klasserta38f7902011-09-27 07:23:50 +0200272 if (priority && !strlen(p->cru_driver_name))
273 return -EINVAL;
274
275 alg = crypto_alg_match(p, 1);
276 if (!alg)
277 return -ENOENT;
278
279 down_write(&crypto_alg_sem);
280
281 crypto_remove_spawns(alg, &list, NULL);
282
283 if (priority)
284 alg->cra_priority = nla_get_u32(priority);
285
286 up_write(&crypto_alg_sem);
287
Herbert Xu016baaa2015-04-07 21:27:01 +0800288 crypto_mod_put(alg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200289 crypto_remove_final(&list);
290
291 return 0;
292}
293
294static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
295 struct nlattr **attrs)
296{
297 struct crypto_alg *alg;
298 struct crypto_user_alg *p = nlmsg_data(nlh);
Herbert Xu016baaa2015-04-07 21:27:01 +0800299 int err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200300
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700301 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800302 return -EPERM;
303
Mathias Krause8fd61d32013-02-05 18:19:15 +0100304 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
305 return -EINVAL;
306
Steffen Klasserta38f7902011-09-27 07:23:50 +0200307 alg = crypto_alg_match(p, 1);
308 if (!alg)
309 return -ENOENT;
310
311 /* We can not unregister core algorithms such as aes-generic.
312 * We would loose the reference in the crypto_alg_list to this algorithm
313 * if we try to unregister. Unregistering such an algorithm without
314 * removing the module is not possible, so we restrict to crypto
315 * instances that are build from templates. */
Herbert Xu016baaa2015-04-07 21:27:01 +0800316 err = -EINVAL;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200317 if (!(alg->cra_flags & CRYPTO_ALG_INSTANCE))
Herbert Xu016baaa2015-04-07 21:27:01 +0800318 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200319
Herbert Xu016baaa2015-04-07 21:27:01 +0800320 err = -EBUSY;
Eric Biggersce8614a2017-12-29 10:00:46 -0600321 if (refcount_read(&alg->cra_refcnt) > 2)
Herbert Xu016baaa2015-04-07 21:27:01 +0800322 goto drop_alg;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200323
Herbert Xu016baaa2015-04-07 21:27:01 +0800324 err = crypto_unregister_instance((struct crypto_instance *)alg);
325
326drop_alg:
327 crypto_mod_put(alg);
328 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200329}
330
331static int crypto_add_alg(struct sk_buff *skb, struct nlmsghdr *nlh,
332 struct nlattr **attrs)
333{
Jesper Juhl0cfdec7a2012-01-29 23:39:22 +0100334 int exact = 0;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200335 const char *name;
336 struct crypto_alg *alg;
337 struct crypto_user_alg *p = nlmsg_data(nlh);
338 struct nlattr *priority = attrs[CRYPTOCFGA_PRIORITY_VAL];
339
Linus Torvalds639b4ac2014-06-07 19:44:40 -0700340 if (!netlink_capable(skb, CAP_NET_ADMIN))
Matthias-Christian Ottc5683982014-05-08 21:58:12 +0800341 return -EPERM;
342
Mathias Krause8fd61d32013-02-05 18:19:15 +0100343 if (!null_terminated(p->cru_name) || !null_terminated(p->cru_driver_name))
344 return -EINVAL;
345
Steffen Klasserta38f7902011-09-27 07:23:50 +0200346 if (strlen(p->cru_driver_name))
347 exact = 1;
348
349 if (priority && !exact)
350 return -EINVAL;
351
352 alg = crypto_alg_match(p, exact);
Herbert Xu016baaa2015-04-07 21:27:01 +0800353 if (alg) {
354 crypto_mod_put(alg);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200355 return -EEXIST;
Herbert Xu016baaa2015-04-07 21:27:01 +0800356 }
Steffen Klasserta38f7902011-09-27 07:23:50 +0200357
358 if (strlen(p->cru_driver_name))
359 name = p->cru_driver_name;
360 else
361 name = p->cru_name;
362
Herbert Xu6cf80a22016-07-12 13:17:49 +0800363 alg = crypto_alg_mod_lookup(name, p->cru_type, p->cru_mask);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200364 if (IS_ERR(alg))
365 return PTR_ERR(alg);
366
367 down_write(&crypto_alg_sem);
368
369 if (priority)
370 alg->cra_priority = nla_get_u32(priority);
371
372 up_write(&crypto_alg_sem);
373
374 crypto_mod_put(alg);
375
376 return 0;
377}
378
Herbert Xu9aa867e2015-06-21 19:11:45 +0800379static int crypto_del_rng(struct sk_buff *skb, struct nlmsghdr *nlh,
380 struct nlattr **attrs)
381{
382 if (!netlink_capable(skb, CAP_NET_ADMIN))
383 return -EPERM;
384 return crypto_del_default_rng();
385}
386
Steffen Klasserta38f7902011-09-27 07:23:50 +0200387#define MSGSIZE(type) sizeof(struct type)
388
389static const int crypto_msg_min[CRYPTO_NR_MSGTYPES] = {
390 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
391 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
392 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Mathias Krause055ddaa2016-06-22 20:29:37 +0200393 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Herbert Xu9aa867e2015-06-21 19:11:45 +0800394 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = 0,
Corentin Labbecac58182018-09-19 10:10:54 +0000395 [CRYPTO_MSG_GETSTAT - CRYPTO_MSG_BASE] = MSGSIZE(crypto_user_alg),
Steffen Klasserta38f7902011-09-27 07:23:50 +0200396};
397
398static const struct nla_policy crypto_policy[CRYPTOCFGA_MAX+1] = {
399 [CRYPTOCFGA_PRIORITY_VAL] = { .type = NLA_U32},
400};
401
402#undef MSGSIZE
403
Mathias Krausea84fb792013-02-24 14:09:12 +0100404static const struct crypto_link {
Steffen Klasserta38f7902011-09-27 07:23:50 +0200405 int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
406 int (*dump)(struct sk_buff *, struct netlink_callback *);
407 int (*done)(struct netlink_callback *);
408} crypto_dispatch[CRYPTO_NR_MSGTYPES] = {
409 [CRYPTO_MSG_NEWALG - CRYPTO_MSG_BASE] = { .doit = crypto_add_alg},
410 [CRYPTO_MSG_DELALG - CRYPTO_MSG_BASE] = { .doit = crypto_del_alg},
411 [CRYPTO_MSG_UPDATEALG - CRYPTO_MSG_BASE] = { .doit = crypto_update_alg},
412 [CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE] = { .doit = crypto_report,
413 .dump = crypto_dump_report,
414 .done = crypto_dump_report_done},
Herbert Xu9aa867e2015-06-21 19:11:45 +0800415 [CRYPTO_MSG_DELRNG - CRYPTO_MSG_BASE] = { .doit = crypto_del_rng },
Corentin Labbe0c99c2a2018-12-13 08:36:37 +0000416 [CRYPTO_MSG_GETSTAT - CRYPTO_MSG_BASE] = { .doit = crypto_reportstat},
Steffen Klasserta38f7902011-09-27 07:23:50 +0200417};
418
Johannes Berg2d4bc932017-04-12 14:34:04 +0200419static int crypto_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
420 struct netlink_ext_ack *extack)
Steffen Klasserta38f7902011-09-27 07:23:50 +0200421{
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +0200422 struct net *net = sock_net(skb->sk);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200423 struct nlattr *attrs[CRYPTOCFGA_MAX+1];
Mathias Krausea84fb792013-02-24 14:09:12 +0100424 const struct crypto_link *link;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200425 int type, err;
426
427 type = nlh->nlmsg_type;
428 if (type > CRYPTO_MSG_MAX)
429 return -EINVAL;
430
431 type -= CRYPTO_MSG_BASE;
432 link = &crypto_dispatch[type];
433
Steffen Klasserta38f7902011-09-27 07:23:50 +0200434 if ((type == (CRYPTO_MSG_GETALG - CRYPTO_MSG_BASE) &&
435 (nlh->nlmsg_flags & NLM_F_DUMP))) {
Steffen Klassert5219a532012-03-29 09:04:46 +0200436 struct crypto_alg *alg;
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800437 unsigned long dump_alloc = 0;
Steffen Klassert5219a532012-03-29 09:04:46 +0200438
Steffen Klasserta38f7902011-09-27 07:23:50 +0200439 if (link->dump == NULL)
440 return -EINVAL;
Steffen Klassert5219a532012-03-29 09:04:46 +0200441
Mathias Krause63e41eb2016-02-01 14:27:30 +0100442 down_read(&crypto_alg_sem);
Steffen Klassert5219a532012-03-29 09:04:46 +0200443 list_for_each_entry(alg, &crypto_alg_list, cra_list)
444 dump_alloc += CRYPTO_REPORT_MAXSIZE;
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800445 up_read(&crypto_alg_sem);
Steffen Klassert5219a532012-03-29 09:04:46 +0200446
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000447 {
448 struct netlink_dump_control c = {
449 .dump = link->dump,
450 .done = link->done,
Eric Biggers0ac6b8f2018-12-06 15:55:41 -0800451 .min_dump_alloc = min(dump_alloc, 65535UL),
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000452 };
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +0200453 err = netlink_dump_start(net->crypto_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000454 }
Mathias Krause63e41eb2016-02-01 14:27:30 +0100455
456 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200457 }
458
Johannes Berg8cb08172019-04-26 14:07:28 +0200459 err = nlmsg_parse_deprecated(nlh, crypto_msg_min[type], attrs,
460 CRYPTOCFGA_MAX, crypto_policy, extack);
Herbert Xufd2efd92016-06-23 18:06:02 +0800461 if (err < 0)
462 return err;
Steffen Klasserta38f7902011-09-27 07:23:50 +0200463
464 if (link->doit == NULL)
465 return -EINVAL;
466
467 return link->doit(skb, nlh, attrs);
468}
469
470static void crypto_netlink_rcv(struct sk_buff *skb)
471{
472 mutex_lock(&crypto_cfg_mutex);
473 netlink_rcv_skb(skb, &crypto_user_rcv_msg);
474 mutex_unlock(&crypto_cfg_mutex);
475}
476
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +0200477static int __net_init crypto_netlink_init(struct net *net)
Steffen Klasserta38f7902011-09-27 07:23:50 +0200478{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000479 struct netlink_kernel_cfg cfg = {
480 .input = crypto_netlink_rcv,
481 };
482
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +0200483 net->crypto_nlsk = netlink_kernel_create(net, NETLINK_CRYPTO, &cfg);
484 return net->crypto_nlsk == NULL ? -ENOMEM : 0;
485}
Steffen Klasserta38f7902011-09-27 07:23:50 +0200486
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +0200487static void __net_exit crypto_netlink_exit(struct net *net)
488{
489 netlink_kernel_release(net->crypto_nlsk);
490 net->crypto_nlsk = NULL;
491}
492
493static struct pernet_operations crypto_netlink_net_ops = {
494 .init = crypto_netlink_init,
495 .exit = crypto_netlink_exit,
496};
497
498static int __init crypto_user_init(void)
499{
500 return register_pernet_subsys(&crypto_netlink_net_ops);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200501}
502
503static void __exit crypto_user_exit(void)
504{
Ondrej Mosnacek91b05a72019-07-09 13:11:24 +0200505 unregister_pernet_subsys(&crypto_netlink_net_ops);
Steffen Klasserta38f7902011-09-27 07:23:50 +0200506}
507
508module_init(crypto_user_init);
509module_exit(crypto_user_exit);
510MODULE_LICENSE("GPL");
511MODULE_AUTHOR("Steffen Klassert <steffen.klassert@secunet.com>");
512MODULE_DESCRIPTION("Crypto userspace configuration API");
Stephan Mueller476c7fe2014-11-24 17:12:45 +0100513MODULE_ALIAS("net-pf-16-proto-21");