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