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 "ospfd/ospfd.h"
37 #include "ospfd/ospf_asbr.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_lsa.h"
40 #include "ospfd/ospf_route.h"
41 #include "ospfd/ospf_zebra.h"
43 /* Hook function for updating route_map assignment. */
44 static void ospf_route_map_update(const char *name
)
48 struct listnode
*n1
= NULL
;
50 /* If OSPF instatnce does not exist, return right now. */
51 if (listcount(om
->ospf
) == 0)
54 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
55 /* Update route-map */
56 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
57 struct list
*red_list
;
58 struct listnode
*node
;
59 struct ospf_redist
*red
;
61 red_list
= ospf
->redist
[type
];
65 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
66 if (ROUTEMAP_NAME(red
)
67 && strcmp(ROUTEMAP_NAME(red
), name
) == 0) {
68 /* Keep old route-map. */
69 struct route_map
*old
= ROUTEMAP(red
);
71 /* Update route-map. */
73 route_map_lookup_by_name(
76 /* No update for this distribute type.
79 && ROUTEMAP(red
) == NULL
)
82 ospf_distribute_list_update(
83 ospf
, type
, red
->instance
);
90 static void ospf_route_map_event(route_map_event_t event
, const char *name
)
94 struct listnode
*n1
= NULL
;
96 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
97 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
98 struct list
*red_list
;
99 struct listnode
*node
;
100 struct ospf_redist
*red
;
102 red_list
= ospf
->redist
[type
];
106 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
107 if (ROUTEMAP_NAME(red
) && ROUTEMAP(red
)
108 && !strcmp(ROUTEMAP_NAME(red
), name
)) {
109 ospf_distribute_list_update(
110 ospf
, type
, red
->instance
);
117 /* `match ip netxthop ' */
118 /* Match function return 1 if match is success else return zero. */
119 static route_map_result_t
route_match_ip_nexthop(void *rule
,
120 struct prefix
*prefix
,
121 route_map_object_t type
,
124 struct access_list
*alist
;
125 struct external_info
*ei
= object
;
126 struct prefix_ipv4 p
;
128 if (type
== RMAP_OSPF
) {
130 p
.prefix
= ei
->nexthop
;
131 p
.prefixlen
= IPV4_MAX_BITLEN
;
133 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
137 return (access_list_apply(alist
, &p
) == FILTER_DENY
144 /* Route map `ip next-hop' match statement. `arg' should be
146 static void *route_match_ip_nexthop_compile(const char *arg
)
148 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
151 /* Free route map's compiled `ip address' value. */
152 static void route_match_ip_nexthop_free(void *rule
)
154 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
157 /* Route map commands for metric matching. */
158 struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
159 "ip next-hop", route_match_ip_nexthop
, route_match_ip_nexthop_compile
,
160 route_match_ip_nexthop_free
};
162 /* `match ip next-hop prefix-list PREFIX_LIST' */
164 static route_map_result_t
165 route_match_ip_next_hop_prefix_list(void *rule
, struct prefix
*prefix
,
166 route_map_object_t type
, void *object
)
168 struct prefix_list
*plist
;
169 struct external_info
*ei
= object
;
170 struct prefix_ipv4 p
;
172 if (type
== RMAP_OSPF
) {
174 p
.prefix
= ei
->nexthop
;
175 p
.prefixlen
= IPV4_MAX_BITLEN
;
177 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
181 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
188 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
190 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
193 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
195 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
198 struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd
= {
199 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list
,
200 route_match_ip_next_hop_prefix_list_compile
,
201 route_match_ip_next_hop_prefix_list_free
};
203 /* `match ip address IP_ACCESS_LIST' */
204 /* Match function should return 1 if match is success else return
206 static route_map_result_t
route_match_ip_address(void *rule
,
207 struct prefix
*prefix
,
208 route_map_object_t type
,
211 struct access_list
*alist
;
212 /* struct prefix_ipv4 match; */
214 if (type
== RMAP_OSPF
) {
215 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
219 return (access_list_apply(alist
, prefix
) == FILTER_DENY
226 /* Route map `ip address' match statement. `arg' should be
228 static void *route_match_ip_address_compile(const char *arg
)
230 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
233 /* Free route map's compiled `ip address' value. */
234 static void route_match_ip_address_free(void *rule
)
236 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
239 /* Route map commands for ip address matching. */
240 struct route_map_rule_cmd route_match_ip_address_cmd
= {
241 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
242 route_match_ip_address_free
};
244 /* `match ip address prefix-list PREFIX_LIST' */
245 static route_map_result_t
246 route_match_ip_address_prefix_list(void *rule
, struct prefix
*prefix
,
247 route_map_object_t type
, void *object
)
249 struct prefix_list
*plist
;
251 if (type
== RMAP_OSPF
) {
252 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
256 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
263 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
265 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
268 static void route_match_ip_address_prefix_list_free(void *rule
)
270 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
273 struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
274 "ip address prefix-list", route_match_ip_address_prefix_list
,
275 route_match_ip_address_prefix_list_compile
,
276 route_match_ip_address_prefix_list_free
};
278 /* `match interface IFNAME' */
279 /* Match function should return 1 if match is success else return
281 static route_map_result_t
route_match_interface(void *rule
,
282 struct prefix
*prefix
,
283 route_map_object_t type
,
286 struct interface
*ifp
;
287 struct external_info
*ei
;
289 if (type
== RMAP_OSPF
) {
291 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
293 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
301 /* Route map `interface' match statement. `arg' should be
303 static void *route_match_interface_compile(const char *arg
)
305 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
308 /* Free route map's compiled `interface' value. */
309 static void route_match_interface_free(void *rule
)
311 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
314 /* Route map commands for ip address matching. */
315 struct route_map_rule_cmd route_match_interface_cmd
= {
316 "interface", route_match_interface
, route_match_interface_compile
,
317 route_match_interface_free
};
319 /* Match function return 1 if match is success else return zero. */
320 static route_map_result_t
route_match_tag(void *rule
, struct prefix
*prefix
,
321 route_map_object_t type
, void *object
)
324 struct external_info
*ei
;
326 if (type
== RMAP_OSPF
) {
330 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
336 /* Route map commands for tag matching. */
337 static struct route_map_rule_cmd route_match_tag_cmd
= {
338 "tag", route_match_tag
, route_map_rule_tag_compile
,
339 route_map_rule_tag_free
,
343 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
348 /* `set metric METRIC' */
349 /* Set metric to attribute. */
350 static route_map_result_t
route_set_metric(void *rule
, struct prefix
*prefix
,
351 route_map_object_t type
,
354 struct ospf_metric
*metric
;
355 struct external_info
*ei
;
357 if (type
== RMAP_OSPF
) {
358 /* Fetch routemap's rule information. */
362 /* Set metric out value. */
365 if (metric
->type
== metric_increment
)
366 ei
->route_map_set
.metric
+= metric
->metric
;
367 if (metric
->type
== metric_decrement
)
368 ei
->route_map_set
.metric
-= metric
->metric
;
369 if (metric
->type
== metric_absolute
)
370 ei
->route_map_set
.metric
= metric
->metric
;
372 if ((signed int)ei
->route_map_set
.metric
< 1)
373 ei
->route_map_set
.metric
= -1;
374 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
375 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
380 /* set metric compilation. */
381 static void *route_set_metric_compile(const char *arg
)
383 struct ospf_metric
*metric
;
385 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
386 metric
->used
= false;
389 metric
->type
= metric_absolute
;
391 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
392 zlog_warn("OSPF does not support 'set metric +rtt / -rtt'");
396 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
397 metric
->type
= metric_increment
;
401 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
402 metric
->type
= metric_decrement
;
406 metric
->metric
= strtoul(arg
, NULL
, 10);
414 /* Free route map's compiled `set metric' value. */
415 static void route_set_metric_free(void *rule
)
417 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
420 /* Set metric rule structure. */
421 struct route_map_rule_cmd route_set_metric_cmd
= {
422 "metric", route_set_metric
, route_set_metric_compile
,
423 route_set_metric_free
,
426 /* `set metric-type TYPE' */
427 /* Set metric-type to attribute. */
428 static route_map_result_t
route_set_metric_type(void *rule
,
429 struct prefix
*prefix
,
430 route_map_object_t type
,
433 uint32_t *metric_type
;
434 struct external_info
*ei
;
436 if (type
== RMAP_OSPF
) {
437 /* Fetch routemap's rule information. */
441 /* Set metric out value. */
442 ei
->route_map_set
.metric_type
= *metric_type
;
447 /* set metric-type compilation. */
448 static void *route_set_metric_type_compile(const char *arg
)
450 uint32_t *metric_type
;
452 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
453 if (strcmp(arg
, "type-1") == 0)
454 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
455 else if (strcmp(arg
, "type-2") == 0)
456 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
458 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
459 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
462 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
466 /* Free route map's compiled `set metric-type' value. */
467 static void route_set_metric_type_free(void *rule
)
469 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
472 /* Set metric rule structure. */
473 struct route_map_rule_cmd route_set_metric_type_cmd
= {
474 "metric-type", route_set_metric_type
, route_set_metric_type_compile
,
475 route_set_metric_type_free
,
478 static route_map_result_t
route_set_tag(void *rule
, struct prefix
*prefix
,
479 route_map_object_t type
, void *object
)
482 struct external_info
*ei
;
484 if (type
== RMAP_OSPF
) {
495 /* Route map commands for tag set. */
496 static struct route_map_rule_cmd route_set_tag_cmd
= {
497 "tag", route_set_tag
, route_map_rule_tag_compile
,
498 route_map_rule_tag_free
,
501 DEFUN (set_metric_type
,
503 "set metric-type <type-1|type-2>",
505 "Type of metric for destination routing protocol\n"
506 "OSPF[6] external type 1 metric\n"
507 "OSPF[6] external type 2 metric\n")
509 char *ext
= argv
[2]->text
;
510 return generic_set_add(vty
, VTY_GET_CONTEXT(route_map_index
),
514 DEFUN (no_set_metric_type
,
515 no_set_metric_type_cmd
,
516 "no set metric-type [<type-1|type-2>]",
519 "Type of metric for destination routing protocol\n"
520 "OSPF[6] external type 1 metric\n"
521 "OSPF[6] external type 2 metric\n")
523 char *ext
= (argc
== 4) ? argv
[3]->text
: NULL
;
524 return generic_set_delete(vty
, VTY_GET_CONTEXT(route_map_index
),
529 void ospf_route_map_init(void)
533 route_map_add_hook(ospf_route_map_update
);
534 route_map_delete_hook(ospf_route_map_update
);
535 route_map_event_hook(ospf_route_map_event
);
537 route_map_set_metric_hook(generic_set_add
);
538 route_map_no_set_metric_hook(generic_set_delete
);
540 route_map_match_ip_next_hop_hook(generic_match_add
);
541 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
543 route_map_match_interface_hook(generic_match_add
);
544 route_map_no_match_interface_hook(generic_match_delete
);
546 route_map_match_ip_address_hook(generic_match_add
);
547 route_map_no_match_ip_address_hook(generic_match_delete
);
549 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
550 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
552 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
553 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
555 route_map_match_tag_hook(generic_match_add
);
556 route_map_no_match_tag_hook(generic_match_delete
);
558 route_map_set_metric_hook(generic_set_add
);
559 route_map_no_set_metric_hook(generic_set_delete
);
561 route_map_set_tag_hook(generic_set_add
);
562 route_map_no_set_tag_hook(generic_set_delete
);
564 route_map_install_match(&route_match_ip_nexthop_cmd
);
565 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
566 route_map_install_match(&route_match_ip_address_cmd
);
567 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
568 route_map_install_match(&route_match_interface_cmd
);
569 route_map_install_match(&route_match_tag_cmd
);
571 route_map_install_set(&route_set_metric_cmd
);
572 route_map_install_set(&route_set_metric_type_cmd
);
573 route_map_install_set(&route_set_tag_cmd
);
575 install_element(RMAP_NODE
, &set_metric_type_cmd
);
576 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);