[PATCH] filter: Add .srclen attribute to filter on source prefix length

Toke Høiland-Jørgensen toke at toke.dk
Sun May 13 22:59:51 CEST 2018


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 at 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



More information about the Bird-users mailing list