2 * Route map function of ospfd.
3 * Copyright (C) 2000 IP Infusion Inc.
5 * Written by Toshiaki Takada.
7 * This file is part of GNU Zebra.
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "northbound_cli.h"
38 #include "ospfd/ospfd.h"
39 #include "ospfd/ospf_asbr.h"
40 #include "ospfd/ospf_interface.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_route.h"
43 #include "ospfd/ospf_zebra.h"
44 #include "ospfd/ospf_errors.h"
46 /* Hook function for updating route_map assignment. */
47 static void ospf_route_map_update(const char *name
)
51 struct listnode
*n1
= NULL
;
53 /* If OSPF instatnce does not exist, return right now. */
54 if (listcount(om
->ospf
) == 0)
57 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
58 /* Update route-map */
59 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
60 struct list
*red_list
;
61 struct listnode
*node
;
62 struct ospf_redist
*red
;
64 red_list
= ospf
->redist
[type
];
68 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
69 if (ROUTEMAP_NAME(red
)
70 && strcmp(ROUTEMAP_NAME(red
), name
) == 0) {
71 /* Keep old route-map. */
72 struct route_map
*old
= ROUTEMAP(red
);
75 route_map_lookup_by_name(
79 route_map_counter_increment(
82 /* No update for this distribute type.
85 && ROUTEMAP(red
) == NULL
)
88 ospf_distribute_list_update(
89 ospf
, type
, red
->instance
);
96 static void ospf_route_map_event(const char *name
)
100 struct listnode
*n1
= NULL
;
102 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
103 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
104 struct list
*red_list
;
105 struct listnode
*node
;
106 struct ospf_redist
*red
;
108 red_list
= ospf
->redist
[type
];
112 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
113 if (ROUTEMAP_NAME(red
) && ROUTEMAP(red
)
114 && !strcmp(ROUTEMAP_NAME(red
), name
)) {
115 ospf_distribute_list_update(
116 ospf
, type
, red
->instance
);
123 /* `match ip netxthop ' */
124 /* Match function return 1 if match is success else return zero. */
125 static enum route_map_cmd_result_t
126 route_match_ip_nexthop(void *rule
, const struct prefix
*prefix
, void *object
)
128 struct access_list
*alist
;
129 struct external_info
*ei
= object
;
130 struct prefix_ipv4 p
;
133 p
.prefix
= ei
->nexthop
;
134 p
.prefixlen
= IPV4_MAX_BITLEN
;
136 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
138 if (CHECK_FLAG(rmap_debug
, DEBUG_ROUTEMAP_DETAIL
))
140 "%s: Access-List Specified: %s does not exist defaulting to NO_MATCH",
141 __func__
, (char *)rule
);
145 return (access_list_apply(alist
, &p
) == FILTER_DENY
? RMAP_NOMATCH
149 /* Route map `ip next-hop' match statement. `arg' should be
151 static void *route_match_ip_nexthop_compile(const char *arg
)
153 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
156 /* Free route map's compiled `ip address' value. */
157 static void route_match_ip_nexthop_free(void *rule
)
159 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
162 /* Route map commands for metric matching. */
163 static const struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
165 route_match_ip_nexthop
,
166 route_match_ip_nexthop_compile
,
167 route_match_ip_nexthop_free
170 /* `match ip next-hop prefix-list PREFIX_LIST' */
172 static enum route_map_cmd_result_t
173 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
176 struct prefix_list
*plist
;
177 struct external_info
*ei
= object
;
178 struct prefix_ipv4 p
;
181 p
.prefix
= ei
->nexthop
;
182 p
.prefixlen
= IPV4_MAX_BITLEN
;
184 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
186 if (CHECK_FLAG(rmap_debug
, DEBUG_ROUTEMAP_DETAIL
))
188 "%s: Prefix List %s specified does not exist defaulting to NO_MATCH",
189 __func__
, (char *)rule
);
193 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
? RMAP_NOMATCH
197 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
199 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
202 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
204 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
207 static const struct route_map_rule_cmd
208 route_match_ip_next_hop_prefix_list_cmd
= {
209 "ip next-hop prefix-list",
210 route_match_ip_next_hop_prefix_list
,
211 route_match_ip_next_hop_prefix_list_compile
,
212 route_match_ip_next_hop_prefix_list_free
215 /* `match ip next-hop type <blackhole>' */
217 static enum route_map_cmd_result_t
218 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
221 struct external_info
*ei
= object
;
223 if (prefix
->family
== AF_INET
) {
224 ei
= (struct external_info
*)object
;
228 if (ei
->nexthop
.s_addr
== INADDR_ANY
&& !ei
->ifindex
)
234 static void *route_match_ip_next_hop_type_compile(const char *arg
)
236 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
239 static void route_match_ip_next_hop_type_free(void *rule
)
241 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
244 static const struct route_map_rule_cmd
245 route_match_ip_next_hop_type_cmd
= {
247 route_match_ip_next_hop_type
,
248 route_match_ip_next_hop_type_compile
,
249 route_match_ip_next_hop_type_free
252 /* `match ip address IP_ACCESS_LIST' */
253 /* Match function should return 1 if match is success else return
255 static enum route_map_cmd_result_t
256 route_match_ip_address(void *rule
, const struct prefix
*prefix
, void *object
)
258 struct access_list
*alist
;
259 /* struct prefix_ipv4 match; */
261 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
263 if (CHECK_FLAG(rmap_debug
, DEBUG_ROUTEMAP_DETAIL
))
265 "%s: Access-List Specified: %s does not exist defaulting to NO_MATCH",
266 __func__
, (char *)rule
);
270 return (access_list_apply(alist
, prefix
) == FILTER_DENY
? RMAP_NOMATCH
274 /* Route map `ip address' match statement. `arg' should be
276 static void *route_match_ip_address_compile(const char *arg
)
278 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
281 /* Free route map's compiled `ip address' value. */
282 static void route_match_ip_address_free(void *rule
)
284 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
287 /* Route map commands for ip address matching. */
288 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
290 route_match_ip_address
,
291 route_match_ip_address_compile
,
292 route_match_ip_address_free
295 /* `match ip address prefix-list PREFIX_LIST' */
296 static enum route_map_cmd_result_t
297 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
300 struct prefix_list
*plist
;
302 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
304 if (CHECK_FLAG(rmap_debug
, DEBUG_ROUTEMAP_DETAIL
))
306 "%s: Prefix List %s specified does not exist defaulting to NO_MATCH",
307 __func__
, (char *)rule
);
312 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
? RMAP_NOMATCH
316 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
318 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
321 static void route_match_ip_address_prefix_list_free(void *rule
)
323 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
326 static const struct route_map_rule_cmd
327 route_match_ip_address_prefix_list_cmd
= {
328 "ip address prefix-list",
329 route_match_ip_address_prefix_list
,
330 route_match_ip_address_prefix_list_compile
,
331 route_match_ip_address_prefix_list_free
334 /* `match interface IFNAME' */
335 /* Match function should return 1 if match is success else return
337 static enum route_map_cmd_result_t
338 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
340 struct interface
*ifp
;
341 struct external_info
*ei
;
344 ifp
= if_lookup_by_name((char *)rule
, ei
->ospf
->vrf_id
);
346 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
352 /* Route map `interface' match statement. `arg' should be
354 static void *route_match_interface_compile(const char *arg
)
356 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
359 /* Free route map's compiled `interface' value. */
360 static void route_match_interface_free(void *rule
)
362 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
365 /* Route map commands for ip address matching. */
366 static const struct route_map_rule_cmd route_match_interface_cmd
= {
368 route_match_interface
,
369 route_match_interface_compile
,
370 route_match_interface_free
373 /* Match function return 1 if match is success else return zero. */
374 static enum route_map_cmd_result_t
375 route_match_tag(void *rule
, const struct prefix
*prefix
, void *object
)
378 struct external_info
*ei
;
383 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
386 /* Route map commands for tag matching. */
387 static const struct route_map_rule_cmd route_match_tag_cmd
= {
390 route_map_rule_tag_compile
,
391 route_map_rule_tag_free
,
395 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
400 /* `set metric METRIC' */
401 /* Set metric to attribute. */
402 static enum route_map_cmd_result_t
403 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
405 struct ospf_metric
*metric
;
406 struct external_info
*ei
;
408 /* Fetch routemap's rule information. */
412 /* Set metric out value. */
416 ei
->route_map_set
.metric
= DEFAULT_DEFAULT_METRIC
;
418 if (metric
->type
== metric_increment
)
419 ei
->route_map_set
.metric
+= metric
->metric
;
420 else if (metric
->type
== metric_decrement
)
421 ei
->route_map_set
.metric
-= metric
->metric
;
422 else if (metric
->type
== metric_absolute
)
423 ei
->route_map_set
.metric
= metric
->metric
;
425 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
426 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
431 /* set metric compilation. */
432 static void *route_set_metric_compile(const char *arg
)
434 struct ospf_metric
*metric
;
436 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(*metric
));
437 metric
->used
= false;
440 metric
->type
= metric_absolute
;
442 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
443 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
444 "OSPF does not support 'set metric +rtt / -rtt'");
448 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
449 metric
->type
= metric_increment
;
453 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
454 metric
->type
= metric_decrement
;
458 metric
->metric
= strtoul(arg
, NULL
, 10);
466 /* Free route map's compiled `set metric' value. */
467 static void route_set_metric_free(void *rule
)
469 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
472 /* Set metric rule structure. */
473 static const struct route_map_rule_cmd route_set_metric_cmd
= {
476 route_set_metric_compile
,
477 route_set_metric_free
,
480 /* `set metric-type TYPE' */
481 /* Set metric-type to attribute. */
482 static enum route_map_cmd_result_t
483 route_set_metric_type(void *rule
, const struct prefix
*prefix
, void *object
)
485 uint32_t *metric_type
;
486 struct external_info
*ei
;
488 /* Fetch routemap's rule information. */
492 /* Set metric out value. */
493 ei
->route_map_set
.metric_type
= *metric_type
;
498 /* set metric-type compilation. */
499 static void *route_set_metric_type_compile(const char *arg
)
501 uint32_t *metric_type
;
503 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
504 if (strcmp(arg
, "type-1") == 0)
505 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
506 else if (strcmp(arg
, "type-2") == 0)
507 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
509 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
510 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
513 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
517 /* Free route map's compiled `set metric-type' value. */
518 static void route_set_metric_type_free(void *rule
)
520 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
523 /* Set metric rule structure. */
524 static const struct route_map_rule_cmd route_set_metric_type_cmd
= {
526 route_set_metric_type
,
527 route_set_metric_type_compile
,
528 route_set_metric_type_free
,
531 static enum route_map_cmd_result_t
532 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
535 struct external_info
*ei
;
546 /* Route map commands for tag set. */
547 static const struct route_map_rule_cmd route_set_tag_cmd
= {
550 route_map_rule_tag_compile
,
551 route_map_rule_tag_free
,
554 DEFUN_YANG (set_metric_type
,
556 "set metric-type <type-1|type-2>",
558 "Type of metric for destination routing protocol\n"
559 "OSPF[6] external type 1 metric\n"
560 "OSPF[6] external type 2 metric\n")
562 char *ext
= argv
[2]->text
;
565 "./set-action[action='frr-ospf-route-map:metric-type']";
566 char xpath_value
[XPATH_MAXLEN
];
568 nb_cli_enqueue_change(vty
, xpath
, NB_OP_CREATE
, NULL
);
569 snprintf(xpath_value
, sizeof(xpath_value
),
570 "%s/rmap-set-action/frr-ospf-route-map:metric-type", xpath
);
571 nb_cli_enqueue_change(vty
, xpath_value
, NB_OP_MODIFY
, ext
);
572 return nb_cli_apply_changes(vty
, NULL
);
575 DEFUN_YANG (no_set_metric_type
,
576 no_set_metric_type_cmd
,
577 "no set metric-type [<type-1|type-2>]",
580 "Type of metric for destination routing protocol\n"
581 "OSPF[6] external type 1 metric\n"
582 "OSPF[6] external type 2 metric\n")
585 "./set-action[action='frr-ospf-route-map:metric-type']";
587 nb_cli_enqueue_change(vty
, xpath
, NB_OP_DESTROY
, NULL
);
588 return nb_cli_apply_changes(vty
, NULL
);
592 void ospf_route_map_init(void)
596 route_map_add_hook(ospf_route_map_update
);
597 route_map_delete_hook(ospf_route_map_update
);
598 route_map_event_hook(ospf_route_map_event
);
600 route_map_set_metric_hook(generic_set_add
);
601 route_map_no_set_metric_hook(generic_set_delete
);
603 route_map_match_ip_next_hop_hook(generic_match_add
);
604 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
606 route_map_match_interface_hook(generic_match_add
);
607 route_map_no_match_interface_hook(generic_match_delete
);
609 route_map_match_ip_address_hook(generic_match_add
);
610 route_map_no_match_ip_address_hook(generic_match_delete
);
612 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
613 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
615 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
616 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
618 route_map_match_ip_next_hop_type_hook(generic_match_add
);
619 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
621 route_map_match_tag_hook(generic_match_add
);
622 route_map_no_match_tag_hook(generic_match_delete
);
624 route_map_set_metric_hook(generic_set_add
);
625 route_map_no_set_metric_hook(generic_set_delete
);
627 route_map_set_tag_hook(generic_set_add
);
628 route_map_no_set_tag_hook(generic_set_delete
);
630 route_map_install_match(&route_match_ip_nexthop_cmd
);
631 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
632 route_map_install_match(&route_match_ip_address_cmd
);
633 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
634 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
635 route_map_install_match(&route_match_interface_cmd
);
636 route_map_install_match(&route_match_tag_cmd
);
638 route_map_install_set(&route_set_metric_cmd
);
639 route_map_install_set(&route_set_metric_type_cmd
);
640 route_map_install_set(&route_set_tag_cmd
);
642 install_element(RMAP_NODE
, &set_metric_type_cmd
);
643 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);