Staging: hv: hv_mouse: use a real struct hid_descriptor
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / esp6.c
blob1b5c9825743b6dd45731782a254d4e3595a5cb2b
1 /*
2 * Copyright (C)2002 USAGI/WIDE Project
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.
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.
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
18 * Authors
20 * Mitsuru KANDA @USAGI : IPv6 Support
21 * Kazunori MIYAZAWA @USAGI :
22 * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
24 * This file is derived from net/ipv4/esp.c
27 #include <crypto/aead.h>
28 #include <crypto/authenc.h>
29 #include <linux/err.h>
30 #include <linux/module.h>
31 #include <net/ip.h>
32 #include <net/xfrm.h>
33 #include <net/esp.h>
34 #include <linux/scatterlist.h>
35 #include <linux/kernel.h>
36 #include <linux/pfkeyv2.h>
37 #include <linux/random.h>
38 #include <linux/slab.h>
39 #include <linux/spinlock.h>
40 #include <net/icmp.h>
41 #include <net/ipv6.h>
42 #include <net/protocol.h>
43 #include <linux/icmpv6.h>
45 struct esp_skb_cb {
46 struct xfrm_skb_cb xfrm;
47 void *tmp;
50 #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
52 static u32 esp6_get_mtu(struct xfrm_state *x, int mtu);
55 * Allocate an AEAD request structure with extra space for SG and IV.
57 * For alignment considerations the IV is placed at the front, followed
58 * by the request and finally the SG list.
60 * TODO: Use spare space in skb for this where possible.
62 static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags)
64 unsigned int len;
66 len = crypto_aead_ivsize(aead);
67 if (len) {
68 len += crypto_aead_alignmask(aead) &
69 ~(crypto_tfm_ctx_alignment() - 1);
70 len = ALIGN(len, crypto_tfm_ctx_alignment());
73 len += sizeof(struct aead_givcrypt_request) + crypto_aead_reqsize(aead);
74 len = ALIGN(len, __alignof__(struct scatterlist));
76 len += sizeof(struct scatterlist) * nfrags;
78 return kmalloc(len, GFP_ATOMIC);
81 static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp)
83 return crypto_aead_ivsize(aead) ?
84 PTR_ALIGN((u8 *)tmp, crypto_aead_alignmask(aead) + 1) : tmp;
87 static inline struct aead_givcrypt_request *esp_tmp_givreq(
88 struct crypto_aead *aead, u8 *iv)
90 struct aead_givcrypt_request *req;
92 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
93 crypto_tfm_ctx_alignment());
94 aead_givcrypt_set_tfm(req, aead);
95 return req;
98 static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
100 struct aead_request *req;
102 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
103 crypto_tfm_ctx_alignment());
104 aead_request_set_tfm(req, aead);
105 return req;
108 static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
109 struct aead_request *req)
111 return (void *)ALIGN((unsigned long)(req + 1) +
112 crypto_aead_reqsize(aead),
113 __alignof__(struct scatterlist));
116 static inline struct scatterlist *esp_givreq_sg(
117 struct crypto_aead *aead, struct aead_givcrypt_request *req)
119 return (void *)ALIGN((unsigned long)(req + 1) +
120 crypto_aead_reqsize(aead),
121 __alignof__(struct scatterlist));
124 static void esp_output_done(struct crypto_async_request *base, int err)
126 struct sk_buff *skb = base->data;
128 kfree(ESP_SKB_CB(skb)->tmp);
129 xfrm_output_resume(skb, err);
132 static int esp6_output(struct xfrm_state *x, struct sk_buff *skb)
134 int err;
135 struct ip_esp_hdr *esph;
136 struct crypto_aead *aead;
137 struct aead_givcrypt_request *req;
138 struct scatterlist *sg;
139 struct scatterlist *asg;
140 struct sk_buff *trailer;
141 void *tmp;
142 int blksize;
143 int clen;
144 int alen;
145 int plen;
146 int tfclen;
147 int nfrags;
148 u8 *iv;
149 u8 *tail;
150 struct esp_data *esp = x->data;
152 /* skb is pure payload to encrypt */
153 err = -ENOMEM;
155 aead = esp->aead;
156 alen = crypto_aead_authsize(aead);
158 tfclen = 0;
159 if (x->tfcpad) {
160 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
161 u32 padto;
163 padto = min(x->tfcpad, esp6_get_mtu(x, dst->child_mtu_cached));
164 if (skb->len < padto)
165 tfclen = padto - skb->len;
167 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
168 clen = ALIGN(skb->len + 2 + tfclen, blksize);
169 if (esp->padlen)
170 clen = ALIGN(clen, esp->padlen);
171 plen = clen - skb->len - tfclen;
173 err = skb_cow_data(skb, tfclen + plen + alen, &trailer);
174 if (err < 0)
175 goto error;
176 nfrags = err;
178 tmp = esp_alloc_tmp(aead, nfrags + 1);
179 if (!tmp)
180 goto error;
182 iv = esp_tmp_iv(aead, tmp);
183 req = esp_tmp_givreq(aead, iv);
184 asg = esp_givreq_sg(aead, req);
185 sg = asg + 1;
187 /* Fill padding... */
188 tail = skb_tail_pointer(trailer);
189 if (tfclen) {
190 memset(tail, 0, tfclen);
191 tail += tfclen;
193 do {
194 int i;
195 for (i = 0; i < plen - 2; i++)
196 tail[i] = i + 1;
197 } while (0);
198 tail[plen - 2] = plen - 2;
199 tail[plen - 1] = *skb_mac_header(skb);
200 pskb_put(skb, trailer, clen - skb->len + alen);
202 skb_push(skb, -skb_network_offset(skb));
203 esph = ip_esp_hdr(skb);
204 *skb_mac_header(skb) = IPPROTO_ESP;
206 esph->spi = x->id.spi;
207 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
209 sg_init_table(sg, nfrags);
210 skb_to_sgvec(skb, sg,
211 esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
212 clen + alen);
213 sg_init_one(asg, esph, sizeof(*esph));
215 aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
216 aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
217 aead_givcrypt_set_assoc(req, asg, sizeof(*esph));
218 aead_givcrypt_set_giv(req, esph->enc_data,
219 XFRM_SKB_CB(skb)->seq.output);
221 ESP_SKB_CB(skb)->tmp = tmp;
222 err = crypto_aead_givencrypt(req);
223 if (err == -EINPROGRESS)
224 goto error;
226 if (err == -EBUSY)
227 err = NET_XMIT_DROP;
229 kfree(tmp);
231 error:
232 return err;
235 static int esp_input_done2(struct sk_buff *skb, int err)
237 struct xfrm_state *x = xfrm_input_state(skb);
238 struct esp_data *esp = x->data;
239 struct crypto_aead *aead = esp->aead;
240 int alen = crypto_aead_authsize(aead);
241 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
242 int elen = skb->len - hlen;
243 int hdr_len = skb_network_header_len(skb);
244 int padlen;
245 u8 nexthdr[2];
247 kfree(ESP_SKB_CB(skb)->tmp);
249 if (unlikely(err))
250 goto out;
252 if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
253 BUG();
255 err = -EINVAL;
256 padlen = nexthdr[0];
257 if (padlen + 2 + alen >= elen) {
258 LIMIT_NETDEBUG(KERN_WARNING "ipsec esp packet is garbage "
259 "padlen=%d, elen=%d\n", padlen + 2, elen - alen);
260 goto out;
263 /* ... check padding bits here. Silly. :-) */
265 pskb_trim(skb, skb->len - alen - padlen - 2);
266 __skb_pull(skb, hlen);
267 skb_set_transport_header(skb, -hdr_len);
269 err = nexthdr[1];
271 /* RFC4303: Drop dummy packets without any error */
272 if (err == IPPROTO_NONE)
273 err = -EINVAL;
275 out:
276 return err;
279 static void esp_input_done(struct crypto_async_request *base, int err)
281 struct sk_buff *skb = base->data;
283 xfrm_input_resume(skb, esp_input_done2(skb, err));
286 static int esp6_input(struct xfrm_state *x, struct sk_buff *skb)
288 struct ip_esp_hdr *esph;
289 struct esp_data *esp = x->data;
290 struct crypto_aead *aead = esp->aead;
291 struct aead_request *req;
292 struct sk_buff *trailer;
293 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
294 int nfrags;
295 int ret = 0;
296 void *tmp;
297 u8 *iv;
298 struct scatterlist *sg;
299 struct scatterlist *asg;
301 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead))) {
302 ret = -EINVAL;
303 goto out;
306 if (elen <= 0) {
307 ret = -EINVAL;
308 goto out;
311 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0) {
312 ret = -EINVAL;
313 goto out;
316 ret = -ENOMEM;
317 tmp = esp_alloc_tmp(aead, nfrags + 1);
318 if (!tmp)
319 goto out;
321 ESP_SKB_CB(skb)->tmp = tmp;
322 iv = esp_tmp_iv(aead, tmp);
323 req = esp_tmp_req(aead, iv);
324 asg = esp_req_sg(aead, req);
325 sg = asg + 1;
327 skb->ip_summed = CHECKSUM_NONE;
329 esph = (struct ip_esp_hdr *)skb->data;
331 /* Get ivec. This can be wrong, check against another impls. */
332 iv = esph->enc_data;
334 sg_init_table(sg, nfrags);
335 skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
336 sg_init_one(asg, esph, sizeof(*esph));
338 aead_request_set_callback(req, 0, esp_input_done, skb);
339 aead_request_set_crypt(req, sg, sg, elen, iv);
340 aead_request_set_assoc(req, asg, sizeof(*esph));
342 ret = crypto_aead_decrypt(req);
343 if (ret == -EINPROGRESS)
344 goto out;
346 ret = esp_input_done2(skb, ret);
348 out:
349 return ret;
352 static u32 esp6_get_mtu(struct xfrm_state *x, int mtu)
354 struct esp_data *esp = x->data;
355 u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
356 u32 align = max_t(u32, blksize, esp->padlen);
357 u32 rem;
359 mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
360 rem = mtu & (align - 1);
361 mtu &= ~(align - 1);
363 if (x->props.mode != XFRM_MODE_TUNNEL) {
364 u32 padsize = ((blksize - 1) & 7) + 1;
365 mtu -= blksize - padsize;
366 mtu += min_t(u32, blksize - padsize, rem);
369 return mtu - 2;
372 static void esp6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
373 u8 type, u8 code, int offset, __be32 info)
375 struct net *net = dev_net(skb->dev);
376 struct ipv6hdr *iph = (struct ipv6hdr*)skb->data;
377 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data + offset);
378 struct xfrm_state *x;
380 if (type != ICMPV6_DEST_UNREACH &&
381 type != ICMPV6_PKT_TOOBIG)
382 return;
384 x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET6);
385 if (!x)
386 return;
387 printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%pI6\n",
388 ntohl(esph->spi), &iph->daddr);
389 xfrm_state_put(x);
392 static void esp6_destroy(struct xfrm_state *x)
394 struct esp_data *esp = x->data;
396 if (!esp)
397 return;
399 crypto_free_aead(esp->aead);
400 kfree(esp);
403 static int esp_init_aead(struct xfrm_state *x)
405 struct esp_data *esp = x->data;
406 struct crypto_aead *aead;
407 int err;
409 aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
410 err = PTR_ERR(aead);
411 if (IS_ERR(aead))
412 goto error;
414 esp->aead = aead;
416 err = crypto_aead_setkey(aead, x->aead->alg_key,
417 (x->aead->alg_key_len + 7) / 8);
418 if (err)
419 goto error;
421 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
422 if (err)
423 goto error;
425 error:
426 return err;
429 static int esp_init_authenc(struct xfrm_state *x)
431 struct esp_data *esp = x->data;
432 struct crypto_aead *aead;
433 struct crypto_authenc_key_param *param;
434 struct rtattr *rta;
435 char *key;
436 char *p;
437 char authenc_name[CRYPTO_MAX_ALG_NAME];
438 unsigned int keylen;
439 int err;
441 err = -EINVAL;
442 if (x->ealg == NULL)
443 goto error;
445 err = -ENAMETOOLONG;
446 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
447 x->aalg ? x->aalg->alg_name : "digest_null",
448 x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
449 goto error;
451 aead = crypto_alloc_aead(authenc_name, 0, 0);
452 err = PTR_ERR(aead);
453 if (IS_ERR(aead))
454 goto error;
456 esp->aead = aead;
458 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
459 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
460 err = -ENOMEM;
461 key = kmalloc(keylen, GFP_KERNEL);
462 if (!key)
463 goto error;
465 p = key;
466 rta = (void *)p;
467 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
468 rta->rta_len = RTA_LENGTH(sizeof(*param));
469 param = RTA_DATA(rta);
470 p += RTA_SPACE(sizeof(*param));
472 if (x->aalg) {
473 struct xfrm_algo_desc *aalg_desc;
475 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
476 p += (x->aalg->alg_key_len + 7) / 8;
478 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
479 BUG_ON(!aalg_desc);
481 err = -EINVAL;
482 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
483 crypto_aead_authsize(aead)) {
484 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
485 x->aalg->alg_name,
486 crypto_aead_authsize(aead),
487 aalg_desc->uinfo.auth.icv_fullbits/8);
488 goto free_key;
491 err = crypto_aead_setauthsize(
492 aead, x->aalg->alg_trunc_len / 8);
493 if (err)
494 goto free_key;
497 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
498 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
500 err = crypto_aead_setkey(aead, key, keylen);
502 free_key:
503 kfree(key);
505 error:
506 return err;
509 static int esp6_init_state(struct xfrm_state *x)
511 struct esp_data *esp;
512 struct crypto_aead *aead;
513 u32 align;
514 int err;
516 if (x->encap)
517 return -EINVAL;
519 esp = kzalloc(sizeof(*esp), GFP_KERNEL);
520 if (esp == NULL)
521 return -ENOMEM;
523 x->data = esp;
525 if (x->aead)
526 err = esp_init_aead(x);
527 else
528 err = esp_init_authenc(x);
530 if (err)
531 goto error;
533 aead = esp->aead;
535 esp->padlen = 0;
537 x->props.header_len = sizeof(struct ip_esp_hdr) +
538 crypto_aead_ivsize(aead);
539 switch (x->props.mode) {
540 case XFRM_MODE_BEET:
541 if (x->sel.family != AF_INET6)
542 x->props.header_len += IPV4_BEET_PHMAXLEN +
543 (sizeof(struct ipv6hdr) - sizeof(struct iphdr));
544 break;
545 case XFRM_MODE_TRANSPORT:
546 break;
547 case XFRM_MODE_TUNNEL:
548 x->props.header_len += sizeof(struct ipv6hdr);
549 break;
550 default:
551 goto error;
554 align = ALIGN(crypto_aead_blocksize(aead), 4);
555 if (esp->padlen)
556 align = max_t(u32, align, esp->padlen);
557 x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
559 error:
560 return err;
563 static const struct xfrm_type esp6_type =
565 .description = "ESP6",
566 .owner = THIS_MODULE,
567 .proto = IPPROTO_ESP,
568 .flags = XFRM_TYPE_REPLAY_PROT,
569 .init_state = esp6_init_state,
570 .destructor = esp6_destroy,
571 .get_mtu = esp6_get_mtu,
572 .input = esp6_input,
573 .output = esp6_output,
574 .hdr_offset = xfrm6_find_1stfragopt,
577 static const struct inet6_protocol esp6_protocol = {
578 .handler = xfrm6_rcv,
579 .err_handler = esp6_err,
580 .flags = INET6_PROTO_NOPOLICY,
583 static int __init esp6_init(void)
585 if (xfrm_register_type(&esp6_type, AF_INET6) < 0) {
586 printk(KERN_INFO "ipv6 esp init: can't add xfrm type\n");
587 return -EAGAIN;
589 if (inet6_add_protocol(&esp6_protocol, IPPROTO_ESP) < 0) {
590 printk(KERN_INFO "ipv6 esp init: can't add protocol\n");
591 xfrm_unregister_type(&esp6_type, AF_INET6);
592 return -EAGAIN;
595 return 0;
598 static void __exit esp6_fini(void)
600 if (inet6_del_protocol(&esp6_protocol, IPPROTO_ESP) < 0)
601 printk(KERN_INFO "ipv6 esp close: can't remove protocol\n");
602 if (xfrm_unregister_type(&esp6_type, AF_INET6) < 0)
603 printk(KERN_INFO "ipv6 esp close: can't remove xfrm type\n");
606 module_init(esp6_init);
607 module_exit(esp6_fini);
609 MODULE_LICENSE("GPL");
610 MODULE_ALIAS_XFRM_TYPE(AF_INET6, XFRM_PROTO_ESP);