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