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"
44 /* Hook function for updating route_map assignment. */
45 static void ospf_route_map_update(const char *name
)
49 struct listnode
*n1
= NULL
;
51 /* If OSPF instatnce does not exist, return right now. */
52 if (listcount(om
->ospf
) == 0)
55 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
56 /* Update route-map */
57 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
58 struct list
*red_list
;
59 struct listnode
*node
;
60 struct ospf_redist
*red
;
62 red_list
= ospf
->redist
[type
];
66 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
67 if (ROUTEMAP_NAME(red
)
68 && strcmp(ROUTEMAP_NAME(red
), name
) == 0) {
69 /* Keep old route-map. */
70 struct route_map
*old
= ROUTEMAP(red
);
72 /* Update route-map. */
74 route_map_lookup_by_name(
77 /* No update for this distribute type.
80 && ROUTEMAP(red
) == NULL
)
83 ospf_distribute_list_update(
84 ospf
, type
, red
->instance
);
91 static void ospf_route_map_event(route_map_event_t event
, const char *name
)
95 struct listnode
*n1
= NULL
;
97 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
98 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
99 struct list
*red_list
;
100 struct listnode
*node
;
101 struct ospf_redist
*red
;
103 red_list
= ospf
->redist
[type
];
107 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
108 if (ROUTEMAP_NAME(red
) && ROUTEMAP(red
)
109 && !strcmp(ROUTEMAP_NAME(red
), name
)) {
110 ospf_distribute_list_update(
111 ospf
, type
, red
->instance
);
118 /* `match ip netxthop ' */
119 /* Match function return 1 if match is success else return zero. */
120 static route_map_result_t
route_match_ip_nexthop(void *rule
,
121 struct prefix
*prefix
,
122 route_map_object_t type
,
125 struct access_list
*alist
;
126 struct external_info
*ei
= object
;
127 struct prefix_ipv4 p
;
129 if (type
== RMAP_OSPF
) {
131 p
.prefix
= ei
->nexthop
;
132 p
.prefixlen
= IPV4_MAX_BITLEN
;
134 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
138 return (access_list_apply(alist
, &p
) == FILTER_DENY
145 /* Route map `ip next-hop' match statement. `arg' should be
147 static void *route_match_ip_nexthop_compile(const char *arg
)
149 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
152 /* Free route map's compiled `ip address' value. */
153 static void route_match_ip_nexthop_free(void *rule
)
155 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
158 /* Route map commands for metric matching. */
159 struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
160 "ip next-hop", route_match_ip_nexthop
, route_match_ip_nexthop_compile
,
161 route_match_ip_nexthop_free
};
163 /* `match ip next-hop prefix-list PREFIX_LIST' */
165 static route_map_result_t
166 route_match_ip_next_hop_prefix_list(void *rule
, struct prefix
*prefix
,
167 route_map_object_t type
, void *object
)
169 struct prefix_list
*plist
;
170 struct external_info
*ei
= object
;
171 struct prefix_ipv4 p
;
173 if (type
== RMAP_OSPF
) {
175 p
.prefix
= ei
->nexthop
;
176 p
.prefixlen
= IPV4_MAX_BITLEN
;
178 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
182 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
189 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
191 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
194 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
196 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
199 struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd
= {
200 "ip next-hop prefix-list", 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
};
204 /* `match ip address IP_ACCESS_LIST' */
205 /* Match function should return 1 if match is success else return
207 static route_map_result_t
route_match_ip_address(void *rule
,
208 struct prefix
*prefix
,
209 route_map_object_t type
,
212 struct access_list
*alist
;
213 /* struct prefix_ipv4 match; */
215 if (type
== RMAP_OSPF
) {
216 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
220 return (access_list_apply(alist
, prefix
) == FILTER_DENY
227 /* Route map `ip address' match statement. `arg' should be
229 static void *route_match_ip_address_compile(const char *arg
)
231 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
234 /* Free route map's compiled `ip address' value. */
235 static void route_match_ip_address_free(void *rule
)
237 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
240 /* Route map commands for ip address matching. */
241 struct route_map_rule_cmd route_match_ip_address_cmd
= {
242 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
243 route_match_ip_address_free
};
245 /* `match ip address prefix-list PREFIX_LIST' */
246 static route_map_result_t
247 route_match_ip_address_prefix_list(void *rule
, struct prefix
*prefix
,
248 route_map_object_t type
, void *object
)
250 struct prefix_list
*plist
;
252 if (type
== RMAP_OSPF
) {
253 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
257 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
264 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
266 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
269 static void route_match_ip_address_prefix_list_free(void *rule
)
271 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
274 struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
275 "ip address prefix-list", route_match_ip_address_prefix_list
,
276 route_match_ip_address_prefix_list_compile
,
277 route_match_ip_address_prefix_list_free
};
279 /* `match interface IFNAME' */
280 /* Match function should return 1 if match is success else return
282 static route_map_result_t
route_match_interface(void *rule
,
283 struct prefix
*prefix
,
284 route_map_object_t type
,
287 struct interface
*ifp
;
288 struct external_info
*ei
;
290 if (type
== RMAP_OSPF
) {
292 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
294 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
302 /* Route map `interface' match statement. `arg' should be
304 static void *route_match_interface_compile(const char *arg
)
306 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
309 /* Free route map's compiled `interface' value. */
310 static void route_match_interface_free(void *rule
)
312 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
315 /* Route map commands for ip address matching. */
316 struct route_map_rule_cmd route_match_interface_cmd
= {
317 "interface", route_match_interface
, route_match_interface_compile
,
318 route_match_interface_free
};
320 /* Match function return 1 if match is success else return zero. */
321 static route_map_result_t
route_match_tag(void *rule
, struct prefix
*prefix
,
322 route_map_object_t type
, void *object
)
325 struct external_info
*ei
;
327 if (type
== RMAP_OSPF
) {
331 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
337 /* Route map commands for tag matching. */
338 static struct route_map_rule_cmd route_match_tag_cmd
= {
339 "tag", route_match_tag
, route_map_rule_tag_compile
,
340 route_map_rule_tag_free
,
344 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
349 /* `set metric METRIC' */
350 /* Set metric to attribute. */
351 static route_map_result_t
route_set_metric(void *rule
, struct prefix
*prefix
,
352 route_map_object_t type
,
355 struct ospf_metric
*metric
;
356 struct external_info
*ei
;
358 if (type
== RMAP_OSPF
) {
359 /* Fetch routemap's rule information. */
363 /* Set metric out value. */
366 if (metric
->type
== metric_increment
)
367 ei
->route_map_set
.metric
+= metric
->metric
;
368 if (metric
->type
== metric_decrement
)
369 ei
->route_map_set
.metric
-= metric
->metric
;
370 if (metric
->type
== metric_absolute
)
371 ei
->route_map_set
.metric
= metric
->metric
;
373 if ((signed int)ei
->route_map_set
.metric
< 1)
374 ei
->route_map_set
.metric
= -1;
375 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
376 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
381 /* set metric compilation. */
382 static void *route_set_metric_compile(const char *arg
)
384 struct ospf_metric
*metric
;
386 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
387 metric
->used
= false;
390 metric
->type
= metric_absolute
;
392 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
393 zlog_warn("OSPF does not support 'set metric +rtt / -rtt'");
397 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
398 metric
->type
= metric_increment
;
402 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
403 metric
->type
= metric_decrement
;
407 metric
->metric
= strtoul(arg
, NULL
, 10);
415 /* Free route map's compiled `set metric' value. */
416 static void route_set_metric_free(void *rule
)
418 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
421 /* Set metric rule structure. */
422 struct route_map_rule_cmd route_set_metric_cmd
= {
423 "metric", route_set_metric
, route_set_metric_compile
,
424 route_set_metric_free
,
427 /* `set metric-type TYPE' */
428 /* Set metric-type to attribute. */
429 static route_map_result_t
route_set_metric_type(void *rule
,
430 struct prefix
*prefix
,
431 route_map_object_t type
,
434 uint32_t *metric_type
;
435 struct external_info
*ei
;
437 if (type
== RMAP_OSPF
) {
438 /* Fetch routemap's rule information. */
442 /* Set metric out value. */
443 ei
->route_map_set
.metric_type
= *metric_type
;
448 /* set metric-type compilation. */
449 static void *route_set_metric_type_compile(const char *arg
)
451 uint32_t *metric_type
;
453 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
454 if (strcmp(arg
, "type-1") == 0)
455 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
456 else if (strcmp(arg
, "type-2") == 0)
457 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
459 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
460 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
463 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
467 /* Free route map's compiled `set metric-type' value. */
468 static void route_set_metric_type_free(void *rule
)
470 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
473 /* Set metric rule structure. */
474 struct route_map_rule_cmd route_set_metric_type_cmd
= {
475 "metric-type", route_set_metric_type
, route_set_metric_type_compile
,
476 route_set_metric_type_free
,
479 static route_map_result_t
route_set_tag(void *rule
, struct prefix
*prefix
,
480 route_map_object_t type
, void *object
)
483 struct external_info
*ei
;
485 if (type
== RMAP_OSPF
) {
496 /* Route map commands for tag set. */
497 static struct route_map_rule_cmd route_set_tag_cmd
= {
498 "tag", route_set_tag
, route_map_rule_tag_compile
,
499 route_map_rule_tag_free
,
502 DEFUN (set_metric_type
,
504 "set metric-type <type-1|type-2>",
506 "Type of metric for destination routing protocol\n"
507 "OSPF[6] external type 1 metric\n"
508 "OSPF[6] external type 2 metric\n")
510 char *ext
= argv
[2]->text
;
511 return generic_set_add(vty
, VTY_GET_CONTEXT(route_map_index
),
515 DEFUN (no_set_metric_type
,
516 no_set_metric_type_cmd
,
517 "no set metric-type [<type-1|type-2>]",
520 "Type of metric for destination routing protocol\n"
521 "OSPF[6] external type 1 metric\n"
522 "OSPF[6] external type 2 metric\n")
524 char *ext
= (argc
== 4) ? argv
[3]->text
: NULL
;
525 return generic_set_delete(vty
, VTY_GET_CONTEXT(route_map_index
),
530 void ospf_route_map_init(void)
534 route_map_add_hook(ospf_route_map_update
);
535 route_map_delete_hook(ospf_route_map_update
);
536 route_map_event_hook(ospf_route_map_event
);
538 route_map_set_metric_hook(generic_set_add
);
539 route_map_no_set_metric_hook(generic_set_delete
);
541 route_map_match_ip_next_hop_hook(generic_match_add
);
542 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
544 route_map_match_interface_hook(generic_match_add
);
545 route_map_no_match_interface_hook(generic_match_delete
);
547 route_map_match_ip_address_hook(generic_match_add
);
548 route_map_no_match_ip_address_hook(generic_match_delete
);
550 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
551 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
553 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
554 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
556 route_map_match_tag_hook(generic_match_add
);
557 route_map_no_match_tag_hook(generic_match_delete
);
559 route_map_set_metric_hook(generic_set_add
);
560 route_map_no_set_metric_hook(generic_set_delete
);
562 route_map_set_tag_hook(generic_set_add
);
563 route_map_no_set_tag_hook(generic_set_delete
);
565 route_map_install_match(&route_match_ip_nexthop_cmd
);
566 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
567 route_map_install_match(&route_match_ip_address_cmd
);
568 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
569 route_map_install_match(&route_match_interface_cmd
);
570 route_map_install_match(&route_match_tag_cmd
);
572 route_map_install_set(&route_set_metric_cmd
);
573 route_map_install_set(&route_set_metric_type_cmd
);
574 route_map_install_set(&route_set_tag_cmd
);
576 install_element(RMAP_NODE
, &set_metric_type_cmd
);
577 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);