2 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
3 * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
5 * This file is part of GNU Zebra.
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
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.
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
31 #include "sockunion.h" /* for inet_aton () */
35 #include "ripd/ripd.h"
37 struct rip_metric_modifier
{
38 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
43 /* `match metric METRIC' */
44 /* Match function return 1 if match is success else return zero. */
45 static enum route_map_cmd_result_t
46 route_match_metric(void *rule
, const struct prefix
*prefix
,
47 route_map_object_t type
, void *object
)
51 struct rip_info
*rinfo
;
53 if (type
== RMAP_RIP
) {
57 /* If external metric is available, the route-map should
58 work on this one (for redistribute purpose) */
59 check
= (rinfo
->external_metric
) ? rinfo
->external_metric
69 /* Route map `match metric' match statement. `arg' is METRIC value */
70 static void *route_match_metric_compile(const char *arg
)
74 metric
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
80 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric
);
84 /* Free route map's compiled `match metric' value. */
85 static void route_match_metric_free(void *rule
)
87 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
90 /* Route map commands for metric matching. */
91 static const struct route_map_rule_cmd route_match_metric_cmd
= {
94 route_match_metric_compile
,
95 route_match_metric_free
98 /* `match interface IFNAME' */
99 /* Match function return 1 if match is success else return zero. */
100 static enum route_map_cmd_result_t
101 route_match_interface(void *rule
, const struct prefix
*prefix
,
102 route_map_object_t type
, void *object
)
104 struct rip_info
*rinfo
;
105 struct interface
*ifp
;
108 if (type
== RMAP_RIP
) {
110 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
117 if (rinfo
->ifindex_out
== ifp
->ifindex
118 || rinfo
->nh
.ifindex
== ifp
->ifindex
)
126 /* Route map `match interface' match statement. `arg' is IFNAME value */
127 /* XXX I don`t know if I need to check does interface exist? */
128 static void *route_match_interface_compile(const char *arg
)
130 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
133 /* Free route map's compiled `match interface' value. */
134 static void route_match_interface_free(void *rule
)
136 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
139 /* Route map commands for interface matching. */
140 static const struct route_map_rule_cmd route_match_interface_cmd
= {
142 route_match_interface
,
143 route_match_interface_compile
,
144 route_match_interface_free
147 /* `match ip next-hop IP_ACCESS_LIST' */
149 /* Match function return 1 if match is success else return zero. */
150 static enum route_map_cmd_result_t
151 route_match_ip_next_hop(void *rule
, const struct prefix
*prefix
,
152 route_map_object_t type
, void *object
)
154 struct access_list
*alist
;
155 struct rip_info
*rinfo
;
156 struct prefix_ipv4 p
;
158 if (type
== RMAP_RIP
) {
161 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
162 ? rinfo
->nh
.gate
.ipv4
164 p
.prefixlen
= IPV4_MAX_BITLEN
;
166 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
170 return (access_list_apply(alist
, &p
) == FILTER_DENY
177 /* Route map `ip next-hop' match statement. `arg' should be
179 static void *route_match_ip_next_hop_compile(const char *arg
)
181 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
184 /* Free route map's compiled `. */
185 static void route_match_ip_next_hop_free(void *rule
)
187 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
190 /* Route map commands for ip next-hop matching. */
191 static const struct route_map_rule_cmd route_match_ip_next_hop_cmd
= {
193 route_match_ip_next_hop
,
194 route_match_ip_next_hop_compile
,
195 route_match_ip_next_hop_free
198 /* `match ip next-hop prefix-list PREFIX_LIST' */
200 static enum route_map_cmd_result_t
201 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
202 route_map_object_t type
, void *object
)
204 struct prefix_list
*plist
;
205 struct rip_info
*rinfo
;
206 struct prefix_ipv4 p
;
208 if (type
== RMAP_RIP
) {
211 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
212 ? rinfo
->nh
.gate
.ipv4
214 p
.prefixlen
= IPV4_MAX_BITLEN
;
216 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
220 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
227 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
229 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
232 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
234 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
237 static const struct route_map_rule_cmd
238 route_match_ip_next_hop_prefix_list_cmd
= {
239 "ip next-hop prefix-list",
240 route_match_ip_next_hop_prefix_list
,
241 route_match_ip_next_hop_prefix_list_compile
,
242 route_match_ip_next_hop_prefix_list_free
245 /* `match ip next-hop type <blackhole>' */
247 static enum route_map_cmd_result_t
248 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
249 route_map_object_t type
, void *object
)
251 struct rip_info
*rinfo
;
253 if (type
== RMAP_RIP
&& prefix
->family
== AF_INET
) {
254 rinfo
= (struct rip_info
*)object
;
258 if (rinfo
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
264 static void *route_match_ip_next_hop_type_compile(const char *arg
)
266 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
269 static void route_match_ip_next_hop_type_free(void *rule
)
271 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
274 static const struct route_map_rule_cmd
275 route_match_ip_next_hop_type_cmd
= {
277 route_match_ip_next_hop_type
,
278 route_match_ip_next_hop_type_compile
,
279 route_match_ip_next_hop_type_free
282 /* `match ip address IP_ACCESS_LIST' */
284 /* Match function should return 1 if match is success else return
286 static enum route_map_cmd_result_t
287 route_match_ip_address(void *rule
, const struct prefix
*prefix
,
288 route_map_object_t type
, void *object
)
290 struct access_list
*alist
;
292 if (type
== RMAP_RIP
) {
293 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
297 return (access_list_apply(alist
, prefix
) == FILTER_DENY
304 /* Route map `ip address' match statement. `arg' should be
306 static void *route_match_ip_address_compile(const char *arg
)
308 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
311 /* Free route map's compiled `ip address' value. */
312 static void route_match_ip_address_free(void *rule
)
314 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
317 /* Route map commands for ip address matching. */
318 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
320 route_match_ip_address
,
321 route_match_ip_address_compile
,
322 route_match_ip_address_free
325 /* `match ip address prefix-list PREFIX_LIST' */
327 static enum route_map_cmd_result_t
328 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
329 route_map_object_t type
, void *object
)
331 struct prefix_list
*plist
;
333 if (type
== RMAP_RIP
) {
334 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
338 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
345 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
347 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
350 static void route_match_ip_address_prefix_list_free(void *rule
)
352 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
355 static const struct route_map_rule_cmd
356 route_match_ip_address_prefix_list_cmd
= {
357 "ip address prefix-list",
358 route_match_ip_address_prefix_list
,
359 route_match_ip_address_prefix_list_compile
,
360 route_match_ip_address_prefix_list_free
363 /* `match tag TAG' */
364 /* Match function return 1 if match is success else return zero. */
365 static enum route_map_cmd_result_t
366 route_match_tag(void *rule
, const struct prefix
*p
, route_map_object_t type
,
370 struct rip_info
*rinfo
;
371 route_tag_t rinfo_tag
;
373 if (type
== RMAP_RIP
) {
377 /* The information stored by rinfo is host ordered. */
378 rinfo_tag
= rinfo
->tag
;
379 if (rinfo_tag
== *tag
)
387 /* Route map commands for tag matching. */
388 static const struct route_map_rule_cmd route_match_tag_cmd
= {
391 route_map_rule_tag_compile
,
392 route_map_rule_tag_free
,
395 /* `set metric METRIC' */
397 /* Set metric to attribute. */
398 static enum route_map_cmd_result_t
399 route_set_metric(void *rule
, const struct prefix
*prefix
,
400 route_map_object_t type
, void *object
)
402 if (type
== RMAP_RIP
) {
403 struct rip_metric_modifier
*mod
;
404 struct rip_info
*rinfo
;
412 if (mod
->type
== metric_increment
)
413 rinfo
->metric_out
+= mod
->metric
;
414 else if (mod
->type
== metric_decrement
)
415 rinfo
->metric_out
-= mod
->metric
;
416 else if (mod
->type
== metric_absolute
)
417 rinfo
->metric_out
= mod
->metric
;
419 if ((signed int)rinfo
->metric_out
< 1)
420 rinfo
->metric_out
= 1;
421 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
422 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
424 rinfo
->metric_set
= 1;
429 /* set metric compilation. */
430 static void *route_set_metric_compile(const char *arg
)
436 struct rip_metric_modifier
*mod
;
438 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
439 sizeof(struct rip_metric_modifier
));
448 /* Examine first character. */
450 mod
->type
= metric_increment
;
452 } else if (arg
[0] == '-') {
453 mod
->type
= metric_decrement
;
456 mod
->type
= metric_absolute
;
458 /* Check beginning with digit string. */
459 if (*pnt
< '0' || *pnt
> '9')
462 /* Convert string to integer. */
463 metric
= strtol(pnt
, &endptr
, 10);
465 if (*endptr
!= '\0' || metric
< 0) {
468 if (metric
> RIP_METRIC_INFINITY
) {
470 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
472 mod
->metric
= RIP_METRIC_INFINITY
;
474 mod
->metric
= metric
;
481 /* Free route map's compiled `set metric' value. */
482 static void route_set_metric_free(void *rule
)
484 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
487 /* Set metric rule structure. */
488 static const struct route_map_rule_cmd route_set_metric_cmd
= {
491 route_set_metric_compile
,
492 route_set_metric_free
,
495 /* `set ip next-hop IP_ADDRESS' */
497 /* Set nexthop to object. ojbect must be pointer to struct attr. */
498 static enum route_map_cmd_result_t
route_set_ip_nexthop(void *rule
,
499 const struct prefix
*prefix
,
500 route_map_object_t type
,
503 struct in_addr
*address
;
504 struct rip_info
*rinfo
;
506 if (type
== RMAP_RIP
) {
507 /* Fetch routemap's rule information. */
511 /* Set next hop value. */
512 rinfo
->nexthop_out
= *address
;
518 /* Route map `ip nexthop' compile function. Given string is converted
519 to struct in_addr structure. */
520 static void *route_set_ip_nexthop_compile(const char *arg
)
523 struct in_addr
*address
;
525 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in_addr
));
527 ret
= inet_aton(arg
, address
);
530 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
537 /* Free route map's compiled `ip nexthop' value. */
538 static void route_set_ip_nexthop_free(void *rule
)
540 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
543 /* Route map commands for ip nexthop set. */
544 static const struct route_map_rule_cmd route_set_ip_nexthop_cmd
= {
546 route_set_ip_nexthop
,
547 route_set_ip_nexthop_compile
,
548 route_set_ip_nexthop_free
553 /* Set tag to object. ojbect must be pointer to struct attr. */
554 static enum route_map_cmd_result_t
555 route_set_tag(void *rule
, const struct prefix
*prefix
, route_map_object_t type
,
559 struct rip_info
*rinfo
;
561 if (type
== RMAP_RIP
) {
562 /* Fetch routemap's rule information. */
566 /* Set next hop value. */
567 rinfo
->tag_out
= *tag
;
573 /* Route map commands for tag set. */
574 static const struct route_map_rule_cmd route_set_tag_cmd
= {
577 route_map_rule_tag_compile
,
578 route_map_rule_tag_free
581 #define MATCH_STR "Match values from routing table\n"
582 #define SET_STR "Set values in destination routing protocol\n"
585 void rip_route_map_init(void)
589 route_map_match_interface_hook(generic_match_add
);
590 route_map_no_match_interface_hook(generic_match_delete
);
592 route_map_match_ip_address_hook(generic_match_add
);
593 route_map_no_match_ip_address_hook(generic_match_delete
);
595 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
596 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
598 route_map_match_ip_next_hop_hook(generic_match_add
);
599 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
601 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
602 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
604 route_map_match_ip_next_hop_type_hook(generic_match_add
);
605 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
607 route_map_match_metric_hook(generic_match_add
);
608 route_map_no_match_metric_hook(generic_match_delete
);
610 route_map_match_tag_hook(generic_match_add
);
611 route_map_no_match_tag_hook(generic_match_delete
);
613 route_map_set_ip_nexthop_hook(generic_set_add
);
614 route_map_no_set_ip_nexthop_hook(generic_set_delete
);
616 route_map_set_metric_hook(generic_set_add
);
617 route_map_no_set_metric_hook(generic_set_delete
);
619 route_map_set_tag_hook(generic_set_add
);
620 route_map_no_set_tag_hook(generic_set_delete
);
622 route_map_install_match(&route_match_metric_cmd
);
623 route_map_install_match(&route_match_interface_cmd
);
624 route_map_install_match(&route_match_ip_next_hop_cmd
);
625 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
626 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
627 route_map_install_match(&route_match_ip_address_cmd
);
628 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
629 route_map_install_match(&route_match_tag_cmd
);
631 route_map_install_set(&route_set_metric_cmd
);
632 route_map_install_set(&route_set_ip_nexthop_cmd
);
633 route_map_install_set(&route_set_tag_cmd
);