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