]> git.proxmox.com Git - mirror_frr.git/blobdiff - bgpd/bgp_clist.c
bgpd: Validate large-community-list against UINT_MAX
[mirror_frr.git] / bgpd / bgp_clist.c
index 7cf14775496cdb2bb6e8f0cc9eb04f2c5bb20b31..837caca41e90720e666c623a43260e0dbc4ed64c 100644 (file)
@@ -26,6 +26,8 @@
 #include "queue.h"
 #include "filter.h"
 #include "stream.h"
+#include "jhash.h"
+#include "frrstr.h"
 
 #include "bgpd/bgpd.h"
 #include "bgpd/bgp_community.h"
 #include "bgpd/bgp_regex.h"
 #include "bgpd/bgp_clist.h"
 
+static uint32_t bgp_clist_hash_key_community_list(void *data)
+{
+       struct community_list *cl = data;
+
+       if (cl->name_hash)
+               return cl->name_hash;
+
+       cl->name_hash = bgp_clist_hash_key(cl->name);
+       return cl->name_hash;
+}
+
+static bool bgp_clist_hash_cmp_community_list(const void *a1, const void *a2)
+{
+       const struct community_list *cl1 = a1;
+       const struct community_list *cl2 = a2;
+
+       if (cl1->name_hash != cl2->name_hash)
+               return false;
+
+       if (strcmp(cl1->name, cl2->name) == 0)
+               return true;
+
+       return false;
+}
+
 /* Lookup master structure for community-list or
    extcommunity-list.  */
 struct community_list_master *
