Skip to content

Commit

Permalink
Merge pull request #9358 from ton31337/fix/dont_convert_by_default_to…
Browse files Browse the repository at this point in the history
…_alias_8.0

bgpd: [8.0] Fix bgp routes filtering by [l]community-list
  • Loading branch information
idryzhov authored Aug 17, 2021
2 parents 32c6c75 + 669026b commit 1ef61cc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
25 changes: 17 additions & 8 deletions bgpd/bgp_clist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -548,6 +549,8 @@ static bool community_regexp_include(regex_t *reg, struct community *com, int i)
static bool community_regexp_match(struct community *com, regex_t *reg)
{
const char *str;
char *regstr;
int rv;

/* When there is no communities attribute it is treated as empty
string. */
Expand All @@ -556,12 +559,14 @@ static bool community_regexp_match(struct community *com, regex_t *reg)
else
str = community_str(com, false);

regstr = bgp_alias2community_str(str);

/* Regular expression match. */
if (regexec(reg, str, 0, NULL, 0) == 0)
return true;
rv = regexec(reg, regstr, 0, NULL, 0);

/* No match. */
return false;
XFREE(MTYPE_TMP, regstr);

return rv == 0;
}

static char *lcommunity_str_get(struct lcommunity *lcom, int i)
Expand Down Expand Up @@ -618,6 +623,8 @@ static bool lcommunity_regexp_include(regex_t *reg, struct lcommunity *lcom,
static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
{
const char *str;
char *regstr;
int rv;

/* When there is no communities attribute it is treated as empty
string. */
Expand All @@ -626,12 +633,14 @@ static bool lcommunity_regexp_match(struct lcommunity *com, regex_t *reg)
else
str = lcommunity_str(com, false);

regstr = bgp_alias2community_str(str);

/* Regular expression match. */
if (regexec(reg, str, 0, NULL, 0) == 0)
return true;
rv = regexec(reg, regstr, 0, NULL, 0);

/* No match. */
return false;
XFREE(MTYPE_TMP, regstr);

return rv == 0;
}


Expand Down
43 changes: 43 additions & 0 deletions bgpd/bgp_community_alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "memory.h"
#include "lib/jhash.h"
#include "frrstr.h"

#include "bgpd/bgpd.h"
#include "bgpd/bgp_community_alias.h"
Expand Down Expand Up @@ -152,3 +153,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.
*/
char *bgp_alias2community_str(const char *str)
{
char **aliases;
char *comstr;
int num, i;

frrstr_split(str, " ", &aliases, &num);
const char *communities[num];

for (i = 0; i < num; i++)
communities[i] = bgp_alias2community(aliases[i]);

comstr = frrstr_join(communities, num, " ");

for (i = 0; i < num; i++)
XFREE(MTYPE_TMP, aliases[i]);
XFREE(MTYPE_TMP, aliases);

return comstr;
}
2 changes: 2 additions & 0 deletions bgpd/bgp_community_alias.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,7 @@ 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 char *bgp_alias2community_str(const char *str);

#endif /* FRR_BGP_COMMUNITY_ALIAS_H */

0 comments on commit 1ef61cc

Please sign in to comment.