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
);
125 return (access_list_apply(alist
, &p
) == FILTER_DENY
? RMAP_NOMATCH
129 /* Route map `ip next-hop' match statement. `arg' should be
131 static void *route_match_ip_nexthop_compile(const char *arg
)
133 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
136 /* Free route map's compiled `ip address' value. */
137 static void route_match_ip_nexthop_free(void *rule
)
139 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
142 /* Route map commands for metric matching. */
143 static const struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
145 route_match_ip_nexthop
,
146 route_match_ip_nexthop_compile
,
147 route_match_ip_nexthop_free
150 /* `match ip next-hop prefix-list PREFIX_LIST' */
152 static enum route_map_cmd_result_t
153 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
156 struct prefix_list
*plist
;
157 struct external_info
*ei
= object
;
158 struct prefix_ipv4 p
;
161 p
.prefix
= ei
->nexthop
;
162 p
.prefixlen
= IPV4_MAX_BITLEN
;
164 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
168 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
? RMAP_NOMATCH
172 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
174 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
177 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
179 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
182 static const struct route_map_rule_cmd
183 route_match_ip_next_hop_prefix_list_cmd
= {
184 "ip next-hop prefix-list",
185 route_match_ip_next_hop_prefix_list
,
186 route_match_ip_next_hop_prefix_list_compile
,
187 route_match_ip_next_hop_prefix_list_free
190 /* `match ip next-hop type <blackhole>' */
192 static enum route_map_cmd_result_t
193 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
196 struct external_info
*ei
= object
;
198 if (prefix
->family
== AF_INET
) {
199 ei
= (struct external_info
*)object
;
203 if (ei
->nexthop
.s_addr
== INADDR_ANY
&& !ei
->ifindex
)
209 static void *route_match_ip_next_hop_type_compile(const char *arg
)
211 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
214 static void route_match_ip_next_hop_type_free(void *rule
)
216 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
219 static const struct route_map_rule_cmd
220 route_match_ip_next_hop_type_cmd
= {
222 route_match_ip_next_hop_type
,
223 route_match_ip_next_hop_type_compile
,
224 route_match_ip_next_hop_type_free
227 /* `match ip address IP_ACCESS_LIST' */
228 /* Match function should return 1 if match is success else return
230 static enum route_map_cmd_result_t
231 route_match_ip_address(void *rule
, const struct prefix
*prefix
, void *object
)
233 struct access_list
*alist
;
234 /* struct prefix_ipv4 match; */
236 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
240 return (access_list_apply(alist
, prefix
) == FILTER_DENY
? RMAP_NOMATCH
244 /* Route map `ip address' match statement. `arg' should be
246 static void *route_match_ip_address_compile(const char *arg
)
248 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
251 /* Free route map's compiled `ip address' value. */
252 static void route_match_ip_address_free(void *rule
)
254 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
257 /* Route map commands for ip address matching. */
258 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
260 route_match_ip_address
,
261 route_match_ip_address_compile
,
262 route_match_ip_address_free
265 /* `match ip address prefix-list PREFIX_LIST' */
266 static enum route_map_cmd_result_t
267 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
270 struct prefix_list
*plist
;
272 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
276 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
? RMAP_NOMATCH
280 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
282 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
285 static void route_match_ip_address_prefix_list_free(void *rule
)
287 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
290 static const struct route_map_rule_cmd
291 route_match_ip_address_prefix_list_cmd
= {
292 "ip address prefix-list",
293 route_match_ip_address_prefix_list
,
294 route_match_ip_address_prefix_list_compile
,
295 route_match_ip_address_prefix_list_free
298 /* `match interface IFNAME' */
299 /* Match function should return 1 if match is success else return
301 static enum route_map_cmd_result_t
302 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
304 struct interface
*ifp
;
305 struct external_info
*ei
;
308 ifp
= if_lookup_by_name((char *)rule
, ei
->ospf
->vrf_id
);
310 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
316 /* Route map `interface' match statement. `arg' should be
318 static void *route_match_interface_compile(const char *arg
)
320 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
323 /* Free route map's compiled `interface' value. */
324 static void route_match_interface_free(void *rule
)
326 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
329 /* Route map commands for ip address matching. */
330 static const struct route_map_rule_cmd route_match_interface_cmd
= {
332 route_match_interface
,
333 route_match_interface_compile
,
334 route_match_interface_free
337 /* Match function return 1 if match is success else return zero. */
338 static enum route_map_cmd_result_t
339 route_match_tag(void *rule
, const struct prefix
*prefix
, void *object
)
342 struct external_info
*ei
;
347 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
350 /* Route map commands for tag matching. */
351 static const struct route_map_rule_cmd route_match_tag_cmd
= {
354 route_map_rule_tag_compile
,
355 route_map_rule_tag_free
,
359 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
364 /* `set metric METRIC' */
365 /* Set metric to attribute. */
366 static enum route_map_cmd_result_t
367 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
369 struct ospf_metric
*metric
;
370 struct external_info
*ei
;
372 /* Fetch routemap's rule information. */
376 /* Set metric out value. */
380 ei
->route_map_set
.metric
= DEFAULT_DEFAULT_METRIC
;
382 if (metric
->type
== metric_increment
)
383 ei
->route_map_set
.metric
+= metric
->metric
;
384 else if (metric
->type
== metric_decrement
)
385 ei
->route_map_set
.metric
-= metric
->metric
;
386 else if (metric
->type
== metric_absolute
)
387 ei
->route_map_set
.metric
= metric
->metric
;
389 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
390 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
395 /* set metric compilation. */
396 static void *route_set_metric_compile(const char *arg
)
398 struct ospf_metric
*metric
;
400 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(*metric
));
401 metric
->used
= false;
404 metric
->type
= metric_absolute
;
406 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
407 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
408 "OSPF does not support 'set metric +rtt / -rtt'");
412 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
413 metric
->type
= metric_increment
;
417 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
418 metric
->type
= metric_decrement
;
422 metric
->metric
= strtoul(arg
, NULL
, 10);
430 /* Free route map's compiled `set metric' value. */
431 static void route_set_metric_free(void *rule
)
433 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
436 /* Set metric rule structure. */
437 static const struct route_map_rule_cmd route_set_metric_cmd
= {
440 route_set_metric_compile
,
441 route_set_metric_free
,
444 /* `set metric-type TYPE' */
445 /* Set metric-type to attribute. */
446 static enum route_map_cmd_result_t
447 route_set_metric_type(void *rule
, const struct prefix
*prefix
, void *object
)
449 uint32_t *metric_type
;
450 struct external_info
*ei
;
452 /* Fetch routemap's rule information. */
456 /* Set metric out value. */
457 ei
->route_map_set
.metric_type
= *metric_type
;
462 /* set metric-type compilation. */
463 static void *route_set_metric_type_compile(const char *arg
)
465 uint32_t *metric_type
;
467 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
468 if (strcmp(arg
, "type-1") == 0)
469 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
470 else if (strcmp(arg
, "type-2") == 0)
471 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
473 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
474 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
477 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
481 /* Free route map's compiled `set metric-type' value. */
482 static void route_set_metric_type_free(void *rule
)
484 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
487 /* Set metric rule structure. */
488 static const struct route_map_rule_cmd route_set_metric_type_cmd
= {
490 route_set_metric_type
,
491 route_set_metric_type_compile
,
492 route_set_metric_type_free
,
495 static enum route_map_cmd_result_t
496 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
499 struct external_info
*ei
;
510 /* Route map commands for tag set. */
511 static const struct route_map_rule_cmd route_set_tag_cmd
= {
514 route_map_rule_tag_compile
,
515 route_map_rule_tag_free
,
518 DEFUN_YANG (set_metric_type
,
520 "set metric-type <type-1|type-2>",
522 "Type of metric for destination routing protocol\n"
523 "OSPF[6] external type 1 metric\n"
524 "OSPF[6] external type 2 metric\n")
526 char *ext
= argv
[2]->text
;
529 "./set-action[action='frr-ospf-route-map:metric-type']";
530 char xpath_value
[XPATH_MAXLEN
];
532 nb_cli_enqueue_change(vty
, xpath
, NB_OP_CREATE
, NULL
);
533 snprintf(xpath_value
, sizeof(xpath_value
),
534 "%s/rmap-set-action/frr-ospf-route-map:metric-type", xpath
);
535 nb_cli_enqueue_change(vty
, xpath_value
, NB_OP_MODIFY
, ext
);
536 return nb_cli_apply_changes(vty
, NULL
);
539 DEFUN_YANG (no_set_metric_type
,
540 no_set_metric_type_cmd
,
541 "no set metric-type [<type-1|type-2>]",
544 "Type of metric for destination routing protocol\n"
545 "OSPF[6] external type 1 metric\n"
546 "OSPF[6] external type 2 metric\n")
549 "./set-action[action='frr-ospf-route-map:metric-type']";
551 nb_cli_enqueue_change(vty
, xpath
, NB_OP_DESTROY
, NULL
);
552 return nb_cli_apply_changes(vty
, NULL
);
556 void ospf_route_map_init(void)
560 route_map_add_hook(ospf_route_map_update
);
561 route_map_delete_hook(ospf_route_map_update
);
562 route_map_event_hook(ospf_route_map_event
);
564 route_map_set_metric_hook(generic_set_add
);
565 route_map_no_set_metric_hook(generic_set_delete
);
567 route_map_match_ip_next_hop_hook(generic_match_add
);
568 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
570 route_map_match_interface_hook(generic_match_add
);
571 route_map_no_match_interface_hook(generic_match_delete
);
573 route_map_match_ip_address_hook(generic_match_add
);
574 route_map_no_match_ip_address_hook(generic_match_delete
);
576 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
577 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
579 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
580 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
582 route_map_match_ip_next_hop_type_hook(generic_match_add
);
583 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
585 route_map_match_tag_hook(generic_match_add
);
586 route_map_no_match_tag_hook(generic_match_delete
);
588 route_map_set_metric_hook(generic_set_add
);
589 route_map_no_set_metric_hook(generic_set_delete
);
591 route_map_set_tag_hook(generic_set_add
);
592 route_map_no_set_tag_hook(generic_set_delete
);
594 route_map_install_match(&route_match_ip_nexthop_cmd
);
595 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
596 route_map_install_match(&route_match_ip_address_cmd
);
597 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
598 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
599 route_map_install_match(&route_match_interface_cmd
);
600 route_map_install_match(&route_match_tag_cmd
);
602 route_map_install_set(&route_set_metric_cmd
);
603 route_map_install_set(&route_set_metric_type_cmd
);
604 route_map_install_set(&route_set_tag_cmd
);
606 install_element(RMAP_NODE
, &set_metric_type_cmd
);
607 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);