]> git.proxmox.com Git - mirror_frr.git/blame - ripngd/ripng_routemap.c
zebra: Allow ns delete to happen after under/over flow checks
[mirror_frr.git] / ripngd / ripng_routemap.c
CommitLineData
718e3744 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 *
896014f4
DL
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
718e3744 19 */
20
21#include <zebra.h>
22
23#include "if.h"
24#include "memory.h"
25#include "prefix.h"
82f97584 26#include "vty.h"
718e3744 27#include "routemap.h"
28#include "command.h"
a94434b6 29#include "sockunion.h"
718e3744 30
31#include "ripngd/ripngd.h"
6b0655a2 32
d62a17ae 33struct rip_metric_modifier {
34 enum { metric_increment, metric_decrement, metric_absolute } type;
6a74c5f9 35 bool used;
d7c0a89a 36 uint8_t metric;
a94434b6 37};
38
a94434b6 39/* `match metric METRIC' */
40/* Match function return 1 if match is success else return zero. */
123214ef
MS
41static route_map_result_t route_match_metric(void *rule,
42 const struct prefix *prefix,
d62a17ae 43 route_map_object_t type,
44 void *object)
a94434b6 45{
d7c0a89a 46 uint32_t *metric;
d62a17ae 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 }
a94434b6 58 return RMAP_NOMATCH;
a94434b6 59}
60
61/* Route map `match metric' match statement. `arg' is METRIC value */
d62a17ae 62static void *route_match_metric_compile(const char *arg)
a94434b6 63{
d7c0a89a 64 uint32_t *metric;
a94434b6 65
d7c0a89a 66 metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
d62a17ae 67 *metric = atoi(arg);
a94434b6 68
d62a17ae 69 if (*metric > 0)
70 return metric;
a94434b6 71
d62a17ae 72 XFREE(MTYPE_ROUTE_MAP_COMPILED, metric);
73 return NULL;
a94434b6 74}
75
76/* Free route map's compiled `match metric' value. */
d62a17ae 77static void route_match_metric_free(void *rule)
a94434b6 78{
d62a17ae 79 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
a94434b6 80}
81
82/* Route map commands for metric matching. */
d62a17ae 83static struct route_map_rule_cmd route_match_metric_cmd = {
84 "metric", route_match_metric, route_match_metric_compile,
85 route_match_metric_free};
6b0655a2 86
718e3744 87/* `match interface IFNAME' */
a94434b6 88/* Match function return 1 if match is success else return zero. */
d62a17ae 89static route_map_result_t route_match_interface(void *rule,
123214ef 90 const struct prefix *prefix,
d62a17ae 91 route_map_object_t type,
92 void *object)
718e3744 93{
d62a17ae 94 struct ripng_info *rinfo;
95 struct interface *ifp;
96 char *ifname;
718e3744 97
d62a17ae 98 if (type == RMAP_RIPNG) {
99 ifname = rule;
100 ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
718e3744 101
d62a17ae 102 if (!ifp)
103 return RMAP_NOMATCH;
718e3744 104
d62a17ae 105 rinfo = object;
718e3744 106
d62a17ae 107 if (rinfo->ifindex == ifp->ifindex)
108 return RMAP_MATCH;
109 else
110 return RMAP_NOMATCH;
111 }
a94434b6 112 return RMAP_NOMATCH;
718e3744 113}
114
a94434b6 115/* Route map `match interface' match statement. `arg' is IFNAME value */
d62a17ae 116static void *route_match_interface_compile(const char *arg)
718e3744 117{
d62a17ae 118 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
718e3744 119}
120
d62a17ae 121static void route_match_interface_free(void *rule)
718e3744 122{
d62a17ae 123 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 124}
125
d62a17ae 126static struct route_map_rule_cmd route_match_interface_cmd = {
127 "interface", route_match_interface, route_match_interface_compile,
128 route_match_interface_free};
a94434b6 129
130/* `match tag TAG' */
131/* Match function return 1 if match is success else return zero. */
123214ef
MS
132static route_map_result_t route_match_tag(void *rule,
133 const struct prefix *prefix,
d62a17ae 134 route_map_object_t type, void *object)
718e3744 135{
d62a17ae 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 }
a94434b6 151 return RMAP_NOMATCH;
a94434b6 152}
153
d62a17ae 154static struct route_map_rule_cmd route_match_tag_cmd = {
9d303b37 155 "tag", route_match_tag, route_map_rule_tag_compile,
d62a17ae 156 route_map_rule_tag_free,
718e3744 157};
6b0655a2 158
a94434b6 159/* `set metric METRIC' */
718e3744 160
a94434b6 161/* Set metric to attribute. */
123214ef
MS
162static route_map_result_t route_set_metric(void *rule,
163 const struct prefix *prefix,
d62a17ae 164 route_map_object_t type,
165 void *object)
718e3744 166{
d62a17ae 167 if (type == RMAP_RIPNG) {
168 struct rip_metric_modifier *mod;
169 struct ripng_info *rinfo;
170
171 mod = rule;
172 rinfo = object;
173
6a74c5f9
DS
174 if (!mod->used)
175 return RMAP_OKAY;
176
d62a17ae 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;
718e3744 192}
193
a94434b6 194/* set metric compilation. */
d62a17ae 195static void *route_set_metric_compile(const char *arg)
718e3744 196{
d62a17ae 197 int len;
198 const char *pnt;
d62a17ae 199 long metric;
200 char *endptr = NULL;
201 struct rip_metric_modifier *mod;
202
203 len = strlen(arg);
204 pnt = arg;
205
6a74c5f9
DS
206 mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
207 sizeof(struct rip_metric_modifier));
208 mod->used = false;
209
d62a17ae 210 if (len == 0)
6a74c5f9 211 return mod;
d62a17ae 212
213 /* Examine first character. */
214 if (arg[0] == '+') {
6a74c5f9 215 mod->type = metric_increment;
d62a17ae 216 pnt++;
217 } else if (arg[0] == '-') {
6a74c5f9 218 mod->type = metric_decrement;
d62a17ae 219 pnt++;
220 } else
6a74c5f9 221 mod->type = metric_absolute;
d62a17ae 222
223 /* Check beginning with digit string. */
224 if (*pnt < '0' || *pnt > '9')
6a74c5f9 225 return mod;
d62a17ae 226
227 /* Convert string to integer. */
228 metric = strtol(pnt, &endptr, 10);
229
6a74c5f9
DS
230 if (*endptr != '\0' || metric < 0)
231 return mod;
d62a17ae 232
6a74c5f9
DS
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;
d62a17ae 240
6a74c5f9 241 mod->used = true;
d62a17ae 242 return mod;
718e3744 243}
244
a94434b6 245/* Free route map's compiled `set metric' value. */
d62a17ae 246static void route_set_metric_free(void *rule)
718e3744 247{
d62a17ae 248 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
718e3744 249}
250
d62a17ae 251static struct route_map_rule_cmd route_set_metric_cmd = {
9d303b37 252 "metric", route_set_metric, route_set_metric_compile,
d62a17ae 253 route_set_metric_free,
718e3744 254};
a94434b6 255
256/* `set ipv6 next-hop local IP_ADDRESS' */
257
258/* Set nexthop to object. ojbect must be pointer to struct attr. */
d62a17ae 259static route_map_result_t route_set_ipv6_nexthop_local(void *rule,
123214ef 260 const struct prefix *p,
d62a17ae 261 route_map_object_t type,
262 void *object)
718e3744 263{
d62a17ae 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;
718e3744 277}
278
a94434b6 279/* Route map `ipv6 nexthop local' compile function. Given string is converted
280 to struct in6_addr structure. */
d62a17ae 281static void *route_set_ipv6_nexthop_local_compile(const char *arg)
718e3744 282{
d62a17ae 283 int ret;
284 struct in6_addr *address;
718e3744 285
d62a17ae 286 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in6_addr));
a94434b6 287
d62a17ae 288 ret = inet_pton(AF_INET6, arg, address);
a94434b6 289
d62a17ae 290 if (ret == 0) {
291 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
292 return NULL;
293 }
a94434b6 294
d62a17ae 295 return address;
718e3744 296}
297
a94434b6 298/* Free route map's compiled `ipv6 nexthop local' value. */
d62a17ae 299static void route_set_ipv6_nexthop_local_free(void *rule)
718e3744 300{
d62a17ae 301 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
a94434b6 302}
718e3744 303
a94434b6 304/* Route map commands for ipv6 nexthop local set. */
d62a17ae 305static 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};
a94434b6 309
310/* `set tag TAG' */
311
312/* Set tag to object. ojbect must be pointer to struct attr. */
123214ef
MS
313static route_map_result_t route_set_tag(void *rule,
314 const struct prefix *prefix,
d62a17ae 315 route_map_object_t type, void *object)
a94434b6 316{
d62a17ae 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;
718e3744 330}
331
a94434b6 332/* Route map commands for tag set. */
d62a17ae 333static 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};
6b0655a2 336
a94434b6 337#define MATCH_STR "Match values from routing table\n"
338#define SET_STR "Set values in destination routing protocol\n"
339
d62a17ae 340void ripng_route_map_init()
718e3744 341{
d62a17ae 342 route_map_init();
718e3744 343
d62a17ae 344 route_map_match_interface_hook(generic_match_add);
345 route_map_no_match_interface_hook(generic_match_delete);
82f97584 346
d62a17ae 347 route_map_match_metric_hook(generic_match_add);
348 route_map_no_match_metric_hook(generic_match_delete);
82f97584 349
d62a17ae 350 route_map_match_tag_hook(generic_match_add);
351 route_map_no_match_tag_hook(generic_match_delete);
82f97584 352
d62a17ae 353 route_map_set_ipv6_nexthop_local_hook(generic_set_add);
354 route_map_no_set_ipv6_nexthop_local_hook(generic_set_delete);
82f97584 355
d62a17ae 356 route_map_set_metric_hook(generic_set_add);
357 route_map_no_set_metric_hook(generic_set_delete);
82f97584 358
d62a17ae 359 route_map_set_tag_hook(generic_set_add);
360 route_map_no_set_tag_hook(generic_set_delete);
82f97584 361
d62a17ae 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);
718e3744 368}