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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
30 #include "sockunion.h"
32 #include "ripngd/ripngd.h"
34 struct rip_metric_modifier
{
35 enum { metric_increment
, metric_decrement
, metric_absolute
} type
;
40 /* `match metric METRIC' */
41 /* Match function return 1 if match is success else return zero. */
42 static route_map_result_t
route_match_metric(void *rule
, struct prefix
*prefix
,
43 route_map_object_t type
,
47 struct ripng_info
*rinfo
;
49 if (type
== RMAP_RIPNG
) {
53 if (rinfo
->metric
== *metric
)
61 /* Route map `match metric' match statement. `arg' is METRIC value */
62 static void *route_match_metric_compile(const char *arg
)
66 metric
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(u_int32_t
));
72 XFREE(MTYPE_ROUTE_MAP_COMPILED
, metric
);
76 /* Free route map's compiled `match metric' value. */
77 static void route_match_metric_free(void *rule
)
79 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
82 /* Route map commands for metric matching. */
83 static struct route_map_rule_cmd route_match_metric_cmd
= {
84 "metric", route_match_metric
, route_match_metric_compile
,
85 route_match_metric_free
};
87 /* `match interface IFNAME' */
88 /* Match function return 1 if match is success else return zero. */
89 static route_map_result_t
route_match_interface(void *rule
,
90 struct prefix
*prefix
,
91 route_map_object_t type
,
94 struct ripng_info
*rinfo
;
95 struct interface
*ifp
;
98 if (type
== RMAP_RIPNG
) {
100 ifp
= if_lookup_by_name(ifname
, VRF_DEFAULT
);
107 if (rinfo
->ifindex
== ifp
->ifindex
)
115 /* Route map `match interface' match statement. `arg' is IFNAME value */
116 static void *route_match_interface_compile(const char *arg
)
118 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED
, arg
);
121 static void route_match_interface_free(void *rule
)
123 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
126 static struct route_map_rule_cmd route_match_interface_cmd
= {
127 "interface", route_match_interface
, route_match_interface_compile
,
128 route_match_interface_free
};
130 /* `match tag TAG' */
131 /* Match function return 1 if match is success else return zero. */
132 static route_map_result_t
route_match_tag(void *rule
, struct prefix
*prefix
,
133 route_map_object_t type
, void *object
)
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 route_map_result_t
route_set_metric(void *rule
, struct prefix
*prefix
,
162 route_map_object_t type
,
165 if (type
== RMAP_RIPNG
) {
166 struct rip_metric_modifier
*mod
;
167 struct ripng_info
*rinfo
;
172 if (mod
->type
== metric_increment
)
173 rinfo
->metric_out
+= mod
->metric
;
174 else if (mod
->type
== metric_decrement
)
175 rinfo
->metric_out
-= mod
->metric
;
176 else if (mod
->type
== metric_absolute
)
177 rinfo
->metric_out
= mod
->metric
;
179 if (rinfo
->metric_out
< 1)
180 rinfo
->metric_out
= 1;
181 if (rinfo
->metric_out
> RIPNG_METRIC_INFINITY
)
182 rinfo
->metric_out
= RIPNG_METRIC_INFINITY
;
184 rinfo
->metric_set
= 1;
189 /* set metric compilation. */
190 static void *route_set_metric_compile(const char *arg
)
197 struct rip_metric_modifier
*mod
;
205 /* Examine first character. */
207 type
= metric_increment
;
209 } else if (arg
[0] == '-') {
210 type
= metric_decrement
;
213 type
= metric_absolute
;
215 /* Check beginning with digit string. */
216 if (*pnt
< '0' || *pnt
> '9')
219 /* Convert string to integer. */
220 metric
= strtol(pnt
, &endptr
, 10);
222 if (metric
== LONG_MAX
|| *endptr
!= '\0')
224 /* Commented out by Hasso Tepper, to avoid problems in vtysh. */
225 /* if (metric < 0 || metric > RIPNG_METRIC_INFINITY) */
229 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
230 sizeof(struct rip_metric_modifier
));
232 mod
->metric
= metric
;
237 /* Free route map's compiled `set metric' value. */
238 static void route_set_metric_free(void *rule
)
240 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
243 static struct route_map_rule_cmd route_set_metric_cmd
= {
244 "metric", route_set_metric
, route_set_metric_compile
,
245 route_set_metric_free
,
248 /* `set ipv6 next-hop local IP_ADDRESS' */
250 /* Set nexthop to object. ojbect must be pointer to struct attr. */
251 static route_map_result_t
route_set_ipv6_nexthop_local(void *rule
,
252 struct prefix
*prefix
,
253 route_map_object_t type
,
256 struct in6_addr
*address
;
257 struct ripng_info
*rinfo
;
259 if (type
== RMAP_RIPNG
) {
260 /* Fetch routemap's rule information. */
264 /* Set next hop value. */
265 rinfo
->nexthop_out
= *address
;
271 /* Route map `ipv6 nexthop local' compile function. Given string is converted
272 to struct in6_addr structure. */
273 static void *route_set_ipv6_nexthop_local_compile(const char *arg
)
276 struct in6_addr
*address
;
278 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in6_addr
));
280 ret
= inet_pton(AF_INET6
, arg
, address
);
283 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
290 /* Free route map's compiled `ipv6 nexthop local' value. */
291 static void route_set_ipv6_nexthop_local_free(void *rule
)
293 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
296 /* Route map commands for ipv6 nexthop local set. */
297 static struct route_map_rule_cmd route_set_ipv6_nexthop_local_cmd
= {
298 "ipv6 next-hop local", route_set_ipv6_nexthop_local
,
299 route_set_ipv6_nexthop_local_compile
,
300 route_set_ipv6_nexthop_local_free
};
304 /* Set tag to object. ojbect must be pointer to struct attr. */
305 static route_map_result_t
route_set_tag(void *rule
, struct prefix
*prefix
,
306 route_map_object_t type
, void *object
)
309 struct ripng_info
*rinfo
;
311 if (type
== RMAP_RIPNG
) {
312 /* Fetch routemap's rule information. */
316 /* Set next hop value. */
317 rinfo
->tag_out
= *tag
;
323 /* Route map commands for tag set. */
324 static struct route_map_rule_cmd route_set_tag_cmd
= {
325 "tag", route_set_tag
, route_map_rule_tag_compile
,
326 route_map_rule_tag_free
};
328 #define MATCH_STR "Match values from routing table\n"
329 #define SET_STR "Set values in destination routing protocol\n"
331 void ripng_route_map_reset()
337 void ripng_route_map_init()
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
);