]>
Commit | Line | Data |
---|---|---|
2b8c19db HX |
1 | /* |
2 | * Create default crypto algorithm instances. | |
3 | * | |
4 | * Copyright (c) 2006 Herbert Xu <[email protected]> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the Free | |
8 | * Software Foundation; either version 2 of the License, or (at your option) | |
9 | * any later version. | |
10 | * | |
11 | */ | |
12 | ||
13 | #include <linux/crypto.h> | |
14 | #include <linux/ctype.h> | |
15 | #include <linux/err.h> | |
16 | #include <linux/init.h> | |
cf02f5da | 17 | #include <linux/kthread.h> |
2b8c19db HX |
18 | #include <linux/module.h> |
19 | #include <linux/notifier.h> | |
20 | #include <linux/rtnetlink.h> | |
6bfd4809 | 21 | #include <linux/sched.h> |
2b8c19db | 22 | #include <linux/string.h> |
2b8c19db HX |
23 | |
24 | #include "internal.h" | |
25 | ||
26 | struct cryptomgr_param { | |
ebc610e5 HX |
27 | struct rtattr *tb[CRYPTOA_MAX]; |
28 | ||
29 | struct { | |
30 | struct rtattr attr; | |
31 | struct crypto_attr_type data; | |
32 | } type; | |
33 | ||
2b8c19db HX |
34 | struct { |
35 | struct rtattr attr; | |
36 | struct crypto_attr_alg data; | |
37 | } alg; | |
38 | ||
39 | struct { | |
40 | char name[CRYPTO_MAX_ALG_NAME]; | |
41 | } larval; | |
42 | ||
43 | char template[CRYPTO_MAX_ALG_NAME]; | |
44 | }; | |
45 | ||
cf02f5da | 46 | static int cryptomgr_probe(void *data) |
2b8c19db | 47 | { |
cf02f5da | 48 | struct cryptomgr_param *param = data; |
2b8c19db HX |
49 | struct crypto_template *tmpl; |
50 | struct crypto_instance *inst; | |
6bfd4809 | 51 | int err; |
2b8c19db HX |
52 | |
53 | tmpl = crypto_lookup_template(param->template); | |
54 | if (!tmpl) | |
55 | goto err; | |
56 | ||
6bfd4809 | 57 | do { |
ebc610e5 | 58 | inst = tmpl->alloc(param->tb); |
6bfd4809 HX |
59 | if (IS_ERR(inst)) |
60 | err = PTR_ERR(inst); | |
61 | else if ((err = crypto_register_instance(tmpl, inst))) | |
62 | tmpl->free(inst); | |
63 | } while (err == -EAGAIN && !signal_pending(current)); | |
2b8c19db HX |
64 | |
65 | crypto_tmpl_put(tmpl); | |
66 | ||
6bfd4809 HX |
67 | if (err) |
68 | goto err; | |
69 | ||
2b8c19db HX |
70 | out: |
71 | kfree(param); | |
cf02f5da | 72 | module_put_and_exit(0); |
2b8c19db HX |
73 | |
74 | err: | |
ebc610e5 HX |
75 | crypto_larval_error(param->larval.name, param->type.data.type, |
76 | param->type.data.mask); | |
2b8c19db HX |
77 | goto out; |
78 | } | |
79 | ||
80 | static int cryptomgr_schedule_probe(struct crypto_larval *larval) | |
81 | { | |
1605b847 | 82 | struct task_struct *thread; |
2b8c19db HX |
83 | struct cryptomgr_param *param; |
84 | const char *name = larval->alg.cra_name; | |
85 | const char *p; | |
86 | unsigned int len; | |
87 | ||
cf02f5da HX |
88 | if (!try_module_get(THIS_MODULE)) |
89 | goto err; | |
90 | ||
ebc610e5 | 91 | param = kzalloc(sizeof(*param), GFP_KERNEL); |
2b8c19db | 92 | if (!param) |
cf02f5da | 93 | goto err_put_module; |
2b8c19db HX |
94 | |
95 | for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++) | |
96 | ; | |
97 | ||
98 | len = p - name; | |
99 | if (!len || *p != '(') | |
100 | goto err_free_param; | |
101 | ||
102 | memcpy(param->template, name, len); | |
2b8c19db HX |
103 | |
104 | name = p + 1; | |
cf02f5da HX |
105 | len = 0; |
106 | for (p = name; *p; p++) { | |
107 | for (; isalnum(*p) || *p == '-' || *p == '_' || *p == '('; p++) | |
108 | ; | |
2b8c19db | 109 | |
cf02f5da HX |
110 | if (*p != ')') |
111 | goto err_free_param; | |
112 | ||
113 | len = p - name; | |
114 | } | |
115 | ||
116 | if (!len || name[len + 1]) | |
2b8c19db HX |
117 | goto err_free_param; |
118 | ||
ebc610e5 HX |
119 | param->type.attr.rta_len = sizeof(param->type); |
120 | param->type.attr.rta_type = CRYPTOA_TYPE; | |
121 | param->type.data.type = larval->alg.cra_flags; | |
122 | param->type.data.mask = larval->mask; | |
123 | param->tb[CRYPTOA_TYPE - 1] = ¶m->type.attr; | |
124 | ||
2b8c19db HX |
125 | param->alg.attr.rta_len = sizeof(param->alg); |
126 | param->alg.attr.rta_type = CRYPTOA_ALG; | |
127 | memcpy(param->alg.data.name, name, len); | |
ebc610e5 | 128 | param->tb[CRYPTOA_ALG - 1] = ¶m->alg.attr; |
2b8c19db HX |
129 | |
130 | memcpy(param->larval.name, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME); | |
131 | ||
1605b847 HX |
132 | thread = kthread_run(cryptomgr_probe, param, "cryptomgr"); |
133 | if (IS_ERR(thread)) | |
cf02f5da | 134 | goto err_free_param; |
2b8c19db HX |
135 | |
136 | return NOTIFY_STOP; | |
137 | ||
138 | err_free_param: | |
139 | kfree(param); | |
cf02f5da HX |
140 | err_put_module: |
141 | module_put(THIS_MODULE); | |
2b8c19db HX |
142 | err: |
143 | return NOTIFY_OK; | |
144 | } | |
145 | ||
146 | static int cryptomgr_notify(struct notifier_block *this, unsigned long msg, | |
147 | void *data) | |
148 | { | |
149 | switch (msg) { | |
150 | case CRYPTO_MSG_ALG_REQUEST: | |
151 | return cryptomgr_schedule_probe(data); | |
152 | } | |
153 | ||
154 | return NOTIFY_DONE; | |
155 | } | |
156 | ||
157 | static struct notifier_block cryptomgr_notifier = { | |
158 | .notifier_call = cryptomgr_notify, | |
159 | }; | |
160 | ||
161 | static int __init cryptomgr_init(void) | |
162 | { | |
163 | return crypto_register_notifier(&cryptomgr_notifier); | |
164 | } | |
165 | ||
166 | static void __exit cryptomgr_exit(void) | |
167 | { | |
168 | int err = crypto_unregister_notifier(&cryptomgr_notifier); | |
169 | BUG_ON(err); | |
170 | } | |
171 | ||
172 | module_init(cryptomgr_init); | |
173 | module_exit(cryptomgr_exit); | |
174 | ||
175 | MODULE_LICENSE("GPL"); | |
176 | MODULE_DESCRIPTION("Crypto Algorithm Manager"); |