]> git.proxmox.com Git - mirror_frr.git/blame - ripd/rip_routemap.c
debianpkg: Support --with-pkg-extra-version and other modifier to build custom packag...
[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 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
ac4d0be5 20 * 02111-1307, USA.
718e3744 21 */
22
23#include <zebra.h>
24
25#include "memory.h"
26#include "prefix.h"
82f97584 27#include "vty.h"
718e3744 28#include "routemap.h"
29#include "command.h"
30#include "filter.h"
31#include "log.h"
ac4d0be5 32#include "sockunion.h" /* for inet_aton () */
718e3744 33#include "plist.h"
1306c09a 34#include "vrf.h"
718e3744 35
36#include "ripd/ripd.h"
6b0655a2 37
ac4d0be5 38struct rip_metric_modifier {
39 enum { metric_increment, metric_decrement, metric_absolute } type;
40
41 u_char metric;
16705130 42};
718e3744 43
718e3744 44/* Hook function for updating route_map assignment. */
11dde9c2 45/* ARGSUSED */
ac4d0be5 46static void rip_route_map_update(const char *notused)
718e3744 47{
ac4d0be5 48 int i;
49
50 if (rip) {
51 for (i = 0; i < ZEBRA_ROUTE_MAX; i++) {
52 if (rip->route_map[i].name)
53 rip->route_map[i].map =
54 route_map_lookup_by_name(
55 rip->route_map[i].name);
56 }
718e3744 57 }
718e3744 58}
6b0655a2 59
718e3744 60/* `match metric METRIC' */
61/* Match function return 1 if match is success else return zero. */
ac4d0be5 62static route_map_result_t route_match_metric(void *rule, struct prefix *prefix,
63 route_map_object_t type,
64 void *object)
65{
66 u_int32_t *metric;
67 u_int32_t check;
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 */
ac4d0be5 87static void *route_match_metric_compile(const char *arg)
718e3744 88{
ac4d0be5 89 u_int32_t *metric;
718e3744 90
ac4d0be5 91 metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(u_int32_t));
92 *metric = atoi(arg);
718e3744 93
ac4d0be5 94 if (*metric > 0)
95 return metric;
718e3744 96
ac4d0be5 97 XFREE(MTYPE_ROUTE_MAP_COMPILED, metric);
98 return NULL;
718e3744 99}
100
101/* Free route map's compiled `match metric' value. */
ac4d0be5 102static void route_match_metric_free(void *rule)
718e3744 103{
ac4d0be5 104 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 105}
106
107/* Route map commands for metric matching. */
ac4d0be5 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. */
ac4d0be5 114static route_map_result_t route_match_interface(void *rule,
115 struct prefix *prefix,
116 route_map_object_t type,
117 void *object)
718e3744 118{
ac4d0be5 119 struct rip_info *rinfo;
120 struct interface *ifp;
121 char *ifname;
718e3744 122
ac4d0be5 123 if (type == RMAP_RIP) {
124 ifname = rule;
125 ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
718e3744 126
ac4d0be5 127 if (!ifp)
128 return RMAP_NOMATCH;
718e3744 129
ac4d0be5 130 rinfo = object;
718e3744 131
ac4d0be5 132 if (rinfo->ifindex_out == ifp->ifindex
133 || rinfo->ifindex == ifp->ifindex)
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? */
ac4d0be5 143static void *route_match_interface_compile(const char *arg)
718e3744 144{
ac4d0be5 145 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 146}
147
148/* Free route map's compiled `match interface' value. */
ac4d0be5 149static void route_match_interface_free(void *rule)
718e3744 150{
ac4d0be5 151 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 152}
153
154/* Route map commands for interface matching. */
ac4d0be5 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. */
ac4d0be5 162static route_map_result_t route_match_ip_next_hop(void *rule,
163 struct prefix *prefix,
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;
174 p.prefix =
175 (rinfo->nexthop.s_addr) ? rinfo->nexthop : rinfo->from;
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. */
ac4d0be5 191static void *route_match_ip_next_hop_compile(const char *arg)
718e3744 192{
ac4d0be5 193 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 194}
195
196/* Free route map's compiled `. */
ac4d0be5 197static void route_match_ip_next_hop_free(void *rule)
718e3744 198{
ac4d0be5 199 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 200}
201
202/* Route map commands for ip next-hop matching. */
ac4d0be5 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
ac4d0be5 210route_match_ip_next_hop_prefix_list(void *rule, struct prefix *prefix,
211 route_map_object_t type, void *object)
718e3744 212{
ac4d0be5 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;
220 p.prefix =
221 (rinfo->nexthop.s_addr) ? rinfo->nexthop : rinfo->from;
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
ac4d0be5 235static void *route_match_ip_next_hop_prefix_list_compile(const char *arg)
718e3744 236{
ac4d0be5 237 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 238}
239
ac4d0be5 240static void route_match_ip_next_hop_prefix_list_free(void *rule)
718e3744 241{
ac4d0be5 242 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 243}
244
ac4d0be5 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. */
ac4d0be5 254static route_map_result_t route_match_ip_address(void *rule,
255 struct prefix *prefix,
256 route_map_object_t type,
257 void *object)
718e3744 258{
ac4d0be5 259 struct access_list *alist;
718e3744 260
ac4d0be5 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. */
ac4d0be5 275static void *route_match_ip_address_compile(const char *arg)
718e3744 276{
ac4d0be5 277 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 278}
279
280/* Free route map's compiled `ip address' value. */
ac4d0be5 281static void route_match_ip_address_free(void *rule)
718e3744 282{
ac4d0be5 283 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 284}
285
286/* Route map commands for ip address matching. */
ac4d0be5 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
ac4d0be5 294route_match_ip_address_prefix_list(void *rule, struct prefix *prefix,
295 route_map_object_t type, void *object)
718e3744 296{
ac4d0be5 297 struct prefix_list *plist;
718e3744 298
ac4d0be5 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
ac4d0be5 311static void *route_match_ip_address_prefix_list_compile(const char *arg)
718e3744 312{
ac4d0be5 313 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 314}
315
ac4d0be5 316static void route_match_ip_address_prefix_list_free(void *rule)
718e3744 317{
ac4d0be5 318 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 319}
320
ac4d0be5 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. */
ac4d0be5 328static route_map_result_t route_match_tag(void *rule, struct prefix *prefix,
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. */
ac4d0be5 350static struct route_map_rule_cmd route_match_tag_cmd = {
351 "tag", route_match_tag, route_map_rule_tag_compile,
352 route_map_rule_tag_free,
16705130 353};
6b0655a2 354
718e3744 355/* `set metric METRIC' */
356
357/* Set metric to attribute. */
ac4d0be5 358static route_map_result_t route_set_metric(void *rule, struct prefix *prefix,
359 route_map_object_t type,
360 void *object)
361{
362 if (type == RMAP_RIP) {
363 struct rip_metric_modifier *mod;
364 struct rip_info *rinfo;
365
366 mod = rule;
367 rinfo = object;
368
369 if (mod->type == metric_increment)
370 rinfo->metric_out += mod->metric;
371 else if (mod->type == metric_decrement)
372 rinfo->metric_out -= mod->metric;
373 else if (mod->type == metric_absolute)
374 rinfo->metric_out = mod->metric;
375
376 if ((signed int)rinfo->metric_out < 1)
377 rinfo->metric_out = 1;
378 if (rinfo->metric_out > RIP_METRIC_INFINITY)
379 rinfo->metric_out = RIP_METRIC_INFINITY;
380
381 rinfo->metric_set = 1;
382 }
383 return RMAP_OKAY;
718e3744 384}
385
386/* set metric compilation. */
ac4d0be5 387static void *route_set_metric_compile(const char *arg)
388{
389 int len;
390 const char *pnt;
391 int type;
392 long metric;
393 char *endptr = NULL;
394 struct rip_metric_modifier *mod;
395
396 len = strlen(arg);
397 pnt = arg;
398
399 if (len == 0)
400 return NULL;
401
402 /* Examine first character. */
403 if (arg[0] == '+') {
404 type = metric_increment;
405 pnt++;
406 } else if (arg[0] == '-') {
407 type = metric_decrement;
408 pnt++;
409 } else
410 type = metric_absolute;
411
412 /* Check beginning with digit string. */
413 if (*pnt < '0' || *pnt > '9')
414 return NULL;
415
416 /* Convert string to integer. */
417 metric = strtol(pnt, &endptr, 10);
418
419 if (metric == LONG_MAX || *endptr != '\0')
420 return NULL;
421 if (metric < 0 || metric > RIP_METRIC_INFINITY)
422 return NULL;
423
424 mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
425 sizeof(struct rip_metric_modifier));
426 mod->type = type;
427 mod->metric = metric;
428
429 return mod;
718e3744 430}
431
432/* Free route map's compiled `set metric' value. */
ac4d0be5 433static void route_set_metric_free(void *rule)
718e3744 434{
ac4d0be5 435 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 436}
437
438/* Set metric rule structure. */
ac4d0be5 439static struct route_map_rule_cmd route_set_metric_cmd = {
440 "metric", route_set_metric, route_set_metric_compile,
441 route_set_metric_free,
718e3744 442};
443
444/* `set ip next-hop IP_ADDRESS' */
445
446/* Set nexthop to object. ojbect must be pointer to struct attr. */
ac4d0be5 447static route_map_result_t route_set_ip_nexthop(void *rule,
448 struct prefix *prefix,
449 route_map_object_t type,
450 void *object)
718e3744 451{
ac4d0be5 452 struct in_addr *address;
453 struct rip_info *rinfo;
454
455 if (type == RMAP_RIP) {
456 /* Fetch routemap's rule information. */
457 address = rule;
458 rinfo = object;
459
460 /* Set next hop value. */
461 rinfo->nexthop_out = *address;
462 }
463
464 return RMAP_OKAY;
718e3744 465}
466
467/* Route map `ip nexthop' compile function. Given string is converted
468 to struct in_addr structure. */
ac4d0be5 469static void *route_set_ip_nexthop_compile(const char *arg)
718e3744 470{
ac4d0be5 471 int ret;
472 struct in_addr *address;
718e3744 473
ac4d0be5 474 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
718e3744 475
ac4d0be5 476 ret = inet_aton(arg, address);
718e3744 477
ac4d0be5 478 if (ret == 0) {
479 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
480 return NULL;
481 }
718e3744 482
ac4d0be5 483 return address;
718e3744 484}
485
486/* Free route map's compiled `ip nexthop' value. */
ac4d0be5 487static void route_set_ip_nexthop_free(void *rule)
718e3744 488{
ac4d0be5 489 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 490}
491
492/* Route map commands for ip nexthop set. */
ac4d0be5 493static struct route_map_rule_cmd route_set_ip_nexthop_cmd = {
494 "ip next-hop", route_set_ip_nexthop, route_set_ip_nexthop_compile,
495 route_set_ip_nexthop_free};
16705130 496
497/* `set tag TAG' */
498
499/* Set tag to object. ojbect must be pointer to struct attr. */
ac4d0be5 500static route_map_result_t route_set_tag(void *rule, struct prefix *prefix,
501 route_map_object_t type, void *object)
16705130 502{
ac4d0be5 503 route_tag_t *tag;
504 struct rip_info *rinfo;
505
506 if (type == RMAP_RIP) {
507 /* Fetch routemap's rule information. */
508 tag = rule;
509 rinfo = object;
510
511 /* Set next hop value. */
512 rinfo->tag_out = *tag;
513 }
514
515 return RMAP_OKAY;
16705130 516}
517
16705130 518/* Route map commands for tag set. */
ac4d0be5 519static struct route_map_rule_cmd route_set_tag_cmd = {
520 "tag", route_set_tag, route_map_rule_tag_compile,
521 route_map_rule_tag_free};
6b0655a2 522
718e3744 523#define MATCH_STR "Match values from routing table\n"
524#define SET_STR "Set values in destination routing protocol\n"
525
ac4d0be5 526void rip_route_map_reset()
718e3744 527{
ac4d0be5 528 ;
718e3744 529}
530
531/* Route-map init */
ac4d0be5 532void rip_route_map_init()
718e3744 533{
ac4d0be5 534 route_map_init();
718e3744 535
ac4d0be5 536 route_map_add_hook(rip_route_map_update);
537 route_map_delete_hook(rip_route_map_update);
718e3744 538
ac4d0be5 539 route_map_match_interface_hook(generic_match_add);
540 route_map_no_match_interface_hook(generic_match_delete);
82f97584 541
ac4d0be5 542 route_map_match_ip_address_hook(generic_match_add);
543 route_map_no_match_ip_address_hook(generic_match_delete);
82f97584 544
ac4d0be5 545 route_map_match_ip_address_prefix_list_hook(generic_match_add);
546 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete);
82f97584 547
ac4d0be5 548 route_map_match_ip_next_hop_hook(generic_match_add);
549 route_map_no_match_ip_next_hop_hook(generic_match_delete);
82f97584 550
ac4d0be5 551 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
552 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
82f97584 553
ac4d0be5 554 route_map_match_metric_hook(generic_match_add);
555 route_map_no_match_metric_hook(generic_match_delete);
82f97584 556
ac4d0be5 557 route_map_match_tag_hook(generic_match_add);
558 route_map_no_match_tag_hook(generic_match_delete);
82f97584 559
ac4d0be5 560 route_map_set_ip_nexthop_hook(generic_set_add);
561 route_map_no_set_ip_nexthop_hook(generic_set_delete);
82f97584 562
ac4d0be5 563 route_map_set_metric_hook(generic_set_add);
564 route_map_no_set_metric_hook(generic_set_delete);
82f97584 565
ac4d0be5 566 route_map_set_tag_hook(generic_set_add);
567 route_map_no_set_tag_hook(generic_set_delete);
82f97584 568
ac4d0be5 569 route_map_install_match(&route_match_metric_cmd);
570 route_map_install_match(&route_match_interface_cmd);
571 route_map_install_match(&route_match_ip_next_hop_cmd);
572 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
573 route_map_install_match(&route_match_ip_address_cmd);
574 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
575 route_map_install_match(&route_match_tag_cmd);
718e3744 576
ac4d0be5 577 route_map_install_set(&route_set_metric_cmd);
578 route_map_install_set(&route_set_ip_nexthop_cmd);
579 route_map_install_set(&route_set_tag_cmd);
718e3744 580}