-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.c
4683 lines (4002 loc) · 122 KB
/
evaluate.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
/*
* Copyright (c) 2008 Patrick McHardy <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Development of this code funded by Astaro AG (http://www.astaro.com/)
*/
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <arpa/inet.h>
#include <linux/netfilter.h>
#include <linux/netfilter_arp.h>
#include <linux/netfilter/nf_tables.h>
#include <linux/netfilter/nf_synproxy.h>
#include <linux/netfilter/nf_nat.h>
#include <linux/netfilter/nf_log.h>
#include <linux/netfilter_ipv4.h>
#include <netinet/ip_icmp.h>
#include <netinet/icmp6.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <errno.h>
#include <expression.h>
#include <statement.h>
#include <netlink.h>
#include <time.h>
#include <rule.h>
#include <cache.h>
#include <erec.h>
#include <gmputil.h>
#include <utils.h>
#include <xt.h>
static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr);
static const char * const byteorder_names[] = {
[BYTEORDER_INVALID] = "invalid",
[BYTEORDER_HOST_ENDIAN] = "host endian",
[BYTEORDER_BIG_ENDIAN] = "big endian",
};
#define chain_error(ctx, s1, fmt, args...) \
__stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args)
#define monitor_error(ctx, s1, fmt, args...) \
__stmt_binary_error(ctx, &(s1)->location, NULL, fmt, ## args)
#define cmd_error(ctx, loc, fmt, args...) \
__stmt_binary_error(ctx, loc, NULL, fmt, ## args)
static int __fmtstring(3, 4) set_error(struct eval_ctx *ctx,
const struct set *set,
const char *fmt, ...)
{
struct error_record *erec;
va_list ap;
va_start(ap, fmt);
erec = erec_vcreate(EREC_ERROR, &set->location, fmt, ap);
va_end(ap);
erec_queue(erec, ctx->msgs);
return -1;
}
static void key_fix_dtype_byteorder(struct expr *key)
{
const struct datatype *dtype = key->dtype;
if (dtype->byteorder == key->byteorder)
return;
datatype_set(key, set_datatype_alloc(dtype, key->byteorder));
}
static int set_evaluate(struct eval_ctx *ctx, struct set *set);
static struct expr *implicit_set_declaration(struct eval_ctx *ctx,
const char *name,
struct expr *key,
struct expr *data,
struct expr *expr)
{
struct cmd *cmd;
struct set *set;
struct handle h;
if (set_is_datamap(expr->set_flags))
key_fix_dtype_byteorder(key);
set = set_alloc(&expr->location);
set->flags = NFT_SET_ANONYMOUS | expr->set_flags;
set->handle.set.name = xstrdup(name);
set->key = key;
set->data = data;
set->init = expr;
set->automerge = set->flags & NFT_SET_INTERVAL;
if (ctx->table != NULL)
list_add_tail(&set->list, &ctx->table->sets);
else {
handle_merge(&set->handle, &ctx->cmd->handle);
memset(&h, 0, sizeof(h));
handle_merge(&h, &set->handle);
h.set.location = expr->location;
cmd = cmd_alloc(CMD_ADD, CMD_OBJ_SET, &h, &expr->location, set);
cmd->location = set->location;
list_add_tail(&cmd->list, &ctx->cmd->list);
}
set_evaluate(ctx, set);
return set_ref_expr_alloc(&expr->location, set);
}
static enum ops byteorder_conversion_op(struct expr *expr,
enum byteorder byteorder)
{
switch (expr->byteorder) {
case BYTEORDER_HOST_ENDIAN:
if (byteorder == BYTEORDER_BIG_ENDIAN)
return OP_HTON;
break;
case BYTEORDER_BIG_ENDIAN:
if (byteorder == BYTEORDER_HOST_ENDIAN)
return OP_NTOH;
break;
default:
break;
}
BUG("invalid byte order conversion %u => %u\n",
expr->byteorder, byteorder);
}
static int byteorder_conversion(struct eval_ctx *ctx, struct expr **expr,
enum byteorder byteorder)
{
enum ops op;
assert(!expr_is_constant(*expr) || expr_is_singleton(*expr));
if ((*expr)->byteorder == byteorder)
return 0;
/* Conversion for EXPR_CONCAT is handled for single composing ranges */
if ((*expr)->etype == EXPR_CONCAT)
return 0;
if (expr_basetype(*expr)->type != TYPE_INTEGER)
return expr_error(ctx->msgs, *expr,
"Byteorder mismatch: expected %s, got %s",
byteorder_names[byteorder],
byteorder_names[(*expr)->byteorder]);
if (expr_is_constant(*expr) || (*expr)->len / BITS_PER_BYTE < 2)
(*expr)->byteorder = byteorder;
else {
op = byteorder_conversion_op(*expr, byteorder);
*expr = unary_expr_alloc(&(*expr)->location, op, *expr);
if (expr_evaluate(ctx, expr) < 0)
return -1;
}
return 0;
}
static struct table *table_lookup_global(struct eval_ctx *ctx)
{
struct table *table;
if (ctx->table != NULL)
return ctx->table;
table = table_lookup(&ctx->cmd->handle, &ctx->nft->cache);
if (table == NULL)
return NULL;
return table;
}
static int table_not_found(struct eval_ctx *ctx)
{
struct table *table;
table = table_lookup_fuzzy(&ctx->cmd->handle, &ctx->nft->cache);
if (table == NULL)
return cmd_error(ctx, &ctx->cmd->handle.table.location,
"%s", strerror(ENOENT));
return cmd_error(ctx, &ctx->cmd->handle.table.location,
"%s; did you mean table ‘%s’ in family %s?",
strerror(ENOENT), table->handle.table.name,
family2str(table->handle.family));
}
static int chain_not_found(struct eval_ctx *ctx)
{
const struct table *table;
struct chain *chain;
chain = chain_lookup_fuzzy(&ctx->cmd->handle, &ctx->nft->cache, &table);
if (chain == NULL)
return cmd_error(ctx, &ctx->cmd->handle.chain.location,
"%s", strerror(ENOENT));
return cmd_error(ctx, &ctx->cmd->handle.chain.location,
"%s; did you mean chain ‘%s’ in table %s ‘%s’?",
strerror(ENOENT), chain->handle.chain.name,
family2str(chain->handle.family),
table->handle.table.name);
}
static int set_not_found(struct eval_ctx *ctx, const struct location *loc,
const char *set_name)
{
const struct table *table;
struct set *set;
set = set_lookup_fuzzy(set_name, &ctx->nft->cache, &table);
if (set == NULL)
return cmd_error(ctx, loc, "%s", strerror(ENOENT));
return cmd_error(ctx, loc,
"%s; did you mean %s ‘%s’ in table %s ‘%s’?",
strerror(ENOENT),
set_is_map(set->flags) ? "map" : "set",
set->handle.set.name,
family2str(set->handle.family),
table->handle.table.name);
}
static int flowtable_not_found(struct eval_ctx *ctx, const struct location *loc,
const char *ft_name)
{
const struct table *table;
struct flowtable *ft;
ft = flowtable_lookup_fuzzy(ft_name, &ctx->nft->cache, &table);
if (ft == NULL)
return cmd_error(ctx, loc, "%s", strerror(ENOENT));
return cmd_error(ctx, loc,
"%s; did you mean flowtable ‘%s’ in table %s ‘%s’?",
strerror(ENOENT), ft->handle.flowtable.name,
family2str(ft->handle.family),
table->handle.table.name);
}
/*
* Symbol expression: parse symbol and evaluate resulting expression.
*/
static int expr_evaluate_symbol(struct eval_ctx *ctx, struct expr **expr)
{
struct parse_ctx parse_ctx = { .tbl = &ctx->nft->output.tbl, };
struct error_record *erec;
struct table *table;
struct set *set;
struct expr *new;
switch ((*expr)->symtype) {
case SYMBOL_VALUE:
datatype_set(*expr, ctx->ectx.dtype);
erec = symbol_parse(&parse_ctx, *expr, &new);
if (erec != NULL) {
erec_queue(erec, ctx->msgs);
return -1;
}
break;
case SYMBOL_SET:
table = table_lookup_global(ctx);
if (table == NULL)
return table_not_found(ctx);
set = set_lookup(table, (*expr)->identifier);
if (set == NULL)
return set_not_found(ctx, &(*expr)->location,
(*expr)->identifier);
new = set_ref_expr_alloc(&(*expr)->location, set);
break;
}
expr_free(*expr);
*expr = new;
return expr_evaluate(ctx, expr);
}
static int expr_evaluate_string(struct eval_ctx *ctx, struct expr **exprp)
{
struct expr *expr = *exprp;
unsigned int len = div_round_up(expr->len, BITS_PER_BYTE), datalen;
struct expr *value, *prefix;
int data_len = ctx->ectx.len > 0 ? ctx->ectx.len : len + 1;
char data[data_len];
if (ctx->ectx.len > 0) {
if (expr->len > ctx->ectx.len)
return expr_error(ctx->msgs, expr,
"String exceeds maximum length of %u",
ctx->ectx.len / BITS_PER_BYTE);
expr->len = ctx->ectx.len;
}
memset(data + len, 0, data_len - len);
mpz_export_data(data, expr->value, BYTEORDER_HOST_ENDIAN, len);
if (strlen(data) == 0)
return expr_error(ctx->msgs, expr,
"Empty string is not allowed");
datalen = strlen(data) - 1;
if (data[datalen] != '*') {
/* We need to reallocate the constant expression with the right
* expression length to avoid problems on big endian.
*/
value = constant_expr_alloc(&expr->location, ctx->ectx.dtype,
BYTEORDER_HOST_ENDIAN,
expr->len, data);
expr_free(expr);
*exprp = value;
return 0;
}
if (datalen >= 1 &&
data[datalen - 1] == '\\') {
char unescaped_str[data_len];
memset(unescaped_str, 0, sizeof(unescaped_str));
xstrunescape(data, unescaped_str);
value = constant_expr_alloc(&expr->location, ctx->ectx.dtype,
BYTEORDER_HOST_ENDIAN,
expr->len, unescaped_str);
expr_free(expr);
*exprp = value;
return 0;
}
value = constant_expr_alloc(&expr->location, ctx->ectx.dtype,
BYTEORDER_HOST_ENDIAN,
datalen * BITS_PER_BYTE, data);
prefix = prefix_expr_alloc(&expr->location, value,
datalen * BITS_PER_BYTE);
datatype_set(prefix, ctx->ectx.dtype);
prefix->flags |= EXPR_F_CONSTANT;
prefix->byteorder = BYTEORDER_HOST_ENDIAN;
expr_free(expr);
*exprp = prefix;
return 0;
}
static int expr_evaluate_integer(struct eval_ctx *ctx, struct expr **exprp)
{
struct expr *expr = *exprp;
char *valstr, *rangestr;
mpz_t mask;
if (ctx->ectx.maxval > 0 &&
mpz_cmp_ui(expr->value, ctx->ectx.maxval) > 0) {
valstr = mpz_get_str(NULL, 10, expr->value);
expr_error(ctx->msgs, expr,
"Value %s exceeds valid range 0-%u",
valstr, ctx->ectx.maxval);
free(valstr);
return -1;
}
mpz_init_bitmask(mask, ctx->ectx.len);
if (mpz_cmp(expr->value, mask) > 0) {
valstr = mpz_get_str(NULL, 10, expr->value);
rangestr = mpz_get_str(NULL, 10, mask);
expr_error(ctx->msgs, expr,
"Value %s exceeds valid range 0-%s",
valstr, rangestr);
free(valstr);
free(rangestr);
mpz_clear(mask);
return -1;
}
expr->byteorder = ctx->ectx.byteorder;
expr->len = ctx->ectx.len;
mpz_clear(mask);
return 0;
}
static int expr_evaluate_value(struct eval_ctx *ctx, struct expr **expr)
{
switch (expr_basetype(*expr)->type) {
case TYPE_INTEGER:
if (expr_evaluate_integer(ctx, expr) < 0)
return -1;
break;
case TYPE_STRING:
if (expr_evaluate_string(ctx, expr) < 0)
return -1;
break;
default:
BUG("invalid basetype %s\n", expr_basetype(*expr)->name);
}
return 0;
}
/*
* Primary expressions determine the datatype context.
*/
static int expr_evaluate_primary(struct eval_ctx *ctx, struct expr **expr)
{
__expr_set_context(&ctx->ectx, (*expr)->dtype, (*expr)->byteorder,
(*expr)->len, 0);
return 0;
}
static int
conflict_resolution_gen_dependency(struct eval_ctx *ctx, int protocol,
const struct expr *expr,
struct stmt **res)
{
enum proto_bases base = expr->payload.base;
const struct proto_hdr_template *tmpl;
const struct proto_desc *desc = NULL;
struct expr *dep, *left, *right;
struct stmt *stmt;
assert(expr->payload.base == PROTO_BASE_LL_HDR);
desc = ctx->pctx.protocol[base].desc;
tmpl = &desc->templates[desc->protocol_key];
left = payload_expr_alloc(&expr->location, desc, desc->protocol_key);
right = constant_expr_alloc(&expr->location, tmpl->dtype,
tmpl->dtype->byteorder, tmpl->len,
constant_data_ptr(protocol, tmpl->len));
dep = relational_expr_alloc(&expr->location, OP_EQ, left, right);
stmt = expr_stmt_alloc(&dep->location, dep);
if (stmt_evaluate(ctx, stmt) < 0)
return expr_error(ctx->msgs, expr,
"dependency statement is invalid");
*res = stmt;
return 0;
}
static uint8_t expr_offset_shift(const struct expr *expr, unsigned int offset,
unsigned int *extra_len)
{
unsigned int new_offset, len;
int shift;
new_offset = offset % BITS_PER_BYTE;
len = round_up(expr->len, BITS_PER_BYTE);
shift = len - (new_offset + expr->len);
while (shift < 0) {
shift += BITS_PER_BYTE;
*extra_len += BITS_PER_BYTE;
}
return shift;
}
static void expr_evaluate_bits(struct eval_ctx *ctx, struct expr **exprp)
{
struct expr *expr = *exprp, *and, *mask, *rshift, *off;
unsigned masklen, len = expr->len, extra_len = 0;
uint8_t shift;
mpz_t bitmask;
switch (expr->etype) {
case EXPR_PAYLOAD:
shift = expr_offset_shift(expr, expr->payload.offset,
&extra_len);
break;
case EXPR_EXTHDR:
shift = expr_offset_shift(expr, expr->exthdr.tmpl->offset,
&extra_len);
break;
default:
BUG("Unknown expression %s\n", expr_name(expr));
}
masklen = len + shift;
assert(masklen <= NFT_REG_SIZE * BITS_PER_BYTE);
mpz_init2(bitmask, masklen);
mpz_bitmask(bitmask, len);
mpz_lshift_ui(bitmask, shift);
mask = constant_expr_alloc(&expr->location, expr_basetype(expr),
BYTEORDER_HOST_ENDIAN, masklen, NULL);
mpz_set(mask->value, bitmask);
mpz_clear(bitmask);
and = binop_expr_alloc(&expr->location, OP_AND, expr, mask);
and->dtype = expr->dtype;
and->byteorder = expr->byteorder;
and->len = masklen;
if (shift) {
off = constant_expr_alloc(&expr->location,
expr_basetype(expr),
BYTEORDER_HOST_ENDIAN,
sizeof(shift), &shift);
rshift = binop_expr_alloc(&expr->location, OP_RSHIFT, and, off);
rshift->dtype = expr->dtype;
rshift->byteorder = expr->byteorder;
rshift->len = masklen;
*exprp = rshift;
} else
*exprp = and;
if (extra_len)
expr->len += extra_len;
}
static int __expr_evaluate_exthdr(struct eval_ctx *ctx, struct expr **exprp)
{
struct expr *expr = *exprp;
if (expr->exthdr.flags & NFT_EXTHDR_F_PRESENT)
datatype_set(expr, &boolean_type);
if (expr_evaluate_primary(ctx, exprp) < 0)
return -1;
if (expr->exthdr.tmpl->offset % BITS_PER_BYTE != 0 ||
expr->len % BITS_PER_BYTE != 0)
expr_evaluate_bits(ctx, exprp);
switch (expr->exthdr.op) {
case NFT_EXTHDR_OP_TCPOPT: {
static const unsigned int max_tcpoptlen = (15 * 4 - 20) * BITS_PER_BYTE;
unsigned int totlen = 0;
totlen += expr->exthdr.tmpl->offset;
totlen += expr->exthdr.tmpl->len;
totlen += expr->exthdr.offset;
if (totlen > max_tcpoptlen)
return expr_error(ctx->msgs, expr,
"offset and size %u exceeds max tcp headerlen (%u)",
totlen, max_tcpoptlen);
break;
}
case NFT_EXTHDR_OP_IPV4: {
static const unsigned int max_ipoptlen = 40 * BITS_PER_BYTE;
unsigned int totlen = 0;
totlen += expr->exthdr.tmpl->offset;
totlen += expr->exthdr.tmpl->len;
totlen += expr->exthdr.offset;
if (totlen > max_ipoptlen)
return expr_error(ctx->msgs, expr,
"offset and size %u exceeds max ip option len (%u)",
totlen, max_ipoptlen);
break;
}
default:
break;
}
return 0;
}
/*
* Exthdr expression: check whether dependencies are fulfilled, otherwise
* generate the necessary relational expression and prepend it to the current
* statement.
*/
static int expr_evaluate_exthdr(struct eval_ctx *ctx, struct expr **exprp)
{
const struct proto_desc *base, *dependency = NULL;
enum proto_bases pb = PROTO_BASE_NETWORK_HDR;
struct expr *expr = *exprp;
struct stmt *nstmt;
switch (expr->exthdr.op) {
case NFT_EXTHDR_OP_TCPOPT:
dependency = &proto_tcp;
pb = PROTO_BASE_TRANSPORT_HDR;
break;
case NFT_EXTHDR_OP_IPV4:
dependency = &proto_ip;
break;
case NFT_EXTHDR_OP_IPV6:
default:
dependency = &proto_ip6;
break;
}
assert(dependency);
base = ctx->pctx.protocol[pb].desc;
if (base == dependency)
return __expr_evaluate_exthdr(ctx, exprp);
if (base)
return expr_error(ctx->msgs, expr,
"cannot use exthdr with %s", base->name);
if (exthdr_gen_dependency(ctx, expr, dependency, pb - 1, &nstmt) < 0)
return -1;
list_add(&nstmt->list, &ctx->rule->stmts);
return __expr_evaluate_exthdr(ctx, exprp);
}
/* dependency supersede.
*
* 'inet' is a 'phony' l2 dependency used by NFPROTO_INET to fulfill network
* header dependency, i.e. ensure that 'ip saddr 1.2.3.4' only sees ip headers.
*
* If a match expression that depends on a particular L2 header, e.g. ethernet,
* is used, we thus get a conflict since we already have a l2 header dependency.
*
* But in the inet case we can just ignore the conflict since only another
* restriction is added, and these are not mutually exclusive.
*
* Example: inet filter in ip saddr 1.2.3.4 ether saddr a:b:c:d:e:f
*
* ip saddr adds meta dependency on ipv4 packets
* ether saddr adds another dependeny on ethernet frames.
*/
static int meta_iiftype_gen_dependency(struct eval_ctx *ctx,
struct expr *payload, struct stmt **res)
{
struct stmt *nstmt;
uint16_t type;
if (proto_dev_type(payload->payload.desc, &type) < 0)
return expr_error(ctx->msgs, payload,
"protocol specification is invalid "
"for this family");
nstmt = meta_stmt_meta_iiftype(&payload->location, type);
if (stmt_evaluate(ctx, nstmt) < 0)
return expr_error(ctx->msgs, payload,
"dependency statement is invalid");
*res = nstmt;
return 0;
}
static bool proto_is_dummy(const struct proto_desc *desc)
{
return desc == &proto_inet || desc == &proto_netdev;
}
static int resolve_protocol_conflict(struct eval_ctx *ctx,
const struct proto_desc *desc,
struct expr *payload)
{
enum proto_bases base = payload->payload.base;
struct stmt *nstmt = NULL;
int link, err;
if (payload->payload.base == PROTO_BASE_LL_HDR &&
proto_is_dummy(desc)) {
err = meta_iiftype_gen_dependency(ctx, payload, &nstmt);
if (err < 0)
return err;
rule_stmt_insert_at(ctx->rule, nstmt, ctx->stmt);
}
assert(base <= PROTO_BASE_MAX);
/* This payload and the existing context don't match, conflict. */
if (ctx->pctx.protocol[base + 1].desc != NULL)
return 1;
link = proto_find_num(desc, payload->payload.desc);
if (link < 0 ||
conflict_resolution_gen_dependency(ctx, link, payload, &nstmt) < 0)
return 1;
payload->payload.offset += ctx->pctx.protocol[base].offset;
rule_stmt_insert_at(ctx->rule, nstmt, ctx->stmt);
return 0;
}
/*
* Payload expression: check whether dependencies are fulfilled, otherwise
* generate the necessary relational expression and prepend it to the current
* statement.
*/
static int __expr_evaluate_payload(struct eval_ctx *ctx, struct expr *expr)
{
struct expr *payload = expr;
enum proto_bases base = payload->payload.base;
const struct proto_desc *desc;
struct stmt *nstmt;
int err;
if (expr->etype == EXPR_PAYLOAD && expr->payload.is_raw)
return 0;
desc = ctx->pctx.protocol[base].desc;
if (desc == NULL) {
if (payload_gen_dependency(ctx, payload, &nstmt) < 0)
return -1;
rule_stmt_insert_at(ctx->rule, nstmt, ctx->stmt);
} else {
/* No conflict: Same payload protocol as context, adjust offset
* if needed.
*/
if (desc == payload->payload.desc) {
payload->payload.offset +=
ctx->pctx.protocol[base].offset;
return 0;
}
/* If we already have context and this payload is on the same
* base, try to resolve the protocol conflict.
*/
if (payload->payload.base == desc->base) {
err = resolve_protocol_conflict(ctx, desc, payload);
if (err <= 0)
return err;
desc = ctx->pctx.protocol[base].desc;
if (desc == payload->payload.desc)
return 0;
}
return expr_error(ctx->msgs, payload,
"conflicting protocols specified: %s vs. %s",
ctx->pctx.protocol[base].desc->name,
payload->payload.desc->name);
}
return 0;
}
static bool payload_needs_adjustment(const struct expr *expr)
{
return expr->payload.offset % BITS_PER_BYTE != 0 ||
expr->len % BITS_PER_BYTE != 0;
}
static int expr_evaluate_payload(struct eval_ctx *ctx, struct expr **exprp)
{
struct expr *expr = *exprp;
if (expr->payload.evaluated)
return 0;
if (__expr_evaluate_payload(ctx, expr) < 0)
return -1;
if (expr_evaluate_primary(ctx, exprp) < 0)
return -1;
if (payload_needs_adjustment(expr))
expr_evaluate_bits(ctx, exprp);
expr->payload.evaluated = true;
return 0;
}
/*
* RT expression: validate protocol dependencies.
*/
static int expr_evaluate_rt(struct eval_ctx *ctx, struct expr **expr)
{
static const char emsg[] = "cannot determine ip protocol version, use \"ip nexthop\" or \"ip6 nexthop\" instead";
struct expr *rt = *expr;
rt_expr_update_type(&ctx->pctx, rt);
switch (rt->rt.key) {
case NFT_RT_NEXTHOP4:
if (rt->dtype != &ipaddr_type)
return expr_error(ctx->msgs, rt, "%s", emsg);
if (ctx->pctx.family == NFPROTO_IPV6)
return expr_error(ctx->msgs, rt, "%s nexthop will not match", "ip");
break;
case NFT_RT_NEXTHOP6:
if (rt->dtype != &ip6addr_type)
return expr_error(ctx->msgs, rt, "%s", emsg);
if (ctx->pctx.family == NFPROTO_IPV4)
return expr_error(ctx->msgs, rt, "%s nexthop will not match", "ip6");
break;
default:
break;
}
return expr_evaluate_primary(ctx, expr);
}
static int ct_gen_nh_dependency(struct eval_ctx *ctx, struct expr *ct)
{
const struct proto_desc *base, *base_now;
struct expr *left, *right, *dep;
struct stmt *nstmt = NULL;
base_now = ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc;
switch (ct->ct.nfproto) {
case NFPROTO_IPV4:
base = &proto_ip;
break;
case NFPROTO_IPV6:
base = &proto_ip6;
break;
default:
base = ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc;
if (base == &proto_ip)
ct->ct.nfproto = NFPROTO_IPV4;
else if (base == &proto_ip)
ct->ct.nfproto = NFPROTO_IPV6;
if (base)
break;
return expr_error(ctx->msgs, ct,
"cannot determine ip protocol version, use \"ip %1$caddr\" or \"ip6 %1$caddr\" instead",
ct->ct.key == NFT_CT_SRC ? 's' : 'd');
}
/* no additional dependency needed? */
if (base == base_now)
return 0;
if (base_now && base_now != base)
return expr_error(ctx->msgs, ct,
"conflicting dependencies: %s vs. %s\n",
base->name,
ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc->name);
switch (ctx->pctx.family) {
case NFPROTO_IPV4:
case NFPROTO_IPV6:
return 0;
}
left = ct_expr_alloc(&ct->location, NFT_CT_L3PROTOCOL, ct->ct.direction);
right = constant_expr_alloc(&ct->location, left->dtype,
left->dtype->byteorder, left->len,
constant_data_ptr(ct->ct.nfproto, left->len));
dep = relational_expr_alloc(&ct->location, OP_EQ, left, right);
relational_expr_pctx_update(&ctx->pctx, dep);
nstmt = expr_stmt_alloc(&dep->location, dep);
rule_stmt_insert_at(ctx->rule, nstmt, ctx->stmt);
return 0;
}
/*
* CT expression: update the protocol dependant types bases on the protocol
* context.
*/
static int expr_evaluate_ct(struct eval_ctx *ctx, struct expr **expr)
{
const struct proto_desc *base, *error;
struct expr *ct = *expr;
base = ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR].desc;
switch (ct->ct.key) {
case NFT_CT_SRC:
case NFT_CT_DST:
ct_gen_nh_dependency(ctx, ct);
break;
case NFT_CT_SRC_IP:
case NFT_CT_DST_IP:
if (base == &proto_ip6) {
error = &proto_ip;
goto err_conflict;
}
break;
case NFT_CT_SRC_IP6:
case NFT_CT_DST_IP6:
if (base == &proto_ip) {
error = &proto_ip6;
goto err_conflict;
}
break;
default:
break;
}
ct_expr_update_type(&ctx->pctx, ct);
return expr_evaluate_primary(ctx, expr);
err_conflict:
return stmt_binary_error(ctx, ct,
&ctx->pctx.protocol[PROTO_BASE_NETWORK_HDR],
"conflicting protocols specified: %s vs. %s",
base->name, error->name);
}
/*
* Prefix expression: the argument must be a constant value of integer or
* string base type; the prefix length must be less than or equal to the type
* width.
*/
static int expr_evaluate_prefix(struct eval_ctx *ctx, struct expr **expr)
{
struct expr *prefix = *expr, *base, *and, *mask;
if (expr_evaluate(ctx, &prefix->prefix) < 0)
return -1;
base = prefix->prefix;
if (!expr_is_constant(base))
return expr_error(ctx->msgs, prefix,
"Prefix expression is undefined for "
"non-constant expressions");
switch (expr_basetype(base)->type) {
case TYPE_INTEGER:
case TYPE_STRING:
break;
default:
return expr_error(ctx->msgs, prefix,
"Prefix expression is undefined for "
"%s types", base->dtype->desc);
}
if (prefix->prefix_len > base->len)
return expr_error(ctx->msgs, prefix,
"Prefix length %u is invalid for type "
"of %u bits width",
prefix->prefix_len, base->len);
/* Clear the uncovered bits of the base value */
mask = constant_expr_alloc(&prefix->location, expr_basetype(base),
BYTEORDER_HOST_ENDIAN, base->len, NULL);
switch (expr_basetype(base)->type) {
case TYPE_INTEGER:
mpz_prefixmask(mask->value, base->len, prefix->prefix_len);
break;
case TYPE_STRING:
mpz_init2(mask->value, base->len);
mpz_bitmask(mask->value, prefix->prefix_len);
break;
}
and = binop_expr_alloc(&prefix->location, OP_AND, base, mask);
prefix->prefix = and;
if (expr_evaluate(ctx, &prefix->prefix) < 0)
return -1;
base = prefix->prefix;
assert(expr_is_constant(base));
prefix->dtype = base->dtype;
prefix->byteorder = base->byteorder;
prefix->len = base->len;
prefix->flags |= EXPR_F_CONSTANT;
return 0;
}
/*
* Range expression: both sides must be constants of integer base type.
*/
static int expr_evaluate_range_expr(struct eval_ctx *ctx,
const struct expr *range,
struct expr **expr)
{
if (expr_evaluate(ctx, expr) < 0)
return -1;
if (expr_basetype(*expr)->type != TYPE_INTEGER)
return expr_binary_error(ctx->msgs, *expr, range,
"Range expression is undefined for "
"%s types", (*expr)->dtype->desc);
if (!expr_is_constant(*expr))
return expr_binary_error(ctx->msgs, *expr, range,
"Range is not constant");
return 0;
}
static int __expr_evaluate_range(struct eval_ctx *ctx, struct expr **expr)
{
struct expr *range = *expr;
if (expr_evaluate_range_expr(ctx, range, &range->left) < 0)
return -1;
if (expr_evaluate_range_expr(ctx, range, &range->right) < 0)
return -1;
return 0;
}
static int expr_evaluate_range(struct eval_ctx *ctx, struct expr **expr)
{
struct expr *range = *expr, *left, *right;
int rc;
rc = __expr_evaluate_range(ctx, expr);
if (rc)
return rc;