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