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
) ? rinfo
->nh
.gate
.ipv4
163 p
.prefixlen
= IPV4_MAX_BITLEN
;
165 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
169 return (access_list_apply(alist
, &p
) == FILTER_DENY
176 /* Route map `ip next-hop' match statement. `arg' should be
178 static void *route_match_ip_next_hop_compile(const char *arg
)
180 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
183 /* Free route map's compiled `. */
184 static void route_match_ip_next_hop_free(void *rule
)
186 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
189 /* Route map commands for ip next-hop matching. */
190 static const struct route_map_rule_cmd route_match_ip_next_hop_cmd
= {
192 route_match_ip_next_hop
,
193 route_match_ip_next_hop_compile
,
194 route_match_ip_next_hop_free
197 /* `match ip next-hop prefix-list PREFIX_LIST' */
199 static enum route_map_cmd_result_t
200 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
201 route_map_object_t type
, void *object
)
203 struct prefix_list
*plist
;
204 struct rip_info
*rinfo
;
205 struct prefix_ipv4 p
;
207 if (type
== RMAP_RIP
) {
210 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
) ? rinfo
->nh
.gate
.ipv4
212 p
.prefixlen
= IPV4_MAX_BITLEN
;
214 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
218 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
225 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
227 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
230 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
232 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
235 static const struct route_map_rule_cmd
236 route_match_ip_next_hop_prefix_list_cmd
= {
237 "ip next-hop prefix-list",
238 route_match_ip_next_hop_prefix_list
,
239 route_match_ip_next_hop_prefix_list_compile
,
240 route_match_ip_next_hop_prefix_list_free
243 /* `match ip next-hop type <blackhole>' */
245 static enum route_map_cmd_result_t
246 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
247 route_map_object_t type
, void *object
)
249 struct rip_info
*rinfo
;
251 if (type
== RMAP_RIP
&& prefix
->family
== AF_INET
) {
252 rinfo
= (struct rip_info
*)object
;
256 if (rinfo
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
262 static void *route_match_ip_next_hop_type_compile(const char *arg
)
264 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
267 static void route_match_ip_next_hop_type_free(void *rule
)
269 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
272 static const struct route_map_rule_cmd
273 route_match_ip_next_hop_type_cmd
= {
275 route_match_ip_next_hop_type
,
276 route_match_ip_next_hop_type_compile
,
277 route_match_ip_next_hop_type_free
280 /* `match ip address IP_ACCESS_LIST' */
282 /* Match function should return 1 if match is success else return
284 static enum route_map_cmd_result_t
285 route_match_ip_address(void *rule
, const struct prefix
*prefix
,
286 route_map_object_t type
, void *object
)
288 struct access_list
*alist
;
290 if (type
== RMAP_RIP
) {
291 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
295 return (access_list_apply(alist
, prefix
) == FILTER_DENY
302 /* Route map `ip address' match statement. `arg' should be
304 static void *route_match_ip_address_compile(const char *arg
)
306 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
309 /* Free route map's compiled `ip address' value. */
310 static void route_match_ip_address_free(void *rule
)
312 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
315 /* Route map commands for ip address matching. */
316 static const struct route_map_rule_cmd route_match_ip_address_cmd
= {
318 route_match_ip_address
,
319 route_match_ip_address_compile
,
320 route_match_ip_address_free
323 /* `match ip address prefix-list PREFIX_LIST' */
325 static enum route_map_cmd_result_t
326 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
327 route_map_object_t type
, void *object
)
329 struct prefix_list
*plist
;
331 if (type
== RMAP_RIP
) {
332 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
336 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
343 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
345 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
348 static void route_match_ip_address_prefix_list_free(void *rule
)
350 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
353 static const struct route_map_rule_cmd
354 route_match_ip_address_prefix_list_cmd
= {
355 "ip address prefix-list",
356 route_match_ip_address_prefix_list
,
357 route_match_ip_address_prefix_list_compile
,
358 route_match_ip_address_prefix_list_free
361 /* `match tag TAG' */
362 /* Match function return 1 if match is success else return zero. */
363 static enum route_map_cmd_result_t
364 route_match_tag(void *rule
, const struct prefix
*p
, route_map_object_t type
,
368 struct rip_info
*rinfo
;
369 route_tag_t rinfo_tag
;
371 if (type
== RMAP_RIP
) {
375 /* The information stored by rinfo is host ordered. */
376 rinfo_tag
= rinfo
->tag
;
377 if (rinfo_tag
== *tag
)
385 /* Route map commands for tag matching. */
386 static const struct route_map_rule_cmd route_match_tag_cmd
= {
389 route_map_rule_tag_compile
,
390 route_map_rule_tag_free
,
393 /* `set metric METRIC' */
395 /* Set metric to attribute. */
396 static enum route_map_cmd_result_t
397 route_set_metric(void *rule
, const struct prefix
*prefix
,
398 route_map_object_t type
, void *object
)
400 if (type
== RMAP_RIP
) {
401 struct rip_metric_modifier
*mod
;
402 struct rip_info
*rinfo
;
410 if (mod
->type
== metric_increment
)
411 rinfo
->metric_out
+= mod
->metric
;
412 else if (mod
->type
== metric_decrement
)
413 rinfo
->metric_out
-= mod
->metric
;
414 else if (mod
->type
== metric_absolute
)
415 rinfo
->metric_out
= mod
->metric
;
417 if ((signed int)rinfo
->metric_out
< 1)
418 rinfo
->metric_out
= 1;
419 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
420 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
422 rinfo
->metric_set
= 1;
427 /* set metric compilation. */
428 static void *route_set_metric_compile(const char *arg
)
434 struct rip_metric_modifier
*mod
;
436 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
437 sizeof(struct rip_metric_modifier
));
446 /* Examine first character. */
448 mod
->type
= metric_increment
;
450 } else if (arg
[0] == '-') {
451 mod
->type
= metric_decrement
;
454 mod
->type
= metric_absolute
;
456 /* Check beginning with digit string. */
457 if (*pnt
< '0' || *pnt
> '9')
460 /* Convert string to integer. */
461 metric
= strtol(pnt
, &endptr
, 10);
463 if (*endptr
!= '\0' || metric
< 0) {
466 if (metric
> RIP_METRIC_INFINITY
) {
468 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
469 __PRETTY_FUNCTION__
, metric
);
470 mod
->metric
= RIP_METRIC_INFINITY
;
472 mod
->metric
= metric
;
479 /* Free route map's compiled `set metric' value. */
480 static void route_set_metric_free(void *rule
)
482 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
485 /* Set metric rule structure. */
486 static const struct route_map_rule_cmd route_set_metric_cmd
= {
489 route_set_metric_compile
,
490 route_set_metric_free
,
493 /* `set ip next-hop IP_ADDRESS' */
495 /* Set nexthop to object. ojbect must be pointer to struct attr. */
496 static enum route_map_cmd_result_t
route_set_ip_nexthop(void *rule
,
497 const struct prefix
*prefix
,
498 route_map_object_t type
,
501 struct in_addr
*address
;
502 struct rip_info
*rinfo
;
504 if (type
== RMAP_RIP
) {
505 /* Fetch routemap's rule information. */
509 /* Set next hop value. */
510 rinfo
->nexthop_out
= *address
;
516 /* Route map `ip nexthop' compile function. Given string is converted
517 to struct in_addr structure. */
518 static void *route_set_ip_nexthop_compile(const char *arg
)
521 struct in_addr
*address
;
523 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in_addr
));
525 ret
= inet_aton(arg
, address
);
528 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
535 /* Free route map's compiled `ip nexthop' value. */
536 static void route_set_ip_nexthop_free(void *rule
)
538 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
541 /* Route map commands for ip nexthop set. */
542 static const struct route_map_rule_cmd route_set_ip_nexthop_cmd
= {
544 route_set_ip_nexthop
,
545 route_set_ip_nexthop_compile
,
546 route_set_ip_nexthop_free
551 /* Set tag to object. ojbect must be pointer to struct attr. */
552 static enum route_map_cmd_result_t
553 route_set_tag(void *rule
, const struct prefix
*prefix
, route_map_object_t type
,
557 struct rip_info
*rinfo
;
559 if (type
== RMAP_RIP
) {
560 /* Fetch routemap's rule information. */
564 /* Set next hop value. */
565 rinfo
->tag_out
= *tag
;
571 /* Route map commands for tag set. */
572 static const struct route_map_rule_cmd route_set_tag_cmd
= {
575 route_map_rule_tag_compile
,
576 route_map_rule_tag_free
579 #define MATCH_STR "Match values from routing table\n"
580 #define SET_STR "Set values in destination routing protocol\n"
583 void rip_route_map_init(void)
587 route_map_match_interface_hook(generic_match_add
);
588 route_map_no_match_interface_hook(generic_match_delete
);
590 route_map_match_ip_address_hook(generic_match_add
);
591 route_map_no_match_ip_address_hook(generic_match_delete
);
593 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
594 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
596 route_map_match_ip_next_hop_hook(generic_match_add
);
597 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
599 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
600 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
602 route_map_match_ip_next_hop_type_hook(generic_match_add
);
603 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
605 route_map_match_metric_hook(generic_match_add
);
606 route_map_no_match_metric_hook(generic_match_delete
);
608 route_map_match_tag_hook(generic_match_add
);
609 route_map_no_match_tag_hook(generic_match_delete
);
611 route_map_set_ip_nexthop_hook(generic_set_add
);
612 route_map_no_set_ip_nexthop_hook(generic_set_delete
);
614 route_map_set_metric_hook(generic_set_add
);
615 route_map_no_set_metric_hook(generic_set_delete
);
617 route_map_set_tag_hook(generic_set_add
);
618 route_map_no_set_tag_hook(generic_set_delete
);
620 route_map_install_match(&route_match_metric_cmd
);
621 route_map_install_match(&route_match_interface_cmd
);
622 route_map_install_match(&route_match_ip_next_hop_cmd
);
623 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
624 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
625 route_map_install_match(&route_match_ip_address_cmd
);
626 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
627 route_map_install_match(&route_match_tag_cmd
);
629 route_map_install_set(&route_set_metric_cmd
);
630 route_map_install_set(&route_set_ip_nexthop_cmd
);
631 route_map_install_set(&route_set_tag_cmd
);