expression: typeof verdict needs verdict datatype
Pablo Neira Ayuso
maintainerPATCH v2 1/4 expression: typeof verdict needs verdict datatype
Otherwise listing breaks showing [invalid type] notice.
# nft list ruleset
table inet x {
map y {
typeof ip saddr : verdict
elements = { 1.1.1.1 : 0x1010101 [invalid type] }
}
}Update tests to cover this usecase.
---
v2: add one more test
src/expression.c | 1 + tests/shell/testcases/maps/dumps/typeof_maps_0.nft | 3 +++ tests/shell/testcases/maps/typeof_maps_0 | 3 +++ 3 files changed, 7 insertions(+)
src/expression.c +1 -0
@@ -268,6 +268,7 @@ static struct expr *verdict_expr_parse_udata(const struct nftnl_udata *attr)struct expr *e; e =symbol_expr_alloc (&internal_location,SYMBOL_VALUE ,NULL ,"verdict" ); + e->dtype = &verdict_type; e->len =NFT_REG_SIZE *BITS_PER_BYTE ;return e; }
tests/shell/testcases/maps/dumps/typeof_maps_0.nft +3 -0
@@ -17,11 +17,14 @@ table inet t { map m4 { typeof iifname . ip protocol . th dport : verdict + elements = { "eth0" . tcp . 22 : accept } } chain c { ct mark set osf name map @m1 meta mark set vlan id map @m2 meta mark set ip saddr . ip daddr map @m3 + iifname . ip protocol . th dport vmap @m4 + iifname . ip protocol . th dport vmap { "eth0" . tcp . 22 : accept, "eth1" . udp . 67 : drop } } }
tests/shell/testcases/maps/typeof_maps_0 +3 -0
@@ -24,12 +24,15 @@ EXPECTED="table inet t { map m4 { typeof iifname . ip protocol . th dport : verdict + elements = { eth0 . tcp . 22 : accept } } chain c { ct mark set osf name map @m1 ether type vlan meta mark set vlan id map @m2 meta mark set ip saddr . ip daddr map @m3 + iifname . ip protocol . th dport vmap @m4 + iifname . ip protocol . th dport vmap { \"eth0\" . tcp . 22 : accept, \"eth1\" . udp . 67 : drop } } }"
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 2/4 src: allow to use typeof of raw expressions in set declaration
Use the dynamic datatype to allocate an instance of TYPE_INTEGER and set length and byteorder. Add missing information to the set userdata area for raw payload expressions which allows to rebuild the set typeof from the listing path.
A few examples:
- With anonymous sets:
nft add rule x y ip saddr . @ih,32,32 { 1.1.1.1 . 0x14, 2.2.2.2 . 0x1e }
- With named sets:
table x {
set y {
typeof ip saddr . @ih,32,32
elements = { 1.1.1.1 . 0x14 }
}}
Incremental updates are also supported, eg.
nft add element x y { 3.3.3.3 . 0x28 }
expr_evaluate_concat() is used to evaluate both set key definitions and set key values, using two different function might help to simplify this code in the future.
---
v2: - fix vmap with anonymous maps.
- add more tests.
include/datatype.h | 1 + include/expression.h | 2 + src/datatype.c | 2 +- src/evaluate.c | 75 ++++++++++++++++--- src/expression.c | 6 +- src/mnl.c | 4 +- src/netlink.c | 33 +++++--- src/payload.c | 55 ++++++++++++-- .../testcases/maps/dumps/typeof_raw_0.nft | 13 ++++ tests/shell/testcases/maps/typeof_raw_0 | 18 +++++ .../testcases/sets/dumps/typeof_raw_0.nft | 12 +++ tests/shell/testcases/sets/typeof_raw_0 | 17 +++++ 12 files changed, 205 insertions(+), 33 deletions(-) create mode 100644 tests/shell/testcases/maps/dumps/typeof_raw_0.nft create mode 100755 tests/shell/testcases/maps/typeof_raw_0 create mode 100644 tests/shell/testcases/sets/dumps/typeof_raw_0.nft create mode 100755 tests/shell/testcases/sets/typeof_raw_0
include/datatype.h +1 -0
@@ -176,6 +176,7 @@ extern const struct datatype *datatype_lookup_byname(const char *name);extern struct datatype *datatype_get (const struct datatype *dtype);extern void datatype_set (struct expr *expr,const struct datatype *dtype);extern void datatype_free (const struct datatype *dtype); +struct datatype *dtype_clone (const struct datatype *orig_dtype);struct parse_ctx {struct symbol_tables *tbl;
include/expression.h +2 -0
@@ -120,6 +120,7 @@ struct expr_ctx {enum byteorder byteorder;unsigned int len;unsigned int maxval; +const struct expr *key; }; static inline void __expr_set_context(struct expr_ctx *ctx,
@@ -131,6 +132,7 @@ static inline void __expr_set_context(struct expr_ctx *ctx, ctx->byteorder = byteorder; ctx->len = len; ctx->maxval = maxval; + ctx->key =NULL ; } static inline void expr_set_context(struct expr_ctx *ctx,
src/datatype.c +1 -1
@@ -1136,7 +1136,7 @@ void datatype_set(struct expr *expr, const struct datatype *dtype) expr->dtype =datatype_get (dtype); } -static struct datatype *dtype_clone (const struct datatype *orig_dtype) +struct datatype *dtype_clone (const struct datatype *orig_dtype) {struct datatype *dtype;
src/evaluate.c +63 -12
150 lines not on this page · raw message
src/expression.c +4 -2
@@ -18,6 +18,7 @@#include <expression.h> #include <statement.h> #include <datatype.h> +#include <netlink.h> #include <rule.h> #include <gmputil.h> #include <utils.h>
@@ -950,7 +951,7 @@ static struct expr *concat_expr_parse_udata(const struct nftnl_udata *attr)const struct nftnl_udata *ud[NFTNL_UDATA_SET_KEY_CONCAT_NEST_MAX ] = {};const struct datatype *dtype;struct expr *concat_expr; -uint32_t dt =0 ; +uint32_t dt =0 , len =0 ;unsigned int i;int err;
@@ -991,6 +992,7 @@ static struct expr *concat_expr_parse_udata(const struct nftnl_udata *attr) dt =concat_subtype_add (dt, expr->dtype->type);compound_expr_add (concat_expr, expr); + len +=netlink_padded_len (expr->len); } dtype =concat_type_alloc (dt);
@@ -998,7 +1000,7 @@ static struct expr *concat_expr_parse_udata(const struct nftnl_udata *attr) goto err_free; concat_expr->dtype =datatype_get (dtype); - concat_expr->len = dtype->size; + concat_expr->len = len;return concat_expr;
src/mnl.c +1 -3
@@ -1093,9 +1093,7 @@ static void set_key_expression(struct netlink_ctx *ctx, {struct nftnl_udata *nest1, *nest2; -if (expr->flags &EXPR_F_CONSTANT || -set_is_anonymous (set_flags) || - !expr_ops (expr)->build_udata) +if (!expr_ops (expr)->build_udata)return ; nest1 =nftnl_udata_nest_start (udbuf, type);
src/netlink.c +23 -10
@@ -1105,17 +1105,25 @@ static struct expr *netlink_parse_interval_elem(const struct set *set,return range_expr_to_prefix (range); } -static struct expr *concat_elem_expr (struct expr *expr, +static struct expr *concat_elem_expr (struct expr *key,const struct datatype *dtype,struct expr *data,int *off) {const struct datatype *subtype; +struct expr *expr; - subtype =concat_subtype_lookup (dtype->type, --(*off)); - - expr =constant_expr_splice (data, subtype->size); - expr->dtype = subtype; - expr->byteorder = subtype->byteorder; +if (key) { + (*off)--; + expr =constant_expr_splice (data, key->len); + expr->dtype =datatype_get (key->dtype); + expr->byteorder = key->byteorder; + expr->len = key->len; + }else { + subtype =concat_subtype_lookup (dtype->type, --(*off)); + expr =constant_expr_splice (data, subtype->size); + expr->dtype = subtype; + expr->byteorder = subtype->byteorder; + }if (expr->byteorder ==BYTEORDER_HOST_ENDIAN )mpz_switch_byteorder (expr->value, expr->len /BITS_PER_BYTE );
@@ -1133,13 +1141,18 @@ static struct expr *netlink_parse_concat_elem_key(const struct set *set, struct expr *data) {const struct datatype *dtype = set->key->dtype; -struct expr *concat, *expr; +struct expr *concat, *expr, *n =NULL ;int off = dtype->subtypes; +if (set->key->etype ==EXPR_CONCAT ) + n = list_first_entry(&set->key->expressions, struct expr, list); + concat =concat_expr_alloc (&data->location);while (off >0 ) { - expr =concat_elem_expr (expr, dtype, data, &off); + expr =concat_elem_expr (n, dtype, data, &off);compound_expr_add (concat, expr); +if (set->key->etype ==EXPR_CONCAT ) + n =list_next_entry (n, list); }expr_free (data);
@@ -1159,7 +1172,7 @@ static struct expr *netlink_parse_concat_elem(const struct set *set, concat =concat_expr_alloc (&data->location);while (off >0 ) { - expr =concat_elem_expr (expr, dtype, data, &off); + expr =concat_elem_expr (NULL , dtype, data, &off);list_add_tail (&expr->list, &expressions); }
@@ -1171,7 +1184,7 @@ static struct expr *netlink_parse_concat_elem(const struct set *set,while (off >0 ) { left = list_first_entry(&expressions, struct expr, list); - expr =concat_elem_expr (expr, dtype, data, &off); + expr =concat_elem_expr (NULL , dtype, data, &off);list_del (&left->list); range =range_expr_alloc (&data->location, left, expr);
src/payload.c +50 -5
93 lines not on this page · raw message
tests/shell/testcases/maps/dumps/typeof_raw_0.nft new file +13 -0
@@ -0,0 +1,13 @@ +table ip x { + map y { + typeof ip saddr . @ih,32,32 : verdict + elements = { 1.1.1.1 . 0x14 : accept, + 7.7.7.7 . 0x86 : accept, + 7.7.7.8 . 0x97 : drop } + } + + chain y { + ip saddr . @ih,32,32 vmap @y + ip saddr . @ih,32,32 vmap { 4.4.4.4 . 0x34 : accept, 5.5.5.5 . 0x45 : drop } + } +}
tests/shell/testcases/maps/typeof_raw_0 new file +18 -0
@@ -0,0 +1,18 @@ +#!/bin/bash + +EXPECTED="table ip x { + map y { + typeof ip saddr . @ih,32,32: verdict + elements = { 1.1.1.1 . 0x14 : accept, 2.2.2.2 . 0x1e : drop } + } + + chain y { + ip saddr . @ih,32,32 vmap @y + ip saddr . @ih,32,32 vmap { 4.4.4.4 . 0x34 : accept, 5.5.5.5 . 0x45 : drop} + } +}" + +set -e +$NFT -f - <<< $EXPECTED +$NFT add element ip x y { 7.7.7.7 . 0x86 : accept, 7.7.7.8 . 0x97 : drop } +$NFT delete element ip x y { 2.2.2.2 . 0x1e : drop }
tests/shell/testcases/sets/dumps/typeof_raw_0.nft new file +12 -0
@@ -0,0 +1,12 @@ +table inet t { + set y { + typeof ip daddr . @ih,32,32 + elements = { 1.1.1.1 . 0x14, + 2.2.2.2 . 0x20 } + } + + chain y { + ip saddr . @ih,32,32 { 1.1.1.1 . 0x14, 2.2.2.2 . 0x1e } + ip daddr . @ih,32,32 @y + } +}
tests/shell/testcases/sets/typeof_raw_0 new file +17 -0
@@ -0,0 +1,17 @@ +#!/bin/bash + +EXPECTED="table inet t { + set y { + typeof ip daddr . @ih,32,32 + elements = { 1.1.1.1 . 0x14, 2.2.2.2 . 0x20} + } + + chain y { + ip saddr . @ih,32,32 { 1.1.1.1 . 0x14, 2.2.2.2 . 0x1e } + ip daddr . @ih,32,32 @y + } +}" + +set -e +$NFT -f - <<< $EXPECTED +
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 3/4 src: allow to use integer type header fields via typeof set declaration
In reply to Pablo Neira AyusoHeader fields such as udp length cannot be used in concatenations because it is using the generic integer_type:
test.nft:3:10-19: Error: can not use variable sized data types (integer) in concat expressions
typeof udp length . @th,32,32
^^^^^^^^^^~~~~~~~~~~~~This patch slightly extends ("src: allow to use typeof of raw expressions in set declaration") to set on NFTNL_UDATA_SET_KEY_PAYLOAD_LEN in userdata if TYPE_INTEGER is used.
---
v2: add more tests.
src/evaluate.c | 2 +- src/payload.c | 15 ++++++----- .../testcases/maps/dumps/typeof_integer_0.nft | 20 ++++++++++++++ tests/shell/testcases/maps/typeof_integer_0 | 27 +++++++++++++++++++ 4 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 tests/shell/testcases/maps/dumps/typeof_integer_0.nft create mode 100755 tests/shell/testcases/maps/typeof_integer_0
src/evaluate.c +1 -1
@@ -3970,7 +3970,7 @@ static int set_expr_evaluate_concat(struct eval_ctx *ctx, struct expr **expr)return expr_error (ctx->msgs, i,"specify either ip or ip6 for address matching" ); -if (i->etype ==EXPR_PAYLOAD && i->payload.is_raw && +if (i->etype ==EXPR_PAYLOAD && i->dtype->type ==TYPE_INTEGER ) {struct datatype *dtype;
src/payload.c +9 -6
@@ -153,9 +153,9 @@ static int payload_expr_build_udata(struct nftnl_udata_buf *udbuf, expr->payload.base);nftnl_udata_put_u32 (udbuf,NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET , expr->payload.offset); -nftnl_udata_put_u32 (udbuf,NFTNL_UDATA_SET_KEY_PAYLOAD_LEN , - expr->len); } +if (expr->dtype->type ==TYPE_INTEGER ) +nftnl_udata_put_u32 (udbuf,NFTNL_UDATA_SET_KEY_PAYLOAD_LEN , expr->len);return 0 ; }
@@ -191,7 +191,7 @@ static int payload_parse_udata(const struct nftnl_udata *attr, void *data)static struct expr *payload_expr_parse_udata (const struct nftnl_udata *attr) {const struct nftnl_udata *ud[NFTNL_UDATA_SET_KEY_PAYLOAD_MAX +1 ] = {}; -unsigned int type, base, offset, len; +unsigned int type, base, offset, len =0 ;const struct proto_desc *desc;bool is_raw = false;struct expr *expr;
@@ -209,20 +209,23 @@ static struct expr *payload_expr_parse_udata(const struct nftnl_udata *attr) desc =find_proto_desc (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_DESC ]);if (!desc) {if (!ud[NFTNL_UDATA_SET_KEY_PAYLOAD_BASE ] || - !ud[NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET ] || - !ud[NFTNL_UDATA_SET_KEY_PAYLOAD_LEN ]) + !ud[NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET ])return NULL ; base =nftnl_udata_get_u32 (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_BASE ]); offset =nftnl_udata_get_u32 (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_OFFSET ]); - len =nftnl_udata_get_u32 (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_LEN ]); is_raw = true; } type =nftnl_udata_get_u32 (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_TYPE ]); +if (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_LEN ]) + len =nftnl_udata_get_u32 (ud[NFTNL_UDATA_SET_KEY_PAYLOAD_LEN ]); expr =payload_expr_alloc (&internal_location, desc, type); +if (len) + expr->len = len; +if (is_raw) {struct datatype *dtype;
tests/shell/testcases/maps/dumps/typeof_integer_0.nft new file +20 -0
@@ -0,0 +1,20 @@ +table inet t { + map m1 { + typeof udp length . @ih,32,32 : verdict + flags interval + elements = { 20-80 . 0x14 : accept, + 1-10 . 0xa : drop } + } + + map m2 { + typeof udp length . @ih,32,32 : verdict + elements = { 30 . 0x1e : drop, + 20 . 0x24 : accept } + } + + chain c { + udp length . @ih,32,32 vmap @m1 + udp length . @ih,32,32 vmap @m2 + udp length . @th,160,128 vmap { 47-63 . 0xe373135363130333131303735353203 : accept } + } +}
tests/shell/testcases/maps/typeof_integer_0 new file +27 -0
@@ -0,0 +1,27 @@ +#!/bin/bash + +EXPECTED="table inet t { + map m1 { + typeof udp length . @ih,32,32 : verdict + flags interval + elements = { 20-80 . 0x14 : accept, 1-10 . 0xa : drop } + } + + map m2 { + typeof udp length . @ih,32,32 : verdict + elements = { 20 . 0x24 : accept, 30 . 0x1e : drop } + } + + chain c { + udp length . @ih,32,32 vmap @m1 + udp length . @ih,32,32 vmap @m2 + udp length . @th,160,128 vmap { 47-63 . 0xe373135363130333131303735353203 : accept } + } +}" + +$NFT add element inet t m1 { 90-100 . 40 : drop } +$NFT delete element inet t m2 { 20 . 20 : accept } + +set -e +$NFT -f - <<< $EXPECTED +
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH v2 4/4 optimize: Restore optimization for raw payload expressions
In reply to Pablo Neira AyusoThis patch reverts d0f14b5337e7 ("optimize: do not merge raw payload expressions") after adding support for concatenation with variable length TYPE_INTEGER.
---
v2: new in this series
src/optimize.c | 3 -- .../optimizations/dumps/merge_vmap_raw.nft | 31 ++++++++++++++++++ .../testcases/optimizations/merge_vmap_raw | 32 +++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 tests/shell/testcases/optimizations/dumps/merge_vmap_raw.nft create mode 100755 tests/shell/testcases/optimizations/merge_vmap_raw
src/optimize.c +0 -3
@@ -40,9 +40,6 @@ static bool __expr_cmp(const struct expr *expr_a, const struct expr *expr_b)switch (expr_a->etype) {case EXPR_PAYLOAD : -/* disable until concatenation with integer works. */ -if (expr_a->payload.is_raw || expr_b->payload.is_raw) -return false;if (expr_a->payload.base != expr_b->payload.base)return false;if (expr_a->payload.offset != expr_b->payload.offset)
tests/shell/testcases/optimizations/dumps/merge_vmap_raw.nft new file +31 -0
@@ -0,0 +1,31 @@ +table inet x { + chain nat_dns_dnstc { + meta l4proto udp redirect to :5300 + drop + } + + chain nat_dns_this_5301 { + meta l4proto udp redirect to :5301 + drop + } + + chain nat_dns_saturn_5301 { + meta nfproto ipv4 meta l4proto udp dnat ip to 240.0.1.2:5301 + drop + } + + chain nat_dns_saturn_5302 { + meta nfproto ipv4 meta l4proto udp dnat ip to 240.0.1.2:5302 + drop + } + + chain nat_dns_saturn_5303 { + meta nfproto ipv4 meta l4proto udp dnat ip to 240.0.1.2:5303 + drop + } + + chain nat_dns_acme { + udp length . @th,160,128 vmap { 47-63 . 0xe373135363130333131303735353203 : goto nat_dns_dnstc, 62-78 . 0xe31393032383939353831343037320e : goto nat_dns_this_5301, 62-78 . 0xe31363436323733373931323934300e : goto nat_dns_saturn_5301, 62-78 . 0xe32393535373539353636383732310e : goto nat_dns_saturn_5302, 62-78 . 0xe38353439353637323038363633390e : goto nat_dns_saturn_5303 } + drop + } +}
tests/shell/testcases/optimizations/merge_vmap_raw new file +32 -0
@@ -0,0 +1,32 @@ +#!/bin/bash + +set -e + +RULESET="table inet x { + chain nat_dns_dnstc { meta l4proto udp redirect to :5300 ; drop ; } + chain nat_dns_this_5301 { meta l4proto udp redirect to :5301 ; drop ; } + chain nat_dns_saturn_5301 { meta nfproto ipv4 meta l4proto udp dnat to 240.0.1.2:5301 ; drop ; } + chain nat_dns_saturn_5302 { meta nfproto ipv4 meta l4proto udp dnat to 240.0.1.2:5302 ; drop ; } + chain nat_dns_saturn_5303 { meta nfproto ipv4 meta l4proto udp dnat to 240.0.1.2:5303 ; drop ; } + + chain nat_dns_acme { + udp length 47-63 @th,160,128 0x0e373135363130333131303735353203 \ + goto nat_dns_dnstc + + udp length 62-78 @th,160,128 0x0e31393032383939353831343037320e \ + goto nat_dns_this_5301 + + udp length 62-78 @th,160,128 0x0e31363436323733373931323934300e \ + goto nat_dns_saturn_5301 + + udp length 62-78 @th,160,128 0x0e32393535373539353636383732310e \ + goto nat_dns_saturn_5302 + + udp length 62-78 @th,160,128 0x0e38353439353637323038363633390e \ + goto nat_dns_saturn_5303 + + drop + } +}" + +$NFT -o -f - <<< $RULESET
Signature · 1 line
2.30.2