-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_json.c
3894 lines (3398 loc) · 96.6 KB
/
parser_json.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
#define _GNU_SOURCE
#include <errno.h>
#include <stdint.h> /* needed by gmputil.h */
#include <string.h>
#include <syslog.h>
#include <erec.h>
#include <expression.h>
#include <tcpopt.h>
#include <list.h>
#include <netlink.h>
#include <parser.h>
#include <rule.h>
#include <socket.h>
#include <netdb.h>
#include <netinet/icmp6.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <net/if.h>
#include <linux/xfrm.h>
#include <linux/netfilter.h>
#include <linux/netfilter/nf_conntrack_tuple_common.h>
#include <linux/netfilter/nf_log.h>
#include <linux/netfilter/nf_nat.h>
#include <linux/netfilter/nf_synproxy.h>
#include <linux/netfilter/nf_tables.h>
#include <jansson.h>
#include <mnl.h>
#include <libnftnl/rule.h>
#include <linux/netfilter/nfnetlink.h>
#define CTX_F_RHS (1 << 0)
#define CTX_F_STMT (1 << 1)
#define CTX_F_PRIMARY (1 << 2)
#define CTX_F_DTYPE (1 << 3)
#define CTX_F_SET_RHS (1 << 4)
#define CTX_F_MANGLE (1 << 5)
#define CTX_F_SES (1 << 6) /* set_elem_expr_stmt */
#define CTX_F_MAP (1 << 7) /* LHS of map_expr */
#define CTX_F_CONCAT (1 << 8) /* inside concat_expr */
struct json_ctx {
struct input_descriptor indesc;
struct nft_ctx *nft;
struct list_head *msgs;
struct list_head *cmds;
uint32_t flags;
};
#define is_RHS(ctx) (ctx->flags & CTX_F_RHS)
#define is_STMT(ctx) (ctx->flags & CTX_F_STMT)
#define is_PRIMARY(ctx) (ctx->flags & CTX_F_PRIMARY)
#define is_DTYPE(ctx) (ctx->flags & CTX_F_DTYPE)
#define is_SET_RHS(ctx) (ctx->flags & CTX_F_SET_RHS)
static char *ctx_flags_to_string(struct json_ctx *ctx)
{
static char buf[1024];
const char *sep = "";
buf[0] = '\0';
if (is_RHS(ctx)) {
strcat(buf, sep);
strcat(buf, "RHS");
sep = ", ";
}
if (is_STMT(ctx)) {
strcat(buf, sep);
strcat(buf, "STMT");
sep = ", ";
}
if (is_PRIMARY(ctx)) {
strcat(buf, sep);
strcat(buf, "PRIMARY");
sep = ", ";
}
if (is_DTYPE(ctx)) {
strcat(buf, sep);
strcat(buf, "DTYPE");
sep = ", ";
}
if (is_SET_RHS(ctx)) {
strcat(buf, sep);
strcat(buf, "SET_RHS");
sep = ", ";
}
return buf;
}
/* common parser entry points */
static struct expr *json_parse_expr(struct json_ctx *ctx, json_t *root);
static struct expr *json_parse_rhs_expr(struct json_ctx *ctx, json_t *root);
static struct expr *json_parse_stmt_expr(struct json_ctx *ctx, json_t *root);
static struct expr *json_parse_primary_expr(struct json_ctx *ctx, json_t *root);
static struct expr *json_parse_set_rhs_expr(struct json_ctx *ctx, json_t *root);
static struct expr *json_parse_set_elem_expr_stmt(struct json_ctx *ctx, json_t *root);
static struct expr *json_parse_map_lhs_expr(struct json_ctx *ctx, json_t *root);
static struct expr *json_parse_concat_elem_expr(struct json_ctx *ctx, json_t *root);
static struct stmt *json_parse_stmt(struct json_ctx *ctx, json_t *root);
/* parsing helpers */
const struct location *int_loc = &internal_location;
static void json_lib_error(struct json_ctx *ctx, json_error_t *err)
{
struct location loc = {
.indesc = &ctx->indesc,
.line_offset = err->position - err->column,
.first_line = err->line,
.last_line = err->line,
.first_column = err->column,
/* no information where problematic part ends :( */
.last_column = err->column,
};
erec_queue(error(&loc, err->text), ctx->msgs);
}
__attribute__((format(printf, 2, 3)))
static void json_error(struct json_ctx *ctx, const char *fmt, ...)
{
struct error_record *erec;
va_list ap;
va_start(ap, fmt);
erec = erec_vcreate(EREC_ERROR, int_loc, fmt, ap);
va_end(ap);
erec_queue(erec, ctx->msgs);
}
static const char *json_typename(const json_t *val)
{
const char *type_name[] = {
[JSON_OBJECT] = "object",
[JSON_ARRAY] = "array",
[JSON_STRING] = "string",
[JSON_INTEGER] = "integer",
[JSON_REAL] = "real",
[JSON_TRUE] = "true",
[JSON_FALSE] = "false",
[JSON_NULL] = "null"
};
return type_name[json_typeof(val)];
}
static int json_unpack_err(struct json_ctx *ctx,
json_t *root, const char *fmt, ...)
{
json_error_t err;
va_list ap;
int rc;
va_start(ap, fmt);
rc = json_vunpack_ex(root, &err, 0, fmt, ap);
va_end(ap);
if (rc)
json_lib_error(ctx, &err);
return rc;
}
static int json_unpack_stmt(struct json_ctx *ctx, json_t *root,
const char **key, json_t **value)
{
assert(key);
assert(value);
if (json_object_size(root) != 1) {
json_error(ctx, "Malformed object (too many properties): '%s'.",
json_dumps(root, 0));
return 1;
}
json_object_foreach(root, *key, *value)
return 0;
/* not reached */
return 1;
}
static int parse_family(const char *name, uint32_t *family)
{
unsigned int i;
struct {
const char *name;
int val;
} family_tbl[] = {
{ "ip", NFPROTO_IPV4 },
{ "ip6", NFPROTO_IPV6 },
{ "inet", NFPROTO_INET },
{ "arp", NFPROTO_ARP },
{ "bridge", NFPROTO_BRIDGE },
{ "netdev", NFPROTO_NETDEV }
};
assert(family);
for (i = 0; i < array_size(family_tbl); i++) {
if (strcmp(name, family_tbl[i].name))
continue;
*family = family_tbl[i].val;
return 0;
}
return -1;
}
static int json_parse_family(struct json_ctx *ctx, json_t *root)
{
const char *family;
if (!json_unpack(root, "{s:s}", "family", &family)) {
uint32_t familyval;
if (parse_family(family, &familyval) ||
(familyval != NFPROTO_IPV6 &&
familyval != NFPROTO_IPV4)) {
json_error(ctx, "Invalid family '%s'.", family);
return -1;
}
return familyval;
}
return NFPROTO_UNSPEC;
}
static bool is_keyword(const char *keyword)
{
const char *keywords[] = {
"ether",
"ip",
"ip6",
"vlan",
"arp",
"dnat",
"snat",
"ecn",
"reset",
"original",
"reply",
"label",
};
unsigned int i;
for (i = 0; i < array_size(keywords); i++) {
if (!strcmp(keyword, keywords[i]))
return true;
}
return false;
}
static bool is_constant(const char *keyword)
{
const char *constants[] = {
"tcp",
"udp",
"udplite",
"esp",
"ah",
"icmp",
"icmpv6",
"comp",
"dccp",
"sctp",
"redirect",
};
unsigned int i;
for (i = 0; i < array_size(constants); i++) {
if (!strcmp(keyword, constants[i]))
return true;
}
return false;
}
static struct expr *json_parse_constant(struct json_ctx *ctx, const char *name)
{
const struct {
const char *name;
uint8_t data;
const struct datatype *dtype;
} constant_tbl[] = {
{ "tcp", IPPROTO_TCP, &inet_protocol_type },
{ "udp", IPPROTO_UDP, &inet_protocol_type },
{ "udplite", IPPROTO_UDPLITE, &inet_protocol_type },
{ "esp", IPPROTO_ESP, &inet_protocol_type },
{ "ah", IPPROTO_AH, &inet_protocol_type },
{ "icmp", IPPROTO_ICMP, &inet_protocol_type },
{ "icmpv6", IPPROTO_ICMPV6, &inet_protocol_type },
{ "comp", IPPROTO_COMP, &inet_protocol_type },
{ "dccp", IPPROTO_DCCP, &inet_protocol_type },
{ "sctp", IPPROTO_SCTP, &inet_protocol_type },
{ "redirect", ICMP_REDIRECT, &icmp_type_type },
};
unsigned int i;
for (i = 0; i < array_size(constant_tbl); i++) {
if (strcmp(name, constant_tbl[i].name))
continue;
return constant_expr_alloc(int_loc,
constant_tbl[i].dtype,
BYTEORDER_HOST_ENDIAN,
BITS_PER_BYTE,
&constant_tbl[i].data);
}
json_error(ctx, "Unknown constant '%s'.", name);
return NULL;
}
static struct expr *wildcard_expr_alloc(void)
{
struct expr *expr;
expr = constant_expr_alloc(int_loc, &integer_type,
BYTEORDER_HOST_ENDIAN, 0, NULL);
return prefix_expr_alloc(int_loc, expr, 0);
}
/* this is a combination of symbol_expr, integer_expr, boolean_expr ... */
static struct expr *json_parse_immediate(struct json_ctx *ctx, json_t *root)
{
enum symbol_types symtype = SYMBOL_VALUE;
const char *str;
char buf[64] = {};
switch (json_typeof(root)) {
case JSON_STRING:
str = json_string_value(root);
if (str[0] == '@') {
symtype = SYMBOL_SET;
str++;
} else if (str[0] == '*' && str[1] == '\0') {
return wildcard_expr_alloc();
} else if (is_keyword(str)) {
return symbol_expr_alloc(int_loc,
SYMBOL_VALUE, NULL, str);
} else if (is_constant(str)) {
return json_parse_constant(ctx, str);
}
break;
case JSON_INTEGER:
snprintf(buf, sizeof(buf),
"%" JSON_INTEGER_FORMAT, json_integer_value(root));
str = buf;
break;
case JSON_TRUE:
case JSON_FALSE:
buf[0] = json_is_true(root);
return constant_expr_alloc(int_loc, &boolean_type,
BYTEORDER_HOST_ENDIAN,
BITS_PER_BYTE, buf);
default:
json_error(ctx, "Unexpected JSON type %s for immediate value.",
json_typename(root));
return NULL;
}
return symbol_expr_alloc(int_loc, symtype, NULL, str);
}
static struct expr *json_parse_meta_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
struct error_record *erec;
unsigned int key;
const char *name;
if (json_unpack_err(ctx, root, "{s:s}", "key", &name))
return NULL;
erec = meta_key_parse(int_loc, name, &key);
if (erec) {
erec_queue(erec, ctx->msgs);
return NULL;
}
return meta_expr_alloc(int_loc, key);
}
static struct expr *json_parse_osf_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const char *key, *ttl;
uint32_t flagval = 0;
uint8_t ttlval = 0;
if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
return NULL;
if (!json_unpack(root, "{s:s}", "ttl", &ttl)) {
if (!strcmp(ttl, "loose")) {
ttlval = 1;
} else if (!strcmp(ttl, "skip")) {
ttlval = 2;
} else {
json_error(ctx, "Invalid osf ttl option '%s'.", ttl);
return NULL;
}
}
if (!strcmp(key, "name")) {
return osf_expr_alloc(int_loc, ttlval, flagval);
} else if (!strcmp(key, "version")) {
flagval |= NFT_OSF_F_VERSION;
return osf_expr_alloc(int_loc, ttlval, flagval);
}
json_error(ctx, "Invalid osf key value.");
return NULL;
}
static struct expr *json_parse_socket_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const char *key;
int keyval = -1;
if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
return NULL;
if (!strcmp(key, "transparent"))
keyval = NFT_SOCKET_TRANSPARENT;
else if (!strcmp(key, "mark"))
keyval = NFT_SOCKET_MARK;
if (keyval == -1) {
json_error(ctx, "Invalid socket key value.");
return NULL;
}
return socket_expr_alloc(int_loc, keyval);
}
static int json_parse_payload_field(const struct proto_desc *desc,
const char *name, int *field)
{
unsigned int i;
for (i = 0; i < PROTO_HDRS_MAX; i++) {
if (desc->templates[i].token &&
!strcmp(desc->templates[i].token, name)) {
if (field)
*field = i;
return 0;
}
}
return 1;
}
static int json_parse_tcp_option_type(const char *name, int *val)
{
unsigned int i;
for (i = 0; i < array_size(tcpopthdr_protocols); i++) {
if (tcpopthdr_protocols[i] &&
!strcmp(tcpopthdr_protocols[i]->name, name)) {
if (val)
*val = i;
return 0;
}
}
/* special case for sack0 - sack3 */
if (sscanf(name, "sack%u", &i) == 1 && i < 4) {
if (val)
*val = TCPOPTHDR_SACK0 + i;
return 0;
}
return 1;
}
static int json_parse_tcp_option_field(int type, const char *name, int *val)
{
unsigned int i;
const struct exthdr_desc *desc = tcpopthdr_protocols[type];
for (i = 0; i < array_size(desc->templates); i++) {
if (desc->templates[i].token &&
!strcmp(desc->templates[i].token, name)) {
if (val)
*val = i;
return 0;
}
}
return 1;
}
static const struct proto_desc *proto_lookup_byname(const char *name)
{
const struct proto_desc *proto_tbl[] = {
&proto_eth,
&proto_vlan,
&proto_arp,
&proto_ip,
&proto_icmp,
&proto_igmp,
&proto_ip6,
&proto_icmp6,
&proto_ah,
&proto_esp,
&proto_comp,
&proto_udp,
&proto_udplite,
&proto_tcp,
&proto_dccp,
&proto_sctp,
&proto_th,
};
unsigned int i;
for (i = 0; i < array_size(proto_tbl); i++) {
if (!strcmp(proto_tbl[i]->name, name))
return proto_tbl[i];
}
return NULL;
}
static struct expr *json_parse_payload_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const char *protocol, *field, *base;
int offset, len, val;
struct expr *expr;
if (!json_unpack(root, "{s:s, s:i, s:i}",
"base", &base, "offset", &offset, "len", &len)) {
if (!strcmp(base, "ll")) {
val = PROTO_BASE_LL_HDR;
} else if (!strcmp(base, "nh")) {
val = PROTO_BASE_NETWORK_HDR;
} else if (!strcmp(base, "th")) {
val = PROTO_BASE_TRANSPORT_HDR;
} else {
json_error(ctx, "Invalid payload base '%s'.", base);
return NULL;
}
expr = payload_expr_alloc(int_loc, NULL, 0);
payload_init_raw(expr, val, offset, len);
expr->byteorder = BYTEORDER_BIG_ENDIAN;
expr->payload.is_raw = true;
return expr;
} else if (!json_unpack(root, "{s:s, s:s}",
"protocol", &protocol, "field", &field)) {
const struct proto_desc *proto = proto_lookup_byname(protocol);
if (!proto) {
json_error(ctx, "Unknown payload protocol '%s'.",
protocol);
return NULL;
}
if (json_parse_payload_field(proto, field, &val)) {
json_error(ctx, "Unknown %s field '%s'.",
protocol, field);
return NULL;
}
expr = payload_expr_alloc(int_loc, proto, val);
if (proto == &proto_th)
expr->payload.is_raw = true;
return expr;
}
json_error(ctx, "Invalid payload expression properties.");
return NULL;
}
static struct expr *json_parse_tcp_option_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const char *desc, *field;
int descval, fieldval;
struct expr *expr;
if (json_unpack_err(ctx, root, "{s:s}", "name", &desc))
return NULL;
if (json_parse_tcp_option_type(desc, &descval)) {
json_error(ctx, "Unknown tcp option name '%s'.", desc);
return NULL;
}
if (json_unpack(root, "{s:s}", "field", &field)) {
expr = tcpopt_expr_alloc(int_loc, descval,
TCPOPTHDR_FIELD_KIND);
expr->exthdr.flags = NFT_EXTHDR_F_PRESENT;
return expr;
}
if (json_parse_tcp_option_field(descval, field, &fieldval)) {
json_error(ctx, "Unknown tcp option field '%s'.", field);
return NULL;
}
return tcpopt_expr_alloc(int_loc, descval, fieldval);
}
static int json_parse_ip_option_type(const char *name, int *val)
{
unsigned int i;
for (i = 0; i < array_size(ipopt_protocols); i++) {
if (ipopt_protocols[i] &&
!strcmp(ipopt_protocols[i]->name, name)) {
if (val)
*val = i;
return 0;
}
}
return 1;
}
static int json_parse_ip_option_field(int type, const char *name, int *val)
{
unsigned int i;
const struct exthdr_desc *desc = ipopt_protocols[type];
for (i = 0; i < array_size(desc->templates); i++) {
if (desc->templates[i].token &&
!strcmp(desc->templates[i].token, name)) {
if (val)
*val = i;
return 0;
}
}
return 1;
}
static struct expr *json_parse_ip_option_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const char *desc, *field;
int descval, fieldval;
struct expr *expr;
if (json_unpack_err(ctx, root, "{s:s}", "name", &desc))
return NULL;
if (json_parse_ip_option_type(desc, &descval)) {
json_error(ctx, "Unknown ip option name '%s'.", desc);
return NULL;
}
if (json_unpack(root, "{s:s}", "field", &field)) {
expr = ipopt_expr_alloc(int_loc, descval,
IPOPT_FIELD_TYPE, 0);
expr->exthdr.flags = NFT_EXTHDR_F_PRESENT;
return expr;
}
if (json_parse_ip_option_field(descval, field, &fieldval)) {
json_error(ctx, "Unknown ip option field '%s'.", field);
return NULL;
}
return ipopt_expr_alloc(int_loc, descval, fieldval, 0);
}
static const struct exthdr_desc *exthdr_lookup_byname(const char *name)
{
const struct exthdr_desc *exthdr_tbl[] = {
&exthdr_hbh,
&exthdr_rt,
&exthdr_rt0,
&exthdr_rt2,
&exthdr_rt4,
&exthdr_frag,
&exthdr_dst,
&exthdr_mh,
};
unsigned int i;
for (i = 0; i < array_size(exthdr_tbl); i++) {
if (!strcmp(exthdr_tbl[i]->name, name))
return exthdr_tbl[i];
}
return NULL;
}
static int json_parse_exthdr_field(const struct exthdr_desc *desc,
const char *name, int *field)
{
unsigned int i;
for (i = 0; i < array_size(desc->templates); i++) {
if (desc->templates[i].token &&
!strcmp(desc->templates[i].token, name)) {
if (field)
*field = i;
return 0;
}
}
return 1;
}
static struct expr *json_parse_exthdr_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const char *name, *field = NULL;
struct expr *expr;
int offset = 0, fieldval;
const struct exthdr_desc *desc;
if (json_unpack_err(ctx, root, "{s:s}", "name", &name))
return NULL;
desc = exthdr_lookup_byname(name);
if (!desc) {
json_error(ctx, "Invalid exthdr protocol '%s'.", name);
return NULL;
}
if (json_unpack(root, "{s:s}", "field", &field)) {
expr = exthdr_expr_alloc(int_loc, desc, 1);
expr->exthdr.flags = NFT_EXTHDR_F_PRESENT;
return expr;
}
if (json_parse_exthdr_field(desc, field, &fieldval)) {
json_error(ctx, "Unknown %s field %s.", desc->name, field);
return NULL;
}
/* special treatment for rt0 */
if (desc == &exthdr_rt0 &&
json_unpack_err(ctx, root, "{s:i}", "offset", &offset))
return NULL;
return exthdr_expr_alloc(int_loc, desc, fieldval + offset);
}
static struct expr *json_parse_rt_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const struct {
const char *name;
int val;
} rt_key_tbl[] = {
{ "classid", NFT_RT_CLASSID },
{ "nexthop", NFT_RT_NEXTHOP4 },
{ "mtu", NFT_RT_TCPMSS },
{ "ipsec", NFT_RT_XFRM },
};
const char *key;
unsigned int i;
int familyval;
if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
return NULL;
familyval = json_parse_family(ctx, root);
if (familyval < 0)
return NULL;
for (i = 0; i < array_size(rt_key_tbl); i++) {
int val = rt_key_tbl[i].val;
bool invalid = true;
if (strcmp(key, rt_key_tbl[i].name))
continue;
if (familyval) {
if (familyval == NFPROTO_IPV6 &&
val == NFT_RT_NEXTHOP4)
val = NFT_RT_NEXTHOP6;
invalid = false;
}
return rt_expr_alloc(int_loc, val, invalid);
}
json_error(ctx, "Unknown rt key '%s'.", key);
return NULL;
}
static bool ct_key_is_dir(enum nft_ct_keys key)
{
const enum nft_ct_keys ct_dir_keys[] = {
NFT_CT_L3PROTOCOL,
NFT_CT_SRC,
NFT_CT_DST,
NFT_CT_PROTOCOL,
NFT_CT_PROTO_SRC,
NFT_CT_PROTO_DST,
NFT_CT_PKTS,
NFT_CT_BYTES,
NFT_CT_AVGPKT,
NFT_CT_ZONE,
NFT_CT_SRC_IP,
NFT_CT_DST_IP,
NFT_CT_SRC_IP6,
NFT_CT_DST_IP6,
};
unsigned int i;
for (i = 0; i < array_size(ct_dir_keys); i++) {
if (key == ct_dir_keys[i])
return true;
}
return false;
}
static struct expr *json_parse_ct_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
int dirval = -1, keyval = -1;
const char *key, *dir;
unsigned int i;
if (json_unpack_err(ctx, root, "{s:s}", "key", &key))
return NULL;
for (i = 0; i < array_size(ct_templates); i++) {
if (ct_templates[i].token &&
!strcmp(key, ct_templates[i].token)) {
keyval = i;
break;
}
}
if (keyval == -1) {
json_error(ctx, "Unknown ct key '%s'.", key);
return NULL;
}
if (!json_unpack(root, "{s:s}", "dir", &dir)) {
if (!strcmp(dir, "original")) {
dirval = IP_CT_DIR_ORIGINAL;
} else if (!strcmp(dir, "reply")) {
dirval = IP_CT_DIR_REPLY;
} else {
json_error(ctx, "Invalid direction '%s'.", dir);
return NULL;
}
if (!ct_key_is_dir(keyval)) {
json_error(ctx, "Direction not supported by CT key '%s'.", key);
return NULL;
}
}
return ct_expr_alloc(int_loc, keyval, dirval);
}
static struct expr *json_parse_numgen_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
int modeval, mod, offset = 0;
const char *mode;
if (json_unpack_err(ctx, root, "{s:s, s:i}",
"mode", &mode, "mod", &mod))
return NULL;
json_unpack(root, "{s:i}", "offset", &offset);
if (!strcmp(mode, "inc")) {
modeval = NFT_NG_INCREMENTAL;
} else if (!strcmp(mode, "random")) {
modeval = NFT_NG_RANDOM;
} else {
json_error(ctx, "Unknown numgen mode '%s'.", mode);
return NULL;
}
return numgen_expr_alloc(int_loc, modeval, mod, offset);
}
static struct expr *json_parse_hash_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
int mod, offset = 0, seed = 0;
struct expr *expr, *hash_expr;
bool have_seed;
json_t *jexpr;
if (json_unpack_err(ctx, root, "{s:i}", "mod", &mod))
return NULL;
json_unpack(root, "{s:i}", "offset", &offset);
if (!strcmp(type, "symhash")) {
return hash_expr_alloc(int_loc, mod, false, 0,
offset, NFT_HASH_SYM);
} else if (strcmp(type, "jhash")) {
json_error(ctx, "Unknown hash type '%s'.", type);
return NULL;
}
if (json_unpack_err(ctx, root, "{s:o}", "expr", &jexpr))
return NULL;
expr = json_parse_expr(ctx, jexpr);
if (!expr) {
json_error(ctx, "Invalid jhash expression.");
return NULL;
}
have_seed = !json_unpack(root, "{s:i}", "seed", &seed);
hash_expr = hash_expr_alloc(int_loc, mod, have_seed,
seed, offset, NFT_HASH_JENKINS);
hash_expr->hash.expr = expr;
return hash_expr;
}
static int fib_flag_parse(const char *name, int *flags)
{
const char *fib_flags[] = {
"saddr",
"daddr",
"mark",
"iif",
"oif",
};
unsigned int i;
for (i = 0; i < array_size(fib_flags); i++) {
if (!strcmp(name, fib_flags[i])) {
*flags |= (1 << i);
return 0;
}
}
return 1;
}
static struct expr *json_parse_fib_expr(struct json_ctx *ctx,
const char *type, json_t *root)
{
const char *fib_result_tbl[] = {
[NFT_FIB_RESULT_UNSPEC] = NULL,
[NFT_FIB_RESULT_OIF] = "oif",
[NFT_FIB_RESULT_OIFNAME] = "oifname",
[NFT_FIB_RESULT_ADDRTYPE] = "type",
};
enum nft_fib_result resultval = NFT_FIB_RESULT_UNSPEC;
json_t *flags, *value;
const char *result;
unsigned int i;
size_t index;
int flagval = 0;
if (json_unpack_err(ctx, root, "{s:s}", "result", &result))
return NULL;
for (i = 1; i < array_size(fib_result_tbl); i++) {
if (!strcmp(result, fib_result_tbl[i])) {
resultval = i;
break;
}
}
if (resultval == NFT_FIB_RESULT_UNSPEC) {
json_error(ctx, "Invalid fib result '%s'.", result);
return NULL;
}
if (!json_unpack(root, "{s:o}", "flags", &flags)) {
const char *flag;
if (json_is_string(flags)) {
flag = json_string_value(flags);
if (fib_flag_parse(flag, &flagval)) {
json_error(ctx, "Invalid fib flag '%s'.", flag);
return NULL;
}
} else if (!json_is_array(flags)) {
json_error(ctx, "Unexpected object type in fib tuple.");
return NULL;
}
json_array_foreach(flags, index, value) {
if (!json_is_string(value)) {
json_error(ctx, "Unexpected object type in fib flags array at index %zd.", index);
return NULL;
}
flag = json_string_value(value);
if (fib_flag_parse(flag, &flagval)) {
json_error(ctx, "Invalid fib flag '%s'.", flag);
return NULL;
}
}
}
/* sanity checks from fib_expr in parser_bison.y */
if ((flagval & (NFTA_FIB_F_SADDR|NFTA_FIB_F_DADDR)) == 0) {
json_error(ctx, "fib: need either saddr or daddr");
return NULL;
}
if ((flagval & (NFTA_FIB_F_SADDR|NFTA_FIB_F_DADDR)) ==
(NFTA_FIB_F_SADDR|NFTA_FIB_F_DADDR)) {
json_error(ctx, "fib: saddr and daddr are mutually exclusive");
return NULL;
}
if ((flagval & (NFTA_FIB_F_IIF|NFTA_FIB_F_OIF)) ==
(NFTA_FIB_F_IIF|NFTA_FIB_F_OIF)) {
json_error(ctx, "fib: iif and oif are mutually exclusive");
return NULL;
}
return fib_expr_alloc(int_loc, flagval, resultval);
}