1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
|
#include <linux/err.h>
#include <linux/module.h>
#include <net/ip.h>
#include <net/xfrm.h>
#include <net/ah.h>
#include <linux/crypto.h>
#include <linux/pfkeyv2.h>
#include <linux/spinlock.h>
#include <net/icmp.h>
#include <net/protocol.h>
#include <crypto/authenc.h>
#include <linux/highmem.h>
#include <crypto/hash.h>
#define DEBUG_AH
#ifndef DEBUG_AH
# define AH_DUMP_PKT print_hex_dump
#else
# define AH_DUMP_PKT(arg...)
#endif
/**
* @brief SKB private data for AH stored in skb cb field
*
* @tmp_req - temporary ahash/aead request
* @icv_trunc_len - AH ICV length for software AH
* @nh - Next header for hardware offload AH
*
*/
struct ah_skb_cb {
void *tmp_req;
u16 icv_trunc_len;
u8 nh;
};
#define AH_SKB_CB(__skb) ((struct ah_skb_cb *)&((__skb)->cb[0]))
/**
* @brief AH work buffer (union) for software AH
* @iph - IP header access
* @buf - byte address access
* @note Used to save IP header and IP options
*
*/
union ah_tmp_iph {
struct iphdr iph;
char buf[60];
};
#define AH_WORK_BUF_MAX_LEN sizeof(union ah_tmp_iph)
/*
* Allocate an ahash request structure with extra space for structure
* ah_tmp_iph (scatch pad), ICV (input save ICV), working ICV
* (space for hash algorithm to store ICV), and SG.
*
*/
static void *ah_alloc_tmp(struct crypto_ahash *ahash, int nfrags)
{
unsigned int len;
len = AH_WORK_BUF_MAX_LEN;
len += MAX_AH_AUTH_LEN;
len += crypto_ahash_digestsize(ahash);
len += sizeof(struct ahash_request) + crypto_ahash_reqsize(ahash);
len += ALIGN(len, __alignof__(struct scatterlist));
len += sizeof(struct scatterlist) * nfrags;
return kmalloc(len, GFP_ATOMIC);
}
static inline void ah_free_tmp(void *tmp)
{
kfree(tmp);
}
static inline union ah_tmp_iph *ah_tmp_work_buf(void *tmp)
{
return tmp;
}
static inline u8 *ah_tmp_icv(union ah_tmp_iph *tmp)
{
return (u8 *) (tmp + 1);
}
static inline u8 *ah_tmp_work_icv(u8 *tmp)
{
return tmp + MAX_AH_AUTH_LEN;
}
static inline struct ahash_request *ah_tmp_req(struct crypto_ahash *ahash,
u8 *tmp)
{
struct ahash_request *req = (struct ahash_request *) (tmp +
crypto_ahash_digestsize(ahash));
ahash_request_set_tfm(req, ahash);
return req;
}
static inline struct scatterlist *ah_tmp_sg(struct crypto_ahash *ahash,
struct ahash_request *req)
{
return (void *) ALIGN((unsigned long) (req + 1) +
crypto_ahash_reqsize(ahash),
__alignof__(struct scatterlist));
}
/*
* Allocate an aead request structure with extra space for structure
* SG.
*
*/
static void *ah_alloc_aead_tmp(struct crypto_aead *aead, int nfrags)
{
unsigned int len;
len = sizeof(struct aead_request) + crypto_aead_reqsize(aead);
len += ALIGN(len, __alignof__(struct scatterlist));
len += sizeof(struct scatterlist) * nfrags;
return kmalloc(len, GFP_ATOMIC);
}
static inline void ah_free_aead_tmp(void *tmp)
{
kfree(tmp);
}
static inline struct aead_request *ah_tmp_aead_req(struct crypto_aead *aead,
void *tmp)
{
struct aead_request *req = (struct aead_request *) tmp;
aead_request_set_tfm(req, aead);
return req;
}
static inline struct scatterlist *ah_tmp_aead_sg(struct crypto_aead *aead,
struct aead_request *req)
{
return (void *) ALIGN((unsigned long) (req + 1) +
crypto_aead_reqsize(aead),
__alignof__(struct scatterlist));
}
static inline struct scatterlist *ah_tmp_aead_dsg(struct scatterlist *sg,
unsigned int nfrags)
{
return (void *) ((unsigned long) sg +
sizeof(struct scatterlist) * nfrags);
}
/* Clear mutable options and find final destination to substitute
* into IP header for icv calculation. Options are already checked
* for validity, so paranoia is not required. */
int ip_clear_mutable_options(struct iphdr *iph, __be32 *daddr)
{
unsigned char * optptr = (unsigned char*)(iph+1);
int l = iph->ihl*4 - sizeof(struct iphdr);
int optlen;
while (l > 0) {
switch (*optptr) {
case IPOPT_END:
return 0;
case IPOPT_NOOP:
l--;
optptr++;
continue;
}
optlen = optptr[1];
if (optlen<2 || optlen>l)
return -EINVAL;
switch (*optptr) {
case IPOPT_SEC:
case 0x85: /* Some "Extended Security" crap. */
case IPOPT_CIPSO:
case IPOPT_RA:
case 0x80|21: /* RFC1770 */
break;
case IPOPT_LSRR:
case IPOPT_SSRR:
if (optlen < 6)
return -EINVAL;
memcpy(daddr, optptr+optlen-4, 4);
/* Fall through */
default:
memset(optptr, 0, optlen);
}
l -= optlen;
optptr += optlen;
}
return 0;
}
EXPORT_SYMBOL_GPL(ip_clear_mutable_options);
/*******************************************************************************
* AH Software Functions
*
*******************************************************************************
*/
static int ah_output_done2(struct sk_buff *skb, int err)
{
void *req_tmp = AH_SKB_CB(skb)->tmp_req;
struct iphdr *iph;
struct iphdr *top_iph;
union ah_tmp_iph *tmp_iph;
struct ip_auth_hdr *ah;
char *icv;
char *work_icv;
if (err < 0)
goto out;
tmp_iph = ah_tmp_work_buf(req_tmp);
icv = ah_tmp_icv(tmp_iph);
work_icv = ah_tmp_work_icv(icv);
iph = &tmp_iph->iph;
top_iph = ip_hdr(skb);
ah = ip_auth_hdr(skb);
/* Set ICV in AH header */
memcpy(ah->auth_data, work_icv, AH_SKB_CB(skb)->icv_trunc_len);
/* Restore mute fields */
top_iph->tos = iph->tos;
top_iph->ttl = iph->ttl;
top_iph->frag_off = iph->frag_off;
if (top_iph->ihl != 5) {
top_iph->daddr = iph->daddr;
memcpy(top_iph+1, iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
}
AH_DUMP_PKT(KERN_INFO, "AH output sw done: ", DUMP_PREFIX_ADDRESS,
16, 4, skb->data, skb->len, 1);
out:
kfree(req_tmp);
return err;
}
static void ah_output_done(struct crypto_async_request *base, int err)
{
struct sk_buff *skb = base->data;
xfrm_output_resume(skb, ah_output_done2(skb, err));
}
static int ah_output_sw(struct xfrm_state *x, struct sk_buff *skb)
{
int err;
struct iphdr *iph, *top_iph;
struct ip_auth_hdr *ah;
struct ah_data *ahp;
struct ahash_request *areq;
struct scatterlist *sg;
int nfrags;
void *req_tmp = NULL;
union ah_tmp_iph *tmp_iph;
char *icv;
char *work_icv;
struct sk_buff *trailer;
/* SKB transport, network, and mac header pointers are set by
transport or tunnel modules.
Transport Input:
-----------------------
| IP | Rsvd | Payload |
-----------------------
^
|
skb.data
Tunnel Input:
----------------------------------------
| Outer IP | Rsvd | Inner IP | Payload |
----------------------------------------
^
|
skb.data
*/
AH_DUMP_PKT(KERN_INFO, "AH output sw : ", DUMP_PREFIX_ADDRESS,
16, 4, skb->data, skb->len, 1);
skb_push(skb, -skb_network_offset(skb));
/* Find # of fragments */
if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
goto error;
nfrags = err;
/* Allocate temp request */
ahp = x->data;
req_tmp = ah_alloc_tmp(ahp->utfm.atfm, nfrags);
if (!req_tmp) {
err = -ENOMEM;
goto error;
}
AH_SKB_CB(skb)->tmp_req = req_tmp;
AH_SKB_CB(skb)->icv_trunc_len = ahp->icv_trunc_len;
tmp_iph = ah_tmp_work_buf(req_tmp);
icv = ah_tmp_icv(tmp_iph);
work_icv = ah_tmp_work_icv(icv);
areq = ah_tmp_req(ahp->utfm.atfm, work_icv);
sg = ah_tmp_sg(ahp->utfm.atfm, areq);
top_iph = ip_hdr(skb);
iph = &tmp_iph->iph;
/* Save IP header to compute hash */
iph->tos = top_iph->tos;
iph->ttl = top_iph->ttl;
iph->frag_off = top_iph->frag_off;
if (top_iph->ihl != 5) {
if ((top_iph->ihl << 2) > AH_WORK_BUF_MAX_LEN) {
err = -EINVAL;
goto error;
}
iph->daddr = top_iph->daddr;
memcpy(iph+1, top_iph+1, top_iph->ihl*4 - sizeof(struct iphdr));
err = ip_clear_mutable_options(top_iph, &top_iph->daddr);
if (err)
goto error;
}
/* Set AH header */
ah = ip_auth_hdr(skb);
ah->nexthdr = *skb_mac_header(skb);
*skb_mac_header(skb) = IPPROTO_AH;
/* Mute field for hash */
top_iph->tos = 0;
top_iph->tot_len = htons(skb->len);
top_iph->frag_off = 0;
top_iph->ttl = 0;
top_iph->check = 0;
/* Set AH fields */
ah->hdrlen = (XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len) >> 2) - 2;
ah->reserved = 0;
ah->spi = x->id.spi;
ah->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output);
/* Mute AH for hash */
memset(ah->auth_data, 0, ahp->icv_trunc_len);
/* Setup SG for hash op */
sg_init_table(sg, nfrags);
skb_to_sgvec(skb, sg, 0, skb->len);
ahash_request_set_callback(areq, 0, ah_output_done, skb);
ahash_request_set_crypt(areq, sg, work_icv, skb->len);
err = crypto_ahash_digest(areq);
if (err == -EINPROGRESS)
goto out;
if (err < 0)
goto error;
return ah_output_done2(skb, err);
error:
if (req_tmp)
ah_free_tmp(req_tmp);
out:
return err;
}
static int ah_input_done2(struct sk_buff *skb, int err)
{
void *req_tmp = AH_SKB_CB(skb)->tmp_req;
struct iphdr *top_iph;
struct ip_auth_hdr *ah;
union ah_tmp_iph *tmp_iph;
int ah_hlen;
int ihl;
char *icv;
char *work_icv;
int nexthdr;
if (err < 0)
goto out;
tmp_iph = ah_tmp_work_buf(req_tmp);
icv = ah_tmp_icv(tmp_iph);
work_icv = ah_tmp_work_icv(icv);
/* Verify ICV */
if (memcmp(icv, work_icv, AH_SKB_CB(skb)->icv_trunc_len)) {
err = -EBADMSG;
goto out;
}
top_iph = ip_hdr(skb);
ihl = top_iph->ihl << 2;
ah = (struct ip_auth_hdr *) ((u8 *) top_iph + ihl);
nexthdr = ah->nexthdr;
ah_hlen = (ah->hdrlen + 2) << 2;
/* Remove AH header */
skb->network_header += ah_hlen;
memcpy(skb_network_header(skb), tmp_iph->buf, ihl);
skb->transport_header = skb->network_header;
__skb_pull(skb, ah_hlen + ihl);
err = nexthdr;
AH_DUMP_PKT(KERN_INFO, "AH input sw done: ", DUMP_PREFIX_ADDRESS,
16, 4, skb->data, skb->len, 1);
out:
kfree(req_tmp);
return err;
}
static void ah_input_done(struct crypto_async_request *base, int err)
{
struct sk_buff *skb = base->data;
xfrm_input_resume(skb, ah_input_done2(skb, err));
}
static int ah_input_sw(struct xfrm_state *x, struct sk_buff *skb)
{
int ah_hlen;
int ihl;
int nexthdr;
int err = -EINVAL;
struct iphdr *iph;
struct ip_auth_hdr *ah;
struct ah_data *ahp;
struct sk_buff *trailer;
struct ahash_request *areq;
struct scatterlist *sg;
union ah_tmp_iph *tmp_iph;
int nfrags;
void *req_tmp = NULL;
char *icv;
char *work_icv;
/* SKB transport, network, and mac header pointers are set by
transport or tunnel modules.
Transport Input:
-----------------------
| IP | AH | Payload |
-----------------------
^
|
skb.data
Tunnel Input:
----------------------------------------
| Outer IP | AH | Inner IP | Payload |
----------------------------------------
^
|
skb.data
*/
AH_DUMP_PKT(KERN_INFO, "AH input sw : ", DUMP_PREFIX_ADDRESS,
16, 4, skb->data, skb->len, 1);
if (!pskb_may_pull(skb, sizeof(*ah)))
goto error;
ah = (struct ip_auth_hdr *)skb->data;
ahp = x->data;
nexthdr = ah->nexthdr;
ah_hlen = (ah->hdrlen + 2) << 2;
if (ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_full_len) &&
ah_hlen != XFRM_ALIGN8(sizeof(*ah) + ahp->icv_trunc_len))
goto error;
if (!pskb_may_pull(skb, ah_hlen))
goto error;
/* We are going to _remove_ AH header to keep sockets happy,
* so... Later this can change. */
if (skb_cloned(skb) &&
pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
goto error;
/* Find # of fragment */
if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
goto error;
nfrags = err;
skb->ip_summed = CHECKSUM_NONE;
ah = (struct ip_auth_hdr *)skb->data;
iph = ip_hdr(skb);
/* Allocate temp ahash request */
req_tmp = ah_alloc_tmp(ahp->utfm.atfm, nfrags);
if (!req_tmp) {
err = -ENOMEM;
goto error;
}
AH_SKB_CB(skb)->tmp_req = req_tmp;
AH_SKB_CB(skb)->icv_trunc_len = ahp->icv_trunc_len;
tmp_iph = ah_tmp_work_buf(req_tmp);
icv = ah_tmp_icv(tmp_iph);
work_icv = ah_tmp_work_icv(icv);
areq = ah_tmp_req(ahp->utfm.atfm, work_icv);
sg = ah_tmp_sg(ahp->utfm.atfm, areq);
ihl = skb->data - skb_network_header(skb);
if (ihl > AH_WORK_BUF_MAX_LEN) {
err = -EBADMSG;
goto error;
}
/* Save IP header for hash computation */
memcpy(tmp_iph->buf, iph, ihl);
/* Mute fields for hash op */
iph->ttl = 0;
iph->tos = 0;
iph->frag_off = 0;
iph->check = 0;
if (ihl > sizeof(*iph)) {
__be32 dummy;
if (ip_clear_mutable_options(iph, &dummy))
goto error;
}
/* Save ICV */
memcpy(icv, ah->auth_data, ahp->icv_trunc_len);
/* Mute ICV for hash op */
memset(ah->auth_data, 0, ahp->icv_trunc_len);
/* Add back IP header for SG */
skb_push(skb, ihl);
/* Setup SG */
sg_init_table(sg, nfrags);
skb_to_sgvec(skb, sg, 0, skb->len);
ahash_request_set_callback(areq, 0, ah_input_done, skb);
ahash_request_set_crypt(areq, sg, work_icv, skb->len);
err = crypto_ahash_digest(areq);
if (err == -EINPROGRESS)
goto out;
if (err < 0)
goto error;
return ah_input_done2(skb, err);
error:
if (req_tmp)
ah_free_tmp(req_tmp);
out:
return err;
}
/*******************************************************************************
* AH HW Offload Functions
*
*******************************************************************************
*/
static int ah_output_done2_hw(struct sk_buff *skb, int err)
{
void *req_tmp = AH_SKB_CB(skb)->tmp_req;
if (err < 0)
goto out;
AH_DUMP_PKT(KERN_INFO, "AH output hw: ", DUMP_PREFIX_ADDRESS,
16, 4, skb->data, skb->len, 1);
out:
kfree(req_tmp);
return err;
}
static void ah_output_done_hw(struct crypto_async_request *base, int err)
{
struct sk_buff *skb = base->data;
xfrm_output_resume(skb, ah_output_done2_hw(skb, err));
}
static int ah_output_hw(struct xfrm_state *x, struct sk_buff *skb)
{
struct ah_data *ahp;
struct aead_request *areq;
struct scatterlist *sg;
struct scatterlist *dsg;
struct sk_buff *trailer;
void *req_tmp = NULL;
int err;
int nfrags;
unsigned int clen;
/* For AH transport mode, skb.data is at IP header. skb.len
includes IP header and payload. skb network header, transport
header, and mac headers are updated by transport module code.
Input:
--------------------------------------------
| Network Hdr| Transport Hdr| IP | Payload |
--------------------------------------------
^
|
skb.data
For AH tunnel mode, outer IP header is formed by tunnel module.
skb network header, transport header, and mac header are updated
by tunnel module code.
Input:
-----------------------------------------------------
| Outer IP | Rsvd | inner IP Header | Payload |
-----------------------------------------------------
^
|
skb.data
*/
ahp = x->data;
/* Find # fragment */
if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
goto error;
nfrags = err;
/* Allocate temp request */
req_tmp = ah_alloc_aead_tmp(ahp->utfm.aeadtfm, 2 * nfrags);
if (!req_tmp) {
err = -ENOMEM;
goto error;
}
AH_SKB_CB(skb)->tmp_req = req_tmp;
areq = ah_tmp_aead_req(ahp->utfm.aeadtfm, req_tmp);
sg = ah_tmp_aead_sg(ahp->utfm.aeadtfm, areq);
dsg = ah_tmp_aead_dsg(sg, nfrags);
/* Set up SG - data will start at IP (inner) header (skb.data) */
sg_init_table(sg, nfrags);
skb_to_sgvec(skb, sg, 0, skb->len);
clen = skb->len;
skb_push(skb, -skb_network_offset(skb));
skb_to_sgvec(skb, dsg, 0, skb->len);
aead_request_set_callback(areq, 0, ah_output_done_hw, skb);
aead_request_set_crypt(areq, sg, dsg, clen, NULL);
/* For AH transport mode, SG is at IP header.
Input:
----------------------
| Rsvd| IP | Payload |
----------------------
Rsvd - space reserved for moved IP and added AH
Output:
---------------------
| IP | AH | Payload |
---------------------
For AH tunnel mode, outer IP header is formed by tunnel module.
SG is at inner IP header.
Input:
----------------------------------------
| Outer IP | Rsvd | inner IP | Payload |
----------------------------------------
Rsvd - space reserved for added AH
Output:
----------------------------------------
| Outer IP | AH | inner IP | Payload |
----------------------------------------
*/
err = crypto_aead_encrypt(areq);
if (err == -EINPROGRESS)
goto out;
if (err < 0)
goto error;
return ah_output_done2_hw(skb, err);
error:
if (req_tmp)
ah_free_tmp(req_tmp);
out:
return err;
}
static int ah_input_done2_hw(struct sk_buff *skb, int err)
{
void *req_tmp = AH_SKB_CB(skb)->tmp_req;
if (err < 0)
goto out;
err = AH_SKB_CB(skb)->nh;
AH_DUMP_PKT(KERN_INFO, "AH input hw: ", DUMP_PREFIX_ADDRESS,
16, 4, skb->data, skb->len, 1);
out:
kfree(req_tmp);
return err;
}
static void ah_input_done_hw(struct crypto_async_request *base, int err)
{
struct sk_buff *skb = base->data;
xfrm_input_resume(skb, ah_input_done2_hw(skb, err));
}
static int ah_input_hw(struct xfrm_state *x, struct sk_buff *skb)
{
int ah_hlen;
int ihl;
int err = -EINVAL;
struct ip_auth_hdr *ah;
struct ah_data *ahp;
struct sk_buff *trailer;
struct aead_request *areq;
struct scatterlist *sg;
struct scatterlist *dsg;
int nfrags;
void *req_tmp = NULL;
/* For AH transport/tunnel mode, skb.data is at AH header. skb.len
includes payload. skb network header, transport header, and
mac headers will be updated by transport module code.
Transport Input:
-------------------------
| IP Hdr | AH | Payload |
-------------------------
^
|
skb.data and length start here
Tunnel Input:
------------------------------------
|Outer IP | AH | inner IP | Payload|
------------------------------------
^
|
skb.data and length start here
*/
AH_DUMP_PKT(KERN_INFO, "AH input hw : ", DUMP_PREFIX_ADDRESS,
16, 4, skb->data, skb->len, 1);
if (skb_cloned(skb) &&
pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
goto error;
/* Find # of fragment */
if ((err = skb_cow_data(skb, 0, &trailer)) < 0)
goto error;
nfrags = err;
skb->ip_summed = CHECKSUM_NONE;
ihl = skb->data - skb_network_header(skb);
ah = (struct ip_auth_hdr *) skb->data;
ah_hlen = (ah->hdrlen + 2) << 2;
AH_SKB_CB(skb)->nh = ah->nexthdr;
/* Allocate temp request */
ahp = x->data;
req_tmp = ah_alloc_aead_tmp(ahp->utfm.aeadtfm, 2 * nfrags);
if (!req_tmp) {
err = -ENOMEM;
goto error;
}
AH_SKB_CB(skb)->tmp_req = req_tmp;
areq = ah_tmp_aead_req(ahp->utfm.aeadtfm, req_tmp);
sg = ah_tmp_aead_sg(ahp->utfm.aeadtfm, areq);
dsg = ah_tmp_aead_dsg(sg, nfrags);
/* Init SG - data starts at AH header */
sg_init_table(sg, nfrags);
skb_to_sgvec(skb, sg, -ihl, skb->len + ihl);
skb->network_header += ah_hlen;
skb->transport_header = skb->network_header;
__skb_pull(skb, ah_hlen);
skb_to_sgvec(skb, dsg, -ihl, skb->len + ihl);
aead_request_set_callback(areq, 0, ah_input_done_hw, skb);
aead_request_set_crypt(areq, sg, dsg, skb->len + ah_hlen + ihl, NULL);
/* For AH transport/tunnel mode, SG is at IP header.
Transport Input:
----------------------------
| IP Hdr | AH | Payload |
----------------------------
IP Hdr - start of SG
Transport Output:
----------------------------
| | IP Hdr | Payload |
----------------------------
Tunnel Input:
-------------------------------------
| Outer IP | AH | inner IP | Payload|
-------------------------------------
Outer IP Hdr - start of SG
Tunnel Output:
-------------------------------------
| Outer IP | AH | inner IP | Payload|
-------------------------------------
Outer IP and AH left un-touch
*/
err = crypto_aead_decrypt(areq);
if (err == -EINPROGRESS)
goto out;
if (err < 0)
goto error;
return ah_input_done2(skb, err);
error:
if (req_tmp)
ah_free_tmp(req_tmp);
out:
return err;
}
static int ah_output(struct xfrm_state *x, struct sk_buff *skb)
{
if ((x->alg_flags & XFRM_ALGO_FLAGS_OFFLOAD_AH) &&
(x->alg_flags & (XFRM_ALGO_FLAGS_OFFLOAD_TUNNEL |
XFRM_ALGO_FLAGS_OFFLOAD_TRANPORT)))
return ah_output_hw(x, skb);
else
return ah_output_sw(x, skb);
}
static int ah_input(struct xfrm_state *x, struct sk_buff *skb)
{
if ((x->alg_flags & XFRM_ALGO_FLAGS_OFFLOAD_AH) &&
(x->alg_flags & (XFRM_ALGO_FLAGS_OFFLOAD_TUNNEL |
XFRM_ALGO_FLAGS_OFFLOAD_TRANPORT)))
return ah_input_hw(x, skb);
else
return ah_input_sw(x, skb);
}
static void ah4_err(struct sk_buff *skb, u32 info)
{
struct net *net = dev_net(skb->dev);
struct iphdr *iph = (struct iphdr*)skb->data;
struct ip_auth_hdr *ah = (struct ip_auth_hdr*)(skb->data+(iph->ihl<<2));
struct xfrm_state *x;
if (icmp_hdr(skb)->type != ICMP_DEST_UNREACH ||
icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
return;
x = xfrm_state_lookup(net, (xfrm_address_t *)&iph->daddr, ah->spi, IPPROTO_AH, AF_INET);
if (!x)
return;
printk(KERN_DEBUG "pmtu discovery on SA AH/%08x/%08x\n",
ntohl(ah->spi), ntohl(iph->daddr));
xfrm_state_put(x);
}
static int ah_init_state(struct xfrm_state *x)
{
struct ah_data *ahp = NULL;
struct xfrm_algo_desc *aalg_desc;
struct crypto_ahash *ahashtfm;
struct crypto_aead *aeadtfm;
char alg_name[CRYPTO_MAX_ALG_NAME];
char *key;
int key_len;
int digest_size;
struct rtattr *rta;
struct ah_param {
__be32 spi;
__be32 seq;
} *param;
if (!x->aalg)
goto error;
if (x->encap)
goto error;
ahp = kzalloc(sizeof(*ahp), GFP_KERNEL);
if (ahp == NULL)
return -ENOMEM;
/* Try AH hardware offload first */
switch (x->props.mode) {
case XFRM_MODE_TUNNEL:
snprintf(alg_name, ARRAY_SIZE(alg_name),
"tunnel(ah(%s))", x->aalg->alg_name);
x->alg_flags |= XFRM_ALGO_FLAGS_OFFLOAD_TUNNEL
| XFRM_ALGO_FLAGS_OFFLOAD_AH;
break;
case XFRM_MODE_TRANSPORT:
snprintf(alg_name, ARRAY_SIZE(alg_name),
"transport(ah(%s))", x->aalg->alg_name);
x->alg_flags |= XFRM_ALGO_FLAGS_OFFLOAD_TRANPORT
| XFRM_ALGO_FLAGS_OFFLOAD_AH;
break;
default:
strncpy(alg_name, x->aalg->alg_name, ARRAY_SIZE(alg_name));
break;
}
if (x->alg_flags & XFRM_ALGO_FLAGS_OFFLOAD_AH) {
aeadtfm = crypto_alloc_aead(alg_name, 0, 0);
if (IS_ERR(aeadtfm)) {
/* No AH hardware offload, go to software AH */
x->alg_flags &= ~(XFRM_ALGO_FLAGS_OFFLOAD_TUNNEL
| XFRM_ALGO_FLAGS_OFFLOAD_TRANPORT
| XFRM_ALGO_FLAGS_OFFLOAD_AH);
aeadtfm = NULL;
ahashtfm = crypto_alloc_ahash(x->aalg->alg_name, 0, 0);
if (IS_ERR(ahashtfm))
goto error;
ahp->utfm.atfm = ahashtfm;
} else {
ahashtfm = NULL;
ahp->utfm.aeadtfm = aeadtfm;
}
} else {
aeadtfm = NULL;
ahashtfm = crypto_alloc_ahash(alg_name, 0, 0);
if (IS_ERR(ahashtfm))
goto error;
ahp->utfm.atfm = ahashtfm;
}
if (x->alg_flags & XFRM_ALGO_FLAGS_OFFLOAD_AH) {
/* For AH offload, we must load AH offload parameters
via setkey function. */
key_len = RTA_SPACE(sizeof(*param)) +
((x->aalg->alg_key_len + 7) / 8);
key = kmalloc(key_len, GFP_KERNEL);
rta = (void *) key;
rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
rta->rta_len = RTA_LENGTH(sizeof(*param));
param = RTA_DATA(rta);
param->spi = cpu_to_be32(x->id.spi);
param->seq = cpu_to_be32(x->replay.oseq);
memcpy(key + RTA_SPACE(sizeof(*param)),
x->aalg->alg_key,
(x->aalg->alg_key_len + 7) / 8);
if (crypto_aead_setkey(aeadtfm, key, key_len))
goto error;
digest_size = crypto_aead_tfm(aeadtfm)->__crt_alg->
cra_aead.maxauthsize;
} else {
key_len = (x->aalg->alg_key_len + 7) / 8;
key = x->aalg->alg_key;
if (crypto_ahash_setkey(ahashtfm, key, key_len))
goto error;
digest_size = crypto_ahash_digestsize(ahashtfm);
}
/*
* Lookup the algorithm description maintained by xfrm_algo,
* verify crypto transform properties, and store information
* we need for AH processing. This lookup cannot fail here
* after a successful crypto_alloc_ahash().
*/
aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
BUG_ON(!aalg_desc);
if (aalg_desc->uinfo.auth.icv_fullbits/8 != digest_size) {
printk(KERN_INFO "AH: %s digestsize %u != %hu\n",
x->aalg->alg_name, digest_size,
aalg_desc->uinfo.auth.icv_fullbits/8);
goto error;
}
ahp->icv_full_len = aalg_desc->uinfo.auth.icv_fullbits/8;
ahp->icv_trunc_len = aalg_desc->uinfo.auth.icv_truncbits/8;
BUG_ON(ahp->icv_trunc_len > MAX_AH_AUTH_LEN);
/* For AH hardware offload, set ICV size */
if (aeadtfm)
crypto_aead_setauthsize(aeadtfm, ahp->icv_trunc_len);
x->props.header_len = XFRM_ALIGN8(sizeof(struct ip_auth_hdr) +
ahp->icv_trunc_len);
if (x->props.mode == XFRM_MODE_TUNNEL)
x->props.header_len += sizeof(struct iphdr);
x->data = ahp;
return 0;
error:
if (ahp) {
crypto_free_ahash(ahp->utfm.atfm);
kfree(ahp);
}
return -EINVAL;
}
static void ah_destroy(struct xfrm_state *x)
{
struct ah_data *ahp = x->data;
if (!ahp)
return;
crypto_free_ahash(ahp->utfm.atfm);
ahp->utfm.atfm = NULL;
kfree(ahp);
}
static const struct xfrm_type ah_type =
{
.description = "AH4",
.owner = THIS_MODULE,
.proto = IPPROTO_AH,
.flags = XFRM_TYPE_REPLAY_PROT,
.init_state = ah_init_state,
.destructor = ah_destroy,
.input = ah_input,
.output = ah_output
};
static const struct net_protocol ah4_protocol = {
.handler = xfrm4_rcv,
.err_handler = ah4_err,
.no_policy = 1,
.netns_ok = 1,
};
static int __init ah4_init(void)
{
if (xfrm_register_type(&ah_type, AF_INET) < 0) {
printk(KERN_INFO "ip ah init: can't add xfrm type\n");
return -EAGAIN;
}
if (inet_add_protocol(&ah4_protocol, IPPROTO_AH) < 0) {
printk(KERN_INFO "ip ah init: can't add protocol\n");
xfrm_unregister_type(&ah_type, AF_INET);
return -EAGAIN;
}
return 0;
}
static void __exit ah4_fini(void)
{
if (inet_del_protocol(&ah4_protocol, IPPROTO_AH) < 0)
printk(KERN_INFO "ip ah close: can't remove protocol\n");
if (xfrm_unregister_type(&ah_type, AF_INET) < 0)
printk(KERN_INFO "ip ah close: can't remove xfrm type\n");
}
module_init(ah4_init);
module_exit(ah4_fini);
MODULE_LICENSE("GPL");
MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_AH);
|