]> git.proxmox.com Git - mirror_frr.git/commitdiff
bgpd: Add an ability to match ipv6 next-hop by prefix-list
authorDonatas Abraitis <donatas.abraitis@gmail.com>
Wed, 24 Nov 2021 14:28:31 +0000 (16:28 +0200)
committerDonatas Abraitis <donatas.abraitis@gmail.com>
Wed, 24 Nov 2021 14:28:31 +0000 (16:28 +0200)
Signed-off-by: Donatas Abraitis <donatas.abraitis@gmail.com>
bgpd/bgp_routemap.c
lib/routemap.c
lib/routemap.h
lib/routemap_cli.c
lib/routemap_northbound.c
yang/frr-route-map.yang

index b2eb70a9cca3ea6a8e82b72d99541d94b30a6a0c..2a1dc1896cc1412b70e8f3ddd2e9592130855979 100644 (file)
@@ -725,6 +725,57 @@ static const struct route_map_rule_cmd
        route_match_ip_next_hop_prefix_list_free
 };
 
+/* `match ipv6 next-hop prefix-list PREFIXLIST_NAME' */
+static enum route_map_cmd_result_t
+route_match_ipv6_next_hop_prefix_list(void *rule, const struct prefix *prefix,
+                                     void *object)
+{
+       struct prefix_list *plist;
+       struct bgp_path_info *path;
+       struct prefix_ipv6 p;
+
+       if (prefix->family == AF_INET6) {
+               path = object;
+               p.family = AF_INET6;
+               p.prefix = path->attr->mp_nexthop_global;
+               p.prefixlen = IPV6_MAX_BITLEN;
+
+               plist = prefix_list_lookup(AFI_IP6, (char *)rule);
+               if (!plist)
+                       return RMAP_NOMATCH;
+
+               if (prefix_list_apply(plist, &p) == PREFIX_PERMIT)
+                       return RMAP_MATCH;
+
+               if (path->attr->mp_nexthop_len
+                   == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL) {
+                       p.prefix = path->attr->mp_nexthop_local;
+                       if (prefix_list_apply(plist, &p) == PREFIX_PERMIT)
+                               return RMAP_MATCH;
+               }
+       }
+
+       return RMAP_NOMATCH;
+}
+
+static void *route_match_ipv6_next_hop_prefix_list_compile(const char *arg)
+{
+       return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
+}
+
+static void route_match_ipv6_next_hop_prefix_list_free(void *rule)
+{
+       XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
+}
+
+static const struct route_map_rule_cmd
+               route_match_ipv6_next_hop_prefix_list_cmd = {
+       "ipv6 next-hop prefix-list",
+       route_match_ipv6_next_hop_prefix_list,
+       route_match_ipv6_next_hop_prefix_list_compile,
+       route_match_ipv6_next_hop_prefix_list_free
+};
+
 /* `match ip next-hop type <blackhole>' */
 
 static enum route_map_cmd_result_t
@@ -6189,6 +6240,45 @@ ALIAS_HIDDEN (no_match_ipv6_next_hop_address,
              "Match IPv6 next-hop address of route\n"
              "IPv6 address of next hop\n")
 
