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
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_asbr.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_lsa.h"
41 #include "ospfd/ospf_route.h"
42 #include "ospfd/ospf_zebra.h"
43 #include "ospfd/ospf_errors.h"
45 /* Hook function for updating route_map assignment. */
46 static void ospf_route_map_update(const char *name
)
50 struct listnode
*n1
= NULL
;
52 /* If OSPF instatnce does not exist, return right now. */
53 if (listcount(om
->ospf
) == 0)
56 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
57 /* Update route-map */
58 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
59 struct list
*red_list
;
60 struct listnode
*node
;
61 struct ospf_redist
*red
;
63 red_list
= ospf
->redist
[type
];
67 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
68 if (ROUTEMAP_NAME(red
)
69 && strcmp(ROUTEMAP_NAME(red
), name
) == 0) {
70 /* Keep old route-map. */
71 struct route_map
*old
= ROUTEMAP(red
);
74 /* Route-map creation */
75 /* Update route-map. */
77 route_map_lookup_by_name(
80 route_map_counter_increment(
83 /* Route-map deletion */
86 /* No update for this distribute type.
89 && ROUTEMAP(red
) == NULL
)
92 ospf_distribute_list_update(
93 ospf
, type
, red
->instance
);
100 static void ospf_route_map_event(const char *name
)
104 struct listnode
*n1
= NULL
;
106 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
107 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
108 struct list
*red_list
;
109 struct listnode
*node
;
110 struct ospf_redist
*red
;
112 red_list
= ospf
->redist
[type
];
116 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
117 if (ROUTEMAP_NAME(red
) && ROUTEMAP(red
)
118 && !strcmp(ROUTEMAP_NAME(red
), name
)) {
119 ospf_distribute_list_update(
120 ospf
, type
, red
->instance
);
127 /* `match ip netxthop ' */
128 /* Match function return 1 if match is success else return zero. */
129 static enum route_map_cmd_result_t
130 route_match_ip_nexthop(void *rule
, const struct prefix
*prefix
,
131 route_map_object_t type
, void *object
)
133 struct access_list
*alist
;
134 struct external_info
*ei
= object
;
135 struct prefix_ipv4 p
;
137 if (type
== RMAP_OSPF
) {
139 p
.prefix
= ei
->nexthop
;
140 p
.prefixlen
= IPV4_MAX_BITLEN
;
142 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
146 return (access_list_apply(alist
, &p
) == FILTER_DENY
153 /* Route map `ip next-hop' match statement. `arg' should be
155 static void *route_match_ip_nexthop_compile(const char *arg
)
157 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
160 /* Free route map's compiled `ip address' value. */
161 static void route_match_ip_nexthop_free(void *rule
)
163 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
166 /* Route map commands for metric matching. */
167 static const struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
169 route_match_ip_nexthop
,
170 route_match_ip_nexthop_compile
,
171 route_match_ip_nexthop_free
174 /* `match ip next-hop prefix-list PREFIX_LIST' */
176 static enum route_map_cmd_result_t
177 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
178 route_map_object_t type
, void *object
)
180 struct prefix_list
*plist
;
181 struct external_info
*ei
= object
;
182 struct prefix_ipv4 p
;
184 if (type
== RMAP_OSPF
) {
186 p
.prefix
= ei
->nexthop
;
187 p
.prefixlen
= IPV4_MAX_BITLEN
;
189 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
193 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
200 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
202 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
205 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
207 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
210 static const struct route_map_rule_cmd
211 route_match_ip_next_hop_prefix_list_cmd
= {
212 "ip next-hop prefix-list",
213 route_match_ip_next_hop_prefix_list
,
214 route_match_ip_next_hop_prefix_list_compile
,
215 route_match_ip_next_hop_prefix_list_free
218 /* `match ip next-hop type <blackhole>' */
220 static enum route_map_cmd_result_t
221 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
222 route_map_object_t type
, void *object
)
224 struct external_info
*ei
= object
;
226 if (type
== RMAP_OSPF
&& prefix
->family
== AF_INET
) {
227 ei
= (struct external_info
*)object
;
231 if (ei
->nexthop
.s_addr
== INADDR_ANY
&& !ei
->ifindex
)
237 static void *route_match_ip_next_hop_type_compile(const char *arg
)
239 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
242 static void route_match_ip_next_hop_type_free(void *rule
)
244 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
247 static const struct route_map_rule_cmd
248 route_match_ip_next_hop_type_cmd
= {
250 route_match_ip_next_hop_type
,
251 route_match_ip_next_hop_type_compile
,
252 route_match_ip_next_hop_type_free
255 /* `match ip address IP_ACCESS_LIST' */
256 /* Match function should return 1 if match is success else return
258 static enum route_map_cmd_result_t
259 route_match_ip_address(void *rule
, const struct prefix
*prefix
,
260 route_map_object_t type
, void *object
)
262 struct access_list
*alist
;
263 /* struct prefix_ipv4 match; */
265 if (type
== RMAP_OSPF
) {
266 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
270 return (access_list_apply(alist
, prefix
) == FILTER_DENY
277 /* Route map `ip address' match statement. `arg' should be
279 static void *route_match_ip_address_compile(const char *arg
)
281 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
284 /* Free route map's compiled `ip address' value. */
285 static void route_match_ip_address_free(void *rule
)
287 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
290 /* Route map commands for ip address matching. */
291 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
293 route_match_ip_address
,
294 route_match_ip_address_compile
,
295 route_match_ip_address_free
298 /* `match ip address prefix-list PREFIX_LIST' */
299 static enum route_map_cmd_result_t
300 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
301 route_map_object_t type
, void *object
)
303 struct prefix_list
*plist
;
305 if (type
== RMAP_OSPF
) {
306 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
310 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
317 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
319 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
322 static void route_match_ip_address_prefix_list_free(void *rule
)
324 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
327 static const struct route_map_rule_cmd
328 route_match_ip_address_prefix_list_cmd
= {
329 "ip address prefix-list",
330 route_match_ip_address_prefix_list
,
331 route_match_ip_address_prefix_list_compile
,
332 route_match_ip_address_prefix_list_free
335 /* `match interface IFNAME' */
336 /* Match function should return 1 if match is success else return
338 static enum route_map_cmd_result_t
339 route_match_interface(void *rule
, const struct prefix
*prefix
,
340 route_map_object_t type
, void *object
)
342 struct interface
*ifp
;
343 struct external_info
*ei
;
345 if (type
== RMAP_OSPF
) {
347 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
349 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
357 /* Route map `interface' match statement. `arg' should be
359 static void *route_match_interface_compile(const char *arg
)
361 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
364 /* Free route map's compiled `interface' value. */
365 static void route_match_interface_free(void *rule
)
367 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
370 /* Route map commands for ip address matching. */
371 static const struct route_map_rule_cmd route_match_interface_cmd
= {
373 route_match_interface
,
374 route_match_interface_compile
,
375 route_match_interface_free
378 /* Match function return 1 if match is success else return zero. */
379 static enum route_map_cmd_result_t
380 route_match_tag(void *rule
, const struct prefix
*prefix
,
381 route_map_object_t type
, void *object
)
384 struct external_info
*ei
;
386 if (type
== RMAP_OSPF
) {
390 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
396 /* Route map commands for tag matching. */
397 static const struct route_map_rule_cmd route_match_tag_cmd
= {
400 route_map_rule_tag_compile
,
401 route_map_rule_tag_free
,
405 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
410 /* `set metric METRIC' */
411 /* Set metric to attribute. */
412 static enum route_map_cmd_result_t
413 route_set_metric(void *rule
, const struct prefix
*prefix
,
414 route_map_object_t type
, void *object
)
416 struct ospf_metric
*metric
;
417 struct external_info
*ei
;
419 if (type
== RMAP_OSPF
) {
420 /* Fetch routemap's rule information. */
424 /* Set metric out value. */
428 ei
->route_map_set
.metric
= DEFAULT_DEFAULT_METRIC
;
430 if (metric
->type
== metric_increment
)
431 ei
->route_map_set
.metric
+= metric
->metric
;
432 else if (metric
->type
== metric_decrement
)
433 ei
->route_map_set
.metric
-= metric
->metric
;
434 else if (metric
->type
== metric_absolute
)
435 ei
->route_map_set
.metric
= metric
->metric
;
437 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
438 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
443 /* set metric compilation. */
444 static void *route_set_metric_compile(const char *arg
)
446 struct ospf_metric
*metric
;
448 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
449 metric
->used
= false;
452 metric
->type
= metric_absolute
;
454 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
455 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
456 "OSPF does not support 'set metric +rtt / -rtt'");
460 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
461 metric
->type
= metric_increment
;
465 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
466 metric
->type
= metric_decrement
;
470 metric
->metric
= strtoul(arg
, NULL
, 10);
478 /* Free route map's compiled `set metric' value. */
479 static void route_set_metric_free(void *rule
)
481 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
484 /* Set metric rule structure. */
485 static const struct route_map_rule_cmd route_set_metric_cmd
= {
488 route_set_metric_compile
,
489 route_set_metric_free
,
492 /* `set metric-type TYPE' */
493 /* Set metric-type to attribute. */
494 static enum route_map_cmd_result_t
495 route_set_metric_type(void *rule
, const struct prefix
*prefix
,
496 route_map_object_t type
, void *object
)
498 uint32_t *metric_type
;
499 struct external_info
*ei
;
501 if (type
== RMAP_OSPF
) {
502 /* Fetch routemap's rule information. */
506 /* Set metric out value. */
507 ei
->route_map_set
.metric_type
= *metric_type
;
512 /* set metric-type compilation. */
513 static void *route_set_metric_type_compile(const char *arg
)
515 uint32_t *metric_type
;
517 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
518 if (strcmp(arg
, "type-1") == 0)
519 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
520 else if (strcmp(arg
, "type-2") == 0)
521 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
523 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
524 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
527 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
531 /* Free route map's compiled `set metric-type' value. */
532 static void route_set_metric_type_free(void *rule
)
534 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
537 /* Set metric rule structure. */
538 static const struct route_map_rule_cmd route_set_metric_type_cmd
= {
540 route_set_metric_type
,
541 route_set_metric_type_compile
,
542 route_set_metric_type_free
,
545 static enum route_map_cmd_result_t
546 route_set_tag(void *rule
, const struct prefix
*prefix
, route_map_object_t type
,
550 struct external_info
*ei
;
552 if (type
== RMAP_OSPF
) {
563 /* Route map commands for tag set. */
564 static const struct route_map_rule_cmd route_set_tag_cmd
= {
567 route_map_rule_tag_compile
,
568 route_map_rule_tag_free
,
571 DEFUN (set_metric_type
,
573 "set metric-type <type-1|type-2>",
575 "Type of metric for destination routing protocol\n"
576 "OSPF[6] external type 1 metric\n"
577 "OSPF[6] external type 2 metric\n")
579 char *ext
= argv
[2]->text
;
580 return generic_set_add(vty
, VTY_GET_CONTEXT(route_map_index
),
584 DEFUN (no_set_metric_type
,
585 no_set_metric_type_cmd
,
586 "no set metric-type [<type-1|type-2>]",
589 "Type of metric for destination routing protocol\n"
590 "OSPF[6] external type 1 metric\n"
591 "OSPF[6] external type 2 metric\n")
593 char *ext
= (argc
== 4) ? argv
[3]->text
: NULL
;
594 return generic_set_delete(vty
, VTY_GET_CONTEXT(route_map_index
),
599 void ospf_route_map_init(void)
603 route_map_add_hook(ospf_route_map_update
);
604 route_map_delete_hook(ospf_route_map_update
);
605 route_map_event_hook(ospf_route_map_event
);
607 route_map_set_metric_hook(generic_set_add
);
608 route_map_no_set_metric_hook(generic_set_delete
);
610 route_map_match_ip_next_hop_hook(generic_match_add
);
611 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
613 route_map_match_interface_hook(generic_match_add
);
614 route_map_no_match_interface_hook(generic_match_delete
);
616 route_map_match_ip_address_hook(generic_match_add
);
617 route_map_no_match_ip_address_hook(generic_match_delete
);
619 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
620 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
622 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
623 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
625 route_map_match_ip_next_hop_type_hook(generic_match_add
);
626 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
628 route_map_match_tag_hook(generic_match_add
);
629 route_map_no_match_tag_hook(generic_match_delete
);
631 route_map_set_metric_hook(generic_set_add
);
632 route_map_no_set_metric_hook(generic_set_delete
);
634 route_map_set_tag_hook(generic_set_add
);
635 route_map_no_set_tag_hook(generic_set_delete
);
637 route_map_install_match(&route_match_ip_nexthop_cmd
);
638 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
639 route_map_install_match(&route_match_ip_address_cmd
);
640 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
641 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
642 route_map_install_match(&route_match_interface_cmd
);
643 route_map_install_match(&route_match_tag_cmd
);
645 route_map_install_set(&route_set_metric_cmd
);
646 route_map_install_set(&route_set_metric_type_cmd
);
647 route_map_install_set(&route_set_tag_cmd
);
649 install_element(RMAP_NODE
, &set_metric_type_cmd
);
650 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);