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 route_map_result_t
route_match_metric(void *rule
,
42 const 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(uint32_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 const 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
,
133 const struct prefix
*prefix
,
134 route_map_object_t type
, void *object
)
137 struct ripng_info
*rinfo
;
138 route_tag_t rinfo_tag
;
140 if (type
== RMAP_RIPNG
) {
144 /* The information stored by rinfo is host ordered. */
145 rinfo_tag
= rinfo
->tag
;
146 if (rinfo_tag
== *tag
)
154 static struct route_map_rule_cmd route_match_tag_cmd
= {
155 "tag", route_match_tag
, route_map_rule_tag_compile
,
156 route_map_rule_tag_free
,
159 /* `set metric METRIC' */
161 /* Set metric to attribute. */
162 static route_map_result_t
route_set_metric(void *rule
,
163 const struct prefix
*prefix
,
164 route_map_object_t type
,
167 if (type
== RMAP_RIPNG
) {
168 struct rip_metric_modifier
*mod
;
169 struct ripng_info
*rinfo
;
177 if (mod
->type
== metric_increment
)
178 rinfo
->metric_out
+= mod
->metric
;
179 else if (mod
->type
== metric_decrement
)
180 rinfo
->metric_out
-= mod
->metric
;
181 else if (mod
->type
== metric_absolute
)
182 rinfo
->metric_out
= mod
->metric
;
184 if (rinfo
->metric_out
< 1)
185 rinfo
->metric_out
= 1;
186 if (rinfo
->metric_out
> RIPNG_METRIC_INFINITY
)
187 rinfo
->metric_out
= RIPNG_METRIC_INFINITY
;
189 rinfo
->metric_set
= 1;
194 /* set metric compilation. */
195 static void *route_set_metric_compile(const char *arg
)
201 struct rip_metric_modifier
*mod
;
206 mod
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
,
207 sizeof(struct rip_metric_modifier
));
213 /* Examine first character. */
215 mod
->type
= metric_increment
;
217 } else if (arg
[0] == '-') {
218 mod
->type
= metric_decrement
;
221 mod
->type
= metric_absolute
;
223 /* Check beginning with digit string. */
224 if (*pnt
< '0' || *pnt
> '9')
227 /* Convert string to integer. */
228 metric
= strtol(pnt
, &endptr
, 10);
230 if (*endptr
!= '\0' || metric
< 0)
233 if (metric
> RIPNG_METRIC_INFINITY
) {
234 zlog_info("%s: Metric specified: %ld is being converted into METRIC_INFINITY",
237 mod
->metric
= RIPNG_METRIC_INFINITY
;
239 mod
->metric
= metric
;
245 /* Free route map's compiled `set metric' value. */
246 static void route_set_metric_free(void *rule
)
248 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
251 static struct route_map_rule_cmd route_set_metric_cmd
= {
252 "metric", route_set_metric
, route_set_metric_compile
,
253 route_set_metric_free
,
256 /* `set ipv6 next-hop local IP_ADDRESS' */
258 /* Set nexthop to object. ojbect must be pointer to struct attr. */
259 static route_map_result_t
route_set_ipv6_nexthop_local(void *rule
,
260 const struct prefix
*p
,
261 route_map_object_t type
,
264 struct in6_addr
*address
;
265 struct ripng_info
*rinfo
;
267 if (type
== RMAP_RIPNG
) {
268 /* Fetch routemap's rule information. */
272 /* Set next hop value. */
273 rinfo
->nexthop_out
= *address
;
279 /* Route map `ipv6 nexthop local' compile function. Given string is converted
280 to struct in6_addr structure. */
281 static void *route_set_ipv6_nexthop_local_compile(const char *arg
)
284 struct in6_addr
*address
;
286 address
= XMALLOC(MTYPE_ROUTE_MAP_COMPILED
, sizeof(struct in6_addr
));
288 ret
= inet_pton(AF_INET6
, arg
, address
);
291 XFREE(MTYPE_ROUTE_MAP_COMPILED
, address
);
298 /* Free route map's compiled `ipv6 nexthop local' value. */
299 static void route_set_ipv6_nexthop_local_free(void *rule
)
301 XFREE(MTYPE_ROUTE_MAP_COMPILED
, rule
);
304 /* Route map commands for ipv6 nexthop local set. */
305 static struct route_map_rule_cmd route_set_ipv6_nexthop_local_cmd
= {
306 "ipv6 next-hop local", route_set_ipv6_nexthop_local
,
307 route_set_ipv6_nexthop_local_compile
,
308 route_set_ipv6_nexthop_local_free
};
312 /* Set tag to object. ojbect must be pointer to struct attr. */
313 static route_map_result_t
route_set_tag(void *rule
,
314 const struct prefix
*prefix
,
315 route_map_object_t type
, void *object
)
318 struct ripng_info
*rinfo
;
320 if (type
== RMAP_RIPNG
) {
321 /* Fetch routemap's rule information. */
325 /* Set next hop value. */
326 rinfo
->tag_out
= *tag
;
332 /* Route map commands for tag set. */
333 static struct route_map_rule_cmd route_set_tag_cmd
= {
334 "tag", route_set_tag
, route_map_rule_tag_compile
,
335 route_map_rule_tag_free
};
337 #define MATCH_STR "Match values from routing table\n"
338 #define SET_STR "Set values in destination routing protocol\n"
340 void ripng_route_map_reset()
346 void ripng_route_map_init()
350 route_map_match_interface_hook(generic_match_add
);
351 route_map_no_match_interface_hook(generic_match_delete
);
353 route_map_match_metric_hook(generic_match_add
);
354 route_map_no_match_metric_hook(generic_match_delete
);
356 route_map_match_tag_hook(generic_match_add
);
357 route_map_no_match_tag_hook(generic_match_delete
);
359 route_map_set_ipv6_nexthop_local_hook(generic_set_add
);
360 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete
);
362 route_map_set_metric_hook(generic_set_add
);
363 route_map_no_set_metric_hook(generic_set_delete
);
365 route_map_set_tag_hook(generic_set_add
);
366 route_map_no_set_tag_hook(generic_set_delete
);
368 route_map_install_match(&route_match_metric_cmd
);
369 route_map_install_match(&route_match_interface_cmd
);
370 route_map_install_match(&route_match_tag_cmd
);
371 route_map_install_set(&route_set_metric_cmd
);
372 route_map_install_set(&route_set_ipv6_nexthop_local_cmd
);
373 route_map_install_set(&route_set_tag_cmd
);