parser_bison: add shortcut syntax for matching flags without binary operations
Pablo Neira Ayuso
maintainerPATCH v2 1/1 parser_bison: add shortcut syntax for matching flags without binary operations
This patch adds the following shortcut syntax:
expression flags / flags
instead of:
expression and flags == flags
For example:
tcp flags syn,ack / syn,ack,fin,rst
^^^^^^^ ^^^^^^^^^^^^^^^
value maskinstead of:
tcp flags and (syn|ack|fin|rst) == syn|ack
The second list of comma-separated flags represents the mask which are examined and the first list of comma-separated flags must be set.
You can also use the != operator with this syntax:
tcp flags != fin,rst / syn,ack,fin,rst
This shortcut is based on the prefix notation, but it is also similar to the iptables tcp matching syntax.
This patch introduces the flagcmp expression to print the tcp flags in this new notation. The delinearize path transforms the binary expression to this new flagcmp expression whenever possible.
---
v2: add flagcmp expression.
update parser and delinearize path to use it.
include/expression.h | 11 +++++++ src/evaluate.c | 17 ++++++++++ src/expression.c | 48 ++++++++++++++++++++++++++++ src/netlink_delinearize.c | 64 ++++++++++++++++++++++++------------- src/parser_bison.y | 16 ++++++++++ tests/py/inet/tcp.t | 2 +- tests/py/inet/tcp.t.payload | 2 +- 7 files changed, 136 insertions(+), 24 deletions(-)
include/expression.h +11 -0
@@ -72,6 +72,7 @@ enum expr_types {EXPR_FIB ,EXPR_XFRM ,EXPR_SET_ELEM_CATCHALL , + EXPR_FLAGCMP, };#define EXPR_MAX EXPR_XFRM
@@ -370,6 +371,12 @@ struct expr {uint8_t ttl;uint32_t flags; } osf; +struct { +/* EXPR_FLAGCMP */ +struct expr *expr; +struct expr *mask; +struct expr *value; + } flagcmp; }; };
@@ -500,6 +507,10 @@ extern struct expr *set_elem_expr_alloc(const struct location *loc,struct expr *set_elem_catchall_expr_alloc (const struct location *loc); +struct expr *flagcmp_expr_alloc (const struct location *loc,enum ops op, +struct expr *expr,struct expr *mask, +struct expr *value); +extern void range_expr_value_low (mpz_t rop,const struct expr *expr);extern void range_expr_value_high (mpz_t rop,const struct expr *expr);
src/evaluate.c +17 -0
@@ -2134,6 +2134,21 @@ static int expr_evaluate_xfrm(struct eval_ctx *ctx, struct expr **exprp)return expr_evaluate_primary (ctx, exprp); } +static int expr_evaluate_flagcmp (struct eval_ctx *ctx,struct expr **exprp) +{ +struct expr *expr = *exprp, *binop, *rel; + + binop =binop_expr_alloc (&expr->location,OP_AND , +expr_get (expr->flagcmp.expr), +expr_get (expr->flagcmp.mask)); + rel =relational_expr_alloc (&expr->location, expr->op, binop, +expr_get (expr->flagcmp.value)); +expr_free (expr); + *exprp = rel; + +return expr_evaluate (ctx, exprp); +} +static int expr_evaluate (struct eval_ctx *ctx,struct expr **expr) {if (ctx->nft->debug_mask &NFT_DEBUG_EVALUATION ) {
@@ -2203,6 +2218,8 @@ static int expr_evaluate(struct eval_ctx *ctx, struct expr **expr)return expr_evaluate_xfrm (ctx, expr);case EXPR_SET_ELEM_CATCHALL :return 0 ; +case EXPR_FLAGCMP : +return expr_evaluate_flagcmp (ctx, expr);default :BUG ("unknown expression type %s\n" ,expr_name (*expr)); }
src/expression.c +48 -0
60 lines not on this page · raw message
src/netlink_delinearize.c +42 -22
84 lines not on this page · raw message
src/parser_bison.y +16 -0
@@ -4463,6 +4463,22 @@ relational_expr : expr /* implicit */ rhs_expr { $$ = relational_expr_alloc(&@$, OP_IMPLICIT, $1, $2); } + | expr /* implicit */ basic_rhs_expr SLASH list_rhs_expr + { + $$ = flagcmp_expr_alloc(&@$, OP_EQ, $1, $4, $2); + } + | expr /* implicit */ list_rhs_expr SLASH list_rhs_expr + { + $$ = flagcmp_expr_alloc(&@$, OP_EQ, $1, $4, $2); + } + | expr relational_op basic_rhs_expr SLASH list_rhs_expr + { + $$ = flagcmp_expr_alloc(&@$, $2, $1, $5, $3); + } + | expr relational_op list_rhs_expr SLASH list_rhs_expr + { + $$ = flagcmp_expr_alloc(&@$, $2, $1, $5, $3); + } | expr relational_op rhs_expr { $$ = relational_expr_alloc(&@2, $2, $1, $3);
tests/py/inet/tcp.t +1 -1
@@ -77,7 +77,7 @@ tcp flags != { fin, urg, ecn, cwr} drop;ok tcp flags cwr;ok tcp flags != cwr;ok tcp flags == syn;ok -tcp flags & (syn|fin) == (syn|fin);ok;tcp flags & (fin | syn) == fin | syn +tcp flags fin,syn / fin,syn;ok tcp flags & (fin | syn | rst | psh | ack | urg | ecn | cwr) == fin | syn | rst | psh | ack | urg | ecn | cwr;ok;tcp flags == 0xff tcp flags { syn, syn | ack };ok tcp flags & (fin | syn | rst | psh | ack | urg) == { fin, ack, psh | ack, fin | psh | ack };ok
tests/py/inet/tcp.t.payload +1 -1
@@ -434,7 +434,7 @@ inet test-inet input [ payload load 1b @ transport header + 13 => reg 1 ] [ cmp eq reg 1 0x00000002 ] -# tcp flags & (syn|fin) == (syn|fin) +# tcp flags fin,syn / fin,syn inet test-inet input [ meta load l4proto => reg 1 ] [ cmp eq reg 1 0x00000006 ]
Signature · 1 line
2.20.1