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 struct route_map_rule_cmd route_match_metric_cmd
= {
83 "metric", route_match_metric
, route_match_metric_compile
,
84 route_match_metric_free
};
86 /* `match interface IFNAME' */
87 /* Match function return 1 if match is success else return zero. */
88 static enum route_map_cmd_result_t
89 route_match_interface(void *rule
, const struct prefix
*prefix
,
90 route_map_object_t type
, void *object
)
92 struct ripng_info
*rinfo
;
93 struct interface
*ifp
;
96 if (type
== RMAP_RIPNG
) {
98 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
105 if (rinfo
->ifindex
== ifp
->ifindex
)
113 /* Route map `match interface' match statement. `arg' is IFNAME value */
114 static void *route_match_interface_compile(const char *arg
)
116 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
119 static void route_match_interface_free(void *rule
)
121 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
124 static struct route_map_rule_cmd route_match_interface_cmd
= {
125 "interface", route_match_interface
, route_match_interface_compile
,
126 route_match_interface_free
};
128 /* `match tag TAG' */
129 /* Match function return 1 if match is success else return zero. */
130 static enum route_map_cmd_result_t
route_match_tag(void *rule
,
131 const struct prefix
*prefix
,
132 route_map_object_t type
,
136 struct ripng_info
*rinfo
;
137 route_tag_t rinfo_tag
;
139 if (type
== RMAP_RIPNG
) {
143 /* The information stored by rinfo is host ordered. */
144 rinfo_tag
= rinfo
->tag
;
145 if (rinfo_tag
== *tag
)
153 static struct route_map_rule_cmd route_match_tag_cmd
= {
154 "tag", route_match_tag
, route_map_rule_tag_compile
,
155 route_map_rule_tag_free
,
158 /* `set metric METRIC' */
160 /* Set metric to attribute. */
161 static enum route_map_cmd_result_t
162 route_set_metric(void *rule
, const struct prefix
*prefix
,
163 route_map_object_t type
, void *object
)
165 if (type
== RMAP_RIPNG
) {
166 struct rip_metric_modifier
*mod
;
167 struct ripng_info
*rinfo
;
175 if (mod
->type
== metric_increment
)
176 rinfo
->metric_out
+= mod
->metric
;
177 else if (mod
->type
== metric_decrement
)
178 rinfo
->metric_out
-= mod
->metric
;
179 else if (mod
->type
== metric_absolute
)
180 rinfo
->metric_out
= mod
->metric
;
182 if (rinfo
->metric_out
< 1)
183 rinfo
->metric_out
= 1;
184 if (rinfo
->metric_out
> RIPNG_METRIC_INFINITY
)
185 rinfo
->metric_out
= RIPNG_METRIC_INFINITY
;
187 rinfo
->metric_set
= 1;
192 /* set metric compilation. */
193 static void *route_set_metric_compile(const char *arg
)
199 struct rip_metric_modifier
*mod
;
204 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
205 sizeof(struct rip_metric_modifier
));
211 /* Examine first character. */
213 mod
->type
= metric_increment
;
215 } else if (arg
[0] == '-') {
216 mod
->type
= metric_decrement
;
219 mod
->type
= metric_absolute
;
221 /* Check beginning with digit string. */
222 if (*pnt
< '0' || *pnt
> '9')
225 /* Convert string to integer. */
226 metric
= strtol(pnt
, &endptr
, 10);
228 if (*endptr
!= '\0' || metric
< 0)
231 if (metric
> RIPNG_METRIC_INFINITY
) {
232 zlog_info("%s: Metric specified: %ld is being converted into METRIC_INFINITY",
235 mod
->metric
= RIPNG_METRIC_INFINITY
;
237 mod
->metric
= metric
;
243 /* Free route map's compiled `set metric' value. */
244 static void route_set_metric_free(void *rule
)
246 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
249 static struct route_map_rule_cmd route_set_metric_cmd
= {
250 "metric", route_set_metric
, route_set_metric_compile
,
251 route_set_metric_free
,
254 /* `set ipv6 next-hop local IP_ADDRESS' */
256 /* Set nexthop to object. ojbect must be pointer to struct attr. */
257 static enum route_map_cmd_result_t
258 route_set_ipv6_nexthop_local(void *rule
, const struct prefix
*p
,
259 route_map_object_t type
, void *object
)
261 struct in6_addr
*address
;
262 struct ripng_info
*rinfo
;
264 if (type
== RMAP_RIPNG
) {
265 /* Fetch routemap's rule information. */
269 /* Set next hop value. */
270 rinfo
->nexthop_out
= *address
;
276 /* Route map `ipv6 nexthop local' compile function. Given string is converted
277 to struct in6_addr structure. */
278 static void *route_set_ipv6_nexthop_local_compile(const char *arg
)
281 struct in6_addr
*address
;
283 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in6_addr
));
285 ret
= inet_pton(AF_INET6
, arg
, address
);
288 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
295 /* Free route map's compiled `ipv6 nexthop local' value. */
296 static void route_set_ipv6_nexthop_local_free(void *rule
)
298 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
301 /* Route map commands for ipv6 nexthop local set. */
302 static struct route_map_rule_cmd route_set_ipv6_nexthop_local_cmd
= {
303 "ipv6 next-hop local", route_set_ipv6_nexthop_local
,
304 route_set_ipv6_nexthop_local_compile
,
305 route_set_ipv6_nexthop_local_free
};
309 /* Set tag to object. ojbect must be pointer to struct attr. */
310 static enum route_map_cmd_result_t
311 route_set_tag(void *rule
, const struct prefix
*prefix
, route_map_object_t type
,
315 struct ripng_info
*rinfo
;
317 if (type
== RMAP_RIPNG
) {
318 /* Fetch routemap's rule information. */
322 /* Set next hop value. */
323 rinfo
->tag_out
= *tag
;
329 /* Route map commands for tag set. */
330 static struct route_map_rule_cmd route_set_tag_cmd
= {
331 "tag", route_set_tag
, route_map_rule_tag_compile
,
332 route_map_rule_tag_free
};
334 #define MATCH_STR "Match values from routing table\n"
335 #define SET_STR "Set values in destination routing protocol\n"
337 void ripng_route_map_init(void)
341 route_map_match_interface_hook(generic_match_add
);
342 route_map_no_match_interface_hook(generic_match_delete
);
344 route_map_match_metric_hook(generic_match_add
);
345 route_map_no_match_metric_hook(generic_match_delete
);
347 route_map_match_tag_hook(generic_match_add
);
348 route_map_no_match_tag_hook(generic_match_delete
);
350 route_map_set_ipv6_nexthop_local_hook(generic_set_add
);
351 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete
);
353 route_map_set_metric_hook(generic_set_add
);
354 route_map_no_set_metric_hook(generic_set_delete
);
356 route_map_set_tag_hook(generic_set_add
);
357 route_map_no_set_tag_hook(generic_set_delete
);
359 route_map_install_match(&route_match_metric_cmd
);
360 route_map_install_match(&route_match_interface_cmd
);
361 route_map_install_match(&route_match_tag_cmd
);
362 route_map_install_set(&route_set_metric_cmd
);
363 route_map_install_set(&route_set_ipv6_nexthop_local_cmd
);
364 route_map_install_set(&route_set_tag_cmd
);