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