1 #include <linux/config.h>
2 #include <linux/module.h>
6 #include <asm/scatterlist.h>
7 #include <linux/crypto.h>
8 #include <linux/pfkeyv2.h>
9 #include <linux/random.h>
13 #define MAX_SG_ONSTACK 4
15 /* decapsulation data for use when post-processing */
16 struct esp_decap_data {
22 int esp_output(struct sk_buff *skb)
25 struct dst_entry *dst = skb->dst;
26 struct xfrm_state *x = dst->xfrm;
27 struct iphdr *iph, *top_iph;
28 struct ip_esp_hdr *esph;
29 struct crypto_tfm *tfm;
31 struct sk_buff *trailer;
32 struct udphdr *uh = NULL;
33 struct xfrm_encap_tmpl *encap = NULL;
43 /* First, if the skb is not checksummed, complete checksum. */
44 if (skb->ip_summed == CHECKSUM_HW && skb_checksum_help(skb) == NULL) {
49 spin_lock_bh(&x->lock);
50 err = xfrm_check_output(x, skb, AF_INET);
55 /* Strip IP header in transport mode. Save it. */
58 memcpy(&tmp_iph, iph, iph->ihl*4);
59 __skb_pull(skb, iph->ihl*4);
61 /* Now skb is pure payload to encrypt */
63 /* Round to block size */
67 alen = esp->auth.icv_trunc_len;
69 blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
70 clen = (clen + 2 + blksize-1)&~(blksize-1);
72 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
74 if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0)
80 for (i=0; i<clen-skb->len - 2; i++)
81 *(u8*)(trailer->tail + i) = i+1;
83 *(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2;
84 pskb_put(skb, trailer, clen - skb->len);
90 top_iph = (struct iphdr*)skb_push(skb, x->props.header_len);
91 esph = (struct ip_esp_hdr*)(top_iph+1);
92 if (encap && encap->encap_type) {
93 switch (encap->encap_type) {
94 case UDP_ENCAP_ESPINUDP:
95 uh = (struct udphdr*) esph;
96 esph = (struct ip_esp_hdr*)(uh+1);
97 top_iph->protocol = IPPROTO_UDP;
101 "esp_output(): Unhandled encap: %u\n",
103 top_iph->protocol = IPPROTO_ESP;
107 top_iph->protocol = IPPROTO_ESP;
108 *(u8*)(trailer->tail - 1) = IPPROTO_IPIP;
110 top_iph->version = 4;
111 top_iph->tos = iph->tos; /* DS disclosed */
112 top_iph->tot_len = htons(skb->len + alen);
113 top_iph->frag_off = iph->frag_off&htons(IP_DF);
114 if (!(top_iph->frag_off))
115 ip_select_ident(top_iph, dst, 0);
116 top_iph->ttl = iph->ttl; /* TTL disclosed */
118 top_iph->saddr = x->props.saddr.a4;
119 top_iph->daddr = x->id.daddr.a4;
120 memset(&(IPCB(skb)->opt), 0, sizeof(struct ip_options));
122 esph = (struct ip_esp_hdr*)skb_push(skb, x->props.header_len);
123 top_iph = (struct iphdr*)skb_push(skb, iph->ihl*4);
124 memcpy(top_iph, &tmp_iph, iph->ihl*4);
125 if (encap && encap->encap_type) {
126 switch (encap->encap_type) {
127 case UDP_ENCAP_ESPINUDP:
128 uh = (struct udphdr*) esph;
129 esph = (struct ip_esp_hdr*)(uh+1);
130 top_iph->protocol = IPPROTO_UDP;
134 "esp_output(): Unhandled encap: %u\n",
136 top_iph->protocol = IPPROTO_ESP;
140 top_iph->protocol = IPPROTO_ESP;
142 top_iph->tot_len = htons(skb->len + alen);
144 top_iph->frag_off = iph->frag_off;
145 *(u8*)(trailer->tail - 1) = iph->protocol;
148 /* this is non-NULL only with UDP Encapsulation */
150 uh->source = encap->encap_sport;
151 uh->dest = encap->encap_dport;
152 uh->len = htons(skb->len + alen - sizeof(struct iphdr));
156 esph->spi = x->id.spi;
157 esph->seq_no = htonl(++x->replay.oseq);
160 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
163 struct scatterlist sgbuf[nfrags>MAX_SG_ONSTACK ? 0 : nfrags];
164 struct scatterlist *sg = sgbuf;
166 if (unlikely(nfrags > MAX_SG_ONSTACK)) {
167 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
171 skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
172 crypto_cipher_encrypt(tfm, sg, sg, clen);
173 if (unlikely(sg != sgbuf))
177 if (esp->conf.ivlen) {
178 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
179 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
182 if (esp->auth.icv_full_len) {
183 esp->auth.icv(esp, skb, (u8*)esph-skb->data,
184 sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
185 pskb_put(skb, trailer, alen);
188 ip_send_check(top_iph);
190 skb->nh.raw = skb->data;
192 x->curlft.bytes += skb->len;
194 spin_unlock_bh(&x->lock);
195 if ((skb->dst = dst_pop(dst)) == NULL) {
199 return NET_XMIT_BYPASS;
202 spin_unlock_bh(&x->lock);
209 * Note: detecting truncated vs. non-truncated authentication data is very
210 * expensive, so we only support truncated data, which is the recommended
213 int esp_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
216 struct ip_esp_hdr *esph;
217 struct esp_data *esp = x->data;
218 struct sk_buff *trailer;
219 int blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
220 int alen = esp->auth.icv_trunc_len;
221 int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
225 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
228 if (elen <= 0 || (elen & (blksize-1)))
231 /* If integrity check is required, do this. */
232 if (esp->auth.icv_full_len) {
233 u8 sum[esp->auth.icv_full_len];
236 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
238 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
241 if (unlikely(memcmp(sum, sum1, alen))) {
242 x->stats.integrity_failed++;
247 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
250 skb->ip_summed = CHECKSUM_NONE;
252 esph = (struct ip_esp_hdr*)skb->data;
255 /* Get ivec. This can be wrong, check against another impls. */
257 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
261 struct scatterlist sgbuf[nfrags>MAX_SG_ONSTACK ? 0 : nfrags];
262 struct scatterlist *sg = sgbuf;
266 if (unlikely(nfrags > MAX_SG_ONSTACK)) {
267 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
271 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
272 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
273 if (unlikely(sg != sgbuf))
276 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
280 if (padlen+2 >= elen)
283 /* ... check padding bits here. Silly. :-) */
285 if (x->encap && decap && decap->decap_type) {
286 struct esp_decap_data *encap_data;
287 struct udphdr *uh = (struct udphdr *) (iph+1);
289 encap_data = (struct esp_decap_data *) (decap->decap_data);
290 encap_data->proto = 0;
292 switch (decap->decap_type) {
293 case UDP_ENCAP_ESPINUDP:
295 if ((void*)uh == (void*)esph) {
297 "esp_input(): Got ESP; expecting ESPinUDP\n");
301 encap_data->proto = AF_INET;
302 encap_data->saddr.a4 = iph->saddr;
303 encap_data->sport = uh->source;
304 encap_len = (void*)esph - (void*)uh;
305 if (encap_len != sizeof(*uh))
307 "esp_input(): UDP -> ESP: too much room: %d\n",
313 "esp_input(): processing unknown encap type: %u\n",
319 iph->protocol = nexthdr[1];
320 pskb_trim(skb, skb->len - alen - padlen - 2);
321 memcpy(workbuf, skb->nh.raw, iph->ihl*4);
322 skb->h.raw = skb_pull(skb, sizeof(struct ip_esp_hdr) + esp->conf.ivlen);
323 skb->nh.raw += encap_len + sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
324 memcpy(skb->nh.raw, workbuf, iph->ihl*4);
325 skb->nh.iph->tot_len = htons(skb->len);
334 int esp_post_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
338 struct xfrm_encap_tmpl *encap;
339 struct esp_decap_data *decap_data;
342 decap_data = (struct esp_decap_data *)(decap->decap_data);
344 /* first, make sure that the decap type == the encap type */
345 if (encap->encap_type != decap->decap_type)
348 /* Next, if we don't have an encap type, then ignore it */
349 if (!encap->encap_type)
352 switch (encap->encap_type) {
353 case UDP_ENCAP_ESPINUDP:
355 * 1) if the NAT-T peer's IP or port changed then
356 * advertize the change to the keying daemon.
357 * This is an inbound SA, so just compare
360 if (decap_data->proto == AF_INET &&
361 (decap_data->saddr.a4 != x->props.saddr.a4 ||
362 decap_data->sport != encap->encap_sport)) {
363 xfrm_address_t ipaddr;
365 ipaddr.a4 = decap_data->saddr.a4;
366 km_new_mapping(x, &ipaddr, decap_data->sport);
368 /* XXX: perhaps add an extra
369 * policy check here, to see
370 * if we should allow or
371 * reject a packet from a
378 * 2) ignore UDP/TCP checksums in case
379 * of NAT-T in Transport Mode, or
380 * perform other post-processing fixes
381 * as per * draft-ietf-ipsec-udp-encaps-06,
385 skb->ip_summed = CHECKSUM_UNNECESSARY;
390 "esp4_post_input(): Unhandled encap type: %u\n",
398 static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
400 struct esp_data *esp = x->data;
401 u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
404 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
406 /* The worst case. */
409 if (esp->conf.padlen)
410 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
412 return mtu + x->props.header_len + esp->auth.icv_trunc_len;
415 void esp4_err(struct sk_buff *skb, u32 info)
417 struct iphdr *iph = (struct iphdr*)skb->data;
418 struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
419 struct xfrm_state *x;
421 if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
422 skb->h.icmph->code != ICMP_FRAG_NEEDED)
425 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
428 printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
429 ntohl(esph->spi), ntohl(iph->daddr));
433 void esp_destroy(struct xfrm_state *x)
435 struct esp_data *esp = x->data;
438 crypto_free_tfm(esp->conf.tfm);
439 esp->conf.tfm = NULL;
441 if (esp->conf.ivec) {
442 kfree(esp->conf.ivec);
443 esp->conf.ivec = NULL;
446 crypto_free_tfm(esp->auth.tfm);
447 esp->auth.tfm = NULL;
449 if (esp->auth.work_icv) {
450 kfree(esp->auth.work_icv);
451 esp->auth.work_icv = NULL;
456 int esp_init_state(struct xfrm_state *x, void *args)
458 struct esp_data *esp = NULL;
460 /* null auth and encryption can have zero length keys */
462 if (x->aalg->alg_key_len > 512)
468 esp = kmalloc(sizeof(*esp), GFP_KERNEL);
472 memset(esp, 0, sizeof(*esp));
475 struct xfrm_algo_desc *aalg_desc;
477 esp->auth.key = x->aalg->alg_key;
478 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
479 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
480 if (esp->auth.tfm == NULL)
482 esp->auth.icv = esp_hmac_digest;
484 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
487 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
488 crypto_tfm_alg_digestsize(esp->auth.tfm)) {
489 printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
491 crypto_tfm_alg_digestsize(esp->auth.tfm),
492 aalg_desc->uinfo.auth.icv_fullbits/8);
496 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
497 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
499 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
500 if (!esp->auth.work_icv)
503 esp->conf.key = x->ealg->alg_key;
504 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
505 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
506 if (esp->conf.tfm == NULL)
508 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
509 esp->conf.padlen = 0;
510 if (esp->conf.ivlen) {
511 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
512 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
514 crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len);
515 x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
517 x->props.header_len += sizeof(struct iphdr);
519 struct xfrm_encap_tmpl *encap = x->encap;
521 if (encap->encap_type) {
522 switch (encap->encap_type) {
523 case UDP_ENCAP_ESPINUDP:
524 x->props.header_len += sizeof(struct udphdr);
528 "esp_init_state(): Unhandled encap type: %u\n",
535 x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
541 crypto_free_tfm(esp->auth.tfm);
542 if (esp->auth.work_icv)
543 kfree(esp->auth.work_icv);
545 crypto_free_tfm(esp->conf.tfm);
551 static struct xfrm_type esp_type =
553 .description = "ESP4",
554 .owner = THIS_MODULE,
555 .proto = IPPROTO_ESP,
556 .init_state = esp_init_state,
557 .destructor = esp_destroy,
558 .get_max_size = esp4_get_max_size,
560 .post_input = esp_post_input,
564 static struct inet_protocol esp4_protocol = {
565 .handler = xfrm4_rcv,
566 .err_handler = esp4_err,
570 static int __init esp4_init(void)
572 struct xfrm_decap_state decap;
574 if (sizeof(struct esp_decap_data) <
575 sizeof(decap.decap_data)) {
576 extern void decap_data_too_small(void);
578 decap_data_too_small();
581 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
582 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
585 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
586 printk(KERN_INFO "ip esp init: can't add protocol\n");
587 xfrm_unregister_type(&esp_type, AF_INET);
593 static void __exit esp4_fini(void)
595 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
596 printk(KERN_INFO "ip esp close: can't remove protocol\n");
597 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
598 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
601 module_init(esp4_init);
602 module_exit(esp4_fini);
603 MODULE_LICENSE("GPL");