1 #include <linux/module.h>
5 #include <asm/scatterlist.h>
6 #include <linux/crypto.h>
7 #include <linux/kernel.h>
8 #include <linux/pfkeyv2.h>
9 #include <linux/random.h>
11 #include <net/protocol.h>
14 static int esp_output(struct xfrm_state
*x
, struct sk_buff
*skb
)
17 struct iphdr
*top_iph
;
18 struct ip_esp_hdr
*esph
;
19 struct crypto_tfm
*tfm
;
21 struct sk_buff
*trailer
;
27 /* Strip IP+ESP header. */
28 __skb_pull(skb
, skb
->h
.raw
- skb
->data
);
29 /* Now skb is pure payload to encrypt */
33 /* Round to block size */
37 alen
= esp
->auth
.icv_trunc_len
;
39 blksize
= ALIGN(crypto_tfm_alg_blocksize(tfm
), 4);
40 clen
= ALIGN(clen
+ 2, blksize
);
42 clen
= ALIGN(clen
, esp
->conf
.padlen
);
44 if ((nfrags
= skb_cow_data(skb
, clen
-skb
->len
+alen
, &trailer
)) < 0)
50 for (i
=0; i
<clen
-skb
->len
- 2; i
++)
51 *(u8
*)(trailer
->tail
+ i
) = i
+1;
53 *(u8
*)(trailer
->tail
+ clen
-skb
->len
- 2) = (clen
- skb
->len
)-2;
54 pskb_put(skb
, trailer
, clen
- skb
->len
);
56 __skb_push(skb
, skb
->data
- skb
->nh
.raw
);
57 top_iph
= skb
->nh
.iph
;
58 esph
= (struct ip_esp_hdr
*)(skb
->nh
.raw
+ top_iph
->ihl
*4);
59 top_iph
->tot_len
= htons(skb
->len
+ alen
);
60 *(u8
*)(trailer
->tail
- 1) = top_iph
->protocol
;
62 /* this is non-NULL only with UDP Encapsulation */
64 struct xfrm_encap_tmpl
*encap
= x
->encap
;
68 uh
= (struct udphdr
*)esph
;
69 uh
->source
= encap
->encap_sport
;
70 uh
->dest
= encap
->encap_dport
;
71 uh
->len
= htons(skb
->len
+ alen
- top_iph
->ihl
*4);
74 switch (encap
->encap_type
) {
76 case UDP_ENCAP_ESPINUDP
:
77 esph
= (struct ip_esp_hdr
*)(uh
+ 1);
79 case UDP_ENCAP_ESPINUDP_NON_IKE
:
80 udpdata32
= (u32
*)(uh
+ 1);
81 udpdata32
[0] = udpdata32
[1] = 0;
82 esph
= (struct ip_esp_hdr
*)(udpdata32
+ 2);
86 top_iph
->protocol
= IPPROTO_UDP
;
88 top_iph
->protocol
= IPPROTO_ESP
;
90 esph
->spi
= x
->id
.spi
;
91 esph
->seq_no
= htonl(++x
->replay
.oseq
);
92 xfrm_aevent_doreplay(x
);
95 crypto_cipher_set_iv(tfm
, esp
->conf
.ivec
, crypto_tfm_alg_ivsize(tfm
));
98 struct scatterlist
*sg
= &esp
->sgbuf
[0];
100 if (unlikely(nfrags
> ESP_NUM_FAST_SG
)) {
101 sg
= kmalloc(sizeof(struct scatterlist
)*nfrags
, GFP_ATOMIC
);
105 skb_to_sgvec(skb
, sg
, esph
->enc_data
+esp
->conf
.ivlen
-skb
->data
, clen
);
106 crypto_cipher_encrypt(tfm
, sg
, sg
, clen
);
107 if (unlikely(sg
!= &esp
->sgbuf
[0]))
111 if (esp
->conf
.ivlen
) {
112 memcpy(esph
->enc_data
, esp
->conf
.ivec
, crypto_tfm_alg_ivsize(tfm
));
113 crypto_cipher_get_iv(tfm
, esp
->conf
.ivec
, crypto_tfm_alg_ivsize(tfm
));
116 if (esp
->auth
.icv_full_len
) {
117 esp
->auth
.icv(esp
, skb
, (u8
*)esph
-skb
->data
,
118 sizeof(struct ip_esp_hdr
) + esp
->conf
.ivlen
+clen
, trailer
->tail
);
119 pskb_put(skb
, trailer
, alen
);
122 ip_send_check(top_iph
);
131 * Note: detecting truncated vs. non-truncated authentication data is very
132 * expensive, so we only support truncated data, which is the recommended
135 static int esp_input(struct xfrm_state
*x
, struct sk_buff
*skb
)
138 struct ip_esp_hdr
*esph
;
139 struct esp_data
*esp
= x
->data
;
140 struct sk_buff
*trailer
;
141 int blksize
= ALIGN(crypto_tfm_alg_blocksize(esp
->conf
.tfm
), 4);
142 int alen
= esp
->auth
.icv_trunc_len
;
143 int elen
= skb
->len
- sizeof(struct ip_esp_hdr
) - esp
->conf
.ivlen
- alen
;
147 struct scatterlist
*sg
;
150 if (!pskb_may_pull(skb
, sizeof(struct ip_esp_hdr
)))
153 if (elen
<= 0 || (elen
& (blksize
-1)))
156 /* If integrity check is required, do this. */
157 if (esp
->auth
.icv_full_len
) {
158 u8 sum
[esp
->auth
.icv_full_len
];
161 esp
->auth
.icv(esp
, skb
, 0, skb
->len
-alen
, sum
);
163 if (skb_copy_bits(skb
, skb
->len
-alen
, sum1
, alen
))
166 if (unlikely(memcmp(sum
, sum1
, alen
))) {
167 x
->stats
.integrity_failed
++;
172 if ((nfrags
= skb_cow_data(skb
, 0, &trailer
)) < 0)
175 skb
->ip_summed
= CHECKSUM_NONE
;
177 esph
= (struct ip_esp_hdr
*)skb
->data
;
179 /* Get ivec. This can be wrong, check against another impls. */
181 crypto_cipher_set_iv(esp
->conf
.tfm
, esph
->enc_data
, crypto_tfm_alg_ivsize(esp
->conf
.tfm
));
185 if (unlikely(nfrags
> ESP_NUM_FAST_SG
)) {
186 sg
= kmalloc(sizeof(struct scatterlist
)*nfrags
, GFP_ATOMIC
);
190 skb_to_sgvec(skb
, sg
, sizeof(struct ip_esp_hdr
) + esp
->conf
.ivlen
, elen
);
191 crypto_cipher_decrypt(esp
->conf
.tfm
, sg
, sg
, elen
);
192 if (unlikely(sg
!= &esp
->sgbuf
[0]))
195 if (skb_copy_bits(skb
, skb
->len
-alen
-2, nexthdr
, 2))
199 if (padlen
+2 >= elen
)
202 /* ... check padding bits here. Silly. :-) */
208 struct xfrm_encap_tmpl
*encap
= x
->encap
;
209 struct udphdr
*uh
= (void *)(skb
->nh
.raw
+ ihl
);
212 * 1) if the NAT-T peer's IP or port changed then
213 * advertize the change to the keying daemon.
214 * This is an inbound SA, so just compare
217 if (iph
->saddr
!= x
->props
.saddr
.a4
||
218 uh
->source
!= encap
->encap_sport
) {
219 xfrm_address_t ipaddr
;
221 ipaddr
.a4
= iph
->saddr
;
222 km_new_mapping(x
, &ipaddr
, uh
->source
);
224 /* XXX: perhaps add an extra
225 * policy check here, to see
226 * if we should allow or
227 * reject a packet from a
234 * 2) ignore UDP/TCP checksums in case
235 * of NAT-T in Transport Mode, or
236 * perform other post-processing fixes
237 * as per draft-ietf-ipsec-udp-encaps-06,
241 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
244 iph
->protocol
= nexthdr
[1];
245 pskb_trim(skb
, skb
->len
- alen
- padlen
- 2);
246 skb
->h
.raw
= __skb_pull(skb
, sizeof(*esph
) + esp
->conf
.ivlen
) - ihl
;
254 static u32
esp4_get_max_size(struct xfrm_state
*x
, int mtu
)
256 struct esp_data
*esp
= x
->data
;
257 u32 blksize
= ALIGN(crypto_tfm_alg_blocksize(esp
->conf
.tfm
), 4);
260 mtu
= ALIGN(mtu
+ 2, blksize
);
262 /* The worst case. */
263 mtu
= ALIGN(mtu
+ 2, 4) + blksize
- 4;
265 if (esp
->conf
.padlen
)
266 mtu
= ALIGN(mtu
, esp
->conf
.padlen
);
268 return mtu
+ x
->props
.header_len
+ esp
->auth
.icv_trunc_len
;
271 static void esp4_err(struct sk_buff
*skb
, u32 info
)
273 struct iphdr
*iph
= (struct iphdr
*)skb
->data
;
274 struct ip_esp_hdr
*esph
= (struct ip_esp_hdr
*)(skb
->data
+(iph
->ihl
<<2));
275 struct xfrm_state
*x
;
277 if (skb
->h
.icmph
->type
!= ICMP_DEST_UNREACH
||
278 skb
->h
.icmph
->code
!= ICMP_FRAG_NEEDED
)
281 x
= xfrm_state_lookup((xfrm_address_t
*)&iph
->daddr
, esph
->spi
, IPPROTO_ESP
, AF_INET
);
284 NETDEBUG(KERN_DEBUG
"pmtu discovery on SA ESP/%08x/%08x\n",
285 ntohl(esph
->spi
), ntohl(iph
->daddr
));
289 static void esp_destroy(struct xfrm_state
*x
)
291 struct esp_data
*esp
= x
->data
;
296 crypto_free_tfm(esp
->conf
.tfm
);
297 esp
->conf
.tfm
= NULL
;
298 kfree(esp
->conf
.ivec
);
299 esp
->conf
.ivec
= NULL
;
300 crypto_free_tfm(esp
->auth
.tfm
);
301 esp
->auth
.tfm
= NULL
;
302 kfree(esp
->auth
.work_icv
);
303 esp
->auth
.work_icv
= NULL
;
307 static int esp_init_state(struct xfrm_state
*x
)
309 struct esp_data
*esp
= NULL
;
311 /* null auth and encryption can have zero length keys */
313 if (x
->aalg
->alg_key_len
> 512)
319 esp
= kzalloc(sizeof(*esp
), GFP_KERNEL
);
324 struct xfrm_algo_desc
*aalg_desc
;
326 esp
->auth
.key
= x
->aalg
->alg_key
;
327 esp
->auth
.key_len
= (x
->aalg
->alg_key_len
+7)/8;
328 esp
->auth
.tfm
= crypto_alloc_tfm(x
->aalg
->alg_name
, 0);
329 if (esp
->auth
.tfm
== NULL
)
331 esp
->auth
.icv
= esp_hmac_digest
;
333 aalg_desc
= xfrm_aalg_get_byname(x
->aalg
->alg_name
, 0);
336 if (aalg_desc
->uinfo
.auth
.icv_fullbits
/8 !=
337 crypto_tfm_alg_digestsize(esp
->auth
.tfm
)) {
338 NETDEBUG(KERN_INFO
"ESP: %s digestsize %u != %hu\n",
340 crypto_tfm_alg_digestsize(esp
->auth
.tfm
),
341 aalg_desc
->uinfo
.auth
.icv_fullbits
/8);
345 esp
->auth
.icv_full_len
= aalg_desc
->uinfo
.auth
.icv_fullbits
/8;
346 esp
->auth
.icv_trunc_len
= aalg_desc
->uinfo
.auth
.icv_truncbits
/8;
348 esp
->auth
.work_icv
= kmalloc(esp
->auth
.icv_full_len
, GFP_KERNEL
);
349 if (!esp
->auth
.work_icv
)
352 esp
->conf
.key
= x
->ealg
->alg_key
;
353 esp
->conf
.key_len
= (x
->ealg
->alg_key_len
+7)/8;
354 if (x
->props
.ealgo
== SADB_EALG_NULL
)
355 esp
->conf
.tfm
= crypto_alloc_tfm(x
->ealg
->alg_name
, CRYPTO_TFM_MODE_ECB
);
357 esp
->conf
.tfm
= crypto_alloc_tfm(x
->ealg
->alg_name
, CRYPTO_TFM_MODE_CBC
);
358 if (esp
->conf
.tfm
== NULL
)
360 esp
->conf
.ivlen
= crypto_tfm_alg_ivsize(esp
->conf
.tfm
);
361 esp
->conf
.padlen
= 0;
362 if (esp
->conf
.ivlen
) {
363 esp
->conf
.ivec
= kmalloc(esp
->conf
.ivlen
, GFP_KERNEL
);
364 if (unlikely(esp
->conf
.ivec
== NULL
))
366 get_random_bytes(esp
->conf
.ivec
, esp
->conf
.ivlen
);
368 if (crypto_cipher_setkey(esp
->conf
.tfm
, esp
->conf
.key
, esp
->conf
.key_len
))
370 x
->props
.header_len
= sizeof(struct ip_esp_hdr
) + esp
->conf
.ivlen
;
372 x
->props
.header_len
+= sizeof(struct iphdr
);
374 struct xfrm_encap_tmpl
*encap
= x
->encap
;
376 switch (encap
->encap_type
) {
379 case UDP_ENCAP_ESPINUDP
:
380 x
->props
.header_len
+= sizeof(struct udphdr
);
382 case UDP_ENCAP_ESPINUDP_NON_IKE
:
383 x
->props
.header_len
+= sizeof(struct udphdr
) + 2 * sizeof(u32
);
388 x
->props
.trailer_len
= esp4_get_max_size(x
, 0) - x
->props
.header_len
;
398 static struct xfrm_type esp_type
=
400 .description
= "ESP4",
401 .owner
= THIS_MODULE
,
402 .proto
= IPPROTO_ESP
,
403 .init_state
= esp_init_state
,
404 .destructor
= esp_destroy
,
405 .get_max_size
= esp4_get_max_size
,
410 static struct net_protocol esp4_protocol
= {
411 .handler
= xfrm4_rcv
,
412 .err_handler
= esp4_err
,
416 static int __init
esp4_init(void)
418 if (xfrm_register_type(&esp_type
, AF_INET
) < 0) {
419 printk(KERN_INFO
"ip esp init: can't add xfrm type\n");
422 if (inet_add_protocol(&esp4_protocol
, IPPROTO_ESP
) < 0) {
423 printk(KERN_INFO
"ip esp init: can't add protocol\n");
424 xfrm_unregister_type(&esp_type
, AF_INET
);
430 static void __exit
esp4_fini(void)
432 if (inet_del_protocol(&esp4_protocol
, IPPROTO_ESP
) < 0)
433 printk(KERN_INFO
"ip esp close: can't remove protocol\n");
434 if (xfrm_unregister_type(&esp_type
, AF_INET
) < 0)
435 printk(KERN_INFO
"ip esp close: can't remove xfrm type\n");
438 module_init(esp4_init
);
439 module_exit(esp4_fini
);
440 MODULE_LICENSE("GPL");