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