5dd38d2d4837e625384a0057ae83ccfd1c53aedc
[linux-2.6.git] / net / ipv4 / esp4.c
1 #include <linux/config.h>
2 #include <linux/module.h>
3 #include <net/inet_ecn.h>
4 #include <net/ip.h>
5 #include <net/xfrm.h>
6 #include <net/esp.h>
7 #include <asm/scatterlist.h>
8 #include <linux/crypto.h>
9 #include <linux/pfkeyv2.h>
10 #include <linux/random.h>
11 #include <net/icmp.h>
12 #include <net/udp.h>
13
14 /* decapsulation data for use when post-processing */
15 struct esp_decap_data {
16         xfrm_address_t  saddr;
17         __u16           sport;
18         __u8            proto;
19 };
20
21 int esp_output(struct sk_buff **pskb)
22 {
23         int err;
24         struct dst_entry *dst = (*pskb)->dst;
25         struct xfrm_state *x  = dst->xfrm;
26         struct iphdr *top_iph;
27         struct ip_esp_hdr *esph;
28         struct crypto_tfm *tfm;
29         struct esp_data *esp;
30         struct sk_buff *trailer;
31         int blksize;
32         int clen;
33         int alen;
34         int nfrags;
35
36         /* Strip IP+ESP header. */
37         __skb_pull(*pskb, (*pskb)->h.raw - (*pskb)->data);
38         /* Now skb is pure payload to encrypt */
39
40         err = -ENOMEM;
41
42         /* Round to block size */
43         clen = (*pskb)->len;
44
45         esp = x->data;
46         alen = esp->auth.icv_trunc_len;
47         tfm = esp->conf.tfm;
48         blksize = (crypto_tfm_alg_blocksize(tfm) + 3) & ~3;
49         clen = (clen + 2 + blksize-1)&~(blksize-1);
50         if (esp->conf.padlen)
51                 clen = (clen + esp->conf.padlen-1)&~(esp->conf.padlen-1);
52
53         if ((nfrags = skb_cow_data(*pskb, clen-(*pskb)->len+alen, &trailer)) < 0)
54                 goto error;
55
56         /* Fill padding... */
57         do {
58                 int i;
59                 for (i=0; i<clen-(*pskb)->len - 2; i++)
60                         *(u8*)(trailer->tail + i) = i+1;
61         } while (0);
62         *(u8*)(trailer->tail + clen-(*pskb)->len - 2) = (clen - (*pskb)->len)-2;
63         pskb_put(*pskb, trailer, clen - (*pskb)->len);
64
65         __skb_push(*pskb, (*pskb)->data - (*pskb)->nh.raw);
66         top_iph = (*pskb)->nh.iph;
67         esph = (struct ip_esp_hdr *)((*pskb)->nh.raw + top_iph->ihl*4);
68         top_iph->tot_len = htons((*pskb)->len + alen);
69         *(u8*)(trailer->tail - 1) = top_iph->protocol;
70
71         /* this is non-NULL only with UDP Encapsulation */
72         if (x->encap) {
73                 struct xfrm_encap_tmpl *encap = x->encap;
74                 struct udphdr *uh;
75                 u32 *udpdata32;
76
77                 uh = (struct udphdr *)esph;
78                 uh->source = encap->encap_sport;
79                 uh->dest = encap->encap_dport;
80                 uh->len = htons((*pskb)->len + alen - top_iph->ihl*4);
81                 uh->check = 0;
82
83                 switch (encap->encap_type) {
84                 default:
85                 case UDP_ENCAP_ESPINUDP:
86                         esph = (struct ip_esp_hdr *)(uh + 1);
87                         break;
88                 case UDP_ENCAP_ESPINUDP_NON_IKE:
89                         udpdata32 = (u32 *)(uh + 1);
90                         udpdata32[0] = udpdata32[1] = 0;
91                         esph = (struct ip_esp_hdr *)(udpdata32 + 2);
92                         break;
93                 }
94
95                 top_iph->protocol = IPPROTO_UDP;
96         } else
97                 top_iph->protocol = IPPROTO_ESP;
98
99         esph->spi = x->id.spi;
100         esph->seq_no = htonl(++x->replay.oseq);
101
102         if (esp->conf.ivlen)
103                 crypto_cipher_set_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
104
105         do {
106                 struct scatterlist *sg = &esp->sgbuf[0];
107
108                 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
109                         sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
110                         if (!sg)
111                                 goto error;
112                 }
113                 skb_to_sgvec(*pskb, sg, esph->enc_data+esp->conf.ivlen-(*pskb)->data, clen);
114                 crypto_cipher_encrypt(tfm, sg, sg, clen);
115                 if (unlikely(sg != &esp->sgbuf[0]))
116                         kfree(sg);
117         } while (0);
118
119         if (esp->conf.ivlen) {
120                 memcpy(esph->enc_data, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
121                 crypto_cipher_get_iv(tfm, esp->conf.ivec, crypto_tfm_alg_ivsize(tfm));
122         }
123
124         if (esp->auth.icv_full_len) {
125                 esp->auth.icv(esp, *pskb, (u8*)esph-(*pskb)->data,
126                               sizeof(struct ip_esp_hdr) + esp->conf.ivlen+clen, trailer->tail);
127                 pskb_put(*pskb, trailer, alen);
128         }
129
130         ip_send_check(top_iph);
131
132         err = 0;
133
134 error:
135         return err;
136 }
137
138 /*
139  * Note: detecting truncated vs. non-truncated authentication data is very
140  * expensive, so we only support truncated data, which is the recommended
141  * and common case.
142  */
143 int esp_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
144 {
145         struct iphdr *iph;
146         struct ip_esp_hdr *esph;
147         struct esp_data *esp = x->data;
148         struct sk_buff *trailer;
149         int blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
150         int alen = esp->auth.icv_trunc_len;
151         int elen = skb->len - sizeof(struct ip_esp_hdr) - esp->conf.ivlen - alen;
152         int nfrags;
153         int encap_len = 0;
154
155         if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr)))
156                 goto out;
157
158         if (elen <= 0 || (elen & (blksize-1)))
159                 goto out;
160
161         /* If integrity check is required, do this. */
162         if (esp->auth.icv_full_len) {
163                 u8 sum[esp->auth.icv_full_len];
164                 u8 sum1[alen];
165                 
166                 esp->auth.icv(esp, skb, 0, skb->len-alen, sum);
167
168                 if (skb_copy_bits(skb, skb->len-alen, sum1, alen))
169                         BUG();
170
171                 if (unlikely(memcmp(sum, sum1, alen))) {
172                         x->stats.integrity_failed++;
173                         goto out;
174                 }
175         }
176
177         if ((nfrags = skb_cow_data(skb, 0, &trailer)) < 0)
178                 goto out;
179
180         skb->ip_summed = CHECKSUM_NONE;
181
182         esph = (struct ip_esp_hdr*)skb->data;
183         iph = skb->nh.iph;
184
185         /* Get ivec. This can be wrong, check against another impls. */
186         if (esp->conf.ivlen)
187                 crypto_cipher_set_iv(esp->conf.tfm, esph->enc_data, crypto_tfm_alg_ivsize(esp->conf.tfm));
188
189         {
190                 u8 nexthdr[2];
191                 struct scatterlist *sg = &esp->sgbuf[0];
192                 u8 workbuf[60];
193                 int padlen;
194
195                 if (unlikely(nfrags > ESP_NUM_FAST_SG)) {
196                         sg = kmalloc(sizeof(struct scatterlist)*nfrags, GFP_ATOMIC);
197                         if (!sg)
198                                 goto out;
199                 }
200                 skb_to_sgvec(skb, sg, sizeof(struct ip_esp_hdr) + esp->conf.ivlen, elen);
201                 crypto_cipher_decrypt(esp->conf.tfm, sg, sg, elen);
202                 if (unlikely(sg != &esp->sgbuf[0]))
203                         kfree(sg);
204
205                 if (skb_copy_bits(skb, skb->len-alen-2, nexthdr, 2))
206                         BUG();
207
208                 padlen = nexthdr[0];
209                 if (padlen+2 >= elen)
210                         goto out;
211
212                 /* ... check padding bits here. Silly. :-) */ 
213
214                 if (x->encap && decap && decap->decap_type) {
215                         struct esp_decap_data *encap_data;
216                         struct udphdr *uh = (struct udphdr *) (iph+1);
217
218                         encap_data = (struct esp_decap_data *) (decap->decap_data);
219                         encap_data->proto = 0;
220
221                         switch (decap->decap_type) {
222                         case UDP_ENCAP_ESPINUDP:
223                         case UDP_ENCAP_ESPINUDP_NON_IKE:
224                                 encap_data->proto = AF_INET;
225                                 encap_data->saddr.a4 = iph->saddr;
226                                 encap_data->sport = uh->source;
227                                 encap_len = (void*)esph - (void*)uh;
228                                 break;
229
230                         default:
231                                 goto out;
232                         }
233                 }
234
235                 iph->protocol = nexthdr[1];
236                 pskb_trim(skb, skb->len - alen - padlen - 2);
237                 memcpy(workbuf, skb->nh.raw, iph->ihl*4);
238                 skb->h.raw = skb_pull(skb, sizeof(struct ip_esp_hdr) + esp->conf.ivlen);
239                 skb->nh.raw += encap_len + sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
240                 memcpy(skb->nh.raw, workbuf, iph->ihl*4);
241                 skb->nh.iph->tot_len = htons(skb->len);
242         }
243
244         return 0;
245
246 out:
247         return -EINVAL;
248 }
249
250 int esp_post_input(struct xfrm_state *x, struct xfrm_decap_state *decap, struct sk_buff *skb)
251 {
252   
253         if (x->encap) {
254                 struct xfrm_encap_tmpl *encap;
255                 struct esp_decap_data *decap_data;
256
257                 encap = x->encap;
258                 decap_data = (struct esp_decap_data *)(decap->decap_data);
259
260                 /* first, make sure that the decap type == the encap type */
261                 if (encap->encap_type != decap->decap_type)
262                         return -EINVAL;
263
264                 switch (encap->encap_type) {
265                 default:
266                 case UDP_ENCAP_ESPINUDP:
267                 case UDP_ENCAP_ESPINUDP_NON_IKE:
268                         /*
269                          * 1) if the NAT-T peer's IP or port changed then
270                          *    advertize the change to the keying daemon.
271                          *    This is an inbound SA, so just compare
272                          *    SRC ports.
273                          */
274                         if (decap_data->proto == AF_INET &&
275                             (decap_data->saddr.a4 != x->props.saddr.a4 ||
276                              decap_data->sport != encap->encap_sport)) {
277                                 xfrm_address_t ipaddr;
278
279                                 ipaddr.a4 = decap_data->saddr.a4;
280                                 km_new_mapping(x, &ipaddr, decap_data->sport);
281                                         
282                                 /* XXX: perhaps add an extra
283                                  * policy check here, to see
284                                  * if we should allow or
285                                  * reject a packet from a
286                                  * different source
287                                  * address/port.
288                                  */
289                         }
290                 
291                         /*
292                          * 2) ignore UDP/TCP checksums in case
293                          *    of NAT-T in Transport Mode, or
294                          *    perform other post-processing fixes
295                          *    as per * draft-ietf-ipsec-udp-encaps-06,
296                          *    section 3.1.2
297                          */
298                         if (!x->props.mode)
299                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
300
301                         break;
302                 }
303         }
304         return 0;
305 }
306
307 static u32 esp4_get_max_size(struct xfrm_state *x, int mtu)
308 {
309         struct esp_data *esp = x->data;
310         u32 blksize = crypto_tfm_alg_blocksize(esp->conf.tfm);
311
312         if (x->props.mode) {
313                 mtu = (mtu + 2 + blksize-1)&~(blksize-1);
314         } else {
315                 /* The worst case. */
316                 mtu += 2 + blksize;
317         }
318         if (esp->conf.padlen)
319                 mtu = (mtu + esp->conf.padlen-1)&~(esp->conf.padlen-1);
320
321         return mtu + x->props.header_len + esp->auth.icv_trunc_len;
322 }
323
324 void esp4_err(struct sk_buff *skb, u32 info)
325 {
326         struct iphdr *iph = (struct iphdr*)skb->data;
327         struct ip_esp_hdr *esph = (struct ip_esp_hdr*)(skb->data+(iph->ihl<<2));
328         struct xfrm_state *x;
329
330         if (skb->h.icmph->type != ICMP_DEST_UNREACH ||
331             skb->h.icmph->code != ICMP_FRAG_NEEDED)
332                 return;
333
334         x = xfrm_state_lookup((xfrm_address_t *)&iph->daddr, esph->spi, IPPROTO_ESP, AF_INET);
335         if (!x)
336                 return;
337         NETDEBUG(printk(KERN_DEBUG "pmtu discovery on SA ESP/%08x/%08x\n",
338                         ntohl(esph->spi), ntohl(iph->daddr)));
339         xfrm_state_put(x);
340 }
341
342 void esp_destroy(struct xfrm_state *x)
343 {
344         struct esp_data *esp = x->data;
345
346         if (!esp)
347                 return;
348
349         if (esp->conf.tfm) {
350                 crypto_free_tfm(esp->conf.tfm);
351                 esp->conf.tfm = NULL;
352         }
353         if (esp->conf.ivec) {
354                 kfree(esp->conf.ivec);
355                 esp->conf.ivec = NULL;
356         }
357         if (esp->auth.tfm) {
358                 crypto_free_tfm(esp->auth.tfm);
359                 esp->auth.tfm = NULL;
360         }
361         if (esp->auth.work_icv) {
362                 kfree(esp->auth.work_icv);
363                 esp->auth.work_icv = NULL;
364         }
365         kfree(esp);
366 }
367
368 int esp_init_state(struct xfrm_state *x, void *args)
369 {
370         struct esp_data *esp = NULL;
371
372         /* null auth and encryption can have zero length keys */
373         if (x->aalg) {
374                 if (x->aalg->alg_key_len > 512)
375                         goto error;
376         }
377         if (x->ealg == NULL)
378                 goto error;
379
380         esp = kmalloc(sizeof(*esp), GFP_KERNEL);
381         if (esp == NULL)
382                 return -ENOMEM;
383
384         memset(esp, 0, sizeof(*esp));
385
386         if (x->aalg) {
387                 struct xfrm_algo_desc *aalg_desc;
388
389                 esp->auth.key = x->aalg->alg_key;
390                 esp->auth.key_len = (x->aalg->alg_key_len+7)/8;
391                 esp->auth.tfm = crypto_alloc_tfm(x->aalg->alg_name, 0);
392                 if (esp->auth.tfm == NULL)
393                         goto error;
394                 esp->auth.icv = esp_hmac_digest;
395
396                 aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name);
397                 BUG_ON(!aalg_desc);
398
399                 if (aalg_desc->uinfo.auth.icv_fullbits/8 !=
400                     crypto_tfm_alg_digestsize(esp->auth.tfm)) {
401                         NETDEBUG(printk(KERN_INFO "ESP: %s digestsize %u != %hu\n",
402                                x->aalg->alg_name,
403                                crypto_tfm_alg_digestsize(esp->auth.tfm),
404                                aalg_desc->uinfo.auth.icv_fullbits/8));
405                         goto error;
406                 }
407
408                 esp->auth.icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
409                 esp->auth.icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
410
411                 esp->auth.work_icv = kmalloc(esp->auth.icv_full_len, GFP_KERNEL);
412                 if (!esp->auth.work_icv)
413                         goto error;
414         }
415         esp->conf.key = x->ealg->alg_key;
416         esp->conf.key_len = (x->ealg->alg_key_len+7)/8;
417         if (x->props.ealgo == SADB_EALG_NULL)
418                 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_ECB);
419         else
420                 esp->conf.tfm = crypto_alloc_tfm(x->ealg->alg_name, CRYPTO_TFM_MODE_CBC);
421         if (esp->conf.tfm == NULL)
422                 goto error;
423         esp->conf.ivlen = crypto_tfm_alg_ivsize(esp->conf.tfm);
424         esp->conf.padlen = 0;
425         if (esp->conf.ivlen) {
426                 esp->conf.ivec = kmalloc(esp->conf.ivlen, GFP_KERNEL);
427                 if (unlikely(esp->conf.ivec == NULL))
428                         goto error;
429                 get_random_bytes(esp->conf.ivec, esp->conf.ivlen);
430         }
431         crypto_cipher_setkey(esp->conf.tfm, esp->conf.key, esp->conf.key_len);
432         x->props.header_len = sizeof(struct ip_esp_hdr) + esp->conf.ivlen;
433         if (x->props.mode)
434                 x->props.header_len += sizeof(struct iphdr);
435         if (x->encap) {
436                 struct xfrm_encap_tmpl *encap = x->encap;
437
438                 switch (encap->encap_type) {
439                 default:
440                 case UDP_ENCAP_ESPINUDP:
441                         x->props.header_len += sizeof(struct udphdr);
442                         break;
443                 case UDP_ENCAP_ESPINUDP_NON_IKE:
444                         x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
445                         break;
446                 }
447         }
448         x->data = esp;
449         x->props.trailer_len = esp4_get_max_size(x, 0) - x->props.header_len;
450         return 0;
451
452 error:
453         if (esp) {
454                 if (esp->auth.tfm)
455                         crypto_free_tfm(esp->auth.tfm);
456                 if (esp->auth.work_icv)
457                         kfree(esp->auth.work_icv);
458                 if (esp->conf.tfm)
459                         crypto_free_tfm(esp->conf.tfm);
460                 kfree(esp);
461         }
462         return -EINVAL;
463 }
464
465 static struct xfrm_type esp_type =
466 {
467         .description    = "ESP4",
468         .owner          = THIS_MODULE,
469         .proto          = IPPROTO_ESP,
470         .init_state     = esp_init_state,
471         .destructor     = esp_destroy,
472         .get_max_size   = esp4_get_max_size,
473         .input          = esp_input,
474         .post_input     = esp_post_input,
475         .output         = esp_output
476 };
477
478 static struct net_protocol esp4_protocol = {
479         .handler        =       xfrm4_rcv,
480         .err_handler    =       esp4_err,
481         .no_policy      =       1,
482 };
483
484 static int __init esp4_init(void)
485 {
486         struct xfrm_decap_state decap;
487
488         if (sizeof(struct esp_decap_data)  <
489             sizeof(decap.decap_data)) {
490                 extern void decap_data_too_small(void);
491
492                 decap_data_too_small();
493         }
494
495         if (xfrm_register_type(&esp_type, AF_INET) < 0) {
496                 printk(KERN_INFO "ip esp init: can't add xfrm type\n");
497                 return -EAGAIN;
498         }
499         if (inet_add_protocol(&esp4_protocol, IPPROTO_ESP) < 0) {
500                 printk(KERN_INFO "ip esp init: can't add protocol\n");
501                 xfrm_unregister_type(&esp_type, AF_INET);
502                 return -EAGAIN;
503         }
504         return 0;
505 }
506
507 static void __exit esp4_fini(void)
508 {
509         if (inet_del_protocol(&esp4_protocol, IPPROTO_ESP) < 0)
510                 printk(KERN_INFO "ip esp close: can't remove protocol\n");
511         if (xfrm_unregister_type(&esp_type, AF_INET) < 0)
512                 printk(KERN_INFO "ip esp close: can't remove xfrm type\n");
513 }
514
515 module_init(esp4_init);
516 module_exit(esp4_fini);
517 MODULE_LICENSE("GPL");