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