fib: Fix for existence check on Big Endian
Phil Sutter
reviewerPATCH 1/1 fib: Fix for existence check on Big Endian
Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x00000001 ]
Though with NFTA_FIB_F_PRESENT flag set, nft_fib.ko writes to the first byte of reg 1 only (using nft_reg_store8()). With this patch in place, byte code is correct:
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x01000000 ]
--- src/evaluate.c | 1 + src/fib.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)
src/evaluate.c +1 -0
@@ -3002,6 +3002,7 @@ static int expr_evaluate_fib(struct eval_ctx *ctx, struct expr **exprp)if (expr->flags &EXPR_F_BOOLEAN ) { expr->fib.flags |=NFTA_FIB_F_PRESENT ;datatype_set (expr, &boolean_type); + expr->len =BITS_PER_BYTE ; }return expr_evaluate_primary (ctx, exprp); }
src/fib.c +3 -1
@@ -198,8 +198,10 @@ struct expr *fib_expr_alloc(const struct location *loc,BUG ("Unknown result %d\n" , result); } -if (flags &NFTA_FIB_F_PRESENT ) +if (flags &NFTA_FIB_F_PRESENT ) { type = &boolean_type; + len =BITS_PER_BYTE ; + } expr =expr_alloc (loc,EXPR_FIB , type,BYTEORDER_HOST_ENDIAN , len);
Signature · 1 line
2.51.0
Pablo Neira Ayuso
maintainerHi Phil,
Quoted 7 lines · Phil Sutter
Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x00000001 ]
Quoted 6 lines
Though with
NFTA_FIB_F_PRESENTflag set, nft_fib.ko writes to the first byte of reg 1 only (usingnft_reg_store8()). With this patch in place, byte code is correct:| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x01000000 ]
Is this a generic issue of boolean that is using 1 bit?
const struct datatype boolean_type = {
.type = TYPE_BOOLEAN,
.name = "boolean",
.desc = "boolean type",
.size = 1,Quoted 39 lines
Fixes f686a17eafa0b ("fib: Support existence check")Cc Yi Chen <[email protected]>Signed-off-by Phil Sutter <[email protected]>--- src/evaluate.c | 1 + src/fib.c | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-)src/evaluate.c +1
-0@@ -3002,6 +3002,7 @@ static int expr_evaluate_fib(struct eval_ctx *ctx, struct expr **exprp)if (expr->flags &EXPR_F_BOOLEAN ) { expr->fib.flags |=NFTA_FIB_F_PRESENT ;datatype_set (expr, &boolean_type); + expr->len =BITS_PER_BYTE ; }return expr_evaluate_primary (ctx, exprp); }src/fib.c +3
-1@@ -198,8 +198,10 @@ struct expr *fib_expr_alloc(const struct location *loc,BUG ("Unknown result %d\n" , result); } -if (flags &NFTA_FIB_F_PRESENT ) +if (flags &NFTA_FIB_F_PRESENT ) { type = &boolean_type; + len =BITS_PER_BYTE ; + } expr =expr_alloc (loc,EXPR_FIB , type,BYTEORDER_HOST_ENDIAN , len);Signature · 1 line
2.51.0
Phil Sutter
reviewerHi Pablo,
Quoted 15 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter
Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x00000001 ]Quoted 6 lines
Though with
NFTA_FIB_F_PRESENTflag set, nft_fib.ko writes to the first byte of reg 1 only (usingnft_reg_store8()). With this patch in place, byte code is correct:| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x01000000 ]
Quoted 7 lines
Is this a generic issue of boolean that is using 1 bit?
const struct datatype
boolean_type= {.type = TYPE_BOOLEAN, .name = "boolean", .desc = "boolean type", .size = 1,
Maybe, yes: I compared fib existence checks to exthdr ones in order to find the bug. With exthdr, we know in parser already that it is an existence check (see exthdr_exists_expr rule in parser_bison.y). If so, exthdr expression is allocated with type 1 which is (assumed to be) the NEXTHDR field in all extension headers. This field has inet_protocol_type, which is size 8b.
Via expr_ctx::len, RHS will then be adjusted to 8b size (see 'expr->len = masklen' in expr_evaluate_integer()).
IIRC, LHS defines the RHS size in relationals. I am not sure if we may sanely reverse this rule if RHS is a boolean_type.
Cheers, Phil
Pablo Neira Ayuso
maintainerQuoted 33 lines · Phil Sutter
Hi Pablo,
Quoted 15 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter
Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x00000001 ]Quoted 6 lines
Though with
NFTA_FIB_F_PRESENTflag set, nft_fib.ko writes to the first byte of reg 1 only (usingnft_reg_store8()). With this patch in place, byte code is correct:| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x01000000 ]Quoted 7 lines
Is this a generic issue of boolean that is using 1 bit?
const struct datatype
boolean_type= {.type = TYPE_BOOLEAN, .name = "boolean", .desc = "boolean type", .size = 1,Maybe, yes: I compared fib existence checks to exthdr ones in order to find the bug. With exthdr, we know in parser already that it is an existence check (see
exthdr_exists_exprrule in parser_bison.y). If so, exthdr expression is allocated with type 1 which is (assumed to be) the NEXTHDR field in all extension headers. This field hasinet_protocol_type, which is size 8b.
Quoted 5 lines
Via
expr_ctx::len, RHS will then be adjusted to 8b size (see 'expr->len= masklen' inexpr_evaluate_integer()).IIRC, LHS defines the RHS size in relationals. I am not sure if we may sanely reverse this rule if RHS is a
boolean_type.
Probably this fix is more generic, see untested patch.
src/datatype.c +1 -1
@@ -1581,7 +1581,7 @@ const struct datatype boolean_type = { .type = TYPE_BOOLEAN, .name = "boolean", .desc = "boolean type", - .size = 1, + .size = BITS_PER_BYTE, .parse = boolean_type_parse, .basetype = &integer_type, .sym_tbl = &boolean_tbl,
Phil Sutter
reviewerQuoted 47 lines · Pablo Neira Ayuso
Quoted 33 lines · Phil Sutter
Hi Pablo,
Quoted 15 lines · Pablo Neira Ayuso
Quoted 7 lines · Phil Sutter
Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x00000001 ]Quoted 6 lines
Though with
NFTA_FIB_F_PRESENTflag set, nft_fib.ko writes to the first byte of reg 1 only (usingnft_reg_store8()). With this patch in place, byte code is correct:| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x01000000 ]Quoted 7 lines
Is this a generic issue of boolean that is using 1 bit?
const struct datatype
boolean_type= {.type = TYPE_BOOLEAN, .name = "boolean", .desc = "boolean type", .size = 1,Maybe, yes: I compared fib existence checks to exthdr ones in order to find the bug. With exthdr, we know in parser already that it is an existence check (see
exthdr_exists_exprrule in parser_bison.y). If so, exthdr expression is allocated with type 1 which is (assumed to be) the NEXTHDR field in all extension headers. This field hasinet_protocol_type, which is size 8b.Quoted 5 lines
Via
expr_ctx::len, RHS will then be adjusted to 8b size (see 'expr->len= masklen' inexpr_evaluate_integer()).IIRC, LHS defines the RHS size in relationals. I am not sure if we may sanely reverse this rule if RHS is a
boolean_type.Probably this fix is more generic, see untested patch.
src/datatype.c +0
-0
src/datatype.c const struct datatype boolean_type = { · line 1587
Quoted 8 lines
src/datatype.c +1
-1@@ -1581,7 +1581,7 @@ const struct datatype boolean_type = { .type = TYPE_BOOLEAN, .name = "boolean", .desc = "boolean type", - .size = 1, + .size = BITS_PER_BYTE, .parse = boolean_type_parse, .basetype = &integer_type, .sym_tbl = &boolean_tbl,
This does not make a difference. Since the fib expr::len remains at value 32, the cmp payload will be 32 bits as well with the first three bytes being zero on BE irrespective of RHS value.
Cheers, Phil
Quoted 11 lines · Phil Sutter
Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x00000001 ]Though with
NFTA_FIB_F_PRESENTflag set, nft_fib.ko writes to the first byte of reg 1 only (usingnft_reg_store8()). With this patch in place, byte code is correct:
Quoted 6 lines
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x01000000 ]Fixes f686a17eafa0b ("fib: Support existence check")Cc Yi Chen <[email protected]>Signed-off-by Phil Sutter <[email protected]>
Thanks.
Phil Sutter
reviewerQuoted 12 lines · Pablo Neira Ayuso
Quoted 11 lines · Phil Sutter
Adjust the expression size to 1B so cmp expression value is correct. Without this, the rule 'fib saddr . iif check exists' generates following byte code on BE:
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x00000001 ]Though with
NFTA_FIB_F_PRESENTflag set, nft_fib.ko writes to the first byte of reg 1 only (usingnft_reg_store8()). With this patch in place, byte code is correct:
Quoted 8 lines
Quoted 6 lines
| [ fib saddr . iif oif present => reg 1 ] | [ cmp eq reg 1 0x01000000 ]Fixes f686a17eafa0b ("fib: Support existence check")Cc Yi Chen <[email protected]>Signed-off-by Phil Sutter <[email protected]>Reviewed-by Pablo Neira Ayuso <[email protected]>
Patch applied, thanks!