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