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 creation */
76 /* Update route-map. */
78 route_map_lookup_by_name(
81 route_map_counter_increment(
84 /* Route-map deletion */
87 /* No update for this distribute type.
90 && ROUTEMAP(red
) == NULL
)
93 ospf_distribute_list_update(
94 ospf
, type
, red
->instance
);
101 static void ospf_route_map_event(const char *name
)
105 struct listnode
*n1
= NULL
;
107 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
108 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
109 struct list
*red_list
;
110 struct listnode
*node
;
111 struct ospf_redist
*red
;
113 red_list
= ospf
->redist
[type
];
117 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
118 if (ROUTEMAP_NAME(red
) && ROUTEMAP(red
)
119 && !strcmp(ROUTEMAP_NAME(red
), name
)) {
120 ospf_distribute_list_update(
121 ospf
, type
, red
->instance
);
128 /* `match ip netxthop ' */
129 /* Match function return 1 if match is success else return zero. */
130 static enum route_map_cmd_result_t
131 route_match_ip_nexthop(void *rule
, const struct prefix
*prefix
, void *object
)
133 struct access_list
*alist
;
134 struct external_info
*ei
= object
;
135 struct prefix_ipv4 p
;
138 p
.prefix
= ei
->nexthop
;
139 p
.prefixlen
= IPV4_MAX_BITLEN
;
141 alist
= access_list_lookup(AFI_IP
, (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
);
188 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
? RMAP_NOMATCH
192 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
194 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
197 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
199 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
202 static const struct route_map_rule_cmd
203 route_match_ip_next_hop_prefix_list_cmd
= {
204 "ip next-hop prefix-list",
205 route_match_ip_next_hop_prefix_list
,
206 route_match_ip_next_hop_prefix_list_compile
,
207 route_match_ip_next_hop_prefix_list_free
210 /* `match ip next-hop type <blackhole>' */
212 static enum route_map_cmd_result_t
213 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
216 struct external_info
*ei
= object
;
218 if (prefix
->family
== AF_INET
) {
219 ei
= (struct external_info
*)object
;
223 if (ei
->nexthop
.s_addr
== INADDR_ANY
&& !ei
->ifindex
)
229 static void *route_match_ip_next_hop_type_compile(const char *arg
)
231 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
234 static void route_match_ip_next_hop_type_free(void *rule
)
236 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
239 static const struct route_map_rule_cmd
240 route_match_ip_next_hop_type_cmd
= {
242 route_match_ip_next_hop_type
,
243 route_match_ip_next_hop_type_compile
,
244 route_match_ip_next_hop_type_free
247 /* `match ip address IP_ACCESS_LIST' */
248 /* Match function should return 1 if match is success else return
250 static enum route_map_cmd_result_t
251 route_match_ip_address(void *rule
, const struct prefix
*prefix
, void *object
)
253 struct access_list
*alist
;
254 /* struct prefix_ipv4 match; */
256 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
260 return (access_list_apply(alist
, prefix
) == FILTER_DENY
? RMAP_NOMATCH
264 /* Route map `ip address' match statement. `arg' should be
266 static void *route_match_ip_address_compile(const char *arg
)
268 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
271 /* Free route map's compiled `ip address' value. */
272 static void route_match_ip_address_free(void *rule
)
274 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
277 /* Route map commands for ip address matching. */
278 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
280 route_match_ip_address
,
281 route_match_ip_address_compile
,
282 route_match_ip_address_free
285 /* `match ip address prefix-list PREFIX_LIST' */
286 static enum route_map_cmd_result_t
287 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
290 struct prefix_list
*plist
;
292 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
296 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
? RMAP_NOMATCH
300 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
302 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
305 static void route_match_ip_address_prefix_list_free(void *rule
)
307 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
310 static const struct route_map_rule_cmd
311 route_match_ip_address_prefix_list_cmd
= {
312 "ip address prefix-list",
313 route_match_ip_address_prefix_list
,
314 route_match_ip_address_prefix_list_compile
,
315 route_match_ip_address_prefix_list_free
318 /* `match interface IFNAME' */
319 /* Match function should return 1 if match is success else return
321 static enum route_map_cmd_result_t
322 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
324 struct interface
*ifp
;
325 struct external_info
*ei
;
328 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
330 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
336 /* Route map `interface' match statement. `arg' should be
338 static void *route_match_interface_compile(const char *arg
)
340 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
343 /* Free route map's compiled `interface' value. */
344 static void route_match_interface_free(void *rule
)
346 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
349 /* Route map commands for ip address matching. */
350 static const struct route_map_rule_cmd route_match_interface_cmd
= {
352 route_match_interface
,
353 route_match_interface_compile
,
354 route_match_interface_free
357 /* Match function return 1 if match is success else return zero. */
358 static enum route_map_cmd_result_t
359 route_match_tag(void *rule
, const struct prefix
*prefix
, void *object
)
362 struct external_info
*ei
;
367 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
370 /* Route map commands for tag matching. */
371 static const struct route_map_rule_cmd route_match_tag_cmd
= {
374 route_map_rule_tag_compile
,
375 route_map_rule_tag_free
,
379 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
384 /* `set metric METRIC' */
385 /* Set metric to attribute. */
386 static enum route_map_cmd_result_t
387 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
389 struct ospf_metric
*metric
;
390 struct external_info
*ei
;
392 /* Fetch routemap's rule information. */
396 /* Set metric out value. */
400 ei
->route_map_set
.metric
= DEFAULT_DEFAULT_METRIC
;
402 if (metric
->type
== metric_increment
)
403 ei
->route_map_set
.metric
+= metric
->metric
;
404 else if (metric
->type
== metric_decrement
)
405 ei
->route_map_set
.metric
-= metric
->metric
;
406 else if (metric
->type
== metric_absolute
)
407 ei
->route_map_set
.metric
= metric
->metric
;
409 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
410 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
415 /* set metric compilation. */
416 static void *route_set_metric_compile(const char *arg
)
418 struct ospf_metric
*metric
;
420 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(*metric
));
421 metric
->used
= false;
424 metric
->type
= metric_absolute
;
426 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
427 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
428 "OSPF does not support 'set metric +rtt / -rtt'");
432 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
433 metric
->type
= metric_increment
;
437 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
438 metric
->type
= metric_decrement
;
442 metric
->metric
= strtoul(arg
, NULL
, 10);
450 /* Free route map's compiled `set metric' value. */
451 static void route_set_metric_free(void *rule
)
453 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
456 /* Set metric rule structure. */
457 static const struct route_map_rule_cmd route_set_metric_cmd
= {
460 route_set_metric_compile
,
461 route_set_metric_free
,
464 /* `set metric-type TYPE' */
465 /* Set metric-type to attribute. */
466 static enum route_map_cmd_result_t
467 route_set_metric_type(void *rule
, const struct prefix
*prefix
, void *object
)
469 uint32_t *metric_type
;
470 struct external_info
*ei
;
472 /* Fetch routemap's rule information. */
476 /* Set metric out value. */
477 ei
->route_map_set
.metric_type
= *metric_type
;
482 /* set metric-type compilation. */
483 static void *route_set_metric_type_compile(const char *arg
)
485 uint32_t *metric_type
;
487 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
488 if (strcmp(arg
, "type-1") == 0)
489 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
490 else if (strcmp(arg
, "type-2") == 0)
491 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
493 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
494 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
497 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
501 /* Free route map's compiled `set metric-type' value. */
502 static void route_set_metric_type_free(void *rule
)
504 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
507 /* Set metric rule structure. */
508 static const struct route_map_rule_cmd route_set_metric_type_cmd
= {
510 route_set_metric_type
,
511 route_set_metric_type_compile
,
512 route_set_metric_type_free
,
515 static enum route_map_cmd_result_t
516 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
519 struct external_info
*ei
;
530 /* Route map commands for tag set. */
531 static const struct route_map_rule_cmd route_set_tag_cmd
= {
534 route_map_rule_tag_compile
,
535 route_map_rule_tag_free
,
538 DEFUN_YANG (set_metric_type
,
540 "set metric-type <type-1|type-2>",
542 "Type of metric for destination routing protocol\n"
543 "OSPF[6] external type 1 metric\n"
544 "OSPF[6] external type 2 metric\n")
546 char *ext
= argv
[2]->text
;
549 "./set-action[action='frr-ospf-route-map:metric-type']";
550 char xpath_value
[XPATH_MAXLEN
];
552 nb_cli_enqueue_change(vty
, xpath
, NB_OP_CREATE
, NULL
);
553 snprintf(xpath_value
, sizeof(xpath_value
),
554 "%s/rmap-set-action/frr-ospf-route-map:metric-type", xpath
);
555 nb_cli_enqueue_change(vty
, xpath_value
, NB_OP_MODIFY
, ext
);
556 return nb_cli_apply_changes(vty
, NULL
);
559 DEFUN_YANG (no_set_metric_type
,
560 no_set_metric_type_cmd
,
561 "no set metric-type [<type-1|type-2>]",
564 "Type of metric for destination routing protocol\n"
565 "OSPF[6] external type 1 metric\n"
566 "OSPF[6] external type 2 metric\n")
569 "./set-action[action='frr-ospf-route-map:metric-type']";
571 nb_cli_enqueue_change(vty
, xpath
, NB_OP_DESTROY
, NULL
);
572 return nb_cli_apply_changes(vty
, NULL
);
576 void ospf_route_map_init(void)
580 route_map_add_hook(ospf_route_map_update
);
581 route_map_delete_hook(ospf_route_map_update
);
582 route_map_event_hook(ospf_route_map_event
);
584 route_map_set_metric_hook(generic_set_add
);
585 route_map_no_set_metric_hook(generic_set_delete
);
587 route_map_match_ip_next_hop_hook(generic_match_add
);
588 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
590 route_map_match_interface_hook(generic_match_add
);
591 route_map_no_match_interface_hook(generic_match_delete
);
593 route_map_match_ip_address_hook(generic_match_add
);
594 route_map_no_match_ip_address_hook(generic_match_delete
);
596 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
597 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
599 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
600 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
602 route_map_match_ip_next_hop_type_hook(generic_match_add
);
603 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
605 route_map_match_tag_hook(generic_match_add
);
606 route_map_no_match_tag_hook(generic_match_delete
);
608 route_map_set_metric_hook(generic_set_add
);
609 route_map_no_set_metric_hook(generic_set_delete
);
611 route_map_set_tag_hook(generic_set_add
);
612 route_map_no_set_tag_hook(generic_set_delete
);
614 route_map_install_match(&route_match_ip_nexthop_cmd
);
615 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
616 route_map_install_match(&route_match_ip_address_cmd
);
617 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
618 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
619 route_map_install_match(&route_match_interface_cmd
);
620 route_map_install_match(&route_match_tag_cmd
);
622 route_map_install_set(&route_set_metric_cmd
);
623 route_map_install_set(&route_set_metric_type_cmd
);
624 route_map_install_set(&route_set_tag_cmd
);
626 install_element(RMAP_NODE
, &set_metric_type_cmd
);
627 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);