Introduce boolean type and existence checks
Phil Sutter
reviewercover Introduce boolean type and existence checks
The following patch series implements a boolean datatype for use in a boolean expression as right-hand side of a relational. Based upon that, header existence matches are implemented for IPv6 extension and TCP option headers as well as an existence match of a FIB entry.
Phil Sutter (5):
Introduce boolean datatype and boolean expression
exthdr: Add support for exthdr specific flags
exthdr: Implement existence check
fib: Support existence check
doc: Document boolean type and applications
doc/nft.xml | 134 ++++++++++++++++++++++++++++++++++++ include/datatype.h | 2 + include/expression.h | 3 + include/exthdr.h | 4 +- include/linux/netfilter/nf_tables.h | 1 + include/tcpopt.h | 3 +- src/datatype.c | 19 +++++ src/evaluate.c | 17 ++++- src/expression.c | 4 ++ src/exthdr.c | 39 ++++++++--- src/fib.c | 5 +- src/netlink_delinearize.c | 5 +- src/netlink_linearize.c | 1 + src/parser_bison.y | 54 +++++++++++++++ src/scanner.l | 5 ++ src/tcpopt.c | 11 ++- tests/py/inet/fib.t | 3 + tests/py/inet/fib.t.payload | 10 +++ tests/py/inet/tcpopt.t | 3 + tests/py/inet/tcpopt.t.payload.inet | 14 ++++ tests/py/ip6/exthdr.t | 19 +++++ tests/py/ip6/exthdr.t.payload.ip6 | 60 ++++++++++++++++ 22 files changed, 399 insertions(+), 17 deletions(-) create mode 100644 tests/py/ip6/exthdr.t create mode 100644 tests/py/ip6/exthdr.t.payload.ip6
Signature · 1 line
2.11.0
Phil Sutter
reviewerPATCH 1/5 Introduce boolean datatype and boolean expression
--- include/datatype.h | 2 ++ src/datatype.c | 19 +++++++++++++++++++ src/parser_bison.y | 20 ++++++++++++++++++++ src/scanner.l | 3 +++ 4 files changed, 44 insertions(+)
include/datatype.h +2 -0
@@ -82,6 +82,7 @@ enum datatypes {TYPE_DSCP ,TYPE_ECN ,TYPE_FIB_ADDR , +TYPE_BOOLEAN , __TYPE_MAX };#define TYPE_MAX (__TYPE_MAX - 1)
@@ -233,6 +234,7 @@ extern const struct datatype icmp_code_type;extern const struct datatype icmpv6_code_type;extern const struct datatype icmpx_code_type;extern const struct datatype time_type; +extern const struct datatype boolean_type;extern const struct datatype *concat_type_alloc (uint32_t type);extern void concat_type_destroy (const struct datatype *dtype);
src/datatype.c +19 -0
@@ -48,6 +48,7 @@ static const struct datatype *datatypes[TYPE_MAX + 1] = { [TYPE_ICMP_CODE] = &icmp_code_type, [TYPE_ICMPV6_CODE] = &icmpv6_code_type, [TYPE_ICMPX_CODE] = &icmpx_code_type, + [TYPE_BOOLEAN] = &boolean_type, };void datatype_register (const struct datatype *dtype)
@@ -1104,3 +1105,21 @@ struct error_record *rate_parse(const struct location *loc, const char *str,return NULL ; } + +static const struct symbol_table boolean_tbl = { + .base =BASE_DECIMAL , + .symbols = { +SYMBOL ("exists" , true), +SYMBOL ("missing" , false), +SYMBOL_LIST_END + }, +}; + +const struct datatype boolean_type = { + .type =TYPE_BOOLEAN , + .name ="boolean" , + .desc ="boolean type" , + .size =1 , + .basetype = &integer_type, + .sym_tbl = &boolean_tbl, +};
src/parser_bison.y +20 -0
@@ -448,6 +448,9 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token NOTRACK "notrack" +%token EXISTS "exists" +%token MISSING "missing" + %type <string> identifier type_identifier string comment_spec %destructor { xfree($$); } identifier type_identifier string comment_spec
@@ -651,6 +654,10 @@ static void location_update(struct location *loc, struct location *rhs, int n) %type <val> tcp_hdr_field %type <val> tcp_hdr_option_type tcp_hdr_option_field +%type <expr> boolean_expr +%destructor { expr_free($$); } boolean_expr +%type <val> boolean_keys + %% input : /* empty */
@@ -2655,8 +2662,21 @@ concat_rhs_expr : basic_rhs_expr } ; +boolean_keys : EXISTS { $$ = true; } + | MISSING { $$ = false; } + ; + +boolean_expr : boolean_keys + { + $$ = constant_expr_alloc(&@$, &boolean_type, + BYTEORDER_HOST_ENDIAN, + 1, &$1); + } + ; + primary_rhs_expr : symbol_expr { $$ = $1; } | integer_expr { $$ = $1; } + | boolean_expr { $$ = $1; } | ETHER { $$ = symbol_expr_alloc(&@$, SYMBOL_VALUE,
src/scanner.l +3 -0
@@ -504,6 +504,9 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "xml" { return XML; } "json" { return JSON; } +"exists" { return EXISTS; } +"missing" { return MISSING; } + {addrstring} { yylval->string = xstrdup(yytext); return STRING;
Signature · 1 line
2.11.0
Phil Sutter
reviewerPATCH 2/5 exthdr: Add support for exthdr specific flags
In reply to Phil SutterThis allows to have custom flags in exthdr expression, which is necessary for upcoming existence checks (of both IPv6 extension headers as well as TCP options).
--- include/expression.h | 1 + include/exthdr.h | 2 +- include/tcpopt.h | 3 ++- src/exthdr.c | 11 +++++++---- src/netlink_delinearize.c | 5 +++-- src/netlink_linearize.c | 1 + src/tcpopt.c | 5 +++-- 7 files changed, 18 insertions(+), 10 deletions(-)
include/expression.h +1 -0
@@ -282,6 +282,7 @@ struct expr {const struct proto_hdr_template *tmpl;unsigned int offset;enum nft_exthdr_op op; +unsigned int flags; } exthdr;struct {/* EXPR_META */
include/exthdr.h +1 -1
@@ -23,7 +23,7 @@ extern struct expr *exthdr_expr_alloc(const struct location *loc,extern void exthdr_init_raw (struct expr *expr,uint8_t type,unsigned int offset,unsigned int len, -enum nft_exthdr_op op); +enum nft_exthdr_op op,uint32_t flags);extern bool exthdr_find_template (struct expr *expr,const struct expr *mask,unsigned int *shift);
include/tcpopt.h +2 -1
@@ -8,7 +8,8 @@ extern struct expr *tcpopt_expr_alloc(const struct location *loc, uint8_t type, uint8_t field);extern void tcpopt_init_raw (struct expr *expr,uint8_t type, -unsigned int offset,unsigned int len); +unsigned int offset,unsigned int len, +uint32_t flags);extern bool tcpopt_find_template (struct expr *expr,const struct expr *mask,unsigned int *shift);
src/exthdr.c +7 -4
@@ -46,7 +46,8 @@ static bool exthdr_expr_cmp(const struct expr *e1, const struct expr *e2) {return e1->exthdr.desc == e2->exthdr.desc && e1->exthdr.tmpl == e2->exthdr.tmpl && - e1->exthdr.op == e2->exthdr.op; + e1->exthdr.op == e2->exthdr.op && + e1->exthdr.flags == e2->exthdr.flags; }static void exthdr_expr_clone (struct expr *new,const struct expr *expr)
@@ -55,6 +56,7 @@ static void exthdr_expr_clone(struct expr *new, const struct expr *expr) new->exthdr.tmpl = expr->exthdr.tmpl; new->exthdr.offset = expr->exthdr.offset; new->exthdr.op = expr->exthdr.op; + new->exthdr.flags = expr->exthdr.flags; }const struct expr_ops exthdr_expr_ops = {
@@ -97,16 +99,17 @@ static const struct exthdr_desc *exthdr_protocols[IPPROTO_MAX] = {void exthdr_init_raw (struct expr *expr,uint8_t type,unsigned int offset,unsigned int len, -enum nft_exthdr_op op) +enum nft_exthdr_op op,uint32_t flags) {const struct proto_hdr_template *tmpl;unsigned int i;assert (expr->ops->type ==EXPR_EXTHDR );if (op ==NFT_EXTHDR_OP_TCPOPT ) -return tcpopt_init_raw (expr, type, offset, len); +return tcpopt_init_raw (expr, type, offset, len, flags); expr->len = len; + expr->exthdr.flags = flags; expr->exthdr.offset = offset; expr->exthdr.desc = exthdr_protocols[type];assert (expr->exthdr.desc !=NULL );
@@ -149,7 +152,7 @@ bool exthdr_find_template(struct expr *expr, const struct expr *mask, unsigned i off +=round_up (mask->len,BITS_PER_BYTE ) - mask_len;exthdr_init_raw (expr, expr->exthdr.desc->type, - off, mask_len - mask_offset,NFT_EXTHDR_OP_IPV6 ); + off, mask_len - mask_offset,NFT_EXTHDR_OP_IPV6 ,0 );/* still failed to find a template... Bug. */ if (expr->exthdr.tmpl == &exthdr_unknown_template)
src/netlink_delinearize.c +3 -2
@@ -498,9 +498,9 @@ static void netlink_parse_exthdr(struct netlink_parse_ctx *ctx,const struct location *loc, const struct nftnl_expr *nle) { + uint32_t offset, len, flags;enum nft_registers dreg;enum nft_exthdr_op op; -uint32_t offset, len;uint8_t type;struct expr *expr;
@@ -508,9 +508,10 @@ static void netlink_parse_exthdr(struct netlink_parse_ctx *ctx, offset =nftnl_expr_get_u32 (nle,NFTNL_EXPR_EXTHDR_OFFSET ) *BITS_PER_BYTE ; len =nftnl_expr_get_u32 (nle,NFTNL_EXPR_EXTHDR_LEN ) *BITS_PER_BYTE ; op =nftnl_expr_get_u32 (nle,NFTNL_EXPR_EXTHDR_OP ); + flags =nftnl_expr_get_u32 (nle,NFTNL_EXPR_EXTHDR_FLAGS ); expr =exthdr_expr_alloc (loc,NULL ,0 ); -exthdr_init_raw (expr, type, offset, len, op); +exthdr_init_raw (expr, type, offset, len, op, flags); dreg =netlink_parse_register (nle,NFTNL_EXPR_EXTHDR_DREG );netlink_set_register (ctx, dreg, expr);
src/netlink_linearize.c +1 -0
@@ -178,6 +178,7 @@ static void netlink_gen_exthdr(struct netlink_linearize_ctx *ctx,nftnl_expr_set_u32 (nle,NFTNL_EXPR_EXTHDR_LEN ,div_round_up (expr->len,BITS_PER_BYTE ));nftnl_expr_set_u8 (nle,NFTNL_EXPR_EXTHDR_OP , expr->exthdr.op); +nftnl_expr_set_u32 (nle,NFTNL_EXPR_EXTHDR_FLAGS , expr->exthdr.flags);nftnl_rule_add_expr (ctx->nlr, nle); }
src/tcpopt.c +3 -2
@@ -192,7 +192,7 @@ struct expr *tcpopt_expr_alloc(const struct location *loc, uint8_t type, }void tcpopt_init_raw (struct expr *expr,uint8_t type,unsigned int offset, -unsigned int len) +unsigned int len,uint32_t flags) {const struct proto_hdr_template *tmpl;unsigned int i, off;
@@ -200,6 +200,7 @@ void tcpopt_init_raw(struct expr *expr, uint8_t type, unsigned int offset,assert (expr->ops->type ==EXPR_EXTHDR ); expr->len = len; + expr->exthdr.flags = flags; expr->exthdr.offset = offset;assert (type <array_size (tcpopt_protocols));
@@ -229,7 +230,7 @@ bool tcpopt_find_template(struct expr *expr, const struct expr *mask,return false;tcpopt_init_raw (expr, expr->exthdr.desc->type, expr->exthdr.offset, - expr->len); + expr->len,0 );if (expr->exthdr.tmpl == &tcpopt_unknown_template)return false;
Signature · 1 line
2.11.0
Phil Sutter
reviewerPATCH 3/5 exthdr: Implement existence check
In reply to Phil SutterThis allows to check for existence of an IPv6 extension or TCP option header by using the following syntax:
| exthdr frag exists
| tcpopt window exists
--- include/exthdr.h | 2 ++ src/evaluate.c | 3 ++ src/exthdr.c | 28 ++++++++++++++--- src/parser_bison.y | 34 +++++++++++++++++++++ src/scanner.l | 2 ++ src/tcpopt.c | 6 +++- tests/py/inet/tcpopt.t | 3 ++ tests/py/inet/tcpopt.t.payload.inet | 14 +++++++++ tests/py/ip6/exthdr.t | 19 ++++++++++++ tests/py/ip6/exthdr.t.payload.ip6 | 60 +++++++++++++++++++++++++++++++++++++ 10 files changed, 166 insertions(+), 5 deletions(-) create mode 100644 tests/py/ip6/exthdr.t create mode 100644 tests/py/ip6/exthdr.t.payload.ip6
include/exthdr.h +2 -0
@@ -21,6 +21,8 @@ extern struct expr *exthdr_expr_alloc(const struct location *loc,const struct exthdr_desc *desc, uint8_t type); +extern const struct exthdr_desc *exthdr_find_proto (uint8_t proto); +extern void exthdr_init_raw (struct expr *expr,uint8_t type,unsigned int offset,unsigned int len,enum nft_exthdr_op op,uint32_t flags);
src/evaluate.c +3 -0
@@ -432,6 +432,9 @@ static int __expr_evaluate_exthdr(struct eval_ctx *ctx, struct expr **exprp) {struct expr *expr = *exprp; +if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT ) + expr->dtype = &boolean_type; +if (expr_evaluate_primary (ctx, exprp) <0 )return -1 ;
src/exthdr.c +24 -4
@@ -32,14 +32,22 @@ static void exthdr_expr_print(const struct expr *expr)unsigned int offset = expr->exthdr.offset /64 ;char buf[3 ] = {0 }; +if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT ) { +printf ("tcp option %s" , expr->exthdr.desc->name); +return ; + } +if (offset)snprintf (buf,sizeof buf,"%d" , offset);printf ("tcp option %s%s %s" , expr->exthdr.desc->name, buf, expr->exthdr.tmpl->token); + }else { +if (expr->exthdr.flags &NFT_EXTHDR_F_PRESENT ) +printf ("exthdr %s" , expr->exthdr.desc->name); +else +printf ("%s %s" , expr->exthdr.desc->name, + expr->exthdr.tmpl->token); } -else -printf ("%s %s" , expr->exthdr.desc->name, - expr->exthdr.tmpl->token); }static bool exthdr_expr_cmp (const struct expr *e1,const struct expr *e2)
@@ -97,6 +105,13 @@ static const struct exthdr_desc *exthdr_protocols[IPPROTO_MAX] = { [IPPROTO_MH] = &exthdr_mh, }; +const struct exthdr_desc *exthdr_find_proto (uint8_t proto) +{ +assert (exthdr_protocols[proto]); + +return exthdr_protocols[proto]; +} +void exthdr_init_raw (struct expr *expr,uint8_t type,unsigned int offset,unsigned int len,enum nft_exthdr_op op,uint32_t flags)
@@ -119,7 +134,12 @@ void exthdr_init_raw(struct expr *expr, uint8_t type,if (tmpl->offset != offset || tmpl->len != len)continue ; - expr->dtype = tmpl->dtype; + +if (flags &NFT_EXTHDR_F_PRESENT ) + expr->dtype = &boolean_type; +else + expr->dtype = tmpl->dtype; + expr->exthdr.tmpl = tmpl;return ; }
src/parser_bison.y +34 -0
@@ -139,6 +139,7 @@ static void location_update(struct location *loc, struct location *rhs, int n) const struct datatype *datatype; struct handle_spec handle_spec; struct position_spec position_spec; + const struct exthdr_desc *exthdr_desc; } %token TOKEN_EOF 0 "end of file"
@@ -451,6 +452,8 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token EXISTS "exists" %token MISSING "missing" +%token EXTHDR "exthdr" + %type <string> identifier type_identifier string comment_spec %destructor { xfree($$); } identifier type_identifier string comment_spec
@@ -658,6 +661,10 @@ static void location_update(struct location *loc, struct location *rhs, int n) %destructor { expr_free($$); } boolean_expr %type <val> boolean_keys +%type <expr> exthdr_exists_expr +%destructor { expr_free($$); } exthdr_exists_expr +%type <val> exthdr_key + %% input : /* empty */
@@ -2291,6 +2298,7 @@ primary_expr : symbol_expr { $$ = $1; } | integer_expr { $$ = $1; } | payload_expr { $$ = $1; } | exthdr_expr { $$ = $1; } + | exthdr_exists_expr { $$ = $1; } | meta_expr { $$ = $1; } | rt_expr { $$ = $1; } | ct_expr { $$ = $1; }
@@ -3254,6 +3262,11 @@ tcp_hdr_expr : TCP tcp_hdr_field { $$ = tcpopt_expr_alloc(&@$, $3, $4); } + | TCP OPTION tcp_hdr_option_type + { + $$ = tcpopt_expr_alloc(&@$, $3, TCPOPTHDR_FIELD_KIND); + $$->exthdr.flags = NFT_EXTHDR_F_PRESENT; + } ; tcp_hdr_field : SPORT { $$ = TCPHDR_SPORT; }
@@ -3404,4 +3417,25 @@ mh_hdr_field : NEXTHDR { $$ = MHHDR_NEXTHDR; } | CHECKSUM { $$ = MHHDR_CHECKSUM; } ; +exthdr_exists_expr : EXTHDR exthdr_key + { + const struct exthdr_desc *desc; + + desc = exthdr_find_proto($2); + + /* Assume that NEXTHDR template is always + * the fist one in list of templates. + */ + $$ = exthdr_expr_alloc(&@$, desc, 1); + $$->exthdr.flags = NFT_EXTHDR_F_PRESENT; + } + ; + +exthdr_key : HBH { $$ = IPPROTO_HOPOPTS; } + | RT { $$ = IPPROTO_ROUTING; } + | FRAG { $$ = IPPROTO_FRAGMENT; } + | DST { $$ = IPPROTO_DSTOPTS; } + | MH { $$ = IPPROTO_MH; } + ; + %%
src/scanner.l +2 -0
@@ -507,6 +507,8 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) "exists" { return EXISTS; } "missing" { return MISSING; } +"exthdr" { return EXTHDR; } + {addrstring} { yylval->string = xstrdup(yytext); return STRING;
src/tcpopt.c +5 -1
@@ -205,6 +205,7 @@ void tcpopt_init_raw(struct expr *expr, uint8_t type, unsigned int offset,assert (type <array_size (tcpopt_protocols)); expr->exthdr.desc = tcpopt_protocols[type]; + expr->exthdr.flags = flags;assert (expr->exthdr.desc !=TCPOPT_OBSOLETE );for (i =0 ; i <array_size (expr->exthdr.desc->templates); ++i) {
@@ -216,7 +217,10 @@ void tcpopt_init_raw(struct expr *expr, uint8_t type, unsigned int offset,if (tmpl->offset != off || tmpl->len != len)continue ; - expr->dtype = tmpl->dtype; +if (flags &NFT_EXTHDR_F_PRESENT ) + expr->dtype = &boolean_type; +else + expr->dtype = tmpl->dtype; expr->exthdr.tmpl = tmpl; expr->exthdr.op =NFT_EXTHDR_OP_TCPOPT ;break ;
tests/py/inet/tcpopt.t +3 -0
@@ -35,3 +35,6 @@ tcp option eol left 1;fail tcp option eol left 1;fail tcp option sack window;fail tcp option sack window 1;fail + +tcp option window exists;ok +tcp option window missing;ok
tests/py/inet/tcpopt.t.payload.inet +14 -0
@@ -179,3 +179,17 @@ inet test-inet input [ cmp eq reg 1 0x00000006 ] [ exthdr load tcpopt 4b @ 8 + 6 => reg 1 ] [ cmp eq reg 1 0x01000000 ] + +# tcp option window exists +inet test-inet input + [ meta load l4proto => reg 1 ] + [ cmp eq reg 1 0x00000006 ] + [ exthdr load tcpopt 1b @ 3 + 0 => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + +# tcp option window missing +inet test-inet input + [ meta load l4proto => reg 1 ] + [ cmp eq reg 1 0x00000006 ] + [ exthdr load tcpopt 1b @ 3 + 0 => reg 1 ] + [ cmp eq reg 1 0x00000000 ]
tests/py/ip6/exthdr.t new file +19 -0
@@ -0,0 +1,19 @@ +:input;type filter hook input priority 0 + +*ip6;test-ip6;input + +exthdr hbh exists;ok +exthdr rt exists;ok +exthdr frag exists;ok +exthdr dst exists;ok +exthdr mh exists;ok + +exthdr hbh missing;ok + +exthdr hbh == exists;ok;exthdr hbh exists +exthdr hbh == missing;ok;exthdr hbh missing +exthdr hbh != exists;ok +exthdr hbh != missing;ok + +exthdr hbh 1;ok;exthdr hbh exists +exthdr hbh 0;ok;exthdr hbh missing
tests/py/ip6/exthdr.t.payload.ip6 new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.11.0
Phil Sutter
reviewerPATCH 4/5 fib: Support existence check
In reply to Phil SutterThis allows to check whether a FIB entry exists for a given packet by comparing the expression with a boolean keyword like so:
| fib daddr oif exists
The implementation requires introduction of a generic expression flag EXPR_F_BOOLEAN which allows relational expression to signal it's LHS that a boolean comparison is being done (indicated by boolean type on RHS). In contrast to exthdr existence checks, fib expression can't know this in beforehand because the LHS syntax is absolutely identical to a non-boolean comparison.
--- include/expression.h | 2 ++ include/linux/netfilter/nf_tables.h | 1 + src/evaluate.c | 14 +++++++++++++- src/expression.c | 4 ++++ src/fib.c | 5 ++++- tests/py/inet/fib.t | 3 +++ tests/py/inet/fib.t.payload | 10 ++++++++++ 7 files changed, 37 insertions(+), 2 deletions(-)
include/expression.h +2 -0
@@ -171,12 +171,14 @@ struct expr_ops {* @EXPR_F_SINGLETON: singleton (implies primary and constant) * @EXPR_F_PROTOCOL: expressions describes upper layer protocol * @EXPR_F_INTERVAL_END: set member ends an open interval +* @EXPR_F_BOOLEAN: expression is boolean (set by relational expr on LHS) */ enum expr_flags {EXPR_F_CONSTANT =0x1 ,EXPR_F_SINGLETON =0x2 ,EXPR_F_PROTOCOL =0x4 ,EXPR_F_INTERVAL_END =0x8 , +EXPR_F_BOOLEAN =0x10 , };#include <payload.h>
include/linux/netfilter/nf_tables.h +1 -0
@@ -1257,6 +1257,7 @@ enum nft_fib_flags {NFTA_FIB_F_MARK =1 <<2 ,/* use skb->mark */ NFTA_FIB_F_IIF =1 <<3 ,/* restrict to iif */ NFTA_FIB_F_OIF =1 <<4 ,/* restrict to oif */ + NFTA_FIB_F_PRESENT = 1 << 5, /* check existence only */ };#define NFT_OBJECT_UNSPEC 0
src/evaluate.c +13 -1
@@ -1658,6 +1658,17 @@ range:return 0 ; } +static int expr_evaluate_fib (struct eval_ctx *ctx,struct expr **exprp) +{ +struct expr *expr = *exprp; + +if (expr->flags &EXPR_F_BOOLEAN ) { + expr->fib.flags |=NFTA_FIB_F_PRESENT ; + expr->dtype = &boolean_type; + } +return expr_evaluate_primary (ctx, exprp); +} +static int expr_evaluate (struct eval_ctx *ctx,struct expr **expr) {#ifdef DEBUG
@@ -1680,8 +1691,9 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)return expr_evaluate_exthdr (ctx, expr);case EXPR_VERDICT :case EXPR_META : -case EXPR_FIB :return expr_evaluate_primary (ctx, expr); +case EXPR_FIB : +return expr_evaluate_fib (ctx, expr);case EXPR_PAYLOAD :return expr_evaluate_payload (ctx, expr);case EXPR_RT :
src/expression.c +4 -0
@@ -592,6 +592,10 @@ struct expr *relational_expr_alloc(const struct location *loc, enum ops op, expr->left = left; expr->op = op; expr->right = right; + +if (right->dtype == &boolean_type) + left->flags |=EXPR_F_BOOLEAN ; +return expr; }
src/fib.c +4 -1
@@ -73,7 +73,7 @@ static void __fib_expr_print_f(unsigned int *flags, unsigned int f, const char *static void fib_expr_print (const struct expr *expr) { -unsigned int flags = expr->fib.flags; +unsigned int flags = expr->fib.flags & ~NFTA_FIB_F_PRESENT ;printf ("fib " );__fib_expr_print_f (&flags,NFTA_FIB_F_SADDR ,"saddr" );
@@ -130,6 +130,9 @@ struct expr *fib_expr_alloc(const struct location *loc,BUG ("Unknown result %d\n" , result); } +if (flags &NFTA_FIB_F_PRESENT ) + type = &boolean_type; + expr =expr_alloc (loc, &fib_expr_ops, type,BYTEORDER_HOST_ENDIAN , len);
tests/py/inet/fib.t +3 -0
@@ -12,3 +12,6 @@ fib saddr . iif oifname "lo";ok fib daddr . iif type local;ok fib daddr . iif type vmap { blackhole : drop, prohibit : drop, unicast : accept };ok fib daddr . oif type local;fail + +fib daddr oif exists;ok +fib daddr oif missing;ok
tests/py/inet/fib.t.payload +10 -0
@@ -20,3 +20,13 @@ __map%d test-ip 0 ip test-ip prerouting [ fib daddr . iif type => reg 1 ] [ lookup reg 1 set __map%d dreg 0 ] + +# fib daddr oif exists +ip test-ip prerouting + [ fib daddr oif => reg 1 ] + [ cmp eq reg 1 0x00000001 ] + +# fib daddr oif missing +ip test-ip prerouting + [ fib daddr oif => reg 1 ] + [ cmp eq reg 1 0x00000000 ]
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerRe: [nft PATCH 4/5] fib: Support existence check
On Fri, Mar 10, 2017 at 06:13:52PM +0100, Phil Sutter wrote:
[...]
tests/py/inet/fib.t.payload __map%d test-ip 0 · line 26
Quoted 12 lines
tests/py/inet/fib.t.payload +4
-0@@ -20,3 +20,13 @@ __map%d test-ip 0 ip test-ip prerouting [ fib daddr . iif type => reg 1 ] [ lookup reg 1 set __map%d dreg 0 ] + +# fib daddr oif exists +ip test-ip prerouting + [ fib daddr oif => reg 1 ]
Instead of casting out the flag like this:
@@ -173,7 +173,7 @@ nftnl_expr_fib_snprintf_default(char *buf, size_t size,
{
struct nftnl_expr_fib *fib = nftnl_expr_data(e);
int len = size, offset = 0, ret, i;
- uint32_t flags = fib->flags;
+ uint32_t flags = fib->flags & ~NFTA_FIB_F_PRESENT;
static const struct {
int bit;
const char *name;I think it would be better if we have a different fib output for the present case, eg.
[ fib daddr oif present => reg 1 ]
So we can see from --debug=netlink this generates bytecode to check the presence.
Same thing applies to:
# exthdr hbh exists ip6 test-ip6 input [ exthdr load 1b @ 43 + 0 => reg 1 ] [ cmp eq reg 1 0x00000001 ]
Instead, I'd suggest something like:
[ exthdr 43 present => reg 1 ]
[ cmp eq reg 1 0x00000001 ]
You would need to send a follow up to amend test for 3/5, but that should be no problem.
Thanks!
Phil Sutter
reviewerRe: [nft PATCH 4/5] fib: Support existence check
Hi,
Quoted 27 lines · Pablo Neira Ayuso
On Fri, Mar 10, 2017 at 06:13:52PM +0100, Phil Sutter wrote:
[...]tests/py/inet/fib.t.payload __map%d test-ip 0 · line 26
Quoted 12 lines
tests/py/inet/fib.t.payload +4
-0@@ -20,3 +20,13 @@ __map%d test-ip 0 ip test-ip prerouting [ fib daddr . iif type => reg 1 ] [ lookup reg 1 set __map%d dreg 0 ] + +# fib daddr oif exists +ip test-ip prerouting + [ fib daddr oif => reg 1 ]Instead of casting out the flag like this:
(quoted diff) +1
-1@@ -173,7 +173,7 @@ nftnl_expr_fib_snprintf_default(char *buf, size_t size, { struct nftnl_expr_fib *fib = nftnl_expr_data(e); int len = size, offset = 0, ret, i; - uint32_t flags = fib->flags; + uint32_t flags = fib->flags & ~NFTA_FIB_F_PRESENT; static const struct { int bit; const char *name;
Quoted 4 lines
I think it would be better if we have a different fib output for the present case, eg.
[ fib daddr oif present => reg 1 ]
Yes, this is a good point! Thanks for suggesting the output format - I was tempted to adjust it but had no good idea and so left it as-is. And indeed, exthdr netlink debug output hiding the existence check detail is ugly as well.
I'll implement the missing bits and then respin, including the man page patch - so no need to apply it now.
Thanks, Phil
Phil Sutter
reviewerPATCH 5/5 doc: Document boolean type and applications
In reply to Phil Sutter--- doc/nft.xml | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+)
doc/nft.xml +134 -0
@@ -1329,6 +1329,110 @@ filter output ip6 daddr ::1 </programlisting> </example> </refsect2> + + <refsect2> + <title>Boolean type</title> + <para> + <table frame="all"> + <tgroup cols='4' align='left' colsep='1' rowsep='1'> + <colspec colname='c1'/> + <colspec colname='c2'/> + <colspec colname='c3'/> + <colspec colname='c4'/> + <thead> + <row> + <entry>Name</entry> + <entry>Keyword</entry> + <entry>Size</entry> + <entry>Base type</entry> + </row> + </thead> + <tbody> + <row> + <entry>Boolean</entry> + <entry>boolean</entry> + <entry>1 bit</entry> + <entry>integer</entry> + </row> + </tbody> + </tgroup> + </table> + </para> + <para> + The boolean type is a syntactical helper type in user space. + It's use is in the right-hand side of a (typically implicit) + relational expression to change the expression on the left-hand + side into a boolean check (usually for existence). + </para> + <para> + The following keywords will automatically resolve into a boolean + type with given value: + <table frame="all"> + <tgroup cols='2' align='left' colsep='1' rowsep='1'> + <colspec colname='c1'/> + <colspec colname='c2'/> + <thead> + <row> + <entry>Keyword</entry> + <entry>Value</entry> + </row> + </thead> + <tbody> + <row> + <entry>exists</entry> + <entry>1</entry> + </row> + <row> + <entry>missing</entry> + <entry>0</entry> + </row> + </tbody> + </tgroup> + </table> + </para> + <example> + <title>Boolean specification</title> + <para> + The following expressions support a boolean comparison: + <table frame="all"> + <tgroup cols='2' align='left' colsep='1' rowsep='1'> + <colspec colname='c1'/> + <colspec colname='c2'/> + <thead> + <row> + <entry>Expression</entry> + <entry>Behaviour</entry> + </row> + </thead> + <tbody> + <row> + <entry>fib</entry> + <entry>Check route existence.</entry> + </row> + <row> + <entry>exthdr</entry> + <entry>Check IPv6 extension header existence.</entry> + </row> + <row> + <entry>tcp option</entry> + <entry>Check TCP option header existence.</entry> + </row> + </tbody> + </tgroup> + </table> + </para> + <programlisting> +# match if route exists +filter input fib iif saddr exists + +# match only non-fragmented packets in IPv6 traffic +filter input exthdr frag missing + +# match if TCP timestamp option is present +filter input tcp option timestamp exists + </programlisting> + </example> + </refsect2> </refsect1> <refsect1>
@@ -2536,6 +2640,36 @@ inet filter meta nfproto ipv6 output rt nexthop fd00::1 <arg choice="none"><replaceable>tcp_option_field</replaceable></arg> </cmdsynopsis> <para> + The following syntaxes are valid only in a relational expression + with boolean type on right-hand side for checking header existence only: + </para> + <cmdsynopsis> + <command>exthdr</command> + <group choice="req"> + <arg>hbh</arg> + <arg>frag</arg> + <arg>rt</arg> + <arg>dst</arg> + <arg>mh</arg> + </group> + </cmdsynopsis> + <cmdsynopsis> + <command>tcp option</command> + <group choice="req"> + <arg>eol</arg> + <arg>noop</arg> + <arg>maxseg</arg> + <arg>window</arg> + <arg>sack-permitted</arg> + <arg>sack</arg> + <arg>sack0</arg> + <arg>sack1</arg> + <arg>sack2</arg> + <arg>sack3</arg> + <arg>timestamp</arg> + </group> + </cmdsynopsis> + <para> <table frame="all"> <title>IPv6 extension headers</title> <tgroup cols='2' align='left' colsep='1' rowsep='1'>
Signature · 1 line
2.11.0
Pablo Neira Ayuso
maintainerRe: [nft PATCH 5/5] doc: Document boolean type and applications
I can place this doc updates now, even if the fib part will be out of sync for a little while until you follow up with the requested changes. Or keep it back for a little while until we get this in sync.
Either way, I'll be fine, so let me know what you prefer.
Thanks.
Pablo Neira Ayuso
maintainerQuoted 4 lines · Phil Sutter
The following patch series implements a boolean datatype for use in a boolean expression as right-hand side of a relational. Based upon that, header existence matches are implemented for IPv6 extension and TCP option headers as well as an existence match of a FIB entry.
Nice series Phil, applied from patch 1/5 to 3/5.
Please see comment on 4/5. Thanks!