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 address IP_ACCESS_LIST' */
236 /* Match function should return 1 if match is success else return
238 static route_map_result_t
route_match_ip_address(void *rule
,
239 const struct prefix
*prefix
,
240 route_map_object_t type
,
243 struct access_list
*alist
;
245 if (type
== RMAP_RIP
) {
246 alist
= access_list_lookup(AFI_IP
, (char *)rule
);
250 return (access_list_apply(alist
, prefix
) == FILTER_DENY
257 /* Route map `ip address' match statement. `arg' should be
259 static void *route_match_ip_address_compile(const char *arg
)
261 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
264 /* Free route map's compiled `ip address' value. */
265 static void route_match_ip_address_free(void *rule
)
267 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
270 /* Route map commands for ip address matching. */
271 static struct route_map_rule_cmd route_match_ip_address_cmd
= {
272 "ip address", route_match_ip_address
, route_match_ip_address_compile
,
273 route_match_ip_address_free
};
275 /* `match ip address prefix-list PREFIX_LIST' */
277 static route_map_result_t
278 route_match_ip_address_prefix_list(void *rule
, const struct prefix
*prefix
,
279 route_map_object_t type
, void *object
)
281 struct prefix_list
*plist
;
283 if (type
== RMAP_RIP
) {
284 plist
= prefix_list_lookup(AFI_IP
, (char *)rule
);
288 return (prefix_list_apply(plist
, prefix
) == PREFIX_DENY
295 static void *route_match_ip_address_prefix_list_compile(const char *arg
)
297 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
300 static void route_match_ip_address_prefix_list_free(void *rule
)
302 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
305 static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd
= {
306 "ip address prefix-list", route_match_ip_address_prefix_list
,
307 route_match_ip_address_prefix_list_compile
,
308 route_match_ip_address_prefix_list_free
};
310 /* `match tag TAG' */
311 /* Match function return 1 if match is success else return zero. */
312 static route_map_result_t
route_match_tag(void *rule
, const struct prefix
*p
,
313 route_map_object_t type
, void *object
)
316 struct rip_info
*rinfo
;
317 route_tag_t rinfo_tag
;
319 if (type
== RMAP_RIP
) {
323 /* The information stored by rinfo is host ordered. */
324 rinfo_tag
= rinfo
->tag
;
325 if (rinfo_tag
== *tag
)
333 /* Route map commands for tag matching. */
334 static struct route_map_rule_cmd route_match_tag_cmd
= {
335 "tag", route_match_tag
, route_map_rule_tag_compile
,
336 route_map_rule_tag_free
,
339 /* `set metric METRIC' */
341 /* Set metric to attribute. */
342 static route_map_result_t
route_set_metric(void *rule
,
343 const struct prefix
*prefix
,
344 route_map_object_t type
,
347 if (type
== RMAP_RIP
) {
348 struct rip_metric_modifier
*mod
;
349 struct rip_info
*rinfo
;
357 if (mod
->type
== metric_increment
)
358 rinfo
->metric_out
+= mod
->metric
;
359 else if (mod
->type
== metric_decrement
)
360 rinfo
->metric_out
-= mod
->metric
;
361 else if (mod
->type
== metric_absolute
)
362 rinfo
->metric_out
= mod
->metric
;
364 if ((signed int)rinfo
->metric_out
< 1)
365 rinfo
->metric_out
= 1;
366 if (rinfo
->metric_out
> RIP_METRIC_INFINITY
)
367 rinfo
->metric_out
= RIP_METRIC_INFINITY
;
369 rinfo
->metric_set
= 1;
374 /* set metric compilation. */
375 static void *route_set_metric_compile(const char *arg
)
381 struct rip_metric_modifier
*mod
;
383 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
384 sizeof(struct rip_metric_modifier
));
393 /* Examine first character. */
395 mod
->type
= metric_increment
;
397 } else if (arg
[0] == '-') {
398 mod
->type
= metric_decrement
;
401 mod
->type
= metric_absolute
;
403 /* Check beginning with digit string. */
404 if (*pnt
< '0' || *pnt
> '9')
407 /* Convert string to integer. */
408 metric
= strtol(pnt
, &endptr
, 10);
410 if (*endptr
!= '\0' || metric
< 0) {
413 if (metric
> RIP_METRIC_INFINITY
) {
415 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
416 __PRETTY_FUNCTION__
, metric
);
417 mod
->metric
= RIP_METRIC_INFINITY
;
419 mod
->metric
= metric
;
426 /* Free route map's compiled `set metric' value. */
427 static void route_set_metric_free(void *rule
)
429 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
432 /* Set metric rule structure. */
433 static struct route_map_rule_cmd route_set_metric_cmd
= {
434 "metric", route_set_metric
, route_set_metric_compile
,
435 route_set_metric_free
,
438 /* `set ip next-hop IP_ADDRESS' */
440 /* Set nexthop to object. ojbect must be pointer to struct attr. */
441 static route_map_result_t
route_set_ip_nexthop(void *rule
,
442 const struct prefix
*prefix
,
443 route_map_object_t type
,
446 struct in_addr
*address
;
447 struct rip_info
*rinfo
;
449 if (type
== RMAP_RIP
) {
450 /* Fetch routemap's rule information. */
454 /* Set next hop value. */
455 rinfo
->nexthop_out
= *address
;
461 /* Route map `ip nexthop' compile function. Given string is converted
462 to struct in_addr structure. */
463 static void *route_set_ip_nexthop_compile(const char *arg
)
466 struct in_addr
*address
;
468 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in_addr
));
470 ret
= inet_aton(arg
, address
);
473 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
480 /* Free route map's compiled `ip nexthop' value. */
481 static void route_set_ip_nexthop_free(void *rule
)
483 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
486 /* Route map commands for ip nexthop set. */
487 static struct route_map_rule_cmd route_set_ip_nexthop_cmd
= {
488 "ip next-hop", route_set_ip_nexthop
, route_set_ip_nexthop_compile
,
489 route_set_ip_nexthop_free
};
493 /* Set tag to object. ojbect must be pointer to struct attr. */
494 static route_map_result_t
route_set_tag(void *rule
, const struct prefix
*prefix
,
495 route_map_object_t type
, void *object
)
498 struct rip_info
*rinfo
;
500 if (type
== RMAP_RIP
) {
501 /* Fetch routemap's rule information. */
505 /* Set next hop value. */
506 rinfo
->tag_out
= *tag
;
512 /* Route map commands for tag set. */
513 static struct route_map_rule_cmd route_set_tag_cmd
= {
514 "tag", route_set_tag
, route_map_rule_tag_compile
,
515 route_map_rule_tag_free
};
517 #define MATCH_STR "Match values from routing table\n"
518 #define SET_STR "Set values in destination routing protocol\n"
521 void rip_route_map_init(void)
525 route_map_match_interface_hook(generic_match_add
);
526 route_map_no_match_interface_hook(generic_match_delete
);
528 route_map_match_ip_address_hook(generic_match_add
);
529 route_map_no_match_ip_address_hook(generic_match_delete
);
531 route_map_match_ip_address_prefix_list_hook(generic_match_add
);
532 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete
);
534 route_map_match_ip_next_hop_hook(generic_match_add
);
535 route_map_no_match_ip_next_hop_hook(generic_match_delete
);
537 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add
);
538 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete
);
540 route_map_match_metric_hook(generic_match_add
);
541 route_map_no_match_metric_hook(generic_match_delete
);
543 route_map_match_tag_hook(generic_match_add
);
544 route_map_no_match_tag_hook(generic_match_delete
);
546 route_map_set_ip_nexthop_hook(generic_set_add
);
547 route_map_no_set_ip_nexthop_hook(generic_set_delete
);
549 route_map_set_metric_hook(generic_set_add
);
550 route_map_no_set_metric_hook(generic_set_delete
);
552 route_map_set_tag_hook(generic_set_add
);
553 route_map_no_set_tag_hook(generic_set_delete
);
555 route_map_install_match(&route_match_metric_cmd
);
556 route_map_install_match(&route_match_interface_cmd
);
557 route_map_install_match(&route_match_ip_next_hop_cmd
);
558 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd
);
559 route_map_install_match(&route_match_ip_address_cmd
);
560 route_map_install_match(&route_match_ip_address_prefix_list_cmd
);
561 route_map_install_match(&route_match_tag_cmd
);
563 route_map_install_set(&route_set_metric_cmd
);
564 route_map_install_set(&route_set_ip_nexthop_cmd
);
565 route_map_install_set(&route_set_tag_cmd
);