blob: 9bbdd4494551347b43bce292bb4406198aec4b18 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <linux/config.h>
2#include <linux/module.h>
3#include <net/ip.h>
4#include <net/xfrm.h>
5#include <net/esp.h>
6#include <asm/scatterlist.h>
7#include <linux/crypto.h>
Herbert Xua02a6422005-10-10 21:11:08 -07008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/pfkeyv2.h>
10#include <linux/random.h>
11#include <net/icmp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020012#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <net/udp.h>
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
16{
17 int err;
18 struct iphdr *top_iph;
19 struct ip_esp_hdr *esph;
20 struct crypto_tfm *tfm;
21 struct esp_data *esp;
22 struct sk_buff *trailer;
23 int blksize;
24 int clen;
25 int alen;
26 int nfrags;
27
28 /* Strip IP+ESP header. */
29 __skb_pull(skb, skb->h.raw - skb->data);
30 /* Now skb is pure payload to encrypt */
31
32 err = -ENOMEM;
33
34 /* Round to block size */
35 clen = skb->len;
36
37 esp = x->data;
38 alen = esp->auth.icv_trunc_len;
39 tfm = esp->conf.tfm;
Herbert Xua02a6422005-10-10 21:11:08 -070040 blksize = ALIGN(crypto_tfm_alg_blocksize(tfm), 4);
41 clen = ALIGN(clen + 2, blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (esp->conf.padlen)
Herbert Xua02a6422005-10-10 21:11:08 -070043 clen = ALIGN(clen, esp->conf.padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 if ((nfrags = skb_cow_data(skb, clen-skb->len+alen, &trailer)) < 0)
46 goto error;
47
48 /* Fill padding... */
49 do {
50 int i;
51 for (i=0; i<clen-skb->len - 2; i++)
52 *(u8*)(trailer->tail + i) = i+1;
53 } while (0);
54 *(u8*)(trailer->tail + clen-skb->len - 2) = (clen - skb->len)-2;
55 pskb_put(skb, trailer, clen - skb->len);
56
57 __skb_push(skb, skb->data - skb->nh.raw);
58 top_iph = skb->nh.iph;
59 esph = (struct ip_esp_hdr *)(skb->nh.raw + top_iph->ihl*4);
60 top_iph->tot_len = htons(skb->len + alen);
61 *(u8*)(trailer->tail - 1) = top_iph->protocol;
62
63 /* this is non-NULL only with UDP Encapsulation */
64 if (x->encap) {
65 struct xfrm_encap_tmpl *encap = x->encap;
66 struct udphdr *uh;
67 u32 *udpdata32;
68
69 uh = (struct udphdr *)esph;
70 uh->source = encap->encap_sport;
71 uh->dest = encap->encap_dport;
72 uh->len = htons(skb->len + alen - top_iph->ihl*4);
73 uh->check = 0;
74
75 switch (encap->encap_type) {
76 default:
77 case UDP_ENCAP_ESPINUDP:
78 esph = (struct ip_esp_hdr *)(uh + 1);
79 break;
80 case UDP_ENCAP_ESPINUDP_NON_IKE:
81 udpdata32 = (u32 *)(uh + 1);
82 udpdata32[0] = udpdata32[1] = 0;
83 esph = (struct ip_esp_hdr *)(udpdata32 + 2);
84 break;
85 }
86
87 top_iph->protocol = IPPROTO_UDP;
88 } else
89 top_iph->protocol = IPPROTO_ESP;
90
91 esph->spi = x->id.spi;
92 esph->seq_no = htonl(++x->replay.oseq);
Jamal Hadi Salim9500e8a2006-03-20 19:15:29 -080093 xfrm_aevent_doreplay(x);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 if (esp->conf.ivlen)
96 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
97
98 do {
99 struct scatterlist *sg = &esp->sgbuf[0];
100
101 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
102 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
103 if (!sg)
104 goto error;
105 }
106 skb_to_sgvec(skb, sg, esph->enc_data+esp->conf.ivlen-skb->data, clen);
107 crypto_cipher_encrypt(tfm, sg, sg, clen);
108 if (unlikely(sg != &esp->sgbuf[0]))
109 kfree(sg);
110 } while (0);
111
112 if (esp->conf.ivlen) {
113 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
114 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
115 }
116
117 if (esp->auth.icv_full_len) {
118 esp->auth.icv(esp, skb, (u8*)esph-skb->data,
119 sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
120 pskb_put(skb, trailer, alen);
121 }
122
123 ip_send_check(top_iph);
124
125 err = 0;
126
127error:
128 return err;
129}
130
131/*
132 * Note: detecting truncated vs. non-truncated authentication data is very
133 * expensive, so we only support truncated data, which is the recommended
134 * and common case.
135 */
Herbert Xue6956332006-04-01 00:52:46 -0800136static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 struct iphdr *iph;
139 struct ip_esp_hdr *esph;
140 struct esp_data *esp = x->data;
141 struct sk_buff *trailer;
Herbert Xud4875b02005-10-10 21:11:34 -0700142 int blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int alen = esp->auth.icv_trunc_len;
144 int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
145 int nfrags;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700146 int ihl;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800147 u8 nexthdr[2];
148 struct scatterlist *sg;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800149 int padlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
152 goto out;
153
154 if (elen <= 0 || (elen & (blksize-1)))
155 goto out;
156
157 /* If integrity check is required, do this. */
158 if (esp->auth.icv_full_len) {
159 u8 sum[esp->auth.icv_full_len];
160 u8 sum1[alen];
161
162 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
163
164 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
165 BUG();
166
167 if (unlikely(memcmp(sum, sum1, alen))) {
168 x->stats.integrity_failed++;
169 goto out;
170 }
171 }
172
173 if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
174 goto out;
175
176 skb->ip_summed = CHECKSUM_NONE;
177
178 esph = (struct ip_esp_hdr*)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* Get ivec. This can be wrong, check against another impls. */
181 if (esp->conf.ivlen)
182 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
183
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800184 sg = &esp->sgbuf[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800186 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
187 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
188 if (!sg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800191 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
192 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
193 if (unlikely(sg != &esp->sgbuf[0]))
194 kfree(sg);
195
196 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
197 BUG();
198
199 padlen = nexthdr[0];
200 if (padlen+2 >= elen)
201 goto out;
202
203 /* ... check padding bits here. Silly. :-) */
204
Herbert Xu31a4ab92006-05-27 23:06:13 -0700205 iph = skb->nh.iph;
206 ihl = iph->ihl * 4;
207
Herbert Xu752c1f42006-02-27 13:00:40 -0800208 if (x->encap) {
209 struct xfrm_encap_tmpl *encap = x->encap;
Herbert Xu31a4ab92006-05-27 23:06:13 -0700210 struct udphdr *uh = (void *)(skb->nh.raw + ihl);
Herbert Xu752c1f42006-02-27 13:00:40 -0800211
212 /*
213 * 1) if the NAT-T peer's IP or port changed then
214 * advertize the change to the keying daemon.
215 * This is an inbound SA, so just compare
216 * SRC ports.
217 */
218 if (iph->saddr != x->props.saddr.a4 ||
219 uh->source != encap->encap_sport) {
220 xfrm_address_t ipaddr;
221
222 ipaddr.a4 = iph->saddr;
223 km_new_mapping(x, &ipaddr, uh->source);
224
225 /* XXX: perhaps add an extra
226 * policy check here, to see
227 * if we should allow or
228 * reject a packet from a
229 * different source
230 * address/port.
231 */
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800232 }
Herbert Xu752c1f42006-02-27 13:00:40 -0800233
234 /*
235 * 2) ignore UDP/TCP checksums in case
236 * of NAT-T in Transport Mode, or
237 * perform other post-processing fixes
238 * as per draft-ietf-ipsec-udp-encaps-06,
239 * section 3.1.2
240 */
241 if (!x->props.mode)
242 skb->ip_summed = CHECKSUM_UNNECESSARY;
Herbert Xu4bf05ec2006-02-27 13:00:01 -0800243 }
244
245 iph->protocol = nexthdr[1];
246 pskb_trim(skb, skb->len - alen - padlen - 2);
Herbert Xu31a4ab92006-05-27 23:06:13 -0700247 skb->h.raw = __skb_pull(skb, sizeof(*esph) + esp->conf.ivlen) - ihl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return 0;
250
251out:
252 return -EINVAL;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
256{
257 struct esp_data *esp = x->data;
Herbert Xud4875b02005-10-10 21:11:34 -0700258 u32 blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 if (x->props.mode) {
Herbert Xua02a6422005-10-10 21:11:08 -0700261 mtu = ALIGN(mtu + 2, blksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 } else {
263 /* The worst case. */
Herbert Xud4875b02005-10-10 21:11:34 -0700264 mtu = ALIGN(mtu + 2, 4) + blksize - 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
266 if (esp->conf.padlen)
Herbert Xua02a6422005-10-10 21:11:08 -0700267 mtu = ALIGN(mtu, esp->conf.padlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 return mtu + x->props.header_len + esp->auth.icv_trunc_len;
270}
271
272static void esp4_err(struct sk_buff *skb, u32 info)
273{
274 struct iphdr *iph = (struct iphdr*)skb->data;
275 struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
276 struct xfrm_state *x;
277
278 if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
279 skb->h.icmph->code != ICMP_FRAG_NEEDED)
280 return;
281
282 x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
283 if (!x)
284 return;
Patrick McHardy64ce2072005-08-09 20:50:53 -0700285 NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
286 ntohl(esph->spi), ntohl(iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 xfrm_state_put(x);
288}
289
290static void esp_destroy(struct xfrm_state *x)
291{
292 struct esp_data *esp = x->data;
293
294 if (!esp)
295 return;
296
Jesper Juhl573dbd92005-09-01 17:44:29 -0700297 crypto_free_tfm(esp->conf.tfm);
298 esp->conf.tfm = NULL;
299 kfree(esp->conf.ivec);
300 esp->conf.ivec = NULL;
301 crypto_free_tfm(esp->auth.tfm);
302 esp->auth.tfm = NULL;
303 kfree(esp->auth.work_icv);
304 esp->auth.work_icv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 kfree(esp);
306}
307
Herbert Xu72cb6962005-06-20 13:18:08 -0700308static int esp_init_state(struct xfrm_state *x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 struct esp_data *esp = NULL;
311
312 /* null auth and encryption can have zero length keys */
313 if (x->aalg) {
314 if (x->aalg->alg_key_len > 512)
315 goto error;
316 }
317 if (x->ealg == NULL)
318 goto error;
319
320 esp = kmalloc(sizeof(*esp), GFP_KERNEL);
321 if (esp == NULL)
322 return -ENOMEM;
323
324 memset(esp, 0, sizeof(*esp));
325
326 if (x->aalg) {
327 struct xfrm_algo_desc *aalg_desc;
328
329 esp->auth.key = x->aalg->alg_key;
330 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
331 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
332 if (esp->auth.tfm == NULL)
333 goto error;
334 esp->auth.icv = esp_hmac_digest;
335
336 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
337 BUG_ON(!aalg_desc);
338
339 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
340 crypto_tfm_alg_digestsize(esp->auth.tfm)) {
Patrick McHardy64ce2072005-08-09 20:50:53 -0700341 NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
342 x->aalg->alg_name,
343 crypto_tfm_alg_digestsize(esp->auth.tfm),
344 aalg_desc->uinfo.auth.icv_fullbits/8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 goto error;
346 }
347
348 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
349 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
350
351 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
352 if (!esp->auth.work_icv)
353 goto error;
354 }
355 esp->conf.key = x->ealg->alg_key;
356 esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
357 if (x->props.ealgo == SADB_EALG_NULL)
358 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
359 else
360 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
361 if (esp->conf.tfm == NULL)
362 goto error;
363 esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
364 esp->conf.padlen = 0;
365 if (esp->conf.ivlen) {
366 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
367 if (unlikely(esp->conf.ivec == NULL))
368 goto error;
369 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
370 }
371 if (crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len))
372 goto error;
373 x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
374 if (x->props.mode)
375 x->props.header_len += sizeof(struct iphdr);
376 if (x->encap) {
377 struct xfrm_encap_tmpl *encap = x->encap;
378
379 switch (encap->encap_type) {
380 default:
381 goto error;
382 case UDP_ENCAP_ESPINUDP:
383 x->props.header_len += sizeof(struct udphdr);
384 break;
385 case UDP_ENCAP_ESPINUDP_NON_IKE:
386 x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
387 break;
388 }
389 }
390 x->data = esp;
391 x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
392 return 0;
393
394error:
395 x->data = esp;
396 esp_destroy(x);
397 x->data = NULL;
398 return -EINVAL;
399}
400
401static struct xfrm_type esp_type =
402{
403 .description = "ESP4",
404 .owner = THIS_MODULE,
405 .proto = IPPROTO_ESP,
406 .init_state = esp_init_state,
407 .destructor = esp_destroy,
408 .get_max_size = esp4_get_max_size,
409 .input = esp_input,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 .output = esp_output
411};
412
413static struct net_protocol esp4_protocol = {
414 .handler = xfrm4_rcv,
415 .err_handler = esp4_err,
416 .no_policy = 1,
417};
418
419static int __init esp4_init(void)
420{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (xfrm_register_type(&esp_type, AF_INET) < 0) {
422 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
423 return -EAGAIN;
424 }
425 if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
426 printk(KERN_INFO "ip esp init: can't add protocol\n");
427 xfrm_unregister_type(&esp_type, AF_INET);
428 return -EAGAIN;
429 }
430 return 0;
431}
432
433static void __exit esp4_fini(void)
434{
435 if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
436 printk(KERN_INFO "ip esp close: can't remove protocol\n");
437 if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
438 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
439}
440
441module_init(esp4_init);
442module_exit(esp4_fini);
443MODULE_LICENSE("GPL");