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