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