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