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