1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <net/inet_ecn.h>
7 #include <asm/scatterlist.h>
8 #include <linux/crypto.h>
9 #include <linux/pfkeyv2.h>
10 #include <linux/random.h>
14 /* decapsulation data for use when post-processing */
15 struct esp_decap_data {
21 int esp_output(struct sk_buff **pskb)
24 struct dst_entry *dst = (*pskb)->dst;
25 struct xfrm_state *x = dst->xfrm;
26 struct iphdr *top_iph;
27 struct ip_esp_hdr *esph;
28 struct crypto_tfm *tfm;
30 struct sk_buff *trailer;
36 /* Strip IP+ESP header. */
37 __skb_pull(*pskb, (*pskb)->h.raw - (*pskb)->data);
38 /* Now skb is pure payload to encrypt */
42 /* Round to block size */
46 alen = esp->auth.icv_trunc_len;
48 blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
49 clen = (clen + 2 + blksize-1)&~(blksize-1);
51 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
53 if ((nfrags = skb_cow_data(*pskb, clen-(*pskb)->len+alen, &trailer)) < 0)
59 for (i=0; i<clen-(*pskb)->len - 2; i++)
60 *(u8*)(trailer->tail + i) = i+1;
62 *(u8*)(trailer->tail + clen-(*pskb)->len - 2) = (clen - (*pskb)->len)-2;
63 pskb_put(*pskb, trailer, clen - (*pskb)->len);
65 __skb_push(*pskb, (*pskb)->data - (*pskb)->nh.raw);
66 top_iph = (*pskb)->nh.iph;
67 esph = (struct ip_esp_hdr *)((*pskb)->nh.raw + top_iph->ihl*4);
68 top_iph->tot_len = htons((*pskb)->len + alen);
69 *(u8*)(trailer->tail - 1) = top_iph->protocol;
71 /* this is non-NULL only with UDP Encapsulation */
73 struct xfrm_encap_tmpl *encap = x->encap;
77 uh = (struct udphdr *)esph;
78 uh->source = encap->encap_sport;
79 uh->dest = encap->encap_dport;
80 uh->len = htons((*pskb)->len + alen - top_iph->ihl*4);
83 switch (encap->encap_type) {
85 case UDP_ENCAP_ESPINUDP:
86 esph = (struct ip_esp_hdr *)(uh + 1);
88 case UDP_ENCAP_ESPINUDP_NON_IKE:
89 udpdata32 = (u32 *)(uh + 1);
90 udpdata32[0] = udpdata32[1] = 0;
91 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
95 top_iph->protocol = IPPROTO_UDP;
97 top_iph->protocol = IPPROTO_ESP;
99 esph->spi = x->id.spi;
100 esph->seq_no = htonl(++x->replay.oseq);
103 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
106 struct scatterlist *sg = &esp->sgbuf[0];
108 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
109 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
113 skb_to_sgvec(*pskb, sg, esph->enc_data+esp->conf.ivlen-(*pskb)->data, clen);
114 crypto_cipher_encrypt(tfm, sg, sg, clen);
115 if (unlikely(sg != &esp->sgbuf[0]))
119 if (esp->conf.ivlen) {
120 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
121 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
124 if (esp->auth.icv_full_len) {
125 esp->auth.icv(esp, *pskb, (u8*)esph-(*pskb)->data,
126 sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
127 pskb_put(*pskb, trailer, alen);
130 ip_send_check(top_iph);
139 * Note: detecting truncated vs. non-truncated authentication data is very
140 * expensive, so we only support truncated data, which is the recommended
143 int esp_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
146 struct ip_esp_hdr *esph;
147 struct esp_data *esp = x->data;
148 struct sk_buff *trailer;
149 int blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
150 int alen = esp->auth.icv_trunc_len;
151 int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
155 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
158 if (elen <= 0 || (elen & (blksize-1)))
161 /* If integrity check is required, do this. */
162 if (esp->auth.icv_full_len) {
163 u8 sum[esp->auth.icv_full_len];
166 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
168 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
171 if (unlikely(memcmp(sum, sum1, alen))) {
172 x->stats.integrity_failed++;
177 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
180 skb->ip_summed = CHECKSUM_NONE;
182 esph = (struct ip_esp_hdr*)skb->data;
185 /* Get ivec. This can be wrong, check against another impls. */
187 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
191 struct scatterlist *sg = &esp->sgbuf[0];
195 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
196 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
200 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
201 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
202 if (unlikely(sg != &esp->sgbuf[0]))
205 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
209 if (padlen+2 >= elen)
212 /* ... check padding bits here. Silly. :-) */
214 if (x->encap && decap && decap->decap_type) {
215 struct esp_decap_data *encap_data;
216 struct udphdr *uh = (struct udphdr *) (iph+1);
218 encap_data = (struct esp_decap_data *) (decap->decap_data);
219 encap_data->proto = 0;
221 switch (decap->decap_type) {
222 case UDP_ENCAP_ESPINUDP:
223 case UDP_ENCAP_ESPINUDP_NON_IKE:
224 encap_data->proto = AF_INET;
225 encap_data->saddr.a4 = iph->saddr;
226 encap_data->sport = uh->source;
227 encap_len = (void*)esph - (void*)uh;
235 iph->protocol = nexthdr[1];
236 pskb_trim(skb, skb->len - alen - padlen - 2);
237 memcpy(workbuf, skb->nh.raw, iph->ihl*4);
238 skb->h.raw = skb_pull(skb, sizeof(struct ip_esp_hdr) + esp->conf.ivlen);
239 skb->nh.raw += encap_len + sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
240 memcpy(skb->nh.raw, workbuf, iph->ihl*4);
241 skb->nh.iph->tot_len = htons(skb->len);
250 int esp_post_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
254 struct xfrm_encap_tmpl *encap;
255 struct esp_decap_data *decap_data;
258 decap_data = (struct esp_decap_data *)(decap->decap_data);
260 /* first, make sure that the decap type == the encap type */
261 if (encap->encap_type != decap->decap_type)
264 switch (encap->encap_type) {
266 case UDP_ENCAP_ESPINUDP:
267 case UDP_ENCAP_ESPINUDP_NON_IKE:
269 * 1) if the NAT-T peer's IP or port changed then
270 * advertize the change to the keying daemon.
271 * This is an inbound SA, so just compare
274 if (decap_data->proto == AF_INET &&
275 (decap_data->saddr.a4 != x->props.saddr.a4 ||
276 decap_data->sport != encap->encap_sport)) {
277 xfrm_address_t ipaddr;
279 ipaddr.a4 = decap_data->saddr.a4;
280 km_new_mapping(x, &ipaddr, decap_data->sport);
282 /* XXX: perhaps add an extra
283 * policy check here, to see
284 * if we should allow or
285 * reject a packet from a
292 * 2) ignore UDP/TCP checksums in case
293 * of NAT-T in Transport Mode, or
294 * perform other post-processing fixes
295 * as per * draft-ietf-ipsec-udp-encaps-06,
299 skb->ip_summed = CHECKSUM_UNNECESSARY;
307 static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
309 struct esp_data *esp = x->data;
310 u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
313 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
315 /* The worst case. */
318 if (esp->conf.padlen)
319 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
321 return mtu + x->props.header_len + esp->auth.icv_trunc_len;
324 void esp4_err(struct sk_buff *skb, u32 info)
326 struct iphdr *iph = (struct iphdr*)skb->data;
327 struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
328 struct xfrm_state *x;
330 if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
331 skb->h.icmph->code != ICMP_FRAG_NEEDED)
334 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
337 NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
338 ntohl(esph->spi), ntohl(iph->daddr)));
342 void esp_destroy(struct xfrm_state *x)
344 struct esp_data *esp = x->data;
350 crypto_free_tfm(esp->conf.tfm);
351 esp->conf.tfm = NULL;
353 if (esp->conf.ivec) {
354 kfree(esp->conf.ivec);
355 esp->conf.ivec = NULL;
358 crypto_free_tfm(esp->auth.tfm);
359 esp->auth.tfm = NULL;
361 if (esp->auth.work_icv) {
362 kfree(esp->auth.work_icv);
363 esp->auth.work_icv = NULL;
368 int esp_init_state(struct xfrm_state *x, void *args)
370 struct esp_data *esp = NULL;
372 /* null auth and encryption can have zero length keys */
374 if (x->aalg->alg_key_len > 512)
380 esp = kmalloc(sizeof(*esp), GFP_KERNEL);
384 memset(esp, 0, sizeof(*esp));
387 struct xfrm_algo_desc *aalg_desc;
389 esp->auth.key = x->aalg->alg_key;
390 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
391 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
392 if (esp->auth.tfm == NULL)
394 esp->auth.icv = esp_hmac_digest;
396 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
399 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
400 crypto_tfm_alg_digestsize(esp->auth.tfm)) {
401 NETDEBUG(printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
403 crypto_tfm_alg_digestsize(esp->auth.tfm),
404 aalg_desc->uinfo.auth.icv_fullbits/8));
408 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
409 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
411 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
412 if (!esp->auth.work_icv)
415 esp->conf.key = x->ealg->alg_key;
416 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
417 if (x->props.ealgo == SADB_EALG_NULL)
418 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
420 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
421 if (esp->conf.tfm == NULL)
423 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
424 esp->conf.padlen = 0;
425 if (esp->conf.ivlen) {
426 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
427 if (unlikely(esp->conf.ivec == NULL))
429 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
431 crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len);
432 x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
434 x->props.header_len += sizeof(struct iphdr);
436 struct xfrm_encap_tmpl *encap = x->encap;
438 switch (encap->encap_type) {
440 case UDP_ENCAP_ESPINUDP:
441 x->props.header_len += sizeof(struct udphdr);
443 case UDP_ENCAP_ESPINUDP_NON_IKE:
444 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
449 x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
455 crypto_free_tfm(esp->auth.tfm);
456 if (esp->auth.work_icv)
457 kfree(esp->auth.work_icv);
459 crypto_free_tfm(esp->conf.tfm);
465 static struct xfrm_type esp_type =
467 .description = "ESP4",
468 .owner = THIS_MODULE,
469 .proto = IPPROTO_ESP,
470 .init_state = esp_init_state,
471 .destructor = esp_destroy,
472 .get_max_size = esp4_get_max_size,
474 .post_input = esp_post_input,
478 static struct net_protocol esp4_protocol = {
479 .handler = xfrm4_rcv,
480 .err_handler = esp4_err,
484 static int __init esp4_init(void)
486 struct xfrm_decap_state decap;
488 if (sizeof(struct esp_decap_data) <
489 sizeof(decap.decap_data)) {
490 extern void decap_data_too_small(void);
492 decap_data_too_small();
495 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
496 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
499 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
500 printk(KERN_INFO "ip esp init: can't add protocol\n");
501 xfrm_unregister_type(&esp_type, AF_INET);
507 static void __exit esp4_fini(void)
509 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
510 printk(KERN_INFO "ip esp close: can't remove protocol\n");
511 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
512 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
515 module_init(esp4_init);
516 module_exit(esp4_fini);
517 MODULE_LICENSE("GPL");