linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / net / ipv4 / esp4.c
1 #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>
8 #include <linux/kernel.h>
9 #include <linux/pfkeyv2.h>
10 #include <linux/random.h>
11 #include <net/icmp.h>
12 #include <net/protocol.h>
13 #include <net/udp.h>
14
15 static 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;
40         blksize = ALIGN(crypto_tfm_alg_blocksize(tfm), 4);
41         clen = ALIGN(clen + 2, blksize);
42         if (esp->conf.padlen)
43                 clen = ALIGN(clen, esp->conf.padlen);
44
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);
93
94         if (esp->conf.ivlen)
95                 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
96
97         do {
98                 struct scatterlist *sg = &esp->sgbuf[0];
99
100                 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
101                         sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
102                         if (!sg)
103                                 goto error;
104                 }
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]))
108                         kfree(sg);
109         } while (0);
110
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));
114         }
115
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);
120         }
121
122         ip_send_check(top_iph);
123
124         err = 0;
125
126 error:
127         return err;
128 }
129
130 /*
131  * Note: detecting truncated vs. non-truncated authentication data is very
132  * expensive, so we only support truncated data, which is the recommended
133  * and common case.
134  */
135 static int esp_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
136 {
137         struct iphdr *iph;
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;
144         int nfrags;
145         int encap_len = 0;
146         u8 nexthdr[2];
147         struct scatterlist *sg;
148         u8 workbuf[60];
149         int padlen;
150
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;
179         iph = skb->nh.iph;
180
181         /* Get ivec. This can be wrong, check against another impls. */
182         if (esp->conf.ivlen)
183                 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
184
185         sg = &esp->sgbuf[0];
186
187         if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
188                 sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
189                 if (!sg)
190                         goto out;
191         }
192         skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
193         crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
194         if (unlikely(sg != &esp->sgbuf[0]))
195                 kfree(sg);
196
197         if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
198                 BUG();
199
200         padlen = nexthdr[0];
201         if (padlen+2 >= elen)
202                 goto out;
203
204         /* ... check padding bits here. Silly. :-) */ 
205
206         if (x->encap) {
207                 struct xfrm_encap_tmpl *encap = x->encap;
208                 struct udphdr *uh;
209
210                 if (encap->encap_type != decap->decap_type)
211                         goto out;
212
213                 uh = (struct udphdr *)(iph + 1);
214                 encap_len = (void*)esph - (void*)uh;
215
216                 /*
217                  * 1) if the NAT-T peer's IP or port changed then
218                  *    advertize the change to the keying daemon.
219                  *    This is an inbound SA, so just compare
220                  *    SRC ports.
221                  */
222                 if (iph->saddr != x->props.saddr.a4 ||
223                     uh->source != encap->encap_sport) {
224                         xfrm_address_t ipaddr;
225
226                         ipaddr.a4 = iph->saddr;
227                         km_new_mapping(x, &ipaddr, uh->source);
228                                 
229                         /* XXX: perhaps add an extra
230                          * policy check here, to see
231                          * if we should allow or
232                          * reject a packet from a
233                          * different source
234                          * address/port.
235                          */
236                 }
237         
238                 /*
239                  * 2) ignore UDP/TCP checksums in case
240                  *    of NAT-T in Transport Mode, or
241                  *    perform other post-processing fixes
242                  *    as per draft-ietf-ipsec-udp-encaps-06,
243                  *    section 3.1.2
244                  */
245                 if (!x->props.mode)
246                         skb->ip_summed = CHECKSUM_UNNECESSARY;
247         }
248
249         iph->protocol = nexthdr[1];
250         pskb_trim(skb, skb->len - alen - padlen - 2);
251         memcpy(workbuf, skb->nh.raw, iph->ihl*4);
252         skb->h.raw = skb_pull(skb, sizeof(struct ip_esp_hdr) + esp->conf.ivlen);
253         skb->nh.raw += encap_len + sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
254         memcpy(skb->nh.raw, workbuf, iph->ihl*4);
255         skb->nh.iph->tot_len = htons(skb->len);
256
257         return 0;
258
259 out:
260         return -EINVAL;
261 }
262
263 static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
264 {
265         struct esp_data *esp = x->data;
266         u32 blksize = ALIGN(crypto_tfm_alg_blocksize(esp->conf.tfm), 4);
267
268         if (x->props.mode) {
269                 mtu = ALIGN(mtu + 2, blksize);
270         } else {
271                 /* The worst case. */
272                 mtu = ALIGN(mtu + 2, 4) + blksize - 4;
273         }
274         if (esp->conf.padlen)
275                 mtu = ALIGN(mtu, esp->conf.padlen);
276
277         return mtu + x->props.header_len + esp->auth.icv_trunc_len;
278 }
279
280 static void esp4_err(struct sk_buff *skb, u32 info)
281 {
282         struct iphdr *iph = (struct iphdr*)skb->data;
283         struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
284         struct xfrm_state *x;
285
286         if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
287             skb->h.icmph->code != ICMP_FRAG_NEEDED)
288                 return;
289
290         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
291         if (!x)
292                 return;
293         NETDEBUG(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
294                  ntohl(esph->spi), ntohl(iph->daddr));
295         xfrm_state_put(x);
296 }
297
298 static void esp_destroy(struct xfrm_state *x)
299 {
300         struct esp_data *esp = x->data;
301
302         if (!esp)
303                 return;
304
305         crypto_free_tfm(esp->conf.tfm);
306         esp->conf.tfm = NULL;
307         kfree(esp->conf.ivec);
308         esp->conf.ivec = NULL;
309         crypto_free_tfm(esp->auth.tfm);
310         esp->auth.tfm = NULL;
311         kfree(esp->auth.work_icv);
312         esp->auth.work_icv = NULL;
313         kfree(esp);
314 }
315
316 static int esp_init_state(struct xfrm_state *x)
317 {
318         struct esp_data *esp = NULL;
319
320         /* null auth and encryption can have zero length keys */
321         if (x->aalg) {
322                 if (x->aalg->alg_key_len > 512)
323                         goto error;
324         }
325         if (x->ealg == NULL)
326                 goto error;
327
328         esp = kmalloc(sizeof(*esp), GFP_KERNEL);
329         if (esp == NULL)
330                 return -ENOMEM;
331
332         memset(esp, 0, sizeof(*esp));
333
334         if (x->aalg) {
335                 struct xfrm_algo_desc *aalg_desc;
336
337                 esp->auth.key = x->aalg->alg_key;
338                 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
339                 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
340                 if (esp->auth.tfm == NULL)
341                         goto error;
342                 esp->auth.icv = esp_hmac_digest;
343
344                 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
345                 BUG_ON(!aalg_desc);
346
347                 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
348                     crypto_tfm_alg_digestsize(esp->auth.tfm)) {
349                         NETDEBUG(KERN_INFO "ESP: %s digestsize %u != %hu\n",
350                                  x->aalg->alg_name,
351                                  crypto_tfm_alg_digestsize(esp->auth.tfm),
352                                  aalg_desc->uinfo.auth.icv_fullbits/8);
353                         goto error;
354                 }
355
356                 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
357                 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
358
359                 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
360                 if (!esp->auth.work_icv)
361                         goto error;
362         }
363         esp->conf.key = x->ealg->alg_key;
364         esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
365         if (x->props.ealgo == SADB_EALG_NULL)
366                 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
367         else
368                 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
369         if (esp->conf.tfm == NULL)
370                 goto error;
371         esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
372         esp->conf.padlen = 0;
373         if (esp->conf.ivlen) {
374                 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
375                 if (unlikely(esp->conf.ivec == NULL))
376                         goto error;
377                 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
378         }
379         if (crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len))
380                 goto error;
381         x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
382         if (x->props.mode)
383                 x->props.header_len += sizeof(struct iphdr);
384         if (x->encap) {
385                 struct xfrm_encap_tmpl *encap = x->encap;
386
387                 switch (encap->encap_type) {
388                 default:
389                         goto error;
390                 case UDP_ENCAP_ESPINUDP:
391                         x->props.header_len += sizeof(struct udphdr);
392                         break;
393                 case UDP_ENCAP_ESPINUDP_NON_IKE:
394                         x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
395                         break;
396                 }
397         }
398         x->data = esp;
399         x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
400         return 0;
401
402 error:
403         x->data = esp;
404         esp_destroy(x);
405         x->data = NULL;
406         return -EINVAL;
407 }
408
409 static struct xfrm_type esp_type =
410 {
411         .description    = "ESP4",
412         .owner          = THIS_MODULE,
413         .proto          = IPPROTO_ESP,
414         .init_state     = esp_init_state,
415         .destructor     = esp_destroy,
416         .get_max_size   = esp4_get_max_size,
417         .input          = esp_input,
418         .output         = esp_output
419 };
420
421 static struct net_protocol esp4_protocol = {
422         .handler        =       xfrm4_rcv,
423         .err_handler    =       esp4_err,
424         .no_policy      =       1,
425 };
426
427 static int __init esp4_init(void)
428 {
429         if (xfrm_register_type(&esp_type, AF_INET) < 0) {
430                 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
431                 return -EAGAIN;
432         }
433         if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
434                 printk(KERN_INFO "ip esp init: can't add protocol\n");
435                 xfrm_unregister_type(&esp_type, AF_INET);
436                 return -EAGAIN;
437         }
438         return 0;
439 }
440
441 static void __exit esp4_fini(void)
442 {
443         if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
444                 printk(KERN_INFO "ip esp close: can't remove protocol\n");
445         if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
446                 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
447 }
448
449 module_init(esp4_init);
450 module_exit(esp4_fini);
451 MODULE_LICENSE("GPL");