[IPSEC]: Allow async algorithms
[linux-2.6-block.git] / net / ipv6 / esp6.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C)2002 USAGI/WIDE Project
1ab1457c 3 *
1da177e4
LT
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
1ab1457c 8 *
1da177e4
LT
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1ab1457c 13 *
1da177e4
LT
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors
19 *
1ab1457c 20 * Mitsuru KANDA @USAGI : IPv6 Support
1da177e4
LT
21 * Kazunori MIYAZAWA @USAGI :
22 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
1ab1457c 23 *
1da177e4
LT
24 * This file is derived from net/ipv4/esp.c
25 */
26
38320c70
HX
27#include <crypto/aead.h>
28#include <crypto/authenc.h>
6b7326c8 29#include <linux/err.h>
1da177e4
LT
30#include <linux/module.h>
31#include <net/ip.h>
32#include <net/xfrm.h>
33#include <net/esp.h>
72998d8c 34#include <linux/scatterlist.h>
a02a6422 35#include <linux/kernel.h>
1da177e4
LT
36#include <linux/pfkeyv2.h>
37#include <linux/random.h>
38320c70 38#include <linux/slab.h>
b7c6538c 39#include <linux/spinlock.h>
1da177e4
LT
40#include <net/icmp.h>
41#include <net/ipv6.h>
14c85021 42#include <net/protocol.h>
1da177e4
LT
43#include <linux/icmpv6.h>
44
38320c70
HX
45struct esp_skb_cb {
46 struct xfrm_skb_cb xfrm;
47 void *tmp;
48};
49
50#define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
51
52/*
53 * Allocate an AEAD request structure with extra space for SG and IV.
54 *
55 * For alignment considerations the IV is placed at the front, followed
56 * by the request and finally the SG list.
57 *
58 * TODO: Use spare space in skb for this where possible.
59 */
60static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags)
61{
62 unsigned int len;
63
64 len = crypto_aead_ivsize(aead);
65 if (len) {
66 len += crypto_aead_alignmask(aead) &
67 ~(crypto_tfm_ctx_alignment() - 1);
68 len = ALIGN(len, crypto_tfm_ctx_alignment());
69 }
70
71 len += sizeof(struct aead_givcrypt_request) + crypto_aead_reqsize(aead);
72 len = ALIGN(len, __alignof__(struct scatterlist));
73
74 len += sizeof(struct scatterlist) * nfrags;
75
76 return kmalloc(len, GFP_ATOMIC);
77}
78
79static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp)
80{
81 return crypto_aead_ivsize(aead) ?
82 PTR_ALIGN((u8 *)tmp, crypto_aead_alignmask(aead) + 1) : tmp;
83}
84
85static inline struct aead_givcrypt_request *esp_tmp_givreq(
86 struct crypto_aead *aead, u8 *iv)
87{
88 struct aead_givcrypt_request *req;
89
90 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
91 crypto_tfm_ctx_alignment());
92 aead_givcrypt_set_tfm(req, aead);
93 return req;
94}
95
96static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
97{
98 struct aead_request *req;
99
100 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
101 crypto_tfm_ctx_alignment());
102 aead_request_set_tfm(req, aead);
103 return req;
104}
105
106static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
107 struct aead_request *req)
108{
109 return (void *)ALIGN((unsigned long)(req + 1) +
110 crypto_aead_reqsize(aead),
111 __alignof__(struct scatterlist));
112}
113
114static inline struct scatterlist *esp_givreq_sg(
115 struct crypto_aead *aead, struct aead_givcrypt_request *req)
116{
117 return (void *)ALIGN((unsigned long)(req + 1) +
118 crypto_aead_reqsize(aead),
119 __alignof__(struct scatterlist));
120}
121
122static void esp_output_done(struct crypto_async_request *base, int err)
123{
124 struct sk_buff *skb = base->data;
125
126 kfree(ESP_SKB_CB(skb)->tmp);
127 xfrm_output_resume(skb, err);
128}
129
1da177e4
LT
130static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
131{
132 int err;
87bdc48d 133 struct ip_esp_hdr *esph;
38320c70
HX
134 struct crypto_aead *aead;
135 struct aead_givcrypt_request *req;
136 struct scatterlist *sg;
137 struct scatterlist *asg;
1da177e4 138 struct sk_buff *trailer;
38320c70 139 void *tmp;
1da177e4
LT
140 int blksize;
141 int clen;
142 int alen;
143 int nfrags;
38320c70 144 u8 *iv;
27a884dc 145 u8 *tail;
ea2ae17d 146 struct esp_data *esp = x->data;
1da177e4 147
7b277b1a 148 /* skb is pure payload to encrypt */
1da177e4
LT
149 err = -ENOMEM;
150
151 /* Round to block size */
152 clen = skb->len;
153
38320c70
HX
154 aead = esp->aead;
155 alen = crypto_aead_authsize(aead);
156
157 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
a02a6422 158 clen = ALIGN(clen + 2, blksize);
38320c70
HX
159 if (esp->padlen)
160 clen = ALIGN(clen, esp->padlen);
1da177e4 161
38320c70 162 if ((err = skb_cow_data(skb, clen - skb->len + alen, &trailer)) < 0)
1da177e4 163 goto error;
38320c70
HX
164 nfrags = err;
165
166 tmp = esp_alloc_tmp(aead, nfrags + 1);
167 if (!tmp)
168 goto error;
169
170 iv = esp_tmp_iv(aead, tmp);
171 req = esp_tmp_givreq(aead, iv);
172 asg = esp_givreq_sg(aead, req);
173 sg = asg + 1;
1da177e4
LT
174
175 /* Fill padding... */
27a884dc 176 tail = skb_tail_pointer(trailer);
1da177e4
LT
177 do {
178 int i;
179 for (i=0; i<clen-skb->len - 2; i++)
27a884dc 180 tail[i] = i + 1;
1da177e4 181 } while (0);
27a884dc 182 tail[clen-skb->len - 2] = (clen - skb->len) - 2;
38320c70
HX
183 tail[clen - skb->len - 1] = *skb_mac_header(skb);
184 pskb_put(skb, trailer, clen - skb->len + alen);
1da177e4 185
7b277b1a 186 skb_push(skb, -skb_network_offset(skb));
87bdc48d 187 esph = ip_esp_hdr(skb);
007f0211 188 *skb_mac_header(skb) = IPPROTO_ESP;
1da177e4
LT
189
190 esph->spi = x->id.spi;
436a0a40 191 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq);
1da177e4 192
38320c70
HX
193 sg_init_table(sg, nfrags);
194 skb_to_sgvec(skb, sg,
195 esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
196 clen + alen);
197 sg_init_one(asg, esph, sizeof(*esph));
b7c6538c 198
38320c70
HX
199 aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
200 aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
201 aead_givcrypt_set_assoc(req, asg, sizeof(*esph));
202 aead_givcrypt_set_giv(req, esph->enc_data, XFRM_SKB_CB(skb)->seq);
1da177e4 203
38320c70
HX
204 ESP_SKB_CB(skb)->tmp = tmp;
205 err = crypto_aead_givencrypt(req);
206 if (err == -EINPROGRESS)
207 goto error;
1da177e4 208
38320c70
HX
209 if (err == -EBUSY)
210 err = NET_XMIT_DROP;
211
212 kfree(tmp);
213
214error:
215 return err;
216}
217
218static int esp_input_done2(struct sk_buff *skb, int err)
219{
220 struct xfrm_state *x = xfrm_input_state(skb);
221 struct esp_data *esp = x->data;
222 struct crypto_aead *aead = esp->aead;
223 int alen = crypto_aead_authsize(aead);
224 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
225 int elen = skb->len - hlen;
226 int hdr_len = skb_network_header_len(skb);
227 int padlen;
228 u8 nexthdr[2];
229
230 kfree(ESP_SKB_CB(skb)->tmp);
1da177e4 231
6b7326c8 232 if (unlikely(err))
38320c70 233 goto out;
6b7326c8 234
38320c70
HX
235 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
236 BUG();
1da177e4 237
38320c70
HX
238 err = -EINVAL;
239 padlen = nexthdr[0];
240 if (padlen + 2 + alen >= elen) {
241 LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage "
242 "padlen=%d, elen=%d\n", padlen + 2, elen - alen);
243 goto out;
1da177e4
LT
244 }
245
38320c70 246 /* ... check padding bits here. Silly. :-) */
b7c6538c 247
38320c70
HX
248 pskb_trim(skb, skb->len - alen - padlen - 2);
249 __skb_pull(skb, hlen);
250 skb_set_transport_header(skb, -hdr_len);
251
252 err = nexthdr[1];
253
254 /* RFC4303: Drop dummy packets without any error */
255 if (err == IPPROTO_NONE)
256 err = -EINVAL;
257
258out:
1da177e4
LT
259 return err;
260}
261
38320c70
HX
262static void esp_input_done(struct crypto_async_request *base, int err)
263{
264 struct sk_buff *skb = base->data;
265
266 xfrm_input_resume(skb, esp_input_done2(skb, err));
267}
268
e695633e 269static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
1da177e4 270{
87bdc48d 271 struct ip_esp_hdr *esph;
1da177e4 272 struct esp_data *esp = x->data;
38320c70
HX
273 struct crypto_aead *aead = esp->aead;
274 struct aead_request *req;
1da177e4 275 struct sk_buff *trailer;
38320c70 276 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
1da177e4 277 int nfrags;
1da177e4 278 int ret = 0;
38320c70
HX
279 void *tmp;
280 u8 *iv;
281 struct scatterlist *sg;
282 struct scatterlist *asg;
1da177e4 283
87bdc48d 284 if (!pskb_may_pull(skb, sizeof(*esph))) {
1da177e4 285 ret = -EINVAL;
31a4ab93 286 goto out;
1da177e4
LT
287 }
288
38320c70 289 if (elen <= 0) {
1da177e4 290 ret = -EINVAL;
31a4ab93 291 goto out;
1da177e4 292 }
1da177e4 293
0ebea8ef
HX
294 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
295 ret = -EINVAL;
296 goto out;
297 }
298
38320c70
HX
299 ret = -ENOMEM;
300 tmp = esp_alloc_tmp(aead, nfrags + 1);
301 if (!tmp)
302 goto out;
1da177e4 303
38320c70
HX
304 ESP_SKB_CB(skb)->tmp = tmp;
305 iv = esp_tmp_iv(aead, tmp);
306 req = esp_tmp_req(aead, iv);
307 asg = esp_req_sg(aead, req);
308 sg = asg + 1;
1da177e4 309
38320c70 310 skb->ip_summed = CHECKSUM_NONE;
1da177e4 311
87bdc48d 312 esph = (struct ip_esp_hdr *)skb->data;
1da177e4
LT
313
314 /* Get ivec. This can be wrong, check against another impls. */
38320c70 315 iv = esph->enc_data;
0ebea8ef 316
38320c70
HX
317 sg_init_table(sg, nfrags);
318 skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
319 sg_init_one(asg, esph, sizeof(*esph));
0ebea8ef 320
38320c70
HX
321 aead_request_set_callback(req, 0, esp_input_done, skb);
322 aead_request_set_crypt(req, sg, sg, elen, iv);
323 aead_request_set_assoc(req, asg, sizeof(*esph));
1da177e4 324
38320c70
HX
325 ret = crypto_aead_decrypt(req);
326 if (ret == -EINPROGRESS)
327 goto out;
95a02cfd 328
38320c70 329 ret = esp_input_done2(skb, ret);
1da177e4
LT
330
331out:
1da177e4
LT
332 return ret;
333}
334
c5c25238 335static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
1da177e4
LT
336{
337 struct esp_data *esp = x->data;
38320c70
HX
338 u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
339 u32 align = max_t(u32, blksize, esp->padlen);
c5c25238 340 u32 rem;
1da177e4 341
38320c70 342 mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
c5c25238
PM
343 rem = mtu & (align - 1);
344 mtu &= ~(align - 1);
345
346 if (x->props.mode != XFRM_MODE_TUNNEL) {
d4875b04 347 u32 padsize = ((blksize - 1) & 7) + 1;
c5c25238
PM
348 mtu -= blksize - padsize;
349 mtu += min_t(u32, blksize - padsize, rem);
1da177e4 350 }
1da177e4 351
c5c25238 352 return mtu - 2;
1da177e4
LT
353}
354
355static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
1ab1457c 356 int type, int code, int offset, __be32 info)
1da177e4
LT
357{
358 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
87bdc48d 359 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
1da177e4
LT
360 struct xfrm_state *x;
361
1ab1457c 362 if (type != ICMPV6_DEST_UNREACH &&
1da177e4
LT
363 type != ICMPV6_PKT_TOOBIG)
364 return;
365
366 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
367 if (!x)
368 return;
1ab1457c 369 printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/" NIP6_FMT "\n",
1da177e4
LT
370 ntohl(esph->spi), NIP6(iph->daddr));
371 xfrm_state_put(x);
372}
373
374static void esp6_destroy(struct xfrm_state *x)
375{
376 struct esp_data *esp = x->data;
377
378 if (!esp)
379 return;
380
38320c70 381 crypto_free_aead(esp->aead);
1da177e4
LT
382 kfree(esp);
383}
384
72cb6962 385static int esp6_init_state(struct xfrm_state *x)
1da177e4
LT
386{
387 struct esp_data *esp = NULL;
38320c70
HX
388 struct crypto_aead *aead;
389 struct crypto_authenc_key_param *param;
390 struct rtattr *rta;
391 char *key;
392 char *p;
393 char authenc_name[CRYPTO_MAX_ALG_NAME];
394 u32 align;
395 unsigned int keylen;
396 int err;
1da177e4 397
1da177e4 398 if (x->ealg == NULL)
38320c70 399 return -EINVAL;
1da177e4
LT
400
401 if (x->encap)
38320c70
HX
402 return -EINVAL;
403
404 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
405 x->aalg ? x->aalg->alg_name : "digest_null",
406 x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
407 return -ENAMETOOLONG;
1da177e4 408
0c600eda 409 esp = kzalloc(sizeof(*esp), GFP_KERNEL);
1da177e4
LT
410 if (esp == NULL)
411 return -ENOMEM;
412
38320c70
HX
413 x->data = esp;
414
415 aead = crypto_alloc_aead(authenc_name, 0, 0);
416 err = PTR_ERR(aead);
417 if (IS_ERR(aead))
418 goto error;
419
420 esp->aead = aead;
421
422 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
423 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
424 err = -ENOMEM;
425 key = kmalloc(keylen, GFP_KERNEL);
426 if (!key)
427 goto error;
428
429 p = key;
430 rta = (void *)p;
431 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
432 rta->rta_len = RTA_LENGTH(sizeof(*param));
433 param = RTA_DATA(rta);
434 p += RTA_SPACE(sizeof(*param));
435
1da177e4
LT
436 if (x->aalg) {
437 struct xfrm_algo_desc *aalg_desc;
438
38320c70
HX
439 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
440 p += (x->aalg->alg_key_len + 7) / 8;
1ab1457c 441
1da177e4
LT
442 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
443 BUG_ON(!aalg_desc);
1ab1457c 444
38320c70 445 err = -EINVAL;
1da177e4 446 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
38320c70 447 crypto_aead_authsize(aead)) {
07d4ee58
HX
448 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
449 x->aalg->alg_name,
38320c70 450 crypto_aead_authsize(aead),
07d4ee58 451 aalg_desc->uinfo.auth.icv_fullbits/8);
38320c70 452 goto free_key;
1da177e4 453 }
1ab1457c 454
38320c70
HX
455 err = crypto_aead_setauthsize(
456 aead, aalg_desc->uinfo.auth.icv_truncbits / 8);
457 if (err)
458 goto free_key;
1da177e4 459 }
38320c70
HX
460
461 esp->padlen = 0;
462
463 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
464 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
465
466 err = crypto_aead_setkey(aead, key, keylen);
467
468free_key:
469 kfree(key);
470
471 if (err)
1da177e4 472 goto error;
38320c70
HX
473
474 x->props.header_len = sizeof(struct ip_esp_hdr) +
475 crypto_aead_ivsize(aead);
ca68145f
HX
476 switch (x->props.mode) {
477 case XFRM_MODE_BEET:
478 case XFRM_MODE_TRANSPORT:
479 break;
480 case XFRM_MODE_TUNNEL:
1da177e4 481 x->props.header_len += sizeof(struct ipv6hdr);
ea2c47b4 482 break;
ca68145f
HX
483 default:
484 goto error;
485 }
38320c70
HX
486
487 align = ALIGN(crypto_aead_blocksize(aead), 4);
488 if (esp->padlen)
489 align = max_t(u32, align, esp->padlen);
490 x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
1da177e4
LT
491
492error:
38320c70 493 return err;
1da177e4
LT
494}
495
496static struct xfrm_type esp6_type =
497{
498 .description = "ESP6",
499 .owner = THIS_MODULE,
500 .proto = IPPROTO_ESP,
436a0a40 501 .flags = XFRM_TYPE_REPLAY_PROT,
1da177e4
LT
502 .init_state = esp6_init_state,
503 .destructor = esp6_destroy,
c5c25238 504 .get_mtu = esp6_get_mtu,
1da177e4 505 .input = esp6_input,
aee5adb4
MN
506 .output = esp6_output,
507 .hdr_offset = xfrm6_find_1stfragopt,
1da177e4
LT
508};
509
510static struct inet6_protocol esp6_protocol = {
511 .handler = xfrm6_rcv,
512 .err_handler = esp6_err,
513 .flags = INET6_PROTO_NOPOLICY,
514};
515
516static int __init esp6_init(void)
517{
518 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
519 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
520 return -EAGAIN;
521 }
522 if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
523 printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
524 xfrm_unregister_type(&esp6_type, AF_INET6);
525 return -EAGAIN;
526 }
527
528 return 0;
529}
530
531static void __exit esp6_fini(void)
532{
533 if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
534 printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
535 if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
536 printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
537}
538
539module_init(esp6_init);
540module_exit(esp6_fini);
541
542MODULE_LICENSE("GPL");
d3d6dd3a 543MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);