[PATCH] filter: Add .srclen attribute to filter on source prefix length
This adds a new attribute to expose the source prefix length in the same way as the destination prefix length, via a new .srclen attribute. Signed-off-by: Toke Høiland-Jørgensen <toke@toke.dk> --- filter/config.Y | 3 ++- filter/filter.c | 8 ++++++++ filter/filter.h | 1 + lib/net.h | 2 ++ 4 files changed, 13 insertions(+), 1 deletion(-) diff --git a/filter/config.Y b/filter/config.Y index f8170a83..ee179699 100644 --- a/filter/config.Y +++ b/filter/config.Y @@ -413,7 +413,7 @@ CF_KEYWORDS(FUNCTION, PRINT, PRINTN, UNSET, RETURN, PREFERENCE, ROA_CHECK, ASN, IS_V4, IS_V6, - LEN, MAXLEN, + LEN, MAXLEN, SRCLEN, DEFINED, ADD, DELETE, CONTAINS, RESET, PREPEND, FIRST, LAST, LAST_NONAGGREGATED, MATCH, @@ -896,6 +896,7 @@ term: | term '.' IP { $$ = f_new_inst(FI_IP); $$->a1.p = $1; $$->aux = T_IP; } | term '.' RD { $$ = f_new_inst(FI_ROUTE_DISTINGUISHER); $$->a1.p = $1; $$->aux = T_RD; } | term '.' LEN { $$ = f_new_inst(FI_LENGTH); $$->a1.p = $1; } + | term '.' SRCLEN { $$ = f_new_inst(FI_SRCLEN); $$->a1.p = $1; } | term '.' MAXLEN { $$ = f_new_inst(FI_ROA_MAXLEN); $$->a1.p = $1; } | term '.' ASN { $$ = f_new_inst(FI_ROA_ASN); $$->a1.p = $1; } | term '.' MASK '(' term ')' { $$ = f_new_inst(FI_IP_MASK); $$->a1.p = $1; $$->a2.p = $5; } diff --git a/filter/filter.c b/filter/filter.c index 881ba420..32a607ec 100644 --- a/filter/filter.c +++ b/filter/filter.c @@ -1241,6 +1241,14 @@ interpret(struct f_inst *what) default: runtime( "Prefix, path, clist or eclist expected" ); } break; + case FI_SRCLEN: /* Get SADR src prefix length */ + ONEARG; + if (v1.type != T_NET || !net_is_sadr(v1.val.net)) + runtime( "SADR expected" ); + + res.type = T_INT; + res.val.i = ((net_addr_ip6_sadr *) v1.val.net)->src_pxlen; + break; case FI_ROA_MAXLEN: /* Get ROA max prefix length */ ONEARG; if (v1.type != T_NET || !net_is_roa(v1.val.net)) diff --git a/filter/filter.h b/filter/filter.h index d347924a..0e1437e6 100644 --- a/filter/filter.h +++ b/filter/filter.h @@ -53,6 +53,7 @@ F(FI_PREF_GET, 0, 'P') \ F(FI_PREF_SET, 'P', 'S') \ F(FI_LENGTH, 0, 'L') \ + F(FI_SRCLEN, 's', 'L') \ F(FI_ROA_MAXLEN, 'R', 'M') \ F(FI_ROA_ASN, 'R', 'A') \ F(FI_IP, 'c', 'p') \ diff --git a/lib/net.h b/lib/net.h index ad4000fd..0cd5f735 100644 --- a/lib/net.h +++ b/lib/net.h @@ -268,6 +268,8 @@ static inline int net_is_roa(const net_addr *a) static inline int net_is_flow(const net_addr *a) { return (a->type == NET_FLOW4) || (a->type == NET_FLOW6); } +static inline int net_is_sadr(const net_addr *a) +{ return (a->type == NET_IP6_SADR); } static inline ip4_addr net4_prefix(const net_addr *a) { return ((net_addr_ip4 *) a)->prefix; } -- 2.17.0
On Sun, May 13, 2018 at 10:59:51PM +0200, Toke Høiland-Jørgensen wrote:
This adds a new attribute to expose the source prefix length in the same way as the destination prefix length, via a new .srclen attribute.
Hi I generalized the patch and merged that - instead of 'srclen' it adds 'src' operator that returns source prefix, so it is possible to use net.src.len, net.src.ip, or just net.src in expressions. Also note that new filter operators must be also added to i_same() function. -- Elen sila lumenn' omentielvo Ondrej 'Santiago' Zajicek (email: santiago@crfreenet.org) OpenPGP encrypted e-mails preferred (KeyID 0x11DEADC3, wwwkeys.pgp.net) "To err is human -- to blame it on a computer is even more so."
Ondrej Zajicek <santiago@crfreenet.org> writes:
On Sun, May 13, 2018 at 10:59:51PM +0200, Toke Høiland-Jørgensen wrote:
This adds a new attribute to expose the source prefix length in the same way as the destination prefix length, via a new .srclen attribute.
Hi
I generalized the patch and merged that - instead of 'srclen' it adds 'src' operator that returns source prefix, so it is possible to use net.src.len, net.src.ip, or just net.src in expressions.
Ah, yes, that is better of course. Thanks!
Also note that new filter operators must be also added to i_same() function.
Noted :) -Toke
participants (2)
-
Ondrej Zajicek -
Toke Høiland-Jørgensen