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