]> git.puffer.fish Git - matthieu/frr.git/commitdiff
bgpd: BGP extended [l]community-list regexp match must work with aliases
authorDonatas Abraitis <donatas.abraitis@gmail.com>
Mon, 9 Aug 2021 10:34:57 +0000 (13:34 +0300)
committerDonatas Abraitis <donatas.abraitis@gmail.com>
Tue, 10 Aug 2021 06:46:15 +0000 (09:46 +0300)
We have to convert BGP alias to numerical format to compare in regexp.

Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
bgpd/bgp_clist.c
bgpd/bgp_community_alias.c
bgpd/bgp_community_alias.h

index 50122ad7da6cb3edac230e53c699993fb05bb14e..fd8f51fed379262cab928050d6546794855defa0 100644 (file)
@@ -33,6 +33,7 @@
 #include "bgpd/bgp_community.h"
 #include "bgpd/bgp_ecommunity.h"
 #include "bgpd/bgp_lcommunity.h"
+#include "bgpd/bgp_community_alias.h"
 #include "bgpd/bgp_aspath.h"
 #include "bgpd/bgp_regex.h"
 #include "bgpd/bgp_clist.h"
@@ -557,7 +558,7 @@ static bool community_regexp_match(struct community *com, regex_t *reg)
                str = community_str(com, false);
 
        /* Regular expression match.  */
-       if (regexec(reg, str, 0, NULL, 0) == 0)
+       if (regexec(reg, bgp_alias2community_str(str), 0, NULL, 0) == 0)
                return true;
 
        /* No match.  */
@@ -627,7 +628,7 @@ static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
                str = lcommunity_str(com, false);
 
        /* Regular expression match.  */
-       if (regexec(reg, str, 0, NULL, 0) == 0)
+       if (regexec(reg, bgp_alias2community_str(str), 0, NULL, 0) == 0)
                return true;
 
        /* No match.  */
index f770ebdd5d9646bf34780954f9b6f920be9d2461..5f45e19a3b73d4ca35e9771e674f2979b432fae2 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "memory.h"
 #include "lib/jhash.h"
+#include "frrstr.h"
 
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_community_alias.h"
@@ -153,6 +154,45 @@ const char *bgp_community2alias(char *community)
        return community;
 }
 
+const char *bgp_alias2community(char *alias)
+{
+       struct community_alias ca;
+       struct community_alias *find;
+
+       memset(&ca, 0, sizeof(ca));
+       strlcpy(ca.alias, alias, sizeof(ca.alias));
+
+       find = bgp_ca_alias_lookup(&ca);
+       if (find)
+               return find->community;
+
+       return alias;
+}
+
+/* Communities structs have `->str` which is used
+ * for vty outputs and extended BGP community lists
+ * with regexp.
+ * This is a helper to convert already aliased version
+ * of communities into numerical-only format.
+ */
+const char *bgp_alias2community_str(const char *str)
+{
+       char **aliases;
+       int num;
+
+       frrstr_split(str, " ", &aliases, &num);
+       const char *communities[num + 1];
+
+       for (int i = 0; i < num; i++) {
+               communities[i] =
+                       XSTRDUP(MTYPE_TMP, bgp_alias2community(aliases[i]));
+               XFREE(MTYPE_TMP, aliases[i]);
+       }
+       XFREE(MTYPE_TMP, aliases);
+
+       return frrstr_join(communities, num, " ");
+}
+
 static int bgp_community_alias_vector_walker(struct hash_bucket *bucket,
                                             void *data)
 {
index ab8ed06ee6c6e351a5f740ddda44535ce9669c66..fc9eb9f9e4bacf69a2abf5a7a8ff022819eaa54b 100644 (file)
@@ -42,6 +42,8 @@ extern void bgp_ca_community_delete(struct community_alias *ca);
 extern void bgp_ca_alias_delete(struct community_alias *ca);
 extern int bgp_community_alias_write(struct vty *vty);
 extern const char *bgp_community2alias(char *community);
+extern const char *bgp_alias2community(char *alias);
+extern const char *bgp_alias2community_str(const char *str);
 extern void bgp_community_alias_command_completion_setup(void);
 
 #endif /* FRR_BGP_COMMUNITY_ALIAS_H */