+DEFUN_YANG (match_ipv6_next_hop_prefix_list,
+           match_ipv6_next_hop_prefix_list_cmd,
+           "match ipv6 next-hop prefix-list PREFIXLIST_NAME",
+           MATCH_STR
+           IPV6_STR
+           "Match IPv6 next-hop address of route\n"
+           "Match entries by prefix-list\n"
+           "IPv6 prefix-list name\n")
+{
+       const char *xpath =
+               "./match-condition[condition='frr-route-map:ipv6-next-hop-prefix-list']";
+       char xpath_value[XPATH_MAXLEN];
+
+       nb_cli_enqueue_change(vty, xpath, NB_OP_CREATE, NULL);
+       snprintf(xpath_value, sizeof(xpath_value),
+                "%s/rmap-match-condition/list-name", xpath);
+       nb_cli_enqueue_change(vty, xpath_value, NB_OP_MODIFY,
+                             argv[argc - 1]->arg);
+
+       return nb_cli_apply_changes(vty, NULL);
+}
+
+DEFUN_YANG (no_match_ipv6_next_hop_prefix_list,
+           no_match_ipv6_next_hop_prefix_list_cmd,
+           "no match ipv6 next-hop prefix-list [PREFIXLIST_NAME]",
+           NO_STR
+           MATCH_STR
+           IPV6_STR
+           "Match IPv6 next-hop address of route\n"
+           "Match entries by prefix-list\n"
+           "IPv6 prefix-list name\n")
+{
+       const char *xpath =
+               "./match-condition[condition='frr-route-map:ipv6-next-hop-prefix-list']";
+
+       nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
+       return nb_cli_apply_changes(vty, NULL);
+}
+
 DEFPY_YANG (match_ipv4_next_hop,
        match_ipv4_next_hop_cmd,
        "match ip next-hop address A.B.C.D",
@@ -6566,6 +6656,9 @@ void bgp_route_map_init(void)
        route_map_match_ipv6_next_hop_type_hook(generic_match_add);
        route_map_no_match_ipv6_next_hop_type_hook(generic_match_delete);
 
+       route_map_match_ipv6_next_hop_prefix_list_hook(generic_match_add);
+       route_map_no_match_ipv6_next_hop_prefix_list_hook(generic_match_delete);
+
        route_map_match_metric_hook(generic_match_add);
        route_map_no_match_metric_hook(generic_match_delete);
 
@@ -6749,6 +6842,7 @@ void bgp_route_map_init(void)
        route_map_install_match(&route_match_ipv6_address_cmd);
        route_map_install_match(&route_match_ipv6_next_hop_cmd);
        route_map_install_match(&route_match_ipv6_next_hop_address_cmd);
+       route_map_install_match(&route_match_ipv6_next_hop_prefix_list_cmd);
        route_map_install_match(&route_match_ipv4_next_hop_cmd);
        route_map_install_match(&route_match_ipv6_address_prefix_list_cmd);
        route_map_install_match(&route_match_ipv6_next_hop_type_cmd);
@@ -6759,8 +6853,10 @@ void bgp_route_map_init(void)
 
        install_element(RMAP_NODE, &match_ipv6_next_hop_cmd);
        install_element(RMAP_NODE, &match_ipv6_next_hop_address_cmd);
+       install_element(RMAP_NODE, &match_ipv6_next_hop_prefix_list_cmd);
        install_element(RMAP_NODE, &no_match_ipv6_next_hop_cmd);
        install_element(RMAP_NODE, &no_match_ipv6_next_hop_address_cmd);
+       install_element(RMAP_NODE, &no_match_ipv6_next_hop_prefix_list_cmd);
        install_element(RMAP_NODE, &match_ipv6_next_hop_old_cmd);
        install_element(RMAP_NODE, &no_match_ipv6_next_hop_old_cmd);
        install_element(RMAP_NODE, &match_ipv4_next_hop_cmd);
index c4651f39a4997944088e8e93d2d8a391f7ebaf0f..7f733c8114aaebb8aeaafc8110f9a9396d9923d3 100644 (file)
@@ -282,6 +282,22 @@ void route_map_no_match_ipv6_next_hop_type_hook(int (*func)(
        rmap_match_set_hook.no_match_ipv6_next_hop_type = func;
 }
 
+/* match ipv6 next-hop prefix-list */
+void route_map_match_ipv6_next_hop_prefix_list_hook(int (*func)(
+       struct route_map_index *index, const char *command, const char *arg,
+       route_map_event_t type, char *errmsg, size_t errmsg_len))
+{
+       rmap_match_set_hook.match_ipv6_next_hop_prefix_list = func;
+}
+
+/* no match ipv6 next-hop prefix-list */
+void route_map_no_match_ipv6_next_hop_prefix_list_hook(int (*func)(
+       struct route_map_index *index, const char *command, const char *arg,
+       route_map_event_t type, char *errmsg, size_t errmsg_len))
+{
+       rmap_match_set_hook.no_match_ipv6_next_hop_prefix_list = func;
+}
+
 /* match metric */
 void route_map_match_metric_hook(int (*func)(
        struct route_map_index *index, const char *command,
index 675f89ba92878575380683d766668f203cee17d0..6c4916898a36dd6cdf4d9401908b77896fdef19b 100644 (file)
@@ -252,6 +252,8 @@ DECLARE_QOBJ_TYPE(route_map);
        (strmatch(C, "frr-route-map:ipv6-prefix-list"))
 #define IS_MATCH_IPv4_NEXTHOP_PREFIX_LIST(C)                                   \
        (strmatch(C, "frr-route-map:ipv4-next-hop-prefix-list"))
+#define IS_MATCH_IPv6_NEXTHOP_PREFIX_LIST(C)                                   \
+       (strmatch(C, "frr-route-map:ipv6-next-hop-prefix-list"))
 #define IS_MATCH_IPv4_NEXTHOP_TYPE(C)                                          \
        (strmatch(C, "frr-route-map:ipv4-next-hop-type"))
 #define IS_MATCH_IPv6_NEXTHOP_TYPE(C)                                          \
@@ -617,6 +619,14 @@ extern void route_map_no_match_ipv6_next_hop_type_hook(int (*func)(
        struct route_map_index *index, const char *command,
        const char *arg, route_map_event_t type,
        char *errmsg, size_t errmsg_len));
+/* match ipv6 next-hop prefix-list */
+extern void route_map_match_ipv6_next_hop_prefix_list_hook(int (*func)(
+       struct route_map_index *index, const char *command, const char *arg,
+       route_map_event_t type, char *errmsg, size_t errmsg_len));
+/* no match ipv6 next-hop prefix-list */
+extern void route_map_no_match_ipv6_next_hop_prefix_list_hook(int (*func)(
+       struct route_map_index *index, const char *command, const char *arg,
+       route_map_event_t type, char *errmsg, size_t errmsg_len));
 /* match metric */
 extern void route_map_match_metric_hook(int (*func)(
        struct route_map_index *index, const char *command,
@@ -764,6 +774,21 @@ struct route_map_match_set_hooks {
                                      route_map_event_t type, char *errmsg,
                                      size_t errmsg_len);
 
+       /* match ipv6 next hop prefix-list */
+       int (*match_ipv6_next_hop_prefix_list)(struct route_map_index *index,
+                                              const char *command,
+                                              const char *arg,
+                                              route_map_event_t type,
+                                              char *errmsg, size_t errmsg_len);
+
+       /* no match ipv6 next-hop prefix-list */
+       int (*no_match_ipv6_next_hop_prefix_list)(struct route_map_index *index,
+                                                 const char *command,
+                                                 const char *arg,
+                                                 route_map_event_t type,
+                                                 char *errmsg,
+                                                 size_t errmsg_len);
+
        /* match ip next hop prefix list */
        int (*match_ip_next_hop_prefix_list)(struct route_map_index *index,
                                             const char *command,
index 552ba565b4240d73bafb3916fe450fdb4d34e850..52fcaaba53afd738f73c526287a08291c7013653 100644 (file)
@@ -575,6 +575,10 @@ void route_map_condition_show(struct vty *vty, const struct lyd_node *dnode,
                vty_out(vty, " match ip next-hop prefix-list %s\n",
                        yang_dnode_get_string(
                                dnode, "./rmap-match-condition/list-name"));
+       } else if (IS_MATCH_IPv6_NEXTHOP_PREFIX_LIST(condition)) {
+               vty_out(vty, " match ipv6 next-hop prefix-list %s\n",
+                       yang_dnode_get_string(
+                               dnode, "./rmap-match-condition/list-name"));
        } else if (IS_MATCH_IPv6_ADDRESS_LIST(condition)) {
                vty_out(vty, " match ipv6 address %s\n",
                        yang_dnode_get_string(
index 7f7bc9f9c4d1629376a1a1ced0bac67e9821c898..51b879959f625ed48ad5e498a8342f5a05aa7b74 100644 (file)
@@ -622,6 +622,16 @@ static int lib_route_map_entry_match_condition_list_name_modify(
                        rhc->rhc_rmi, "ip next-hop prefix-list", acl,
                        RMAP_EVENT_PLIST_ADDED,
                        args->errmsg, args->errmsg_len);
+       } else if (IS_MATCH_IPv6_NEXTHOP_PREFIX_LIST(condition)) {
+               if (rmap_match_set_hook.match_ipv6_next_hop_prefix_list == NULL)
+                       return NB_OK;
+               rhc->rhc_mhook =
+                       rmap_match_set_hook.no_match_ipv6_next_hop_prefix_list;
+               rhc->rhc_rule = "ipv6 next-hop prefix-list";
+               rhc->rhc_event = RMAP_EVENT_PLIST_DELETED;
+               rv = rmap_match_set_hook.match_ipv6_next_hop_prefix_list(
+                       rhc->rhc_rmi, "ipv6 next-hop prefix-list", acl,
+                       RMAP_EVENT_PLIST_ADDED, args->errmsg, args->errmsg_len);
        } else if (IS_MATCH_IPv6_ADDRESS_LIST(condition)) {
                if (rmap_match_set_hook.match_ipv6_address == NULL)
                        return NB_OK;
index fa55a8c7cb760949bcd9601ed2bcc3d9056ae82b..1e8c04bc6f9baf169911ed51ab7cd60d7bf686fc 100644 (file)
@@ -112,6 +112,12 @@ module frr-route-map {
       "Match an IPv6 next-hop";
   }
 
+  identity ipv6-next-hop-prefix-list {
+    base rmap-match-type;
+    description
+      "Match an IPv6 next-hop prefix list";
+  }
+
   identity ipv6-next-hop-type {
     base rmap-match-type;
     description
@@ -207,6 +213,7 @@ module frr-route-map {
              + "derived-from-or-self(../condition, 'ipv4-next-hop-prefix-list') or "
              + "derived-from-or-self(../condition, 'ipv6-address-list') or "
              + "derived-from-or-self(../condition, 'ipv6-next-hop-list') or "
+             + "derived-from-or-self(../condition, 'ipv6-next-hop-prefix-list') or "
              + "derived-from-or-self(../condition, 'ipv6-prefix-list')";
           leaf list-name {
             type filter:access-list-name;