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
45 /* `match metric METRIC' */
46 /* Match function return 1 if match is success else return zero. */
47 static route_map_result_t
48 route_match_metric (void *rule
, struct prefix
*prefix
,
49 route_map_object_t type
, void *object
)
52 struct ripng_info
*rinfo
;
54 if (type
== RMAP_RIPNG
)
59 if (rinfo
->metric
== *metric
)
67 /* Route map `match metric' match statement. `arg' is METRIC value */
69 route_match_metric_compile (const char *arg
)
73 metric
= XMALLOC (MTYPE_ROUTE_MAP_COMPILED
, sizeof (u_int32_t
));
79 XFREE (MTYPE_ROUTE_MAP_COMPILED
, metric
);
83 /* Free route map's compiled `match metric' value. */
85 route_match_metric_free (void *rule
)
87 XFREE (MTYPE_ROUTE_MAP_COMPILED
, rule
);
90 /* Route map commands for metric matching. */
91 static struct route_map_rule_cmd route_match_metric_cmd
=
95 route_match_metric_compile
,
96 route_match_metric_free
99 /* `match interface IFNAME' */
100 /* Match function return 1 if match is success else return zero. */
101 static route_map_result_t
102 route_match_interface (void *rule
, struct prefix
*prefix
,
103 route_map_object_t type
, void *object
)
105 struct ripng_info
*rinfo
;
106 struct interface
*ifp
;
109 if (type
== RMAP_RIPNG
)
112 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
119 if (rinfo
->ifindex
== ifp
->ifindex
)
127 /* Route map `match interface' match statement. `arg' is IFNAME value */
129 route_match_interface_compile (const char *arg
)
131 return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED
, arg
);
135 route_match_interface_free (void *rule
)
137 XFREE (MTYPE_ROUTE_MAP_COMPILED
, rule
);
140 static struct route_map_rule_cmd route_match_interface_cmd
=
143 route_match_interface
,
144 route_match_interface_compile
,
145 route_match_interface_free
148 /* `match tag TAG' */
149 /* Match function return 1 if match is success else return zero. */
150 static route_map_result_t
151 route_match_tag (void *rule
, struct prefix
*prefix
,
152 route_map_object_t type
, void *object
)
155 struct ripng_info
*rinfo
;
156 route_tag_t rinfo_tag
;
158 if (type
== RMAP_RIPNG
)
163 /* The information stored by rinfo is host ordered. */
164 rinfo_tag
= rinfo
->tag
;
165 if (rinfo_tag
== *tag
)
173 static struct route_map_rule_cmd route_match_tag_cmd
=
177 route_map_rule_tag_compile
,
178 route_map_rule_tag_free
,
181 /* `set metric METRIC' */
183 /* Set metric to attribute. */
184 static route_map_result_t
185 route_set_metric (void *rule
, struct prefix
*prefix
,
186 route_map_object_t type
, void *object
)
188 if (type
== RMAP_RIPNG
)
190 struct rip_metric_modifier
*mod
;
191 struct ripng_info
*rinfo
;
196 if (mod
->type
== metric_increment
)
197 rinfo
->metric_out
+= mod
->metric
;
198 else if (mod
->type
== metric_decrement
)
199 rinfo
->metric_out
-= mod
->metric
;
200 else if (mod
->type
== metric_absolute
)
201 rinfo
->metric_out
= mod
->metric
;
203 if (rinfo
->metric_out
< 1)
204 rinfo
->metric_out
= 1;
205 if (rinfo
->metric_out
> RIPNG_METRIC_INFINITY
)
206 rinfo
->metric_out
= RIPNG_METRIC_INFINITY
;
208 rinfo
->metric_set
= 1;
213 /* set metric compilation. */
215 route_set_metric_compile (const char *arg
)
222 struct rip_metric_modifier
*mod
;
230 /* Examine first character. */
233 type
= metric_increment
;
236 else if (arg
[0] == '-')
238 type
= metric_decrement
;
242 type
= metric_absolute
;
244 /* Check beginning with digit string. */
245 if (*pnt
< '0' || *pnt
> '9')
248 /* Convert string to integer. */
249 metric
= strtol (pnt
, &endptr
, 10);
251 if (metric
== LONG_MAX
|| *endptr
!= '\0')
253 /* Commented out by Hasso Tepper, to avoid problems in vtysh. */
254 /* if (metric < 0 || metric > RIPNG_METRIC_INFINITY) */
258 mod
= XMALLOC (MTYPE_ROUTE_MAP_COMPILED
,
259 sizeof (struct rip_metric_modifier
));
261 mod
->metric
= metric
;
266 /* Free route map's compiled `set metric' value. */
268 route_set_metric_free (void *rule
)
270 XFREE (MTYPE_ROUTE_MAP_COMPILED
, rule
);
273 static struct route_map_rule_cmd route_set_metric_cmd
=
277 route_set_metric_compile
,
278 route_set_metric_free
,
281 /* `set ipv6 next-hop local IP_ADDRESS' */
283 /* Set nexthop to object. ojbect must be pointer to struct attr. */
284 static route_map_result_t
285 route_set_ipv6_nexthop_local (void *rule
, struct prefix
*prefix
,
286 route_map_object_t type
, void *object
)
288 struct in6_addr
*address
;
289 struct ripng_info
*rinfo
;
291 if(type
== RMAP_RIPNG
)
293 /* Fetch routemap's rule information. */
297 /* Set next hop value. */
298 rinfo
->nexthop_out
= *address
;
304 /* Route map `ipv6 nexthop local' compile function. Given string is converted
305 to struct in6_addr structure. */
307 route_set_ipv6_nexthop_local_compile (const char *arg
)
310 struct in6_addr
*address
;
312 address
= XMALLOC (MTYPE_ROUTE_MAP_COMPILED
, sizeof (struct in6_addr
));
314 ret
= inet_pton (AF_INET6
, arg
, address
);
318 XFREE (MTYPE_ROUTE_MAP_COMPILED
, address
);
325 /* Free route map's compiled `ipv6 nexthop local' value. */
327 route_set_ipv6_nexthop_local_free (void *rule
)
329 XFREE (MTYPE_ROUTE_MAP_COMPILED
, rule
);
332 /* Route map commands for ipv6 nexthop local set. */
333 static struct route_map_rule_cmd route_set_ipv6_nexthop_local_cmd
=
335 "ipv6 next-hop local",
336 route_set_ipv6_nexthop_local
,
337 route_set_ipv6_nexthop_local_compile
,
338 route_set_ipv6_nexthop_local_free
343 /* Set tag to object. ojbect must be pointer to struct attr. */
344 static route_map_result_t
345 route_set_tag (void *rule
, struct prefix
*prefix
,
346 route_map_object_t type
, void *object
)
349 struct ripng_info
*rinfo
;
351 if(type
== RMAP_RIPNG
)
353 /* Fetch routemap's rule information. */
357 /* Set next hop value. */
358 rinfo
->tag_out
= *tag
;
364 /* Route map commands for tag set. */
365 static struct route_map_rule_cmd route_set_tag_cmd
=
369 route_map_rule_tag_compile
,
370 route_map_rule_tag_free
373 #define MATCH_STR "Match values from routing table\n"
374 #define SET_STR "Set values in destination routing protocol\n"
377 ripng_route_map_reset ()
384 ripng_route_map_init ()
388 route_map_match_interface_hook (generic_match_add
);
389 route_map_no_match_interface_hook (generic_match_delete
);
391 route_map_match_metric_hook (generic_match_add
);
392 route_map_no_match_metric_hook (generic_match_delete
);
394 route_map_match_tag_hook (generic_match_add
);
395 route_map_no_match_tag_hook (generic_match_delete
);
397 route_map_set_ipv6_nexthop_local_hook (generic_set_add
);
398 route_map_no_set_ipv6_nexthop_local_hook (generic_set_delete
);
400 route_map_set_metric_hook (generic_set_add
);
401 route_map_no_set_metric_hook (generic_set_delete
);
403 route_map_set_tag_hook (generic_set_add
);
404 route_map_no_set_tag_hook (generic_set_delete
);
406 route_map_install_match (&route_match_metric_cmd
);
407 route_map_install_match (&route_match_interface_cmd
);
408 route_map_install_match (&route_match_tag_cmd
);
409 route_map_install_set (&route_set_metric_cmd
);
410 route_map_install_set (&route_set_ipv6_nexthop_local_cmd
);
411 route_map_install_set (&route_set_tag_cmd
);