@@ -65,7 +92,7 @@ static void community_entry_free(struct community_entry *entry)
        switch (entry->style) {
        case COMMUNITY_LIST_STANDARD:
                if (entry->u.com)
-                       community_free(entry->u.com);
+                       community_free(&entry->u.com);
                break;
        case LARGE_COMMUNITY_LIST_STANDARD:
                if (entry->u.lcom)
@@ -74,16 +101,14 @@ static void community_entry_free(struct community_entry *entry)
        case EXTCOMMUNITY_LIST_STANDARD:
                /* In case of standard extcommunity-list, configuration string
                   is made by ecommunity_ecom2str().  */
-               if (entry->config)
-                       XFREE(MTYPE_ECOMMUNITY_STR, entry->config);
+               XFREE(MTYPE_ECOMMUNITY_STR, entry->config);
                if (entry->u.ecom)
                        ecommunity_free(&entry->u.ecom);
                break;
        case COMMUNITY_LIST_EXPANDED:
        case EXTCOMMUNITY_LIST_EXPANDED:
        case LARGE_COMMUNITY_LIST_EXPANDED:
-               if (entry->config)
-                       XFREE(MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
+               XFREE(MTYPE_COMMUNITY_LIST_CONFIG, entry->config);
                if (entry->reg)
                        bgp_regex_free(entry->reg);
        default:
@@ -101,8 +126,7 @@ static struct community_list *community_list_new(void)
 /* Free community-list.  */
 static void community_list_free(struct community_list *list)
 {
-       if (list->name)
-               XFREE(MTYPE_COMMUNITY_LIST_NAME, list->name);
+       XFREE(MTYPE_COMMUNITY_LIST_NAME, list->name);
        XFREE(MTYPE_COMMUNITY_LIST, list);
 }
 
@@ -125,6 +149,10 @@ community_list_insert(struct community_list_handler *ch, const char *name,
        /* Allocate new community_list and copy given name. */
        new = community_list_new();
        new->name = XSTRDUP(MTYPE_COMMUNITY_LIST_NAME, name);
+       new->name_hash = bgp_clist_hash_key_community_list(new);
+
+       /* Save for later */
+       hash_get(cm->hash, new, hash_alloc_intern);
 
        /* If name is made by all digit character.  We treat it as
           number. */
@@ -194,9 +222,11 @@ community_list_insert(struct community_list_handler *ch, const char *name,
 }
 
 struct community_list *community_list_lookup(struct community_list_handler *ch,
-                                            const char *name, int master)
+                                            const char *name,
+                                            uint32_t name_hash,
+                                            int master)
 {
-       struct community_list *list;
+       struct community_list lookup;
        struct community_list_master *cm;
 
        if (!name)
@@ -206,14 +236,9 @@ struct community_list *community_list_lookup(struct community_list_handler *ch,
        if (!cm)
                return NULL;
 
-       for (list = cm->num.head; list; list = list->next)
-               if (strcmp(list->name, name) == 0)
-                       return list;
-       for (list = cm->str.head; list; list = list->next)
-               if (strcmp(list->name, name) == 0)
-                       return list;
-
-       return NULL;
+       lookup.name = (char *)name;
+       lookup.name_hash = name_hash;
+       return hash_get(cm->hash, &lookup, NULL);
 }
 
 static struct community_list *
@@ -222,13 +247,14 @@ community_list_get(struct community_list_handler *ch, const char *name,
 {
        struct community_list *list;
 
-       list = community_list_lookup(ch, name, master);
+       list = community_list_lookup(ch, name, 0, master);
        if (!list)
                list = community_list_insert(ch, name, master);
        return list;
 }
 
-static void community_list_delete(struct community_list *list)
+static void community_list_delete(struct community_list_master *cm,
+                                 struct community_list *list)
 {
        struct community_list_list *clist;
        struct community_entry *entry, *next;
@@ -250,6 +276,7 @@ static void community_list_delete(struct community_list *list)
        else
                clist->head = list->next;
 
+       hash_release(cm->hash, list);
        community_list_free(list);
 }
 
@@ -273,9 +300,9 @@ static void community_list_entry_add(struct community_list *list,
 }
 
 /* Delete community-list entry from the list.  */
-static void community_list_entry_delete(struct community_list *list,
-                                       struct community_entry *entry,
-                                       int style)
+static void community_list_entry_delete(struct community_list_master *cm,
+                                       struct community_list *list,
+                                       struct community_entry *entry)
 {
        if (entry->next)
                entry->next->prev = entry->prev;
@@ -290,7 +317,7 @@ static void community_list_entry_delete(struct community_list *list,
        community_entry_free(entry);
 
        if (community_list_empty_p(list))
-               community_list_delete(list);
+               community_list_delete(cm, list);
 }
 
 /* Lookup community-list entry from the list.  */
@@ -333,71 +360,70 @@ community_list_entry_lookup(struct community_list *list, const void *arg,
 
 static char *community_str_get(struct community *com, int i)
 {
-       int len;
        uint32_t comval;
        uint16_t as;
        uint16_t val;
        char *str;
-       char *pnt;
 
        memcpy(&comval, com_nthval(com, i), sizeof(uint32_t));
        comval = ntohl(comval);
 
        switch (comval) {
        case COMMUNITY_INTERNET:
-               len = strlen(" internet");
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "internet");
                break;
-       case COMMUNITY_NO_EXPORT:
-               len = strlen(" no-export");
+       case COMMUNITY_GSHUT:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "graceful-shutdown");
                break;
-       case COMMUNITY_NO_ADVERTISE:
-               len = strlen(" no-advertise");
+       case COMMUNITY_ACCEPT_OWN:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "accept-own");
                break;
-       case COMMUNITY_LOCAL_AS:
-               len = strlen(" local-AS");
+       case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR,
+                             "route-filter-translated-v4");
                break;
-       case COMMUNITY_GSHUT:
-               len = strlen(" graceful-shutdown");
+       case COMMUNITY_ROUTE_FILTER_v4:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "route-filter-v4");
                break;
-       default:
-               len = strlen(" 65536:65535");
+       case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR,
+                             "route-filter-translated-v6");
                break;
-       }
-
-       /* Allocate memory.  */
-       str = pnt = XMALLOC(MTYPE_COMMUNITY_STR, len);
-
-       switch (comval) {
-       case COMMUNITY_INTERNET:
-               strcpy(pnt, "internet");
-               pnt += strlen("internet");
+       case COMMUNITY_ROUTE_FILTER_v6:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "route-filter-v6");
+               break;
+       case COMMUNITY_LLGR_STALE:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "llgr-stale");
+               break;
+       case COMMUNITY_NO_LLGR:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-llgr");
+               break;
+       case COMMUNITY_ACCEPT_OWN_NEXTHOP:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "accept-own-nexthop");
+               break;
+       case COMMUNITY_BLACKHOLE:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "blackhole");
                break;
        case COMMUNITY_NO_EXPORT:
-               strcpy(pnt, "no-export");
-               pnt += strlen("no-export");
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-export");
                break;
        case COMMUNITY_NO_ADVERTISE:
-               strcpy(pnt, "no-advertise");
-               pnt += strlen("no-advertise");
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-advertise");
                break;
        case COMMUNITY_LOCAL_AS:
-               strcpy(pnt, "local-AS");
-               pnt += strlen("local-AS");
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "local-AS");
                break;
