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