2 * Copyright (C) 1999 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "sockunion.h"
31 #include "ripngd/ripngd.h"
33 struct rip_metric_modifier
{
34 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
39 /* `match metric METRIC' */
40 /* Match function return 1 if match is success else return zero. */
41 static enum route_map_cmd_result_t
42 route_match_metric(void *rule
, const struct prefix
*prefix
, void *object
)
45 struct ripng_info
*rinfo
;
50 if (rinfo
->metric
== *metric
)
58 /* Route map `match metric' match statement. `arg' is METRIC value */
59 static void *route_match_metric_compile(const char *arg
)
63 metric
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
69 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric
);
73 /* Free route map's compiled `match metric' value. */
74 static void route_match_metric_free(void *rule
)
76 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
79 /* Route map commands for metric matching. */
80 static const struct route_map_rule_cmd route_match_metric_cmd
= {
83 route_match_metric_compile
,
84 route_match_metric_free
87 /* `match interface IFNAME' */
88 /* Match function return 1 if match is success else return zero. */
89 static enum route_map_cmd_result_t
90 route_match_interface(void *rule
, const struct prefix
*prefix
, void *object
)
92 struct ripng_info
*rinfo
;
93 struct interface
*ifp
;
97 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
104 if (rinfo
->ifindex
== ifp
->ifindex
)
112 /* Route map `match interface' match statement. `arg' is IFNAME value */
113 static void *route_match_interface_compile(const char *arg
)
115 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
118 static void route_match_interface_free(void *rule
)
120 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
123 static const struct route_map_rule_cmd route_match_interface_cmd
= {
125 route_match_interface
,
126 route_match_interface_compile
,
127 route_match_interface_free
130 /* `match tag TAG' */
131 /* Match function return 1 if match is success else return zero. */
132 static enum route_map_cmd_result_t
route_match_tag(void *rule
,
133 const struct prefix
*prefix
,
137 struct ripng_info
*rinfo
;
138 route_tag_t rinfo_tag
;
143 /* The information stored by rinfo is host ordered. */
144 rinfo_tag
= rinfo
->tag
;
145 if (rinfo_tag
== *tag
)
152 static const struct route_map_rule_cmd route_match_tag_cmd
= {
155 route_map_rule_tag_compile
,
156 route_map_rule_tag_free
,
159 /* `set metric METRIC' */
161 /* Set metric to attribute. */
162 static enum route_map_cmd_result_t
163 route_set_metric(void *rule
, const struct prefix
*prefix
, void *object
)
165 struct rip_metric_modifier
*mod
;
166 struct ripng_info
*rinfo
;
174 if (mod
->type
== metric_increment
)
175 rinfo
->metric_out
+= mod
->metric
;
176 else if (mod
->type
== metric_decrement
)
177 rinfo
->metric_out
-= mod
->metric
;
178 else if (mod
->type
== metric_absolute
)
179 rinfo
->metric_out
= mod
->metric
;
181 if (rinfo
->metric_out
< 1)
182 rinfo
->metric_out
= 1;
183 if (rinfo
->metric_out
> RIPNG_METRIC_INFINITY
)
184 rinfo
->metric_out
= RIPNG_METRIC_INFINITY
;
186 rinfo
->metric_set
= 1;
191 /* set metric compilation. */
192 static void *route_set_metric_compile(const char *arg
)
198 struct rip_metric_modifier
*mod
;
203 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
204 sizeof(struct rip_metric_modifier
));
210 /* Examine first character. */
212 mod
->type
= metric_increment
;
214 } else if (arg
[0] == '-') {
215 mod
->type
= metric_decrement
;
218 mod
->type
= metric_absolute
;
220 /* Check beginning with digit string. */
221 if (*pnt
< '0' || *pnt
> '9')
224 /* Convert string to integer. */
225 metric
= strtol(pnt
, &endptr
, 10);
227 if (*endptr
!= '\0' || metric
< 0)
230 if (metric
> RIPNG_METRIC_INFINITY
) {
232 "%s: Metric specified: %ld is being converted into METRIC_INFINITY",
234 mod
->metric
= RIPNG_METRIC_INFINITY
;
236 mod
->metric
= metric
;
242 /* Free route map's compiled `set metric' value. */
243 static void route_set_metric_free(void *rule
)
245 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
248 static const struct route_map_rule_cmd route_set_metric_cmd
= {
251 route_set_metric_compile
,
252 route_set_metric_free
,
255 /* `set ipv6 next-hop local IP_ADDRESS' */
257 /* Set nexthop to object. ojbect must be pointer to struct attr. */
258 static enum route_map_cmd_result_t
259 route_set_ipv6_nexthop_local(void *rule
, const struct prefix
*p
, void *object
)
261 struct in6_addr
*address
;
262 struct ripng_info
*rinfo
;
264 /* Fetch routemap's rule information. */
268 /* Set next hop value. */
269 rinfo
->nexthop_out
= *address
;
274 /* Route map `ipv6 nexthop local' compile function. Given string is converted
275 to struct in6_addr structure. */
276 static void *route_set_ipv6_nexthop_local_compile(const char *arg
)
279 struct in6_addr
*address
;
281 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in6_addr
));
283 ret
= inet_pton(AF_INET6
, arg
, address
);
286 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
293 /* Free route map's compiled `ipv6 nexthop local' value. */
294 static void route_set_ipv6_nexthop_local_free(void *rule
)
296 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
299 /* Route map commands for ipv6 nexthop local set. */
300 static const struct route_map_rule_cmd
301 route_set_ipv6_nexthop_local_cmd
= {
302 "ipv6 next-hop local",
303 route_set_ipv6_nexthop_local
,
304 route_set_ipv6_nexthop_local_compile
,
305 route_set_ipv6_nexthop_local_free
310 /* Set tag to object. ojbect must be pointer to struct attr. */
311 static enum route_map_cmd_result_t
312 route_set_tag(void *rule
, const struct prefix
*prefix
, void *object
)
315 struct ripng_info
*rinfo
;
317 /* Fetch routemap's rule information. */
321 /* Set next hop value. */
322 rinfo
->tag_out
= *tag
;
327 /* Route map commands for tag set. */
328 static const struct route_map_rule_cmd route_set_tag_cmd
= {
331 route_map_rule_tag_compile
,
332 route_map_rule_tag_free
335 #define MATCH_STR "Match values from routing table\n"
336 #define SET_STR "Set values in destination routing protocol\n"
338 void ripng_route_map_init(void)
342 route_map_match_interface_hook(generic_match_add
);
343 route_map_no_match_interface_hook(generic_match_delete
);
345 route_map_match_metric_hook(generic_match_add
);
346 route_map_no_match_metric_hook(generic_match_delete
);
348 route_map_match_tag_hook(generic_match_add
);
349 route_map_no_match_tag_hook(generic_match_delete
);
351 route_map_set_ipv6_nexthop_local_hook(generic_set_add
);
352 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete
);
354 route_map_set_metric_hook(generic_set_add
);
355 route_map_no_set_metric_hook(generic_set_delete
);
357 route_map_set_tag_hook(generic_set_add
);
358 route_map_no_set_tag_hook(generic_set_delete
);
360 route_map_install_match(&route_match_metric_cmd
);
361 route_map_install_match(&route_match_interface_cmd
);
362 route_map_install_match(&route_match_tag_cmd
);
363 route_map_install_set(&route_set_metric_cmd
);
364 route_map_install_set(&route_set_ipv6_nexthop_local_cmd
);
365 route_map_install_set(&route_set_tag_cmd
);