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 /* Hook function for updating route_map assignment. */
45 static void rip_route_map_update(const char *notused
)
50 for (i
= 0; i
< ZEBRA_ROUTE_MAX
; i
++) {
51 if (rip
->route_map
[i
].name
)
52 rip
->route_map
[i
].map
=
53 route_map_lookup_by_name(
54 rip
->route_map
[i
].name
);
59 /* `match metric METRIC' */
60 /* Match function return 1 if match is success else return zero. */
61 static route_map_result_t
route_match_metric(void *rule
, struct prefix
*prefix
,
62 route_map_object_t type
,
67 struct rip_info
*rinfo
;
69 if (type
== RMAP_RIP
) {
73 /* If external metric is available, the route-map should
74 work on this one (for redistribute purpose) */
75 check
= (rinfo
->external_metric
) ? rinfo
->external_metric
85 /* Route map `match metric' match statement. `arg' is METRIC value */
86 static void *route_match_metric_compile(const char *arg
)
90 metric
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(u_int32_t
));
96 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric
);
100 /* Free route map's compiled `match metric' value. */
101 static void route_match_metric_free(void *rule
)
103 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
106 /* Route map commands for metric matching. */
107 struct route_map_rule_cmd route_match_metric_cmd
= {
108 "metric", route_match_metric
, route_match_metric_compile
,
109 route_match_metric_free
};
111 /* `match interface IFNAME' */
112 /* Match function return 1 if match is success else return zero. */
113 static route_map_result_t
route_match_interface(void *rule
,
114 struct prefix
*prefix
,
115 route_map_object_t type
,
118 struct rip_info
*rinfo
;
119 struct interface
*ifp
;
122 if (type
== RMAP_RIP
) {
124 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
131 if (rinfo
->ifindex_out
== ifp
->ifindex
132 || rinfo
->nh
.ifindex
== ifp
->ifindex
)
140 /* Route map `match interface' match statement. `arg' is IFNAME value */
141 /* XXX I don`t know if I need to check does interface exist? */
142 static void *route_match_interface_compile(const char *arg
)
144 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
147 /* Free route map's compiled `match interface' value. */
148 static void route_match_interface_free(void *rule
)
150 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
153 /* Route map commands for interface matching. */
154 struct route_map_rule_cmd route_match_interface_cmd
= {
155 "interface", route_match_interface
, route_match_interface_compile
,
156 route_match_interface_free
};
158 /* `match ip next-hop IP_ACCESS_LIST' */
160 /* Match function return 1 if match is success else return zero. */
161 static route_map_result_t
route_match_ip_next_hop(void *rule
,
162 struct prefix
*prefix
,
163 route_map_object_t type
,
166 struct access_list
*alist
;
167 struct rip_info
*rinfo
;
168 struct prefix_ipv4 p
;
170 if (type
== RMAP_RIP
) {
173 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
) ? rinfo
->nh
.gate
.ipv4
175 p
.prefixlen
= IPV4_MAX_BITLEN
;
177 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
181 return (access_list_apply(alist
, &p
) == FILTER_DENY
188 /* Route map `ip next-hop' match statement. `arg' should be
190 static void *route_match_ip_next_hop_compile(const char *arg
)
192 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
195 /* Free route map's compiled `. */
196 static void route_match_ip_next_hop_free(void *rule
)
198 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
201 /* Route map commands for ip next-hop matching. */
202 static struct route_map_rule_cmd route_match_ip_next_hop_cmd
= {
203 "ip next-hop", route_match_ip_next_hop
, route_match_ip_next_hop_compile
,
204 route_match_ip_next_hop_free
};
206 /* `match ip next-hop prefix-list PREFIX_LIST' */
208 static route_map_result_t
209 route_match_ip_next_hop_prefix_list(void *rule
, struct prefix
*prefix
,
210 route_map_object_t type
, void *object
)
212 struct prefix_list
*plist
;
213 struct rip_info
*rinfo
;
214 struct prefix_ipv4 p
;
216 if (type
== RMAP_RIP
) {
219 p
.prefix
= (rinfo
->nh
.gate
.ipv4
.s_addr
) ? rinfo
->nh
.gate
.ipv4
221 p
.prefixlen
= IPV4_MAX_BITLEN
;
223 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
227 return (prefix_list_apply(plist
, &p
) == PREFIX_DENY
234 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg
)
236 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
239 static void route_match_ip_next_hop_prefix_list_free(void *rule
)
241 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
244 static struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd
= {
245 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list
,
246 route_match_ip_next_hop_prefix_list_compile
,
247 route_match_ip_next_hop_prefix_list_free
};
249 /* `match ip address IP_ACCESS_LIST' */
251 /* Match function should return 1 if match is success else return
253 static route_map_result_t
route_match_ip_address(void *rule
,
254 struct prefix
*prefix
,
255 route_map_object_t type
,
258 struct access_list
*alist
;
260 if (type
== RMAP_RIP
) {
261 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
265 return (access_list_apply(alist
, prefix
) == FILTER_DENY
272 /* Route map `ip address' match statement. `arg' should be
274 static void *route_match_ip_address_compile(const char *arg
)
276 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
279 /* Free route map's compiled `ip address' value. */
280 static void route_match_ip_address_free(void *rule
)
282 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
285 /* Route map commands for ip address matching. */
286 static struct route_map_rule_cmd route_match_ip_address_cmd
= {
287 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
288 route_match_ip_address_free
};
290 /* `match ip address prefix-list PREFIX_LIST' */
292 static route_map_result_t
293 route_match_ip_address_prefix_list(void *rule
, struct prefix
*prefix
,
294 route_map_object_t type
, void *object
)
296 struct prefix_list
*plist
;
298 if (type
== RMAP_RIP
) {
299 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
303 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
310 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
312 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
315 static void route_match_ip_address_prefix_list_free(void *rule
)
317 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
320 static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
321 "ip address prefix-list", route_match_ip_address_prefix_list
,
322 route_match_ip_address_prefix_list_compile
,
323 route_match_ip_address_prefix_list_free
};
325 /* `match tag TAG' */
326 /* Match function return 1 if match is success else return zero. */
327 static route_map_result_t
route_match_tag(void *rule
, struct prefix
*prefix
,
328 route_map_object_t type
, void *object
)
331 struct rip_info
*rinfo
;
332 route_tag_t rinfo_tag
;
334 if (type
== RMAP_RIP
) {
338 /* The information stored by rinfo is host ordered. */
339 rinfo_tag
= rinfo
->tag
;
340 if (rinfo_tag
== *tag
)
348 /* Route map commands for tag matching. */
349 static struct route_map_rule_cmd route_match_tag_cmd
= {
350 "tag", route_match_tag
, route_map_rule_tag_compile
,
351 route_map_rule_tag_free
,
354 /* `set metric METRIC' */
356 /* Set metric to attribute. */
357 static route_map_result_t
route_set_metric(void *rule
, struct prefix
*prefix
,
358 route_map_object_t type
,
361 if (type
== RMAP_RIP
) {
362 struct rip_metric_modifier
*mod
;
363 struct rip_info
*rinfo
;
371 if (mod
->type
== metric_increment
)
372 rinfo
->metric_out
+= mod
->metric
;
373 else if (mod
->type
== metric_decrement
)
374 rinfo
->metric_out
-= mod
->metric
;
375 else if (mod
->type
== metric_absolute
)
376 rinfo
->metric_out
= mod
->metric
;
378 if ((signed int)rinfo
->metric_out
< 1)
379 rinfo
->metric_out
= 1;
380 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
381 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
383 rinfo
->metric_set
= 1;
388 /* set metric compilation. */
389 static void *route_set_metric_compile(const char *arg
)
395 struct rip_metric_modifier
*mod
;
397 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
398 sizeof(struct rip_metric_modifier
));
407 /* Examine first character. */
409 mod
->type
= metric_increment
;
411 } else if (arg
[0] == '-') {
412 mod
->type
= metric_decrement
;
415 mod
->type
= metric_absolute
;
417 /* Check beginning with digit string. */
418 if (*pnt
< '0' || *pnt
> '9')
421 /* Convert string to integer. */
422 metric
= strtol(pnt
, &endptr
, 10);
424 if (*endptr
!= '\0' || metric
< 0) {
427 if (metric
> RIP_METRIC_INFINITY
) {
429 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
430 __PRETTY_FUNCTION__
, metric
);
431 mod
->metric
= RIP_METRIC_INFINITY
;
433 mod
->metric
= metric
;
440 /* Free route map's compiled `set metric' value. */
441 static void route_set_metric_free(void *rule
)
443 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
446 /* Set metric rule structure. */
447 static struct route_map_rule_cmd route_set_metric_cmd
= {
448 "metric", route_set_metric
, route_set_metric_compile
,
449 route_set_metric_free
,
452 /* `set ip next-hop IP_ADDRESS' */
454 /* Set nexthop to object. ojbect must be pointer to struct attr. */
455 static route_map_result_t
route_set_ip_nexthop(void *rule
,
456 struct prefix
*prefix
,
457 route_map_object_t type
,
460 struct in_addr
*address
;
461 struct rip_info
*rinfo
;
463 if (type
== RMAP_RIP
) {
464 /* Fetch routemap's rule information. */
468 /* Set next hop value. */
469 rinfo
->nexthop_out
= *address
;
475 /* Route map `ip nexthop' compile function. Given string is converted
476 to struct in_addr structure. */
477 static void *route_set_ip_nexthop_compile(const char *arg
)
480 struct in_addr
*address
;
482 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in_addr
));
484 ret
= inet_aton(arg
, address
);
487 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
494 /* Free route map's compiled `ip nexthop' value. */
495 static void route_set_ip_nexthop_free(void *rule
)
497 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
500 /* Route map commands for ip nexthop set. */
501 static struct route_map_rule_cmd route_set_ip_nexthop_cmd
= {
502 "ip next-hop", route_set_ip_nexthop
, route_set_ip_nexthop_compile
,
503 route_set_ip_nexthop_free
};
507 /* Set tag to object. ojbect must be pointer to struct attr. */
508 static route_map_result_t
route_set_tag(void *rule
, struct prefix
*prefix
,
509 route_map_object_t type
, void *object
)
512 struct rip_info
*rinfo
;
514 if (type
== RMAP_RIP
) {
515 /* Fetch routemap's rule information. */
519 /* Set next hop value. */
520 rinfo
->tag_out
= *tag
;
526 /* Route map commands for tag set. */
527 static struct route_map_rule_cmd route_set_tag_cmd
= {
528 "tag", route_set_tag
, route_map_rule_tag_compile
,
529 route_map_rule_tag_free
};
531 #define MATCH_STR "Match values from routing table\n"
532 #define SET_STR "Set values in destination routing protocol\n"
534 void rip_route_map_reset()
540 void rip_route_map_init()
544 route_map_add_hook(rip_route_map_update
);
545 route_map_delete_hook(rip_route_map_update
);
547 route_map_match_interface_hook(generic_match_add
);
548 route_map_no_match_interface_hook(generic_match_delete
);
550 route_map_match_ip_address_hook(generic_match_add
);
551 route_map_no_match_ip_address_hook(generic_match_delete
);
553 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
554 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
556 route_map_match_ip_next_hop_hook(generic_match_add
);
557 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
559 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
560 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
562 route_map_match_metric_hook(generic_match_add
);
563 route_map_no_match_metric_hook(generic_match_delete
);
565 route_map_match_tag_hook(generic_match_add
);
566 route_map_no_match_tag_hook(generic_match_delete
);
568 route_map_set_ip_nexthop_hook(generic_set_add
);
569 route_map_no_set_ip_nexthop_hook(generic_set_delete
);
571 route_map_set_metric_hook(generic_set_add
);
572 route_map_no_set_metric_hook(generic_set_delete
);
574 route_map_set_tag_hook(generic_set_add
);
575 route_map_no_set_tag_hook(generic_set_delete
);
577 route_map_install_match(&route_match_metric_cmd
);
578 route_map_install_match(&route_match_interface_cmd
);
579 route_map_install_match(&route_match_ip_next_hop_cmd
);
580 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
581 route_map_install_match(&route_match_ip_address_cmd
);
582 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
583 route_map_install_match(&route_match_tag_cmd
);
585 route_map_install_set(&route_set_metric_cmd
);
586 route_map_install_set(&route_set_ip_nexthop_cmd
);
587 route_map_install_set(&route_set_tag_cmd
);