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 route_map_result_t
route_match_metric(void *rule
,
46 const struct prefix
*prefix
,
47 route_map_object_t type
,
52 struct rip_info
*rinfo
;
54 if (type
== RMAP_RIP
) {
58 /* If external metric is available, the route-map should
59 work on this one (for redistribute purpose) */
60 check
= (rinfo
->external_metric
) ? rinfo
->external_metric
70 /* Route map `match metric' match statement. `arg' is METRIC value */
71 static void *route_match_metric_compile(const char *arg
)
75 metric
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
81 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric
);
85 /* Free route map's compiled `match metric' value. */
86 static void route_match_metric_free(void *rule
)
88 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
91 /* Route map commands for metric matching. */
92 struct route_map_rule_cmd route_match_metric_cmd
= {
93 "metric", route_match_metric
, route_match_metric_compile
,
94 route_match_metric_free
};
96 /* `match interface IFNAME' */
97 /* Match function return 1 if match is success else return zero. */
98 static route_map_result_t
route_match_interface(void *rule
,
99 const struct prefix
*prefix
,
100 route_map_object_t type
,
103 struct rip_info
*rinfo
;
104 struct interface
*ifp
;
107 if (type
== RMAP_RIP
) {
109 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
116 if (rinfo
->ifindex_out
== ifp
->ifindex
117 || rinfo
->nh
.ifindex
== ifp
->ifindex
)
125 /* Route map `match interface' match statement. `arg' is IFNAME value */
126 /* XXX I don`t know if I need to check does interface exist? */
127 static void *route_match_interface_compile(const char *arg
)
129 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
132 /* Free route map's compiled `match interface' value. */
133 static void route_match_interface_free(void *rule
)
135 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
138 /* Route map commands for interface matching. */
139 struct route_map_rule_cmd route_match_interface_cmd
= {
140 "interface", route_match_interface
, route_match_interface_compile
,
141 route_match_interface_free
};
143 /* `match ip next-hop IP_ACCESS_LIST' */
145 /* Match function return 1 if match is success else return zero. */
146 static route_map_result_t
route_match_ip_next_hop(void *rule
,
147 const struct prefix
*prefix
,
148 route_map_object_t type
,
151 struct access_list
*alist
;
152 struct rip_info
*rinfo
;
153 struct prefix_ipv4 p
;
155 if (type
== RMAP_RIP
) {
158 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
) ? rinfo
->nh
.gate
.ipv4
160 p
.prefixlen
= IPV4_MAX_BITLEN
;
162 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
166 return (access_list_apply(alist
, &p
) == FILTER_DENY
173 /* Route map `ip next-hop' match statement. `arg' should be
175 static void *route_match_ip_next_hop_compile(const char *arg
)
177 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
180 /* Free route map's compiled `. */
181 static void route_match_ip_next_hop_free(void *rule
)
183 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
186 /* Route map commands for ip next-hop matching. */
187 static struct route_map_rule_cmd route_match_ip_next_hop_cmd
= {
188 "ip next-hop", route_match_ip_next_hop
, route_match_ip_next_hop_compile
,
189 route_match_ip_next_hop_free
};
191 /* `match ip next-hop prefix-list PREFIX_LIST' */
193 static route_map_result_t
194 route_match_ip_next_hop_prefix_list(void *rule
, const struct prefix
*prefix
,
195 route_map_object_t type
, void *object
)
197 struct prefix_list
*plist
;
198 struct rip_info
*rinfo
;
199 struct prefix_ipv4 p
;
201 if (type
== RMAP_RIP
) {
204 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
) ? rinfo
->nh
.gate
.ipv4
206 p
.prefixlen
= IPV4_MAX_BITLEN
;
208 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
212 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
219 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
221 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
224 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
226 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
229 static struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd
= {
230 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list
,
231 route_match_ip_next_hop_prefix_list_compile
,
232 route_match_ip_next_hop_prefix_list_free
};
234 /* `match ip next-hop type <blackhole>' */
236 static route_map_result_t
237 route_match_ip_next_hop_type(void *rule
, const struct prefix
*prefix
,
238 route_map_object_t type
, void *object
)
240 struct rip_info
*rinfo
;
242 if (type
== RMAP_RIP
&& prefix
->family
== AF_INET
) {
243 rinfo
= (struct rip_info
*)object
;
245 return RMAP_DENYMATCH
;
247 if (rinfo
->nh
.type
== NEXTHOP_TYPE_BLACKHOLE
)
253 static void *route_match_ip_next_hop_type_compile(const char *arg
)
255 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
258 static void route_match_ip_next_hop_type_free(void *rule
)
260 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
263 static struct route_map_rule_cmd route_match_ip_next_hop_type_cmd
= {
264 "ip next-hop type", route_match_ip_next_hop_type
,
265 route_match_ip_next_hop_type_compile
,
266 route_match_ip_next_hop_type_free
};
268 /* `match ip address IP_ACCESS_LIST' */
270 /* Match function should return 1 if match is success else return
272 static route_map_result_t
route_match_ip_address(void *rule
,
273 const struct prefix
*prefix
,
274 route_map_object_t type
,
277 struct access_list
*alist
;
279 if (type
== RMAP_RIP
) {
280 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
284 return (access_list_apply(alist
, prefix
) == FILTER_DENY
291 /* Route map `ip address' match statement. `arg' should be
293 static void *route_match_ip_address_compile(const char *arg
)
295 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
298 /* Free route map's compiled `ip address' value. */
299 static void route_match_ip_address_free(void *rule
)
301 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
304 /* Route map commands for ip address matching. */
305 static struct route_map_rule_cmd route_match_ip_address_cmd
= {
306 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
307 route_match_ip_address_free
};
309 /* `match ip address prefix-list PREFIX_LIST' */
311 static route_map_result_t
312 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
313 route_map_object_t type
, void *object
)
315 struct prefix_list
*plist
;
317 if (type
== RMAP_RIP
) {
318 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
322 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
329 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
331 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
334 static void route_match_ip_address_prefix_list_free(void *rule
)
336 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
339 static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
340 "ip address prefix-list", route_match_ip_address_prefix_list
,
341 route_match_ip_address_prefix_list_compile
,
342 route_match_ip_address_prefix_list_free
};
344 /* `match tag TAG' */
345 /* Match function return 1 if match is success else return zero. */
346 static route_map_result_t
route_match_tag(void *rule
, const struct prefix
*p
,
347 route_map_object_t type
, void *object
)
350 struct rip_info
*rinfo
;
351 route_tag_t rinfo_tag
;
353 if (type
== RMAP_RIP
) {
357 /* The information stored by rinfo is host ordered. */
358 rinfo_tag
= rinfo
->tag
;
359 if (rinfo_tag
== *tag
)
367 /* Route map commands for tag matching. */
368 static struct route_map_rule_cmd route_match_tag_cmd
= {
369 "tag", route_match_tag
, route_map_rule_tag_compile
,
370 route_map_rule_tag_free
,
373 /* `set metric METRIC' */
375 /* Set metric to attribute. */
376 static route_map_result_t
route_set_metric(void *rule
,
377 const struct prefix
*prefix
,
378 route_map_object_t type
,
381 if (type
== RMAP_RIP
) {
382 struct rip_metric_modifier
*mod
;
383 struct rip_info
*rinfo
;
391 if (mod
->type
== metric_increment
)
392 rinfo
->metric_out
+= mod
->metric
;
393 else if (mod
->type
== metric_decrement
)
394 rinfo
->metric_out
-= mod
->metric
;
395 else if (mod
->type
== metric_absolute
)
396 rinfo
->metric_out
= mod
->metric
;
398 if ((signed int)rinfo
->metric_out
< 1)
399 rinfo
->metric_out
= 1;
400 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
401 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
403 rinfo
->metric_set
= 1;
408 /* set metric compilation. */
409 static void *route_set_metric_compile(const char *arg
)
415 struct rip_metric_modifier
*mod
;
417 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
418 sizeof(struct rip_metric_modifier
));
427 /* Examine first character. */
429 mod
->type
= metric_increment
;
431 } else if (arg
[0] == '-') {
432 mod
->type
= metric_decrement
;
435 mod
->type
= metric_absolute
;
437 /* Check beginning with digit string. */
438 if (*pnt
< '0' || *pnt
> '9')
441 /* Convert string to integer. */
442 metric
= strtol(pnt
, &endptr
, 10);
444 if (*endptr
!= '\0' || metric
< 0) {
447 if (metric
> RIP_METRIC_INFINITY
) {
449 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
450 __PRETTY_FUNCTION__
, metric
);
451 mod
->metric
= RIP_METRIC_INFINITY
;
453 mod
->metric
= metric
;
460 /* Free route map's compiled `set metric' value. */
461 static void route_set_metric_free(void *rule
)
463 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
466 /* Set metric rule structure. */
467 static struct route_map_rule_cmd route_set_metric_cmd
= {
468 "metric", route_set_metric
, route_set_metric_compile
,
469 route_set_metric_free
,
472 /* `set ip next-hop IP_ADDRESS' */
474 /* Set nexthop to object. ojbect must be pointer to struct attr. */
475 static route_map_result_t
route_set_ip_nexthop(void *rule
,
476 const struct prefix
*prefix
,
477 route_map_object_t type
,
480 struct in_addr
*address
;
481 struct rip_info
*rinfo
;
483 if (type
== RMAP_RIP
) {
484 /* Fetch routemap's rule information. */
488 /* Set next hop value. */
489 rinfo
->nexthop_out
= *address
;
495 /* Route map `ip nexthop' compile function. Given string is converted
496 to struct in_addr structure. */
497 static void *route_set_ip_nexthop_compile(const char *arg
)
500 struct in_addr
*address
;
502 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in_addr
));
504 ret
= inet_aton(arg
, address
);
507 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
514 /* Free route map's compiled `ip nexthop' value. */
515 static void route_set_ip_nexthop_free(void *rule
)
517 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
520 /* Route map commands for ip nexthop set. */
521 static struct route_map_rule_cmd route_set_ip_nexthop_cmd
= {
522 "ip next-hop", route_set_ip_nexthop
, route_set_ip_nexthop_compile
,
523 route_set_ip_nexthop_free
};
527 /* Set tag to object. ojbect must be pointer to struct attr. */
528 static route_map_result_t
route_set_tag(void *rule
, const struct prefix
*prefix
,
529 route_map_object_t type
, void *object
)
532 struct rip_info
*rinfo
;
534 if (type
== RMAP_RIP
) {
535 /* Fetch routemap's rule information. */
539 /* Set next hop value. */
540 rinfo
->tag_out
= *tag
;
546 /* Route map commands for tag set. */
547 static struct route_map_rule_cmd route_set_tag_cmd
= {
548 "tag", route_set_tag
, route_map_rule_tag_compile
,
549 route_map_rule_tag_free
};
551 #define MATCH_STR "Match values from routing table\n"
552 #define SET_STR "Set values in destination routing protocol\n"
555 void rip_route_map_init(void)
559 route_map_match_interface_hook(generic_match_add
);
560 route_map_no_match_interface_hook(generic_match_delete
);
562 route_map_match_ip_address_hook(generic_match_add
);
563 route_map_no_match_ip_address_hook(generic_match_delete
);
565 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
566 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
568 route_map_match_ip_next_hop_hook(generic_match_add
);
569 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
571 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
572 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
574 route_map_match_ip_next_hop_type_hook(generic_match_add
);
575 route_map_no_match_ip_next_hop_type_hook(generic_match_delete
);
577 route_map_match_metric_hook(generic_match_add
);
578 route_map_no_match_metric_hook(generic_match_delete
);
580 route_map_match_tag_hook(generic_match_add
);
581 route_map_no_match_tag_hook(generic_match_delete
);
583 route_map_set_ip_nexthop_hook(generic_set_add
);
584 route_map_no_set_ip_nexthop_hook(generic_set_delete
);
586 route_map_set_metric_hook(generic_set_add
);
587 route_map_no_set_metric_hook(generic_set_delete
);
589 route_map_set_tag_hook(generic_set_add
);
590 route_map_no_set_tag_hook(generic_set_delete
);
592 route_map_install_match(&route_match_metric_cmd
);
593 route_map_install_match(&route_match_interface_cmd
);
594 route_map_install_match(&route_match_ip_next_hop_cmd
);
595 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
596 route_map_install_match(&route_match_ip_next_hop_type_cmd
);
597 route_map_install_match(&route_match_ip_address_cmd
);
598 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
599 route_map_install_match(&route_match_tag_cmd
);
601 route_map_install_set(&route_set_metric_cmd
);
602 route_map_install_set(&route_set_ip_nexthop_cmd
);
603 route_map_install_set(&route_set_tag_cmd
);