2 #include <linux/module.h>
6 #include <asm/scatterlist.h>
7 #include <linux/crypto.h>
8 #include <linux/kernel.h>
9 #include <linux/pfkeyv2.h>
10 #include <linux/random.h>
12 #include <net/protocol.h>
15 static int esp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
18 struct iphdr
*top_iph
;
19 struct ip_esp_hdr
*esph
;
20 struct crypto_blkcipher
*tfm
;
21 struct blkcipher_desc desc
;
23 struct sk_buff
*trailer
;
29 /* Strip IP+ESP header. */
30 __skb_pull(skb
, skb_transport_offset(skb
));
31 /* Now skb is pure payload to encrypt */
35 /* Round to block size */
39 alen
= esp
->auth
.icv_trunc_len
;
43 blksize
= ALIGN(crypto_blkcipher_blocksize(tfm
), 4);
44 clen
= ALIGN(clen
+ 2, blksize
);
46 clen
= ALIGN(clen
, esp
->conf
.padlen
);
48 if ((nfrags
= skb_cow_data(skb
, clen
-skb
->len
+alen
, &trailer
)) < 0)
54 for (i
=0; i
<clen
-skb
->len
- 2; i
++)
55 *(u8
*)(trailer
->tail
+ i
) = i
+1;
57 *(u8
*)(trailer
->tail
+ clen
-skb
->len
- 2) = (clen
- skb
->len
)-2;
58 pskb_put(skb
, trailer
, clen
- skb
->len
);
60 __skb_push(skb
, skb
->data
- skb_network_header(skb
));
61 top_iph
= ip_hdr(skb
);
62 esph
= (struct ip_esp_hdr
*)(skb_network_header(skb
) +
64 top_iph
->tot_len
= htons(skb
->len
+ alen
);
65 *(u8
*)(trailer
->tail
- 1) = top_iph
->protocol
;
67 /* this is non-NULL only with UDP Encapsulation */
69 struct xfrm_encap_tmpl
*encap
= x
->encap
;
73 uh
= (struct udphdr
*)esph
;
74 uh
->source
= encap
->encap_sport
;
75 uh
->dest
= encap
->encap_dport
;
76 uh
->len
= htons(skb
->len
+ alen
- top_iph
->ihl
*4);
79 switch (encap
->encap_type
) {
81 case UDP_ENCAP_ESPINUDP
:
82 esph
= (struct ip_esp_hdr
*)(uh
+ 1);
84 case UDP_ENCAP_ESPINUDP_NON_IKE
:
85 udpdata32
= (__be32
*)(uh
+ 1);
86 udpdata32
[0] = udpdata32
[1] = 0;
87 esph
= (struct ip_esp_hdr
*)(udpdata32
+ 2);
91 top_iph
->protocol
= IPPROTO_UDP
;
93 top_iph
->protocol
= IPPROTO_ESP
;
95 esph
->spi
= x
->id
.spi
;
96 esph
->seq_no
= htonl(++x
->replay
.oseq
);
97 xfrm_aevent_doreplay(x
);
99 if (esp
->conf
.ivlen
) {
100 if (unlikely(!esp
->conf
.ivinitted
)) {
101 get_random_bytes(esp
->conf
.ivec
, esp
->conf
.ivlen
);
102 esp
->conf
.ivinitted
= 1;
104 crypto_blkcipher_set_iv(tfm
, esp
->conf
.ivec
, esp
->conf
.ivlen
);
108 struct scatterlist
*sg
= &esp
->sgbuf
[0];
110 if (unlikely(nfrags
> ESP_NUM_FAST_SG
)) {
111 sg
= kmalloc(sizeof(struct scatterlist
)*nfrags
, GFP_ATOMIC
);
115 skb_to_sgvec(skb
, sg
, esph
->enc_data
+esp
->conf
.ivlen
-skb
->data
, clen
);
116 err
= crypto_blkcipher_encrypt(&desc
, sg
, sg
, clen
);
117 if (unlikely(sg
!= &esp
->sgbuf
[0]))
124 if (esp
->conf
.ivlen
) {
125 memcpy(esph
->enc_data
, esp
->conf
.ivec
, esp
->conf
.ivlen
);
126 crypto_blkcipher_get_iv(tfm
, esp
->conf
.ivec
, esp
->conf
.ivlen
);
129 if (esp
->auth
.icv_full_len
) {
130 err
= esp_mac_digest(esp
, skb
, (u8
*)esph
- skb
->data
,
131 sizeof(*esph
) + esp
->conf
.ivlen
+ clen
);
132 memcpy(pskb_put(skb
, trailer
, alen
), esp
->auth
.work_icv
, alen
);
135 ip_send_check(top_iph
);
142 * Note: detecting truncated vs. non-truncated authentication data is very
143 * expensive, so we only support truncated data, which is the recommended
146 static int esp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
149 struct ip_esp_hdr
*esph
;
150 struct esp_data
*esp
= x
->data
;
151 struct crypto_blkcipher
*tfm
= esp
->conf
.tfm
;
152 struct blkcipher_desc desc
= { .tfm
= tfm
};
153 struct sk_buff
*trailer
;
154 int blksize
= ALIGN(crypto_blkcipher_blocksize(tfm
), 4);
155 int alen
= esp
->auth
.icv_trunc_len
;
156 int elen
= skb
->len
- sizeof(struct ip_esp_hdr
) - esp
->conf
.ivlen
- alen
;
160 struct scatterlist
*sg
;
164 if (!pskb_may_pull(skb
, sizeof(struct ip_esp_hdr
)))
167 if (elen
<= 0 || (elen
& (blksize
-1)))
170 /* If integrity check is required, do this. */
171 if (esp
->auth
.icv_full_len
) {
174 err
= esp_mac_digest(esp
, skb
, 0, skb
->len
- alen
);
178 if (skb_copy_bits(skb
, skb
->len
- alen
, sum
, alen
))
181 if (unlikely(memcmp(esp
->auth
.work_icv
, sum
, alen
))) {
182 x
->stats
.integrity_failed
++;
187 if ((nfrags
= skb_cow_data(skb
, 0, &trailer
)) < 0)
190 skb
->ip_summed
= CHECKSUM_NONE
;
192 esph
= (struct ip_esp_hdr
*)skb
->data
;
194 /* Get ivec. This can be wrong, check against another impls. */
196 crypto_blkcipher_set_iv(tfm
, esph
->enc_data
, esp
->conf
.ivlen
);
200 if (unlikely(nfrags
> ESP_NUM_FAST_SG
)) {
201 sg
= kmalloc(sizeof(struct scatterlist
)*nfrags
, GFP_ATOMIC
);
205 skb_to_sgvec(skb
, sg
, sizeof(struct ip_esp_hdr
) + esp
->conf
.ivlen
, elen
);
206 err
= crypto_blkcipher_decrypt(&desc
, sg
, sg
, elen
);
207 if (unlikely(sg
!= &esp
->sgbuf
[0]))
212 if (skb_copy_bits(skb
, skb
->len
-alen
-2, nexthdr
, 2))
216 if (padlen
+2 >= elen
)
219 /* ... check padding bits here. Silly. :-) */
225 struct xfrm_encap_tmpl
*encap
= x
->encap
;
226 struct udphdr
*uh
= (void *)(skb_network_header(skb
) + ihl
);
229 * 1) if the NAT-T peer's IP or port changed then
230 * advertize the change to the keying daemon.
231 * This is an inbound SA, so just compare
234 if (iph
->saddr
!= x
->props
.saddr
.a4
||
235 uh
->source
!= encap
->encap_sport
) {
236 xfrm_address_t ipaddr
;
238 ipaddr
.a4
= iph
->saddr
;
239 km_new_mapping(x
, &ipaddr
, uh
->source
);
241 /* XXX: perhaps add an extra
242 * policy check here, to see
243 * if we should allow or
244 * reject a packet from a
251 * 2) ignore UDP/TCP checksums in case
252 * of NAT-T in Transport Mode, or
253 * perform other post-processing fixes
254 * as per draft-ietf-ipsec-udp-encaps-06,
257 if (x
->props
.mode
== XFRM_MODE_TRANSPORT
||
258 x
->props
.mode
== XFRM_MODE_BEET
)
259 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
262 iph
->protocol
= nexthdr
[1];
263 pskb_trim(skb
, skb
->len
- alen
- padlen
- 2);
264 __skb_pull(skb
, sizeof(*esph
) + esp
->conf
.ivlen
);
265 skb_set_transport_header(skb
, -ihl
);
273 static u32
esp4_get_max_size(struct xfrm_state
*x
, int mtu
)
275 struct esp_data
*esp
= x
->data
;
276 u32 blksize
= ALIGN(crypto_blkcipher_blocksize(esp
->conf
.tfm
), 4);
279 switch (x
->props
.mode
) {
280 case XFRM_MODE_TUNNEL
:
281 mtu
= ALIGN(mtu
+2, blksize
);
284 case XFRM_MODE_TRANSPORT
:
286 mtu
= ALIGN(mtu
+ 2, 4) + blksize
- 4;
289 /* The worst case. */
290 enclen
= IPV4_BEET_PHMAXLEN
;
291 mtu
= ALIGN(mtu
+ enclen
+ 2, blksize
);
295 if (esp
->conf
.padlen
)
296 mtu
= ALIGN(mtu
, esp
->conf
.padlen
);
298 return mtu
+ x
->props
.header_len
+ esp
->auth
.icv_trunc_len
- enclen
;
301 static void esp4_err(struct sk_buff
*skb
, u32 info
)
303 struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
304 struct ip_esp_hdr
*esph
= (struct ip_esp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
305 struct xfrm_state
*x
;
307 if (skb
->h
.icmph
->type
!= ICMP_DEST_UNREACH
||
308 skb
->h
.icmph
->code
!= ICMP_FRAG_NEEDED
)
311 x
= xfrm_state_lookup((xfrm_address_t
*)&iph
->daddr
, esph
->spi
, IPPROTO_ESP
, AF_INET
);
314 NETDEBUG(KERN_DEBUG
"pmtu discovery on SA ESP/%08x/%08x\n",
315 ntohl(esph
->spi
), ntohl(iph
->daddr
));
319 static void esp_destroy(struct xfrm_state
*x
)
321 struct esp_data
*esp
= x
->data
;
326 crypto_free_blkcipher(esp
->conf
.tfm
);
327 esp
->conf
.tfm
= NULL
;
328 kfree(esp
->conf
.ivec
);
329 esp
->conf
.ivec
= NULL
;
330 crypto_free_hash(esp
->auth
.tfm
);
331 esp
->auth
.tfm
= NULL
;
332 kfree(esp
->auth
.work_icv
);
333 esp
->auth
.work_icv
= NULL
;
337 static int esp_init_state(struct xfrm_state
*x
)
339 struct esp_data
*esp
= NULL
;
340 struct crypto_blkcipher
*tfm
;
342 /* null auth and encryption can have zero length keys */
344 if (x
->aalg
->alg_key_len
> 512)
350 esp
= kzalloc(sizeof(*esp
), GFP_KERNEL
);
355 struct xfrm_algo_desc
*aalg_desc
;
356 struct crypto_hash
*hash
;
358 esp
->auth
.key
= x
->aalg
->alg_key
;
359 esp
->auth
.key_len
= (x
->aalg
->alg_key_len
+7)/8;
360 hash
= crypto_alloc_hash(x
->aalg
->alg_name
, 0,
365 esp
->auth
.tfm
= hash
;
366 if (crypto_hash_setkey(hash
, esp
->auth
.key
, esp
->auth
.key_len
))
369 aalg_desc
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
372 if (aalg_desc
->uinfo
.auth
.icv_fullbits
/8 !=
373 crypto_hash_digestsize(hash
)) {
374 NETDEBUG(KERN_INFO
"ESP: %s digestsize %u != %hu\n",
376 crypto_hash_digestsize(hash
),
377 aalg_desc
->uinfo
.auth
.icv_fullbits
/8);
381 esp
->auth
.icv_full_len
= aalg_desc
->uinfo
.auth
.icv_fullbits
/8;
382 esp
->auth
.icv_trunc_len
= aalg_desc
->uinfo
.auth
.icv_truncbits
/8;
384 esp
->auth
.work_icv
= kmalloc(esp
->auth
.icv_full_len
, GFP_KERNEL
);
385 if (!esp
->auth
.work_icv
)
388 esp
->conf
.key
= x
->ealg
->alg_key
;
389 esp
->conf
.key_len
= (x
->ealg
->alg_key_len
+7)/8;
390 tfm
= crypto_alloc_blkcipher(x
->ealg
->alg_name
, 0, CRYPTO_ALG_ASYNC
);
394 esp
->conf
.ivlen
= crypto_blkcipher_ivsize(tfm
);
395 esp
->conf
.padlen
= 0;
396 if (esp
->conf
.ivlen
) {
397 esp
->conf
.ivec
= kmalloc(esp
->conf
.ivlen
, GFP_KERNEL
);
398 if (unlikely(esp
->conf
.ivec
== NULL
))
400 esp
->conf
.ivinitted
= 0;
402 if (crypto_blkcipher_setkey(tfm
, esp
->conf
.key
, esp
->conf
.key_len
))
404 x
->props
.header_len
= sizeof(struct ip_esp_hdr
) + esp
->conf
.ivlen
;
405 if (x
->props
.mode
== XFRM_MODE_TUNNEL
)
406 x
->props
.header_len
+= sizeof(struct iphdr
);
408 struct xfrm_encap_tmpl
*encap
= x
->encap
;
410 switch (encap
->encap_type
) {
413 case UDP_ENCAP_ESPINUDP
:
414 x
->props
.header_len
+= sizeof(struct udphdr
);
416 case UDP_ENCAP_ESPINUDP_NON_IKE
:
417 x
->props
.header_len
+= sizeof(struct udphdr
) + 2 * sizeof(u32
);
422 x
->props
.trailer_len
= esp4_get_max_size(x
, 0) - x
->props
.header_len
;
432 static struct xfrm_type esp_type
=
434 .description
= "ESP4",
435 .owner
= THIS_MODULE
,
436 .proto
= IPPROTO_ESP
,
437 .init_state
= esp_init_state
,
438 .destructor
= esp_destroy
,
439 .get_max_size
= esp4_get_max_size
,
444 static struct net_protocol esp4_protocol
= {
445 .handler
= xfrm4_rcv
,
446 .err_handler
= esp4_err
,
450 static int __init
esp4_init(void)
452 if (xfrm_register_type(&esp_type
, AF_INET
) < 0) {
453 printk(KERN_INFO
"ip esp init: can't add xfrm type\n");
456 if (inet_add_protocol(&esp4_protocol
, IPPROTO_ESP
) < 0) {
457 printk(KERN_INFO
"ip esp init: can't add protocol\n");
458 xfrm_unregister_type(&esp_type
, AF_INET
);
464 static void __exit
esp4_fini(void)
466 if (inet_del_protocol(&esp4_protocol
, IPPROTO_ESP
) < 0)
467 printk(KERN_INFO
"ip esp close: can't remove protocol\n");
468 if (xfrm_unregister_type(&esp_type
, AF_INET
) < 0)
469 printk(KERN_INFO
"ip esp close: can't remove xfrm type\n");
472 module_init(esp4_init
);
473 module_exit(esp4_fini
);
474 MODULE_LICENSE("GPL");