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