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