]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_routemap.c
ospfd: fix clang warning & ci-ospf test
[mirror_frr.git] / ospfd / ospf_routemap.c
CommitLineData
718e3744 1/*
2 * Route map function of ospfd.
3 * Copyright (C) 2000 IP Infusion Inc.
4 *
5 * Written by Toshiaki Takada.
6 *
7 * This file is part of GNU Zebra.
8 *
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
12 * later version.
13 *
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.
18 *
896014f4
DL
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
718e3744 22 */
23
24#include <zebra.h>
25
26#include "memory.h"
27#include "prefix.h"
28#include "table.h"
82f97584 29#include "vty.h"
718e3744 30#include "routemap.h"
31#include "command.h"
32#include "log.h"
33#include "plist.h"
1306c09a 34#include "vrf.h"
718e3744 35
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"
42
43/* Hook function for updating route_map assignment. */
d62a17ae 44static void ospf_route_map_update(const char *name)
45{
46 struct ospf *ospf;
47 int type;
b5a8894d 48 struct listnode *n1 = NULL;
d62a17ae 49
50 /* If OSPF instatnce does not exist, return right now. */
b5a8894d 51 if (listcount(om->ospf) == 0)
d62a17ae 52 return;
53
b5a8894d
CS
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;
60
61 red_list = ospf->redist[type];
62 if (!red_list)
63 continue;
64
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);
70
71 /* Update route-map. */
72 ROUTEMAP(red) = route_map_lookup_by_name(
73 ROUTEMAP_NAME(red));
74
75 /* No update for this distribute type. */
76 if (old == NULL && ROUTEMAP(red) == NULL)
77 continue;
78
79 ospf_distribute_list_update(ospf, type,
80 red->instance);
81 }
d62a17ae 82 }
83 }
84 }
718e3744 85}
86
d62a17ae 87static void ospf_route_map_event(route_map_event_t event, const char *name)
718e3744 88{
d62a17ae 89 struct ospf *ospf;
90 int type;
b5a8894d
CS
91 struct listnode *n1 = NULL;
92
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;
98
99 red_list = ospf->redist[type];
100 if (!red_list)
101 continue;
102
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,
107 red->instance);
108 }
d62a17ae 109 }
110 }
111 }
718e3744 112}
113
718e3744 114/* `match ip netxthop ' */
115/* Match function return 1 if match is success else return zero. */
d62a17ae 116static route_map_result_t route_match_ip_nexthop(void *rule,
117 struct prefix *prefix,
118 route_map_object_t type,
119 void *object)
120{
121 struct access_list *alist;
122 struct external_info *ei = object;
123 struct prefix_ipv4 p;
124
125 if (type == RMAP_OSPF) {
126 p.family = AF_INET;
127 p.prefix = ei->nexthop;
128 p.prefixlen = IPV4_MAX_BITLEN;
129
130 alist = access_list_lookup(AFI_IP, (char *)rule);
131 if (alist == NULL)
132 return RMAP_NOMATCH;
133
134 return (access_list_apply(alist, &p) == FILTER_DENY
135 ? RMAP_NOMATCH
136 : RMAP_MATCH);
137 }
138 return RMAP_NOMATCH;
718e3744 139}
140
141/* Route map `ip next-hop' match statement. `arg' should be
142 access-list name. */
d62a17ae 143static void *route_match_ip_nexthop_compile(const char *arg)
718e3744 144{
d62a17ae 145 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 146}
147
148/* Free route map's compiled `ip address' value. */
d62a17ae 149static void route_match_ip_nexthop_free(void *rule)
718e3744 150{
d62a17ae 151 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 152}
153
154/* Route map commands for metric matching. */
d62a17ae 155struct 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};
718e3744 158
159/* `match ip next-hop prefix-list PREFIX_LIST' */
160
4dadc291 161static route_map_result_t
d62a17ae 162route_match_ip_next_hop_prefix_list(void *rule, struct prefix *prefix,
163 route_map_object_t type, void *object)
718e3744 164{
d62a17ae 165 struct prefix_list *plist;
166 struct external_info *ei = object;
167 struct prefix_ipv4 p;
168
169 if (type == RMAP_OSPF) {
170 p.family = AF_INET;
171 p.prefix = ei->nexthop;
172 p.prefixlen = IPV4_MAX_BITLEN;
173
174 plist = prefix_list_lookup(AFI_IP, (char *)rule);
175 if (plist == NULL)
176 return RMAP_NOMATCH;
177
178 return (prefix_list_apply(plist, &p) == PREFIX_DENY
179 ? RMAP_NOMATCH
180 : RMAP_MATCH);
181 }
182 return RMAP_NOMATCH;
718e3744 183}
184
d62a17ae 185static void *route_match_ip_next_hop_prefix_list_compile(const char *arg)
718e3744 186{
d62a17ae 187 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 188}
189
d62a17ae 190static void route_match_ip_next_hop_prefix_list_free(void *rule)
718e3744 191{
d62a17ae 192 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 193}
194
d62a17ae 195struct 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};
718e3744 199
200/* `match ip address IP_ACCESS_LIST' */
201/* Match function should return 1 if match is success else return
202 zero. */
d62a17ae 203static route_map_result_t route_match_ip_address(void *rule,
204 struct prefix *prefix,
205 route_map_object_t type,
206 void *object)
207{
208 struct access_list *alist;
209 /* struct prefix_ipv4 match; */
210
211 if (type == RMAP_OSPF) {
212 alist = access_list_lookup(AFI_IP, (char *)rule);
213 if (alist == NULL)
214 return RMAP_NOMATCH;
215
216 return (access_list_apply(alist, prefix) == FILTER_DENY
217 ? RMAP_NOMATCH
218 : RMAP_MATCH);
219 }
220 return RMAP_NOMATCH;
718e3744 221}
222
223/* Route map `ip address' match statement. `arg' should be
224 access-list name. */
d62a17ae 225static void *route_match_ip_address_compile(const char *arg)
718e3744 226{
d62a17ae 227 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 228}
229
230/* Free route map's compiled `ip address' value. */
d62a17ae 231static void route_match_ip_address_free(void *rule)
718e3744 232{
d62a17ae 233 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 234}
235
236/* Route map commands for ip address matching. */
d62a17ae 237struct 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};
718e3744 240
241/* `match ip address prefix-list PREFIX_LIST' */
4dadc291 242static route_map_result_t
d62a17ae 243route_match_ip_address_prefix_list(void *rule, struct prefix *prefix,
244 route_map_object_t type, void *object)
718e3744 245{
d62a17ae 246 struct prefix_list *plist;
247
248 if (type == RMAP_OSPF) {
249 plist = prefix_list_lookup(AFI_IP, (char *)rule);
250 if (plist == NULL)
251 return RMAP_NOMATCH;
252
253 return (prefix_list_apply(plist, prefix) == PREFIX_DENY
254 ? RMAP_NOMATCH
255 : RMAP_MATCH);
256 }
257 return RMAP_NOMATCH;
718e3744 258}
259
d62a17ae 260static void *route_match_ip_address_prefix_list_compile(const char *arg)
718e3744 261{
d62a17ae 262 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 263}
264
d62a17ae 265static void route_match_ip_address_prefix_list_free(void *rule)
718e3744 266{
d62a17ae 267 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 268}
269
d62a17ae 270struct 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};
718e3744 274
275/* `match interface IFNAME' */
276/* Match function should return 1 if match is success else return
277 zero. */
d62a17ae 278static route_map_result_t route_match_interface(void *rule,
279 struct prefix *prefix,
280 route_map_object_t type,
281 void *object)
718e3744 282{
d62a17ae 283 struct interface *ifp;
284 struct external_info *ei;
718e3744 285
d62a17ae 286 if (type == RMAP_OSPF) {
287 ei = object;
b5a8894d 288 ifp = if_lookup_by_name_all_vrf((char *)rule);
718e3744 289
d62a17ae 290 if (ifp == NULL || ifp->ifindex != ei->ifindex)
291 return RMAP_NOMATCH;
718e3744 292
d62a17ae 293 return RMAP_MATCH;
294 }
295 return RMAP_NOMATCH;
718e3744 296}
297
298/* Route map `interface' match statement. `arg' should be
299 interface name. */
d62a17ae 300static void *route_match_interface_compile(const char *arg)
718e3744 301{
d62a17ae 302 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 303}
304
305/* Free route map's compiled `interface' value. */
d62a17ae 306static void route_match_interface_free(void *rule)
718e3744 307{
d62a17ae 308 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 309}
310
311/* Route map commands for ip address matching. */
d62a17ae 312struct route_map_rule_cmd route_match_interface_cmd = {
313 "interface", route_match_interface, route_match_interface_compile,
314 route_match_interface_free};
718e3744 315
0d9551dc 316/* Match function return 1 if match is success else return zero. */
d62a17ae 317static route_map_result_t route_match_tag(void *rule, struct prefix *prefix,
318 route_map_object_t type, void *object)
0d9551dc 319{
d62a17ae 320 route_tag_t *tag;
321 struct external_info *ei;
0d9551dc 322
d62a17ae 323 if (type == RMAP_OSPF) {
324 tag = rule;
325 ei = object;
0d9551dc 326
d62a17ae 327 return ((ei->tag == *tag) ? RMAP_MATCH : RMAP_NOMATCH);
328 }
0d9551dc 329
d62a17ae 330 return RMAP_NOMATCH;
0d9551dc
DS
331}
332
0d9551dc 333/* Route map commands for tag matching. */
d62a17ae 334static struct route_map_rule_cmd route_match_tag_cmd = {
9d303b37 335 "tag", route_match_tag, route_map_rule_tag_compile,
d62a17ae 336 route_map_rule_tag_free,
0d9551dc
DS
337};
338
6a74c5f9
DS
339struct ospf_metric {
340 bool used;
341 u_int32_t metric;
342};
0d9551dc 343
718e3744 344/* `set metric METRIC' */
345/* Set metric to attribute. */
d62a17ae 346static route_map_result_t route_set_metric(void *rule, struct prefix *prefix,
347 route_map_object_t type,
348 void *object)
718e3744 349{
6a74c5f9 350 struct ospf_metric *metric;
d62a17ae 351 struct external_info *ei;
352
353 if (type == RMAP_OSPF) {
354 /* Fetch routemap's rule information. */
355 metric = rule;
356 ei = object;
357
358 /* Set metric out value. */
6a74c5f9
DS
359 if (metric->used)
360 ei->route_map_set.metric = metric->metric;
d62a17ae 361 }
362 return RMAP_OKAY;
718e3744 363}
364
365/* set metric compilation. */
d62a17ae 366static void *route_set_metric_compile(const char *arg)
367{
6a74c5f9
DS
368 struct ospf_metric *metric;
369
370 metric = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(u_int32_t));
371 metric->used = false;
d62a17ae 372
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 +/-'");
379 arg++;
380 } else {
381 if (strmatch(arg, "+rtt") || strmatch(arg, "-rtt"))
382 zlog_warn(
383 "OSPF does not support 'set metric +rtt / -rtt'");
6a74c5f9
DS
384
385 return metric;
d62a17ae 386 }
ed2eb093 387 }
6a74c5f9
DS
388 metric->metric = strtoul(arg, NULL, 10);
389 metric->used = true;
d62a17ae 390
391 return metric;
718e3744 392}
393
394/* Free route map's compiled `set metric' value. */
d62a17ae 395static void route_set_metric_free(void *rule)
718e3744 396{
d62a17ae 397 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 398}
399
400/* Set metric rule structure. */
d62a17ae 401struct route_map_rule_cmd route_set_metric_cmd = {
9d303b37 402 "metric", route_set_metric, route_set_metric_compile,
d62a17ae 403 route_set_metric_free,
718e3744 404};
405
406/* `set metric-type TYPE' */
407/* Set metric-type to attribute. */
d62a17ae 408static route_map_result_t route_set_metric_type(void *rule,
409 struct prefix *prefix,
410 route_map_object_t type,
411 void *object)
718e3744 412{
d62a17ae 413 u_int32_t *metric_type;
414 struct external_info *ei;
415
416 if (type == RMAP_OSPF) {
417 /* Fetch routemap's rule information. */
418 metric_type = rule;
419 ei = object;
420
421 /* Set metric out value. */
422 ei->route_map_set.metric_type = *metric_type;
423 }
424 return RMAP_OKAY;
718e3744 425}
426
427/* set metric-type compilation. */
d62a17ae 428static void *route_set_metric_type_compile(const char *arg)
718e3744 429{
d62a17ae 430 u_int32_t *metric_type;
718e3744 431
d62a17ae 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;
718e3744 437
d62a17ae 438 if (*metric_type == EXTERNAL_METRIC_TYPE_1
439 || *metric_type == EXTERNAL_METRIC_TYPE_2)
440 return metric_type;
718e3744 441
d62a17ae 442 XFREE(MTYPE_ROUTE_MAP_COMPILED, metric_type);
443 return NULL;
718e3744 444}
445
446/* Free route map's compiled `set metric-type' value. */
d62a17ae 447static void route_set_metric_type_free(void *rule)
718e3744 448{
d62a17ae 449 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 450}
451
452/* Set metric rule structure. */
d62a17ae 453struct route_map_rule_cmd route_set_metric_type_cmd = {
9d303b37 454 "metric-type", route_set_metric_type, route_set_metric_type_compile,
d62a17ae 455 route_set_metric_type_free,
718e3744 456};
457
d62a17ae 458static route_map_result_t route_set_tag(void *rule, struct prefix *prefix,
459 route_map_object_t type, void *object)
0d9551dc 460{
d62a17ae 461 route_tag_t *tag;
462 struct external_info *ei;
0d9551dc 463
d62a17ae 464 if (type == RMAP_OSPF) {
465 tag = rule;
466 ei = object;
0d9551dc 467
d62a17ae 468 /* Set tag value */
469 ei->tag = *tag;
470 }
0d9551dc 471
d62a17ae 472 return RMAP_OKAY;
0d9551dc
DS
473}
474
0d9551dc 475/* Route map commands for tag set. */
d62a17ae 476static struct route_map_rule_cmd route_set_tag_cmd = {
9d303b37 477 "tag", route_set_tag, route_map_rule_tag_compile,
d62a17ae 478 route_map_rule_tag_free,
0d9551dc
DS
479};
480
718e3744 481DEFUN (set_metric_type,
482 set_metric_type_cmd,
6147e2c6 483 "set metric-type <type-1|type-2>",
718e3744 484 SET_STR
485 "Type of metric for destination routing protocol\n"
73ffb25b 486 "OSPF[6] external type 1 metric\n"
487 "OSPF[6] external type 2 metric\n")
718e3744 488{
d62a17ae 489 char *ext = argv[2]->text;
490 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
491 "metric-type", ext);
718e3744 492}
493
494DEFUN (no_set_metric_type,
495 no_set_metric_type_cmd,
692b4c65 496 "no set metric-type [<type-1|type-2>]",
718e3744 497 NO_STR
498 SET_STR
ff788d08 499 "Type of metric for destination routing protocol\n"
692b4c65 500 "OSPF[6] external type 1 metric\n"
ff788d08 501 "OSPF[6] external type 2 metric\n")
718e3744 502{
d62a17ae 503 char *ext = (argc == 4) ? argv[3]->text : NULL;
504 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
505 "metric-type", ext);
0d9551dc
DS
506}
507
718e3744 508/* Route-map init */
d62a17ae 509void ospf_route_map_init(void)
718e3744 510{
d62a17ae 511 route_map_init();
512
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);
718e3744 516
d62a17ae 517 route_map_set_metric_hook(generic_set_add);
518 route_map_no_set_metric_hook(generic_set_delete);
82f97584 519
d62a17ae 520 route_map_match_ip_next_hop_hook(generic_match_add);
521 route_map_no_match_ip_next_hop_hook(generic_match_delete);
58b0878a 522
d62a17ae 523 route_map_match_interface_hook(generic_match_add);
524 route_map_no_match_interface_hook(generic_match_delete);
58b0878a 525
d62a17ae 526 route_map_match_ip_address_hook(generic_match_add);
527 route_map_no_match_ip_address_hook(generic_match_delete);
82f97584 528
d62a17ae 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);
82f97584 531
d62a17ae 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);
82f97584 534
d62a17ae 535 route_map_match_tag_hook(generic_match_add);
536 route_map_no_match_tag_hook(generic_match_delete);
82f97584 537
d62a17ae 538 route_map_set_metric_hook(generic_set_add);
539 route_map_no_set_metric_hook(generic_set_delete);
82f97584 540
d62a17ae 541 route_map_set_tag_hook(generic_set_add);
542 route_map_no_set_tag_hook(generic_set_delete);
82f97584 543
d62a17ae 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);
718e3744 550
d62a17ae 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);
718e3744 554
d62a17ae 555 install_element(RMAP_NODE, &set_metric_type_cmd);
556 install_element(RMAP_NODE, &no_set_metric_type_cmd);
718e3744 557}