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 struct route_map_rule_cmd route_match_metric_cmd
= {
92 "metric", route_match_metric
, route_match_metric_compile
,
93 route_match_metric_free
};
95 /* `match interface IFNAME' */
96 /* Match function return 1 if match is success else return zero. */
97 static enum route_map_cmd_result_t
98 route_match_interface(void *rule
, const struct prefix
*prefix
,
99 route_map_object_t type
, void *object
)
101 struct rip_info
*rinfo
;
102 struct interface
*ifp
;
105 if (type
== RMAP_RIP
) {
107 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
114 if (rinfo
->ifindex_out
== ifp
->ifindex
115 || rinfo
->nh
.ifindex
== ifp
->ifindex
)
123 /* Route map `match interface' match statement. `arg' is IFNAME value */
124 /* XXX I don`t know if I need to check does interface exist? */
125 static void *route_match_interface_compile(const char *arg
)
127 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
130 /* Free route map's compiled `match interface' value. */
131 static void route_match_interface_free(void *rule
)
133 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
136 /* Route map commands for interface matching. */
137 struct route_map_rule_cmd route_match_interface_cmd
= {
138 "interface", route_match_interface
, route_match_interface_compile
,
139 route_match_interface_free
};
141 /* `match ip next-hop IP_ACCESS_LIST' */
143 /* Match function return 1 if match is success else return zero. */
144 static enum route_map_cmd_result_t
145 route_match_ip_next_hop(void *rule
, const struct prefix
*prefix
,
146 route_map_object_t type
, void *object
)
148 struct access_list
*alist
;
149 struct rip_info
*rinfo
;
150 struct prefix_ipv4 p
;
152 if (type
== RMAP_RIP
) {
155 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
) ? rinfo
->nh
.gate
.ipv4
157 p
.prefixlen
= IPV4_MAX_BITLEN
;
159 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
163 return (access_list_apply(alist
, &p
) == FILTER_DENY
170 /* Route map `ip next-hop' match statement. `arg' should be
172 static void *route_match_ip_next_hop_compile(const char *arg
)
174 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
177 /* Free route map's compiled `. */
178 static void route_match_ip_next_hop_free(void *rule
)
180 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
183 /* Route map commands for ip next-hop matching. */
184 static struct route_map_rule_cmd route_match_ip_next_hop_cmd
= {
185 "ip next-hop", route_match_ip_next_hop
, route_match_ip_next_hop_compile
,
186 route_match_ip_next_hop_free
};
188 /* `match ip next-hop prefix-list PREFIX_LIST' */
190 static enum route_map_cmd_result_t
191 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
192 route_map_object_t type
, void *object
)
194 struct prefix_list
*plist
;
195 struct rip_info
*rinfo
;
196 struct prefix_ipv4 p
;
198 if (type
== RMAP_RIP
) {
201 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
) ? rinfo
->nh
.gate
.ipv4
203 p
.prefixlen
= IPV4_MAX_BITLEN
;
205 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
209 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
216 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
218 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
221 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
223 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
226 static struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd
= {
227 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list
,
228 route_match_ip_next_hop_prefix_list_compile
,
229 route_match_ip_next_hop_prefix_list_free
};
231 /* `match ip next-hop type <blackhole>' */
233 static enum route_map_cmd_result_t
234 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
235 route_map_object_t type
, void *object
)
237 struct rip_info
*rinfo
;
239 if (type
== RMAP_RIP
&& prefix
->family
== AF_INET
) {
240 rinfo
= (struct rip_info
*)object
;
244 if (rinfo
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
250 static void *route_match_ip_next_hop_type_compile(const char *arg
)
252 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
255 static void route_match_ip_next_hop_type_free(void *rule
)
257 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
260 static struct route_map_rule_cmd route_match_ip_next_hop_type_cmd
= {
261 "ip next-hop type", route_match_ip_next_hop_type
,
262 route_match_ip_next_hop_type_compile
,
263 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
,
271 route_map_object_t type
, void *object
)
273 struct access_list
*alist
;
275 if (type
== RMAP_RIP
) {
276 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
280 return (access_list_apply(alist
, prefix
) == FILTER_DENY
287 /* Route map `ip address' match statement. `arg' should be
289 static void *route_match_ip_address_compile(const char *arg
)
291 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
294 /* Free route map's compiled `ip address' value. */
295 static void route_match_ip_address_free(void *rule
)
297 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
300 /* Route map commands for ip address matching. */
301 static struct route_map_rule_cmd route_match_ip_address_cmd
= {
302 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
303 route_match_ip_address_free
};
305 /* `match ip address prefix-list PREFIX_LIST' */
307 static enum route_map_cmd_result_t
308 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
309 route_map_object_t type
, void *object
)
311 struct prefix_list
*plist
;
313 if (type
== RMAP_RIP
) {
314 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
318 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
325 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
327 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
330 static void route_match_ip_address_prefix_list_free(void *rule
)
332 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
335 static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
336 "ip address prefix-list", route_match_ip_address_prefix_list
,
337 route_match_ip_address_prefix_list_compile
,
338 route_match_ip_address_prefix_list_free
};
340 /* `match tag TAG' */
341 /* Match function return 1 if match is success else return zero. */
342 static enum route_map_cmd_result_t
343 route_match_tag(void *rule
, const struct prefix
*p
, route_map_object_t type
,
347 struct rip_info
*rinfo
;
348 route_tag_t rinfo_tag
;
350 if (type
== RMAP_RIP
) {
354 /* The information stored by rinfo is host ordered. */
355 rinfo_tag
= rinfo
->tag
;
356 if (rinfo_tag
== *tag
)
364 /* Route map commands for tag matching. */
365 static struct route_map_rule_cmd route_match_tag_cmd
= {
366 "tag", route_match_tag
, route_map_rule_tag_compile
,
367 route_map_rule_tag_free
,
370 /* `set metric METRIC' */
372 /* Set metric to attribute. */
373 static enum route_map_cmd_result_t
374 route_set_metric(void *rule
, const struct prefix
*prefix
,
375 route_map_object_t type
, void *object
)
377 if (type
== RMAP_RIP
) {
378 struct rip_metric_modifier
*mod
;
379 struct rip_info
*rinfo
;
387 if (mod
->type
== metric_increment
)
388 rinfo
->metric_out
+= mod
->metric
;
389 else if (mod
->type
== metric_decrement
)
390 rinfo
->metric_out
-= mod
->metric
;
391 else if (mod
->type
== metric_absolute
)
392 rinfo
->metric_out
= mod
->metric
;
394 if ((signed int)rinfo
->metric_out
< 1)
395 rinfo
->metric_out
= 1;
396 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
397 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
399 rinfo
->metric_set
= 1;
404 /* set metric compilation. */
405 static void *route_set_metric_compile(const char *arg
)
411 struct rip_metric_modifier
*mod
;
413 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
414 sizeof(struct rip_metric_modifier
));
423 /* Examine first character. */
425 mod
->type
= metric_increment
;
427 } else if (arg
[0] == '-') {
428 mod
->type
= metric_decrement
;
431 mod
->type
= metric_absolute
;
433 /* Check beginning with digit string. */
434 if (*pnt
< '0' || *pnt
> '9')
437 /* Convert string to integer. */
438 metric
= strtol(pnt
, &endptr
, 10);
440 if (*endptr
!= '\0' || metric
< 0) {
443 if (metric
> RIP_METRIC_INFINITY
) {
445 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
446 __PRETTY_FUNCTION__
, metric
);
447 mod
->metric
= RIP_METRIC_INFINITY
;
449 mod
->metric
= metric
;
456 /* Free route map's compiled `set metric' value. */
457 static void route_set_metric_free(void *rule
)
459 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
462 /* Set metric rule structure. */
463 static struct route_map_rule_cmd route_set_metric_cmd
= {
464 "metric", route_set_metric
, route_set_metric_compile
,
465 route_set_metric_free
,
468 /* `set ip next-hop IP_ADDRESS' */
470 /* Set nexthop to object. ojbect must be pointer to struct attr. */
471 static enum route_map_cmd_result_t
route_set_ip_nexthop(void *rule
,
472 const struct prefix
*prefix
,
473 route_map_object_t type
,
476 struct in_addr
*address
;
477 struct rip_info
*rinfo
;
479 if (type
== RMAP_RIP
) {
480 /* Fetch routemap's rule information. */
484 /* Set next hop value. */
485 rinfo
->nexthop_out
= *address
;
491 /* Route map `ip nexthop' compile function. Given string is converted
492 to struct in_addr structure. */
493 static void *route_set_ip_nexthop_compile(const char *arg
)
496 struct in_addr
*address
;
498 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in_addr
));
500 ret
= inet_aton(arg
, address
);
503 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
510 /* Free route map's compiled `ip nexthop' value. */
511 static void route_set_ip_nexthop_free(void *rule
)
513 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
516 /* Route map commands for ip nexthop set. */
517 static struct route_map_rule_cmd route_set_ip_nexthop_cmd
= {
518 "ip next-hop", route_set_ip_nexthop
, route_set_ip_nexthop_compile
,
519 route_set_ip_nexthop_free
};
523 /* Set tag to object. ojbect must be pointer to struct attr. */
524 static enum route_map_cmd_result_t
525 route_set_tag(void *rule
, const struct prefix
*prefix
, route_map_object_t type
,
529 struct rip_info
*rinfo
;
531 if (type
== RMAP_RIP
) {
532 /* Fetch routemap's rule information. */
536 /* Set next hop value. */
537 rinfo
->tag_out
= *tag
;
543 /* Route map commands for tag set. */
544 static struct route_map_rule_cmd route_set_tag_cmd
= {
545 "tag", route_set_tag
, route_map_rule_tag_compile
,
546 route_map_rule_tag_free
};
548 #define MATCH_STR "Match values from routing table\n"
549 #define SET_STR "Set values in destination routing protocol\n"
552 void rip_route_map_init(void)
556 route_map_match_interface_hook(generic_match_add
);
557 route_map_no_match_interface_hook(generic_match_delete
);
559 route_map_match_ip_address_hook(generic_match_add
);
560 route_map_no_match_ip_address_hook(generic_match_delete
);
562 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
563 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
565 route_map_match_ip_next_hop_hook(generic_match_add
);
566 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
568 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
569 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
571 route_map_match_ip_next_hop_type_hook(generic_match_add
);
572 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
574 route_map_match_metric_hook(generic_match_add
);
575 route_map_no_match_metric_hook(generic_match_delete
);
577 route_map_match_tag_hook(generic_match_add
);
578 route_map_no_match_tag_hook(generic_match_delete
);
580 route_map_set_ip_nexthop_hook(generic_set_add
);
581 route_map_no_set_ip_nexthop_hook(generic_set_delete
);
583 route_map_set_metric_hook(generic_set_add
);
584 route_map_no_set_metric_hook(generic_set_delete
);
586 route_map_set_tag_hook(generic_set_add
);
587 route_map_no_set_tag_hook(generic_set_delete
);
589 route_map_install_match(&route_match_metric_cmd
);
590 route_map_install_match(&route_match_interface_cmd
);
591 route_map_install_match(&route_match_ip_next_hop_cmd
);
592 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
593 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
594 route_map_install_match(&route_match_ip_address_cmd
);
595 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
596 route_map_install_match(&route_match_tag_cmd
);
598 route_map_install_set(&route_set_metric_cmd
);
599 route_map_install_set(&route_set_ip_nexthop_cmd
);
600 route_map_install_set(&route_set_tag_cmd
);