]> git.proxmox.com Git - mirror_frr.git/blob - ripngd/ripng_routemap.c
*: reindent
[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
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
19 * 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23
24 #include "if.h"
25 #include "memory.h"
26 #include "prefix.h"
27 #include "vty.h"
28 #include "routemap.h"
29 #include "command.h"
30 #include "sockunion.h"
31
32 #include "ripngd/ripngd.h"
33
34 struct rip_metric_modifier {
35 enum { metric_increment, metric_decrement, metric_absolute } type;
36
37 u_char metric;
38 };
39
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,
44 void *object)
45 {
46 u_int32_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 u_int32_t *metric;
65
66 metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(u_int32_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 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, struct prefix *prefix,
133 route_map_object_t type, void *object)
134 {
135 route_tag_t *tag;
136 struct ripng_info *rinfo;
137 route_tag_t rinfo_tag;
138
139 if (type == RMAP_RIPNG) {
140 tag = rule;
141 rinfo = object;
142
143 /* The information stored by rinfo is host ordered. */
144 rinfo_tag = rinfo->tag;
145 if (rinfo_tag == *tag)
146 return RMAP_MATCH;
147 else
148 return RMAP_NOMATCH;
149 }
150 return RMAP_NOMATCH;
151 }
152
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,
156 };
157
158 /* `set metric METRIC' */
159
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,
163 void *object)
164 {
165 if (type == RMAP_RIPNG) {
166 struct rip_metric_modifier *mod;
167 struct ripng_info *rinfo;
168
169 mod = rule;
170 rinfo = object;
171
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;
178
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;
183
184 rinfo->metric_set = 1;
185 }
186 return RMAP_OKAY;
187 }
188
189 /* set metric compilation. */
190 static void *route_set_metric_compile(const char *arg)
191 {
192 int len;
193 const char *pnt;
194 int type;
195 long metric;
196 char *endptr = NULL;
197 struct rip_metric_modifier *mod;
198
199 len = strlen(arg);
200 pnt = arg;
201
202 if (len == 0)
203 return NULL;
204
205 /* Examine first character. */
206 if (arg[0] == '+') {
207 type = metric_increment;
208 pnt++;
209 } else if (arg[0] == '-') {
210 type = metric_decrement;
211 pnt++;
212 } else
213 type = metric_absolute;
214
215 /* Check beginning with digit string. */
216 if (*pnt < '0' || *pnt > '9')
217 return NULL;
218
219 /* Convert string to integer. */
220 metric = strtol(pnt, &endptr, 10);
221
222 if (metric == LONG_MAX || *endptr != '\0')
223 return NULL;
224 /* Commented out by Hasso Tepper, to avoid problems in vtysh. */
225 /* if (metric < 0 || metric > RIPNG_METRIC_INFINITY) */
226 if (metric < 0)
227 return NULL;
228
229 mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
230 sizeof(struct rip_metric_modifier));
231 mod->type = type;
232 mod->metric = metric;
233
234 return mod;
235 }
236
237 /* Free route map's compiled `set metric' value. */
238 static void route_set_metric_free(void *rule)
239 {
240 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
241 }
242
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,
246 };
247
248 /* `set ipv6 next-hop local IP_ADDRESS' */
249
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,
254 void *object)
255 {
256 struct in6_addr *address;
257 struct ripng_info *rinfo;
258
259 if (type == RMAP_RIPNG) {
260 /* Fetch routemap's rule information. */
261 address = rule;
262 rinfo = object;
263
264 /* Set next hop value. */
265 rinfo->nexthop_out = *address;
266 }
267
268 return RMAP_OKAY;
269 }
270
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)
274 {
275 int ret;
276 struct in6_addr *address;
277
278 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
279
280 ret = inet_pton(AF_INET6, arg, address);
281
282 if (ret == 0) {
283 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
284 return NULL;
285 }
286
287 return address;
288 }
289
290 /* Free route map's compiled `ipv6 nexthop local' value. */
291 static void route_set_ipv6_nexthop_local_free(void *rule)
292 {
293 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
294 }
295
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};
301
302 /* `set tag TAG' */
303
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)
307 {
308 route_tag_t *tag;
309 struct ripng_info *rinfo;
310
311 if (type == RMAP_RIPNG) {
312 /* Fetch routemap's rule information. */
313 tag = rule;
314 rinfo = object;
315
316 /* Set next hop value. */
317 rinfo->tag_out = *tag;
318 }
319
320 return RMAP_OKAY;
321 }
322
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};
327
328 #define MATCH_STR "Match values from routing table\n"
329 #define SET_STR "Set values in destination routing protocol\n"
330
331 void ripng_route_map_reset()
332 {
333 /* XXX ??? */
334 ;
335 }
336
337 void ripng_route_map_init()
338 {
339 route_map_init();
340
341 route_map_match_interface_hook(generic_match_add);
342 route_map_no_match_interface_hook(generic_match_delete);
343
344 route_map_match_metric_hook(generic_match_add);
345 route_map_no_match_metric_hook(generic_match_delete);
346
347 route_map_match_tag_hook(generic_match_add);
348 route_map_no_match_tag_hook(generic_match_delete);
349
350 route_map_set_ipv6_nexthop_local_hook(generic_set_add);
351 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete);
352
353 route_map_set_metric_hook(generic_set_add);
354 route_map_no_set_metric_hook(generic_set_delete);
355
356 route_map_set_tag_hook(generic_set_add);
357 route_map_no_set_tag_hook(generic_set_delete);
358
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);
365 }