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
, void *object
)
50 struct rip_info
*rinfo
;
55 /* If external metric is available, the route-map should
56 work on this one (for redistribute purpose) */
57 check
= (rinfo
->external_metric
) ? rinfo
->external_metric
65 /* Route map `match metric' match statement. `arg' is METRIC value */
66 static void *route_match_metric_compile(const char *arg
)
70 metric
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
76 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric
);
80 /* Free route map's compiled `match metric' value. */
81 static void route_match_metric_free(void *rule
)
83 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
86 /* Route map commands for metric matching. */
87 static const struct route_map_rule_cmd route_match_metric_cmd
= {
90 route_match_metric_compile
,
91 route_match_metric_free
94 /* `match interface IFNAME' */
95 /* Match function return 1 if match is success else return zero. */
96 static enum route_map_cmd_result_t
97 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
99 struct rip_info
*rinfo
;
100 struct interface
*ifp
;
104 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
111 if (rinfo
->ifindex_out
== ifp
->ifindex
112 || rinfo
->nh
.ifindex
== ifp
->ifindex
)
118 /* Route map `match interface' match statement. `arg' is IFNAME value */
119 /* XXX I don`t know if I need to check does interface exist? */
120 static void *route_match_interface_compile(const char *arg
)
122 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
125 /* Free route map's compiled `match interface' value. */
126 static void route_match_interface_free(void *rule
)
128 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
131 /* Route map commands for interface matching. */
132 static const struct route_map_rule_cmd route_match_interface_cmd
= {
134 route_match_interface
,
135 route_match_interface_compile
,
136 route_match_interface_free
139 /* `match ip next-hop IP_ACCESS_LIST' */
141 /* Match function return 1 if match is success else return zero. */
142 static enum route_map_cmd_result_t
143 route_match_ip_next_hop(void *rule
, const struct prefix
*prefix
, void *object
)
145 struct access_list
*alist
;
146 struct rip_info
*rinfo
;
147 struct prefix_ipv4 p
;
151 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
152 ? rinfo
->nh
.gate
.ipv4
154 p
.prefixlen
= IPV4_MAX_BITLEN
;
156 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
160 return (access_list_apply(alist
, &p
) == FILTER_DENY
? RMAP_NOMATCH
164 /* Route map `ip next-hop' match statement. `arg' should be
166 static void *route_match_ip_next_hop_compile(const char *arg
)
168 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
171 /* Free route map's compiled `. */
172 static void route_match_ip_next_hop_free(void *rule
)
174 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
177 /* Route map commands for ip next-hop matching. */
178 static const struct route_map_rule_cmd route_match_ip_next_hop_cmd
= {
180 route_match_ip_next_hop
,
181 route_match_ip_next_hop_compile
,
182 route_match_ip_next_hop_free
185 /* `match ip next-hop prefix-list PREFIX_LIST' */
187 static enum route_map_cmd_result_t
188 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
191 struct prefix_list
*plist
;
192 struct rip_info
*rinfo
;
193 struct prefix_ipv4 p
;
197 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
!= INADDR_ANY
)
198 ? rinfo
->nh
.gate
.ipv4
200 p
.prefixlen
= IPV4_MAX_BITLEN
;
202 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
206 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
? RMAP_NOMATCH
210 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
212 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
215 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
217 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
220 static const struct route_map_rule_cmd
221 route_match_ip_next_hop_prefix_list_cmd
= {
222 "ip next-hop prefix-list",
223 route_match_ip_next_hop_prefix_list
,
224 route_match_ip_next_hop_prefix_list_compile
,
225 route_match_ip_next_hop_prefix_list_free
228 /* `match ip next-hop type <blackhole>' */
230 static enum route_map_cmd_result_t
231 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
234 struct rip_info
*rinfo
;
236 if (prefix
->family
== AF_INET
) {
237 rinfo
= (struct rip_info
*)object
;
241 if (rinfo
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
247 static void *route_match_ip_next_hop_type_compile(const char *arg
)
249 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
252 static void route_match_ip_next_hop_type_free(void *rule
)
254 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
257 static const struct route_map_rule_cmd
258 route_match_ip_next_hop_type_cmd
= {
260 route_match_ip_next_hop_type
,
261 route_match_ip_next_hop_type_compile
,
262 route_match_ip_next_hop_type_free
265 /* `match ip address IP_ACCESS_LIST' */
267 /* Match function should return 1 if match is success else return
269 static enum route_map_cmd_result_t
270 route_match_ip_address(void *rule
, const struct prefix
*prefix
, void *object
)
272 struct access_list
*alist
;
274 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
278 return (access_list_apply(alist
, prefix
) == FILTER_DENY
? RMAP_NOMATCH
282 /* Route map `ip address' match statement. `arg' should be
284 static void *route_match_ip_address_compile(const char *arg
)
286 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
289 /* Free route map's compiled `ip address' value. */
290 static void route_match_ip_address_free(void *rule
)
292 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
295 /* Route map commands for ip address matching. */
296 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
298 route_match_ip_address
,
299 route_match_ip_address_compile
,
300 route_match_ip_address_free
303 /* `match ip address prefix-list PREFIX_LIST' */
305 static enum route_map_cmd_result_t
306 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
309 struct prefix_list
*plist
;
311 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
315 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
? RMAP_NOMATCH
319 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
321 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
324 static void route_match_ip_address_prefix_list_free(void *rule
)
326 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
329 static const struct route_map_rule_cmd
330 route_match_ip_address_prefix_list_cmd
= {
331 "ip address prefix-list",
332 route_match_ip_address_prefix_list
,
333 route_match_ip_address_prefix_list_compile
,
334 route_match_ip_address_prefix_list_free
337 /* `match tag TAG' */
338 /* Match function return 1 if match is success else return zero. */
339 static enum route_map_cmd_result_t
340 route_match_tag(void *rule
, const struct prefix
*p
, void *object
)
343 struct rip_info
*rinfo
;
344 route_tag_t rinfo_tag
;
349 /* The information stored by rinfo is host ordered. */
350 rinfo_tag
= rinfo
->tag
;
351 if (rinfo_tag
== *tag
)
357 /* Route map commands for tag matching. */
358 static const struct route_map_rule_cmd route_match_tag_cmd
= {
361 route_map_rule_tag_compile
,
362 route_map_rule_tag_free
,
365 /* `set metric METRIC' */
367 /* Set metric to attribute. */
368 static enum route_map_cmd_result_t
369 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
371 struct rip_metric_modifier
*mod
;
372 struct rip_info
*rinfo
;
380 if (mod
->type
== metric_increment
)
381 rinfo
->metric_out
+= mod
->metric
;
382 else if (mod
->type
== metric_decrement
)
383 rinfo
->metric_out
-= mod
->metric
;
384 else if (mod
->type
== metric_absolute
)
385 rinfo
->metric_out
= mod
->metric
;
387 if ((signed int)rinfo
->metric_out
< 1)
388 rinfo
->metric_out
= 1;
389 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
390 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
392 rinfo
->metric_set
= 1;
396 /* set metric compilation. */
397 static void *route_set_metric_compile(const char *arg
)
403 struct rip_metric_modifier
*mod
;
405 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
406 sizeof(struct rip_metric_modifier
));
415 /* Examine first character. */
417 mod
->type
= metric_increment
;
419 } else if (arg
[0] == '-') {
420 mod
->type
= metric_decrement
;
423 mod
->type
= metric_absolute
;
425 /* Check beginning with digit string. */
426 if (*pnt
< '0' || *pnt
> '9')
429 /* Convert string to integer. */
430 metric
= strtol(pnt
, &endptr
, 10);
432 if (*endptr
!= '\0' || metric
< 0) {
435 if (metric
> RIP_METRIC_INFINITY
) {
437 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
439 mod
->metric
= RIP_METRIC_INFINITY
;
441 mod
->metric
= metric
;
448 /* Free route map's compiled `set metric' value. */
449 static void route_set_metric_free(void *rule
)
451 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
454 /* Set metric rule structure. */
455 static const struct route_map_rule_cmd route_set_metric_cmd
= {
458 route_set_metric_compile
,
459 route_set_metric_free
,
462 /* `set ip next-hop IP_ADDRESS' */
464 /* Set nexthop to object. ojbect must be pointer to struct attr. */
465 static enum route_map_cmd_result_t
466 route_set_ip_nexthop(void *rule
, const struct prefix
*prefix
,
470 struct in_addr
*address
;
471 struct rip_info
*rinfo
;
473 /* Fetch routemap's rule information. */
477 /* Set next hop value. */
478 rinfo
->nexthop_out
= *address
;
483 /* Route map `ip nexthop' compile function. Given string is converted
484 to struct in_addr structure. */
485 static void *route_set_ip_nexthop_compile(const char *arg
)
488 struct in_addr
*address
;
490 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in_addr
));
492 ret
= inet_aton(arg
, address
);
495 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
502 /* Free route map's compiled `ip nexthop' value. */
503 static void route_set_ip_nexthop_free(void *rule
)
505 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
508 /* Route map commands for ip nexthop set. */
509 static const struct route_map_rule_cmd route_set_ip_nexthop_cmd
= {
511 route_set_ip_nexthop
,
512 route_set_ip_nexthop_compile
,
513 route_set_ip_nexthop_free
518 /* Set tag to object. ojbect must be pointer to struct attr. */
519 static enum route_map_cmd_result_t
520 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
523 struct rip_info
*rinfo
;
525 /* Fetch routemap's rule information. */
529 /* Set next hop value. */
530 rinfo
->tag_out
= *tag
;
535 /* Route map commands for tag set. */
536 static const struct route_map_rule_cmd route_set_tag_cmd
= {
539 route_map_rule_tag_compile
,
540 route_map_rule_tag_free
543 #define MATCH_STR "Match values from routing table\n"
544 #define SET_STR "Set values in destination routing protocol\n"
547 void rip_route_map_init(void)
551 route_map_match_interface_hook(generic_match_add
);
552 route_map_no_match_interface_hook(generic_match_delete
);
554 route_map_match_ip_address_hook(generic_match_add
);
555 route_map_no_match_ip_address_hook(generic_match_delete
);
557 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
558 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
560 route_map_match_ip_next_hop_hook(generic_match_add
);
561 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
563 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
564 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
566 route_map_match_ip_next_hop_type_hook(generic_match_add
);
567 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
569 route_map_match_metric_hook(generic_match_add
);
570 route_map_no_match_metric_hook(generic_match_delete
);
572 route_map_match_tag_hook(generic_match_add
);
573 route_map_no_match_tag_hook(generic_match_delete
);
575 route_map_set_ip_nexthop_hook(generic_set_add
);
576 route_map_no_set_ip_nexthop_hook(generic_set_delete
);
578 route_map_set_metric_hook(generic_set_add
);
579 route_map_no_set_metric_hook(generic_set_delete
);
581 route_map_set_tag_hook(generic_set_add
);
582 route_map_no_set_tag_hook(generic_set_delete
);
584 route_map_install_match(&route_match_metric_cmd
);
585 route_map_install_match(&route_match_interface_cmd
);
586 route_map_install_match(&route_match_ip_next_hop_cmd
);
587 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
588 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
589 route_map_install_match(&route_match_ip_address_cmd
);
590 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
591 route_map_install_match(&route_match_tag_cmd
);
593 route_map_install_set(&route_set_metric_cmd
);
594 route_map_install_set(&route_set_ip_nexthop_cmd
);
595 route_map_install_set(&route_set_tag_cmd
);