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
);
140 return (access_list_apply(alist
, &p
) == FILTER_DENY
? RMAP_NOMATCH
144 /* Route map `ip next-hop' match statement. `arg' should be
146 static void *route_match_ip_nexthop_compile(const char *arg
)
148 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
151 /* Free route map's compiled `ip address' value. */
152 static void route_match_ip_nexthop_free(void *rule
)
154 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
157 /* Route map commands for metric matching. */
158 static const struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
160 route_match_ip_nexthop
,
161 route_match_ip_nexthop_compile
,
162 route_match_ip_nexthop_free
165 /* `match ip next-hop prefix-list PREFIX_LIST' */
167 static enum route_map_cmd_result_t
168 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
171 struct prefix_list
*plist
;
172 struct external_info
*ei
= object
;
173 struct prefix_ipv4 p
;
176 p
.prefix
= ei
->nexthop
;
177 p
.prefixlen
= IPV4_MAX_BITLEN
;
179 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
183 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
? RMAP_NOMATCH
187 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
189 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
192 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
194 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
197 static const struct route_map_rule_cmd
198 route_match_ip_next_hop_prefix_list_cmd
= {
199 "ip next-hop prefix-list",
200 route_match_ip_next_hop_prefix_list
,
201 route_match_ip_next_hop_prefix_list_compile
,
202 route_match_ip_next_hop_prefix_list_free
205 /* `match ip next-hop type <blackhole>' */
207 static enum route_map_cmd_result_t
208 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
211 struct external_info
*ei
= object
;
213 if (prefix
->family
== AF_INET
) {
214 ei
= (struct external_info
*)object
;
218 if (ei
->nexthop
.s_addr
== INADDR_ANY
&& !ei
->ifindex
)
224 static void *route_match_ip_next_hop_type_compile(const char *arg
)
226 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
229 static void route_match_ip_next_hop_type_free(void *rule
)
231 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
234 static const struct route_map_rule_cmd
235 route_match_ip_next_hop_type_cmd
= {
237 route_match_ip_next_hop_type
,
238 route_match_ip_next_hop_type_compile
,
239 route_match_ip_next_hop_type_free
242 /* `match ip address IP_ACCESS_LIST' */
243 /* Match function should return 1 if match is success else return
245 static enum route_map_cmd_result_t
246 route_match_ip_address(void *rule
, const struct prefix
*prefix
, void *object
)
248 struct access_list
*alist
;
249 /* struct prefix_ipv4 match; */
251 alist
= access_list_lookup(AFI_IP
, (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
);
291 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
? RMAP_NOMATCH
295 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
297 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
300 static void route_match_ip_address_prefix_list_free(void *rule
)
302 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
305 static const struct route_map_rule_cmd
306 route_match_ip_address_prefix_list_cmd
= {
307 "ip address prefix-list",
308 route_match_ip_address_prefix_list
,
309 route_match_ip_address_prefix_list_compile
,
310 route_match_ip_address_prefix_list_free
313 /* `match interface IFNAME' */
314 /* Match function should return 1 if match is success else return
316 static enum route_map_cmd_result_t
317 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
319 struct interface
*ifp
;
320 struct external_info
*ei
;
323 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
325 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
331 /* Route map `interface' match statement. `arg' should be
333 static void *route_match_interface_compile(const char *arg
)
335 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
338 /* Free route map's compiled `interface' value. */
339 static void route_match_interface_free(void *rule
)
341 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
344 /* Route map commands for ip address matching. */
345 static const struct route_map_rule_cmd route_match_interface_cmd
= {
347 route_match_interface
,
348 route_match_interface_compile
,
349 route_match_interface_free
352 /* Match function return 1 if match is success else return zero. */
353 static enum route_map_cmd_result_t
354 route_match_tag(void *rule
, const struct prefix
*prefix
, void *object
)
357 struct external_info
*ei
;
362 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
365 /* Route map commands for tag matching. */
366 static const struct route_map_rule_cmd route_match_tag_cmd
= {
369 route_map_rule_tag_compile
,
370 route_map_rule_tag_free
,
374 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
379 /* `set metric METRIC' */
380 /* Set metric to attribute. */
381 static enum route_map_cmd_result_t
382 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
384 struct ospf_metric
*metric
;
385 struct external_info
*ei
;
387 /* Fetch routemap's rule information. */
391 /* Set metric out value. */
395 ei
->route_map_set
.metric
= DEFAULT_DEFAULT_METRIC
;
397 if (metric
->type
== metric_increment
)
398 ei
->route_map_set
.metric
+= metric
->metric
;
399 else if (metric
->type
== metric_decrement
)
400 ei
->route_map_set
.metric
-= metric
->metric
;
401 else if (metric
->type
== metric_absolute
)
402 ei
->route_map_set
.metric
= metric
->metric
;
404 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
405 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
410 /* set metric compilation. */
411 static void *route_set_metric_compile(const char *arg
)
413 struct ospf_metric
*metric
;
415 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(*metric
));
416 metric
->used
= false;
419 metric
->type
= metric_absolute
;
421 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
422 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
423 "OSPF does not support 'set metric +rtt / -rtt'");
427 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
428 metric
->type
= metric_increment
;
432 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
433 metric
->type
= metric_decrement
;
437 metric
->metric
= strtoul(arg
, NULL
, 10);
445 /* Free route map's compiled `set metric' value. */
446 static void route_set_metric_free(void *rule
)
448 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
451 /* Set metric rule structure. */
452 static const struct route_map_rule_cmd route_set_metric_cmd
= {
455 route_set_metric_compile
,
456 route_set_metric_free
,
459 /* `set metric-type TYPE' */
460 /* Set metric-type to attribute. */
461 static enum route_map_cmd_result_t
462 route_set_metric_type(void *rule
, const struct prefix
*prefix
, void *object
)
464 uint32_t *metric_type
;
465 struct external_info
*ei
;
467 /* Fetch routemap's rule information. */
471 /* Set metric out value. */
472 ei
->route_map_set
.metric_type
= *metric_type
;
477 /* set metric-type compilation. */
478 static void *route_set_metric_type_compile(const char *arg
)
480 uint32_t *metric_type
;
482 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
483 if (strcmp(arg
, "type-1") == 0)
484 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
485 else if (strcmp(arg
, "type-2") == 0)
486 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
488 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
489 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
492 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
496 /* Free route map's compiled `set metric-type' value. */
497 static void route_set_metric_type_free(void *rule
)
499 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
502 /* Set metric rule structure. */
503 static const struct route_map_rule_cmd route_set_metric_type_cmd
= {
505 route_set_metric_type
,
506 route_set_metric_type_compile
,
507 route_set_metric_type_free
,
510 static enum route_map_cmd_result_t
511 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
514 struct external_info
*ei
;
525 /* Route map commands for tag set. */
526 static const struct route_map_rule_cmd route_set_tag_cmd
= {
529 route_map_rule_tag_compile
,
530 route_map_rule_tag_free
,
533 DEFUN_YANG (set_metric_type
,
535 "set metric-type <type-1|type-2>",
537 "Type of metric for destination routing protocol\n"
538 "OSPF[6] external type 1 metric\n"
539 "OSPF[6] external type 2 metric\n")
541 char *ext
= argv
[2]->text
;
544 "./set-action[action='frr-ospf-route-map:metric-type']";
545 char xpath_value
[XPATH_MAXLEN
];
547 nb_cli_enqueue_change(vty
, xpath
, NB_OP_CREATE
, NULL
);
548 snprintf(xpath_value
, sizeof(xpath_value
),
549 "%s/rmap-set-action/frr-ospf-route-map:metric-type", xpath
);
550 nb_cli_enqueue_change(vty
, xpath_value
, NB_OP_MODIFY
, ext
);
551 return nb_cli_apply_changes(vty
, NULL
);
554 DEFUN_YANG (no_set_metric_type
,
555 no_set_metric_type_cmd
,
556 "no set metric-type [<type-1|type-2>]",
559 "Type of metric for destination routing protocol\n"
560 "OSPF[6] external type 1 metric\n"
561 "OSPF[6] external type 2 metric\n")
564 "./set-action[action='frr-ospf-route-map:metric-type']";
566 nb_cli_enqueue_change(vty
, xpath
, NB_OP_DESTROY
, NULL
);
567 return nb_cli_apply_changes(vty
, NULL
);
571 void ospf_route_map_init(void)
575 route_map_add_hook(ospf_route_map_update
);
576 route_map_delete_hook(ospf_route_map_update
);
577 route_map_event_hook(ospf_route_map_event
);
579 route_map_set_metric_hook(generic_set_add
);
580 route_map_no_set_metric_hook(generic_set_delete
);
582 route_map_match_ip_next_hop_hook(generic_match_add
);
583 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
585 route_map_match_interface_hook(generic_match_add
);
586 route_map_no_match_interface_hook(generic_match_delete
);
588 route_map_match_ip_address_hook(generic_match_add
);
589 route_map_no_match_ip_address_hook(generic_match_delete
);
591 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
592 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
594 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
595 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
597 route_map_match_ip_next_hop_type_hook(generic_match_add
);
598 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
600 route_map_match_tag_hook(generic_match_add
);
601 route_map_no_match_tag_hook(generic_match_delete
);
603 route_map_set_metric_hook(generic_set_add
);
604 route_map_no_set_metric_hook(generic_set_delete
);
606 route_map_set_tag_hook(generic_set_add
);
607 route_map_no_set_tag_hook(generic_set_delete
);
609 route_map_install_match(&route_match_ip_nexthop_cmd
);
610 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
611 route_map_install_match(&route_match_ip_address_cmd
);
612 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
613 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
614 route_map_install_match(&route_match_interface_cmd
);
615 route_map_install_match(&route_match_tag_cmd
);
617 route_map_install_set(&route_set_metric_cmd
);
618 route_map_install_set(&route_set_metric_type_cmd
);
619 route_map_install_set(&route_set_tag_cmd
);
621 install_element(RMAP_NODE
, &set_metric_type_cmd
);
622 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);