Pull dock into test branch
[linux-2.6] / net / xfrm / xfrm_algo.c
1 /* 
2  * xfrm algorithm interface
3  *
4  * Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; either version 2 of the License, or (at your option) 
9  * any later version.
10  */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/pfkeyv2.h>
15 #include <linux/crypto.h>
16 #include <net/xfrm.h>
17 #if defined(CONFIG_INET_AH) || defined(CONFIG_INET_AH_MODULE) || defined(CONFIG_INET6_AH) || defined(CONFIG_INET6_AH_MODULE)
18 #include <net/ah.h>
19 #endif
20 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
21 #include <net/esp.h>
22 #endif
23 #include <asm/scatterlist.h>
24
25 /*
26  * Algorithms supported by IPsec.  These entries contain properties which
27  * are used in key negotiation and xfrm processing, and are used to verify
28  * that instantiated crypto transforms have correct parameters for IPsec
29  * purposes.
30  */
31 static struct xfrm_algo_desc aalg_list[] = {
32 {
33         .name = "hmac(digest_null)",
34         .compat = "digest_null",
35         
36         .uinfo = {
37                 .auth = {
38                         .icv_truncbits = 0,
39                         .icv_fullbits = 0,
40                 }
41         },
42         
43         .desc = {
44                 .sadb_alg_id = SADB_X_AALG_NULL,
45                 .sadb_alg_ivlen = 0,
46                 .sadb_alg_minbits = 0,
47                 .sadb_alg_maxbits = 0
48         }
49 },
50 {
51         .name = "hmac(md5)",
52         .compat = "md5",
53
54         .uinfo = {
55                 .auth = {
56                         .icv_truncbits = 96,
57                         .icv_fullbits = 128,
58                 }
59         },
60         
61         .desc = {
62                 .sadb_alg_id = SADB_AALG_MD5HMAC,
63                 .sadb_alg_ivlen = 0,
64                 .sadb_alg_minbits = 128,
65                 .sadb_alg_maxbits = 128
66         }
67 },
68 {
69         .name = "hmac(sha1)",
70         .compat = "sha1",
71
72         .uinfo = {
73                 .auth = {
74                         .icv_truncbits = 96,
75                         .icv_fullbits = 160,
76                 }
77         },
78
79         .desc = {
80                 .sadb_alg_id = SADB_AALG_SHA1HMAC,
81                 .sadb_alg_ivlen = 0,
82                 .sadb_alg_minbits = 160,
83                 .sadb_alg_maxbits = 160
84         }
85 },
86 {
87         .name = "hmac(sha256)",
88         .compat = "sha256",
89
90         .uinfo = {
91                 .auth = {
92                         .icv_truncbits = 96,
93                         .icv_fullbits = 256,
94                 }
95         },
96
97         .desc = {
98                 .sadb_alg_id = SADB_X_AALG_SHA2_256HMAC,
99                 .sadb_alg_ivlen = 0,
100                 .sadb_alg_minbits = 256,
101                 .sadb_alg_maxbits = 256
102         }
103 },
104 {
105         .name = "hmac(ripemd160)",
106         .compat = "ripemd160",
107
108         .uinfo = {
109                 .auth = {
110                         .icv_truncbits = 96,
111                         .icv_fullbits = 160,
112                 }
113         },
114
115         .desc = {
116                 .sadb_alg_id = SADB_X_AALG_RIPEMD160HMAC,
117                 .sadb_alg_ivlen = 0,
118                 .sadb_alg_minbits = 160,
119                 .sadb_alg_maxbits = 160
120         }
121 },
122 {
123         .name = "xcbc(aes)",
124
125         .uinfo = {
126                 .auth = {
127                         .icv_truncbits = 96,
128                         .icv_fullbits = 128,
129                 }
130         },
131
132         .desc = {
133                 .sadb_alg_id = SADB_X_AALG_AES_XCBC_MAC,
134                 .sadb_alg_ivlen = 0,
135                 .sadb_alg_minbits = 128,
136                 .sadb_alg_maxbits = 128
137         }
138 },
139 };
140
141 static struct xfrm_algo_desc ealg_list[] = {
142 {
143         .name = "ecb(cipher_null)",
144         .compat = "cipher_null",
145         
146         .uinfo = {
147                 .encr = {
148                         .blockbits = 8,
149                         .defkeybits = 0,
150                 }
151         },
152         
153         .desc = {
154                 .sadb_alg_id =  SADB_EALG_NULL,
155                 .sadb_alg_ivlen = 0,
156                 .sadb_alg_minbits = 0,
157                 .sadb_alg_maxbits = 0
158         }
159 },
160 {
161         .name = "cbc(des)",
162         .compat = "des",
163
164         .uinfo = {
165                 .encr = {
166                         .blockbits = 64,
167                         .defkeybits = 64,
168                 }
169         },
170
171         .desc = {
172                 .sadb_alg_id = SADB_EALG_DESCBC,
173                 .sadb_alg_ivlen = 8,
174                 .sadb_alg_minbits = 64,
175                 .sadb_alg_maxbits = 64
176         }
177 },
178 {
179         .name = "cbc(des3_ede)",
180         .compat = "des3_ede",
181
182         .uinfo = {
183                 .encr = {
184                         .blockbits = 64,
185                         .defkeybits = 192,
186                 }
187         },
188
189         .desc = {
190                 .sadb_alg_id = SADB_EALG_3DESCBC,
191                 .sadb_alg_ivlen = 8,
192                 .sadb_alg_minbits = 192,
193                 .sadb_alg_maxbits = 192
194         }
195 },
196 {
197         .name = "cbc(cast128)",
198         .compat = "cast128",
199
200         .uinfo = {
201                 .encr = {
202                         .blockbits = 64,
203                         .defkeybits = 128,
204                 }
205         },
206
207         .desc = {
208                 .sadb_alg_id = SADB_X_EALG_CASTCBC,
209                 .sadb_alg_ivlen = 8,
210                 .sadb_alg_minbits = 40,
211                 .sadb_alg_maxbits = 128
212         }
213 },
214 {
215         .name = "cbc(blowfish)",
216         .compat = "blowfish",
217
218         .uinfo = {
219                 .encr = {
220                         .blockbits = 64,
221                         .defkeybits = 128,
222                 }
223         },
224
225         .desc = {
226                 .sadb_alg_id = SADB_X_EALG_BLOWFISHCBC,
227                 .sadb_alg_ivlen = 8,
228                 .sadb_alg_minbits = 40,
229                 .sadb_alg_maxbits = 448
230         }
231 },
232 {
233         .name = "cbc(aes)",
234         .compat = "aes",
235
236         .uinfo = {
237                 .encr = {
238                         .blockbits = 128,
239                         .defkeybits = 128,
240                 }
241         },
242
243         .desc = {
244                 .sadb_alg_id = SADB_X_EALG_AESCBC,
245                 .sadb_alg_ivlen = 8,
246                 .sadb_alg_minbits = 128,
247                 .sadb_alg_maxbits = 256
248         }
249 },
250 {
251         .name = "cbc(serpent)",
252         .compat = "serpent",
253
254         .uinfo = {
255                 .encr = {
256                         .blockbits = 128,
257                         .defkeybits = 128,
258                 }
259         },
260
261         .desc = {
262                 .sadb_alg_id = SADB_X_EALG_SERPENTCBC,
263                 .sadb_alg_ivlen = 8,
264                 .sadb_alg_minbits = 128,
265                 .sadb_alg_maxbits = 256,
266         }
267 },
268 {
269         .name = "cbc(twofish)",
270         .compat = "twofish",
271                  
272         .uinfo = {
273                 .encr = {
274                         .blockbits = 128,
275                         .defkeybits = 128,
276                 }
277         },
278
279         .desc = {
280                 .sadb_alg_id = SADB_X_EALG_TWOFISHCBC,
281                 .sadb_alg_ivlen = 8,
282                 .sadb_alg_minbits = 128,
283                 .sadb_alg_maxbits = 256
284         }
285 },
286 };
287
288 static struct xfrm_algo_desc calg_list[] = {
289 {
290         .name = "deflate",
291         .uinfo = {
292                 .comp = {
293                         .threshold = 90,
294                 }
295         },
296         .desc = { .sadb_alg_id = SADB_X_CALG_DEFLATE }
297 },
298 {
299         .name = "lzs",
300         .uinfo = {
301                 .comp = {
302                         .threshold = 90,
303                 }
304         },
305         .desc = { .sadb_alg_id = SADB_X_CALG_LZS }
306 },
307 {
308         .name = "lzjh",
309         .uinfo = {
310                 .comp = {
311                         .threshold = 50,
312                 }
313         },
314         .desc = { .sadb_alg_id = SADB_X_CALG_LZJH }
315 },
316 };
317
318 static inline int aalg_entries(void)
319 {
320         return ARRAY_SIZE(aalg_list);
321 }
322
323 static inline int ealg_entries(void)
324 {
325         return ARRAY_SIZE(ealg_list);
326 }
327
328 static inline int calg_entries(void)
329 {
330         return ARRAY_SIZE(calg_list);
331 }
332
333 /* Todo: generic iterators */
334 struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id)
335 {
336         int i;
337
338         for (i = 0; i < aalg_entries(); i++) {
339                 if (aalg_list[i].desc.sadb_alg_id == alg_id) {
340                         if (aalg_list[i].available)
341                                 return &aalg_list[i];
342                         else
343                                 break;
344                 }
345         }
346         return NULL;
347 }
348 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byid);
349
350 struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id)
351 {
352         int i;
353
354         for (i = 0; i < ealg_entries(); i++) {
355                 if (ealg_list[i].desc.sadb_alg_id == alg_id) {
356                         if (ealg_list[i].available)
357                                 return &ealg_list[i];
358                         else
359                                 break;
360                 }
361         }
362         return NULL;
363 }
364 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byid);
365
366 struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id)
367 {
368         int i;
369
370         for (i = 0; i < calg_entries(); i++) {
371                 if (calg_list[i].desc.sadb_alg_id == alg_id) {
372                         if (calg_list[i].available)
373                                 return &calg_list[i];
374                         else
375                                 break;
376                 }
377         }
378         return NULL;
379 }
380 EXPORT_SYMBOL_GPL(xfrm_calg_get_byid);
381
382 static struct xfrm_algo_desc *xfrm_get_byname(struct xfrm_algo_desc *list,
383                                               int entries, u32 type, u32 mask,
384                                               char *name, int probe)
385 {
386         int i, status;
387
388         if (!name)
389                 return NULL;
390
391         for (i = 0; i < entries; i++) {
392                 if (strcmp(name, list[i].name) &&
393                     (!list[i].compat || strcmp(name, list[i].compat)))
394                         continue;
395
396                 if (list[i].available)
397                         return &list[i];
398
399                 if (!probe)
400                         break;
401
402                 status = crypto_has_alg(name, type, mask | CRYPTO_ALG_ASYNC);
403                 if (!status)
404                         break;
405
406                 list[i].available = status;
407                 return &list[i];
408         }
409         return NULL;
410 }
411
412 struct xfrm_algo_desc *xfrm_aalg_get_byname(char *name, int probe)
413 {
414         return xfrm_get_byname(aalg_list, aalg_entries(),
415                                CRYPTO_ALG_TYPE_HASH, CRYPTO_ALG_TYPE_HASH_MASK,
416                                name, probe);
417 }
418 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byname);
419
420 struct xfrm_algo_desc *xfrm_ealg_get_byname(char *name, int probe)
421 {
422         return xfrm_get_byname(ealg_list, ealg_entries(),
423                                CRYPTO_ALG_TYPE_BLKCIPHER, CRYPTO_ALG_TYPE_MASK,
424                                name, probe);
425 }
426 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byname);
427
428 struct xfrm_algo_desc *xfrm_calg_get_byname(char *name, int probe)
429 {
430         return xfrm_get_byname(calg_list, calg_entries(),
431                                CRYPTO_ALG_TYPE_COMPRESS, CRYPTO_ALG_TYPE_MASK,
432                                name, probe);
433 }
434 EXPORT_SYMBOL_GPL(xfrm_calg_get_byname);
435
436 struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx)
437 {
438         if (idx >= aalg_entries())
439                 return NULL;
440
441         return &aalg_list[idx];
442 }
443 EXPORT_SYMBOL_GPL(xfrm_aalg_get_byidx);
444
445 struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx)
446 {
447         if (idx >= ealg_entries())
448                 return NULL;
449
450         return &ealg_list[idx];
451 }
452 EXPORT_SYMBOL_GPL(xfrm_ealg_get_byidx);
453
454 /*
455  * Probe for the availability of crypto algorithms, and set the available
456  * flag for any algorithms found on the system.  This is typically called by
457  * pfkey during userspace SA add, update or register.
458  */
459 void xfrm_probe_algs(void)
460 {
461 #ifdef CONFIG_CRYPTO
462         int i, status;
463         
464         BUG_ON(in_softirq());
465
466         for (i = 0; i < aalg_entries(); i++) {
467                 status = crypto_has_hash(aalg_list[i].name, 0,
468                                          CRYPTO_ALG_ASYNC);
469                 if (aalg_list[i].available != status)
470                         aalg_list[i].available = status;
471         }
472         
473         for (i = 0; i < ealg_entries(); i++) {
474                 status = crypto_has_blkcipher(ealg_list[i].name, 0,
475                                               CRYPTO_ALG_ASYNC);
476                 if (ealg_list[i].available != status)
477                         ealg_list[i].available = status;
478         }
479         
480         for (i = 0; i < calg_entries(); i++) {
481                 status = crypto_has_comp(calg_list[i].name, 0,
482                                          CRYPTO_ALG_ASYNC);
483                 if (calg_list[i].available != status)
484                         calg_list[i].available = status;
485         }
486 #endif
487 }
488 EXPORT_SYMBOL_GPL(xfrm_probe_algs);
489
490 int xfrm_count_auth_supported(void)
491 {
492         int i, n;
493
494         for (i = 0, n = 0; i < aalg_entries(); i++)
495                 if (aalg_list[i].available)
496                         n++;
497         return n;
498 }
499 EXPORT_SYMBOL_GPL(xfrm_count_auth_supported);
500
501 int xfrm_count_enc_supported(void)
502 {
503         int i, n;
504
505         for (i = 0, n = 0; i < ealg_entries(); i++)
506                 if (ealg_list[i].available)
507                         n++;
508         return n;
509 }
510 EXPORT_SYMBOL_GPL(xfrm_count_enc_supported);
511
512 /* Move to common area: it is shared with AH. */
513
514 int skb_icv_walk(const struct sk_buff *skb, struct hash_desc *desc,
515                  int offset, int len, icv_update_fn_t icv_update)
516 {
517         int start = skb_headlen(skb);
518         int i, copy = start - offset;
519         int err;
520         struct scatterlist sg;
521
522         /* Checksum header. */
523         if (copy > 0) {
524                 if (copy > len)
525                         copy = len;
526                 
527                 sg.page = virt_to_page(skb->data + offset);
528                 sg.offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
529                 sg.length = copy;
530                 
531                 err = icv_update(desc, &sg, copy);
532                 if (unlikely(err))
533                         return err;
534                 
535                 if ((len -= copy) == 0)
536                         return 0;
537                 offset += copy;
538         }
539
540         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
541                 int end;
542
543                 BUG_TRAP(start <= offset + len);
544
545                 end = start + skb_shinfo(skb)->frags[i].size;
546                 if ((copy = end - offset) > 0) {
547                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
548
549                         if (copy > len)
550                                 copy = len;
551                         
552                         sg.page = frag->page;
553                         sg.offset = frag->page_offset + offset-start;
554                         sg.length = copy;
555                         
556                         err = icv_update(desc, &sg, copy);
557                         if (unlikely(err))
558                                 return err;
559
560                         if (!(len -= copy))
561                                 return 0;
562                         offset += copy;
563                 }
564                 start = end;
565         }
566
567         if (skb_shinfo(skb)->frag_list) {
568                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
569
570                 for (; list; list = list->next) {
571                         int end;
572
573                         BUG_TRAP(start <= offset + len);
574
575                         end = start + list->len;
576                         if ((copy = end - offset) > 0) {
577                                 if (copy > len)
578                                         copy = len;
579                                 err = skb_icv_walk(list, desc, offset-start,
580                                                    copy, icv_update);
581                                 if (unlikely(err))
582                                         return err;
583                                 if ((len -= copy) == 0)
584                                         return 0;
585                                 offset += copy;
586                         }
587                         start = end;
588                 }
589         }
590         BUG_ON(len);
591         return 0;
592 }
593 EXPORT_SYMBOL_GPL(skb_icv_walk);
594
595 #if defined(CONFIG_INET_ESP) || defined(CONFIG_INET_ESP_MODULE) || defined(CONFIG_INET6_ESP) || defined(CONFIG_INET6_ESP_MODULE)
596
597 /* Looking generic it is not used in another places. */
598
599 int
600 skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len)
601 {
602         int start = skb_headlen(skb);
603         int i, copy = start - offset;
604         int elt = 0;
605
606         if (copy > 0) {
607                 if (copy > len)
608                         copy = len;
609                 sg[elt].page = virt_to_page(skb->data + offset);
610                 sg[elt].offset = (unsigned long)(skb->data + offset) % PAGE_SIZE;
611                 sg[elt].length = copy;
612                 elt++;
613                 if ((len -= copy) == 0)
614                         return elt;
615                 offset += copy;
616         }
617
618         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
619                 int end;
620
621                 BUG_TRAP(start <= offset + len);
622
623                 end = start + skb_shinfo(skb)->frags[i].size;
624                 if ((copy = end - offset) > 0) {
625                         skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
626
627                         if (copy > len)
628                                 copy = len;
629                         sg[elt].page = frag->page;
630                         sg[elt].offset = frag->page_offset+offset-start;
631                         sg[elt].length = copy;
632                         elt++;
633                         if (!(len -= copy))
634                                 return elt;
635                         offset += copy;
636                 }
637                 start = end;
638         }
639
640         if (skb_shinfo(skb)->frag_list) {
641                 struct sk_buff *list = skb_shinfo(skb)->frag_list;
642
643                 for (; list; list = list->next) {
644                         int end;
645
646                         BUG_TRAP(start <= offset + len);
647
648                         end = start + list->len;
649                         if ((copy = end - offset) > 0) {
650                                 if (copy > len)
651                                         copy = len;
652                                 elt += skb_to_sgvec(list, sg+elt, offset - start, copy);
653                                 if ((len -= copy) == 0)
654                                         return elt;
655                                 offset += copy;
656                         }
657                         start = end;
658                 }
659         }
660         BUG_ON(len);
661         return elt;
662 }
663 EXPORT_SYMBOL_GPL(skb_to_sgvec);
664
665 /* Check that skb data bits are writable. If they are not, copy data
666  * to newly created private area. If "tailbits" is given, make sure that
667  * tailbits bytes beyond current end of skb are writable.
668  *
669  * Returns amount of elements of scatterlist to load for subsequent
670  * transformations and pointer to writable trailer skb.
671  */
672
673 int skb_cow_data(struct sk_buff *skb, int tailbits, struct sk_buff **trailer)
674 {
675         int copyflag;
676         int elt;
677         struct sk_buff *skb1, **skb_p;
678
679         /* If skb is cloned or its head is paged, reallocate
680          * head pulling out all the pages (pages are considered not writable
681          * at the moment even if they are anonymous).
682          */
683         if ((skb_cloned(skb) || skb_shinfo(skb)->nr_frags) &&
684             __pskb_pull_tail(skb, skb_pagelen(skb)-skb_headlen(skb)) == NULL)
685                 return -ENOMEM;
686
687         /* Easy case. Most of packets will go this way. */
688         if (!skb_shinfo(skb)->frag_list) {
689                 /* A little of trouble, not enough of space for trailer.
690                  * This should not happen, when stack is tuned to generate
691                  * good frames. OK, on miss we reallocate and reserve even more
692                  * space, 128 bytes is fair. */
693
694                 if (skb_tailroom(skb) < tailbits &&
695                     pskb_expand_head(skb, 0, tailbits-skb_tailroom(skb)+128, GFP_ATOMIC))
696                         return -ENOMEM;
697
698                 /* Voila! */
699                 *trailer = skb;
700                 return 1;
701         }
702
703         /* Misery. We are in troubles, going to mincer fragments... */
704
705         elt = 1;
706         skb_p = &skb_shinfo(skb)->frag_list;
707         copyflag = 0;
708
709         while ((skb1 = *skb_p) != NULL) {
710                 int ntail = 0;
711
712                 /* The fragment is partially pulled by someone,
713                  * this can happen on input. Copy it and everything
714                  * after it. */
715
716                 if (skb_shared(skb1))
717                         copyflag = 1;
718
719                 /* If the skb is the last, worry about trailer. */
720
721                 if (skb1->next == NULL && tailbits) {
722                         if (skb_shinfo(skb1)->nr_frags ||
723                             skb_shinfo(skb1)->frag_list ||
724                             skb_tailroom(skb1) < tailbits)
725                                 ntail = tailbits + 128;
726                 }
727
728                 if (copyflag ||
729                     skb_cloned(skb1) ||
730                     ntail ||
731                     skb_shinfo(skb1)->nr_frags ||
732                     skb_shinfo(skb1)->frag_list) {
733                         struct sk_buff *skb2;
734
735                         /* Fuck, we are miserable poor guys... */
736                         if (ntail == 0)
737                                 skb2 = skb_copy(skb1, GFP_ATOMIC);
738                         else
739                                 skb2 = skb_copy_expand(skb1,
740                                                        skb_headroom(skb1),
741                                                        ntail,
742                                                        GFP_ATOMIC);
743                         if (unlikely(skb2 == NULL))
744                                 return -ENOMEM;
745
746                         if (skb1->sk)
747                                 skb_set_owner_w(skb2, skb1->sk);
748
749                         /* Looking around. Are we still alive?
750                          * OK, link new skb, drop old one */
751
752                         skb2->next = skb1->next;
753                         *skb_p = skb2;
754                         kfree_skb(skb1);
755                         skb1 = skb2;
756                 }
757                 elt++;
758                 *trailer = skb1;
759                 skb_p = &skb1->next;
760         }
761
762         return elt;
763 }
764 EXPORT_SYMBOL_GPL(skb_cow_data);
765
766 void *pskb_put(struct sk_buff *skb, struct sk_buff *tail, int len)
767 {
768         if (tail != skb) {
769                 skb->data_len += len;
770                 skb->len += len;
771         }
772         return skb_put(tail, len);
773 }
774 EXPORT_SYMBOL_GPL(pskb_put);
775 #endif