xen mmu: fix a race window causing leave_mm BUG()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / esp4.c
blobe42a905180f00f93d3113fbdb2922bf5b1bb2217
1 #include <crypto/aead.h>
2 #include <crypto/authenc.h>
3 #include <linux/err.h>
4 #include <linux/module.h>
5 #include <net/ip.h>
6 #include <net/xfrm.h>
7 #include <net/esp.h>
8 #include <linux/scatterlist.h>
9 #include <linux/kernel.h>
10 #include <linux/pfkeyv2.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
14 #include <linux/in6.h>
15 #include <net/icmp.h>
16 #include <net/protocol.h>
17 #include <net/udp.h>
19 struct esp_skb_cb {
20 struct xfrm_skb_cb xfrm;
21 void *tmp;
24 #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
26 static u32 esp4_get_mtu(struct xfrm_state *x, int mtu);
29 * Allocate an AEAD request structure with extra space for SG and IV.
31 * For alignment considerations the IV is placed at the front, followed
32 * by the request and finally the SG list.
34 * TODO: Use spare space in skb for this where possible.
36 static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags)
38 unsigned int len;
40 len = crypto_aead_ivsize(aead);
41 if (len) {
42 len += crypto_aead_alignmask(aead) &
43 ~(crypto_tfm_ctx_alignment() - 1);
44 len = ALIGN(len, crypto_tfm_ctx_alignment());
47 len += sizeof(struct aead_givcrypt_request) + crypto_aead_reqsize(aead);
48 len = ALIGN(len, __alignof__(struct scatterlist));
50 len += sizeof(struct scatterlist) * nfrags;
52 return kmalloc(len, GFP_ATOMIC);
55 static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp)
57 return crypto_aead_ivsize(aead) ?
58 PTR_ALIGN((u8 *)tmp, crypto_aead_alignmask(aead) + 1) : tmp;
61 static inline struct aead_givcrypt_request *esp_tmp_givreq(
62 struct crypto_aead *aead, u8 *iv)
64 struct aead_givcrypt_request *req;
66 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
67 crypto_tfm_ctx_alignment());
68 aead_givcrypt_set_tfm(req, aead);
69 return req;
72 static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
74 struct aead_request *req;
76 req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
77 crypto_tfm_ctx_alignment());
78 aead_request_set_tfm(req, aead);
79 return req;
82 static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
83 struct aead_request *req)
85 return (void *)ALIGN((unsigned long)(req + 1) +
86 crypto_aead_reqsize(aead),
87 __alignof__(struct scatterlist));
90 static inline struct scatterlist *esp_givreq_sg(
91 struct crypto_aead *aead, struct aead_givcrypt_request *req)
93 return (void *)ALIGN((unsigned long)(req + 1) +
94 crypto_aead_reqsize(aead),
95 __alignof__(struct scatterlist));
98 static void esp_output_done(struct crypto_async_request *base, int err)
100 struct sk_buff *skb = base->data;
102 kfree(ESP_SKB_CB(skb)->tmp);
103 xfrm_output_resume(skb, err);
106 static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
108 int err;
109 struct ip_esp_hdr *esph;
110 struct crypto_aead *aead;
111 struct aead_givcrypt_request *req;
112 struct scatterlist *sg;
113 struct scatterlist *asg;
114 struct esp_data *esp;
115 struct sk_buff *trailer;
116 void *tmp;
117 u8 *iv;
118 u8 *tail;
119 int blksize;
120 int clen;
121 int alen;
122 int plen;
123 int tfclen;
124 int nfrags;
126 /* skb is pure payload to encrypt */
128 err = -ENOMEM;
130 esp = x->data;
131 aead = esp->aead;
132 alen = crypto_aead_authsize(aead);
134 tfclen = 0;
135 if (x->tfcpad) {
136 struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
137 u32 padto;
139 padto = min(x->tfcpad, esp4_get_mtu(x, dst->child_mtu_cached));
140 if (skb->len < padto)
141 tfclen = padto - skb->len;
143 blksize = ALIGN(crypto_aead_blocksize(aead), 4);
144 clen = ALIGN(skb->len + 2 + tfclen, blksize);
145 if (esp->padlen)
146 clen = ALIGN(clen, esp->padlen);
147 plen = clen - skb->len - tfclen;
149 err = skb_cow_data(skb, tfclen + plen + alen, &trailer);
150 if (err < 0)
151 goto error;
152 nfrags = err;
154 tmp = esp_alloc_tmp(aead, nfrags + 1);
155 if (!tmp)
156 goto error;
158 iv = esp_tmp_iv(aead, tmp);
159 req = esp_tmp_givreq(aead, iv);
160 asg = esp_givreq_sg(aead, req);
161 sg = asg + 1;
163 /* Fill padding... */
164 tail = skb_tail_pointer(trailer);
165 if (tfclen) {
166 memset(tail, 0, tfclen);
167 tail += tfclen;
169 do {
170 int i;
171 for (i = 0; i < plen - 2; i++)
172 tail[i] = i + 1;
173 } while (0);
174 tail[plen - 2] = plen - 2;
175 tail[plen - 1] = *skb_mac_header(skb);
176 pskb_put(skb, trailer, clen - skb->len + alen);
178 skb_push(skb, -skb_network_offset(skb));
179 esph = ip_esp_hdr(skb);
180 *skb_mac_header(skb) = IPPROTO_ESP;
182 /* this is non-NULL only with UDP Encapsulation */
183 if (x->encap) {
184 struct xfrm_encap_tmpl *encap = x->encap;
185 struct udphdr *uh;
186 __be32 *udpdata32;
187 __be16 sport, dport;
188 int encap_type;
190 spin_lock_bh(&x->lock);
191 sport = encap->encap_sport;
192 dport = encap->encap_dport;
193 encap_type = encap->encap_type;
194 spin_unlock_bh(&x->lock);
196 uh = (struct udphdr *)esph;
197 uh->source = sport;
198 uh->dest = dport;
199 uh->len = htons(skb->len - skb_transport_offset(skb));
200 uh->check = 0;
202 switch (encap_type) {
203 default:
204 case UDP_ENCAP_ESPINUDP:
205 esph = (struct ip_esp_hdr *)(uh + 1);
206 break;
207 case UDP_ENCAP_ESPINUDP_NON_IKE:
208 udpdata32 = (__be32 *)(uh + 1);
209 udpdata32[0] = udpdata32[1] = 0;
210 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
211 break;
214 *skb_mac_header(skb) = IPPROTO_UDP;
217 esph->spi = x->id.spi;
218 esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
220 sg_init_table(sg, nfrags);
221 skb_to_sgvec(skb, sg,
222 esph->enc_data + crypto_aead_ivsize(aead) - skb->data,
223 clen + alen);
224 sg_init_one(asg, esph, sizeof(*esph));
226 aead_givcrypt_set_callback(req, 0, esp_output_done, skb);
227 aead_givcrypt_set_crypt(req, sg, sg, clen, iv);
228 aead_givcrypt_set_assoc(req, asg, sizeof(*esph));
229 aead_givcrypt_set_giv(req, esph->enc_data,
230 XFRM_SKB_CB(skb)->seq.output);
232 ESP_SKB_CB(skb)->tmp = tmp;
233 err = crypto_aead_givencrypt(req);
234 if (err == -EINPROGRESS)
235 goto error;
237 if (err == -EBUSY)
238 err = NET_XMIT_DROP;
240 kfree(tmp);
242 error:
243 return err;
246 static int esp_input_done2(struct sk_buff *skb, int err)
248 struct iphdr *iph;
249 struct xfrm_state *x = xfrm_input_state(skb);
250 struct esp_data *esp = x->data;
251 struct crypto_aead *aead = esp->aead;
252 int alen = crypto_aead_authsize(aead);
253 int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
254 int elen = skb->len - hlen;
255 int ihl;
256 u8 nexthdr[2];
257 int padlen;
259 kfree(ESP_SKB_CB(skb)->tmp);
261 if (unlikely(err))
262 goto out;
264 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
265 BUG();
267 err = -EINVAL;
268 padlen = nexthdr[0];
269 if (padlen + 2 + alen >= elen)
270 goto out;
272 /* ... check padding bits here. Silly. :-) */
274 iph = ip_hdr(skb);
275 ihl = iph->ihl * 4;
277 if (x->encap) {
278 struct xfrm_encap_tmpl *encap = x->encap;
279 struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
282 * 1) if the NAT-T peer's IP or port changed then
283 * advertize the change to the keying daemon.
284 * This is an inbound SA, so just compare
285 * SRC ports.
287 if (iph->saddr != x->props.saddr.a4 ||
288 uh->source != encap->encap_sport) {
289 xfrm_address_t ipaddr;
291 ipaddr.a4 = iph->saddr;
292 km_new_mapping(x, &ipaddr, uh->source);
294 /* XXX: perhaps add an extra
295 * policy check here, to see
296 * if we should allow or
297 * reject a packet from a
298 * different source
299 * address/port.
304 * 2) ignore UDP/TCP checksums in case
305 * of NAT-T in Transport Mode, or
306 * perform other post-processing fixes
307 * as per draft-ietf-ipsec-udp-encaps-06,
308 * section 3.1.2
310 if (x->props.mode == XFRM_MODE_TRANSPORT)
311 skb->ip_summed = CHECKSUM_UNNECESSARY;
314 pskb_trim(skb, skb->len - alen - padlen - 2);
315 __skb_pull(skb, hlen);
316 skb_set_transport_header(skb, -ihl);
318 err = nexthdr[1];
320 /* RFC4303: Drop dummy packets without any error */
321 if (err == IPPROTO_NONE)
322 err = -EINVAL;
324 out:
325 return err;
328 static void esp_input_done(struct crypto_async_request *base, int err)
330 struct sk_buff *skb = base->data;
332 xfrm_input_resume(skb, esp_input_done2(skb, err));
336 * Note: detecting truncated vs. non-truncated authentication data is very
337 * expensive, so we only support truncated data, which is the recommended
338 * and common case.
340 static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
342 struct ip_esp_hdr *esph;
343 struct esp_data *esp = x->data;
344 struct crypto_aead *aead = esp->aead;
345 struct aead_request *req;
346 struct sk_buff *trailer;
347 int elen = skb->len - sizeof(*esph) - crypto_aead_ivsize(aead);
348 int nfrags;
349 void *tmp;
350 u8 *iv;
351 struct scatterlist *sg;
352 struct scatterlist *asg;
353 int err = -EINVAL;
355 if (!pskb_may_pull(skb, sizeof(*esph) + crypto_aead_ivsize(aead)))
356 goto out;
358 if (elen <= 0)
359 goto out;
361 if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
362 goto out;
363 nfrags = err;
365 err = -ENOMEM;
366 tmp = esp_alloc_tmp(aead, nfrags + 1);
367 if (!tmp)
368 goto out;
370 ESP_SKB_CB(skb)->tmp = tmp;
371 iv = esp_tmp_iv(aead, tmp);
372 req = esp_tmp_req(aead, iv);
373 asg = esp_req_sg(aead, req);
374 sg = asg + 1;
376 skb->ip_summed = CHECKSUM_NONE;
378 esph = (struct ip_esp_hdr *)skb->data;
380 /* Get ivec. This can be wrong, check against another impls. */
381 iv = esph->enc_data;
383 sg_init_table(sg, nfrags);
384 skb_to_sgvec(skb, sg, sizeof(*esph) + crypto_aead_ivsize(aead), elen);
385 sg_init_one(asg, esph, sizeof(*esph));
387 aead_request_set_callback(req, 0, esp_input_done, skb);
388 aead_request_set_crypt(req, sg, sg, elen, iv);
389 aead_request_set_assoc(req, asg, sizeof(*esph));
391 err = crypto_aead_decrypt(req);
392 if (err == -EINPROGRESS)
393 goto out;
395 err = esp_input_done2(skb, err);
397 out:
398 return err;
401 static u32 esp4_get_mtu(struct xfrm_state *x, int mtu)
403 struct esp_data *esp = x->data;
404 u32 blksize = ALIGN(crypto_aead_blocksize(esp->aead), 4);
405 u32 align = max_t(u32, blksize, esp->padlen);
406 u32 rem;
408 mtu -= x->props.header_len + crypto_aead_authsize(esp->aead);
409 rem = mtu & (align - 1);
410 mtu &= ~(align - 1);
412 switch (x->props.mode) {
413 case XFRM_MODE_TUNNEL:
414 break;
415 default:
416 case XFRM_MODE_TRANSPORT:
417 /* The worst case */
418 mtu -= blksize - 4;
419 mtu += min_t(u32, blksize - 4, rem);
420 break;
421 case XFRM_MODE_BEET:
422 /* The worst case. */
423 mtu += min_t(u32, IPV4_BEET_PHMAXLEN, rem);
424 break;
427 return mtu - 2;
430 static void esp4_err(struct sk_buff *skb, u32 info)
432 struct net *net = dev_net(skb->dev);
433 struct iphdr *iph = (struct iphdr *)skb->data;
434 struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
435 struct xfrm_state *x;
437 if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
438 icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
439 return;
441 x = xfrm_state_lookup(net, skb->mark, (xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
442 if (!x)
443 return;
444 NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
445 ntohl(esph->spi), ntohl(iph->daddr));
446 xfrm_state_put(x);
449 static void esp_destroy(struct xfrm_state *x)
451 struct esp_data *esp = x->data;
453 if (!esp)
454 return;
456 crypto_free_aead(esp->aead);
457 kfree(esp);
460 static int esp_init_aead(struct xfrm_state *x)
462 struct esp_data *esp = x->data;
463 struct crypto_aead *aead;
464 int err;
466 aead = crypto_alloc_aead(x->aead->alg_name, 0, 0);
467 err = PTR_ERR(aead);
468 if (IS_ERR(aead))
469 goto error;
471 esp->aead = aead;
473 err = crypto_aead_setkey(aead, x->aead->alg_key,
474 (x->aead->alg_key_len + 7) / 8);
475 if (err)
476 goto error;
478 err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
479 if (err)
480 goto error;
482 error:
483 return err;
486 static int esp_init_authenc(struct xfrm_state *x)
488 struct esp_data *esp = x->data;
489 struct crypto_aead *aead;
490 struct crypto_authenc_key_param *param;
491 struct rtattr *rta;
492 char *key;
493 char *p;
494 char authenc_name[CRYPTO_MAX_ALG_NAME];
495 unsigned int keylen;
496 int err;
498 err = -EINVAL;
499 if (x->ealg == NULL)
500 goto error;
502 err = -ENAMETOOLONG;
503 if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME, "authenc(%s,%s)",
504 x->aalg ? x->aalg->alg_name : "digest_null",
505 x->ealg->alg_name) >= CRYPTO_MAX_ALG_NAME)
506 goto error;
508 aead = crypto_alloc_aead(authenc_name, 0, 0);
509 err = PTR_ERR(aead);
510 if (IS_ERR(aead))
511 goto error;
513 esp->aead = aead;
515 keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
516 (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
517 err = -ENOMEM;
518 key = kmalloc(keylen, GFP_KERNEL);
519 if (!key)
520 goto error;
522 p = key;
523 rta = (void *)p;
524 rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
525 rta->rta_len = RTA_LENGTH(sizeof(*param));
526 param = RTA_DATA(rta);
527 p += RTA_SPACE(sizeof(*param));
529 if (x->aalg) {
530 struct xfrm_algo_desc *aalg_desc;
532 memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
533 p += (x->aalg->alg_key_len + 7) / 8;
535 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
536 BUG_ON(!aalg_desc);
538 err = -EINVAL;
539 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
540 crypto_aead_authsize(aead)) {
541 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
542 x->aalg->alg_name,
543 crypto_aead_authsize(aead),
544 aalg_desc->uinfo.auth.icv_fullbits/8);
545 goto free_key;
548 err = crypto_aead_setauthsize(
549 aead, x->aalg->alg_trunc_len / 8);
550 if (err)
551 goto free_key;
554 param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
555 memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
557 err = crypto_aead_setkey(aead, key, keylen);
559 free_key:
560 kfree(key);
562 error:
563 return err;
566 static int esp_init_state(struct xfrm_state *x)
568 struct esp_data *esp;
569 struct crypto_aead *aead;
570 u32 align;
571 int err;
573 esp = kzalloc(sizeof(*esp), GFP_KERNEL);
574 if (esp == NULL)
575 return -ENOMEM;
577 x->data = esp;
579 if (x->aead)
580 err = esp_init_aead(x);
581 else
582 err = esp_init_authenc(x);
584 if (err)
585 goto error;
587 aead = esp->aead;
589 esp->padlen = 0;
591 x->props.header_len = sizeof(struct ip_esp_hdr) +
592 crypto_aead_ivsize(aead);
593 if (x->props.mode == XFRM_MODE_TUNNEL)
594 x->props.header_len += sizeof(struct iphdr);
595 else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
596 x->props.header_len += IPV4_BEET_PHMAXLEN;
597 if (x->encap) {
598 struct xfrm_encap_tmpl *encap = x->encap;
600 switch (encap->encap_type) {
601 default:
602 goto error;
603 case UDP_ENCAP_ESPINUDP:
604 x->props.header_len += sizeof(struct udphdr);
605 break;
606 case UDP_ENCAP_ESPINUDP_NON_IKE:
607 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
608 break;
612 align = ALIGN(crypto_aead_blocksize(aead), 4);
613 if (esp->padlen)
614 align = max_t(u32, align, esp->padlen);
615 x->props.trailer_len = align + 1 + crypto_aead_authsize(esp->aead);
617 error:
618 return err;
621 static const struct xfrm_type esp_type =
623 .description = "ESP4",
624 .owner = THIS_MODULE,
625 .proto = IPPROTO_ESP,
626 .flags = XFRM_TYPE_REPLAY_PROT,
627 .init_state = esp_init_state,
628 .destructor = esp_destroy,
629 .get_mtu = esp4_get_mtu,
630 .input = esp_input,
631 .output = esp_output
634 static const struct net_protocol esp4_protocol = {
635 .handler = xfrm4_rcv,
636 .err_handler = esp4_err,
637 .no_policy = 1,
638 .netns_ok = 1,
641 static int __init esp4_init(void)
643 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
644 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
645 return -EAGAIN;
647 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
648 printk(KERN_INFO "ip esp init: can't add protocol\n");
649 xfrm_unregister_type(&esp_type, AF_INET);
650 return -EAGAIN;
652 return 0;
655 static void __exit esp4_fini(void)
657 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
658 printk(KERN_INFO "ip esp close: can't remove protocol\n");
659 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
660 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
663 module_init(esp4_init);
664 module_exit(esp4_fini);
665 MODULE_LICENSE("GPL");
666 MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);