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
,
43 route_map_object_t type
, void *object
)
46 struct ripng_info
*rinfo
;
48 if (type
== RMAP_RIPNG
) {
52 if (rinfo
->metric
== *metric
)
60 /* Route map `match metric' match statement. `arg' is METRIC value */
61 static void *route_match_metric_compile(const char *arg
)
65 metric
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(uint32_t));
71 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric
);
75 /* Free route map's compiled `match metric' value. */
76 static void route_match_metric_free(void *rule
)
78 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
81 /* Route map commands for metric matching. */
82 static const struct route_map_rule_cmd route_match_metric_cmd
= {
85 route_match_metric_compile
,
86 route_match_metric_free
89 /* `match interface IFNAME' */
90 /* Match function return 1 if match is success else return zero. */
91 static enum route_map_cmd_result_t
92 route_match_interface(void *rule
, const struct prefix
*prefix
,
93 route_map_object_t type
, void *object
)
95 struct ripng_info
*rinfo
;
96 struct interface
*ifp
;
99 if (type
== RMAP_RIPNG
) {
101 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
108 if (rinfo
->ifindex
== ifp
->ifindex
)
116 /* Route map `match interface' match statement. `arg' is IFNAME value */
117 static void *route_match_interface_compile(const char *arg
)
119 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
122 static void route_match_interface_free(void *rule
)
124 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
127 static const struct route_map_rule_cmd route_match_interface_cmd
= {
129 route_match_interface
,
130 route_match_interface_compile
,
131 route_match_interface_free
134 /* `match tag TAG' */
135 /* Match function return 1 if match is success else return zero. */
136 static enum route_map_cmd_result_t
route_match_tag(void *rule
,
137 const struct prefix
*prefix
,
138 route_map_object_t type
,
142 struct ripng_info
*rinfo
;
143 route_tag_t rinfo_tag
;
145 if (type
== RMAP_RIPNG
) {
149 /* The information stored by rinfo is host ordered. */
150 rinfo_tag
= rinfo
->tag
;
151 if (rinfo_tag
== *tag
)
159 static const struct route_map_rule_cmd route_match_tag_cmd
= {
162 route_map_rule_tag_compile
,
163 route_map_rule_tag_free
,
166 /* `set metric METRIC' */
168 /* Set metric to attribute. */
169 static enum route_map_cmd_result_t
170 route_set_metric(void *rule
, const struct prefix
*prefix
,
171 route_map_object_t type
, void *object
)
173 if (type
== RMAP_RIPNG
) {
174 struct rip_metric_modifier
*mod
;
175 struct ripng_info
*rinfo
;
183 if (mod
->type
== metric_increment
)
184 rinfo
->metric_out
+= mod
->metric
;
185 else if (mod
->type
== metric_decrement
)
186 rinfo
->metric_out
-= mod
->metric
;
187 else if (mod
->type
== metric_absolute
)
188 rinfo
->metric_out
= mod
->metric
;
190 if (rinfo
->metric_out
< 1)
191 rinfo
->metric_out
= 1;
192 if (rinfo
->metric_out
> RIPNG_METRIC_INFINITY
)
193 rinfo
->metric_out
= RIPNG_METRIC_INFINITY
;
195 rinfo
->metric_set
= 1;
200 /* set metric compilation. */
201 static void *route_set_metric_compile(const char *arg
)
207 struct rip_metric_modifier
*mod
;
212 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
213 sizeof(struct rip_metric_modifier
));
219 /* Examine first character. */
221 mod
->type
= metric_increment
;
223 } else if (arg
[0] == '-') {
224 mod
->type
= metric_decrement
;
227 mod
->type
= metric_absolute
;
229 /* Check beginning with digit string. */
230 if (*pnt
< '0' || *pnt
> '9')
233 /* Convert string to integer. */
234 metric
= strtol(pnt
, &endptr
, 10);
236 if (*endptr
!= '\0' || metric
< 0)
239 if (metric
> RIPNG_METRIC_INFINITY
) {
241 "%s: Metric specified: %ld is being converted into METRIC_INFINITY",
243 mod
->metric
= RIPNG_METRIC_INFINITY
;
245 mod
->metric
= metric
;
251 /* Free route map's compiled `set metric' value. */
252 static void route_set_metric_free(void *rule
)
254 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
257 static const struct route_map_rule_cmd route_set_metric_cmd
= {
260 route_set_metric_compile
,
261 route_set_metric_free
,
264 /* `set ipv6 next-hop local IP_ADDRESS' */
266 /* Set nexthop to object. ojbect must be pointer to struct attr. */
267 static enum route_map_cmd_result_t
268 route_set_ipv6_nexthop_local(void *rule
, const struct prefix
*p
,
269 route_map_object_t type
, void *object
)
271 struct in6_addr
*address
;
272 struct ripng_info
*rinfo
;
274 if (type
== RMAP_RIPNG
) {
275 /* Fetch routemap's rule information. */
279 /* Set next hop value. */
280 rinfo
->nexthop_out
= *address
;
286 /* Route map `ipv6 nexthop local' compile function. Given string is converted
287 to struct in6_addr structure. */
288 static void *route_set_ipv6_nexthop_local_compile(const char *arg
)
291 struct in6_addr
*address
;
293 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in6_addr
));
295 ret
= inet_pton(AF_INET6
, arg
, address
);
298 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
305 /* Free route map's compiled `ipv6 nexthop local' value. */
306 static void route_set_ipv6_nexthop_local_free(void *rule
)
308 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
311 /* Route map commands for ipv6 nexthop local set. */
312 static const struct route_map_rule_cmd
313 route_set_ipv6_nexthop_local_cmd
= {
314 "ipv6 next-hop local",
315 route_set_ipv6_nexthop_local
,
316 route_set_ipv6_nexthop_local_compile
,
317 route_set_ipv6_nexthop_local_free
322 /* Set tag to object. ojbect must be pointer to struct attr. */
323 static enum route_map_cmd_result_t
324 route_set_tag(void *rule
, const struct prefix
*prefix
, route_map_object_t type
,
328 struct ripng_info
*rinfo
;
330 if (type
== RMAP_RIPNG
) {
331 /* Fetch routemap's rule information. */
335 /* Set next hop value. */
336 rinfo
->tag_out
= *tag
;
342 /* Route map commands for tag set. */
343 static const struct route_map_rule_cmd route_set_tag_cmd
= {
346 route_map_rule_tag_compile
,
347 route_map_rule_tag_free
350 #define MATCH_STR "Match values from routing table\n"
351 #define SET_STR "Set values in destination routing protocol\n"
353 void ripng_route_map_init(void)
357 route_map_match_interface_hook(generic_match_add
);
358 route_map_no_match_interface_hook(generic_match_delete
);
360 route_map_match_metric_hook(generic_match_add
);
361 route_map_no_match_metric_hook(generic_match_delete
);
363 route_map_match_tag_hook(generic_match_add
);
364 route_map_no_match_tag_hook(generic_match_delete
);
366 route_map_set_ipv6_nexthop_local_hook(generic_set_add
);
367 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete
);
369 route_map_set_metric_hook(generic_set_add
);
370 route_map_no_set_metric_hook(generic_set_delete
);
372 route_map_set_tag_hook(generic_set_add
);
373 route_map_no_set_tag_hook(generic_set_delete
);
375 route_map_install_match(&route_match_metric_cmd
);
376 route_map_install_match(&route_match_interface_cmd
);
377 route_map_install_match(&route_match_tag_cmd
);
378 route_map_install_set(&route_set_metric_cmd
);
379 route_map_install_set(&route_set_ipv6_nexthop_local_cmd
);
380 route_map_install_set(&route_set_tag_cmd
);