1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
4 * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
16 #include "sockunion.h" /* for inet_aton () */
20 #include "ripd/ripd.h"
22 struct rip_metric_modifier
{
23 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
28 /* `match metric METRIC' */
29 /* Match function return 1 if match is success else return zero. */
30 static enum route_map_cmd_result_t
31 route_match_metric(void *rule
, const struct prefix
*prefix
, void *object
)
35 struct rip_info
*rinfo
;
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
50 /* Route map `match metric' match statement. `arg' is METRIC value */
51 static void *route_match_metric_compile(const char *arg
)
55 metric
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
61 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric
);
65 /* Free route map's compiled `match metric' value. */
66 static void route_match_metric_free(void *rule
)
68 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
71 /* Route map commands for metric matching. */
72 static const struct route_map_rule_cmd route_match_metric_cmd
= {
75 route_match_metric_compile
,
76 route_match_metric_free
79 /* `match interface IFNAME' */
80 /* Match function return 1 if match is success else return zero. */
81 static enum route_map_cmd_result_t
82 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
84 struct rip_info
*rinfo
;
85 struct interface
*ifp
;
89 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
96 if (rinfo
->ifindex_out
== ifp
->ifindex
97 || rinfo
->nh
.ifindex
== ifp
->ifindex
)
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? */
105 static void *route_match_interface_compile(const char *arg
)
107 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
110 /* Free route map's compiled `match interface' value. */
111 static void route_match_interface_free(void *rule
)
113 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
116 /* Route map commands for interface matching. */
117 static const struct route_map_rule_cmd route_match_interface_cmd
= {
119 route_match_interface
,
120 route_match_interface_compile
,
121 route_match_interface_free
124 /* `match ip next-hop IP_ACCESS_LIST' */
126 /* Match function return 1 if match is success else return zero. */
127 static enum route_map_cmd_result_t
128 route_match_ip_next_hop(void *rule
, const struct prefix
*prefix
, void *object
)
130 struct access_list
*alist
;
131 struct rip_info
*rinfo
;
132 struct prefix_ipv4 p
;
136 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
137 ? rinfo
->nh
.gate
.ipv4
139 p
.prefixlen
= IPV4_MAX_BITLEN
;
141 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
145 return (access_list_apply(alist
, &p
) == FILTER_DENY
? RMAP_NOMATCH
149 /* Route map `ip next-hop' match statement. `arg' should be
151 static void *route_match_ip_next_hop_compile(const char *arg
)
153 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
156 /* Free route map's compiled `. */
157 static void route_match_ip_next_hop_free(void *rule
)
159 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
162 /* Route map commands for ip next-hop matching. */
163 static const struct route_map_rule_cmd route_match_ip_next_hop_cmd
= {
165 route_match_ip_next_hop
,
166 route_match_ip_next_hop_compile
,
167 route_match_ip_next_hop_free
170 /* `match ip next-hop prefix-list PREFIX_LIST' */
172 static enum route_map_cmd_result_t
173 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
176 struct prefix_list
*plist
;
177 struct rip_info
*rinfo
;
178 struct prefix_ipv4 p
;
182 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
183 ? rinfo
->nh
.gate
.ipv4
185 p
.prefixlen
= IPV4_MAX_BITLEN
;
187 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
191 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
? RMAP_NOMATCH
195 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
197 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
200 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
202 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
205 static 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
,
209 route_match_ip_next_hop_prefix_list_compile
,
210 route_match_ip_next_hop_prefix_list_free
213 /* `match ip next-hop type <blackhole>' */
215 static enum route_map_cmd_result_t
216 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
219 struct rip_info
*rinfo
;
221 if (prefix
->family
== AF_INET
) {
222 rinfo
= (struct rip_info
*)object
;
226 if (rinfo
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
232 static void *route_match_ip_next_hop_type_compile(const char *arg
)
234 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
237 static void route_match_ip_next_hop_type_free(void *rule
)
239 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
242 static const struct route_map_rule_cmd
243 route_match_ip_next_hop_type_cmd
= {
245 route_match_ip_next_hop_type
,
246 route_match_ip_next_hop_type_compile
,
247 route_match_ip_next_hop_type_free
250 /* `match ip address IP_ACCESS_LIST' */
252 /* Match function should return 1 if match is success else return
254 static enum route_map_cmd_result_t
255 route_match_ip_address(void *rule
, const struct prefix
*prefix
, void *object
)
257 struct access_list
*alist
;
259 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
263 return (access_list_apply(alist
, prefix
) == FILTER_DENY
? RMAP_NOMATCH
267 /* Route map `ip address' match statement. `arg' should be
269 static void *route_match_ip_address_compile(const char *arg
)
271 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
274 /* Free route map's compiled `ip address' value. */
275 static void route_match_ip_address_free(void *rule
)
277 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
280 /* Route map commands for ip address matching. */
281 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
283 route_match_ip_address
,
284 route_match_ip_address_compile
,
285 route_match_ip_address_free
288 /* `match ip address prefix-list PREFIX_LIST' */
290 static enum route_map_cmd_result_t
291 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
294 struct prefix_list
*plist
;
296 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
300 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
? RMAP_NOMATCH
304 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
306 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
309 static void route_match_ip_address_prefix_list_free(void *rule
)
311 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
314 static 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
,
318 route_match_ip_address_prefix_list_compile
,
319 route_match_ip_address_prefix_list_free
322 /* `match tag TAG' */
323 /* Match function return 1 if match is success else return zero. */
324 static enum route_map_cmd_result_t
325 route_match_tag(void *rule
, const struct prefix
*p
, void *object
)
328 struct rip_info
*rinfo
;
329 route_tag_t rinfo_tag
;
334 /* The information stored by rinfo is host ordered. */
335 rinfo_tag
= rinfo
->tag
;
336 if (rinfo_tag
== *tag
)
342 /* Route map commands for tag matching. */
343 static const struct route_map_rule_cmd route_match_tag_cmd
= {
346 route_map_rule_tag_compile
,
347 route_map_rule_tag_free
,
350 /* `set metric METRIC' */
352 /* Set metric to attribute. */
353 static enum route_map_cmd_result_t
354 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
356 struct rip_metric_modifier
*mod
;
357 struct rip_info
*rinfo
;
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
;
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
;
377 rinfo
->metric_set
= 1;
381 /* set metric compilation. */
382 static void *route_set_metric_compile(const char *arg
)
388 struct rip_metric_modifier
*mod
;
390 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
391 sizeof(struct rip_metric_modifier
));
400 /* Examine first character. */
402 mod
->type
= metric_increment
;
404 } else if (arg
[0] == '-') {
405 mod
->type
= metric_decrement
;
408 mod
->type
= metric_absolute
;
410 /* Check beginning with digit string. */
411 if (*pnt
< '0' || *pnt
> '9')
414 /* Convert string to integer. */
415 metric
= strtol(pnt
, &endptr
, 10);
417 if (*endptr
!= '\0' || metric
< 0) {
420 if (metric
> RIP_METRIC_INFINITY
) {
422 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
424 mod
->metric
= RIP_METRIC_INFINITY
;
426 mod
->metric
= metric
;
433 /* Free route map's compiled `set metric' value. */
434 static void route_set_metric_free(void *rule
)
436 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
439 /* Set metric rule structure. */
440 static const struct route_map_rule_cmd route_set_metric_cmd
= {
443 route_set_metric_compile
,
444 route_set_metric_free
,
447 /* `set ip next-hop IP_ADDRESS' */
449 /* Set nexthop to object. object must be pointer to struct attr. */
450 static enum route_map_cmd_result_t
451 route_set_ip_nexthop(void *rule
, const struct prefix
*prefix
,
455 struct in_addr
*address
;
456 struct rip_info
*rinfo
;
458 /* Fetch routemap's rule information. */
462 /* Set next hop value. */
463 rinfo
->nexthop_out
= *address
;
468 /* Route map `ip nexthop' compile function. Given string is converted
469 to struct in_addr structure. */
470 static void *route_set_ip_nexthop_compile(const char *arg
)
473 struct in_addr
*address
;
475 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in_addr
));
477 ret
= inet_aton(arg
, address
);
480 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
487 /* Free route map's compiled `ip nexthop' value. */
488 static void route_set_ip_nexthop_free(void *rule
)
490 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
493 /* Route map commands for ip nexthop set. */
494 static const struct route_map_rule_cmd route_set_ip_nexthop_cmd
= {
496 route_set_ip_nexthop
,
497 route_set_ip_nexthop_compile
,
498 route_set_ip_nexthop_free
503 /* Set tag to object. object must be pointer to struct attr. */
504 static enum route_map_cmd_result_t
505 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
508 struct rip_info
*rinfo
;
510 /* Fetch routemap's rule information. */
514 /* Set next hop value. */
515 rinfo
->tag_out
= *tag
;
520 /* Route map commands for tag set. */
521 static const struct route_map_rule_cmd route_set_tag_cmd
= {
524 route_map_rule_tag_compile
,
525 route_map_rule_tag_free
528 #define MATCH_STR "Match values from routing table\n"
529 #define SET_STR "Set values in destination routing protocol\n"
532 void rip_route_map_init(void)
536 route_map_match_interface_hook(generic_match_add
);
537 route_map_no_match_interface_hook(generic_match_delete
);
539 route_map_match_ip_address_hook(generic_match_add
);
540 route_map_no_match_ip_address_hook(generic_match_delete
);
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
);
545 route_map_match_ip_next_hop_hook(generic_match_add
);
546 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
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
);
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
);
554 route_map_match_metric_hook(generic_match_add
);
555 route_map_no_match_metric_hook(generic_match_delete
);
557 route_map_match_tag_hook(generic_match_add
);
558 route_map_no_match_tag_hook(generic_match_delete
);
560 route_map_set_ip_nexthop_hook(generic_set_add
);
561 route_map_no_set_ip_nexthop_hook(generic_set_delete
);
563 route_map_set_metric_hook(generic_set_add
);
564 route_map_no_set_metric_hook(generic_set_delete
);
566 route_map_set_tag_hook(generic_set_add
);
567 route_map_no_set_tag_hook(generic_set_delete
);
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_next_hop_type_cmd
);
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
);
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
);