-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.c
1065 lines (937 loc) · 25.2 KB
/
config.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 <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <rte_byteorder.h>
#include "config.h"
#include "lf.h"
#include "lib/json-parser/json.h"
#include "lib/json-parser/lf_json_util.h"
#include "lib/log/log.h"
/*
* JSON Field Identifiers
*/
#define FIELD_ISD_AS "isd_as"
#define FIELD_PEERS "peers"
#define FIELD_RATELIMIT "ratelimit"
#define FIELD_PACKET_RATE "packet_rate"
#define FIELD_PACKET_BURST "packet_burst"
#define FIELD_BYTE_RATE "byte_rate"
#define FIELD_BYTE_BURST "byte_burst"
#define FIELD_PORT "port"
#define FIELD_IP "ip"
#define FIELD_IPV6 "ipv6"
#define FIELD_IP_PUBLIC "ip_public"
#define FIELD_IP_PRIVATE "ip_private"
#define FIELD_ETHER "ether"
#define FIELD_AUTH_PEERS "auth_peers"
#define FIELD_BEST_EFFORT "best_effort"
#define FIELD_INBOUND "inbound"
#define FIELD_OUTBOUND "outbound"
#define FIELD_DRKEY_PROTOCOL "drkey_protocol"
#define FIELD_DRKEY_SERVICE_ADDR "drkey_service_addr"
#define FIELD_SHARED_SECRETS "shared_secrets"
#define FIELD_NOT_BEFORE "not_before"
#define FIELD_SECRET_VALUE "sv"
#define FIELD_DST_RATELIMITER "dst_ratelimiter"
#define FIELD_WG_RATELIMITER "wg_ratelimiter"
#define FIELD_WG_RATELIMITER_HANDSHAKE FIELD_RATELIMIT "_handshake"
#define FIELD_WG_RATELIMITER_DATA FIELD_RATELIMIT "_data"
/* potential value for the ether field of a packet modifier */
#define VALUE_ETHER_SRC_ADDR "src_addr"
/* 16 byte buffer with zero value. */
static const uint8_t zero_secret_value[16] = { 0 };
/**
* Ratelimit struct with the rate set to 0.
*
*/
#define zero_ratelimit \
(struct lf_config_ratelimit) \
{ \
0 \
}
/**
* Packet modification struct with no modification enabled.
*/
#define default_pkt_mod \
(struct lf_config_pkt_mod) \
{ \
0 \
}
/**
* Initialize peer struct with default values.
*
* @param config_peer Struct to be initialized.
*/
void
peer_init(struct lf_config_peer *config_peer)
{
*config_peer = (struct lf_config_peer){
.drkey_protocol = rte_cpu_to_be_16(LF_DRKEY_PROTOCOL),
.ip = 0,
.isd_as = 1,
.next = NULL,
.shared_secrets_configured_option = false,
.shared_secrets = { 0 },
/* per default no rate limit is defined for a peer */
.ratelimit_option = false,
.ratelimit = zero_ratelimit,
};
}
/**
* The rate limit struct consists of byte as well as packet rate and burst
* sizes. Per default, all values are set to zero. If the burst size is not
* provided, the value of the rate is used.
*
* @return Returns 0 on success.
*/
static int
parse_ratelimit(json_value *json_val, struct lf_config_ratelimit *ratelimit)
{
int res, error_count = 0;
unsigned int length;
unsigned int i;
char *field_name;
json_value *field_value;
bool packet_burst_set = false, byte_burst_set = false;
/* Initialize ratelimit struct. Set all to 0. */
(void)memset(ratelimit, 0, sizeof *ratelimit);
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_BYTE_RATE) == 0) {
res = lf_json_parse_uint64(field_value, &ratelimit->byte_rate);
if (res != 0) {
LF_LOG(ERR, "Invalid byte rate (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_BYTE_BURST) == 0) {
res = lf_json_parse_uint64(field_value, &ratelimit->byte_burst);
if (res != 0) {
LF_LOG(ERR, "Invalid byte burst (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
byte_burst_set = true;
} else if (strcmp(field_name, FIELD_PACKET_RATE) == 0) {
res = lf_json_parse_uint64(field_value, &ratelimit->packet_rate);
if (res != 0) {
LF_LOG(ERR, "Invalid packet rate (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_PACKET_BURST) == 0) {
res = lf_json_parse_uint64(field_value, &ratelimit->packet_burst);
if (res != 0) {
LF_LOG(ERR, "Invalid packet burst (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
packet_burst_set = true;
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
/* set default values for burst size */
if (!byte_burst_set) {
ratelimit->byte_burst = ratelimit->byte_rate;
}
if (!packet_burst_set) {
ratelimit->packet_burst = ratelimit->packet_rate;
}
return 0;
}
/**
* The shared_secret struct consists of a key and a timestamp.
* This should be used for preconfigured keys.
*
* @return Returns 0 on success.
*/
static int
parse_shared_secret(json_value *json_val,
struct lf_config_shared_secret *shared_secret)
{
int res, error_count = 0;
unsigned int length;
unsigned int i;
char *field_name;
json_value *field_value;
bool sv_flag = false, ts_flag = false;
/* Initialize drkey struct. Set all to 0. */
(void)memset(shared_secret, 0, sizeof *shared_secret);
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_SECRET_VALUE) == 0) {
res = lf_json_parse_byte_buffer(field_value, LF_CRYPTO_DRKEY_SIZE,
shared_secret->sv);
if (res != 0) {
LF_LOG(ERR, "Invalid shared secret (%d:%d)\n",
field_value->line, field_value->col);
error_count++;
}
if (memcmp(shared_secret->sv, zero_secret_value,
sizeof shared_secret->sv) == 0) {
LF_LOG(ERR,
"Invalid shared secret. "
"Secret value can not be the all zero secret value.\n");
return -1;
}
sv_flag = true;
} else if (strcmp(field_name, FIELD_NOT_BEFORE) == 0) {
res = lf_json_parse_timestamp(field_value,
&shared_secret->not_before);
if (res != 0) {
LF_LOG(ERR, "Invalid timestamp (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
ts_flag = true;
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
if (error_count > 0) {
return -1;
}
if (!sv_flag || !ts_flag) {
LF_LOG(ERR, "Invalid shared secret configuration. Need to define both "
"secret value "
"and not before timestamp.\n");
return -1;
}
return 0;
}
static int
parse_shared_secret_list(json_value *json_val,
struct lf_config_shared_secret shared_secret[LF_CONFIG_SV_MAX])
{
unsigned int length;
unsigned int i;
unsigned int res;
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_array) {
return -1;
}
length = json_val->u.array.length;
if (length > LF_CONFIG_SV_MAX) {
LF_LOG(ERR, "Exceed shared secret limit (%d:%d)\n", json_val->line,
json_val->col);
return -1;
}
for (i = 0; i < length; ++i) {
res = parse_shared_secret(json_val->u.array.values[i],
&shared_secret[i]);
if (res != 0) {
return -1;
}
}
return length;
}
static int
parse_peer(json_value *json_val, struct lf_config_peer *peer)
{
int res;
int error_count;
unsigned int length;
unsigned int i;
char *field_name;
json_value *field_value;
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
error_count = 0;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_ISD_AS) == 0) {
res = lf_json_parse_isd_as_be(field_value, &peer->isd_as);
if (res != 0) {
LF_LOG(ERR, "Invalid ISD AS address (%d:%d)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_DRKEY_PROTOCOL) == 0) {
res = lf_json_parse_uint16(field_value, &peer->drkey_protocol);
if (res != 0) {
LF_LOG(ERR, "Invalid ISD AS address (%d:%d)\n",
field_value->line, field_value->col);
error_count++;
}
/* set to network byte order */
peer->drkey_protocol = rte_cpu_to_be_16(peer->drkey_protocol);
} else if (strcmp(field_name, FIELD_IP) == 0) {
res = lf_json_parse_ipv4(field_value, &peer->ip);
if (res != 0) {
LF_LOG(ERR, "Invalid IP address (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_RATELIMIT) == 0) {
res = parse_ratelimit(field_value, &peer->ratelimit);
if (res != 0) {
LF_LOG(ERR, "Invalid ratelimit (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
peer->ratelimit_option = true;
} else if (strcmp(field_name, FIELD_SHARED_SECRETS) == 0) {
res = parse_shared_secret_list(field_value, peer->shared_secrets);
if (res >= 0) {
peer->shared_secrets_configured_option = true;
}
if (res < 0) {
LF_LOG(ERR, "Invalid shared secret (%d:%d)\n",
field_value->line, field_value->col);
error_count++;
}
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
if (error_count > 0) {
return -1;
} else {
return 0;
}
}
static int
parse_peer_list(json_value *json_val, struct lf_config *config)
{
int res;
unsigned int length;
unsigned int i;
json_value *peer_json_val;
struct lf_config_peer *peer;
struct lf_config_peer *current_peer, *next_peer;
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_array) {
return -1;
}
length = json_val->u.array.length;
if (length > LF_CONFIG_PEERS_MAX) {
LF_LOG(ERR, "Exceed peer limit (%d:%d)\n", json_val->line,
json_val->col);
return -1;
}
assert(config->peers == NULL);
assert(config->nb_peers == 0);
res = 0;
for (i = 0; i < length; ++i) {
/*
* Iterate the array in reverse order,
* such that the linked list is in same order.
*/
peer_json_val = json_val->u.array.values[length - 1 - i];
/*
* Create new peer struct according the configuration.
*/
peer = malloc(sizeof(struct lf_config_peer));
if (peer == NULL) {
LF_LOG(ERR, "Failed to allocate memory for peer (%d:%d)\n",
json_val->line, json_val->col);
res = -1;
break;
}
peer_init(peer);
res = parse_peer(peer_json_val, peer);
if (res != 0) {
free(peer);
break;
}
/*
* Extend peer list with new peer.
*/
peer->next = config->peers;
config->peers = peer;
config->nb_peers++;
}
if (res != 0) {
/* Something went wrong. Remove all parsed peers. */
current_peer = config->peers;
while (current_peer != NULL) {
next_peer = current_peer->next;
free(current_peer);
current_peer = next_peer;
}
config->nb_peers = 0;
return -1;
}
return 0;
}
static int
parse_pkt_mod_ether(json_value *json_val, struct lf_config_pkt_mod *pkt_mod)
{
int res;
if (json_val->type != json_string) {
goto err;
}
if (strcmp(json_val->u.string.ptr, VALUE_ETHER_SRC_ADDR) == 0) {
pkt_mod->ether_switch = true;
return 0;
}
res = lf_json_parse_ether(json_val, pkt_mod->ether);
if (res == 0) {
pkt_mod->ether_option = true;
return 0;
}
err:
LF_LOG(ERR,
"Invalid Ether field. Must be either %s or a valid Ethernet "
"address (%d:%d)\n",
VALUE_ETHER_SRC_ADDR, json_val->line, json_val->col);
return -1;
}
static int
parse_pkt_mod(json_value *json_val, struct lf_config_pkt_mod *pkt_mod)
{
int res = 0;
int error_count;
unsigned int length, i;
char *field_name;
json_value *field_value;
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
error_count = 0;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_ETHER) == 0) {
res = parse_pkt_mod_ether(field_value, pkt_mod);
if (res != 0) {
LF_LOG(ERR, "Invalid pkt mod Ether field (%d:%d)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_IPV6) == 0) {
#if LF_IPV6
res = lf_json_parse_ipv6(field_value, pkt_mod->ipv6);
if (res != 0) {
LF_LOG(ERR, "Invalid IPv6 address (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
#else
LF_LOG(ERR, "Detected IPv6 address but IPv6 is disabled (%d:%d)\n",
field_value->line, field_value->col);
error_count++;
#endif
} else if (strcmp(field_name, FIELD_IP) == 0) {
#if !LF_IPV6
res = lf_json_parse_ipv4(field_value, &pkt_mod->ip);
if (res != 0) {
LF_LOG(ERR, "Invalid IPv4 address (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
pkt_mod->ip_option = true;
#else
LF_LOG(ERR, "Detected IPv4 address but IPv6 is enabled (%d:%d)\n",
field_value->line, field_value->col);
error_count++;
#endif
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
if (error_count > 0) {
return -1;
} else {
return 0;
}
}
static int
parse_auth_peers(json_value *json_val, struct lf_config_auth_peers *best_effort)
{
int res;
int error_count;
unsigned int length;
unsigned int i;
char *field_name;
json_value *field_value;
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
error_count = 0;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_RATELIMIT) == 0) {
res = parse_ratelimit(field_value, &best_effort->ratelimit);
if (res != 0) {
LF_LOG(ERR, "Invalid ratelimit (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
if (error_count > 0) {
return -1;
} else {
return 0;
}
}
static int
parse_best_effort(json_value *json_val,
struct lf_config_best_effort *best_effort)
{
int res;
int error_count;
unsigned int length;
unsigned int i;
char *field_name;
json_value *field_value;
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
error_count = 0;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_RATELIMIT) == 0) {
res = parse_ratelimit(field_value, &best_effort->ratelimit);
if (res != 0) {
LF_LOG(ERR, "Invalid ratelimit (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
if (error_count > 0) {
return -1;
} else {
return 0;
}
}
/**
* Configuration parser for destination ratelimiter plugin.
*/
static int
parse_dst_ratelimiter(json_value *json_val,
struct lf_config_dst_ratelimiter *dst_ratelimiter)
{
int res;
int error_count;
unsigned int length;
unsigned int i;
char *field_name;
json_value *field_value;
#if !LF_PLUGIN_DST_RATELIMITER
LF_LOG(CRIT,
"Config for inactive plugin dst_ratelimiter detected (%d:%d)\n",
json_val->line, json_val->col);
#endif
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
error_count = 0;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_IP) == 0) {
res = lf_json_parse_ipv4(field_value, &dst_ratelimiter->dst_ip);
if (res != 0) {
LF_LOG(ERR, "Invalid byte rate (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_RATELIMIT) == 0) {
res = parse_ratelimit(field_value, &dst_ratelimiter->ratelimit);
if (res != 0) {
LF_LOG(ERR, "Invalid byte rate (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
if (error_count > 0) {
return -1;
} else {
return 0;
}
}
/**
* Configuration parser for WireGuard ratelimiter plugin.
*/
static int
parse_wg_ratelimiter(json_value *json_val,
struct lf_config_wg_ratelimiter *wg_ratelimiter)
{
int res;
int error_count;
unsigned int length;
unsigned int i;
char *field_name;
json_value *field_value;
#if !LF_PLUGIN_WG_RATELIMITER
LF_LOG(CRIT, "Config for inactive plugin wg_ratelimiter detected (%d:%d)\n",
json_val->line, json_val->col);
#endif
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
error_count = 0;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_PORT) == 0) {
res = lf_json_parse_port(field_value, &wg_ratelimiter->wg_port);
if (res != 0) {
LF_LOG(ERR, "Invalid port number (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_WG_RATELIMITER_HANDSHAKE) == 0) {
res = parse_ratelimit(field_value,
&wg_ratelimiter->handshake_ratelimit);
if (res != 0) {
LF_LOG(ERR, "Invalid byte rate (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_WG_RATELIMITER_DATA) == 0) {
res = parse_ratelimit(field_value, &wg_ratelimiter->data_ratelimit);
if (res != 0) {
LF_LOG(ERR, "Invalid byte rate (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
if (error_count > 0) {
return -1;
} else {
return 0;
}
}
/**
* Main config json parsing function.
* @return number of errors.
*/
static int
parse_config(json_value *json_val, struct lf_config *config)
{
int res = 0;
int error_count;
unsigned int length, i;
char *field_name;
json_value *field_value;
if (json_val == NULL) {
return -1;
}
if (json_val->type != json_object) {
return -1;
}
length = json_val->u.object.length;
error_count = 0;
for (i = 0; i < length; ++i) {
field_name = json_val->u.object.values[i].name;
field_value = json_val->u.object.values[i].value;
if (strcmp(field_name, FIELD_ISD_AS) == 0) {
res = lf_json_parse_isd_as_be(field_value, &config->isd_as);
if (res != 0) {
LF_LOG(ERR, "Invalid ISD AS field (%d:%d)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_DRKEY_PROTOCOL) == 0) {
res = lf_json_parse_uint16(field_value, &config->drkey_protocol);
if (res != 0) {
LF_LOG(ERR, "Invalid ISD AS address (%d:%d)\n",
field_value->line, field_value->col);
error_count++;
}
/* set to network byte order */
config->drkey_protocol = rte_cpu_to_be_16(config->drkey_protocol);
} else if (strcmp(field_name, FIELD_RATELIMIT) == 0) {
res = parse_ratelimit(field_value, &config->ratelimit);
if (res != 0) {
LF_LOG(ERR, "Invalid overall rate limit (%u:%u)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_PEERS) == 0) {
res = parse_peer_list(field_value, config);
if (res != 0) {
LF_LOG(ERR, "Invalid peers (%u:%u)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_INBOUND) == 0) {
res = parse_pkt_mod(field_value, &config->inbound_next_hop);
if (res != 0) {
LF_LOG(ERR, "Invalid inbound config (%u:%u)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_OUTBOUND) == 0) {
res = parse_pkt_mod(field_value, &config->outbound_next_hop);
if (res != 0) {
LF_LOG(ERR, "Invalid outbound config (%u:%u)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_AUTH_PEERS) == 0) {
res = parse_auth_peers(field_value, &config->auth_peers);
if (res != 0) {
LF_LOG(ERR, "Invalid auth_peers config (%u:%u)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_BEST_EFFORT) == 0) {
res = parse_best_effort(field_value, &config->best_effort);
if (res != 0) {
LF_LOG(ERR, "Invalid best_effort config (%u:%u)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_DRKEY_SERVICE_ADDR) == 0) {
/* TODO: check addr format */
res = lf_json_parse_string(field_value, config->drkey_service_addr,
sizeof config->drkey_service_addr);
if (res != 0) {
LF_LOG(ERR, "Invalid DRKey service address (%u:%u)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_PORT) == 0) {
res = lf_json_parse_port(field_value, &config->port);
if (res != 0) {
LF_LOG(ERR, "Invalid port number (%u:%u)\n", field_value->line,
field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_IP_PUBLIC) == 0) {
res = lf_json_parse_ipv4(field_value, &config->ip_public);
if (res != 0) {
LF_LOG(ERR, "Invalid public IP (%u:%u)\n", field_value->line,
field_value->col);
error_count++;
}
config->option_ip_public = true;
} else if (strcmp(field_name, FIELD_DST_RATELIMITER) == 0) {
res = parse_dst_ratelimiter(field_value, &config->dst_ratelimiter);
if (res != 0) {
LF_LOG(ERR, "Invalid dst ratelimiter config (%u:%u)\n",
field_value->line, field_value->col);
error_count++;
}
} else if (strcmp(field_name, FIELD_WG_RATELIMITER) == 0) {
res = parse_wg_ratelimiter(field_value, &config->wg_ratelimiter);
if (res != 0) {
LF_LOG(ERR, "Invalid wg ratelimiter config (%u:%u)\n",
field_value->line, field_value->col);
error_count++;
}
} else {
LF_LOG(ERR, "Unknown field %s (%d:%d)\n", field_name,
field_value->line, field_value->col);
error_count++;
}
}
if (error_count > 0) {
return -1;
} else {
return 0;
}
}
struct lf_config *
lf_config_new_from_file(const char *filename)
{
int res;
struct lf_config *config;
json_value *json_val;
struct stat filestatus;
size_t file_size;
char *file_content;
FILE *file;
LF_LOG(DEBUG, "Parse config file %s\n", filename);
file = fopen(filename, "rt");
if (file == NULL) {
LF_LOG(ERR, "Unable to open %s\n", filename);
return NULL;
}
if (fstat(fileno(file), &filestatus) != 0) {
LF_LOG(ERR, "Unable to fstat %s\n", filename);
(void)fclose(file);
return NULL;
}
file_size = filestatus.st_size;
file_content = (char *)malloc(file_size);
if (file_content == NULL) {
LF_LOG(ERR, "Unable to allocate %d bytes\n", file_size);
(void)fclose(file);
return NULL;
}
if (fread(file_content, file_size, 1, file) != 1) {
LF_LOG(ERR, "Unable to read content of %s\n", filename);
(void)fclose(file);
free(file_content);
return NULL;
}
(void)fclose(file);
/* Initialize config struct. Set all to 0. */
config = lf_config_new();
if (config == NULL) {
free(file_content);
return NULL;
}
json_val = json_parse(file_content, file_size);
if (json_val == NULL) {
LF_LOG(ERR, "Unable to parse json.\n");
free(file_content);
free(config);
return NULL;
}
res = parse_config(json_val, config);
if (res != 0) {
LF_LOG(ERR, "Unable to parse config.\n");
free(file_content);
free(config);
json_value_free(json_val);
return NULL;
}
free(file_content);
json_value_free(json_val);
return config;
}
void
lf_config_init(struct lf_config *config)
{
*config = (struct lf_config) {
.isd_as = 0,
.drkey_protocol = LF_DRKEY_PROTOCOL,
/* Rate limits */
.ratelimit = zero_ratelimit,
.best_effort = {
.ratelimit = zero_ratelimit,
},