-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathworker_scion.c
1128 lines (994 loc) · 32.7 KB
/
worker_scion.c
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
/* SPDX-License-Identifier: BSD-3-Clause
* Copyright (c) 2021 ETH Zurich
*/
#include <stdint.h>
#include <rte_branch_prediction.h>
#include <rte_byteorder.h>
#include <rte_common.h>
#include <rte_ethdev.h>
#include <rte_ether.h>
#include <rte_ip.h>
#include <rte_mbuf.h>
#include <rte_mbuf_core.h>
#include <rte_tcp.h>
#include <rte_udp.h>
#include "config.h"
#include "configmanager.h"
#include "lf.h"
#include "lib/crypto/crypto.h"
#include "lib/scion/scion.h"
#include "lib/utils/packet.h"
#include "statistics.h"
#include "worker.h"
struct scion_mac_input {
// scion authenticator option metadata
uint8_t hdr_len;
uint8_t payload_protocol;
uint16_t payload_length;
uint8_t algorithm;
uint8_t time_stamp[3];
uint8_t reserved;
uint8_t sequence_number[3];
// hash
uint8_t hash[20];
} __attribute__((__packed__));
#define SPAO_GET_MAC_INPUT(spao_hdr) \
((struct scion_mac_input *)((uint8_t *)(spao_hdr) + 2))
/**
* Structure to store information of a parsed inter-AS packet, which is supposed
* to be handled by LightningFilter.
*/
struct parsed_pkt {
struct rte_ether_hdr *ether_hdr;
union {
void *l3_hdr;
struct rte_ipv4_hdr *ipv4_hdr;
struct rte_ipv6_hdr *ipv6_hdr;
};
struct rte_udp_hdr *udp_hdr;
struct scion_cmn_hdr *scion_cmn_hdr;
struct scion_addr_ia_hdr *scion_addr_ia_hdr;
uint32_t scion_addr_hdr_len;
void *scion_path_hdr;
uint32_t scion_path_hdr_len;
/* Offset to the memory after the SCION path,
* i.e., to the payload or SCION extension headers. */
unsigned int offset;
struct scion_pao *spao_hdr;
};
/**
* Move pointers in the parsed_pkt struct by a certain offset.
*/
static inline void
pkt_hdrs_move(struct parsed_pkt *parsed_pkt, int offset)
{
parsed_pkt->ether_hdr = lf_ether_hdr_move(parsed_pkt->ether_hdr, offset);
parsed_pkt->l3_hdr = (void *)((uint8_t *)parsed_pkt->l3_hdr + offset);
parsed_pkt->udp_hdr =
(struct rte_udp_hdr *)((uint8_t *)parsed_pkt->udp_hdr + offset);
parsed_pkt->scion_cmn_hdr =
(struct scion_cmn_hdr *)((uint8_t *)parsed_pkt->scion_cmn_hdr +
offset);
parsed_pkt->scion_addr_ia_hdr = (struct scion_addr_ia_hdr
*)((uint8_t *)parsed_pkt->scion_addr_ia_hdr + offset);
parsed_pkt->scion_path_hdr =
(void *)((uint8_t *)parsed_pkt->scion_path_hdr + offset);
}
/**
* Structure to store SPAO data, as well as, payload information.
* Furthermore, this structure also acts as storage for values that are
* temporarily overwritten for the MAC computation.
*/
struct parsed_spao {
/* Pointer to the SPAO header. */
struct scion_packet_authenticator_opt *spao_hdr;
/* Protocol of the next layer */
uint8_t payload_protocol;
/* Offset to the next layer */
unsigned int payload_offset;
/* Length of the next layer. */
unsigned int payload_length;
/*
* Temporary storage for overwritten values.
*/
uint8_t hdr_len_old;
uint8_t upper_layer_protocol_old;
uint16_t upper_layer_length_old;
};
/**
* @param offset: Offset to header after SCION common, address and path header
* @param next_hdr_ptr: Return next hdr protocol number.
* @return length of SCION extension headers on success. If the packet does not
* contain a SPAO header, 0 is returned. If an error occurs a negative number is
* returned.
*/
static inline int
get_spao_hdr(const struct rte_mbuf *m, unsigned int offset,
const struct scion_cmn_hdr *scion_cmn_hdr,
struct scion_packet_authenticator_opt **spao_hdr_ptr, uint8_t *next_hdr)
{
struct scion_ext_hdr *scion_ext_hdr;
struct scion_ext_tlv_hdr *tlv_hdr;
int ext_hdr_len;
unsigned int offset_max;
*next_hdr = scion_cmn_hdr->next_hdr;
ext_hdr_len = 0;
/* skip HBH header and adjust next_hdr and offset*/
if (likely(*next_hdr == SCION_PROTOCOL_HBH)) {
if (unlikely(scion_get_ext_hdr(m, offset, &scion_ext_hdr) == 0)) {
return -1;
}
offset += SCION_EXT_HDR_LEN(scion_ext_hdr);
if (unlikely(offset > m->data_len)) {
LF_WORKER_LOG_DP(NOTICE,
"SCION HBH extension header length (%u) larger than "
"data len (%u).",
SCION_EXT_HDR_LEN(scion_ext_hdr), m->data_len);
return -1;
}
ext_hdr_len += SCION_EXT_HDR_LEN(scion_ext_hdr);
*next_hdr = scion_ext_hdr->next_hdr;
}
/* check if E2E extension headers are present */
if (unlikely(*next_hdr != SCION_PROTOCOL_E2E)) {
LF_WORKER_LOG_DP(NOTICE, "No SCION E2E extension header found.\n");
/* No SPAO header detected */
return 0;
}
if (unlikely(scion_get_ext_hdr(m, offset, &scion_ext_hdr) == 0)) {
return -1;
}
*next_hdr = scion_ext_hdr->next_hdr;
/* get and check size of scion extension header */
offset_max = offset + SCION_EXT_HDR_LEN(scion_ext_hdr);
if (unlikely(offset_max > m->data_len)) {
LF_WORKER_LOG_DP(NOTICE,
"SCION E2E extension header length (%u) larger than data "
"len (%u).",
SCION_EXT_HDR_LEN(scion_ext_hdr), m->data_len);
return -1;
}
ext_hdr_len += SCION_EXT_HDR_LEN(scion_ext_hdr);
/* skip E2E header */
offset += 2;
while (offset < offset_max) {
tlv_hdr =
rte_pktmbuf_mtod_offset(m, struct scion_ext_tlv_hdr *, offset);
if (tlv_hdr->type == SCION_E2E_OPTION_TYPE_SPAO) {
/* found a SPAO header */
if (tlv_hdr->data_len + 2 + offset > offset_max) {
LF_WORKER_LOG_DP(NOTICE, "Invalid SCION packet: SPAO length "
"exceeds extension header length.\n");
return -1;
}
if (tlv_hdr->data_len + 2 !=
sizeof(struct scion_packet_authenticator_opt)) {
/* incompatible SPAO size */
return -1;
}
*spao_hdr_ptr =
(struct scion_packet_authenticator_opt *)(void *)tlv_hdr;
return ext_hdr_len;
} else if (tlv_hdr->type == SCION_E2E_OPTION_TYPE_PAD1) {
offset += 1;
} else {
offset += 2 + tlv_hdr->data_len;
}
}
/* No SPAO header detected */
return 0;
}
/**
* @param ns_drkey_epoch_start: in nanoseconds (CPU endian)
* @param timestamp: current (unique) timestamp in nanoseconds (CPU endian)
*/
static inline int
set_spao_timestamp(uint64_t ns_drkey_epoch_start, uint64_t timestamp,
struct scion_packet_authenticator_opt *spao_hdr)
{
uint64_t ns_rel_time;
if (unlikely(ns_drkey_epoch_start > timestamp)) {
LF_WORKER_LOG_DP(NOTICE,
"DRKey epoch start timestamp (%" PRIu64
"ns) is in the future (now: %" PRIu64 ").\n",
ns_drkey_epoch_start, timestamp);
#if !LF_WORKER_IGNORE_DRKEY_TIMESTAMP_CHECK
return -1;
#endif
}
ns_rel_time = timestamp - ns_drkey_epoch_start;
/* ensure that timestamp fits into 6 bytes */
if (unlikely(ns_rel_time >> 48)) {
LF_WORKER_LOG_DP(NOTICE,
"DRKey epoch start timestamp (%" PRIu64
" ns) is too far in the past (ns_rel_time: %" PRIu64 ").\n",
ns_drkey_epoch_start, ns_rel_time);
#if !LF_WORKER_IGNORE_DRKEY_TIMESTAMP_CHECK
return -1;
#endif
}
/* Set header fields */
scion_spao_set_timestamp(spao_hdr, ns_rel_time);
return 0;
}
static inline int
hash_cmn_hdr(struct lf_crypto_hash_ctx *ctx,
struct scion_cmn_hdr *scion_cmn_hdr)
{
uint8_t ecn_old;
ecn_old = scion_cmn_hdr->version_qos_flowid[1];
scion_cmn_hdr->version_qos_flowid[1] &= 0xCF; // 0b11001111;
lf_crypto_hash_update(ctx, (uint8_t *)scion_cmn_hdr, 4);
lf_crypto_hash_update(ctx, (uint8_t *)scion_cmn_hdr + 8, 4);
scion_cmn_hdr->version_qos_flowid[1] = ecn_old;
return 0;
}
static inline int
hash_path_hdr(struct lf_worker_context *worker_context, void *path_hdr,
uint8_t path_type, uint32_t path_header_len)
{
switch (path_type) {
case SCION_PATH_TYPE_EMPTY:
/* nothing to do here */
break;
case SCION_PATH_TYPE_SCION: {
if (unlikely(sizeof(struct scion_path_meta_hdr) > path_header_len)) {
LF_WORKER_LOG_DP(NOTICE,
"Invalid SCION packet: path header type "
"inconsistent with expected path header length.\n");
return -1;
}
struct scion_path_meta_hdr *scion_path_meta_hdr =
(struct scion_path_meta_hdr *)path_hdr;
uint32_t seg_len[3];
seg_len[0] = ((scion_path_meta_hdr->seg_len[0] & 0x03) << 4) |
((scion_path_meta_hdr->seg_len[1] & 0xF0) >> 4);
seg_len[1] = ((scion_path_meta_hdr->seg_len[1] & 0x0F) << 2) |
((scion_path_meta_hdr->seg_len[2] & 0xC0) >> 6);
seg_len[2] = ((scion_path_meta_hdr->seg_len[2] & 0x3F));
if (unlikely(seg_len[0] + seg_len[1] + seg_len[2] > 64)) {
LF_WORKER_LOG_DP(NOTICE, "Invalid SCION packet: path header hop "
"field number exceeds 64.\n");
return -1;
}
uint32_t actual_path_header_len =
sizeof *scion_path_meta_hdr +
(seg_len[0] ? SCION_PATH_INFOFIELD_SIZE +
seg_len[0] * SCION_PATH_HOPFIELD_SIZE
: 0) +
(seg_len[1] ? SCION_PATH_INFOFIELD_SIZE +
seg_len[1] * SCION_PATH_HOPFIELD_SIZE
: 0) +
(seg_len[2] ? SCION_PATH_INFOFIELD_SIZE +
seg_len[2] * SCION_PATH_HOPFIELD_SIZE
: 0);
if (unlikely(actual_path_header_len != path_header_len)) {
LF_WORKER_LOG_DP(NOTICE,
"Invalid SCION packet: SCION path header length "
"inconsistent with path header length.\n");
return -1;
}
/* PathMeta Header (with CurrINF, CurrHF zeroed) */
uint8_t curr_old = scion_path_meta_hdr->curr_inf_hf;
scion_path_meta_hdr->curr_inf_hf = 0;
/* InfoField Header (with SegID zeroed) */
uint16_t seg_id_old[3];
struct scion_path_info_hdr *info_field =
(struct scion_path_info_hdr *)(scion_path_meta_hdr + 1);
for (size_t i = 0; i < 3; ++i) {
if (seg_len[i] != 0) {
seg_id_old[i] = info_field->seg_id;
info_field->seg_id = 0;
info_field += 1;
}
}
/* HopField Header (with router alerts zeroed) */
struct scion_path_hop_hdr *hop_field =
(struct scion_path_hop_hdr *)info_field;
uint8_t router_alerts_old[64];
for (size_t i = 0; i < seg_len[0] + seg_len[1] + seg_len[2]; ++i) {
router_alerts_old[i] = hop_field->rie;
hop_field->rie &= 0xFC; // 0b11111100;
hop_field += 1;
}
lf_crypto_hash_update(&worker_context->crypto_hash_ctx,
(uint8_t *)path_hdr, path_header_len);
/* PathMeta Header reset */
scion_path_meta_hdr->curr_inf_hf = curr_old;
/* InfoField Header reset */
info_field = (struct scion_path_info_hdr *)(scion_path_meta_hdr + 1);
for (size_t i = 0; i < 3; ++i) {
if (seg_len[i] != 0) {
info_field->seg_id = seg_id_old[i];
info_field += 1;
}
}
/* HopField Header reset */
hop_field = (struct scion_path_hop_hdr *)info_field;
for (size_t i = 0; i < seg_len[0] + seg_len[1] + seg_len[2]; ++i) {
hop_field->rie = router_alerts_old[i];
hop_field += 1;
}
break;
}
case SCION_PATH_TYPE_ONEHOP: {
if (unlikely(SCION_PATH_INFOFIELD_SIZE + 2 * SCION_PATH_HOPFIELD_SIZE >
path_header_len)) {
LF_WORKER_LOG_DP(NOTICE, "Invalid SCION packet: path header type "
"inconsistent with header length.\n");
return -1;
}
struct scion_path_info_hdr *scion_path_info_hdr =
(struct scion_path_info_hdr *)path_hdr;
/* add info field and first hop field (with router alert flags zeroed)
*/
struct scion_path_hop_hdr *hop_field_1 =
(struct scion_path_hop_hdr *)(scion_path_info_hdr + 1);
uint8_t router_alerts_old = hop_field_1->rie;
hop_field_1->rie &= 0xFC; // 0b11111100;
lf_crypto_hash_update(&worker_context->crypto_hash_ctx,
(uint8_t *)path_hdr,
SCION_PATH_INFOFIELD_SIZE + SCION_PATH_HOPFIELD_SIZE);
hop_field_1->rie = router_alerts_old;
/* add second hop field (with everything zeroed) */
uint8_t hop_field_zeroed[SCION_PATH_HOPFIELD_SIZE] = { 0 };
lf_crypto_hash_update(&worker_context->crypto_hash_ctx,
hop_field_zeroed, SCION_PATH_HOPFIELD_SIZE);
break;
}
default:
LF_WORKER_LOG_DP(NOTICE, "Unknown SCION path type %u.\n", path_type);
return -1;
break;
}
return 0;
}
/**
* Assume that the complete SCION header (limited through its size defined
* in the cmn header) can be accessed in the same mbuf.
* @return 0 if succeeds.
*/
static inline int
compute_pkt_hash(struct lf_worker_context *worker_context, struct rte_mbuf *m,
struct parsed_pkt *parsed_pkt, struct parsed_spao *parsed_spao,
uint8_t hash[LF_CRYPTO_HASH_LENGTH])
{
int res;
uint8_t *payload;
/* hash common header */
res = hash_cmn_hdr(&worker_context->crypto_hash_ctx,
parsed_pkt->scion_cmn_hdr);
if (unlikely(res != 0)) {
return res;
}
/* hash path header */
res = hash_path_hdr(worker_context, parsed_pkt->scion_path_hdr,
parsed_pkt->scion_cmn_hdr->path_type,
parsed_pkt->scion_path_hdr_len);
if (unlikely(res != 0)) {
return res;
}
/* hash payload */
if (unlikely(parsed_spao->payload_offset + parsed_spao->payload_length >
m->data_len)) {
LF_WORKER_LOG_DP(NOTICE,
"Not yet implemented: SCION payload exceeds "
"first buffer segment (offset = %d, length = %d, segment = "
"%d).\n",
parsed_spao->payload_offset, parsed_spao->payload_length,
m->data_len);
return -1;
}
payload =
rte_pktmbuf_mtod_offset(m, uint8_t *, parsed_spao->payload_offset);
(void)lf_crypto_hash_update(&worker_context->crypto_hash_ctx, payload,
parsed_spao->payload_length);
LF_WORKER_LOG_DP(DEBUG, "Finalize hash\n");
(void)lf_crypto_hash_final(&worker_context->crypto_hash_ctx, hash);
return 0;
}
/**
* Perform packet hash check.
* If this check is disable, the check is not performed and the function just
* returns valid.
* If this check is ignored, the check is performed but the function
* always return valid.
*
* @return Returns 0 if the packet hash is valid.
*/
static int
check_pkt_hash(struct lf_worker_context *worker_context, struct rte_mbuf *m,
struct parsed_pkt *parsed_pkt, struct parsed_spao *parsed_spao)
{
int res;
uint8_t hash[20];
#if (LF_WORKER_OMIT_HASH_CHECK)
return 0;
#endif /* !(LF_WORKER_OMIT_HASH_CHECK) */
res = compute_pkt_hash(worker_context, m, parsed_pkt, parsed_spao, hash);
if (res != 0) {
LF_WORKER_LOG_DP(ERR, "Failed to compute hash. res = %d\n", res);
lf_statistics_worker_counter_inc(worker_context->statistics, error);
return 1;
}
res = lf_crypto_hash_cmp(hash, parsed_spao->spao_hdr->hash);
if (likely(res != 0)) {
LF_WORKER_LOG_DP(DEBUG, "Packet hash check failed.\n");
lf_statistics_worker_counter_inc(worker_context->statistics,
invalid_hash);
} else {
LF_WORKER_LOG_DP(DEBUG, "Packet hash check passed.\n");
}
#if (LF_WORKER_IGNORE_HASH_CHECK)
res = 0;
#endif /* !(LF_WORKER_IGNORE_HASH_CHECK) */
return res;
}
static void
preprocess_mac_input(const struct parsed_pkt *parsed_pkt,
struct parsed_spao *parsed_spao)
{
struct scion_mac_input *mac_input =
SPAO_GET_MAC_INPUT(parsed_spao->spao_hdr);
parsed_spao->hdr_len_old = mac_input->hdr_len;
parsed_spao->upper_layer_protocol_old = mac_input->payload_protocol;
parsed_spao->upper_layer_length_old = mac_input->payload_length;
mac_input->hdr_len = parsed_pkt->scion_cmn_hdr->hdr_len;
mac_input->payload_protocol = parsed_spao->payload_protocol;
mac_input->payload_length = parsed_spao->payload_length;
}
static void
postprocess_mac_input(struct parsed_spao *parsed_spao)
{
struct scion_mac_input *mac_input =
SPAO_GET_MAC_INPUT(parsed_spao->spao_hdr);
mac_input->hdr_len = parsed_spao->hdr_len_old;
mac_input->payload_protocol = parsed_spao->upper_layer_protocol_old;
mac_input->payload_length = parsed_spao->upper_layer_length_old;
}
/**
* This function looks for a LF SPAO header.
*
* @param parsed_pkt The parsed packet.
* @param parsed_spao Returns a parsed LF SPAO header if it the packet contains
* one.
* @param pkt_data Returns the packet data.
* @return Returns 0 if the packet contains a LF SPAO header. Returns > 0 if the
* packet does not contains a LF SPAO header. Returns < 0 if an error occurred.
*/
static int
get_lf_spao_hdr(struct rte_mbuf *m, struct parsed_pkt *parsed_pkt,
struct parsed_spao *parsed_spao, struct lf_pkt_data *pkt_data)
{
int scion_ext_hdr_len;
/*
* Get SPAO header and check if it corresponds to the LightingFilter
* format.
*/
scion_ext_hdr_len =
get_spao_hdr(m, parsed_pkt->offset, parsed_pkt->scion_cmn_hdr,
&parsed_spao->spao_hdr, &parsed_spao->payload_protocol);
if (unlikely(scion_ext_hdr_len == 0)) {
/* no SPAO header found */
return 1;
} else if (unlikely(scion_ext_hdr_len < 0)) {
LF_WORKER_LOG_DP(NOTICE, "Failed to get SPAO header.\n");
return -1;
}
/* SPI must be in the DRKey format */
if (unlikely(
(parsed_spao->spao_hdr->spi &
rte_cpu_to_be_32(
SCION_PACKET_AUTHENTICATOR_OPT_SPI_DRKEY_MASK)) !=
parsed_spao->spao_hdr->spi ||
parsed_spao->spao_hdr->spi_drkey_rrr != 0 ||
parsed_spao->spao_hdr->spi_drkey_t !=
SCION_SPAO_SPI_DRKEY_TYPE_HH ||
parsed_spao->spao_hdr->spi_drkey_d !=
SCION_SPAO_SPI_DRKEY_DIRECTION_RECEIVER)) {
LF_WORKER_LOG_DP(NOTICE, "Unexpected SPAO SPI DRKey information.\n");
return -1;
}
/* Algorithm must be SHA and AES-CBC*/
if (unlikely(parsed_spao->spao_hdr->algorithm !=
SCION_SPAO_ALGORITHM_TYPE_SHA_AES_CBC)) {
LF_WORKER_LOG_DP(NOTICE,
"Unexpected SPAO algorithm (%d), expected %d.\n",
parsed_spao->spao_hdr->algorithm,
SCION_SPAO_ALGORITHM_TYPE_SHA_AES_CBC);
return -1;
}
if (unlikely(parsed_spao->spao_hdr->reserved != 0)) {
LF_WORKER_LOG_DP(NOTICE,
"Unexpected SPAO reserved field (%d), expected %d.\n",
parsed_spao->spao_hdr, 0);
return -1;
}
/*
* Set packet data
*/
pkt_data->src_as = parsed_pkt->scion_addr_ia_hdr->src_ia;
pkt_data->dst_as = parsed_pkt->scion_addr_ia_hdr->dst_ia;
pkt_data->src_addr = (struct lf_host_addr){
.addr = scion_get_addr_host_src(parsed_pkt->scion_cmn_hdr),
.type_length = parsed_pkt->scion_cmn_hdr->st_sl,
};
pkt_data->dst_addr = (struct lf_host_addr){
.addr = scion_get_addr_host_dst(parsed_pkt->scion_cmn_hdr),
.type_length = parsed_pkt->scion_cmn_hdr->dt_dl,
};
/* Obtain required SPAO fields before they are temporarily overwritten.
*/
pkt_data->timestamp = scion_spao_get_timestamp(parsed_spao->spao_hdr);
pkt_data->drkey_protocol = parsed_spao->spao_hdr->spi_drkey_protocol_id;
pkt_data->mac = parsed_spao->spao_hdr->mac;
/* The MAC input starts at the after the type and length field of the
* SPAO extension header. */
pkt_data->auth_data = (uint8_t *)parsed_spao->spao_hdr + 2;
pkt_data->pkt_len =
rte_be_to_cpu_16(parsed_pkt->scion_cmn_hdr->payload_len);
/* Get Payload offset and length */
parsed_spao->payload_offset = parsed_pkt->offset + scion_ext_hdr_len;
parsed_spao->payload_length =
rte_be_to_cpu_16(parsed_pkt->scion_cmn_hdr->payload_len) -
scion_ext_hdr_len;
return 0;
}
static enum lf_check_state
handle_inbound_pkt_without_lf_hdr(struct lf_worker_context *worker_context,
struct rte_mbuf *m, struct parsed_pkt *parsed_pkt)
{
(void)parsed_pkt;
return lf_worker_check_best_effort_pkt(worker_context, m->pkt_len);
}
static enum lf_check_state
handle_inbound_pkt_with_lf_hdr(struct lf_worker_context *worker_context,
struct rte_mbuf *m, struct parsed_pkt *parsed_pkt,
struct parsed_spao *parsed_spao, struct lf_pkt_data *pkt_data)
{
int res;
enum lf_check_state check_state;
preprocess_mac_input(parsed_pkt, parsed_spao);
check_state = lf_worker_check_pkt(worker_context, pkt_data);
postprocess_mac_input(parsed_spao);
if (likely(check_state != LF_CHECK_VALID)) {
/* Under attack, it is more likely that packets do not pass all checks.
*/
return check_state;
}
/* Only if all checks are passed, the packet hash is checked. */
res = check_pkt_hash(worker_context, m, parsed_pkt, parsed_spao);
if (res == 0) {
return LF_CHECK_VALID;
} else {
return LF_CHECK_VALID_MAC_BUT_INVALID_HASH;
}
}
/**
* Handle inbound packets.
*
* @param worker_context Worker Context
* @param m Packet buffer.
* @param parsed_pkt Parsed packet headers.
* @return enum lf_pkt_action
*/
static enum lf_pkt_action
handle_inbound_pkt(struct lf_worker_context *worker_context, struct rte_mbuf *m,
struct parsed_pkt *parsed_pkt)
{
int res;
enum lf_check_state check_state;
struct parsed_spao parsed_spao;
struct lf_pkt_data pkt_data;
res = get_lf_spao_hdr(m, parsed_pkt, &parsed_spao, &pkt_data);
if (res == 0) {
check_state = handle_inbound_pkt_with_lf_hdr(worker_context, m,
parsed_pkt, &parsed_spao, &pkt_data);
} else if (res > 0) {
check_state = handle_inbound_pkt_without_lf_hdr(worker_context, m,
parsed_pkt);
} else {
check_state = LF_CHECK_ERROR;
}
lf_worker_pkt_mod(m, parsed_pkt->ether_hdr, parsed_pkt->l3_hdr,
lf_configmanager_worker_get_inbound_pkt_mod(
worker_context->config));
if (check_state == LF_CHECK_VALID || check_state == LF_CHECK_BE) {
return LF_PKT_INBOUND_FORWARD;
} else {
return LF_PKT_INBOUND_DROP;
}
}
/**
* Add SPAO to SCION header
* @param offset: Offset to SCION next header (header after address and path
* header)
* @param drkey_protocol: (network byte order).
* @return Returns the length of the additional headers.
* Returns -1 if failed.
*/
static inline int
add_spao(struct lf_worker_context *worker_context, struct rte_mbuf *m,
struct parsed_pkt *parsed_pkt)
{
int res;
int add_hdr_len; /* added header length */
struct scion_packet_authenticator_opt *spao_hdr;
struct parsed_spao parsed_spao;
uint64_t timestamp;
struct lf_host_addr src_addr;
struct lf_host_addr dst_addr;
struct lf_crypto_drkey drkey;
uint16_t drkey_protocol;
uint8_t payload_protocol;
unsigned int payload_offset;
unsigned int payload_len;
/* get current time */
res = lf_time_worker_get_unique(&worker_context->time, ×tamp);
if (unlikely(res != 0)) {
LF_WORKER_LOG_DP(ERR, "Failed to get timestamp.\n");
return -1;
}
/*
* Get DRKey for the dst address, src address, and drkey protocol.
* Querying for the DRKey also reveals if a key exists.
*/
dst_addr = (struct lf_host_addr){
.addr = scion_get_addr_host_dst(parsed_pkt->scion_cmn_hdr),
.type_length = parsed_pkt->scion_cmn_hdr->dt_dl,
};
src_addr = (struct lf_host_addr){
.addr = scion_get_addr_host_src(parsed_pkt->scion_cmn_hdr),
.type_length = parsed_pkt->scion_cmn_hdr->st_sl,
};
drkey_protocol = lf_configmanager_worker_get_outbound_drkey_protocol(
worker_context->config);
uint64_t ns_drkey_epoch_start;
res = lf_keymanager_worker_outbound_get_drkey(worker_context->key_manager,
parsed_pkt->scion_addr_ia_hdr->dst_ia, &dst_addr, &src_addr,
drkey_protocol, timestamp, &ns_drkey_epoch_start, &drkey);
if (unlikely(res < 0)) {
LF_WORKER_LOG_DP(NOTICE,
"Outbound DRKey not found for AS " PRIISDAS
" and drkey_protocol %d (timestamp = %" PRIu64 ", res = %d)!\n",
PRIISDAS_VAL(rte_be_to_cpu_64(
parsed_pkt->scion_addr_ia_hdr->dst_ia)),
rte_be_to_cpu_16(drkey_protocol), timestamp, res);
return -1;
}
LF_WORKER_LOG_DP(DEBUG,
"Outbound DRKey [" PRIISDAS "]:" PRIIP " - [XX]:" PRIIP
" and drkey_protocol %d is %x (timestamp = %" PRIu64
", res = %d)\n",
PRIISDAS_VAL(
rte_be_to_cpu_64(parsed_pkt->scion_addr_ia_hdr->dst_ia)),
PRIIP_VAL(*(uint32_t *)dst_addr.addr),
PRIIP_VAL(*(uint32_t *)src_addr.addr),
rte_be_to_cpu_16(drkey_protocol), drkey.key[0], timestamp, res);
/*
* Add SPAO Extension Header
* When adding the extension header, the preceding headers are moved.
* Hence, any pointer to these headers must be moved or should be
* invalidated afterwards.
*/
add_hdr_len = scion_add_spao_hdr(m, parsed_pkt->offset,
parsed_pkt->scion_cmn_hdr, &spao_hdr);
if (add_hdr_len < 0) {
return -1;
}
/* adjust and invalidate pointers to moved memory */
pkt_hdrs_move(parsed_pkt, -add_hdr_len);
(void)src_addr;
(void)dst_addr;
/*
* Get payload offset, length, and protocol.
* The payload length can be computed by subtracting the SCION extension
* header length from the payload length provided in the SCION common
* header.
*/
payload_offset = scion_skip_extension_hdr(m, parsed_pkt->scion_cmn_hdr,
parsed_pkt->offset, &payload_protocol);
if (payload_offset == 0) {
return -1;
}
payload_len = rte_be_to_cpu_16(parsed_pkt->scion_cmn_hdr->payload_len) -
(payload_offset - parsed_pkt->offset);
/*
* Set SPAO header fields
*/
spao_hdr->spi_drkey_zero0 = 0;
spao_hdr->spi_drkey_zero1 = 0;
spao_hdr->spi_drkey_rrr = 0;
spao_hdr->spi_drkey_t = SCION_SPAO_SPI_DRKEY_TYPE_HH;
spao_hdr->spi_drkey_d = SCION_SPAO_SPI_DRKEY_DIRECTION_RECEIVER;
spao_hdr->spi_drkey_protocol_id = drkey_protocol;
spao_hdr->algorithm = SCION_SPAO_ALGORITHM_TYPE_SHA_AES_CBC;
spao_hdr->reserved = 0;
/* Initialize parsed_spao struct. */
parsed_spao.spao_hdr = spao_hdr;
parsed_spao.payload_protocol = payload_protocol;
parsed_spao.payload_offset = payload_offset;
parsed_spao.payload_length = payload_len;
/* packet hash */
LF_WORKER_LOG_DP(DEBUG, "Compute packet hash.\n");
res = compute_pkt_hash(worker_context, m, parsed_pkt, &parsed_spao,
spao_hdr->hash);
if (unlikely(res != 0)) {
LF_WORKER_LOG_DP(ERR, "Failed to compute hash. res = %d\n", res);
/* TODO: error handling */
return -1;
}
/* set timestamp */
res = set_spao_timestamp(ns_drkey_epoch_start, timestamp, spao_hdr);
if (unlikely(res != 0)) {
/* TODO: error handling */
return -1;
}
/* Temporarily overwrite certain SPAO header fields */
preprocess_mac_input(parsed_pkt, &parsed_spao);
/* Compute MAC */
lf_crypto_drkey_compute_mac(&worker_context->crypto_drkey_ctx, &drkey,
(uint8_t *)SPAO_GET_MAC_INPUT(spao_hdr), spao_hdr->mac);
/* Revert overwrites */
postprocess_mac_input(&parsed_spao);
/* adjust header length fields */
#if LF_IPV6
parsed_pkt->ipv6_hdr->payload_len = rte_cpu_to_be_16(
rte_be_to_cpu_16(parsed_pkt->ipv6_hdr->payload_len) + add_hdr_len);
#else
parsed_pkt->ipv4_hdr->total_length = rte_cpu_to_be_16(
rte_be_to_cpu_16(parsed_pkt->ipv4_hdr->total_length) + add_hdr_len);
#endif
parsed_pkt->udp_hdr->dgram_len = rte_cpu_to_be_16(
rte_be_to_cpu_16(parsed_pkt->udp_hdr->dgram_len) + add_hdr_len);
return add_hdr_len;
}
/**
* Handle outbound packets.
*
* @param worker_context Worker Context
* @param m Packet buffer.
* @param parsed_pkt Parsed packet headers.
* @return enum lf_pkt_action
*/
static enum lf_pkt_action
handle_outbound_pkt(struct lf_worker_context *worker_context,
struct rte_mbuf *m, struct parsed_pkt *parsed_pkt)
{
int add_len;
add_len = add_spao(worker_context, m, parsed_pkt);
if (unlikely(add_len < 0)) {
LF_WORKER_LOG_DP(ERR, "Failed to add SPAO header.\n");
return LF_PKT_OUTBOUND_DROP;
}
LF_WORKER_LOG_DP(DEBUG, "SPAO header added.\n");
/*
* Apply outbound packet modifications, i.e., ethernet and IP address,
* and reset checksums.
*/
lf_worker_pkt_mod(m, parsed_pkt->ether_hdr, parsed_pkt->l3_hdr,
lf_configmanager_worker_get_outbound_pkt_mod(
worker_context->config));
return LF_PKT_OUTBOUND_FORWARD;
}
/**
* Parse packet according the expected format:
* ETH/IPV4|IPV6/UDP/SCION_CMN/SCION_ADDR/SCION_PATH.
*
* From the SCION path, also the timestamp is parsed.
*
* @return Returns 0 on success.
* Returns 1 if packet is a intra-AS packet.
* Returns -1 on error.
*/
static int
parse_pkt(struct rte_mbuf *m, unsigned int offset,
struct parsed_pkt *parsed_pkt)
{
int res;
if (unlikely(m->data_len != m->pkt_len)) {
LF_WORKER_LOG_DP(NOTICE,
"Not yet implemented: buffer with multiple segments "
"received.\n");
return -1;
}
offset = 0;
offset = lf_get_eth_hdr(m, offset, &parsed_pkt->ether_hdr);
if (unlikely(offset == 0)) {
return -1;
}
#if LF_IPV6
if (unlikely(parsed_pkt->ether_hdr->ether_type !=
rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6))) {
LF_WORKER_LOG_DP(NOTICE,
"Unsupported packet type %#X: must be IPv6 (%#X).\n",
rte_be_to_cpu_16(parsed_pkt->ether_hdr->ether_type),
RTE_ETHER_TYPE_IPV6);
return -1;
}
offset = lf_get_ipv6_hdr(m, offset, &parsed_pkt->ipv6_hdr);
if (unlikely(offset == 0)) {
return -1;
}
if (parsed_pkt->ipv6_hdr->proto != IP_PROTO_ID_UDP) {
/* Probably intra AS traffic: forward without checks */
LF_WORKER_LOG_DP(DEBUG,
"IPv6 packet type is not UDP (%#X) but %#X. Probably intra "
"AS traffic.\n",
IP_PROTO_ID_UDP, parsed_pkt->ipv6_hdr->proto);
return 1;
}
#else
if (unlikely(parsed_pkt->ether_hdr->ether_type !=
rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4))) {
LF_WORKER_LOG_DP(NOTICE,
"Unsupported packet type %#X: must be IPv4 (%#X).\n",
rte_be_to_cpu_16(parsed_pkt->ether_hdr->ether_type),
RTE_ETHER_TYPE_IPV4);
return -1;
}
offset = lf_get_ip_hdr(m, offset, &parsed_pkt->ipv4_hdr);
if (unlikely(offset == 0)) {
return -1;
}
if (parsed_pkt->ipv4_hdr->next_proto_id != IP_PROTO_ID_UDP) {
/* Probably intra AS traffic: forward without checks */
LF_WORKER_LOG_DP(DEBUG,
"IPv4 packet type is not UDP (%#X) but %#X. Probably intra "
"AS traffic.\n",
IP_PROTO_ID_UDP, parsed_pkt->ipv4_hdr->next_proto_id);
return 1;
}
#endif /* LF_IPV6 */
offset = lf_get_udp_hdr(m, offset, &parsed_pkt->udp_hdr);
if (unlikely(offset == 0)) {
return -1;
}
/*
* Get the full SCION header (Common, Address, Path):
* Validate that the full SCION header is inside the first mbuf
* segment.
*/
/* scion common header */
offset = scion_get_cmn_hdr(m, offset, &parsed_pkt->scion_cmn_hdr);
if (unlikely(offset == 0)) {
return -1;
}
/* address header */
parsed_pkt->scion_addr_hdr_len =
SCION_ADDR_HDR_LEN(parsed_pkt->scion_cmn_hdr);
if (parsed_pkt->scion_addr_hdr_len > m->data_len - offset) {
LF_WORKER_LOG_DP(NOTICE, "Not yet implemented: SCION address header "
"exceeds first buffer segment.\n");
return -1;
}
/* no need to check length again */
(void)scion_get_addr_ia_hdr(m, offset, &parsed_pkt->scion_addr_ia_hdr);
offset += parsed_pkt->scion_addr_hdr_len;
/* path header */
res = scion_path_hdr_length(m, offset,
parsed_pkt->scion_cmn_hdr->path_type);
if (res < 0) {
LF_WORKER_LOG_DP(NOTICE,
"Failed to calculate SCION path header length (res = %d)\n",
res);
return -1;
}
parsed_pkt->scion_path_hdr_len = res;
parsed_pkt->scion_path_hdr = rte_pktmbuf_mtod_offset(m, void *, offset);
offset += parsed_pkt->scion_path_hdr_len;
/* validate header length provided in scion common header */