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 /* decapsulation data for use when post-processing */
14 struct esp_decap_data {
20 static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
23 struct iphdr *top_iph;
24 struct ip_esp_hdr *esph;
25 struct crypto_tfm *tfm;
27 struct sk_buff *trailer;
33 /* Strip IP+ESP header. */
34 __skb_pull(skb, skb->h.raw - skb->data);
35 /* Now skb is pure payload to encrypt */
39 /* Round to block size */
43 alen = esp->auth.icv_trunc_len;
45 blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
46 clen = (clen + 2 + blksize-1)&~(blksize-1);
48 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
50 if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0)
56 for (i=0; i<clen-skb->len - 2; i++)
57 *(u8*)(trailer->tail + i) = i+1;
59 *(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2;
60 pskb_put(skb, trailer, clen - skb->len);
62 __skb_push(skb, skb->data - skb->nh.raw);
63 top_iph = skb->nh.iph;
64 esph = (struct ip_esp_hdr *)(skb->nh.raw + top_iph->ihl*4);
65 top_iph->tot_len = htons(skb->len + alen);
66 *(u8*)(trailer->tail - 1) = top_iph->protocol;
68 /* this is non-NULL only with UDP Encapsulation */
70 struct xfrm_encap_tmpl *encap = x->encap;
74 uh = (struct udphdr *)esph;
75 uh->source = encap->encap_sport;
76 uh->dest = encap->encap_dport;
77 uh->len = htons(skb->len + alen - top_iph->ihl*4);
80 switch (encap->encap_type) {
82 case UDP_ENCAP_ESPINUDP:
83 esph = (struct ip_esp_hdr *)(uh + 1);
85 case UDP_ENCAP_ESPINUDP_NON_IKE:
86 udpdata32 = (u32 *)(uh + 1);
87 udpdata32[0] = udpdata32[1] = 0;
88 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
92 top_iph->protocol = IPPROTO_UDP;
94 top_iph->protocol = IPPROTO_ESP;
96 esph->spi = x->id.spi;
97 esph->seq_no = htonl(++x->replay.oseq);
100 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
103 struct scatterlist *sg = &esp->sgbuf[0];
105 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
106 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
110 skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
111 crypto_cipher_encrypt(tfm, sg, sg, clen);
112 if (unlikely(sg != &esp->sgbuf[0]))
116 if (esp->conf.ivlen) {
117 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
118 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
121 if (esp->auth.icv_full_len) {
122 esp->auth.icv(esp, skb, (u8*)esph-skb->data,
123 sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
124 pskb_put(skb, trailer, alen);
127 ip_send_check(top_iph);
136 * Note: detecting truncated vs. non-truncated authentication data is very
137 * expensive, so we only support truncated data, which is the recommended
140 static int esp_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
143 struct ip_esp_hdr *esph;
144 struct esp_data *esp = x->data;
145 struct sk_buff *trailer;
146 int blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
147 int alen = esp->auth.icv_trunc_len;
148 int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
152 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
155 if (elen <= 0 || (elen & (blksize-1)))
158 /* If integrity check is required, do this. */
159 if (esp->auth.icv_full_len) {
160 u8 sum[esp->auth.icv_full_len];
163 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
165 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
168 if (unlikely(memcmp(sum, sum1, alen))) {
169 x->stats.integrity_failed++;
174 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
177 skb->ip_summed = CHECKSUM_NONE;
179 esph = (struct ip_esp_hdr*)skb->data;
182 /* Get ivec. This can be wrong, check against another impls. */
184 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
188 struct scatterlist *sg = &esp->sgbuf[0];
192 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
193 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
197 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
198 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
199 if (unlikely(sg != &esp->sgbuf[0]))
202 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
206 if (padlen+2 >= elen)
209 /* ... check padding bits here. Silly. :-) */
211 if (x->encap && decap && decap->decap_type) {
212 struct esp_decap_data *encap_data;
213 struct udphdr *uh = (struct udphdr *) (iph+1);
215 encap_data = (struct esp_decap_data *) (decap->decap_data);
216 encap_data->proto = 0;
218 switch (decap->decap_type) {
219 case UDP_ENCAP_ESPINUDP:
220 case UDP_ENCAP_ESPINUDP_NON_IKE:
221 encap_data->proto = AF_INET;
222 encap_data->saddr.a4 = iph->saddr;
223 encap_data->sport = uh->source;
224 encap_len = (void*)esph - (void*)uh;
232 iph->protocol = nexthdr[1];
233 pskb_trim(skb, skb->len - alen - padlen - 2);
234 memcpy(workbuf, skb->nh.raw, iph->ihl*4);
235 skb->h.raw = skb_pull(skb, sizeof(struct ip_esp_hdr) + esp->conf.ivlen);
236 skb->nh.raw += encap_len + sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
237 memcpy(skb->nh.raw, workbuf, iph->ihl*4);
238 skb->nh.iph->tot_len = htons(skb->len);
247 static int esp_post_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
251 struct xfrm_encap_tmpl *encap;
252 struct esp_decap_data *decap_data;
255 decap_data = (struct esp_decap_data *)(decap->decap_data);
257 /* first, make sure that the decap type == the encap type */
258 if (encap->encap_type != decap->decap_type)
261 switch (encap->encap_type) {
263 case UDP_ENCAP_ESPINUDP:
264 case UDP_ENCAP_ESPINUDP_NON_IKE:
266 * 1) if the NAT-T peer's IP or port changed then
267 * advertize the change to the keying daemon.
268 * This is an inbound SA, so just compare
271 if (decap_data->proto == AF_INET &&
272 (decap_data->saddr.a4 != x->props.saddr.a4 ||
273 decap_data->sport != encap->encap_sport)) {
274 xfrm_address_t ipaddr;
276 ipaddr.a4 = decap_data->saddr.a4;
277 km_new_mapping(x, &ipaddr, decap_data->sport);
279 /* XXX: perhaps add an extra
280 * policy check here, to see
281 * if we should allow or
282 * reject a packet from a
289 * 2) ignore UDP/TCP checksums in case
290 * of NAT-T in Transport Mode, or
291 * perform other post-processing fixes
292 * as per * draft-ietf-ipsec-udp-encaps-06,
296 skb->ip_summed = CHECKSUM_UNNECESSARY;
304 static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
306 struct esp_data *esp = x->data;
307 u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
310 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
312 /* The worst case. */
315 if (esp->conf.padlen)
316 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
318 return mtu + x->props.header_len + esp->auth.icv_trunc_len;
321 static void esp4_err(struct sk_buff *skb, u32 info)
323 struct iphdr *iph = (struct iphdr*)skb->data;
324 struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
325 struct xfrm_state *x;
327 if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
328 skb->h.icmph->code != ICMP_FRAG_NEEDED)
331 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
334 NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
335 ntohl(esph->spi), ntohl(iph->daddr)));
339 static void esp_destroy(struct xfrm_state *x)
341 struct esp_data *esp = x->data;
347 crypto_free_tfm(esp->conf.tfm);
348 esp->conf.tfm = NULL;
350 if (esp->conf.ivec) {
351 kfree(esp->conf.ivec);
352 esp->conf.ivec = NULL;
355 crypto_free_tfm(esp->auth.tfm);
356 esp->auth.tfm = NULL;
358 if (esp->auth.work_icv) {
359 kfree(esp->auth.work_icv);
360 esp->auth.work_icv = NULL;
365 static int esp_init_state(struct xfrm_state *x, void *args)
367 struct esp_data *esp = NULL;
369 /* null auth and encryption can have zero length keys */
371 if (x->aalg->alg_key_len > 512)
377 esp = kmalloc(sizeof(*esp), GFP_KERNEL);
381 memset(esp, 0, sizeof(*esp));
384 struct xfrm_algo_desc *aalg_desc;
386 esp->auth.key = x->aalg->alg_key;
387 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
388 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
389 if (esp->auth.tfm == NULL)
391 esp->auth.icv = esp_hmac_digest;
393 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
396 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
397 crypto_tfm_alg_digestsize(esp->auth.tfm)) {
398 NETDEBUG(printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
400 crypto_tfm_alg_digestsize(esp->auth.tfm),
401 aalg_desc->uinfo.auth.icv_fullbits/8));
405 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
406 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
408 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
409 if (!esp->auth.work_icv)
412 esp->conf.key = x->ealg->alg_key;
413 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
414 if (x->props.ealgo == SADB_EALG_NULL)
415 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
417 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
418 if (esp->conf.tfm == NULL)
420 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
421 esp->conf.padlen = 0;
422 if (esp->conf.ivlen) {
423 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
424 if (unlikely(esp->conf.ivec == NULL))
426 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
428 if (crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len))
430 x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
432 x->props.header_len += sizeof(struct iphdr);
434 struct xfrm_encap_tmpl *encap = x->encap;
436 switch (encap->encap_type) {
439 case UDP_ENCAP_ESPINUDP:
440 x->props.header_len += sizeof(struct udphdr);
442 case UDP_ENCAP_ESPINUDP_NON_IKE:
443 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
448 x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
458 static struct xfrm_type esp_type =
460 .description = "ESP4",
461 .owner = THIS_MODULE,
462 .proto = IPPROTO_ESP,
463 .init_state = esp_init_state,
464 .destructor = esp_destroy,
465 .get_max_size = esp4_get_max_size,
467 .post_input = esp_post_input,
471 static struct net_protocol esp4_protocol = {
472 .handler = xfrm4_rcv,
473 .err_handler = esp4_err,
477 static int __init esp4_init(void)
479 struct xfrm_decap_state decap;
481 if (sizeof(struct esp_decap_data) >
482 sizeof(decap.decap_data)) {
483 extern void decap_data_too_small(void);
485 decap_data_too_small();
488 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
489 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
492 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
493 printk(KERN_INFO "ip esp init: can't add protocol\n");
494 xfrm_unregister_type(&esp_type, AF_INET);
500 static void __exit esp4_fini(void)
502 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
503 printk(KERN_INFO "ip esp close: can't remove protocol\n");
504 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
505 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
508 module_init(esp4_init);
509 module_exit(esp4_fini);
510 MODULE_LICENSE("GPL");