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. */
72 ROUTEMAP(red
) = route_map_lookup_by_name(
75 /* No update for this distribute type. */
76 if (old
== NULL
&& ROUTEMAP(red
) == NULL
)
79 ospf_distribute_list_update(ospf
, type
,
87 static void ospf_route_map_event(route_map_event_t event
, const char *name
)
91 struct listnode
*n1
= NULL
;
93 for (ALL_LIST_ELEMENTS_RO(om
->ospf
, n1
, ospf
)) {
94 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
95 struct list
*red_list
;
96 struct listnode
*node
;
97 struct ospf_redist
*red
;
99 red_list
= ospf
->redist
[type
];
103 for (ALL_LIST_ELEMENTS_RO(red_list
, node
, red
)) {
104 if (ROUTEMAP_NAME(red
) && ROUTEMAP(red
)
105 && !strcmp(ROUTEMAP_NAME(red
), name
)) {
106 ospf_distribute_list_update(ospf
, type
,
114 /* `match ip netxthop ' */
115 /* Match function return 1 if match is success else return zero. */
116 static route_map_result_t
route_match_ip_nexthop(void *rule
,
117 struct prefix
*prefix
,
118 route_map_object_t type
,
121 struct access_list
*alist
;
122 struct external_info
*ei
= object
;
123 struct prefix_ipv4 p
;
125 if (type
== RMAP_OSPF
) {
127 p
.prefix
= ei
->nexthop
;
128 p
.prefixlen
= IPV4_MAX_BITLEN
;
130 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
134 return (access_list_apply(alist
, &p
) == FILTER_DENY
141 /* Route map `ip next-hop' match statement. `arg' should be
143 static void *route_match_ip_nexthop_compile(const char *arg
)
145 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
148 /* Free route map's compiled `ip address' value. */
149 static void route_match_ip_nexthop_free(void *rule
)
151 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
154 /* Route map commands for metric matching. */
155 struct route_map_rule_cmd route_match_ip_nexthop_cmd
= {
156 "ip next-hop", route_match_ip_nexthop
, route_match_ip_nexthop_compile
,
157 route_match_ip_nexthop_free
};
159 /* `match ip next-hop prefix-list PREFIX_LIST' */
161 static route_map_result_t
162 route_match_ip_next_hop_prefix_list(void *rule
, struct prefix
*prefix
,
163 route_map_object_t type
, void *object
)
165 struct prefix_list
*plist
;
166 struct external_info
*ei
= object
;
167 struct prefix_ipv4 p
;
169 if (type
== RMAP_OSPF
) {
171 p
.prefix
= ei
->nexthop
;
172 p
.prefixlen
= IPV4_MAX_BITLEN
;
174 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
178 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
185 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
187 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
190 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
192 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
195 struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd
= {
196 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list
,
197 route_match_ip_next_hop_prefix_list_compile
,
198 route_match_ip_next_hop_prefix_list_free
};
200 /* `match ip address IP_ACCESS_LIST' */
201 /* Match function should return 1 if match is success else return
203 static route_map_result_t
route_match_ip_address(void *rule
,
204 struct prefix
*prefix
,
205 route_map_object_t type
,
208 struct access_list
*alist
;
209 /* struct prefix_ipv4 match; */
211 if (type
== RMAP_OSPF
) {
212 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
216 return (access_list_apply(alist
, prefix
) == FILTER_DENY
223 /* Route map `ip address' match statement. `arg' should be
225 static void *route_match_ip_address_compile(const char *arg
)
227 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
230 /* Free route map's compiled `ip address' value. */
231 static void route_match_ip_address_free(void *rule
)
233 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
236 /* Route map commands for ip address matching. */
237 struct route_map_rule_cmd route_match_ip_address_cmd
= {
238 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
239 route_match_ip_address_free
};
241 /* `match ip address prefix-list PREFIX_LIST' */
242 static route_map_result_t
243 route_match_ip_address_prefix_list(void *rule
, struct prefix
*prefix
,
244 route_map_object_t type
, void *object
)
246 struct prefix_list
*plist
;
248 if (type
== RMAP_OSPF
) {
249 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
253 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
260 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
262 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
265 static void route_match_ip_address_prefix_list_free(void *rule
)
267 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
270 struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
271 "ip address prefix-list", route_match_ip_address_prefix_list
,
272 route_match_ip_address_prefix_list_compile
,
273 route_match_ip_address_prefix_list_free
};
275 /* `match interface IFNAME' */
276 /* Match function should return 1 if match is success else return
278 static route_map_result_t
route_match_interface(void *rule
,
279 struct prefix
*prefix
,
280 route_map_object_t type
,
283 struct interface
*ifp
;
284 struct external_info
*ei
;
286 if (type
== RMAP_OSPF
) {
288 ifp
= if_lookup_by_name_all_vrf((char *)rule
);
290 if (ifp
== NULL
|| ifp
->ifindex
!= ei
->ifindex
)
298 /* Route map `interface' match statement. `arg' should be
300 static void *route_match_interface_compile(const char *arg
)
302 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
305 /* Free route map's compiled `interface' value. */
306 static void route_match_interface_free(void *rule
)
308 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
311 /* Route map commands for ip address matching. */
312 struct route_map_rule_cmd route_match_interface_cmd
= {
313 "interface", route_match_interface
, route_match_interface_compile
,
314 route_match_interface_free
};
316 /* Match function return 1 if match is success else return zero. */
317 static route_map_result_t
route_match_tag(void *rule
, struct prefix
*prefix
,
318 route_map_object_t type
, void *object
)
321 struct external_info
*ei
;
323 if (type
== RMAP_OSPF
) {
327 return ((ei
->tag
== *tag
) ? RMAP_MATCH
: RMAP_NOMATCH
);
333 /* Route map commands for tag matching. */
334 static struct route_map_rule_cmd route_match_tag_cmd
= {
335 "tag", route_match_tag
, route_map_rule_tag_compile
,
336 route_map_rule_tag_free
,
344 /* `set metric METRIC' */
345 /* Set metric to attribute. */
346 static route_map_result_t
route_set_metric(void *rule
, struct prefix
*prefix
,
347 route_map_object_t type
,
350 struct ospf_metric
*metric
;
351 struct external_info
*ei
;
353 if (type
== RMAP_OSPF
) {
354 /* Fetch routemap's rule information. */
358 /* Set metric out value. */
360 ei
->route_map_set
.metric
= metric
->metric
;
365 /* set metric compilation. */
366 static void *route_set_metric_compile(const char *arg
)
368 struct ospf_metric
*metric
;
370 metric
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(u_int32_t
));
371 metric
->used
= false;
373 /* OSPF doesn't support the +/- in
374 set metric <+/-metric> check
375 Ignore the +/- component */
376 if (!all_digit(arg
)) {
377 if ((arg
[0] == '+' || arg
[0] == '-') && all_digit(arg
+ 1)) {
378 zlog_warn("OSPF does not support 'set metric +/-'");
381 if (strmatch(arg
, "+rtt") || strmatch(arg
, "-rtt"))
383 "OSPF does not support 'set metric +rtt / -rtt'");
388 metric
->metric
= strtoul(arg
, NULL
, 10);
394 /* Free route map's compiled `set metric' value. */
395 static void route_set_metric_free(void *rule
)
397 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
400 /* Set metric rule structure. */
401 struct route_map_rule_cmd route_set_metric_cmd
= {
402 "metric", route_set_metric
, route_set_metric_compile
,
403 route_set_metric_free
,
406 /* `set metric-type TYPE' */
407 /* Set metric-type to attribute. */
408 static route_map_result_t
route_set_metric_type(void *rule
,
409 struct prefix
*prefix
,
410 route_map_object_t type
,
413 u_int32_t
*metric_type
;
414 struct external_info
*ei
;
416 if (type
== RMAP_OSPF
) {
417 /* Fetch routemap's rule information. */
421 /* Set metric out value. */
422 ei
->route_map_set
.metric_type
= *metric_type
;
427 /* set metric-type compilation. */
428 static void *route_set_metric_type_compile(const char *arg
)
430 u_int32_t
*metric_type
;
432 metric_type
= XCALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(u_int32_t
));
433 if (strcmp(arg
, "type-1") == 0)
434 *metric_type
= EXTERNAL_METRIC_TYPE_1
;
435 else if (strcmp(arg
, "type-2") == 0)
436 *metric_type
= EXTERNAL_METRIC_TYPE_2
;
438 if (*metric_type
== EXTERNAL_METRIC_TYPE_1
439 || *metric_type
== EXTERNAL_METRIC_TYPE_2
)
442 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric_type
);
446 /* Free route map's compiled `set metric-type' value. */
447 static void route_set_metric_type_free(void *rule
)
449 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
452 /* Set metric rule structure. */
453 struct route_map_rule_cmd route_set_metric_type_cmd
= {
454 "metric-type", route_set_metric_type
, route_set_metric_type_compile
,
455 route_set_metric_type_free
,
458 static route_map_result_t
route_set_tag(void *rule
, struct prefix
*prefix
,
459 route_map_object_t type
, void *object
)
462 struct external_info
*ei
;
464 if (type
== RMAP_OSPF
) {
475 /* Route map commands for tag set. */
476 static struct route_map_rule_cmd route_set_tag_cmd
= {
477 "tag", route_set_tag
, route_map_rule_tag_compile
,
478 route_map_rule_tag_free
,
481 DEFUN (set_metric_type
,
483 "set metric-type <type-1|type-2>",
485 "Type of metric for destination routing protocol\n"
486 "OSPF[6] external type 1 metric\n"
487 "OSPF[6] external type 2 metric\n")
489 char *ext
= argv
[2]->text
;
490 return generic_set_add(vty
, VTY_GET_CONTEXT(route_map_index
),
494 DEFUN (no_set_metric_type
,
495 no_set_metric_type_cmd
,
496 "no set metric-type [<type-1|type-2>]",
499 "Type of metric for destination routing protocol\n"
500 "OSPF[6] external type 1 metric\n"
501 "OSPF[6] external type 2 metric\n")
503 char *ext
= (argc
== 4) ? argv
[3]->text
: NULL
;
504 return generic_set_delete(vty
, VTY_GET_CONTEXT(route_map_index
),
509 void ospf_route_map_init(void)
513 route_map_add_hook(ospf_route_map_update
);
514 route_map_delete_hook(ospf_route_map_update
);
515 route_map_event_hook(ospf_route_map_event
);
517 route_map_set_metric_hook(generic_set_add
);
518 route_map_no_set_metric_hook(generic_set_delete
);
520 route_map_match_ip_next_hop_hook(generic_match_add
);
521 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
523 route_map_match_interface_hook(generic_match_add
);
524 route_map_no_match_interface_hook(generic_match_delete
);
526 route_map_match_ip_address_hook(generic_match_add
);
527 route_map_no_match_ip_address_hook(generic_match_delete
);
529 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
530 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
532 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
533 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
535 route_map_match_tag_hook(generic_match_add
);
536 route_map_no_match_tag_hook(generic_match_delete
);
538 route_map_set_metric_hook(generic_set_add
);
539 route_map_no_set_metric_hook(generic_set_delete
);
541 route_map_set_tag_hook(generic_set_add
);
542 route_map_no_set_tag_hook(generic_set_delete
);
544 route_map_install_match(&route_match_ip_nexthop_cmd
);
545 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
546 route_map_install_match(&route_match_ip_address_cmd
);
547 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
548 route_map_install_match(&route_match_interface_cmd
);
549 route_map_install_match(&route_match_tag_cmd
);
551 route_map_install_set(&route_set_metric_cmd
);
552 route_map_install_set(&route_set_metric_type_cmd
);
553 route_map_install_set(&route_set_tag_cmd
);
555 install_element(RMAP_NODE
, &set_metric_type_cmd
);
556 install_element(RMAP_NODE
, &no_set_metric_type_cmd
);