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 #define MAX_SG_ONSTACK 4
16 /* decapsulation data for use when post-processing */
17 struct esp_decap_data {
23 int esp_output(struct sk_buff **pskb)
26 struct dst_entry *dst = (*pskb)->dst;
27 struct xfrm_state *x = dst->xfrm;
28 struct iphdr *iph, *top_iph;
29 struct ip_esp_hdr *esph;
30 struct crypto_tfm *tfm;
32 struct sk_buff *trailer;
33 struct udphdr *uh = NULL;
35 struct xfrm_encap_tmpl *encap = NULL;
45 if ((*pskb)->ip_summed == CHECKSUM_HW) {
46 err = skb_checksum_help(pskb, 0);
51 spin_lock_bh(&x->lock);
52 err = xfrm_check_output(x, *pskb, AF_INET);
57 /* Strip IP header in transport mode. Save it. */
59 iph = (*pskb)->nh.iph;
60 memcpy(&tmp_iph, iph, iph->ihl*4);
61 __skb_pull(*pskb, iph->ihl*4);
63 /* Now skb is pure payload to encrypt */
65 /* Round to block size */
69 alen = esp->auth.icv_trunc_len;
71 blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
72 clen = (clen + 2 + blksize-1)&~(blksize-1);
74 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
76 if ((nfrags = skb_cow_data(*pskb, clen-(*pskb)->len+alen, &trailer)) < 0)
82 for (i=0; i<clen-(*pskb)->len - 2; i++)
83 *(u8*)(trailer->tail + i) = i+1;
85 *(u8*)(trailer->tail + clen-(*pskb)->len - 2) = (clen - (*pskb)->len)-2;
86 pskb_put(*pskb, trailer, clen - (*pskb)->len);
90 iph = (*pskb)->nh.iph;
92 top_iph = (struct iphdr*)skb_push(*pskb, x->props.header_len);
93 esph = (struct ip_esp_hdr*)(top_iph+1);
94 if (encap && encap->encap_type) {
95 switch (encap->encap_type) {
96 case UDP_ENCAP_ESPINUDP:
97 uh = (struct udphdr*) esph;
98 esph = (struct ip_esp_hdr*)(uh+1);
99 top_iph->protocol = IPPROTO_UDP;
101 case UDP_ENCAP_ESPINUDP_NON_IKE:
102 uh = (struct udphdr*) esph;
103 udpdata32 = (u32*)(uh+1);
104 udpdata32[0] = udpdata32[1] = 0;
105 esph = (struct ip_esp_hdr*)(udpdata32+2);
107 top_iph->protocol = IPPROTO_UDP;
111 "esp_output(): Unhandled encap: %u\n",
113 top_iph->protocol = IPPROTO_ESP;
117 top_iph->protocol = IPPROTO_ESP;
118 *(u8*)(trailer->tail - 1) = IPPROTO_IPIP;
120 top_iph->version = 4;
121 top_iph->tos = iph->tos; /* DS disclosed */
122 if (x->props.flags & XFRM_STATE_NOECN)
123 IP_ECN_clear(top_iph);
124 top_iph->tot_len = htons((*pskb)->len + alen);
125 top_iph->frag_off = iph->frag_off&htons(IP_DF);
126 if (!(top_iph->frag_off))
127 ip_select_ident(top_iph, dst, 0);
128 top_iph->ttl = iph->ttl; /* TTL disclosed */
130 top_iph->saddr = x->props.saddr.a4;
131 top_iph->daddr = x->id.daddr.a4;
132 memset(&(IPCB(*pskb)->opt), 0, sizeof(struct ip_options));
134 esph = (struct ip_esp_hdr*)skb_push(*pskb, x->props.header_len);
135 top_iph = (struct iphdr*)skb_push(*pskb, iph->ihl*4);
136 memcpy(top_iph, &tmp_iph, iph->ihl*4);
137 if (encap && encap->encap_type) {
138 switch (encap->encap_type) {
139 case UDP_ENCAP_ESPINUDP:
140 uh = (struct udphdr*) esph;
141 esph = (struct ip_esp_hdr*)(uh+1);
142 top_iph->protocol = IPPROTO_UDP;
144 case UDP_ENCAP_ESPINUDP_NON_IKE:
145 uh = (struct udphdr*) esph;
146 udpdata32 = (u32*)(uh+1);
147 udpdata32[0] = udpdata32[1] = 0;
148 esph = (struct ip_esp_hdr*)(udpdata32+2);
150 top_iph->protocol = IPPROTO_UDP;
154 "esp_output(): Unhandled encap: %u\n",
156 top_iph->protocol = IPPROTO_ESP;
160 top_iph->protocol = IPPROTO_ESP;
162 top_iph->tot_len = htons((*pskb)->len + alen);
164 top_iph->frag_off = iph->frag_off;
165 *(u8*)(trailer->tail - 1) = iph->protocol;
168 /* this is non-NULL only with UDP Encapsulation */
170 uh->source = encap->encap_sport;
171 uh->dest = encap->encap_dport;
172 uh->len = htons((*pskb)->len + alen - sizeof(struct iphdr));
176 esph->spi = x->id.spi;
177 esph->seq_no = htonl(++x->replay.oseq);
180 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
183 struct scatterlist sgbuf[nfrags>MAX_SG_ONSTACK ? 0 : nfrags];
184 struct scatterlist *sg = sgbuf;
186 if (unlikely(nfrags > MAX_SG_ONSTACK)) {
187 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
191 skb_to_sgvec(*pskb, sg, esph->enc_data+esp->conf.ivlen-(*pskb)->data, clen);
192 crypto_cipher_encrypt(tfm, sg, sg, clen);
193 if (unlikely(sg != sgbuf))
197 if (esp->conf.ivlen) {
198 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
199 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
202 if (esp->auth.icv_full_len) {
203 esp->auth.icv(esp, *pskb, (u8*)esph-(*pskb)->data,
204 sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
205 pskb_put(*pskb, trailer, alen);
208 ip_send_check(top_iph);
210 (*pskb)->nh.raw = (*pskb)->data;
212 x->curlft.bytes += (*pskb)->len;
214 spin_unlock_bh(&x->lock);
215 if (((*pskb)->dst = dst_pop(dst)) == NULL) {
219 return NET_XMIT_BYPASS;
222 spin_unlock_bh(&x->lock);
229 * Note: detecting truncated vs. non-truncated authentication data is very
230 * expensive, so we only support truncated data, which is the recommended
233 int esp_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
236 struct ip_esp_hdr *esph;
237 struct esp_data *esp = x->data;
238 struct sk_buff *trailer;
239 int blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
240 int alen = esp->auth.icv_trunc_len;
241 int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
245 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
248 if (elen <= 0 || (elen & (blksize-1)))
251 /* If integrity check is required, do this. */
252 if (esp->auth.icv_full_len) {
253 u8 sum[esp->auth.icv_full_len];
256 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
258 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
261 if (unlikely(memcmp(sum, sum1, alen))) {
262 x->stats.integrity_failed++;
267 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
270 skb->ip_summed = CHECKSUM_NONE;
272 esph = (struct ip_esp_hdr*)skb->data;
275 /* Get ivec. This can be wrong, check against another impls. */
277 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
281 struct scatterlist sgbuf[nfrags>MAX_SG_ONSTACK ? 0 : nfrags];
282 struct scatterlist *sg = sgbuf;
286 if (unlikely(nfrags > MAX_SG_ONSTACK)) {
287 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
291 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
292 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
293 if (unlikely(sg != sgbuf))
296 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
300 if (padlen+2 >= elen)
303 /* ... check padding bits here. Silly. :-) */
305 if (x->encap && decap && decap->decap_type) {
306 struct esp_decap_data *encap_data;
307 struct udphdr *uh = (struct udphdr *) (iph+1);
309 encap_data = (struct esp_decap_data *) (decap->decap_data);
310 encap_data->proto = 0;
312 switch (decap->decap_type) {
313 case UDP_ENCAP_ESPINUDP:
314 case UDP_ENCAP_ESPINUDP_NON_IKE:
316 if ((void*)uh == (void*)esph) {
318 "esp_input(): Got ESP; expecting ESPinUDP\n");
322 encap_data->proto = AF_INET;
323 encap_data->saddr.a4 = iph->saddr;
324 encap_data->sport = uh->source;
325 encap_len = (void*)esph - (void*)uh;
326 if (encap_len != sizeof(*uh))
328 "esp_input(): UDP -> ESP: too much room: %d\n",
334 "esp_input(): processing unknown encap type: %u\n",
340 iph->protocol = nexthdr[1];
341 pskb_trim(skb, skb->len - alen - padlen - 2);
342 memcpy(workbuf, skb->nh.raw, iph->ihl*4);
343 skb->h.raw = skb_pull(skb, sizeof(struct ip_esp_hdr) + esp->conf.ivlen);
344 skb->nh.raw += encap_len + sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
345 memcpy(skb->nh.raw, workbuf, iph->ihl*4);
346 skb->nh.iph->tot_len = htons(skb->len);
355 int esp_post_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
359 struct xfrm_encap_tmpl *encap;
360 struct esp_decap_data *decap_data;
363 decap_data = (struct esp_decap_data *)(decap->decap_data);
365 /* first, make sure that the decap type == the encap type */
366 if (encap->encap_type != decap->decap_type)
369 /* Next, if we don't have an encap type, then ignore it */
370 if (!encap->encap_type)
373 switch (encap->encap_type) {
374 case UDP_ENCAP_ESPINUDP:
375 case UDP_ENCAP_ESPINUDP_NON_IKE:
377 * 1) if the NAT-T peer's IP or port changed then
378 * advertize the change to the keying daemon.
379 * This is an inbound SA, so just compare
382 if (decap_data->proto == AF_INET &&
383 (decap_data->saddr.a4 != x->props.saddr.a4 ||
384 decap_data->sport != encap->encap_sport)) {
385 xfrm_address_t ipaddr;
387 ipaddr.a4 = decap_data->saddr.a4;
388 km_new_mapping(x, &ipaddr, decap_data->sport);
390 /* XXX: perhaps add an extra
391 * policy check here, to see
392 * if we should allow or
393 * reject a packet from a
400 * 2) ignore UDP/TCP checksums in case
401 * of NAT-T in Transport Mode, or
402 * perform other post-processing fixes
403 * as per * draft-ietf-ipsec-udp-encaps-06,
407 skb->ip_summed = CHECKSUM_UNNECESSARY;
412 "esp4_post_input(): Unhandled encap type: %u\n",
420 static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
422 struct esp_data *esp = x->data;
423 u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
426 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
428 /* The worst case. */
431 if (esp->conf.padlen)
432 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
434 return mtu + x->props.header_len + esp->auth.icv_trunc_len;
437 void esp4_err(struct sk_buff *skb, u32 info)
439 struct iphdr *iph = (struct iphdr*)skb->data;
440 struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
441 struct xfrm_state *x;
443 if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
444 skb->h.icmph->code != ICMP_FRAG_NEEDED)
447 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
450 NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
451 ntohl(esph->spi), ntohl(iph->daddr)));
455 void esp_destroy(struct xfrm_state *x)
457 struct esp_data *esp = x->data;
463 crypto_free_tfm(esp->conf.tfm);
464 esp->conf.tfm = NULL;
466 if (esp->conf.ivec) {
467 kfree(esp->conf.ivec);
468 esp->conf.ivec = NULL;
471 crypto_free_tfm(esp->auth.tfm);
472 esp->auth.tfm = NULL;
474 if (esp->auth.work_icv) {
475 kfree(esp->auth.work_icv);
476 esp->auth.work_icv = NULL;
481 int esp_init_state(struct xfrm_state *x, void *args)
483 struct esp_data *esp = NULL;
485 /* null auth and encryption can have zero length keys */
487 if (x->aalg->alg_key_len > 512)
493 esp = kmalloc(sizeof(*esp), GFP_KERNEL);
497 memset(esp, 0, sizeof(*esp));
500 struct xfrm_algo_desc *aalg_desc;
502 esp->auth.key = x->aalg->alg_key;
503 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
504 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
505 if (esp->auth.tfm == NULL)
507 esp->auth.icv = esp_hmac_digest;
509 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
512 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
513 crypto_tfm_alg_digestsize(esp->auth.tfm)) {
514 NETDEBUG(printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
516 crypto_tfm_alg_digestsize(esp->auth.tfm),
517 aalg_desc->uinfo.auth.icv_fullbits/8));
521 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
522 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
524 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
525 if (!esp->auth.work_icv)
528 esp->conf.key = x->ealg->alg_key;
529 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
530 if (x->props.ealgo == SADB_EALG_NULL)
531 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
533 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
534 if (esp->conf.tfm == NULL)
536 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
537 esp->conf.padlen = 0;
538 if (esp->conf.ivlen) {
539 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
540 if (unlikely(esp->conf.ivec == NULL))
542 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
544 crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len);
545 x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
547 x->props.header_len += sizeof(struct iphdr);
549 struct xfrm_encap_tmpl *encap = x->encap;
551 if (encap->encap_type) {
552 switch (encap->encap_type) {
553 case UDP_ENCAP_ESPINUDP:
554 x->props.header_len += sizeof(struct udphdr);
556 case UDP_ENCAP_ESPINUDP_NON_IKE:
557 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
561 "esp_init_state(): Unhandled encap type: %u\n",
568 x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
574 crypto_free_tfm(esp->auth.tfm);
575 if (esp->auth.work_icv)
576 kfree(esp->auth.work_icv);
578 crypto_free_tfm(esp->conf.tfm);
584 static struct xfrm_type esp_type =
586 .description = "ESP4",
587 .owner = THIS_MODULE,
588 .proto = IPPROTO_ESP,
589 .init_state = esp_init_state,
590 .destructor = esp_destroy,
591 .get_max_size = esp4_get_max_size,
593 .post_input = esp_post_input,
597 static struct inet_protocol esp4_protocol = {
598 .handler = xfrm4_rcv,
599 .err_handler = esp4_err,
603 static int __init esp4_init(void)
605 struct xfrm_decap_state decap;
607 if (sizeof(struct esp_decap_data) <
608 sizeof(decap.decap_data)) {
609 extern void decap_data_too_small(void);
611 decap_data_too_small();
614 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
615 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
618 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
619 printk(KERN_INFO "ip esp init: can't add protocol\n");
620 xfrm_unregister_type(&esp_type, AF_INET);
626 static void __exit esp4_fini(void)
628 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
629 printk(KERN_INFO "ip esp close: can't remove protocol\n");
630 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
631 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
634 module_init(esp4_init);
635 module_exit(esp4_fini);
636 MODULE_LICENSE("GPL");