YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * xfrm algorithm interface |
| 3 | * |
| 4 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.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 |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 8 | * Software Foundation; either version 2 of the License, or (at your option) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * any later version. |
| 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/pfkeyv2.h> |
| 15 | #include <linux/crypto.h> |
Heiko Carstens | b3b724f | 2007-10-23 09:28:34 +0200 | [diff] [blame] | 16 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <net/xfrm.h> |
| 18 | #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE) |
| 19 | #include <net/ah.h> |
| 20 | #endif |
| 21 | #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE) |
| 22 | #include <net/esp.h> |
| 23 | #endif |
| 24 | #include <asm/scatterlist.h> |
| 25 | |
| 26 | /* |
| 27 | * Algorithms supported by IPsec. These entries contain properties which |
| 28 | * are used in key negotiation and xfrm processing, and are used to verify |
| 29 | * that instantiated crypto transforms have correct parameters for IPsec |
| 30 | * purposes. |
| 31 | */ |
| 32 | static struct xfrm_algo_desc aalg_list[] = { |
| 33 | { |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 34 | .name = "hmac(digest_null)", |
| 35 | .compat = "digest_null", |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | .uinfo = { |
| 38 | .auth = { |
| 39 | .icv_truncbits = 0, |
| 40 | .icv_fullbits = 0, |
| 41 | } |
| 42 | }, |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 43 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | .desc = { |
| 45 | .sadb_alg_id = SADB_X_AALG_NULL, |
| 46 | .sadb_alg_ivlen = 0, |
| 47 | .sadb_alg_minbits = 0, |
| 48 | .sadb_alg_maxbits = 0 |
| 49 | } |
| 50 | }, |
| 51 | { |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 52 | .name = "hmac(md5)", |
| 53 | .compat = "md5", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | .uinfo = { |
| 56 | .auth = { |
| 57 | .icv_truncbits = 96, |
| 58 | .icv_fullbits = 128, |
| 59 | } |
| 60 | }, |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | .desc = { |
| 63 | .sadb_alg_id = SADB_AALG_MD5HMAC, |
| 64 | .sadb_alg_ivlen = 0, |
| 65 | .sadb_alg_minbits = 128, |
| 66 | .sadb_alg_maxbits = 128 |
| 67 | } |
| 68 | }, |
| 69 | { |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 70 | .name = "hmac(sha1)", |
| 71 | .compat = "sha1", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | .uinfo = { |
| 74 | .auth = { |
| 75 | .icv_truncbits = 96, |
| 76 | .icv_fullbits = 160, |
| 77 | } |
| 78 | }, |
| 79 | |
| 80 | .desc = { |
| 81 | .sadb_alg_id = SADB_AALG_SHA1HMAC, |
| 82 | .sadb_alg_ivlen = 0, |
| 83 | .sadb_alg_minbits = 160, |
| 84 | .sadb_alg_maxbits = 160 |
| 85 | } |
| 86 | }, |
| 87 | { |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 88 | .name = "hmac(sha256)", |
| 89 | .compat = "sha256", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | .uinfo = { |
| 92 | .auth = { |
| 93 | .icv_truncbits = 96, |
| 94 | .icv_fullbits = 256, |
| 95 | } |
| 96 | }, |
| 97 | |
| 98 | .desc = { |
| 99 | .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC, |
| 100 | .sadb_alg_ivlen = 0, |
| 101 | .sadb_alg_minbits = 256, |
| 102 | .sadb_alg_maxbits = 256 |
| 103 | } |
| 104 | }, |
| 105 | { |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 106 | .name = "hmac(ripemd160)", |
| 107 | .compat = "ripemd160", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | .uinfo = { |
| 110 | .auth = { |
| 111 | .icv_truncbits = 96, |
| 112 | .icv_fullbits = 160, |
| 113 | } |
| 114 | }, |
| 115 | |
| 116 | .desc = { |
| 117 | .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC, |
| 118 | .sadb_alg_ivlen = 0, |
| 119 | .sadb_alg_minbits = 160, |
| 120 | .sadb_alg_maxbits = 160 |
| 121 | } |
| 122 | }, |
Kazunori MIYAZAWA | 7cf4c1a | 2006-10-28 13:21:22 +1000 | [diff] [blame] | 123 | { |
| 124 | .name = "xcbc(aes)", |
| 125 | |
| 126 | .uinfo = { |
| 127 | .auth = { |
| 128 | .icv_truncbits = 96, |
| 129 | .icv_fullbits = 128, |
| 130 | } |
| 131 | }, |
| 132 | |
| 133 | .desc = { |
| 134 | .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC, |
| 135 | .sadb_alg_ivlen = 0, |
| 136 | .sadb_alg_minbits = 128, |
| 137 | .sadb_alg_maxbits = 128 |
| 138 | } |
| 139 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static struct xfrm_algo_desc ealg_list[] = { |
| 143 | { |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 144 | .name = "ecb(cipher_null)", |
| 145 | .compat = "cipher_null", |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | .uinfo = { |
| 148 | .encr = { |
| 149 | .blockbits = 8, |
| 150 | .defkeybits = 0, |
| 151 | } |
| 152 | }, |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 153 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | .desc = { |
| 155 | .sadb_alg_id = SADB_EALG_NULL, |
| 156 | .sadb_alg_ivlen = 0, |
| 157 | .sadb_alg_minbits = 0, |
| 158 | .sadb_alg_maxbits = 0 |
| 159 | } |
| 160 | }, |
| 161 | { |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 162 | .name = "cbc(des)", |
| 163 | .compat = "des", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | .uinfo = { |
| 166 | .encr = { |
| 167 | .blockbits = 64, |
| 168 | .defkeybits = 64, |
| 169 | } |
| 170 | }, |
| 171 | |
| 172 | .desc = { |
| 173 | .sadb_alg_id = SADB_EALG_DESCBC, |
| 174 | .sadb_alg_ivlen = 8, |
| 175 | .sadb_alg_minbits = 64, |
| 176 | .sadb_alg_maxbits = 64 |
| 177 | } |
| 178 | }, |
| 179 | { |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 180 | .name = "cbc(des3_ede)", |
| 181 | .compat = "des3_ede", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | .uinfo = { |
| 184 | .encr = { |
| 185 | .blockbits = 64, |
| 186 | .defkeybits = 192, |
| 187 | } |
| 188 | }, |
| 189 | |
| 190 | .desc = { |
| 191 | .sadb_alg_id = SADB_EALG_3DESCBC, |
| 192 | .sadb_alg_ivlen = 8, |
| 193 | .sadb_alg_minbits = 192, |
| 194 | .sadb_alg_maxbits = 192 |
| 195 | } |
| 196 | }, |
| 197 | { |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 198 | .name = "cbc(cast128)", |
| 199 | .compat = "cast128", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
| 201 | .uinfo = { |
| 202 | .encr = { |
| 203 | .blockbits = 64, |
| 204 | .defkeybits = 128, |
| 205 | } |
| 206 | }, |
| 207 | |
| 208 | .desc = { |
| 209 | .sadb_alg_id = SADB_X_EALG_CASTCBC, |
| 210 | .sadb_alg_ivlen = 8, |
| 211 | .sadb_alg_minbits = 40, |
| 212 | .sadb_alg_maxbits = 128 |
| 213 | } |
| 214 | }, |
| 215 | { |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 216 | .name = "cbc(blowfish)", |
| 217 | .compat = "blowfish", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | .uinfo = { |
| 220 | .encr = { |
| 221 | .blockbits = 64, |
| 222 | .defkeybits = 128, |
| 223 | } |
| 224 | }, |
| 225 | |
| 226 | .desc = { |
| 227 | .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC, |
| 228 | .sadb_alg_ivlen = 8, |
| 229 | .sadb_alg_minbits = 40, |
| 230 | .sadb_alg_maxbits = 448 |
| 231 | } |
| 232 | }, |
| 233 | { |
Herbert Xu | 6b7326c | 2006-07-30 15:41:01 +1000 | [diff] [blame] | 234 | .name = "cbc(aes)", |
| 235 | .compat = "aes", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | .uinfo = { |
| 238 | .encr = { |
| 239 | .blockbits = 128, |
| 240 | .defkeybits = 128, |
| 241 | } |
| 242 | }, |
| 243 | |
| 244 | .desc = { |
| 245 | .sadb_alg_id = SADB_X_EALG_AESCBC, |
| 246 | .sadb_alg_ivlen = 8, |
| 247 | .sadb_alg_minbits = 128, |
| 248 | .sadb_alg_maxbits = 256 |
| 249 | } |
| 250 | }, |
| 251 | { |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 252 | .name = "cbc(serpent)", |
| 253 | .compat = "serpent", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 255 | .uinfo = { |
| 256 | .encr = { |
| 257 | .blockbits = 128, |
| 258 | .defkeybits = 128, |
| 259 | } |
| 260 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 262 | .desc = { |
| 263 | .sadb_alg_id = SADB_X_EALG_SERPENTCBC, |
| 264 | .sadb_alg_ivlen = 8, |
| 265 | .sadb_alg_minbits = 128, |
| 266 | .sadb_alg_maxbits = 256, |
| 267 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | }, |
| 269 | { |
Noriaki TAKAMIYA | 6a0dc8d | 2006-10-22 15:05:57 +1000 | [diff] [blame] | 270 | .name = "cbc(camellia)", |
| 271 | |
| 272 | .uinfo = { |
| 273 | .encr = { |
| 274 | .blockbits = 128, |
| 275 | .defkeybits = 128, |
| 276 | } |
| 277 | }, |
| 278 | |
| 279 | .desc = { |
| 280 | .sadb_alg_id = SADB_X_EALG_CAMELLIACBC, |
| 281 | .sadb_alg_ivlen = 8, |
| 282 | .sadb_alg_minbits = 128, |
| 283 | .sadb_alg_maxbits = 256 |
| 284 | } |
| 285 | }, |
| 286 | { |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 287 | .name = "cbc(twofish)", |
| 288 | .compat = "twofish", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 290 | .uinfo = { |
| 291 | .encr = { |
| 292 | .blockbits = 128, |
| 293 | .defkeybits = 128, |
| 294 | } |
| 295 | }, |
| 296 | |
| 297 | .desc = { |
| 298 | .sadb_alg_id = SADB_X_EALG_TWOFISHCBC, |
| 299 | .sadb_alg_ivlen = 8, |
| 300 | .sadb_alg_minbits = 128, |
| 301 | .sadb_alg_maxbits = 256 |
| 302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | }, |
| 304 | }; |
| 305 | |
| 306 | static struct xfrm_algo_desc calg_list[] = { |
| 307 | { |
| 308 | .name = "deflate", |
| 309 | .uinfo = { |
| 310 | .comp = { |
| 311 | .threshold = 90, |
| 312 | } |
| 313 | }, |
| 314 | .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE } |
| 315 | }, |
| 316 | { |
| 317 | .name = "lzs", |
| 318 | .uinfo = { |
| 319 | .comp = { |
| 320 | .threshold = 90, |
| 321 | } |
| 322 | }, |
| 323 | .desc = { .sadb_alg_id = SADB_X_CALG_LZS } |
| 324 | }, |
| 325 | { |
| 326 | .name = "lzjh", |
| 327 | .uinfo = { |
| 328 | .comp = { |
| 329 | .threshold = 50, |
| 330 | } |
| 331 | }, |
| 332 | .desc = { .sadb_alg_id = SADB_X_CALG_LZJH } |
| 333 | }, |
| 334 | }; |
| 335 | |
| 336 | static inline int aalg_entries(void) |
| 337 | { |
| 338 | return ARRAY_SIZE(aalg_list); |
| 339 | } |
| 340 | |
| 341 | static inline int ealg_entries(void) |
| 342 | { |
| 343 | return ARRAY_SIZE(ealg_list); |
| 344 | } |
| 345 | |
| 346 | static inline int calg_entries(void) |
| 347 | { |
| 348 | return ARRAY_SIZE(calg_list); |
| 349 | } |
| 350 | |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 351 | struct xfrm_algo_list { |
| 352 | struct xfrm_algo_desc *algs; |
| 353 | int entries; |
| 354 | u32 type; |
| 355 | u32 mask; |
| 356 | }; |
| 357 | |
| 358 | static const struct xfrm_algo_list xfrm_aalg_list = { |
| 359 | .algs = aalg_list, |
| 360 | .entries = ARRAY_SIZE(aalg_list), |
| 361 | .type = CRYPTO_ALG_TYPE_HASH, |
| 362 | .mask = CRYPTO_ALG_TYPE_HASH_MASK | CRYPTO_ALG_ASYNC, |
| 363 | }; |
| 364 | |
| 365 | static const struct xfrm_algo_list xfrm_ealg_list = { |
| 366 | .algs = ealg_list, |
| 367 | .entries = ARRAY_SIZE(ealg_list), |
| 368 | .type = CRYPTO_ALG_TYPE_BLKCIPHER, |
| 369 | .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC, |
| 370 | }; |
| 371 | |
| 372 | static const struct xfrm_algo_list xfrm_calg_list = { |
| 373 | .algs = calg_list, |
| 374 | .entries = ARRAY_SIZE(calg_list), |
| 375 | .type = CRYPTO_ALG_TYPE_COMPRESS, |
| 376 | .mask = CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_ASYNC, |
| 377 | }; |
| 378 | |
| 379 | static struct xfrm_algo_desc *xfrm_find_algo( |
| 380 | const struct xfrm_algo_list *algo_list, |
| 381 | int match(const struct xfrm_algo_desc *entry, const void *data), |
| 382 | const void *data, int probe) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 384 | struct xfrm_algo_desc *list = algo_list->algs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | int i, status; |
| 386 | |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 387 | for (i = 0; i < algo_list->entries; i++) { |
| 388 | if (!match(list + i, data)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | continue; |
| 390 | |
| 391 | if (list[i].available) |
| 392 | return &list[i]; |
| 393 | |
| 394 | if (!probe) |
| 395 | break; |
| 396 | |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 397 | status = crypto_has_alg(list[i].name, algo_list->type, |
| 398 | algo_list->mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | if (!status) |
| 400 | break; |
| 401 | |
| 402 | list[i].available = status; |
| 403 | return &list[i]; |
| 404 | } |
| 405 | return NULL; |
| 406 | } |
| 407 | |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 408 | static int xfrm_alg_id_match(const struct xfrm_algo_desc *entry, |
| 409 | const void *data) |
| 410 | { |
Herbert Xu | 26b8e51 | 2007-05-22 16:12:26 -0700 | [diff] [blame] | 411 | return entry->desc.sadb_alg_id == (unsigned long)data; |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id) |
| 415 | { |
| 416 | return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_id_match, |
Herbert Xu | 26b8e51 | 2007-05-22 16:12:26 -0700 | [diff] [blame] | 417 | (void *)(unsigned long)alg_id, 1); |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 418 | } |
| 419 | EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid); |
| 420 | |
| 421 | struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id) |
| 422 | { |
| 423 | return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_id_match, |
Herbert Xu | 26b8e51 | 2007-05-22 16:12:26 -0700 | [diff] [blame] | 424 | (void *)(unsigned long)alg_id, 1); |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 425 | } |
| 426 | EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid); |
| 427 | |
| 428 | struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id) |
| 429 | { |
| 430 | return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_id_match, |
Herbert Xu | 26b8e51 | 2007-05-22 16:12:26 -0700 | [diff] [blame] | 431 | (void *)(unsigned long)alg_id, 1); |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 432 | } |
| 433 | EXPORT_SYMBOL_GPL(xfrm_calg_get_byid); |
| 434 | |
| 435 | static int xfrm_alg_name_match(const struct xfrm_algo_desc *entry, |
| 436 | const void *data) |
| 437 | { |
| 438 | const char *name = data; |
| 439 | |
| 440 | return name && (!strcmp(name, entry->name) || |
| 441 | (entry->compat && !strcmp(name, entry->compat))); |
| 442 | } |
| 443 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe) |
| 445 | { |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 446 | return xfrm_find_algo(&xfrm_aalg_list, xfrm_alg_name_match, name, |
| 447 | probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname); |
| 450 | |
| 451 | struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe) |
| 452 | { |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 453 | return xfrm_find_algo(&xfrm_ealg_list, xfrm_alg_name_match, name, |
| 454 | probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname); |
| 457 | |
| 458 | struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe) |
| 459 | { |
Herbert Xu | c92b3a2 | 2007-05-19 14:21:18 -0700 | [diff] [blame] | 460 | return xfrm_find_algo(&xfrm_calg_list, xfrm_alg_name_match, name, |
| 461 | probe); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | EXPORT_SYMBOL_GPL(xfrm_calg_get_byname); |
| 464 | |
| 465 | struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx) |
| 466 | { |
| 467 | if (idx >= aalg_entries()) |
| 468 | return NULL; |
| 469 | |
| 470 | return &aalg_list[idx]; |
| 471 | } |
| 472 | EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx); |
| 473 | |
| 474 | struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx) |
| 475 | { |
| 476 | if (idx >= ealg_entries()) |
| 477 | return NULL; |
| 478 | |
| 479 | return &ealg_list[idx]; |
| 480 | } |
| 481 | EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx); |
| 482 | |
| 483 | /* |
| 484 | * Probe for the availability of crypto algorithms, and set the available |
| 485 | * flag for any algorithms found on the system. This is typically called by |
| 486 | * pfkey during userspace SA add, update or register. |
| 487 | */ |
| 488 | void xfrm_probe_algs(void) |
| 489 | { |
| 490 | #ifdef CONFIG_CRYPTO |
| 491 | int i, status; |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 492 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | BUG_ON(in_softirq()); |
| 494 | |
| 495 | for (i = 0; i < aalg_entries(); i++) { |
Herbert Xu | e4d5b79 | 2006-08-26 18:12:40 +1000 | [diff] [blame] | 496 | status = crypto_has_hash(aalg_list[i].name, 0, |
| 497 | CRYPTO_ALG_ASYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | if (aalg_list[i].available != status) |
| 499 | aalg_list[i].available = status; |
| 500 | } |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | for (i = 0; i < ealg_entries(); i++) { |
Herbert Xu | e4d5b79 | 2006-08-26 18:12:40 +1000 | [diff] [blame] | 503 | status = crypto_has_blkcipher(ealg_list[i].name, 0, |
| 504 | CRYPTO_ALG_ASYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | if (ealg_list[i].available != status) |
| 506 | ealg_list[i].available = status; |
| 507 | } |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | for (i = 0; i < calg_entries(); i++) { |
Herbert Xu | e4d5b79 | 2006-08-26 18:12:40 +1000 | [diff] [blame] | 510 | status = crypto_has_comp(calg_list[i].name, 0, |
| 511 | CRYPTO_ALG_ASYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (calg_list[i].available != status) |
| 513 | calg_list[i].available = status; |
| 514 | } |
| 515 | #endif |
| 516 | } |
| 517 | EXPORT_SYMBOL_GPL(xfrm_probe_algs); |
| 518 | |
| 519 | int xfrm_count_auth_supported(void) |
| 520 | { |
| 521 | int i, n; |
| 522 | |
| 523 | for (i = 0, n = 0; i < aalg_entries(); i++) |
| 524 | if (aalg_list[i].available) |
| 525 | n++; |
| 526 | return n; |
| 527 | } |
| 528 | EXPORT_SYMBOL_GPL(xfrm_count_auth_supported); |
| 529 | |
| 530 | int xfrm_count_enc_supported(void) |
| 531 | { |
| 532 | int i, n; |
| 533 | |
| 534 | for (i = 0, n = 0; i < ealg_entries(); i++) |
| 535 | if (ealg_list[i].available) |
| 536 | n++; |
| 537 | return n; |
| 538 | } |
| 539 | EXPORT_SYMBOL_GPL(xfrm_count_enc_supported); |
| 540 | |
| 541 | /* Move to common area: it is shared with AH. */ |
| 542 | |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 543 | int skb_icv_walk(const struct sk_buff *skb, struct hash_desc *desc, |
| 544 | int offset, int len, icv_update_fn_t icv_update) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 546 | int start = skb_headlen(skb); |
| 547 | int i, copy = start - offset; |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 548 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | struct scatterlist sg; |
| 550 | |
| 551 | /* Checksum header. */ |
| 552 | if (copy > 0) { |
| 553 | if (copy > len) |
| 554 | copy = len; |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 555 | |
David S. Miller | 0e0940d | 2007-10-26 00:39:27 -0700 | [diff] [blame] | 556 | sg_init_one(&sg, skb->data + offset, copy); |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 557 | |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 558 | err = icv_update(desc, &sg, copy); |
| 559 | if (unlikely(err)) |
| 560 | return err; |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | if ((len -= copy) == 0) |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 563 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | offset += copy; |
| 565 | } |
| 566 | |
| 567 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 568 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 570 | BUG_TRAP(start <= offset + len); |
| 571 | |
| 572 | end = start + skb_shinfo(skb)->frags[i].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | if ((copy = end - offset) > 0) { |
| 574 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 575 | |
| 576 | if (copy > len) |
| 577 | copy = len; |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 578 | |
David S. Miller | 0e0940d | 2007-10-26 00:39:27 -0700 | [diff] [blame] | 579 | sg_init_table(&sg, 1); |
Jens Axboe | 642f149 | 2007-10-24 11:20:47 +0200 | [diff] [blame] | 580 | sg_set_page(&sg, frag->page, copy, |
David S. Miller | 0e0940d | 2007-10-26 00:39:27 -0700 | [diff] [blame] | 581 | frag->page_offset + offset-start); |
YOSHIFUJI Hideaki | a716c11 | 2007-02-09 23:25:29 +0900 | [diff] [blame] | 582 | |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 583 | err = icv_update(desc, &sg, copy); |
| 584 | if (unlikely(err)) |
| 585 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | |
| 587 | if (!(len -= copy)) |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 588 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | offset += copy; |
| 590 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 591 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | if (skb_shinfo(skb)->frag_list) { |
| 595 | struct sk_buff *list = skb_shinfo(skb)->frag_list; |
| 596 | |
| 597 | for (; list; list = list->next) { |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 598 | int end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 600 | BUG_TRAP(start <= offset + len); |
| 601 | |
| 602 | end = start + list->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | if ((copy = end - offset) > 0) { |
| 604 | if (copy > len) |
| 605 | copy = len; |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 606 | err = skb_icv_walk(list, desc, offset-start, |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 607 | copy, icv_update); |
| 608 | if (unlikely(err)) |
| 609 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if ((len -= copy) == 0) |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 611 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | offset += copy; |
| 613 | } |
David S. Miller | 1a028e5 | 2007-04-27 15:21:23 -0700 | [diff] [blame] | 614 | start = end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | } |
Kris Katterjohn | 09a6266 | 2006-01-08 22:24:28 -0800 | [diff] [blame] | 617 | BUG_ON(len); |
Herbert Xu | 07d4ee5 | 2006-08-20 14:24:50 +1000 | [diff] [blame] | 618 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | EXPORT_SYMBOL_GPL(skb_icv_walk); |
| 621 | |
| 622 | #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE) |
| 623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len) |
| 625 | { |
| 626 | if (tail != skb) { |
| 627 | skb->data_len += len; |
| 628 | skb->len += len; |
| 629 | } |
| 630 | return skb_put(tail, len); |
| 631 | } |
| 632 | EXPORT_SYMBOL_GPL(pskb_put); |
| 633 | #endif |