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
, void *object
)
132 struct access_list
*alist
;
133 struct external_info
*ei
= object
;
134 struct prefix_ipv4 p
;
137 p
.prefix
= ei
->nexthop
;
138 p
.prefixlen
= IPV4_MAX_BITLEN
;
140 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
144 return (access_list_apply(alist
, &p
) == FILTER_DENY
? RMAP_NOMATCH
148 /* Route map `ip next-hop' match statement. `arg' should be
150 static void *route_match_ip_nexthop_compile(const char *arg
)
152 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
155 /* Free route map's compiled `ip address' value. */
156 static void route_match_ip_nexthop_free(void *rule
)
158 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
161 /* Route map commands for metric matching. */
162 static const struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
164 route_match_ip_nexthop
,
165 route_match_ip_nexthop_compile
,
166 route_match_ip_nexthop_free
169 /* `match ip next-hop prefix-list PREFIX_LIST' */
171 static enum route_map_cmd_result_t
172 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
175 struct prefix_list
*plist
;
176 struct external_info
*ei
= object
;
177 struct prefix_ipv4 p
;
180 p
.prefix
= ei
->nexthop
;
181 p
.prefixlen
= IPV4_MAX_BITLEN
;
183 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
187 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
? RMAP_NOMATCH
191 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
193 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
196 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
198 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
201 static const struct route_map_rule_cmd
202 route_match_ip_next_hop_prefix_list_cmd
= {
203 "ip next-hop prefix-list",
204 route_match_ip_next_hop_prefix_list
,
205 route_match_ip_next_hop_prefix_list_compile
,
206 route_match_ip_next_hop_prefix_list_free
209 /* `match ip next-hop type <blackhole>' */
211 static enum route_map_cmd_result_t
212 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
215 struct external_info
*ei
= object
;
217 if (prefix
->family
== AF_INET
) {
218 ei
= (struct external_info
*)object
;
222 if (ei
->nexthop
.s_addr
== INADDR_ANY
&& !ei
->ifindex
)
228 static void *route_match_ip_next_hop_type_compile(const char *arg
)
230 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
233 static void route_match_ip_next_hop_type_free(void *rule
)
235 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
238 static const struct route_map_rule_cmd
239 route_match_ip_next_hop_type_cmd
= {
241 route_match_ip_next_hop_type
,
242 route_match_ip_next_hop_type_compile
,
243 route_match_ip_next_hop_type_free
246 /* `match ip address IP_ACCESS_LIST' */
247 /* Match function should return 1 if match is success else return
249 static enum route_map_cmd_result_t
250 route_match_ip_address(void *rule
, const struct prefix
*prefix
, void *object
)
252 struct access_list
*alist
;
253 /* struct prefix_ipv4 match; */
255 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
259 return (access_list_apply(alist
, prefix
) == FILTER_DENY
? RMAP_NOMATCH
263 /* Route map `ip address' match statement. `arg' should be
265 static void *route_match_ip_address_compile(const char *arg
)
267 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
270 /* Free route map's compiled `ip address' value. */
271 static void route_match_ip_address_free(void *rule
)
273 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
276 /* Route map commands for ip address matching. */
277 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
279 route_match_ip_address
,
280 route_match_ip_address_compile
,
281 route_match_ip_address_free
284 /* `match ip address prefix-list PREFIX_LIST' */
285 static enum route_map_cmd_result_t
286 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
289 struct prefix_list
*plist
;
291 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
295 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
? RMAP_NOMATCH
299 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
301 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
304 static void route_match_ip_address_prefix_list_free(void *rule
)
306 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
309 static const struct route_map_rule_cmd
310 route_match_ip_address_prefix_list_cmd
= {
311 "ip address prefix-list",
312 route_match_ip_address_prefix_list
,
313 route_match_ip_address_prefix_list_compile
,
314 route_match_ip_address_prefix_list_free
317 /* `match interface IFNAME' */
318 /* Match function should return 1 if match is success else return
320 static enum route_map_cmd_result_t
321 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
323 struct interface
*ifp
;
324 struct external_info
*ei
;
327 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
329 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
335 /* Route map `interface' match statement. `arg' should be
337 static void *route_match_interface_compile(const char *arg
)
339 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
342 /* Free route map's compiled `interface' value. */
343 static void route_match_interface_free(void *rule
)
345 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
348 /* Route map commands for ip address matching. */
349 static const struct route_map_rule_cmd route_match_interface_cmd
= {
351 route_match_interface
,
352 route_match_interface_compile
,
353 route_match_interface_free
356 /* Match function return 1 if match is success else return zero. */
357 static enum route_map_cmd_result_t
358 route_match_tag(void *rule
, const struct prefix
*prefix
, void *object
)
361 struct external_info
*ei
;
366 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
369 /* Route map commands for tag matching. */
370 static const struct route_map_rule_cmd route_match_tag_cmd
= {
373 route_map_rule_tag_compile
,
374 route_map_rule_tag_free
,
378 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
383 /* `set metric METRIC' */
384 /* Set metric to attribute. */
385 static enum route_map_cmd_result_t
386 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
388 struct ospf_metric
*metric
;
389 struct external_info
*ei
;
391 /* Fetch routemap's rule information. */
395 /* Set metric out value. */
399 ei
->route_map_set
.metric
= DEFAULT_DEFAULT_METRIC
;
401 if (metric
->type
== metric_increment
)
402 ei
->route_map_set
.metric
+= metric
->metric
;
403 else if (metric
->type
== metric_decrement
)
404 ei
->route_map_set
.metric
-= metric
->metric
;
405 else if (metric
->type
== metric_absolute
)
406 ei
->route_map_set
.metric
= metric
->metric
;
408 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
409 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
414 /* set metric compilation. */
415 static void *route_set_metric_compile(const char *arg
)
417 struct ospf_metric
*metric
;
419 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(*metric
));
420 metric
->used
= false;
423 metric
->type
= metric_absolute
;
425 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
426 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
427 "OSPF does not support 'set metric +rtt / -rtt'");
431 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
432 metric
->type
= metric_increment
;
436 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
437 metric
->type
= metric_decrement
;
441 metric
->metric
= strtoul(arg
, NULL
, 10);
449 /* Free route map's compiled `set metric' value. */
450 static void route_set_metric_free(void *rule
)
452 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
455 /* Set metric rule structure. */
456 static const struct route_map_rule_cmd route_set_metric_cmd
= {
459 route_set_metric_compile
,
460 route_set_metric_free
,
463 /* `set metric-type TYPE' */
464 /* Set metric-type to attribute. */
465 static enum route_map_cmd_result_t
466 route_set_metric_type(void *rule
, const struct prefix
*prefix
, void *object
)
468 uint32_t *metric_type
;
469 struct external_info
*ei
;
471 /* Fetch routemap's rule information. */
475 /* Set metric out value. */
476 ei
->route_map_set
.metric_type
= *metric_type
;
481 /* set metric-type compilation. */
482 static void *route_set_metric_type_compile(const char *arg
)
484 uint32_t *metric_type
;
486 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
487 if (strcmp(arg
, "type-1") == 0)
488 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
489 else if (strcmp(arg
, "type-2") == 0)
490 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
492 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
493 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
496 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
500 /* Free route map's compiled `set metric-type' value. */
501 static void route_set_metric_type_free(void *rule
)
503 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
506 /* Set metric rule structure. */
507 static const struct route_map_rule_cmd route_set_metric_type_cmd
= {
509 route_set_metric_type
,
510 route_set_metric_type_compile
,
511 route_set_metric_type_free
,
514 static enum route_map_cmd_result_t
515 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
518 struct external_info
*ei
;
529 /* Route map commands for tag set. */
530 static const struct route_map_rule_cmd route_set_tag_cmd
= {
533 route_map_rule_tag_compile
,
534 route_map_rule_tag_free
,
537 DEFUN (set_metric_type
,
539 "set metric-type <type-1|type-2>",
541 "Type of metric for destination routing protocol\n"
542 "OSPF[6] external type 1 metric\n"
543 "OSPF[6] external type 2 metric\n")
545 char *ext
= argv
[2]->text
;
546 return generic_set_add(vty
, VTY_GET_CONTEXT(route_map_index
),
550 DEFUN (no_set_metric_type
,
551 no_set_metric_type_cmd
,
552 "no set metric-type [<type-1|type-2>]",
555 "Type of metric for destination routing protocol\n"
556 "OSPF[6] external type 1 metric\n"
557 "OSPF[6] external type 2 metric\n")
559 char *ext
= (argc
== 4) ? argv
[3]->text
: NULL
;
560 return generic_set_delete(vty
, VTY_GET_CONTEXT(route_map_index
),
565 void ospf_route_map_init(void)
569 route_map_add_hook(ospf_route_map_update
);
570 route_map_delete_hook(ospf_route_map_update
);
571 route_map_event_hook(ospf_route_map_event
);
573 route_map_set_metric_hook(generic_set_add
);
574 route_map_no_set_metric_hook(generic_set_delete
);
576 route_map_match_ip_next_hop_hook(generic_match_add
);
577 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
579 route_map_match_interface_hook(generic_match_add
);
580 route_map_no_match_interface_hook(generic_match_delete
);
582 route_map_match_ip_address_hook(generic_match_add
);
583 route_map_no_match_ip_address_hook(generic_match_delete
);
585 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
586 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
588 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
589 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
591 route_map_match_ip_next_hop_type_hook(generic_match_add
);
592 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
594 route_map_match_tag_hook(generic_match_add
);
595 route_map_no_match_tag_hook(generic_match_delete
);
597 route_map_set_metric_hook(generic_set_add
);
598 route_map_no_set_metric_hook(generic_set_delete
);
600 route_map_set_tag_hook(generic_set_add
);
601 route_map_no_set_tag_hook(generic_set_delete
);
603 route_map_install_match(&route_match_ip_nexthop_cmd
);
604 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
605 route_map_install_match(&route_match_ip_address_cmd
);
606 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
607 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
608 route_map_install_match(&route_match_interface_cmd
);
609 route_map_install_match(&route_match_tag_cmd
);
611 route_map_install_set(&route_set_metric_cmd
);
612 route_map_install_set(&route_set_metric_type_cmd
);
613 route_map_install_set(&route_set_tag_cmd
);
615 install_element(RMAP_NODE
, &set_metric_type_cmd
);
616 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);