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