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