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 struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
168 "ip next-hop", route_match_ip_nexthop
, route_match_ip_nexthop_compile
,
169 route_match_ip_nexthop_free
};
171 /* `match ip next-hop prefix-list PREFIX_LIST' */
173 static enum route_map_cmd_result_t
174 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
175 route_map_object_t type
, void *object
)
177 struct prefix_list
*plist
;
178 struct external_info
*ei
= object
;
179 struct prefix_ipv4 p
;
181 if (type
== RMAP_OSPF
) {
183 p
.prefix
= ei
->nexthop
;
184 p
.prefixlen
= IPV4_MAX_BITLEN
;
186 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
190 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
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 struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd
= {
208 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list
,
209 route_match_ip_next_hop_prefix_list_compile
,
210 route_match_ip_next_hop_prefix_list_free
};
212 /* `match ip next-hop type <blackhole>' */
214 static enum route_map_cmd_result_t
215 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
216 route_map_object_t type
, void *object
)
218 struct external_info
*ei
= object
;
220 if (type
== RMAP_OSPF
&& prefix
->family
== AF_INET
) {
221 ei
= (struct external_info
*)object
;
225 if (ei
->nexthop
.s_addr
== INADDR_ANY
&& !ei
->ifindex
)
231 static void *route_match_ip_next_hop_type_compile(const char *arg
)
233 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
236 static void route_match_ip_next_hop_type_free(void *rule
)
238 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
241 static struct route_map_rule_cmd route_match_ip_next_hop_type_cmd
= {
242 "ip next-hop type", route_match_ip_next_hop_type
,
243 route_match_ip_next_hop_type_compile
,
244 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
,
251 route_map_object_t type
, void *object
)
253 struct access_list
*alist
;
254 /* struct prefix_ipv4 match; */
256 if (type
== RMAP_OSPF
) {
257 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
261 return (access_list_apply(alist
, prefix
) == FILTER_DENY
268 /* Route map `ip address' match statement. `arg' should be
270 static void *route_match_ip_address_compile(const char *arg
)
272 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
275 /* Free route map's compiled `ip address' value. */
276 static void route_match_ip_address_free(void *rule
)
278 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
281 /* Route map commands for ip address matching. */
282 struct route_map_rule_cmd route_match_ip_address_cmd
= {
283 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
284 route_match_ip_address_free
};
286 /* `match ip address prefix-list PREFIX_LIST' */
287 static enum route_map_cmd_result_t
288 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
289 route_map_object_t type
, void *object
)
291 struct prefix_list
*plist
;
293 if (type
== RMAP_OSPF
) {
294 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
298 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
305 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
307 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
310 static void route_match_ip_address_prefix_list_free(void *rule
)
312 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
315 struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
316 "ip address prefix-list", route_match_ip_address_prefix_list
,
317 route_match_ip_address_prefix_list_compile
,
318 route_match_ip_address_prefix_list_free
};
320 /* `match interface IFNAME' */
321 /* Match function should return 1 if match is success else return
323 static enum route_map_cmd_result_t
324 route_match_interface(void *rule
, const struct prefix
*prefix
,
325 route_map_object_t type
, void *object
)
327 struct interface
*ifp
;
328 struct external_info
*ei
;
330 if (type
== RMAP_OSPF
) {
332 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
334 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
342 /* Route map `interface' match statement. `arg' should be
344 static void *route_match_interface_compile(const char *arg
)
346 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
349 /* Free route map's compiled `interface' value. */
350 static void route_match_interface_free(void *rule
)
352 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
355 /* Route map commands for ip address matching. */
356 struct route_map_rule_cmd route_match_interface_cmd
= {
357 "interface", route_match_interface
, route_match_interface_compile
,
358 route_match_interface_free
};
360 /* Match function return 1 if match is success else return zero. */
361 static enum route_map_cmd_result_t
362 route_match_tag(void *rule
, const struct prefix
*prefix
,
363 route_map_object_t type
, void *object
)
366 struct external_info
*ei
;
368 if (type
== RMAP_OSPF
) {
372 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
378 /* Route map commands for tag matching. */
379 static struct route_map_rule_cmd route_match_tag_cmd
= {
380 "tag", route_match_tag
, route_map_rule_tag_compile
,
381 route_map_rule_tag_free
,
385 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
390 /* `set metric METRIC' */
391 /* Set metric to attribute. */
392 static enum route_map_cmd_result_t
393 route_set_metric(void *rule
, const struct prefix
*prefix
,
394 route_map_object_t type
, void *object
)
396 struct ospf_metric
*metric
;
397 struct external_info
*ei
;
399 if (type
== RMAP_OSPF
) {
400 /* Fetch routemap's rule information. */
404 /* Set metric out value. */
408 ei
->route_map_set
.metric
= DEFAULT_DEFAULT_METRIC
;
410 if (metric
->type
== metric_increment
)
411 ei
->route_map_set
.metric
+= metric
->metric
;
412 else if (metric
->type
== metric_decrement
)
413 ei
->route_map_set
.metric
-= metric
->metric
;
414 else if (metric
->type
== metric_absolute
)
415 ei
->route_map_set
.metric
= metric
->metric
;
417 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
418 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
423 /* set metric compilation. */
424 static void *route_set_metric_compile(const char *arg
)
426 struct ospf_metric
*metric
;
428 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
429 metric
->used
= false;
432 metric
->type
= metric_absolute
;
434 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
435 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
436 "OSPF does not support 'set metric +rtt / -rtt'");
440 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
441 metric
->type
= metric_increment
;
445 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
446 metric
->type
= metric_decrement
;
450 metric
->metric
= strtoul(arg
, NULL
, 10);
458 /* Free route map's compiled `set metric' value. */
459 static void route_set_metric_free(void *rule
)
461 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
464 /* Set metric rule structure. */
465 struct route_map_rule_cmd route_set_metric_cmd
= {
466 "metric", route_set_metric
, route_set_metric_compile
,
467 route_set_metric_free
,
470 /* `set metric-type TYPE' */
471 /* Set metric-type to attribute. */
472 static enum route_map_cmd_result_t
473 route_set_metric_type(void *rule
, const struct prefix
*prefix
,
474 route_map_object_t type
, void *object
)
476 uint32_t *metric_type
;
477 struct external_info
*ei
;
479 if (type
== RMAP_OSPF
) {
480 /* Fetch routemap's rule information. */
484 /* Set metric out value. */
485 ei
->route_map_set
.metric_type
= *metric_type
;
490 /* set metric-type compilation. */
491 static void *route_set_metric_type_compile(const char *arg
)
493 uint32_t *metric_type
;
495 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
496 if (strcmp(arg
, "type-1") == 0)
497 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
498 else if (strcmp(arg
, "type-2") == 0)
499 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
501 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
502 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
505 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
509 /* Free route map's compiled `set metric-type' value. */
510 static void route_set_metric_type_free(void *rule
)
512 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
515 /* Set metric rule structure. */
516 struct route_map_rule_cmd route_set_metric_type_cmd
= {
517 "metric-type", route_set_metric_type
, route_set_metric_type_compile
,
518 route_set_metric_type_free
,
521 static enum route_map_cmd_result_t
522 route_set_tag(void *rule
, const struct prefix
*prefix
, route_map_object_t type
,
526 struct external_info
*ei
;
528 if (type
== RMAP_OSPF
) {
539 /* Route map commands for tag set. */
540 static struct route_map_rule_cmd route_set_tag_cmd
= {
541 "tag", route_set_tag
, route_map_rule_tag_compile
,
542 route_map_rule_tag_free
,
545 DEFUN (set_metric_type
,
547 "set metric-type <type-1|type-2>",
549 "Type of metric for destination routing protocol\n"
550 "OSPF[6] external type 1 metric\n"
551 "OSPF[6] external type 2 metric\n")
553 char *ext
= argv
[2]->text
;
554 return generic_set_add(vty
, VTY_GET_CONTEXT(route_map_index
),
558 DEFUN (no_set_metric_type
,
559 no_set_metric_type_cmd
,
560 "no set metric-type [<type-1|type-2>]",
563 "Type of metric for destination routing protocol\n"
564 "OSPF[6] external type 1 metric\n"
565 "OSPF[6] external type 2 metric\n")
567 char *ext
= (argc
== 4) ? argv
[3]->text
: NULL
;
568 return generic_set_delete(vty
, VTY_GET_CONTEXT(route_map_index
),
573 void ospf_route_map_init(void)
577 route_map_add_hook(ospf_route_map_update
);
578 route_map_delete_hook(ospf_route_map_update
);
579 route_map_event_hook(ospf_route_map_event
);
581 route_map_set_metric_hook(generic_set_add
);
582 route_map_no_set_metric_hook(generic_set_delete
);
584 route_map_match_ip_next_hop_hook(generic_match_add
);
585 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
587 route_map_match_interface_hook(generic_match_add
);
588 route_map_no_match_interface_hook(generic_match_delete
);
590 route_map_match_ip_address_hook(generic_match_add
);
591 route_map_no_match_ip_address_hook(generic_match_delete
);
593 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
594 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
596 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
597 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
599 route_map_match_ip_next_hop_type_hook(generic_match_add
);
600 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
602 route_map_match_tag_hook(generic_match_add
);
603 route_map_no_match_tag_hook(generic_match_delete
);
605 route_map_set_metric_hook(generic_set_add
);
606 route_map_no_set_metric_hook(generic_set_delete
);
608 route_map_set_tag_hook(generic_set_add
);
609 route_map_no_set_tag_hook(generic_set_delete
);
611 route_map_install_match(&route_match_ip_nexthop_cmd
);
612 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
613 route_map_install_match(&route_match_ip_address_cmd
);
614 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
615 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
616 route_map_install_match(&route_match_interface_cmd
);
617 route_map_install_match(&route_match_tag_cmd
);
619 route_map_install_set(&route_set_metric_cmd
);
620 route_map_install_set(&route_set_metric_type_cmd
);
621 route_map_install_set(&route_set_tag_cmd
);
623 install_element(RMAP_NODE
, &set_metric_type_cmd
);
624 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);