-       case COMMUNITY_GSHUT:
-               strcpy(pnt, "graceful-shutdown");
-               pnt += strlen("graceful-shutdown");
+       case COMMUNITY_NO_PEER:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "no-peer");
                break;
        default:
+               str = XSTRDUP(MTYPE_COMMUNITY_STR, "65536:65535");
                as = (comval >> 16) & 0xFFFF;
                val = comval & 0xFFFF;
-               sprintf(pnt, "%u:%d", as, val);
-               pnt += strlen(pnt);
+               snprintf(str, strlen(str), "%u:%d", as, val);
                break;
        }
 
-       *pnt = '\0';
-
        return str;
 }
 
@@ -548,47 +574,77 @@ static int ecommunity_regexp_match(struct ecommunity *ecom, regex_t *reg)
 static struct community *
 community_regexp_delete (struct community *com, regex_t * reg)
 {
-  int i;
-  uint32_t comval;
-  /* Maximum is "65535:65535" + '\0'. */
-  char c[12];
-  const char *str;
-
-  if (!com)
-    return NULL;
-
-  i = 0;
-  while (i < com->size)
-    {
-      memcpy (&comval, com_nthval (com, i), sizeof (uint32_t));
-      comval = ntohl (comval);
-
-      switch (comval)
-        {
-        case COMMUNITY_INTERNET:
-          str = "internet";
-          break;
-        case COMMUNITY_NO_EXPORT:
-          str = "no-export";
-          break;
-        case COMMUNITY_NO_ADVERTISE:
-          str = "no-advertise";
-          break;
-        case COMMUNITY_LOCAL_AS:
-          str = "local-AS";
-          break;
-        default:
-          sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF, comval & 0xFFFF);
-          str = c;
-          break;
-        }
-
-      if (regexec (reg, str, 0, NULL, 0) == 0)
-        community_del_val (com, com_nthval (com, i));
-      else
-        i++;
-    }
-  return com;
+       int i;
+       uint32_t comval;
+       /* Maximum is "65535:65535" + '\0'. */
+       char c[12];
+       const char *str;
+
+       if (!com)
+               return NULL;
+
+       i = 0;
+       while (i < com->size)
+       {
+               memcpy (&comval, com_nthval (com, i), sizeof (uint32_t));
+               comval = ntohl (comval);
+
+               switch (comval) {
+               case COMMUNITY_INTERNET:
+                       str = "internet";
+                       break;
+               case COMMUNITY_ACCEPT_OWN:
+                       str = "accept-own";
+                       break;
+               case COMMUNITY_ROUTE_FILTER_TRANSLATED_v4:
+                       str = "route-filter-translated-v4";
+                       break;
+               case COMMUNITY_ROUTE_FILTER_v4:
+                       str = "route-filter-v4";
+                       break;
+               case COMMUNITY_ROUTE_FILTER_TRANSLATED_v6:
+                       str = "route-filter-translated-v6";
+                       break;
+               case COMMUNITY_ROUTE_FILTER_v6:
+                       str = "route-filter-v6";
+                       break;
+               case COMMUNITY_LLGR_STALE:
+                       str = "llgr-stale";
+                       break;
+               case COMMUNITY_NO_LLGR:
+                       str = "no-llgr";
+                       break;
+               case COMMUNITY_ACCEPT_OWN_NEXTHOP:
+                       str = "accept-own-nexthop";
+                       break;
+               case COMMUNITY_BLACKHOLE:
+                       str = "blackhole";
+                       break;
+               case COMMUNITY_NO_EXPORT:
+                       str = "no-export";
+                       break;
+               case COMMUNITY_NO_ADVERTISE:
+                       str = "no-advertise";
+                       break;
+               case COMMUNITY_LOCAL_AS:
+                       str = "local-AS";
+                       break;
+               case COMMUNITY_NO_PEER:
+                       str = "no-peer";
+                       break;
+               default:
+                       sprintf (c, "%d:%d", (comval >> 16) & 0xFFFF,
+                        comval & 0xFFFF);
+                       str = c;
+                       break;
+               }
+
+               if (regexec (reg, str, 0, NULL, 0) == 0)
+                       community_del_val (com, com_nthval (com, i));
+               else
+                       i++;
+       }
+       return com;
 }
 #endif
 
