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 route_map_result_t
route_match_ip_nexthop(void *rule
,
130 const struct prefix
*prefix
,
131 route_map_object_t type
,
134 struct access_list
*alist
;
135 struct external_info
*ei
= object
;
136 struct prefix_ipv4 p
;
138 if (type
== RMAP_OSPF
) {
140 p
.prefix
= ei
->nexthop
;
141 p
.prefixlen
= IPV4_MAX_BITLEN
;
143 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
147 return (access_list_apply(alist
, &p
) == FILTER_DENY
154 /* Route map `ip next-hop' match statement. `arg' should be
156 static void *route_match_ip_nexthop_compile(const char *arg
)
158 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
161 /* Free route map's compiled `ip address' value. */
162 static void route_match_ip_nexthop_free(void *rule
)
164 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
167 /* Route map commands for metric matching. */
168 struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
169 "ip next-hop", route_match_ip_nexthop
, route_match_ip_nexthop_compile
,
170 route_match_ip_nexthop_free
};
172 /* `match ip next-hop prefix-list PREFIX_LIST' */
174 static route_map_result_t
175 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
176 route_map_object_t type
, void *object
)
178 struct prefix_list
*plist
;
179 struct external_info
*ei
= object
;
180 struct prefix_ipv4 p
;
182 if (type
== RMAP_OSPF
) {
184 p
.prefix
= ei
->nexthop
;
185 p
.prefixlen
= IPV4_MAX_BITLEN
;
187 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
191 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
198 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
200 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
203 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
205 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
208 struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd
= {
209 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list
,
210 route_match_ip_next_hop_prefix_list_compile
,
211 route_match_ip_next_hop_prefix_list_free
};
213 /* `match ip address IP_ACCESS_LIST' */
214 /* Match function should return 1 if match is success else return
216 static route_map_result_t
route_match_ip_address(void *rule
,
217 const struct prefix
*prefix
,
218 route_map_object_t type
,
221 struct access_list
*alist
;
222 /* struct prefix_ipv4 match; */
224 if (type
== RMAP_OSPF
) {
225 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
229 return (access_list_apply(alist
, prefix
) == FILTER_DENY
236 /* Route map `ip address' match statement. `arg' should be
238 static void *route_match_ip_address_compile(const char *arg
)
240 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
243 /* Free route map's compiled `ip address' value. */
244 static void route_match_ip_address_free(void *rule
)
246 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
249 /* Route map commands for ip address matching. */
250 struct route_map_rule_cmd route_match_ip_address_cmd
= {
251 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
252 route_match_ip_address_free
};
254 /* `match ip address prefix-list PREFIX_LIST' */
255 static route_map_result_t
256 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
257 route_map_object_t type
, void *object
)
259 struct prefix_list
*plist
;
261 if (type
== RMAP_OSPF
) {
262 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
266 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
273 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
275 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
278 static void route_match_ip_address_prefix_list_free(void *rule
)
280 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
283 struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
284 "ip address prefix-list", route_match_ip_address_prefix_list
,
285 route_match_ip_address_prefix_list_compile
,
286 route_match_ip_address_prefix_list_free
};
288 /* `match interface IFNAME' */
289 /* Match function should return 1 if match is success else return
291 static route_map_result_t
route_match_interface(void *rule
,
292 const struct prefix
*prefix
,
293 route_map_object_t type
,
296 struct interface
*ifp
;
297 struct external_info
*ei
;
299 if (type
== RMAP_OSPF
) {
301 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
303 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
311 /* Route map `interface' match statement. `arg' should be
313 static void *route_match_interface_compile(const char *arg
)
315 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
318 /* Free route map's compiled `interface' value. */
319 static void route_match_interface_free(void *rule
)
321 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
324 /* Route map commands for ip address matching. */
325 struct route_map_rule_cmd route_match_interface_cmd
= {
326 "interface", route_match_interface
, route_match_interface_compile
,
327 route_match_interface_free
};
329 /* Match function return 1 if match is success else return zero. */
330 static route_map_result_t
route_match_tag(void *rule
,
331 const struct prefix
*prefix
,
332 route_map_object_t type
, void *object
)
335 struct external_info
*ei
;
337 if (type
== RMAP_OSPF
) {
341 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
347 /* Route map commands for tag matching. */
348 static struct route_map_rule_cmd route_match_tag_cmd
= {
349 "tag", route_match_tag
, route_map_rule_tag_compile
,
350 route_map_rule_tag_free
,
354 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
359 /* `set metric METRIC' */
360 /* Set metric to attribute. */
361 static route_map_result_t
route_set_metric(void *rule
,
362 const struct prefix
*prefix
,
363 route_map_object_t type
,
366 struct ospf_metric
*metric
;
367 struct external_info
*ei
;
369 if (type
== RMAP_OSPF
) {
370 /* Fetch routemap's rule information. */
374 /* Set metric out value. */
378 ei
->route_map_set
.metric
= DEFAULT_DEFAULT_METRIC
;
380 if (metric
->type
== metric_increment
)
381 ei
->route_map_set
.metric
+= metric
->metric
;
382 else if (metric
->type
== metric_decrement
)
383 ei
->route_map_set
.metric
-= metric
->metric
;
384 else if (metric
->type
== metric_absolute
)
385 ei
->route_map_set
.metric
= metric
->metric
;
387 if (ei
->route_map_set
.metric
> OSPF_LS_INFINITY
)
388 ei
->route_map_set
.metric
= OSPF_LS_INFINITY
;
393 /* set metric compilation. */
394 static void *route_set_metric_compile(const char *arg
)
396 struct ospf_metric
*metric
;
398 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
399 metric
->used
= false;
402 metric
->type
= metric_absolute
;
404 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt")) {
405 flog_warn(EC_OSPF_SET_METRIC_PLUS
,
406 "OSPF does not support 'set metric +rtt / -rtt'");
410 if ((arg
[0] == '+') && all_digit(arg
+ 1)) {
411 metric
->type
= metric_increment
;
415 if ((arg
[0] == '-') && all_digit(arg
+ 1)) {
416 metric
->type
= metric_decrement
;
420 metric
->metric
= strtoul(arg
, NULL
, 10);
428 /* Free route map's compiled `set metric' value. */
429 static void route_set_metric_free(void *rule
)
431 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
434 /* Set metric rule structure. */
435 struct route_map_rule_cmd route_set_metric_cmd
= {
436 "metric", route_set_metric
, route_set_metric_compile
,
437 route_set_metric_free
,
440 /* `set metric-type TYPE' */
441 /* Set metric-type to attribute. */
442 static route_map_result_t
route_set_metric_type(void *rule
,
443 const struct prefix
*prefix
,
444 route_map_object_t type
,
447 uint32_t *metric_type
;
448 struct external_info
*ei
;
450 if (type
== RMAP_OSPF
) {
451 /* Fetch routemap's rule information. */
455 /* Set metric out value. */
456 ei
->route_map_set
.metric_type
= *metric_type
;
461 /* set metric-type compilation. */
462 static void *route_set_metric_type_compile(const char *arg
)
464 uint32_t *metric_type
;
466 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
467 if (strcmp(arg
, "type-1") == 0)
468 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
469 else if (strcmp(arg
, "type-2") == 0)
470 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
472 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
473 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
476 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
480 /* Free route map's compiled `set metric-type' value. */
481 static void route_set_metric_type_free(void *rule
)
483 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
486 /* Set metric rule structure. */
487 struct route_map_rule_cmd route_set_metric_type_cmd
= {
488 "metric-type", route_set_metric_type
, route_set_metric_type_compile
,
489 route_set_metric_type_free
,
492 static route_map_result_t
route_set_tag(void *rule
, const struct prefix
*prefix
,
493 route_map_object_t type
, void *object
)
496 struct external_info
*ei
;
498 if (type
== RMAP_OSPF
) {
509 /* Route map commands for tag set. */
510 static struct route_map_rule_cmd route_set_tag_cmd
= {
511 "tag", route_set_tag
, route_map_rule_tag_compile
,
512 route_map_rule_tag_free
,
515 DEFUN (set_metric_type
,
517 "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
= argv
[2]->text
;
524 return generic_set_add(vty
, VTY_GET_CONTEXT(route_map_index
),
528 DEFUN (no_set_metric_type
,
529 no_set_metric_type_cmd
,
530 "no set metric-type [<type-1|type-2>]",
533 "Type of metric for destination routing protocol\n"
534 "OSPF[6] external type 1 metric\n"
535 "OSPF[6] external type 2 metric\n")
537 char *ext
= (argc
== 4) ? argv
[3]->text
: NULL
;
538 return generic_set_delete(vty
, VTY_GET_CONTEXT(route_map_index
),
543 void ospf_route_map_init(void)
547 route_map_add_hook(ospf_route_map_update
);
548 route_map_delete_hook(ospf_route_map_update
);
549 route_map_event_hook(ospf_route_map_event
);
551 route_map_set_metric_hook(generic_set_add
);
552 route_map_no_set_metric_hook(generic_set_delete
);
554 route_map_match_ip_next_hop_hook(generic_match_add
);
555 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
557 route_map_match_interface_hook(generic_match_add
);
558 route_map_no_match_interface_hook(generic_match_delete
);
560 route_map_match_ip_address_hook(generic_match_add
);
561 route_map_no_match_ip_address_hook(generic_match_delete
);
563 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
564 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
566 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
567 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
569 route_map_match_tag_hook(generic_match_add
);
570 route_map_no_match_tag_hook(generic_match_delete
);
572 route_map_set_metric_hook(generic_set_add
);
573 route_map_no_set_metric_hook(generic_set_delete
);
575 route_map_set_tag_hook(generic_set_add
);
576 route_map_no_set_tag_hook(generic_set_delete
);
578 route_map_install_match(&route_match_ip_nexthop_cmd
);
579 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
580 route_map_install_match(&route_match_ip_address_cmd
);
581 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
582 route_map_install_match(&route_match_interface_cmd
);
583 route_map_install_match(&route_match_tag_cmd
);
585 route_map_install_set(&route_set_metric_cmd
);
586 route_map_install_set(&route_set_metric_type_cmd
);
587 route_map_install_set(&route_set_tag_cmd
);
589 install_element(RMAP_NODE
, &set_metric_type_cmd
);
590 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);