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