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