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