blob: 4e736944c4ad7a52bee8c59219d825d4bbd65a3b [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* RIPv2 routemap.
vincentfbf5d032005-09-29 11:25:50 +00002 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
paul718e3742002-12-13 20:15:29 +00003 * Copyright (C) 1999 Kunihiro Ishiguro <kunihiro@zebra.org>
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "memory.h"
26#include "prefix.h"
27#include "routemap.h"
28#include "command.h"
29#include "filter.h"
30#include "log.h"
31#include "sockunion.h" /* for inet_aton () */
32#include "plist.h"
33
34#include "ripd/ripd.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020035
hasso16705132003-05-25 14:49:19 +000036struct rip_metric_modifier
37{
38 enum
39 {
40 metric_increment,
41 metric_decrement,
42 metric_absolute
43 } type;
44
45 u_char metric;
46};
paul718e3742002-12-13 20:15:29 +000047
48/* Add rip route map rule. */
pauldc63bfd2005-10-25 23:31:05 +000049static int
paul718e3742002-12-13 20:15:29 +000050rip_route_match_add (struct vty *vty, struct route_map_index *index,
hasso98b718a2004-10-11 12:57:57 +000051 const char *command, const char *arg)
paul718e3742002-12-13 20:15:29 +000052{
53 int ret;
54
55 ret = route_map_add_match (index, command, arg);
56 if (ret)
57 {
58 switch (ret)
59 {
60 case RMAP_RULE_MISSING:
Daniel Waltonc7f25b92015-05-19 17:47:22 -070061 vty_out (vty, "%% RIP Can't find rule.%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +000062 return CMD_WARNING;
paul718e3742002-12-13 20:15:29 +000063 case RMAP_COMPILE_ERROR:
Daniel Waltonc7f25b92015-05-19 17:47:22 -070064 vty_out (vty, "%% RIP Argument is malformed.%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +000065 return CMD_WARNING;
paul718e3742002-12-13 20:15:29 +000066 }
67 }
68 return CMD_SUCCESS;
69}
70
71/* Delete rip route map rule. */
pauldc63bfd2005-10-25 23:31:05 +000072static int
paul718e3742002-12-13 20:15:29 +000073rip_route_match_delete (struct vty *vty, struct route_map_index *index,
hasso98b718a2004-10-11 12:57:57 +000074 const char *command, const char *arg)
paul718e3742002-12-13 20:15:29 +000075{
76 int ret;
77
78 ret = route_map_delete_match (index, command, arg);
79 if (ret)
80 {
81 switch (ret)
82 {
83 case RMAP_RULE_MISSING:
Daniel Waltonc7f25b92015-05-19 17:47:22 -070084 vty_out (vty, "%% RIP Can't find rule.%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +000085 return CMD_WARNING;
paul718e3742002-12-13 20:15:29 +000086 case RMAP_COMPILE_ERROR:
Daniel Waltonc7f25b92015-05-19 17:47:22 -070087 vty_out (vty, "%% RIP Argument is malformed.%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +000088 return CMD_WARNING;
paul718e3742002-12-13 20:15:29 +000089 }
90 }
91 return CMD_SUCCESS;
92}
93
94/* Add rip route map rule. */
pauldc63bfd2005-10-25 23:31:05 +000095static int
paul718e3742002-12-13 20:15:29 +000096rip_route_set_add (struct vty *vty, struct route_map_index *index,
hasso98b718a2004-10-11 12:57:57 +000097 const char *command, const char *arg)
paul718e3742002-12-13 20:15:29 +000098{
99 int ret;
100
101 ret = route_map_add_set (index, command, arg);
102 if (ret)
103 {
104 switch (ret)
105 {
106 case RMAP_RULE_MISSING:
Daniel Waltonc7f25b92015-05-19 17:47:22 -0700107 vty_out (vty, "%% RIP Can't find rule.%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000108 return CMD_WARNING;
paul718e3742002-12-13 20:15:29 +0000109 case RMAP_COMPILE_ERROR:
vincentfbf5d032005-09-29 11:25:50 +0000110 /* rip, ripng and other protocols share the set metric command
111 but only values from 0 to 16 are valid for rip and ripng
112 if metric is out of range for rip and ripng, it is not for
113 other protocols. Do not return an error */
114 if (strcmp(command, "metric")) {
Daniel Waltonc7f25b92015-05-19 17:47:22 -0700115 vty_out (vty, "%% RIP Argument is malformed.%s", VTY_NEWLINE);
vincentfbf5d032005-09-29 11:25:50 +0000116 return CMD_WARNING;
117 }
paul718e3742002-12-13 20:15:29 +0000118 }
119 }
120 return CMD_SUCCESS;
121}
122
123/* Delete rip route map rule. */
pauldc63bfd2005-10-25 23:31:05 +0000124static int
paul718e3742002-12-13 20:15:29 +0000125rip_route_set_delete (struct vty *vty, struct route_map_index *index,
hasso98b718a2004-10-11 12:57:57 +0000126 const char *command, const char *arg)
paul718e3742002-12-13 20:15:29 +0000127{
128 int ret;
129
130 ret = route_map_delete_set (index, command, arg);
131 if (ret)
132 {
133 switch (ret)
134 {
135 case RMAP_RULE_MISSING:
Daniel Waltonc7f25b92015-05-19 17:47:22 -0700136 vty_out (vty, "%% RIP Can't find rule.%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000137 return CMD_WARNING;
paul718e3742002-12-13 20:15:29 +0000138 case RMAP_COMPILE_ERROR:
Daniel Waltonc7f25b92015-05-19 17:47:22 -0700139 vty_out (vty, "%% RIP Argument is malformed.%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +0000140 return CMD_WARNING;
paul718e3742002-12-13 20:15:29 +0000141 }
142 }
143 return CMD_SUCCESS;
144}
145
146/* Hook function for updating route_map assignment. */
paul11dde9c2004-05-31 14:00:00 +0000147/* ARGSUSED */
pauldc63bfd2005-10-25 23:31:05 +0000148static void
hasso98b718a2004-10-11 12:57:57 +0000149rip_route_map_update (const char *notused)
paul718e3742002-12-13 20:15:29 +0000150{
151 int i;
152
153 if (rip)
154 {
155 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
156 {
157 if (rip->route_map[i].name)
158 rip->route_map[i].map =
159 route_map_lookup_by_name (rip->route_map[i].name);
160 }
161 }
162}
David Lamparter6b0655a2014-06-04 06:53:35 +0200163
paul718e3742002-12-13 20:15:29 +0000164/* `match metric METRIC' */
165/* Match function return 1 if match is success else return zero. */
pauldc63bfd2005-10-25 23:31:05 +0000166static route_map_result_t
paul718e3742002-12-13 20:15:29 +0000167route_match_metric (void *rule, struct prefix *prefix,
168 route_map_object_t type, void *object)
169{
170 u_int32_t *metric;
vincentfbf5d032005-09-29 11:25:50 +0000171 u_int32_t check;
paul718e3742002-12-13 20:15:29 +0000172 struct rip_info *rinfo;
173
174 if (type == RMAP_RIP)
175 {
176 metric = rule;
177 rinfo = object;
178
vincentfbf5d032005-09-29 11:25:50 +0000179 /* If external metric is available, the route-map should
180 work on this one (for redistribute purpose) */
181 check = (rinfo->external_metric) ? rinfo->external_metric :
182 rinfo->metric;
183 if (check == *metric)
paul718e3742002-12-13 20:15:29 +0000184 return RMAP_MATCH;
185 else
186 return RMAP_NOMATCH;
187 }
188 return RMAP_NOMATCH;
189}
190
191/* Route map `match metric' match statement. `arg' is METRIC value */
pauldc63bfd2005-10-25 23:31:05 +0000192static void *
hasso98b718a2004-10-11 12:57:57 +0000193route_match_metric_compile (const char *arg)
paul718e3742002-12-13 20:15:29 +0000194{
195 u_int32_t *metric;
196
197 metric = XMALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (u_int32_t));
198 *metric = atoi (arg);
199
200 if(*metric > 0)
201 return metric;
202
203 XFREE (MTYPE_ROUTE_MAP_COMPILED, metric);
204 return NULL;
205}
206
207/* Free route map's compiled `match metric' value. */
pauldc63bfd2005-10-25 23:31:05 +0000208static void
paul718e3742002-12-13 20:15:29 +0000209route_match_metric_free (void *rule)
210{
211 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
212}
213
214/* Route map commands for metric matching. */
215struct route_map_rule_cmd route_match_metric_cmd =
216{
217 "metric",
218 route_match_metric,
219 route_match_metric_compile,
220 route_match_metric_free
221};
222
223/* `match interface IFNAME' */
224/* Match function return 1 if match is success else return zero. */
pauldc63bfd2005-10-25 23:31:05 +0000225static route_map_result_t
paul718e3742002-12-13 20:15:29 +0000226route_match_interface (void *rule, struct prefix *prefix,
227 route_map_object_t type, void *object)
228{
229 struct rip_info *rinfo;
230 struct interface *ifp;
231 char *ifname;
232
233 if (type == RMAP_RIP)
234 {
235 ifname = rule;
236 ifp = if_lookup_by_name(ifname);
237
238 if (!ifp)
239 return RMAP_NOMATCH;
240
241 rinfo = object;
242
hassocf96db12005-05-25 21:15:32 +0000243 if (rinfo->ifindex_out == ifp->ifindex || rinfo->ifindex == ifp->ifindex)
paul718e3742002-12-13 20:15:29 +0000244 return RMAP_MATCH;
245 else
246 return RMAP_NOMATCH;
247 }
248 return RMAP_NOMATCH;
249}
250
251/* Route map `match interface' match statement. `arg' is IFNAME value */
252/* XXX I don`t know if I need to check does interface exist? */
pauldc63bfd2005-10-25 23:31:05 +0000253static void *
hasso98b718a2004-10-11 12:57:57 +0000254route_match_interface_compile (const char *arg)
paul718e3742002-12-13 20:15:29 +0000255{
256 return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);
257}
258
259/* Free route map's compiled `match interface' value. */
pauldc63bfd2005-10-25 23:31:05 +0000260static void
paul718e3742002-12-13 20:15:29 +0000261route_match_interface_free (void *rule)
262{
263 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
264}
265
266/* Route map commands for interface matching. */
267struct route_map_rule_cmd route_match_interface_cmd =
268{
269 "interface",
270 route_match_interface,
271 route_match_interface_compile,
272 route_match_interface_free
273};
274
275/* `match ip next-hop IP_ACCESS_LIST' */
276
277/* Match function return 1 if match is success else return zero. */
pauldc63bfd2005-10-25 23:31:05 +0000278static route_map_result_t
paul718e3742002-12-13 20:15:29 +0000279route_match_ip_next_hop (void *rule, struct prefix *prefix,
280 route_map_object_t type, void *object)
281{
282 struct access_list *alist;
283 struct rip_info *rinfo;
284 struct prefix_ipv4 p;
285
286 if (type == RMAP_RIP)
287 {
288 rinfo = object;
289 p.family = AF_INET;
hassodc625e82005-05-26 06:26:40 +0000290 p.prefix = (rinfo->nexthop.s_addr) ? rinfo->nexthop : rinfo->from;
paul718e3742002-12-13 20:15:29 +0000291 p.prefixlen = IPV4_MAX_BITLEN;
292
293 alist = access_list_lookup (AFI_IP, (char *) rule);
294 if (alist == NULL)
295 return RMAP_NOMATCH;
296
297 return (access_list_apply (alist, &p) == FILTER_DENY ?
298 RMAP_NOMATCH : RMAP_MATCH);
299 }
300 return RMAP_NOMATCH;
301}
302
303/* Route map `ip next-hop' match statement. `arg' should be
304 access-list name. */
pauldc63bfd2005-10-25 23:31:05 +0000305static void *
hasso98b718a2004-10-11 12:57:57 +0000306route_match_ip_next_hop_compile (const char *arg)
paul718e3742002-12-13 20:15:29 +0000307{
308 return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);
309}
310
311/* Free route map's compiled `. */
pauldc63bfd2005-10-25 23:31:05 +0000312static void
paul718e3742002-12-13 20:15:29 +0000313route_match_ip_next_hop_free (void *rule)
314{
315 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
316}
317
318/* Route map commands for ip next-hop matching. */
pauldc63bfd2005-10-25 23:31:05 +0000319static struct route_map_rule_cmd route_match_ip_next_hop_cmd =
paul718e3742002-12-13 20:15:29 +0000320{
321 "ip next-hop",
322 route_match_ip_next_hop,
323 route_match_ip_next_hop_compile,
324 route_match_ip_next_hop_free
325};
David Lamparter6b0655a2014-06-04 06:53:35 +0200326
paul718e3742002-12-13 20:15:29 +0000327/* `match ip next-hop prefix-list PREFIX_LIST' */
328
pauldc63bfd2005-10-25 23:31:05 +0000329static route_map_result_t
paul718e3742002-12-13 20:15:29 +0000330route_match_ip_next_hop_prefix_list (void *rule, struct prefix *prefix,
331 route_map_object_t type, void *object)
332{
333 struct prefix_list *plist;
334 struct rip_info *rinfo;
335 struct prefix_ipv4 p;
336
337 if (type == RMAP_RIP)
338 {
339 rinfo = object;
340 p.family = AF_INET;
hassodc625e82005-05-26 06:26:40 +0000341 p.prefix = (rinfo->nexthop.s_addr) ? rinfo->nexthop : rinfo->from;
paul718e3742002-12-13 20:15:29 +0000342 p.prefixlen = IPV4_MAX_BITLEN;
343
344 plist = prefix_list_lookup (AFI_IP, (char *) rule);
345 if (plist == NULL)
346 return RMAP_NOMATCH;
347
348 return (prefix_list_apply (plist, &p) == PREFIX_DENY ?
349 RMAP_NOMATCH : RMAP_MATCH);
350 }
351 return RMAP_NOMATCH;
352}
353
pauldc63bfd2005-10-25 23:31:05 +0000354static void *
hasso98b718a2004-10-11 12:57:57 +0000355route_match_ip_next_hop_prefix_list_compile (const char *arg)
paul718e3742002-12-13 20:15:29 +0000356{
357 return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);
358}
359
pauldc63bfd2005-10-25 23:31:05 +0000360static void
paul718e3742002-12-13 20:15:29 +0000361route_match_ip_next_hop_prefix_list_free (void *rule)
362{
363 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
364}
365
pauldc63bfd2005-10-25 23:31:05 +0000366static struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd =
paul718e3742002-12-13 20:15:29 +0000367{
368 "ip next-hop prefix-list",
369 route_match_ip_next_hop_prefix_list,
370 route_match_ip_next_hop_prefix_list_compile,
371 route_match_ip_next_hop_prefix_list_free
372};
David Lamparter6b0655a2014-06-04 06:53:35 +0200373
paul718e3742002-12-13 20:15:29 +0000374/* `match ip address IP_ACCESS_LIST' */
375
376/* Match function should return 1 if match is success else return
377 zero. */
pauldc63bfd2005-10-25 23:31:05 +0000378static route_map_result_t
paul718e3742002-12-13 20:15:29 +0000379route_match_ip_address (void *rule, struct prefix *prefix,
380 route_map_object_t type, void *object)
381{
382 struct access_list *alist;
383
384 if (type == RMAP_RIP)
385 {
386 alist = access_list_lookup (AFI_IP, (char *) rule);
387 if (alist == NULL)
388 return RMAP_NOMATCH;
389
390 return (access_list_apply (alist, prefix) == FILTER_DENY ?
391 RMAP_NOMATCH : RMAP_MATCH);
392 }
393 return RMAP_NOMATCH;
394}
395
396/* Route map `ip address' match statement. `arg' should be
397 access-list name. */
pauldc63bfd2005-10-25 23:31:05 +0000398static void *
hasso98b718a2004-10-11 12:57:57 +0000399route_match_ip_address_compile (const char *arg)
paul718e3742002-12-13 20:15:29 +0000400{
401 return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);
402}
403
404/* Free route map's compiled `ip address' value. */
pauldc63bfd2005-10-25 23:31:05 +0000405static void
paul718e3742002-12-13 20:15:29 +0000406route_match_ip_address_free (void *rule)
407{
408 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
409}
410
411/* Route map commands for ip address matching. */
pauldc63bfd2005-10-25 23:31:05 +0000412static struct route_map_rule_cmd route_match_ip_address_cmd =
paul718e3742002-12-13 20:15:29 +0000413{
414 "ip address",
415 route_match_ip_address,
416 route_match_ip_address_compile,
417 route_match_ip_address_free
418};
David Lamparter6b0655a2014-06-04 06:53:35 +0200419
paul718e3742002-12-13 20:15:29 +0000420/* `match ip address prefix-list PREFIX_LIST' */
421
pauldc63bfd2005-10-25 23:31:05 +0000422static route_map_result_t
paul718e3742002-12-13 20:15:29 +0000423route_match_ip_address_prefix_list (void *rule, struct prefix *prefix,
424 route_map_object_t type, void *object)
425{
426 struct prefix_list *plist;
427
428 if (type == RMAP_RIP)
429 {
430 plist = prefix_list_lookup (AFI_IP, (char *) rule);
431 if (plist == NULL)
432 return RMAP_NOMATCH;
433
434 return (prefix_list_apply (plist, prefix) == PREFIX_DENY ?
435 RMAP_NOMATCH : RMAP_MATCH);
436 }
437 return RMAP_NOMATCH;
438}
439
pauldc63bfd2005-10-25 23:31:05 +0000440static void *
hasso98b718a2004-10-11 12:57:57 +0000441route_match_ip_address_prefix_list_compile (const char *arg)
paul718e3742002-12-13 20:15:29 +0000442{
443 return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);
444}
445
pauldc63bfd2005-10-25 23:31:05 +0000446static void
paul718e3742002-12-13 20:15:29 +0000447route_match_ip_address_prefix_list_free (void *rule)
448{
449 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
450}
451
pauldc63bfd2005-10-25 23:31:05 +0000452static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd =
paul718e3742002-12-13 20:15:29 +0000453{
454 "ip address prefix-list",
455 route_match_ip_address_prefix_list,
456 route_match_ip_address_prefix_list_compile,
457 route_match_ip_address_prefix_list_free
458};
hasso16705132003-05-25 14:49:19 +0000459
460/* `match tag TAG' */
461/* Match function return 1 if match is success else return zero. */
pauldc63bfd2005-10-25 23:31:05 +0000462static route_map_result_t
hasso16705132003-05-25 14:49:19 +0000463route_match_tag (void *rule, struct prefix *prefix,
464 route_map_object_t type, void *object)
465{
Paul Jakma96d10602016-07-01 14:23:45 +0100466 route_tag_t *tag;
hasso16705132003-05-25 14:49:19 +0000467 struct rip_info *rinfo;
468
469 if (type == RMAP_RIP)
470 {
471 tag = rule;
472 rinfo = object;
473
474 /* The information stored by rinfo is host ordered. */
475 if (rinfo->tag == *tag)
476 return RMAP_MATCH;
477 else
478 return RMAP_NOMATCH;
479 }
480 return RMAP_NOMATCH;
481}
482
hasso16705132003-05-25 14:49:19 +0000483/* Route map commands for tag matching. */
Christian Frankeddc160c2016-10-01 20:42:34 +0200484static struct route_map_rule_cmd route_match_tag_cmd =
hasso16705132003-05-25 14:49:19 +0000485{
486 "tag",
487 route_match_tag,
Christian Frankeddc160c2016-10-01 20:42:34 +0200488 route_map_rule_tag_compile,
489 route_map_rule_tag_free,
hasso16705132003-05-25 14:49:19 +0000490};
David Lamparter6b0655a2014-06-04 06:53:35 +0200491
paul718e3742002-12-13 20:15:29 +0000492/* `set metric METRIC' */
493
494/* Set metric to attribute. */
pauldc63bfd2005-10-25 23:31:05 +0000495static route_map_result_t
paul718e3742002-12-13 20:15:29 +0000496route_set_metric (void *rule, struct prefix *prefix,
497 route_map_object_t type, void *object)
498{
paul718e3742002-12-13 20:15:29 +0000499 if (type == RMAP_RIP)
500 {
hasso16705132003-05-25 14:49:19 +0000501 struct rip_metric_modifier *mod;
502 struct rip_info *rinfo;
503
504 mod = rule;
paul718e3742002-12-13 20:15:29 +0000505 rinfo = object;
hasso16705132003-05-25 14:49:19 +0000506
507 if (mod->type == metric_increment)
508 rinfo->metric_out += mod->metric;
509 else if (mod->type == metric_decrement)
510 rinfo->metric_out -= mod->metric;
511 else if (mod->type == metric_absolute)
512 rinfo->metric_out = mod->metric;
513
Paul Jakmab25ea4d2006-09-11 02:14:16 +0000514 if ((signed int)rinfo->metric_out < 1)
hasso16705132003-05-25 14:49:19 +0000515 rinfo->metric_out = 1;
516 if (rinfo->metric_out > RIP_METRIC_INFINITY)
517 rinfo->metric_out = RIP_METRIC_INFINITY;
518
paul718e3742002-12-13 20:15:29 +0000519 rinfo->metric_set = 1;
520 }
521 return RMAP_OKAY;
522}
523
524/* set metric compilation. */
pauldc63bfd2005-10-25 23:31:05 +0000525static void *
hasso98b718a2004-10-11 12:57:57 +0000526route_set_metric_compile (const char *arg)
paul718e3742002-12-13 20:15:29 +0000527{
hasso16705132003-05-25 14:49:19 +0000528 int len;
hasso98b718a2004-10-11 12:57:57 +0000529 const char *pnt;
hasso16705132003-05-25 14:49:19 +0000530 int type;
531 long metric;
532 char *endptr = NULL;
533 struct rip_metric_modifier *mod;
paul718e3742002-12-13 20:15:29 +0000534
hasso16705132003-05-25 14:49:19 +0000535 len = strlen (arg);
536 pnt = arg;
paul718e3742002-12-13 20:15:29 +0000537
hasso16705132003-05-25 14:49:19 +0000538 if (len == 0)
539 return NULL;
paul718e3742002-12-13 20:15:29 +0000540
hasso16705132003-05-25 14:49:19 +0000541 /* Examine first character. */
542 if (arg[0] == '+')
543 {
544 type = metric_increment;
545 pnt++;
546 }
547 else if (arg[0] == '-')
548 {
549 type = metric_decrement;
550 pnt++;
551 }
552 else
553 type = metric_absolute;
paul718e3742002-12-13 20:15:29 +0000554
hasso16705132003-05-25 14:49:19 +0000555 /* Check beginning with digit string. */
556 if (*pnt < '0' || *pnt > '9')
557 return NULL;
558
559 /* Convert string to integer. */
560 metric = strtol (pnt, &endptr, 10);
561
562 if (metric == LONG_MAX || *endptr != '\0')
563 return NULL;
564 if (metric < 0 || metric > RIP_METRIC_INFINITY)
565 return NULL;
566
567 mod = XMALLOC (MTYPE_ROUTE_MAP_COMPILED,
568 sizeof (struct rip_metric_modifier));
569 mod->type = type;
570 mod->metric = metric;
571
572 return mod;
paul718e3742002-12-13 20:15:29 +0000573}
574
575/* Free route map's compiled `set metric' value. */
pauldc63bfd2005-10-25 23:31:05 +0000576static void
paul718e3742002-12-13 20:15:29 +0000577route_set_metric_free (void *rule)
578{
579 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
580}
581
582/* Set metric rule structure. */
pauldc63bfd2005-10-25 23:31:05 +0000583static struct route_map_rule_cmd route_set_metric_cmd =
paul718e3742002-12-13 20:15:29 +0000584{
585 "metric",
586 route_set_metric,
587 route_set_metric_compile,
588 route_set_metric_free,
589};
590
591/* `set ip next-hop IP_ADDRESS' */
592
593/* Set nexthop to object. ojbect must be pointer to struct attr. */
pauldc63bfd2005-10-25 23:31:05 +0000594static route_map_result_t
paul718e3742002-12-13 20:15:29 +0000595route_set_ip_nexthop (void *rule, struct prefix *prefix,
596 route_map_object_t type, void *object)
597{
598 struct in_addr *address;
599 struct rip_info *rinfo;
600
601 if(type == RMAP_RIP)
602 {
603 /* Fetch routemap's rule information. */
604 address = rule;
605 rinfo = object;
606
607 /* Set next hop value. */
608 rinfo->nexthop_out = *address;
609 }
610
611 return RMAP_OKAY;
612}
613
614/* Route map `ip nexthop' compile function. Given string is converted
615 to struct in_addr structure. */
pauldc63bfd2005-10-25 23:31:05 +0000616static void *
hasso98b718a2004-10-11 12:57:57 +0000617route_set_ip_nexthop_compile (const char *arg)
paul718e3742002-12-13 20:15:29 +0000618{
619 int ret;
620 struct in_addr *address;
621
622 address = XMALLOC (MTYPE_ROUTE_MAP_COMPILED, sizeof (struct in_addr));
623
624 ret = inet_aton (arg, address);
625
626 if (ret == 0)
627 {
628 XFREE (MTYPE_ROUTE_MAP_COMPILED, address);
629 return NULL;
630 }
631
632 return address;
633}
634
635/* Free route map's compiled `ip nexthop' value. */
pauldc63bfd2005-10-25 23:31:05 +0000636static void
paul718e3742002-12-13 20:15:29 +0000637route_set_ip_nexthop_free (void *rule)
638{
639 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
640}
641
642/* Route map commands for ip nexthop set. */
pauldc63bfd2005-10-25 23:31:05 +0000643static struct route_map_rule_cmd route_set_ip_nexthop_cmd =
paul718e3742002-12-13 20:15:29 +0000644{
645 "ip next-hop",
646 route_set_ip_nexthop,
647 route_set_ip_nexthop_compile,
648 route_set_ip_nexthop_free
649};
hasso16705132003-05-25 14:49:19 +0000650
651/* `set tag TAG' */
652
653/* Set tag to object. ojbect must be pointer to struct attr. */
pauldc63bfd2005-10-25 23:31:05 +0000654static route_map_result_t
hasso16705132003-05-25 14:49:19 +0000655route_set_tag (void *rule, struct prefix *prefix,
656 route_map_object_t type, void *object)
657{
Paul Jakma96d10602016-07-01 14:23:45 +0100658 route_tag_t *tag;
hasso16705132003-05-25 14:49:19 +0000659 struct rip_info *rinfo;
660
661 if(type == RMAP_RIP)
662 {
663 /* Fetch routemap's rule information. */
664 tag = rule;
665 rinfo = object;
666
667 /* Set next hop value. */
668 rinfo->tag_out = *tag;
669 }
670
671 return RMAP_OKAY;
672}
673
hasso16705132003-05-25 14:49:19 +0000674/* Route map commands for tag set. */
pauldc63bfd2005-10-25 23:31:05 +0000675static struct route_map_rule_cmd route_set_tag_cmd =
hasso16705132003-05-25 14:49:19 +0000676{
677 "tag",
678 route_set_tag,
Christian Frankeddc160c2016-10-01 20:42:34 +0200679 route_map_rule_tag_compile,
680 route_map_rule_tag_free
hasso16705132003-05-25 14:49:19 +0000681};
David Lamparter6b0655a2014-06-04 06:53:35 +0200682
paul718e3742002-12-13 20:15:29 +0000683#define MATCH_STR "Match values from routing table\n"
684#define SET_STR "Set values in destination routing protocol\n"
685
686DEFUN (match_metric,
687 match_metric_cmd,
688 "match metric <0-4294967295>",
689 MATCH_STR
690 "Match metric of route\n"
691 "Metric value\n")
692{
693 return rip_route_match_add (vty, vty->index, "metric", argv[0]);
694}
695
696DEFUN (no_match_metric,
697 no_match_metric_cmd,
698 "no match metric",
699 NO_STR
700 MATCH_STR
701 "Match metric of route\n")
702{
703 if (argc == 0)
704 return rip_route_match_delete (vty, vty->index, "metric", NULL);
705
706 return rip_route_match_delete (vty, vty->index, "metric", argv[0]);
707}
708
709ALIAS (no_match_metric,
710 no_match_metric_val_cmd,
711 "no match metric <0-4294967295>",
712 NO_STR
713 MATCH_STR
714 "Match metric of route\n"
715 "Metric value\n")
716
717DEFUN (match_interface,
718 match_interface_cmd,
719 "match interface WORD",
720 MATCH_STR
721 "Match first hop interface of route\n"
722 "Interface name\n")
723{
724 return rip_route_match_add (vty, vty->index, "interface", argv[0]);
725}
726
727DEFUN (no_match_interface,
728 no_match_interface_cmd,
729 "no match interface",
730 NO_STR
731 MATCH_STR
732 "Match first hop interface of route\n")
733{
734 if (argc == 0)
735 return rip_route_match_delete (vty, vty->index, "interface", NULL);
736
737 return rip_route_match_delete (vty, vty->index, "interface", argv[0]);
738}
739
740ALIAS (no_match_interface,
741 no_match_interface_val_cmd,
742 "no match interface WORD",
743 NO_STR
744 MATCH_STR
745 "Match first hop interface of route\n"
746 "Interface name\n")
747
748DEFUN (match_ip_next_hop,
749 match_ip_next_hop_cmd,
paul73ffb252003-04-19 15:49:49 +0000750 "match ip next-hop (<1-199>|<1300-2699>|WORD)",
paul718e3742002-12-13 20:15:29 +0000751 MATCH_STR
752 IP_STR
753 "Match next-hop address of route\n"
paul73ffb252003-04-19 15:49:49 +0000754 "IP access-list number\n"
755 "IP access-list number (expanded range)\n"
756 "IP Access-list name\n")
paul718e3742002-12-13 20:15:29 +0000757{
758 return rip_route_match_add (vty, vty->index, "ip next-hop", argv[0]);
759}
760
761DEFUN (no_match_ip_next_hop,
762 no_match_ip_next_hop_cmd,
763 "no match ip next-hop",
764 NO_STR
765 MATCH_STR
766 IP_STR
767 "Match next-hop address of route\n")
768{
769 if (argc == 0)
770 return rip_route_match_delete (vty, vty->index, "ip next-hop", NULL);
771
772 return rip_route_match_delete (vty, vty->index, "ip next-hop", argv[0]);
773}
774
775ALIAS (no_match_ip_next_hop,
776 no_match_ip_next_hop_val_cmd,
paul73ffb252003-04-19 15:49:49 +0000777 "no match ip next-hop (<1-199>|<1300-2699>|WORD)",
paul718e3742002-12-13 20:15:29 +0000778 NO_STR
779 MATCH_STR
780 IP_STR
781 "Match next-hop address of route\n"
paul73ffb252003-04-19 15:49:49 +0000782 "IP access-list number\n"
783 "IP access-list number (expanded range)\n"
784 "IP Access-list name\n")
paul718e3742002-12-13 20:15:29 +0000785
786DEFUN (match_ip_next_hop_prefix_list,
787 match_ip_next_hop_prefix_list_cmd,
788 "match ip next-hop prefix-list WORD",
789 MATCH_STR
790 IP_STR
791 "Match next-hop address of route\n"
792 "Match entries of prefix-lists\n"
793 "IP prefix-list name\n")
794{
795 return rip_route_match_add (vty, vty->index, "ip next-hop prefix-list", argv[0]);
796}
797
798DEFUN (no_match_ip_next_hop_prefix_list,
799 no_match_ip_next_hop_prefix_list_cmd,
800 "no match ip next-hop prefix-list",
801 NO_STR
802 MATCH_STR
803 IP_STR
804 "Match next-hop address of route\n"
805 "Match entries of prefix-lists\n")
806{
807 if (argc == 0)
808 return rip_route_match_delete (vty, vty->index, "ip next-hop prefix-list", NULL);
809
810 return rip_route_match_delete (vty, vty->index, "ip next-hop prefix-list", argv[0]);
811}
812
813ALIAS (no_match_ip_next_hop_prefix_list,
814 no_match_ip_next_hop_prefix_list_val_cmd,
815 "no match ip next-hop prefix-list WORD",
816 NO_STR
817 MATCH_STR
818 IP_STR
819 "Match next-hop address of route\n"
820 "Match entries of prefix-lists\n"
821 "IP prefix-list name\n")
822
paul73ffb252003-04-19 15:49:49 +0000823DEFUN (match_ip_address,
paul718e3742002-12-13 20:15:29 +0000824 match_ip_address_cmd,
paul73ffb252003-04-19 15:49:49 +0000825 "match ip address (<1-199>|<1300-2699>|WORD)",
paul718e3742002-12-13 20:15:29 +0000826 MATCH_STR
827 IP_STR
828 "Match address of route\n"
paul73ffb252003-04-19 15:49:49 +0000829 "IP access-list number\n"
830 "IP access-list number (expanded range)\n"
831 "IP Access-list name\n")
832
paul718e3742002-12-13 20:15:29 +0000833{
834 return rip_route_match_add (vty, vty->index, "ip address", argv[0]);
835}
836
837DEFUN (no_match_ip_address,
838 no_match_ip_address_cmd,
839 "no match ip address",
840 NO_STR
841 MATCH_STR
842 IP_STR
843 "Match address of route\n")
844{
845 if (argc == 0)
846 return rip_route_match_delete (vty, vty->index, "ip address", NULL);
847
848 return rip_route_match_delete (vty, vty->index, "ip address", argv[0]);
849}
850
paul73ffb252003-04-19 15:49:49 +0000851ALIAS (no_match_ip_address,
paul718e3742002-12-13 20:15:29 +0000852 no_match_ip_address_val_cmd,
paul73ffb252003-04-19 15:49:49 +0000853 "no match ip address (<1-199>|<1300-2699>|WORD)",
paul718e3742002-12-13 20:15:29 +0000854 NO_STR
855 MATCH_STR
856 IP_STR
857 "Match address of route\n"
paul73ffb252003-04-19 15:49:49 +0000858 "IP access-list number\n"
859 "IP access-list number (expanded range)\n"
860 "IP Access-list name\n")
paul718e3742002-12-13 20:15:29 +0000861
862DEFUN (match_ip_address_prefix_list,
863 match_ip_address_prefix_list_cmd,
864 "match ip address prefix-list WORD",
865 MATCH_STR
866 IP_STR
867 "Match address of route\n"
868 "Match entries of prefix-lists\n"
869 "IP prefix-list name\n")
870{
871 return rip_route_match_add (vty, vty->index, "ip address prefix-list", argv[0]);
872}
873
874DEFUN (no_match_ip_address_prefix_list,
875 no_match_ip_address_prefix_list_cmd,
876 "no match ip address prefix-list",
877 NO_STR
878 MATCH_STR
879 IP_STR
880 "Match address of route\n"
881 "Match entries of prefix-lists\n")
882{
883 if (argc == 0)
884 return rip_route_match_delete (vty, vty->index, "ip address prefix-list", NULL);
885
886 return rip_route_match_delete (vty, vty->index, "ip address prefix-list", argv[0]);
887}
888
889ALIAS (no_match_ip_address_prefix_list,
890 no_match_ip_address_prefix_list_val_cmd,
891 "no match ip address prefix-list WORD",
892 NO_STR
893 MATCH_STR
894 IP_STR
895 "Match address of route\n"
896 "Match entries of prefix-lists\n"
897 "IP prefix-list name\n")
898
hasso16705132003-05-25 14:49:19 +0000899DEFUN (match_tag,
900 match_tag_cmd,
Christian Frankeddc160c2016-10-01 20:42:34 +0200901 "match tag <1-4294967295>",
hasso16705132003-05-25 14:49:19 +0000902 MATCH_STR
903 "Match tag of route\n"
904 "Metric value\n")
905{
906 return rip_route_match_add (vty, vty->index, "tag", argv[0]);
907}
908
909DEFUN (no_match_tag,
910 no_match_tag_cmd,
911 "no match tag",
912 NO_STR
913 MATCH_STR
914 "Match tag of route\n")
915{
916 if (argc == 0)
917 return rip_route_match_delete (vty, vty->index, "tag", NULL);
918
919 return rip_route_match_delete (vty, vty->index, "tag", argv[0]);
920}
921
922ALIAS (no_match_tag,
923 no_match_tag_val_cmd,
Christian Frankeddc160c2016-10-01 20:42:34 +0200924 "no match tag <1-4294967295>",
hasso16705132003-05-25 14:49:19 +0000925 NO_STR
926 MATCH_STR
927 "Match tag of route\n"
928 "Metric value\n")
929
paul718e3742002-12-13 20:15:29 +0000930/* set functions */
931
932DEFUN (set_metric,
933 set_metric_cmd,
hassodd55f9e2003-05-25 16:02:39 +0000934 "set metric <0-4294967295>",
paul718e3742002-12-13 20:15:29 +0000935 SET_STR
936 "Metric value for destination routing protocol\n"
hassodd55f9e2003-05-25 16:02:39 +0000937 "Metric value\n")
paul718e3742002-12-13 20:15:29 +0000938{
939 return rip_route_set_add (vty, vty->index, "metric", argv[0]);
940}
941
hassodd55f9e2003-05-25 16:02:39 +0000942ALIAS (set_metric,
943 set_metric_addsub_cmd,
944 "set metric <+/-metric>",
945 SET_STR
946 "Metric value for destination routing protocol\n"
hasso033e8612005-05-28 04:50:54 +0000947 "Add or subtract metric\n")
hassodd55f9e2003-05-25 16:02:39 +0000948
paul718e3742002-12-13 20:15:29 +0000949DEFUN (no_set_metric,
950 no_set_metric_cmd,
951 "no set metric",
952 NO_STR
953 SET_STR
954 "Metric value for destination routing protocol\n")
955{
956 if (argc == 0)
957 return rip_route_set_delete (vty, vty->index, "metric", NULL);
958
959 return rip_route_set_delete (vty, vty->index, "metric", argv[0]);
960}
961
962ALIAS (no_set_metric,
963 no_set_metric_val_cmd,
Daniel Walton62f936e2015-11-09 20:21:59 -0500964 "no set metric <0-4294967295>",
paul718e3742002-12-13 20:15:29 +0000965 NO_STR
966 SET_STR
967 "Metric value for destination routing protocol\n"
Daniel Walton62f936e2015-11-09 20:21:59 -0500968 "Metric value\n")
969
970ALIAS (no_set_metric,
971 no_set_metric_addsub_cmd,
972 "no set metric <+/-metric>",
973 NO_STR
974 SET_STR
975 "Metric value for destination routing protocol\n"
hasso16705132003-05-25 14:49:19 +0000976 "Add or subtract metric\n")
paul718e3742002-12-13 20:15:29 +0000977
978DEFUN (set_ip_nexthop,
979 set_ip_nexthop_cmd,
980 "set ip next-hop A.B.C.D",
981 SET_STR
982 IP_STR
983 "Next hop address\n"
984 "IP address of next hop\n")
985{
986 union sockunion su;
987 int ret;
988
989 ret = str2sockunion (argv[0], &su);
990 if (ret < 0)
991 {
992 vty_out (vty, "%% Malformed next-hop address%s", VTY_NEWLINE);
993 return CMD_WARNING;
994 }
995
996 return rip_route_set_add (vty, vty->index, "ip next-hop", argv[0]);
997}
998
999DEFUN (no_set_ip_nexthop,
1000 no_set_ip_nexthop_cmd,
1001 "no set ip next-hop",
1002 NO_STR
1003 SET_STR
1004 IP_STR
1005 "Next hop address\n")
1006{
1007 if (argc == 0)
1008 return rip_route_set_delete (vty, vty->index, "ip next-hop", NULL);
1009
1010 return rip_route_set_delete (vty, vty->index, "ip next-hop", argv[0]);
1011}
1012
1013ALIAS (no_set_ip_nexthop,
1014 no_set_ip_nexthop_val_cmd,
1015 "no set ip next-hop A.B.C.D",
1016 NO_STR
1017 SET_STR
1018 IP_STR
1019 "Next hop address\n"
1020 "IP address of next hop\n")
1021
hasso16705132003-05-25 14:49:19 +00001022DEFUN (set_tag,
1023 set_tag_cmd,
Christian Frankeddc160c2016-10-01 20:42:34 +02001024 "set tag <1-4294967295>",
hasso16705132003-05-25 14:49:19 +00001025 SET_STR
1026 "Tag value for routing protocol\n"
1027 "Tag value\n")
1028{
1029 return rip_route_set_add (vty, vty->index, "tag", argv[0]);
1030}
1031
1032DEFUN (no_set_tag,
1033 no_set_tag_cmd,
1034 "no set tag",
1035 NO_STR
1036 SET_STR
1037 "Tag value for routing protocol\n")
1038{
1039 if (argc == 0)
1040 return rip_route_set_delete (vty, vty->index, "tag", NULL);
1041
1042 return rip_route_set_delete (vty, vty->index, "tag", argv[0]);
1043}
1044
1045ALIAS (no_set_tag,
1046 no_set_tag_val_cmd,
Christian Frankeddc160c2016-10-01 20:42:34 +02001047 "no set tag <1-4294967295>",
hasso16705132003-05-25 14:49:19 +00001048 NO_STR
1049 SET_STR
1050 "Tag value for routing protocol\n"
1051 "Tag value\n")
1052
paul718e3742002-12-13 20:15:29 +00001053void
1054rip_route_map_reset ()
1055{
1056 ;
1057}
1058
1059/* Route-map init */
1060void
1061rip_route_map_init ()
1062{
1063 route_map_init ();
1064 route_map_init_vty ();
1065 route_map_add_hook (rip_route_map_update);
1066 route_map_delete_hook (rip_route_map_update);
1067
1068 route_map_install_match (&route_match_metric_cmd);
1069 route_map_install_match (&route_match_interface_cmd);
1070 route_map_install_match (&route_match_ip_next_hop_cmd);
1071 route_map_install_match (&route_match_ip_next_hop_prefix_list_cmd);
1072 route_map_install_match (&route_match_ip_address_cmd);
1073 route_map_install_match (&route_match_ip_address_prefix_list_cmd);
hasso16705132003-05-25 14:49:19 +00001074 route_map_install_match (&route_match_tag_cmd);
paul718e3742002-12-13 20:15:29 +00001075
1076 route_map_install_set (&route_set_metric_cmd);
1077 route_map_install_set (&route_set_ip_nexthop_cmd);
hasso16705132003-05-25 14:49:19 +00001078 route_map_install_set (&route_set_tag_cmd);
paul718e3742002-12-13 20:15:29 +00001079
1080 install_element (RMAP_NODE, &match_metric_cmd);
1081 install_element (RMAP_NODE, &no_match_metric_cmd);
1082 install_element (RMAP_NODE, &no_match_metric_val_cmd);
1083 install_element (RMAP_NODE, &match_interface_cmd);
1084 install_element (RMAP_NODE, &no_match_interface_cmd);
1085 install_element (RMAP_NODE, &no_match_interface_val_cmd);
1086 install_element (RMAP_NODE, &match_ip_next_hop_cmd);
1087 install_element (RMAP_NODE, &no_match_ip_next_hop_cmd);
1088 install_element (RMAP_NODE, &no_match_ip_next_hop_val_cmd);
1089 install_element (RMAP_NODE, &match_ip_next_hop_prefix_list_cmd);
1090 install_element (RMAP_NODE, &no_match_ip_next_hop_prefix_list_cmd);
1091 install_element (RMAP_NODE, &no_match_ip_next_hop_prefix_list_val_cmd);
1092 install_element (RMAP_NODE, &match_ip_address_cmd);
1093 install_element (RMAP_NODE, &no_match_ip_address_cmd);
1094 install_element (RMAP_NODE, &no_match_ip_address_val_cmd);
1095 install_element (RMAP_NODE, &match_ip_address_prefix_list_cmd);
1096 install_element (RMAP_NODE, &no_match_ip_address_prefix_list_cmd);
1097 install_element (RMAP_NODE, &no_match_ip_address_prefix_list_val_cmd);
hasso16705132003-05-25 14:49:19 +00001098 install_element (RMAP_NODE, &match_tag_cmd);
1099 install_element (RMAP_NODE, &no_match_tag_cmd);
1100 install_element (RMAP_NODE, &no_match_tag_val_cmd);
paul718e3742002-12-13 20:15:29 +00001101
1102 install_element (RMAP_NODE, &set_metric_cmd);
hassodd55f9e2003-05-25 16:02:39 +00001103 install_element (RMAP_NODE, &set_metric_addsub_cmd);
paul718e3742002-12-13 20:15:29 +00001104 install_element (RMAP_NODE, &no_set_metric_cmd);
1105 install_element (RMAP_NODE, &no_set_metric_val_cmd);
Daniel Walton62f936e2015-11-09 20:21:59 -05001106 install_element (RMAP_NODE, &no_set_metric_addsub_cmd);
paul718e3742002-12-13 20:15:29 +00001107 install_element (RMAP_NODE, &set_ip_nexthop_cmd);
1108 install_element (RMAP_NODE, &no_set_ip_nexthop_cmd);
1109 install_element (RMAP_NODE, &no_set_ip_nexthop_val_cmd);
hasso16705132003-05-25 14:49:19 +00001110 install_element (RMAP_NODE, &set_tag_cmd);
1111 install_element (RMAP_NODE, &no_set_tag_cmd);
1112 install_element (RMAP_NODE, &no_set_tag_val_cmd);
paul718e3742002-12-13 20:15:29 +00001113}