src: add interface wildcard matching
Pablo Neira Ayuso
maintainerPATCH v2 1/1 src: add interface wildcard matching
Contrary to iptables, we use the asterisk character '*' as wildcard.
# nft --debug=netlink add rule test test iifname eth\* ip test test [ meta load iifname => reg 1 ] [ cmp eq reg 1 0x00687465 ]
Note that this generates an optimized comparison without bitwise.
In case you want to match a device that contains an asterisk, you have to escape the asterisk, ie.
# nft add rule test test iifname eth\\*
The wildcard string handling occurs from the evaluation step, where we convert from:
relational
/ \
/ \
meta value
oifname eth*to:
relational
/ \
/ \
meta prefix
ofinameAs Patrick suggested, this not actually a wildcard but a prefix since it only applies to the string when placed at the end.
More comments:
* This relaxes the left->size > right->size from netlink_parse_cmp() for strings since the optimization that this patch applies may now result in bogus errors.
* This patch can be later on extended to apply a similar optimization to payload expressions when:
expr->len % BITS_PER_BYTE == 0
For meta and ct, the kernel checks for the exact length of the attributes (it expects integer 32 bits) so we can't do it unless we relax that.
* Wildcard strings are not supported from sets and maps yet. Error reporting is not very good at this stage since expr_evaluate_prefix() doesn't have enough context (ctx->set is NULL, the set object is currently created later after evaluating the lhs and rhs of the relational). I'll be following up on this later.
---
v2: Use prefix expression instead of binop and optimize netlink bytecode not
to generate bitwise.
include/utils.h | 1 + src/evaluate.c | 91 +++++++++++++++++++++++----- src/netlink_delinearize.c | 115 +++++++++++++++++++++++++++++------- src/netlink_linearize.c | 17 +++++- src/parser_bison.y | 4 +- src/scanner.l | 6 ++ src/utils.c | 13 ++++ tests/regression/any/meta.t | 4 ++ tests/regression/any/meta.t.payload | 20 +++++++ 9 files changed, 231 insertions(+), 40 deletions(-)
include/utils.h +1 -0
@@ -129,5 +129,6 @@ extern void *xmalloc(size_t size);extern void *xrealloc (void *ptr,size_t size);extern void *xzalloc (size_t size);extern char *xstrdup (const char *s); +extern void xstrunescape (const char *in,char *out);#endif /* NFTABLES_UTILS_H */
src/evaluate.c +77 -14
136 lines not on this page · raw message
src/netlink_delinearize.c +93 -22
139 lines not on this page · raw message
src/netlink_linearize.c +14 -3
@@ -325,6 +325,7 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,struct nftnl_expr *nle;enum nft_registers sreg;struct expr *right; +int len;assert (dreg ==NFT_REG_VERDICT );
@@ -332,14 +333,24 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx,return netlink_gen_range (ctx, expr, dreg); sreg =get_register (ctx, expr->left); -netlink_gen_expr (ctx, expr->left, sreg);switch (expr->right->ops->type) {case EXPR_PREFIX : - right =netlink_gen_prefix (ctx, expr, sreg); +if (expr->left->dtype->type !=TYPE_STRING ) { + len =div_round_up (expr->right->len,BITS_PER_BYTE ); +netlink_gen_expr (ctx, expr->left, sreg); + right =netlink_gen_prefix (ctx, expr, sreg); + }else { + len =div_round_up (expr->right->prefix_len,BITS_PER_BYTE ); + right = expr->right->prefix; + expr->left->len = expr->right->prefix_len; +netlink_gen_expr (ctx, expr->left, sreg); + }break ;default : + len =div_round_up (expr->right->len,BITS_PER_BYTE ); right = expr->right; +netlink_gen_expr (ctx, expr->left, sreg);break ; }
@@ -349,7 +360,7 @@ static void netlink_gen_cmp(struct netlink_linearize_ctx *ctx, netlink_gen_cmp_op(expr->op));payload_shift_value (expr->left, right);netlink_gen_data (right, &nld); -nftnl_expr_set (nle,NFTNL_EXPR_CMP_DATA , nld.value, nld.len); +nftnl_expr_set (nle,NFTNL_EXPR_CMP_DATA , nld.value, len);release_register (ctx, expr->left);nftnl_rule_add_expr (ctx->nlr, nle);
src/parser_bison.y +3 -1
@@ -216,7 +216,8 @@ static void location_update(struct location *loc, struct location *rhs, int n) %token <val> NUM "number" %token <string> STRING "string" %token <string> QUOTED_STRING -%destructor { xfree($$); } STRING QUOTED_STRING +%token <string> ASTERISK_STRING +%destructor { xfree($$); } STRING QUOTED_STRING ASTERISK_STRING %token LL_HDR "ll" %token NETWORK_HDR "nh"
@@ -1159,6 +1160,7 @@ identifier : STRING string : STRING | QUOTED_STRING + | ASTERISK_STRING ; time_spec : STRING
src/scanner.l +6 -0
@@ -114,6 +114,7 @@ range ({decstring}?:{decstring}?) letter [a-zA-Z] string ({letter})({letter}|{digit}|[/\-_\.])* quotedstring \"[^"]*\" +asteriskstring ({string}\*|{string}\\\*) comment #.*$ slash \/
@@ -498,6 +499,11 @@ addrstring ({macaddr}|{ip4addr}|{ip6addr}) return QUOTED_STRING; } +{asteriskstring} { + yylval->string = xstrdup(yytext); + return ASTERISK_STRING; + } + {string} { yylval->string = xstrdup(yytext); return STRING;
src/utils.c +13 -0
@@ -66,3 +66,16 @@ char *xstrdup(const char *s)memory_allocation_error ();return res; } + +void xstrunescape (const char *in,char *out) +{ +unsigned int i, k =0 ; + +for (i =0 ; i <strlen (in); i++) { +if (in[i] =='\\' ) +continue ; + + out[k++] = in[i]; + } + out[k++] ='\0' ; +}
tests/regression/any/meta.t +4 -0
@@ -66,6 +66,8 @@ meta iifname "eth0";ok;iifname "eth0" meta iifname != "eth0";ok;iifname != "eth0" meta iifname {"eth0", "lo"};ok - meta iifname != {"eth0", "lo"};ok +meta iifname "eth*";ok;iifname "eth*" +meta iifname "eth\*";ok;iifname "eth\*" meta iiftype {ether, ppp, ipip, ipip6, loopback, sit, ipgre};ok - meta iiftype != {ether, ppp, ipip, ipip6, loopback, sit, ipgre};ok
@@ -83,6 +85,8 @@ meta oifname "eth0";ok;oifname "eth0" meta oifname != "eth0";ok;oifname != "eth0" meta oifname { "eth0", "lo"};ok - meta iifname != {"eth0", "lo"};ok +meta oifname "eth*";ok;oifname "eth*" +meta oifname "eth\*";ok;oifname "eth\*" meta oiftype {ether, ppp, ipip, ipip6, loopback, sit, ipgre};ok - meta oiftype != {ether, ppp, ipip, ipip6, loopback, sit, ipgre};ok
tests/regression/any/meta.t.payload +20 -0
@@ -217,6 +217,16 @@ ip test-ip4 input [ meta load iifname => reg 1 ] [ lookup reg 1 set set%d ] +# meta iifname "eth*" +ip test-ip4 input + [ meta load iifname => reg 1 ] + [ cmp eq reg 1 0x00687465 ] + +# meta iifname "eth\*" +ip test-ip4 input + [ meta load iifname => reg 1 ] + [ cmp eq reg 1 0x2a687465 0x00000000 0x00000000 0x00000000 ] + # meta iiftype {ether, ppp, ipip, ipip6, loopback, sit, ipgre} set%d test-ip4 3 set%d test-ip4 0
@@ -284,6 +294,16 @@ ip test-ip4 input [ meta load oifname => reg 1 ] [ lookup reg 1 set set%d ] +# meta oifname "eth*" +ip test-ip4 input + [ meta load oifname => reg 1 ] + [ cmp eq reg 1 0x00687465 ] + +# meta oifname "eth\*" +ip test-ip4 input + [ meta load oifname => reg 1 ] + [ cmp eq reg 1 0x2a687465 0x00000000 0x00000000 0x00000000 ] + # meta oiftype {ether, ppp, ipip, ipip6, loopback, sit, ipgre} set%d test-ip4 3 set%d test-ip4 0
Signature · 1 line
2.1.4
2015-11-02 12:48 GMT+01:00 Pablo Neira Ayuso <[email protected]>:
Quoted 3 lines
Contrary to iptables, we use the asterisk character '*' as wildcard.
# nft --debug=netlink add rule test test iifname eth\*
Any reason this is not iifprefix instead of iifname.. I find the wildcard confusing,
since it can only be placed in the end.
Signature · 1 line
Bj(/)rnar
Pablo Neira Ayuso
maintainerQuoted 6 lines · Bjørnar Ness
2015-11-02 12:48 GMT+01:00 Pablo Neira Ayuso <[email protected]>:
Quoted 3 lines
Contrary to iptables, we use the asterisk character '*' as wildcard.
# nft --debug=netlink add rule test test iifname eth\*Any reason this is not iifprefix instead of iifname..
We can use this approach from any key using a string datatype, eg. ct helper.
If we do this at key level, we'll have to add whateverprefix for every key where we want to support string prefixes.
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html