]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_routemap.c
*: reindent pt. 2
[mirror_frr.git] / ripngd / ripng_routemap.c
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
36 u_char 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, struct prefix *prefix,
42 route_map_object_t type,
43 void *object)
44 {
45 u_int32_t *metric;
46 struct ripng_info *rinfo;
47
48 if (type == RMAP_RIPNG) {
49 metric = rule;
50 rinfo = object;
51
52 if (rinfo->metric == *metric)
53 return RMAP_MATCH;
54 else
55 return RMAP_NOMATCH;
56 }
57 return RMAP_NOMATCH;
58 }
59
60 /* Route map `match metric' match statement. `arg' is METRIC value */
61 static void *route_match_metric_compile(const char *arg)
62 {
63 u_int32_t *metric;
64
65 metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(u_int32_t));
66 *metric = atoi(arg);
67
68 if (*metric > 0)
69 return metric;
70
71 XFREE(MTYPE_ROUTE_MAP_COMPILED, metric);
72 return NULL;
73 }
74
75 /* Free route map's compiled `match metric' value. */
76 static void route_match_metric_free(void *rule)
77 {
78 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
79 }
80
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};
85
86 /* `match interface IFNAME' */
87 /* Match function return 1 if match is success else return zero. */
88 static route_map_result_t route_match_interface(void *rule,
89 struct prefix *prefix,
90 route_map_object_t type,
91 void *object)
92 {
93 struct ripng_info *rinfo;
94 struct interface *ifp;
95 char *ifname;
96
97 if (type == RMAP_RIPNG) {
98 ifname = rule;
99 ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
100
101 if (!ifp)
102 return RMAP_NOMATCH;
103
104 rinfo = object;
105
106 if (rinfo->ifindex == ifp->ifindex)
107 return RMAP_MATCH;
108 else
109 return RMAP_NOMATCH;
110 }
111 return RMAP_NOMATCH;
112 }
113
114 /* Route map `match interface' match statement. `arg' is IFNAME value */
115 static void *route_match_interface_compile(const char *arg)
116 {
117 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
118 }
119
120 static void route_match_interface_free(void *rule)
121 {
122 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
123 }
124
125 static struct route_map_rule_cmd route_match_interface_cmd = {
126 "interface", route_match_interface, route_match_interface_compile,
127 route_match_interface_free};
128
129 /* `match tag TAG' */
130 /* Match function return 1 if match is success else return zero. */
131 static route_map_result_t route_match_tag(void *rule, struct prefix *prefix,
132 route_map_object_t type, void *object)
133 {
134 route_tag_t *tag;
135 struct ripng_info *rinfo;
136 route_tag_t rinfo_tag;
137
138 if (type == RMAP_RIPNG) {
139 tag = rule;
140 rinfo = object;
141
142 /* The information stored by rinfo is host ordered. */
143 rinfo_tag = rinfo->tag;
144 if (rinfo_tag == *tag)
145 return RMAP_MATCH;
146 else
147 return RMAP_NOMATCH;
148 }
149 return RMAP_NOMATCH;
150 }
151
152 static struct route_map_rule_cmd route_match_tag_cmd = {
153 "tag",
154 route_match_tag,
155 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, struct prefix *prefix,
163 route_map_object_t type,
164 void *object)
165 {
166 if (type == RMAP_RIPNG) {
167 struct rip_metric_modifier *mod;
168 struct ripng_info *rinfo;
169
170 mod = rule;
171 rinfo = object;
172
173 if (mod->type == metric_increment)
174 rinfo->metric_out += mod->metric;
175 else if (mod->type == metric_decrement)
176 rinfo->metric_out -= mod->metric;
177 else if (mod->type == metric_absolute)
178 rinfo->metric_out = mod->metric;
179
180 if (rinfo->metric_out < 1)
181 rinfo->metric_out = 1;
182 if (rinfo->metric_out > RIPNG_METRIC_INFINITY)
183 rinfo->metric_out = RIPNG_METRIC_INFINITY;
184
185 rinfo->metric_set = 1;
186 }
187 return RMAP_OKAY;
188 }
189
190 /* set metric compilation. */
191 static void *route_set_metric_compile(const char *arg)
192 {
193 int len;
194 const char *pnt;
195 int type;
196 long metric;
197 char *endptr = NULL;
198 struct rip_metric_modifier *mod;
199
200 len = strlen(arg);
201 pnt = arg;
202
203 if (len == 0)
204 return NULL;
205
206 /* Examine first character. */
207 if (arg[0] == '+') {
208 type = metric_increment;
209 pnt++;
210 } else if (arg[0] == '-') {
211 type = metric_decrement;
212 pnt++;
213 } else
214 type = metric_absolute;
215
216 /* Check beginning with digit string. */
217 if (*pnt < '0' || *pnt > '9')
218 return NULL;
219
220 /* Convert string to integer. */
221 metric = strtol(pnt, &endptr, 10);
222
223 if (metric == LONG_MAX || *endptr != '\0')
224 return NULL;
225 /* Commented out by Hasso Tepper, to avoid problems in vtysh. */
226 /* if (metric < 0 || metric > RIPNG_METRIC_INFINITY) */
227 if (metric < 0)
228 return NULL;
229
230 mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
231 sizeof(struct rip_metric_modifier));
232 mod->type = type;
233 mod->metric = metric;
234
235 return mod;
236 }
237
238 /* Free route map's compiled `set metric' value. */
239 static void route_set_metric_free(void *rule)
240 {
241 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
242 }
243
244 static struct route_map_rule_cmd route_set_metric_cmd = {
245 "metric",
246 route_set_metric,
247 route_set_metric_compile,
248 route_set_metric_free,
249 };
250
251 /* `set ipv6 next-hop local IP_ADDRESS' */
252
253 /* Set nexthop to object. ojbect must be pointer to struct attr. */
254 static route_map_result_t route_set_ipv6_nexthop_local(void *rule,
255 struct prefix *prefix,
256 route_map_object_t type,
257 void *object)
258 {
259 struct in6_addr *address;
260 struct ripng_info *rinfo;
261
262 if (type == RMAP_RIPNG) {
263 /* Fetch routemap's rule information. */
264 address = rule;
265 rinfo = object;
266
267 /* Set next hop value. */
268 rinfo->nexthop_out = *address;
269 }
270
271 return RMAP_OKAY;
272 }
273
274 /* Route map `ipv6 nexthop local' compile function. Given string is converted
275 to struct in6_addr structure. */
276 static void *route_set_ipv6_nexthop_local_compile(const char *arg)
277 {
278 int ret;
279 struct in6_addr *address;
280
281 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
282
283 ret = inet_pton(AF_INET6, arg, address);
284
285 if (ret == 0) {
286 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
287 return NULL;
288 }
289
290 return address;
291 }
292
293 /* Free route map's compiled `ipv6 nexthop local' value. */
294 static void route_set_ipv6_nexthop_local_free(void *rule)
295 {
296 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
297 }
298
299 /* Route map commands for ipv6 nexthop local set. */
300 static struct route_map_rule_cmd route_set_ipv6_nexthop_local_cmd = {
301 "ipv6 next-hop local", route_set_ipv6_nexthop_local,
302 route_set_ipv6_nexthop_local_compile,
303 route_set_ipv6_nexthop_local_free};
304
305 /* `set tag TAG' */
306
307 /* Set tag to object. ojbect must be pointer to struct attr. */
308 static route_map_result_t route_set_tag(void *rule, struct prefix *prefix,
309 route_map_object_t type, void *object)
310 {
311 route_tag_t *tag;
312 struct ripng_info *rinfo;
313
314 if (type == RMAP_RIPNG) {
315 /* Fetch routemap's rule information. */
316 tag = rule;
317 rinfo = object;
318
319 /* Set next hop value. */
320 rinfo->tag_out = *tag;
321 }
322
323 return RMAP_OKAY;
324 }
325
326 /* Route map commands for tag set. */
327 static struct route_map_rule_cmd route_set_tag_cmd = {
328 "tag", route_set_tag, route_map_rule_tag_compile,
329 route_map_rule_tag_free};
330
331 #define MATCH_STR "Match values from routing table\n"
332 #define SET_STR "Set values in destination routing protocol\n"
333
334 void ripng_route_map_reset()
335 {
336 /* XXX ??? */
337 ;
338 }
339
340 void ripng_route_map_init()
341 {
342 route_map_init();
343
344 route_map_match_interface_hook(generic_match_add);
345 route_map_no_match_interface_hook(generic_match_delete);
346
347 route_map_match_metric_hook(generic_match_add);
348 route_map_no_match_metric_hook(generic_match_delete);
349
350 route_map_match_tag_hook(generic_match_add);
351 route_map_no_match_tag_hook(generic_match_delete);
352
353 route_map_set_ipv6_nexthop_local_hook(generic_set_add);
354 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete);
355
356 route_map_set_metric_hook(generic_set_add);
357 route_map_no_set_metric_hook(generic_set_delete);
358
359 route_map_set_tag_hook(generic_set_add);
360 route_map_no_set_tag_hook(generic_set_delete);
361
362 route_map_install_match(&route_match_metric_cmd);
363 route_map_install_match(&route_match_interface_cmd);
364 route_map_install_match(&route_match_tag_cmd);
365 route_map_install_set(&route_set_metric_cmd);
366 route_map_install_set(&route_set_ipv6_nexthop_local_cmd);
367 route_map_install_set(&route_set_tag_cmd);
368 }