]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_routemap.c
Merge pull request #2652 from LabNConsulting/working/master/confdate_cleanup
[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,
121 struct prefix *prefix,
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
d62a17ae 166route_match_ip_next_hop_prefix_list(void *rule, struct prefix *prefix,
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,
208 struct prefix *prefix,
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
d62a17ae 247route_match_ip_address_prefix_list(void *rule, struct prefix *prefix,
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,
283 struct prefix *prefix,
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. */
d62a17ae 321static route_map_result_t route_match_tag(void *rule, struct prefix *prefix,
322 route_map_object_t type, void *object)
0d9551dc 323{
d62a17ae 324 route_tag_t *tag;
325 struct external_info *ei;
0d9551dc 326
d62a17ae 327 if (type == RMAP_OSPF) {
328 tag = rule;
329 ei = object;
0d9551dc 330
d62a17ae 331 return ((ei->tag == *tag) ? RMAP_MATCH : RMAP_NOMATCH);
332 }
0d9551dc 333
d62a17ae 334 return RMAP_NOMATCH;
0d9551dc
DS
335}
336
0d9551dc 337/* Route map commands for tag matching. */
d62a17ae 338static struct route_map_rule_cmd route_match_tag_cmd = {
9d303b37 339 "tag", route_match_tag, route_map_rule_tag_compile,
d62a17ae 340 route_map_rule_tag_free,
0d9551dc
DS
341};
342
6a74c5f9 343struct ospf_metric {
7a5e6e46 344 enum { metric_increment, metric_decrement, metric_absolute } type;
6a74c5f9 345 bool used;
d7c0a89a 346 uint32_t metric;
6a74c5f9 347};
0d9551dc 348
718e3744 349/* `set metric METRIC' */
350/* Set metric to attribute. */
d62a17ae 351static route_map_result_t route_set_metric(void *rule, struct prefix *prefix,
352 route_map_object_t type,
353 void *object)
718e3744 354{
6a74c5f9 355 struct ospf_metric *metric;
d62a17ae 356 struct external_info *ei;
357
358 if (type == RMAP_OSPF) {
359 /* Fetch routemap's rule information. */
360 metric = rule;
361 ei = object;
362
363 /* Set metric out value. */
7a5e6e46 364 if (!metric->used)
365 return RMAP_OKAY;
366 if (metric->type == metric_increment)
367 ei->route_map_set.metric += metric->metric;
368 if (metric->type == metric_decrement)
369 ei->route_map_set.metric -= metric->metric;
370 if (metric->type == metric_absolute)
6a74c5f9 371 ei->route_map_set.metric = metric->metric;
7a5e6e46 372
373 if ((signed int)ei->route_map_set.metric < 1)
374 ei->route_map_set.metric = -1;
375 if (ei->route_map_set.metric > OSPF_LS_INFINITY)
376 ei->route_map_set.metric = OSPF_LS_INFINITY;
d62a17ae 377 }
378 return RMAP_OKAY;
718e3744 379}
380
381/* set metric compilation. */
d62a17ae 382static void *route_set_metric_compile(const char *arg)
383{
6a74c5f9
DS
384 struct ospf_metric *metric;
385
d7c0a89a 386 metric = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
6a74c5f9 387 metric->used = false;
d62a17ae 388
7a5e6e46 389 if (all_digit(arg))
390 metric->type = metric_absolute;
391
392 if (strmatch(arg, "+rtt") || strmatch(arg, "-rtt")) {
393 zlog_warn("OSPF does not support 'set metric +rtt / -rtt'");
394 return metric;
395 }
396
397 if ((arg[0] == '+') && all_digit(arg + 1)) {
398 metric->type = metric_increment;
399 arg++;
ed2eb093 400 }
7a5e6e46 401
402 if ((arg[0] == '-') && all_digit(arg + 1)) {
403 metric->type = metric_decrement;
404 arg++;
405 }
406
6a74c5f9 407 metric->metric = strtoul(arg, NULL, 10);
7a5e6e46 408
409 if (metric->metric)
410 metric->used = true;
d62a17ae 411
412 return metric;
718e3744 413}
414
415/* Free route map's compiled `set metric' value. */
d62a17ae 416static void route_set_metric_free(void *rule)
718e3744 417{
d62a17ae 418 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 419}
420
421/* Set metric rule structure. */
d62a17ae 422struct route_map_rule_cmd route_set_metric_cmd = {
9d303b37 423 "metric", route_set_metric, route_set_metric_compile,
d62a17ae 424 route_set_metric_free,
718e3744 425};
426
427/* `set metric-type TYPE' */
428/* Set metric-type to attribute. */
d62a17ae 429static route_map_result_t route_set_metric_type(void *rule,
430 struct prefix *prefix,
431 route_map_object_t type,
432 void *object)
718e3744 433{
d7c0a89a 434 uint32_t *metric_type;
d62a17ae 435 struct external_info *ei;
436
437 if (type == RMAP_OSPF) {
438 /* Fetch routemap's rule information. */
439 metric_type = rule;
440 ei = object;
441
442 /* Set metric out value. */
443 ei->route_map_set.metric_type = *metric_type;
444 }
445 return RMAP_OKAY;
718e3744 446}
447
448/* set metric-type compilation. */
d62a17ae 449static void *route_set_metric_type_compile(const char *arg)
718e3744 450{
d7c0a89a 451 uint32_t *metric_type;
718e3744 452
d7c0a89a 453 metric_type = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
d62a17ae 454 if (strcmp(arg, "type-1") == 0)
455 *metric_type = EXTERNAL_METRIC_TYPE_1;
456 else if (strcmp(arg, "type-2") == 0)
457 *metric_type = EXTERNAL_METRIC_TYPE_2;
718e3744 458
d62a17ae 459 if (*metric_type == EXTERNAL_METRIC_TYPE_1
460 || *metric_type == EXTERNAL_METRIC_TYPE_2)
461 return metric_type;
718e3744 462
d62a17ae 463 XFREE(MTYPE_ROUTE_MAP_COMPILED, metric_type);
464 return NULL;
718e3744 465}
466
467/* Free route map's compiled `set metric-type' value. */
d62a17ae 468static void route_set_metric_type_free(void *rule)
718e3744 469{
d62a17ae 470 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 471}
472
473/* Set metric rule structure. */
d62a17ae 474struct route_map_rule_cmd route_set_metric_type_cmd = {
9d303b37 475 "metric-type", route_set_metric_type, route_set_metric_type_compile,
d62a17ae 476 route_set_metric_type_free,
718e3744 477};
478
d62a17ae 479static route_map_result_t route_set_tag(void *rule, struct prefix *prefix,
480 route_map_object_t type, void *object)
0d9551dc 481{
d62a17ae 482 route_tag_t *tag;
483 struct external_info *ei;
0d9551dc 484
d62a17ae 485 if (type == RMAP_OSPF) {
486 tag = rule;
487 ei = object;
0d9551dc 488
d62a17ae 489 /* Set tag value */
490 ei->tag = *tag;
491 }
0d9551dc 492
d62a17ae 493 return RMAP_OKAY;
0d9551dc
DS
494}
495
0d9551dc 496/* Route map commands for tag set. */
d62a17ae 497static struct route_map_rule_cmd route_set_tag_cmd = {
9d303b37 498 "tag", route_set_tag, route_map_rule_tag_compile,
d62a17ae 499 route_map_rule_tag_free,
0d9551dc
DS
500};
501
718e3744 502DEFUN (set_metric_type,
503 set_metric_type_cmd,
6147e2c6 504 "set metric-type <type-1|type-2>",
718e3744 505 SET_STR
506 "Type of metric for destination routing protocol\n"
73ffb25b 507 "OSPF[6] external type 1 metric\n"
508 "OSPF[6] external type 2 metric\n")
718e3744 509{
d62a17ae 510 char *ext = argv[2]->text;
511 return generic_set_add(vty, VTY_GET_CONTEXT(route_map_index),
512 "metric-type", ext);
718e3744 513}
514
515DEFUN (no_set_metric_type,
516 no_set_metric_type_cmd,
692b4c65 517 "no set metric-type [<type-1|type-2>]",
718e3744 518 NO_STR
519 SET_STR
ff788d08 520 "Type of metric for destination routing protocol\n"
692b4c65 521 "OSPF[6] external type 1 metric\n"
ff788d08 522 "OSPF[6] external type 2 metric\n")
718e3744 523{
d62a17ae 524 char *ext = (argc == 4) ? argv[3]->text : NULL;
525 return generic_set_delete(vty, VTY_GET_CONTEXT(route_map_index),
526 "metric-type", ext);
0d9551dc
DS
527}
528
718e3744 529/* Route-map init */
d62a17ae 530void ospf_route_map_init(void)
718e3744 531{
d62a17ae 532 route_map_init();
533
534 route_map_add_hook(ospf_route_map_update);
535 route_map_delete_hook(ospf_route_map_update);
536 route_map_event_hook(ospf_route_map_event);
718e3744 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_match_ip_next_hop_hook(generic_match_add);
542 route_map_no_match_ip_next_hop_hook(generic_match_delete);
58b0878a 543
d62a17ae 544 route_map_match_interface_hook(generic_match_add);
545 route_map_no_match_interface_hook(generic_match_delete);
58b0878a 546
d62a17ae 547 route_map_match_ip_address_hook(generic_match_add);
548 route_map_no_match_ip_address_hook(generic_match_delete);
82f97584 549
d62a17ae 550 route_map_match_ip_address_prefix_list_hook(generic_match_add);
551 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete);
82f97584 552
d62a17ae 553 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
554 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
82f97584 555
d62a17ae 556 route_map_match_tag_hook(generic_match_add);
557 route_map_no_match_tag_hook(generic_match_delete);
82f97584 558
d62a17ae 559 route_map_set_metric_hook(generic_set_add);
560 route_map_no_set_metric_hook(generic_set_delete);
82f97584 561
d62a17ae 562 route_map_set_tag_hook(generic_set_add);
563 route_map_no_set_tag_hook(generic_set_delete);
82f97584 564
d62a17ae 565 route_map_install_match(&route_match_ip_nexthop_cmd);
566 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
567 route_map_install_match(&route_match_ip_address_cmd);
568 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
569 route_map_install_match(&route_match_interface_cmd);
570 route_map_install_match(&route_match_tag_cmd);
718e3744 571
d62a17ae 572 route_map_install_set(&route_set_metric_cmd);
573 route_map_install_set(&route_set_metric_type_cmd);
574 route_map_install_set(&route_set_tag_cmd);
718e3744 575
d62a17ae 576 install_element(RMAP_NODE, &set_metric_type_cmd);
577 install_element(RMAP_NODE, &no_set_metric_type_cmd);
718e3744 578}