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