@@ -854,18 +910,20 @@ int community_list_set(struct community_list_handler *ch, const char *name,
 int community_list_unset(struct community_list_handler *ch, const char *name,
                         const char *str, int direct, int style)
 {
+       struct community_list_master *cm = NULL;
        struct community_entry *entry = NULL;
        struct community_list *list;
        struct community *com = NULL;
 
        /* Lookup community list.  */
-       list = community_list_lookup(ch, name, COMMUNITY_LIST_MASTER);
+       list = community_list_lookup(ch, name, 0, COMMUNITY_LIST_MASTER);
        if (list == NULL)
                return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
 
+       cm = community_list_master_lookup(ch, COMMUNITY_LIST_MASTER);
        /* Delete all of entry belongs to this community-list.  */
        if (!str) {
-               community_list_delete(list);
+               community_list_delete(cm, list);
                route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
                return 0;
        }
@@ -875,14 +933,14 @@ int community_list_unset(struct community_list_handler *ch, const char *name,
 
        if (com) {
                entry = community_list_entry_lookup(list, com, direct);
-               community_free(com);
+               community_free(&com);
        } else
                entry = community_list_entry_lookup(list, str, direct);
 
        if (!entry)
                return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
 
-       community_list_entry_delete(list, entry, style);
+       community_list_entry_delete(cm, list, entry);
        route_map_notify_dependencies(name, RMAP_EVENT_CLIST_DELETED);
 
        return 0;
@@ -943,6 +1001,33 @@ struct lcommunity *lcommunity_list_match_delete(struct lcommunity *lcom,
        return lcom;
 }
 
+/* Helper to check if every octet do not exceed UINT_MAX */
+static int lcommunity_list_valid(const char *community)
+{
+       int octets = 0;
+       char **splits;
+       int num;
+
+       frrstr_split(community, ":", &splits, &num);
+
+       for (int i = 0; i < num; i++) {
+               if (strtoul(splits[i], NULL, 10) > UINT_MAX)
+                       return 0;
+
+               if (strlen(splits[i]) == 0)
+                       return 0;
+
+               octets++;
+               XFREE(MTYPE_TMP, splits[i]);
+       }
+       XFREE(MTYPE_TMP, splits);
+
+       if (octets < 3)
+               return 0;
+
+       return 1;
+}
+
 /* Set lcommunity-list.  */
 int lcommunity_list_set(struct community_list_handler *ch, const char *name,
                        const char *str, int direct, int style)
@@ -971,6 +1056,9 @@ int lcommunity_list_set(struct community_list_handler *ch, const char *name,
        }
 
        if (str) {
+               if (!lcommunity_list_valid(str))
+                       return COMMUNITY_LIST_ERR_MALFORMED_VAL;
+
                if (style == LARGE_COMMUNITY_LIST_STANDARD)
                        lcom = lcommunity_str2com(str);
                else
@@ -992,8 +1080,10 @@ int lcommunity_list_set(struct community_list_handler *ch, const char *name,
        /* Do not put duplicated community entry.  */
        if (community_list_dup_check(list, entry))
                community_entry_free(entry);
-       else
+       else {
                community_list_entry_add(list, entry);
+               route_map_notify_dependencies(name, RMAP_EVENT_LLIST_ADDED);
+       }
 
        return 0;
 }
@@ -1003,19 +1093,22 @@ int lcommunity_list_set(struct community_list_handler *ch, const char *name,
 int lcommunity_list_unset(struct community_list_handler *ch, const char *name,
                          const char *str, int direct, int style)
 {
+       struct community_list_master *cm = NULL;
        struct community_entry *entry = NULL;
        struct community_list *list;
        struct lcommunity *lcom = NULL;
        regex_t *regex = NULL;
 
        /* Lookup community list.  */
-       list = community_list_lookup(ch, name, LARGE_COMMUNITY_LIST_MASTER);
+       list = community_list_lookup(ch, name, 0, LARGE_COMMUNITY_LIST_MASTER);
        if (list == NULL)
                return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
 
+       cm = community_list_master_lookup(ch, LARGE_COMMUNITY_LIST_MASTER);
        /* Delete all of entry belongs to this community-list.  */
        if (!str) {
-               community_list_delete(list);
+               community_list_delete(cm, list);
+               route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
                return 0;
        }
 
@@ -1040,7 +1133,8 @@ int lcommunity_list_unset(struct community_list_handler *ch, const char *name,
        if (!entry)
                return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
 
-       community_list_entry_delete(list, entry, style);
+       community_list_entry_delete(cm, list, entry);
+       route_map_notify_dependencies(name, RMAP_EVENT_LLIST_DELETED);
 
        return 0;
 }
@@ -1054,7 +1148,8 @@ int extcommunity_list_set(struct community_list_handler *ch, const char *name,
        struct ecommunity *ecom = NULL;
        regex_t *regex = NULL;
 
-       entry = NULL;
+       if (str == NULL)
+               return COMMUNITY_LIST_ERR_MALFORMED_VAL;
 
        /* Get community list. */
        list = community_list_get(ch, name, EXTCOMMUNITY_LIST_MASTER);
@@ -1089,7 +1184,7 @@ int extcommunity_list_set(struct community_list_handler *ch, const char *name,
        entry = community_entry_new();
        entry->direct = direct;
        entry->style = style;
-       entry->any = (str ? 0 : 1);
+       entry->any = 0;
        if (ecom)
                entry->config = ecommunity_ecom2str(
                        ecom, ECOMMUNITY_FORMAT_COMMUNITY_LIST, 0);
@@ -1118,18 +1213,20 @@ int extcommunity_list_set(struct community_list_handler *ch, const char *name,
 int extcommunity_list_unset(struct community_list_handler *ch, const char *name,
                            const char *str, int direct, int style)
 {
+       struct community_list_master *cm = NULL;
        struct community_entry *entry = NULL;
        struct community_list *list;
        struct ecommunity *ecom = NULL;
 
        /* Lookup extcommunity list.  */
-       list = community_list_lookup(ch, name, EXTCOMMUNITY_LIST_MASTER);
+       list = community_list_lookup(ch, name, 0, EXTCOMMUNITY_LIST_MASTER);
        if (list == NULL)
                return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
 
+       cm = community_list_master_lookup(ch, EXTCOMMUNITY_LIST_MASTER);
        /* Delete all of entry belongs to this extcommunity-list.  */
        if (!str) {
-               community_list_delete(list);
+               community_list_delete(cm, list);
                route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
                return 0;
        }
@@ -1146,7 +1243,7 @@ int extcommunity_list_unset(struct community_list_handler *ch, const char *name,
        if (!entry)
                return COMMUNITY_LIST_ERR_CANT_FIND_LIST;
 
-       community_list_entry_delete(list, entry, style);
+       community_list_entry_delete(cm, list, entry);
        route_map_notify_dependencies(name, RMAP_EVENT_ECLIST_DELETED);
 
        return 0;
@@ -1158,6 +1255,22 @@ struct community_list_handler *community_list_init(void)
        struct community_list_handler *ch;
        ch = XCALLOC(MTYPE_COMMUNITY_LIST_HANDLER,
                     sizeof(struct community_list_handler));
+
+       ch->community_list.hash =
+               hash_create_size(4, bgp_clist_hash_key_community_list,
+                                bgp_clist_hash_cmp_community_list,
+                                "Community List Number Quick Lookup");
+
+       ch->extcommunity_list.hash =
+               hash_create_size(4, bgp_clist_hash_key_community_list,
+                                bgp_clist_hash_cmp_community_list,
+                                "Extended Community List Quick Lookup");
+
+       ch->lcommunity_list.hash =
+               hash_create_size(4, bgp_clist_hash_key_community_list,
+                                bgp_clist_hash_cmp_community_list,
+                                "Large Community List Quick Lookup");
+
        return ch;
 }
 
@@ -1169,21 +1282,24 @@ void community_list_terminate(struct community_list_handler *ch)
 
        cm = &ch->community_list;
        while ((list = cm->num.head) != NULL)
-               community_list_delete(list);
+               community_list_delete(cm, list);
        while ((list = cm->str.head) != NULL)
-               community_list_delete(list);
+               community_list_delete(cm, list);
+       hash_free(cm->hash);
 
        cm = &ch->lcommunity_list;
        while ((list = cm->num.head) != NULL)
-               community_list_delete(list);
+               community_list_delete(cm, list);
        while ((list = cm->str.head) != NULL)
-               community_list_delete(list);
+               community_list_delete(cm, list);
+       hash_free(cm->hash);
 
        cm = &ch->extcommunity_list;
        while ((list = cm->num.head) != NULL)
-               community_list_delete(list);
+               community_list_delete(cm, list);
        while ((list = cm->str.head) != NULL)
-               community_list_delete(list);
+               community_list_delete(cm, list);
+       hash_free(cm->hash);
 
        XFREE(MTYPE_COMMUNITY_LIST_HANDLER, ch);
 }