]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_routemap.c
Merge pull request #5197 from SumitAgarwal123/BFD_ADMIN_DOWN
[mirror_frr.git] / ripd / rip_routemap.c
1 /* RIPv2 routemap.
2 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
3 * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "memory.h"
25 #include "prefix.h"
26 #include "vty.h"
27 #include "routemap.h"
28 #include "command.h"
29 #include "filter.h"
30 #include "log.h"
31 #include "sockunion.h" /* for inet_aton () */
32 #include "plist.h"
33 #include "vrf.h"
34
35 #include "ripd/ripd.h"
36
37 struct rip_metric_modifier {
38 enum { metric_increment, metric_decrement, metric_absolute } type;
39 bool used;
40 uint8_t metric;
41 };
42
43 /* `match metric METRIC' */
44 /* Match function return 1 if match is success else return zero. */
45 static enum route_map_cmd_result_t
46 route_match_metric(void *rule, const struct prefix *prefix,
47 route_map_object_t type, void *object)
48 {
49 uint32_t *metric;
50 uint32_t check;
51 struct rip_info *rinfo;
52
53 if (type == RMAP_RIP) {
54 metric = rule;
55 rinfo = object;
56
57 /* If external metric is available, the route-map should
58 work on this one (for redistribute purpose) */
59 check = (rinfo->external_metric) ? rinfo->external_metric
60 : rinfo->metric;
61 if (check == *metric)
62 return RMAP_MATCH;
63 else
64 return RMAP_NOMATCH;
65 }
66 return RMAP_NOMATCH;
67 }
68
69 /* Route map `match metric' match statement. `arg' is METRIC value */
70 static void *route_match_metric_compile(const char *arg)
71 {
72 uint32_t *metric;
73
74 metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
75 *metric = atoi(arg);
76
77 if (*metric > 0)
78 return metric;
79
80 XFREE(MTYPE_ROUTE_MAP_COMPILED, metric);
81 return NULL;
82 }
83
84 /* Free route map's compiled `match metric' value. */
85 static void route_match_metric_free(void *rule)
86 {
87 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
88 }
89
90 /* Route map commands for metric matching. */
91 struct route_map_rule_cmd route_match_metric_cmd = {
92 "metric", route_match_metric, route_match_metric_compile,
93 route_match_metric_free};
94
95 /* `match interface IFNAME' */
96 /* Match function return 1 if match is success else return zero. */
97 static enum route_map_cmd_result_t
98 route_match_interface(void *rule, const struct prefix *prefix,
99 route_map_object_t type, void *object)
100 {
101 struct rip_info *rinfo;
102 struct interface *ifp;
103 char *ifname;
104
105 if (type == RMAP_RIP) {
106 ifname = rule;
107 ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
108
109 if (!ifp)
110 return RMAP_NOMATCH;
111
112 rinfo = object;
113
114 if (rinfo->ifindex_out == ifp->ifindex
115 || rinfo->nh.ifindex == ifp->ifindex)
116 return RMAP_MATCH;
117 else
118 return RMAP_NOMATCH;
119 }
120 return RMAP_NOMATCH;
121 }
122
123 /* Route map `match interface' match statement. `arg' is IFNAME value */
124 /* XXX I don`t know if I need to check does interface exist? */
125 static void *route_match_interface_compile(const char *arg)
126 {
127 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
128 }
129
130 /* Free route map's compiled `match interface' value. */
131 static void route_match_interface_free(void *rule)
132 {
133 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
134 }
135
136 /* Route map commands for interface matching. */
137 struct route_map_rule_cmd route_match_interface_cmd = {
138 "interface", route_match_interface, route_match_interface_compile,
139 route_match_interface_free};
140
141 /* `match ip next-hop IP_ACCESS_LIST' */
142
143 /* Match function return 1 if match is success else return zero. */
144 static enum route_map_cmd_result_t
145 route_match_ip_next_hop(void *rule, const struct prefix *prefix,
146 route_map_object_t type, void *object)
147 {
148 struct access_list *alist;
149 struct rip_info *rinfo;
150 struct prefix_ipv4 p;
151
152 if (type == RMAP_RIP) {
153 rinfo = object;
154 p.family = AF_INET;
155 p.prefix = (rinfo->nh.gate.ipv4.s_addr) ? rinfo->nh.gate.ipv4
156 : rinfo->from;
157 p.prefixlen = IPV4_MAX_BITLEN;
158
159 alist = access_list_lookup(AFI_IP, (char *)rule);
160 if (alist == NULL)
161 return RMAP_NOMATCH;
162
163 return (access_list_apply(alist, &p) == FILTER_DENY
164 ? RMAP_NOMATCH
165 : RMAP_MATCH);
166 }
167 return RMAP_NOMATCH;
168 }
169
170 /* Route map `ip next-hop' match statement. `arg' should be
171 access-list name. */
172 static void *route_match_ip_next_hop_compile(const char *arg)
173 {
174 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
175 }
176
177 /* Free route map's compiled `. */
178 static void route_match_ip_next_hop_free(void *rule)
179 {
180 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
181 }
182
183 /* Route map commands for ip next-hop matching. */
184 static struct route_map_rule_cmd route_match_ip_next_hop_cmd = {
185 "ip next-hop", route_match_ip_next_hop, route_match_ip_next_hop_compile,
186 route_match_ip_next_hop_free};
187
188 /* `match ip next-hop prefix-list PREFIX_LIST' */
189
190 static enum route_map_cmd_result_t
191 route_match_ip_next_hop_prefix_list(void *rule, const struct prefix *prefix,
192 route_map_object_t type, void *object)
193 {
194 struct prefix_list *plist;
195 struct rip_info *rinfo;
196 struct prefix_ipv4 p;
197
198 if (type == RMAP_RIP) {
199 rinfo = object;
200 p.family = AF_INET;
201 p.prefix = (rinfo->nh.gate.ipv4.s_addr) ? rinfo->nh.gate.ipv4
202 : rinfo->from;
203 p.prefixlen = IPV4_MAX_BITLEN;
204
205 plist = prefix_list_lookup(AFI_IP, (char *)rule);
206 if (plist == NULL)
207 return RMAP_NOMATCH;
208
209 return (prefix_list_apply(plist, &p) == PREFIX_DENY
210 ? RMAP_NOMATCH
211 : RMAP_MATCH);
212 }
213 return RMAP_NOMATCH;
214 }
215
216 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg)
217 {
218 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
219 }
220
221 static void route_match_ip_next_hop_prefix_list_free(void *rule)
222 {
223 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
224 }
225
226 static struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd = {
227 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list,
228 route_match_ip_next_hop_prefix_list_compile,
229 route_match_ip_next_hop_prefix_list_free};
230
231 /* `match ip next-hop type <blackhole>' */
232
233 static enum route_map_cmd_result_t
234 route_match_ip_next_hop_type(void *rule, const struct prefix *prefix,
235 route_map_object_t type, void *object)
236 {
237 struct rip_info *rinfo;
238
239 if (type == RMAP_RIP && prefix->family == AF_INET) {
240 rinfo = (struct rip_info *)object;
241 if (!rinfo)
242 return RMAP_NOMATCH;
243
244 if (rinfo->nh.type == NEXTHOP_TYPE_BLACKHOLE)
245 return RMAP_MATCH;
246 }
247 return RMAP_NOMATCH;
248 }
249
250 static void *route_match_ip_next_hop_type_compile(const char *arg)
251 {
252 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
253 }
254
255 static void route_match_ip_next_hop_type_free(void *rule)
256 {
257 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
258 }
259
260 static struct route_map_rule_cmd route_match_ip_next_hop_type_cmd = {
261 "ip next-hop type", route_match_ip_next_hop_type,
262 route_match_ip_next_hop_type_compile,
263 route_match_ip_next_hop_type_free};
264
265 /* `match ip address IP_ACCESS_LIST' */
266
267 /* Match function should return 1 if match is success else return
268 zero. */
269 static enum route_map_cmd_result_t
270 route_match_ip_address(void *rule, const struct prefix *prefix,
271 route_map_object_t type, void *object)
272 {
273 struct access_list *alist;
274
275 if (type == RMAP_RIP) {
276 alist = access_list_lookup(AFI_IP, (char *)rule);
277 if (alist == NULL)
278 return RMAP_NOMATCH;
279
280 return (access_list_apply(alist, prefix) == FILTER_DENY
281 ? RMAP_NOMATCH
282 : RMAP_MATCH);
283 }
284 return RMAP_NOMATCH;
285 }
286
287 /* Route map `ip address' match statement. `arg' should be
288 access-list name. */
289 static void *route_match_ip_address_compile(const char *arg)
290 {
291 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
292 }
293
294 /* Free route map's compiled `ip address' value. */
295 static void route_match_ip_address_free(void *rule)
296 {
297 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
298 }
299
300 /* Route map commands for ip address matching. */
301 static struct route_map_rule_cmd route_match_ip_address_cmd = {
302 "ip address", route_match_ip_address, route_match_ip_address_compile,
303 route_match_ip_address_free};
304
305 /* `match ip address prefix-list PREFIX_LIST' */
306
307 static enum route_map_cmd_result_t
308 route_match_ip_address_prefix_list(void *rule, const struct prefix *prefix,
309 route_map_object_t type, void *object)
310 {
311 struct prefix_list *plist;
312
313 if (type == RMAP_RIP) {
314 plist = prefix_list_lookup(AFI_IP, (char *)rule);
315 if (plist == NULL)
316 return RMAP_NOMATCH;
317
318 return (prefix_list_apply(plist, prefix) == PREFIX_DENY
319 ? RMAP_NOMATCH
320 : RMAP_MATCH);
321 }
322 return RMAP_NOMATCH;
323 }
324
325 static void *route_match_ip_address_prefix_list_compile(const char *arg)
326 {
327 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
328 }
329
330 static void route_match_ip_address_prefix_list_free(void *rule)
331 {
332 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
333 }
334
335 static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd = {
336 "ip address prefix-list", route_match_ip_address_prefix_list,
337 route_match_ip_address_prefix_list_compile,
338 route_match_ip_address_prefix_list_free};
339
340 /* `match tag TAG' */
341 /* Match function return 1 if match is success else return zero. */
342 static enum route_map_cmd_result_t
343 route_match_tag(void *rule, const struct prefix *p, route_map_object_t type,
344 void *object)
345 {
346 route_tag_t *tag;
347 struct rip_info *rinfo;
348 route_tag_t rinfo_tag;
349
350 if (type == RMAP_RIP) {
351 tag = rule;
352 rinfo = object;
353
354 /* The information stored by rinfo is host ordered. */
355 rinfo_tag = rinfo->tag;
356 if (rinfo_tag == *tag)
357 return RMAP_MATCH;
358 else
359 return RMAP_NOMATCH;
360 }
361 return RMAP_NOMATCH;
362 }
363
364 /* Route map commands for tag matching. */
365 static struct route_map_rule_cmd route_match_tag_cmd = {
366 "tag", route_match_tag, route_map_rule_tag_compile,
367 route_map_rule_tag_free,
368 };
369
370 /* `set metric METRIC' */
371
372 /* Set metric to attribute. */
373 static enum route_map_cmd_result_t
374 route_set_metric(void *rule, const struct prefix *prefix,
375 route_map_object_t type, void *object)
376 {
377 if (type == RMAP_RIP) {
378 struct rip_metric_modifier *mod;
379 struct rip_info *rinfo;
380
381 mod = rule;
382 rinfo = object;
383
384 if (!mod->used)
385 return RMAP_OKAY;
386
387 if (mod->type == metric_increment)
388 rinfo->metric_out += mod->metric;
389 else if (mod->type == metric_decrement)
390 rinfo->metric_out -= mod->metric;
391 else if (mod->type == metric_absolute)
392 rinfo->metric_out = mod->metric;
393
394 if ((signed int)rinfo->metric_out < 1)
395 rinfo->metric_out = 1;
396 if (rinfo->metric_out > RIP_METRIC_INFINITY)
397 rinfo->metric_out = RIP_METRIC_INFINITY;
398
399 rinfo->metric_set = 1;
400 }
401 return RMAP_OKAY;
402 }
403
404 /* set metric compilation. */
405 static void *route_set_metric_compile(const char *arg)
406 {
407 int len;
408 const char *pnt;
409 long metric;
410 char *endptr = NULL;
411 struct rip_metric_modifier *mod;
412
413 mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
414 sizeof(struct rip_metric_modifier));
415 mod->used = false;
416
417 len = strlen(arg);
418 pnt = arg;
419
420 if (len == 0)
421 return mod;
422
423 /* Examine first character. */
424 if (arg[0] == '+') {
425 mod->type = metric_increment;
426 pnt++;
427 } else if (arg[0] == '-') {
428 mod->type = metric_decrement;
429 pnt++;
430 } else
431 mod->type = metric_absolute;
432
433 /* Check beginning with digit string. */
434 if (*pnt < '0' || *pnt > '9')
435 return mod;
436
437 /* Convert string to integer. */
438 metric = strtol(pnt, &endptr, 10);
439
440 if (*endptr != '\0' || metric < 0) {
441 return mod;
442 }
443 if (metric > RIP_METRIC_INFINITY) {
444 zlog_info(
445 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
446 __PRETTY_FUNCTION__, metric);
447 mod->metric = RIP_METRIC_INFINITY;
448 } else
449 mod->metric = metric;
450
451 mod->used = true;
452
453 return mod;
454 }
455
456 /* Free route map's compiled `set metric' value. */
457 static void route_set_metric_free(void *rule)
458 {
459 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
460 }
461
462 /* Set metric rule structure. */
463 static struct route_map_rule_cmd route_set_metric_cmd = {
464 "metric", route_set_metric, route_set_metric_compile,
465 route_set_metric_free,
466 };
467
468 /* `set ip next-hop IP_ADDRESS' */
469
470 /* Set nexthop to object. ojbect must be pointer to struct attr. */
471 static enum route_map_cmd_result_t route_set_ip_nexthop(void *rule,
472 const struct prefix *prefix,
473 route_map_object_t type,
474 void *object)
475 {
476 struct in_addr *address;
477 struct rip_info *rinfo;
478
479 if (type == RMAP_RIP) {
480 /* Fetch routemap's rule information. */
481 address = rule;
482 rinfo = object;
483
484 /* Set next hop value. */
485 rinfo->nexthop_out = *address;
486 }
487
488 return RMAP_OKAY;
489 }
490
491 /* Route map `ip nexthop' compile function. Given string is converted
492 to struct in_addr structure. */
493 static void *route_set_ip_nexthop_compile(const char *arg)
494 {
495 int ret;
496 struct in_addr *address;
497
498 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
499
500 ret = inet_aton(arg, address);
501
502 if (ret == 0) {
503 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
504 return NULL;
505 }
506
507 return address;
508 }
509
510 /* Free route map's compiled `ip nexthop' value. */
511 static void route_set_ip_nexthop_free(void *rule)
512 {
513 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
514 }
515
516 /* Route map commands for ip nexthop set. */
517 static struct route_map_rule_cmd route_set_ip_nexthop_cmd = {
518 "ip next-hop", route_set_ip_nexthop, route_set_ip_nexthop_compile,
519 route_set_ip_nexthop_free};
520
521 /* `set tag TAG' */
522
523 /* Set tag to object. ojbect must be pointer to struct attr. */
524 static enum route_map_cmd_result_t
525 route_set_tag(void *rule, const struct prefix *prefix, route_map_object_t type,
526 void *object)
527 {
528 route_tag_t *tag;
529 struct rip_info *rinfo;
530
531 if (type == RMAP_RIP) {
532 /* Fetch routemap's rule information. */
533 tag = rule;
534 rinfo = object;
535
536 /* Set next hop value. */
537 rinfo->tag_out = *tag;
538 }
539
540 return RMAP_OKAY;
541 }
542
543 /* Route map commands for tag set. */
544 static struct route_map_rule_cmd route_set_tag_cmd = {
545 "tag", route_set_tag, route_map_rule_tag_compile,
546 route_map_rule_tag_free};
547
548 #define MATCH_STR "Match values from routing table\n"
549 #define SET_STR "Set values in destination routing protocol\n"
550
551 /* Route-map init */
552 void rip_route_map_init(void)
553 {
554 route_map_init();
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_hook(generic_match_add);
566 route_map_no_match_ip_next_hop_hook(generic_match_delete);
567
568 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
569 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
570
571 route_map_match_ip_next_hop_type_hook(generic_match_add);
572 route_map_no_match_ip_next_hop_type_hook(generic_match_delete);
573
574 route_map_match_metric_hook(generic_match_add);
575 route_map_no_match_metric_hook(generic_match_delete);
576
577 route_map_match_tag_hook(generic_match_add);
578 route_map_no_match_tag_hook(generic_match_delete);
579
580 route_map_set_ip_nexthop_hook(generic_set_add);
581 route_map_no_set_ip_nexthop_hook(generic_set_delete);
582
583 route_map_set_metric_hook(generic_set_add);
584 route_map_no_set_metric_hook(generic_set_delete);
585
586 route_map_set_tag_hook(generic_set_add);
587 route_map_no_set_tag_hook(generic_set_delete);
588
589 route_map_install_match(&route_match_metric_cmd);
590 route_map_install_match(&route_match_interface_cmd);
591 route_map_install_match(&route_match_ip_next_hop_cmd);
592 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
593 route_map_install_match(&route_match_ip_next_hop_type_cmd);
594 route_map_install_match(&route_match_ip_address_cmd);
595 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
596 route_map_install_match(&route_match_tag_cmd);
597
598 route_map_install_set(&route_set_metric_cmd);
599 route_map_install_set(&route_set_ip_nexthop_cmd);
600 route_map_install_set(&route_set_tag_cmd);
601 }