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