]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_routemap.c
staticd: Do not ready prefix for printing till it's decoded
[mirror_frr.git] / ripd / rip_routemap.c
1 /* RIPv2 routemap.
2 * Copyright (C) 2005 6WIND <alain.ritoux@6wind.com>
3 * 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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "memory.h"
25 #include "prefix.h"
26 #include "vty.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 #include "vrf.h"
34
35 #include "ripd/ripd.h"
36
37 struct rip_metric_modifier {
38 enum { metric_increment, metric_decrement, metric_absolute } type;
39 bool used;
40 uint8_t metric;
41 };
42
43 /* `match metric METRIC' */
44 /* Match function return 1 if match is success else return zero. */
45 static route_map_result_t route_match_metric(void *rule,
46 const struct prefix *prefix,
47 route_map_object_t type,
48 void *object)
49 {
50 uint32_t *metric;
51 uint32_t check;
52 struct rip_info *rinfo;
53
54 if (type == RMAP_RIP) {
55 metric = rule;
56 rinfo = object;
57
58 /* If external metric is available, the route-map should
59 work on this one (for redistribute purpose) */
60 check = (rinfo->external_metric) ? rinfo->external_metric
61 : rinfo->metric;
62 if (check == *metric)
63 return RMAP_MATCH;
64 else
65 return RMAP_NOMATCH;
66 }
67 return RMAP_NOMATCH;
68 }
69
70 /* Route map `match metric' match statement. `arg' is METRIC value */
71 static void *route_match_metric_compile(const char *arg)
72 {
73 uint32_t *metric;
74
75 metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t));
76 *metric = atoi(arg);
77
78 if (*metric > 0)
79 return metric;
80
81 XFREE(MTYPE_ROUTE_MAP_COMPILED, metric);
82 return NULL;
83 }
84
85 /* Free route map's compiled `match metric' value. */
86 static void route_match_metric_free(void *rule)
87 {
88 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
89 }
90
91 /* Route map commands for metric matching. */
92 struct route_map_rule_cmd route_match_metric_cmd = {
93 "metric", route_match_metric, route_match_metric_compile,
94 route_match_metric_free};
95
96 /* `match interface IFNAME' */
97 /* Match function return 1 if match is success else return zero. */
98 static route_map_result_t route_match_interface(void *rule,
99 const struct prefix *prefix,
100 route_map_object_t type,
101 void *object)
102 {
103 struct rip_info *rinfo;
104 struct interface *ifp;
105 char *ifname;
106
107 if (type == RMAP_RIP) {
108 ifname = rule;
109 ifp = if_lookup_by_name(ifname, VRF_DEFAULT);
110
111 if (!ifp)
112 return RMAP_NOMATCH;
113
114 rinfo = object;
115
116 if (rinfo->ifindex_out == ifp->ifindex
117 || rinfo->nh.ifindex == ifp->ifindex)
118 return RMAP_MATCH;
119 else
120 return RMAP_NOMATCH;
121 }
122 return RMAP_NOMATCH;
123 }
124
125 /* Route map `match interface' match statement. `arg' is IFNAME value */
126 /* XXX I don`t know if I need to check does interface exist? */
127 static void *route_match_interface_compile(const char *arg)
128 {
129 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
130 }
131
132 /* Free route map's compiled `match interface' value. */
133 static void route_match_interface_free(void *rule)
134 {
135 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
136 }
137
138 /* Route map commands for interface matching. */
139 struct route_map_rule_cmd route_match_interface_cmd = {
140 "interface", route_match_interface, route_match_interface_compile,
141 route_match_interface_free};
142
143 /* `match ip next-hop IP_ACCESS_LIST' */
144
145 /* Match function return 1 if match is success else return zero. */
146 static route_map_result_t route_match_ip_next_hop(void *rule,
147 const struct prefix *prefix,
148 route_map_object_t type,
149 void *object)
150 {
151 struct access_list *alist;
152 struct rip_info *rinfo;
153 struct prefix_ipv4 p;
154
155 if (type == RMAP_RIP) {
156 rinfo = object;
157 p.family = AF_INET;
158 p.prefix = (rinfo->nh.gate.ipv4.s_addr) ? rinfo->nh.gate.ipv4
159 : rinfo->from;
160 p.prefixlen = IPV4_MAX_BITLEN;
161
162 alist = access_list_lookup(AFI_IP, (char *)rule);
163 if (alist == NULL)
164 return RMAP_NOMATCH;
165
166 return (access_list_apply(alist, &p) == FILTER_DENY
167 ? RMAP_NOMATCH
168 : RMAP_MATCH);
169 }
170 return RMAP_NOMATCH;
171 }
172
173 /* Route map `ip next-hop' match statement. `arg' should be
174 access-list name. */
175 static void *route_match_ip_next_hop_compile(const char *arg)
176 {
177 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
178 }
179
180 /* Free route map's compiled `. */
181 static void route_match_ip_next_hop_free(void *rule)
182 {
183 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
184 }
185
186 /* Route map commands for ip next-hop matching. */
187 static struct route_map_rule_cmd route_match_ip_next_hop_cmd = {
188 "ip next-hop", route_match_ip_next_hop, route_match_ip_next_hop_compile,
189 route_match_ip_next_hop_free};
190
191 /* `match ip next-hop prefix-list PREFIX_LIST' */
192
193 static route_map_result_t
194 route_match_ip_next_hop_prefix_list(void *rule, const struct prefix *prefix,
195 route_map_object_t type, void *object)
196 {
197 struct prefix_list *plist;
198 struct rip_info *rinfo;
199 struct prefix_ipv4 p;
200
201 if (type == RMAP_RIP) {
202 rinfo = object;
203 p.family = AF_INET;
204 p.prefix = (rinfo->nh.gate.ipv4.s_addr) ? rinfo->nh.gate.ipv4
205 : rinfo->from;
206 p.prefixlen = IPV4_MAX_BITLEN;
207
208 plist = prefix_list_lookup(AFI_IP, (char *)rule);
209 if (plist == NULL)
210 return RMAP_NOMATCH;
211
212 return (prefix_list_apply(plist, &p) == PREFIX_DENY
213 ? RMAP_NOMATCH
214 : RMAP_MATCH);
215 }
216 return RMAP_NOMATCH;
217 }
218
219 static void *route_match_ip_next_hop_prefix_list_compile(const char *arg)
220 {
221 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
222 }
223
224 static void route_match_ip_next_hop_prefix_list_free(void *rule)
225 {
226 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
227 }
228
229 static struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd = {
230 "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list,
231 route_match_ip_next_hop_prefix_list_compile,
232 route_match_ip_next_hop_prefix_list_free};
233
234 /* `match ip address IP_ACCESS_LIST' */
235
236 /* Match function should return 1 if match is success else return
237 zero. */
238 static route_map_result_t route_match_ip_address(void *rule,
239 const struct prefix *prefix,
240 route_map_object_t type,
241 void *object)
242 {
243 struct access_list *alist;
244
245 if (type == RMAP_RIP) {
246 alist = access_list_lookup(AFI_IP, (char *)rule);
247 if (alist == NULL)
248 return RMAP_NOMATCH;
249
250 return (access_list_apply(alist, prefix) == FILTER_DENY
251 ? RMAP_NOMATCH
252 : RMAP_MATCH);
253 }
254 return RMAP_NOMATCH;
255 }
256
257 /* Route map `ip address' match statement. `arg' should be
258 access-list name. */
259 static void *route_match_ip_address_compile(const char *arg)
260 {
261 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
262 }
263
264 /* Free route map's compiled `ip address' value. */
265 static void route_match_ip_address_free(void *rule)
266 {
267 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
268 }
269
270 /* Route map commands for ip address matching. */
271 static struct route_map_rule_cmd route_match_ip_address_cmd = {
272 "ip address", route_match_ip_address, route_match_ip_address_compile,
273 route_match_ip_address_free};
274
275 /* `match ip address prefix-list PREFIX_LIST' */
276
277 static route_map_result_t
278 route_match_ip_address_prefix_list(void *rule, const struct prefix *prefix,
279 route_map_object_t type, void *object)
280 {
281 struct prefix_list *plist;
282
283 if (type == RMAP_RIP) {
284 plist = prefix_list_lookup(AFI_IP, (char *)rule);
285 if (plist == NULL)
286 return RMAP_NOMATCH;
287
288 return (prefix_list_apply(plist, prefix) == PREFIX_DENY
289 ? RMAP_NOMATCH
290 : RMAP_MATCH);
291 }
292 return RMAP_NOMATCH;
293 }
294
295 static void *route_match_ip_address_prefix_list_compile(const char *arg)
296 {
297 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
298 }
299
300 static void route_match_ip_address_prefix_list_free(void *rule)
301 {
302 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
303 }
304
305 static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd = {
306 "ip address prefix-list", route_match_ip_address_prefix_list,
307 route_match_ip_address_prefix_list_compile,
308 route_match_ip_address_prefix_list_free};
309
310 /* `match tag TAG' */
311 /* Match function return 1 if match is success else return zero. */
312 static route_map_result_t route_match_tag(void *rule, const struct prefix *p,
313 route_map_object_t type, void *object)
314 {
315 route_tag_t *tag;
316 struct rip_info *rinfo;
317 route_tag_t rinfo_tag;
318
319 if (type == RMAP_RIP) {
320 tag = rule;
321 rinfo = object;
322
323 /* The information stored by rinfo is host ordered. */
324 rinfo_tag = rinfo->tag;
325 if (rinfo_tag == *tag)
326 return RMAP_MATCH;
327 else
328 return RMAP_NOMATCH;
329 }
330 return RMAP_NOMATCH;
331 }
332
333 /* Route map commands for tag matching. */
334 static struct route_map_rule_cmd route_match_tag_cmd = {
335 "tag", route_match_tag, route_map_rule_tag_compile,
336 route_map_rule_tag_free,
337 };
338
339 /* `set metric METRIC' */
340
341 /* Set metric to attribute. */
342 static route_map_result_t route_set_metric(void *rule,
343 const struct prefix *prefix,
344 route_map_object_t type,
345 void *object)
346 {
347 if (type == RMAP_RIP) {
348 struct rip_metric_modifier *mod;
349 struct rip_info *rinfo;
350
351 mod = rule;
352 rinfo = object;
353
354 if (!mod->used)
355 return RMAP_OKAY;
356
357 if (mod->type == metric_increment)
358 rinfo->metric_out += mod->metric;
359 else if (mod->type == metric_decrement)
360 rinfo->metric_out -= mod->metric;
361 else if (mod->type == metric_absolute)
362 rinfo->metric_out = mod->metric;
363
364 if ((signed int)rinfo->metric_out < 1)
365 rinfo->metric_out = 1;
366 if (rinfo->metric_out > RIP_METRIC_INFINITY)
367 rinfo->metric_out = RIP_METRIC_INFINITY;
368
369 rinfo->metric_set = 1;
370 }
371 return RMAP_OKAY;
372 }
373
374 /* set metric compilation. */
375 static void *route_set_metric_compile(const char *arg)
376 {
377 int len;
378 const char *pnt;
379 long metric;
380 char *endptr = NULL;
381 struct rip_metric_modifier *mod;
382
383 mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
384 sizeof(struct rip_metric_modifier));
385 mod->used = false;
386
387 len = strlen(arg);
388 pnt = arg;
389
390 if (len == 0)
391 return mod;
392
393 /* Examine first character. */
394 if (arg[0] == '+') {
395 mod->type = metric_increment;
396 pnt++;
397 } else if (arg[0] == '-') {
398 mod->type = metric_decrement;
399 pnt++;
400 } else
401 mod->type = metric_absolute;
402
403 /* Check beginning with digit string. */
404 if (*pnt < '0' || *pnt > '9')
405 return mod;
406
407 /* Convert string to integer. */
408 metric = strtol(pnt, &endptr, 10);
409
410 if (*endptr != '\0' || metric < 0) {
411 return mod;
412 }
413 if (metric > RIP_METRIC_INFINITY) {
414 zlog_info(
415 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
416 __PRETTY_FUNCTION__, metric);
417 mod->metric = RIP_METRIC_INFINITY;
418 } else
419 mod->metric = metric;
420
421 mod->used = true;
422
423 return mod;
424 }
425
426 /* Free route map's compiled `set metric' value. */
427 static void route_set_metric_free(void *rule)
428 {
429 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
430 }
431
432 /* Set metric rule structure. */
433 static struct route_map_rule_cmd route_set_metric_cmd = {
434 "metric", route_set_metric, route_set_metric_compile,
435 route_set_metric_free,
436 };
437
438 /* `set ip next-hop IP_ADDRESS' */
439
440 /* Set nexthop to object. ojbect must be pointer to struct attr. */
441 static route_map_result_t route_set_ip_nexthop(void *rule,
442 const struct prefix *prefix,
443 route_map_object_t type,
444 void *object)
445 {
446 struct in_addr *address;
447 struct rip_info *rinfo;
448
449 if (type == RMAP_RIP) {
450 /* Fetch routemap's rule information. */
451 address = rule;
452 rinfo = object;
453
454 /* Set next hop value. */
455 rinfo->nexthop_out = *address;
456 }
457
458 return RMAP_OKAY;
459 }
460
461 /* Route map `ip nexthop' compile function. Given string is converted
462 to struct in_addr structure. */
463 static void *route_set_ip_nexthop_compile(const char *arg)
464 {
465 int ret;
466 struct in_addr *address;
467
468 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
469
470 ret = inet_aton(arg, address);
471
472 if (ret == 0) {
473 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
474 return NULL;
475 }
476
477 return address;
478 }
479
480 /* Free route map's compiled `ip nexthop' value. */
481 static void route_set_ip_nexthop_free(void *rule)
482 {
483 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
484 }
485
486 /* Route map commands for ip nexthop set. */
487 static struct route_map_rule_cmd route_set_ip_nexthop_cmd = {
488 "ip next-hop", route_set_ip_nexthop, route_set_ip_nexthop_compile,
489 route_set_ip_nexthop_free};
490
491 /* `set tag TAG' */
492
493 /* Set tag to object. ojbect must be pointer to struct attr. */
494 static route_map_result_t route_set_tag(void *rule, const struct prefix *prefix,
495 route_map_object_t type, void *object)
496 {
497 route_tag_t *tag;
498 struct rip_info *rinfo;
499
500 if (type == RMAP_RIP) {
501 /* Fetch routemap's rule information. */
502 tag = rule;
503 rinfo = object;
504
505 /* Set next hop value. */
506 rinfo->tag_out = *tag;
507 }
508
509 return RMAP_OKAY;
510 }
511
512 /* Route map commands for tag set. */
513 static struct route_map_rule_cmd route_set_tag_cmd = {
514 "tag", route_set_tag, route_map_rule_tag_compile,
515 route_map_rule_tag_free};
516
517 #define MATCH_STR "Match values from routing table\n"
518 #define SET_STR "Set values in destination routing protocol\n"
519
520 /* Route-map init */
521 void rip_route_map_init()
522 {
523 route_map_init();
524
525 route_map_match_interface_hook(generic_match_add);
526 route_map_no_match_interface_hook(generic_match_delete);
527
528 route_map_match_ip_address_hook(generic_match_add);
529 route_map_no_match_ip_address_hook(generic_match_delete);
530
531 route_map_match_ip_address_prefix_list_hook(generic_match_add);
532 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete);
533
534 route_map_match_ip_next_hop_hook(generic_match_add);
535 route_map_no_match_ip_next_hop_hook(generic_match_delete);
536
537 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
538 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
539
540 route_map_match_metric_hook(generic_match_add);
541 route_map_no_match_metric_hook(generic_match_delete);
542
543 route_map_match_tag_hook(generic_match_add);
544 route_map_no_match_tag_hook(generic_match_delete);
545
546 route_map_set_ip_nexthop_hook(generic_set_add);
547 route_map_no_set_ip_nexthop_hook(generic_set_delete);
548
549 route_map_set_metric_hook(generic_set_add);
550 route_map_no_set_metric_hook(generic_set_delete);
551
552 route_map_set_tag_hook(generic_set_add);
553 route_map_no_set_tag_hook(generic_set_delete);
554
555 route_map_install_match(&route_match_metric_cmd);
556 route_map_install_match(&route_match_interface_cmd);
557 route_map_install_match(&route_match_ip_next_hop_cmd);
558 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
559 route_map_install_match(&route_match_ip_address_cmd);
560 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
561 route_map_install_match(&route_match_tag_cmd);
562
563 route_map_install_set(&route_set_metric_cmd);
564 route_map_install_set(&route_set_ip_nexthop_cmd);
565 route_map_install_set(&route_set_tag_cmd);
566 }