]>
Commit | Line | Data |
---|---|---|
1 | /* RIPng routemap. | |
2 | * Copyright (C) 1999 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
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 | |
9 | * later version. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | */ | |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "if.h" | |
24 | #include "memory.h" | |
25 | #include "prefix.h" | |
26 | #include "vty.h" | |
27 | #include "routemap.h" | |
28 | #include "command.h" | |
29 | #include "sockunion.h" | |
30 | ||
31 | #include "ripngd/ripngd.h" | |
32 | ||
33 | struct rip_metric_modifier { | |
34 | enum { metric_increment, metric_decrement, metric_absolute } type; | |
35 | bool used; | |
36 | uint8_t metric; | |
37 | }; | |
38 | ||
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, | |
44 | void *object) | |
45 | { | |
46 | uint32_t *metric; | |
47 | struct ripng_info *rinfo; | |
48 | ||
49 | if (type == RMAP_RIPNG) { | |
50 | metric = rule; | |
51 | rinfo = object; | |
52 | ||
53 | if (rinfo->metric == *metric) | |
54 | return RMAP_MATCH; | |
55 | else | |
56 | return RMAP_NOMATCH; | |
57 | } | |
58 | return RMAP_NOMATCH; | |
59 | } | |
60 | ||
61 | /* Route map `match metric' match statement. `arg' is METRIC value */ | |
62 | static void *route_match_metric_compile(const char *arg) | |
63 | { | |
64 | uint32_t *metric; | |
65 | ||
66 | metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t)); | |
67 | *metric = atoi(arg); | |
68 | ||
69 | if (*metric > 0) | |
70 | return metric; | |
71 | ||
72 | XFREE(MTYPE_ROUTE_MAP_COMPILED, metric); | |
73 | return NULL; | |
74 | } | |
75 | ||
76 | /* Free route map's compiled `match metric' value. */ | |
77 | static void route_match_metric_free(void *rule) | |
78 | { | |
79 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
80 | } | |
81 | ||
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}; | |
86 | ||
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, | |
92 | void *object) | |
93 | { | |
94 | struct ripng_info *rinfo; | |
95 | struct interface *ifp; | |
96 | char *ifname; | |
97 | ||
98 | if (type == RMAP_RIPNG) { | |
99 | ifname = rule; | |
100 | ifp = if_lookup_by_name(ifname, VRF_DEFAULT); | |
101 | ||
102 | if (!ifp) | |
103 | return RMAP_NOMATCH; | |
104 | ||
105 | rinfo = object; | |
106 | ||
107 | if (rinfo->ifindex == ifp->ifindex) | |
108 | return RMAP_MATCH; | |
109 | else | |
110 | return RMAP_NOMATCH; | |
111 | } | |
112 | return RMAP_NOMATCH; | |
113 | } | |
114 | ||
115 | /* Route map `match interface' match statement. `arg' is IFNAME value */ | |
116 | static void *route_match_interface_compile(const char *arg) | |
117 | { | |
118 | return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg); | |
119 | } | |
120 | ||
121 | static void route_match_interface_free(void *rule) | |
122 | { | |
123 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
124 | } | |
125 | ||
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}; | |
129 | ||
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) | |
135 | { | |
136 | route_tag_t *tag; | |
137 | struct ripng_info *rinfo; | |
138 | route_tag_t rinfo_tag; | |
139 | ||
140 | if (type == RMAP_RIPNG) { | |
141 | tag = rule; | |
142 | rinfo = object; | |
143 | ||
144 | /* The information stored by rinfo is host ordered. */ | |
145 | rinfo_tag = rinfo->tag; | |
146 | if (rinfo_tag == *tag) | |
147 | return RMAP_MATCH; | |
148 | else | |
149 | return RMAP_NOMATCH; | |
150 | } | |
151 | return RMAP_NOMATCH; | |
152 | } | |
153 | ||
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, | |
157 | }; | |
158 | ||
159 | /* `set metric METRIC' */ | |
160 | ||
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, | |
165 | void *object) | |
166 | { | |
167 | if (type == RMAP_RIPNG) { | |
168 | struct rip_metric_modifier *mod; | |
169 | struct ripng_info *rinfo; | |
170 | ||
171 | mod = rule; | |
172 | rinfo = object; | |
173 | ||
174 | if (!mod->used) | |
175 | return RMAP_OKAY; | |
176 | ||
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; | |
183 | ||
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; | |
188 | ||
189 | rinfo->metric_set = 1; | |
190 | } | |
191 | return RMAP_OKAY; | |
192 | } | |
193 | ||
194 | /* set metric compilation. */ | |
195 | static void *route_set_metric_compile(const char *arg) | |
196 | { | |
197 | int len; | |
198 | const char *pnt; | |
199 | long metric; | |
200 | char *endptr = NULL; | |
201 | struct rip_metric_modifier *mod; | |
202 | ||
203 | len = strlen(arg); | |
204 | pnt = arg; | |
205 | ||
206 | mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, | |
207 | sizeof(struct rip_metric_modifier)); | |
208 | mod->used = false; | |
209 | ||
210 | if (len == 0) | |
211 | return mod; | |
212 | ||
213 | /* Examine first character. */ | |
214 | if (arg[0] == '+') { | |
215 | mod->type = metric_increment; | |
216 | pnt++; | |
217 | } else if (arg[0] == '-') { | |
218 | mod->type = metric_decrement; | |
219 | pnt++; | |
220 | } else | |
221 | mod->type = metric_absolute; | |
222 | ||
223 | /* Check beginning with digit string. */ | |
224 | if (*pnt < '0' || *pnt > '9') | |
225 | return mod; | |
226 | ||
227 | /* Convert string to integer. */ | |
228 | metric = strtol(pnt, &endptr, 10); | |
229 | ||
230 | if (*endptr != '\0' || metric < 0) | |
231 | return mod; | |
232 | ||
233 | if (metric > RIPNG_METRIC_INFINITY) { | |
234 | zlog_info("%s: Metric specified: %ld is being converted into METRIC_INFINITY", | |
235 | __PRETTY_FUNCTION__, | |
236 | metric); | |
237 | mod->metric = RIPNG_METRIC_INFINITY; | |
238 | } else | |
239 | mod->metric = metric; | |
240 | ||
241 | mod->used = true; | |
242 | return mod; | |
243 | } | |
244 | ||
245 | /* Free route map's compiled `set metric' value. */ | |
246 | static void route_set_metric_free(void *rule) | |
247 | { | |
248 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
249 | } | |
250 | ||
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, | |
254 | }; | |
255 | ||
256 | /* `set ipv6 next-hop local IP_ADDRESS' */ | |
257 | ||
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, | |
262 | void *object) | |
263 | { | |
264 | struct in6_addr *address; | |
265 | struct ripng_info *rinfo; | |
266 | ||
267 | if (type == RMAP_RIPNG) { | |
268 | /* Fetch routemap's rule information. */ | |
269 | address = rule; | |
270 | rinfo = object; | |
271 | ||
272 | /* Set next hop value. */ | |
273 | rinfo->nexthop_out = *address; | |
274 | } | |
275 | ||
276 | return RMAP_OKAY; | |
277 | } | |
278 | ||
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) | |
282 | { | |
283 | int ret; | |
284 | struct in6_addr *address; | |
285 | ||
286 | address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr)); | |
287 | ||
288 | ret = inet_pton(AF_INET6, arg, address); | |
289 | ||
290 | if (ret == 0) { | |
291 | XFREE(MTYPE_ROUTE_MAP_COMPILED, address); | |
292 | return NULL; | |
293 | } | |
294 | ||
295 | return address; | |
296 | } | |
297 | ||
298 | /* Free route map's compiled `ipv6 nexthop local' value. */ | |
299 | static void route_set_ipv6_nexthop_local_free(void *rule) | |
300 | { | |
301 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
302 | } | |
303 | ||
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}; | |
309 | ||
310 | /* `set tag TAG' */ | |
311 | ||
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) | |
316 | { | |
317 | route_tag_t *tag; | |
318 | struct ripng_info *rinfo; | |
319 | ||
320 | if (type == RMAP_RIPNG) { | |
321 | /* Fetch routemap's rule information. */ | |
322 | tag = rule; | |
323 | rinfo = object; | |
324 | ||
325 | /* Set next hop value. */ | |
326 | rinfo->tag_out = *tag; | |
327 | } | |
328 | ||
329 | return RMAP_OKAY; | |
330 | } | |
331 | ||
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}; | |
336 | ||
337 | #define MATCH_STR "Match values from routing table\n" | |
338 | #define SET_STR "Set values in destination routing protocol\n" | |
339 | ||
340 | void ripng_route_map_reset() | |
341 | { | |
342 | /* XXX ??? */ | |
343 | ; | |
344 | } | |
345 | ||
346 | void ripng_route_map_init() | |
347 | { | |
348 | route_map_init(); | |
349 | ||
350 | route_map_match_interface_hook(generic_match_add); | |
351 | route_map_no_match_interface_hook(generic_match_delete); | |
352 | ||
353 | route_map_match_metric_hook(generic_match_add); | |
354 | route_map_no_match_metric_hook(generic_match_delete); | |
355 | ||
356 | route_map_match_tag_hook(generic_match_add); | |
357 | route_map_no_match_tag_hook(generic_match_delete); | |
358 | ||
359 | route_map_set_ipv6_nexthop_local_hook(generic_set_add); | |
360 | route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete); | |
361 | ||
362 | route_map_set_metric_hook(generic_set_add); | |
363 | route_map_no_set_metric_hook(generic_set_delete); | |
364 | ||
365 | route_map_set_tag_hook(generic_set_add); | |
366 | route_map_no_set_tag_hook(generic_set_delete); | |
367 | ||
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); | |
374 | } |