1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Route map function of ospfd.
4 * Copyright (C) 2000 IP Infusion Inc.
6 * Written by Toshiaki Takada.
21 #include "northbound_cli.h"
23 #include "ospfd/ospfd.h"
24 #include "ospfd/ospf_asbr.h"
25 #include "ospfd/ospf_interface.h"
26 #include "ospfd/ospf_lsa.h"
27 #include "ospfd/ospf_route.h"
28 #include "ospfd/ospf_zebra.h"
29 #include "ospfd/ospf_errors.h"
31 /* Hook function for updating route_map assignment. */
32 static void ospf_route_map_update(const char *name
)
36 struct listnode
*n1
= NULL
;
38 /* If OSPF instatnce does not exist, return right now. */
39 if (listcount(om
->ospf
) == 0)
42 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
43 /* Update route-map */
44 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
45 struct list
*red_list
;
46 struct listnode
*node
;
47 struct ospf_redist
*red
;
49 red_list
= ospf
->redist
[type
];
53 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
54 if (ROUTEMAP_NAME(red
)
55 && strcmp(ROUTEMAP_NAME(red
), name
) == 0) {
56 /* Keep old route-map. */
57 struct route_map
*old
= ROUTEMAP(red
);
60 route_map_lookup_by_name(
64 route_map_counter_increment(
67 /* No update for this distribute type.
70 && ROUTEMAP(red
) == NULL
)
73 ospf_distribute_list_update(
74 ospf
, type
, red
->instance
);
81 static void ospf_route_map_event(const char *name
)
85 struct listnode
*n1
= NULL
;
87 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
88 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
89 struct list
*red_list
;
90 struct listnode
*node
;
91 struct ospf_redist
*red
;
93 red_list
= ospf
->redist
[type
];
97 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
98 if (ROUTEMAP_NAME(red
) && ROUTEMAP(red
)
99 && !strcmp(ROUTEMAP_NAME(red
), name
)) {
100 ospf_distribute_list_update(
101 ospf
, type
, red
->instance
);
108 /* `match ip netxthop ' */
109 /* Match function return 1 if match is success else return zero. */
110 static enum route_map_cmd_result_t
111 route_match_ip_nexthop(void *rule
, const struct prefix
*prefix
, void *object
)
113 struct access_list
*alist
;
114 struct external_info
*ei
= object
;
115 struct prefix_ipv4 p
;
118 p
.prefix
= ei
->nexthop
;
119 p
.prefixlen
= IPV4_MAX_BITLEN
;
121 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
123 if (unlikely(CHECK_FLAG(rmap_debug
, DEBUG_ROUTEMAP_DETAIL
)))
125 "%s: Access-List Specified: %s does not exist defaulting to NO_MATCH",
126 __func__
, (char *)rule
);
130 return (access_list_apply(alist
, &p
) == FILTER_DENY
? RMAP_NOMATCH
134 /* Route map `ip next-hop' match statement. `arg' should be
136 static void *route_match_ip_nexthop_compile(const char *arg
)
138 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
141 /* Free route map's compiled `ip address' value. */
142 static void route_match_ip_nexthop_free(void *rule
)
144 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
147 /* Route map commands for metric matching. */
148 static const struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
150 route_match_ip_nexthop
,
151 route_match_ip_nexthop_compile
,
152 route_match_ip_nexthop_free
155 /* `match ip next-hop prefix-list PREFIX_LIST' */
157 static enum route_map_cmd_result_t
158 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
161 struct prefix_list
*plist
;
162 struct external_info
*ei
= object
;
163 struct prefix_ipv4 p
;
166 p
.prefix
= ei
->nexthop
;
167 p
.prefixlen
= IPV4_MAX_BITLEN
;
169 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
171 if (unlikely(CHECK_FLAG(rmap_debug
, DEBUG_ROUTEMAP_DETAIL
)))
173 "%s: Prefix List %s specified does not exist defaulting to NO_MATCH",
174 __func__
, (char *)rule
);
178 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
? RMAP_NOMATCH
182 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
184 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
187 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
189 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
192 static const struct route_map_rule_cmd
193 route_match_ip_next_hop_prefix_list_cmd
= {
194 "ip next-hop prefix-list",
195 route_match_ip_next_hop_prefix_list
,
196 route_match_ip_next_hop_prefix_list_compile
,
197 route_match_ip_next_hop_prefix_list_free
200 /* `match ip next-hop type <blackhole>' */
202 static enum route_map_cmd_result_t
203 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
206 struct external_info
*ei
= object
;
208 if (prefix
->family
== AF_INET
) {
209 ei
= (struct external_info
*)object
;
213 if (ei
->nexthop
.s_addr
== INADDR_ANY
&& !ei
->ifindex
)
219 static void *route_match_ip_next_hop_type_compile(const char *arg
)
221 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
224 static void route_match_ip_next_hop_type_free(void *rule
)
226 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
229 static const struct route_map_rule_cmd
230 route_match_ip_next_hop_type_cmd
= {
232 route_match_ip_next_hop_type
,
233 route_match_ip_next_hop_type_compile
,
234 route_match_ip_next_hop_type_free
237 /* `match ip address IP_ACCESS_LIST' */
238 /* Match function should return 1 if match is success else return
240 static enum route_map_cmd_result_t
241 route_match_ip_address(void *rule
, const struct prefix
*prefix
, void *object
)
243 struct access_list
*alist
;
244 /* struct prefix_ipv4 match; */
246 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
248 if (unlikely(CHECK_FLAG(rmap_debug
, DEBUG_ROUTEMAP_DETAIL
)))
250 "%s: Access-List Specified: %s does not exist defaulting to NO_MATCH",
251 __func__
, (char *)rule
);
255 return (access_list_apply(alist
, prefix
) == FILTER_DENY
? RMAP_NOMATCH
259 /* Route map `ip address' match statement. `arg' should be
261 static void *route_match_ip_address_compile(const char *arg
)
263 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
266 /* Free route map's compiled `ip address' value. */
267 static void route_match_ip_address_free(void *rule
)
269 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
272 /* Route map commands for ip address matching. */
273 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
275 route_match_ip_address
,
276 route_match_ip_address_compile
,
277 route_match_ip_address_free
280 /* `match ip address prefix-list PREFIX_LIST' */
281 static enum route_map_cmd_result_t
282 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
285 struct prefix_list
*plist
;
287 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
289 if (unlikely(CHECK_FLAG(rmap_debug
, DEBUG_ROUTEMAP_DETAIL
)))
291 "%s: Prefix List %s specified does not exist defaulting to NO_MATCH",
292 __func__
, (char *)rule
);
297 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
? RMAP_NOMATCH
301 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
303 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
306 static void route_match_ip_address_prefix_list_free(void *rule
)
308 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
311 static const struct route_map_rule_cmd
312 route_match_ip_address_prefix_list_cmd
= {
313 "ip address prefix-list",
314 route_match_ip_address_prefix_list
,
315 route_match_ip_address_prefix_list_compile
,
316 route_match_ip_address_prefix_list_free
319 /* `match interface IFNAME' */
320 /* Match function should return 1 if match is success else return
322 static enum route_map_cmd_result_t
323 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
325 struct interface
*ifp
;
326 struct external_info
*ei
;
329 ifp
= if_lookup_by_name((char *)rule
, ei
->ospf
->vrf_id
);
331 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
337 /* Route map `interface' match statement. `arg' should be
339 static void *route_match_interface_compile(const char *arg
)
341 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
344 /* Free route map's compiled `interface' value. */
345 static void route_match_interface_free(void *rule
)
347 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
350 /* Route map commands for ip address matching. */
351 static const struct route_map_rule_cmd route_match_interface_cmd
= {
353 route_match_interface
,
354 route_match_interface_compile
,
355 route_match_interface_free
358 /* Match function return 1 if match is success else return zero. */
359 static enum route_map_cmd_result_t
360 route_match_tag(void *rule
, const struct prefix
*prefix
, void *object
)
363 struct external_info
*ei
;
368 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
371 /* Route map commands for tag matching. */
372 static const struct route_map_rule_cmd route_match_tag_cmd
= {
375 route_map_rule_tag_compile
,
376 route_map_rule_tag_free
,
380 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
385 /* `set metric METRIC' */
386 /* Set metric to attribute. */
387 static enum route_map_cmd_result_t
388 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
390 struct ospf_metric
*metric
;
391 struct external_info
*ei
;
393 /* Fetch routemap's rule information. */
397 /* Set metric out value. */
401 ROUTEMAP_METRIC(ei
) = ei
->metric
;
403 if (metric
->type
== metric_increment
)
404 ROUTEMAP_METRIC(ei
) += metric
->metric
;
405 else if (metric
->type
== metric_decrement
)
406 ROUTEMAP_METRIC(ei
) -= metric
->metric
;
407 else if (metric
->type
== metric_absolute
)
408 ROUTEMAP_METRIC(ei
) = metric
->metric
;
410 if ((uint32_t)ROUTEMAP_METRIC(ei
) < ei
->min_metric
)
411 ROUTEMAP_METRIC(ei
) = ei
->min_metric
;
412 if ((uint32_t)ROUTEMAP_METRIC(ei
) > ei
->max_metric
)
413 ROUTEMAP_METRIC(ei
) = ei
->max_metric
;
418 /* set metric compilation. */
419 static void *route_set_metric_compile(const char *arg
)
421 struct ospf_metric
*metric
;
423 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(*metric
));
424 metric
->used
= false;
427 metric
->type
= metric_absolute
;
429 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
430 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
431 "OSPF does not support 'set metric +rtt / -rtt'");
435 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
436 metric
->type
= metric_increment
;
440 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
441 metric
->type
= metric_decrement
;
445 metric
->metric
= strtoul(arg
, NULL
, 10);
453 /* Free route map's compiled `set metric' value. */
454 static void route_set_metric_free(void *rule
)
456 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
459 /* Set metric rule structure. */
460 static const struct route_map_rule_cmd route_set_metric_cmd
= {
463 route_set_metric_compile
,
464 route_set_metric_free
,
467 /* `set min-metric METRIC' */
468 /* Set min-metric to attribute. */
469 static enum route_map_cmd_result_t
470 route_set_min_metric(void *rule
, const struct prefix
*prefix
, void *object
)
472 uint32_t *min_metric
;
473 struct external_info
*ei
;
475 /* Fetch routemap's rule information. */
479 ei
->min_metric
= *min_metric
;
481 if (ei
->min_metric
> OSPF_LS_INFINITY
)
482 ei
->min_metric
= OSPF_LS_INFINITY
;
484 if ((uint32_t)ROUTEMAP_METRIC(ei
) < ei
->min_metric
)
485 ROUTEMAP_METRIC(ei
) = ei
->min_metric
;
490 /* set min-metric compilation. */
491 static void *route_set_min_metric_compile(const char *arg
)
494 uint32_t *min_metric
;
496 min_metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
498 *min_metric
= strtoul(arg
, NULL
, 10);
503 XFREE(MTYPE_ROUTE_MAP_COMPILED
, min_metric
);
507 /* Free route map's compiled `set min-metric' value. */
508 static void route_set_min_metric_free(void *rule
)
510 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
513 /* Set metric rule structure. */
514 static const struct route_map_rule_cmd route_set_min_metric_cmd
= {
516 route_set_min_metric
,
517 route_set_min_metric_compile
,
518 route_set_min_metric_free
,
522 /* `set max-metric METRIC' */
523 /* Set max-metric to attribute. */
524 static enum route_map_cmd_result_t
525 route_set_max_metric(void *rule
, const struct prefix
*prefix
, void *object
)
527 uint32_t *max_metric
;
528 struct external_info
*ei
;
530 /* Fetch routemap's rule information. */
534 ei
->max_metric
= *max_metric
;
536 if (ei
->max_metric
> OSPF_LS_INFINITY
)
537 ei
->max_metric
= OSPF_LS_INFINITY
;
539 if ((uint32_t)ROUTEMAP_METRIC(ei
) > ei
->max_metric
)
540 ROUTEMAP_METRIC(ei
) = ei
->max_metric
;
545 /* set max-metric compilation. */
546 static void *route_set_max_metric_compile(const char *arg
)
549 uint32_t *max_metric
;
551 max_metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
553 *max_metric
= strtoul(arg
, NULL
, 10);
558 XFREE(MTYPE_ROUTE_MAP_COMPILED
, max_metric
);
562 /* Free route map's compiled `set max-metric' value. */
563 static void route_set_max_metric_free(void *rule
)
565 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
568 /* Set metric rule structure. */
569 static const struct route_map_rule_cmd route_set_max_metric_cmd
= {
571 route_set_max_metric
,
572 route_set_max_metric_compile
,
573 route_set_max_metric_free
,
576 /* `set metric-type TYPE' */
577 /* Set metric-type to attribute. */
578 static enum route_map_cmd_result_t
579 route_set_metric_type(void *rule
, const struct prefix
*prefix
, void *object
)
581 uint32_t *metric_type
;
582 struct external_info
*ei
;
584 /* Fetch routemap's rule information. */
588 /* Set metric out value. */
589 ROUTEMAP_METRIC_TYPE(ei
) = *metric_type
;
594 /* set metric-type compilation. */
595 static void *route_set_metric_type_compile(const char *arg
)
597 uint32_t *metric_type
;
599 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
600 if (strcmp(arg
, "type-1") == 0)
601 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
602 else if (strcmp(arg
, "type-2") == 0)
603 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
605 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
606 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
609 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
613 /* Free route map's compiled `set metric-type' value. */
614 static void route_set_metric_type_free(void *rule
)
616 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
619 /* Set metric rule structure. */
620 static const struct route_map_rule_cmd route_set_metric_type_cmd
= {
622 route_set_metric_type
,
623 route_set_metric_type_compile
,
624 route_set_metric_type_free
,
627 static enum route_map_cmd_result_t
628 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
631 struct external_info
*ei
;
642 /* Route map commands for tag set. */
643 static const struct route_map_rule_cmd route_set_tag_cmd
= {
646 route_map_rule_tag_compile
,
647 route_map_rule_tag_free
,
650 DEFUN_YANG (set_metric_type
,
652 "set metric-type <type-1|type-2>",
654 "Type of metric for destination routing protocol\n"
655 "OSPF[6] external type 1 metric\n"
656 "OSPF[6] external type 2 metric\n")
658 char *ext
= argv
[2]->text
;
661 "./set-action[action='frr-ospf-route-map:metric-type']";
662 char xpath_value
[XPATH_MAXLEN
];
664 nb_cli_enqueue_change(vty
, xpath
, NB_OP_CREATE
, NULL
);
665 snprintf(xpath_value
, sizeof(xpath_value
),
666 "%s/rmap-set-action/frr-ospf-route-map:metric-type", xpath
);
667 nb_cli_enqueue_change(vty
, xpath_value
, NB_OP_MODIFY
, ext
);
668 return nb_cli_apply_changes(vty
, NULL
);
671 DEFUN_YANG (no_set_metric_type
,
672 no_set_metric_type_cmd
,
673 "no set metric-type [<type-1|type-2>]",
676 "Type of metric for destination routing protocol\n"
677 "OSPF[6] external type 1 metric\n"
678 "OSPF[6] external type 2 metric\n")
681 "./set-action[action='frr-ospf-route-map:metric-type']";
683 nb_cli_enqueue_change(vty
, xpath
, NB_OP_DESTROY
, NULL
);
684 return nb_cli_apply_changes(vty
, NULL
);
688 void ospf_route_map_init(void)
692 route_map_add_hook(ospf_route_map_update
);
693 route_map_delete_hook(ospf_route_map_update
);
694 route_map_event_hook(ospf_route_map_event
);
696 route_map_match_ip_next_hop_hook(generic_match_add
);
697 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
699 route_map_match_interface_hook(generic_match_add
);
700 route_map_no_match_interface_hook(generic_match_delete
);
702 route_map_match_ip_address_hook(generic_match_add
);
703 route_map_no_match_ip_address_hook(generic_match_delete
);
705 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
706 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
708 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
709 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
711 route_map_match_ip_next_hop_type_hook(generic_match_add
);
712 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
714 route_map_match_tag_hook(generic_match_add
);
715 route_map_no_match_tag_hook(generic_match_delete
);
717 route_map_set_metric_hook(generic_set_add
);
718 route_map_no_set_metric_hook(generic_set_delete
);
720 route_map_set_min_metric_hook(generic_set_add
);
721 route_map_no_set_min_metric_hook(generic_set_delete
);
723 route_map_set_max_metric_hook(generic_set_add
);
724 route_map_no_set_max_metric_hook(generic_set_delete
);
726 route_map_set_tag_hook(generic_set_add
);
727 route_map_no_set_tag_hook(generic_set_delete
);
729 route_map_install_match(&route_match_ip_nexthop_cmd
);
730 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
731 route_map_install_match(&route_match_ip_address_cmd
);
732 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
733 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
734 route_map_install_match(&route_match_interface_cmd
);
735 route_map_install_match(&route_match_tag_cmd
);
737 route_map_install_set(&route_set_metric_cmd
);
738 route_map_install_set(&route_set_min_metric_cmd
);
739 route_map_install_set(&route_set_max_metric_cmd
);
740 route_map_install_set(&route_set_metric_type_cmd
);
741 route_map_install_set(&route_set_tag_cmd
);
743 install_element(RMAP_NODE
, &set_metric_type_cmd
);
744 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);