]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_routemap.c
bgpd: fix large route-distinguisher's format
[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 next-hop type <blackhole>' */
235
236 static route_map_result_t
237 route_match_ip_next_hop_type(void *rule, const struct prefix *prefix,
238 route_map_object_t type, void *object)
239 {
240 struct rip_info *rinfo;
241
242 if (type == RMAP_RIP && prefix->family == AF_INET) {
243 rinfo = (struct rip_info *)object;
244 if (!rinfo)
245 return RMAP_DENYMATCH;
246
247 if (rinfo->nh.type == NEXTHOP_TYPE_BLACKHOLE)
248 return RMAP_MATCH;
249 }
250 return RMAP_NOMATCH;
251 }
252
253 static void *route_match_ip_next_hop_type_compile(const char *arg)
254 {
255 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
256 }
257
258 static void route_match_ip_next_hop_type_free(void *rule)
259 {
260 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
261 }
262
263 static struct route_map_rule_cmd route_match_ip_next_hop_type_cmd = {
264 "ip next-hop type", route_match_ip_next_hop_type,
265 route_match_ip_next_hop_type_compile,
266 route_match_ip_next_hop_type_free};
267
268 /* `match ip address IP_ACCESS_LIST' */
269
270 /* Match function should return 1 if match is success else return
271 zero. */
272 static route_map_result_t route_match_ip_address(void *rule,
273 const struct prefix *prefix,
274 route_map_object_t type,
275 void *object)
276 {
277 struct access_list *alist;
278
279 if (type == RMAP_RIP) {
280 alist = access_list_lookup(AFI_IP, (char *)rule);
281 if (alist == NULL)
282 return RMAP_NOMATCH;
283
284 return (access_list_apply(alist, prefix) == FILTER_DENY
285 ? RMAP_NOMATCH
286 : RMAP_MATCH);
287 }
288 return RMAP_NOMATCH;
289 }
290
291 /* Route map `ip address' match statement. `arg' should be
292 access-list name. */
293 static void *route_match_ip_address_compile(const char *arg)
294 {
295 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
296 }
297
298 /* Free route map's compiled `ip address' value. */
299 static void route_match_ip_address_free(void *rule)
300 {
301 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
302 }
303
304 /* Route map commands for ip address matching. */
305 static struct route_map_rule_cmd route_match_ip_address_cmd = {
306 "ip address", route_match_ip_address, route_match_ip_address_compile,
307 route_match_ip_address_free};
308
309 /* `match ip address prefix-list PREFIX_LIST' */
310
311 static route_map_result_t
312 route_match_ip_address_prefix_list(void *rule, const struct prefix *prefix,
313 route_map_object_t type, void *object)
314 {
315 struct prefix_list *plist;
316
317 if (type == RMAP_RIP) {
318 plist = prefix_list_lookup(AFI_IP, (char *)rule);
319 if (plist == NULL)
320 return RMAP_NOMATCH;
321
322 return (prefix_list_apply(plist, prefix) == PREFIX_DENY
323 ? RMAP_NOMATCH
324 : RMAP_MATCH);
325 }
326 return RMAP_NOMATCH;
327 }
328
329 static void *route_match_ip_address_prefix_list_compile(const char *arg)
330 {
331 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
332 }
333
334 static void route_match_ip_address_prefix_list_free(void *rule)
335 {
336 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
337 }
338
339 static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd = {
340 "ip address prefix-list", route_match_ip_address_prefix_list,
341 route_match_ip_address_prefix_list_compile,
342 route_match_ip_address_prefix_list_free};
343
344 /* `match tag TAG' */
345 /* Match function return 1 if match is success else return zero. */
346 static route_map_result_t route_match_tag(void *rule, const struct prefix *p,
347 route_map_object_t type, void *object)
348 {
349 route_tag_t *tag;
350 struct rip_info *rinfo;
351 route_tag_t rinfo_tag;
352
353 if (type == RMAP_RIP) {
354 tag = rule;
355 rinfo = object;
356
357 /* The information stored by rinfo is host ordered. */
358 rinfo_tag = rinfo->tag;
359 if (rinfo_tag == *tag)
360 return RMAP_MATCH;
361 else
362 return RMAP_NOMATCH;
363 }
364 return RMAP_NOMATCH;
365 }
366
367 /* Route map commands for tag matching. */
368 static struct route_map_rule_cmd route_match_tag_cmd = {
369 "tag", route_match_tag, route_map_rule_tag_compile,
370 route_map_rule_tag_free,
371 };
372
373 /* `set metric METRIC' */
374
375 /* Set metric to attribute. */
376 static route_map_result_t route_set_metric(void *rule,
377 const struct prefix *prefix,
378 route_map_object_t type,
379 void *object)
380 {
381 if (type == RMAP_RIP) {
382 struct rip_metric_modifier *mod;
383 struct rip_info *rinfo;
384
385 mod = rule;
386 rinfo = object;
387
388 if (!mod->used)
389 return RMAP_OKAY;
390
391 if (mod->type == metric_increment)
392 rinfo->metric_out += mod->metric;
393 else if (mod->type == metric_decrement)
394 rinfo->metric_out -= mod->metric;
395 else if (mod->type == metric_absolute)
396 rinfo->metric_out = mod->metric;
397
398 if ((signed int)rinfo->metric_out < 1)
399 rinfo->metric_out = 1;
400 if (rinfo->metric_out > RIP_METRIC_INFINITY)
401 rinfo->metric_out = RIP_METRIC_INFINITY;
402
403 rinfo->metric_set = 1;
404 }
405 return RMAP_OKAY;
406 }
407
408 /* set metric compilation. */
409 static void *route_set_metric_compile(const char *arg)
410 {
411 int len;
412 const char *pnt;
413 long metric;
414 char *endptr = NULL;
415 struct rip_metric_modifier *mod;
416
417 mod = XMALLOC(MTYPE_ROUTE_MAP_COMPILED,
418 sizeof(struct rip_metric_modifier));
419 mod->used = false;
420
421 len = strlen(arg);
422 pnt = arg;
423
424 if (len == 0)
425 return mod;
426
427 /* Examine first character. */
428 if (arg[0] == '+') {
429 mod->type = metric_increment;
430 pnt++;
431 } else if (arg[0] == '-') {
432 mod->type = metric_decrement;
433 pnt++;
434 } else
435 mod->type = metric_absolute;
436
437 /* Check beginning with digit string. */
438 if (*pnt < '0' || *pnt > '9')
439 return mod;
440
441 /* Convert string to integer. */
442 metric = strtol(pnt, &endptr, 10);
443
444 if (*endptr != '\0' || metric < 0) {
445 return mod;
446 }
447 if (metric > RIP_METRIC_INFINITY) {
448 zlog_info(
449 "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead",
450 __PRETTY_FUNCTION__, metric);
451 mod->metric = RIP_METRIC_INFINITY;
452 } else
453 mod->metric = metric;
454
455 mod->used = true;
456
457 return mod;
458 }
459
460 /* Free route map's compiled `set metric' value. */
461 static void route_set_metric_free(void *rule)
462 {
463 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
464 }
465
466 /* Set metric rule structure. */
467 static struct route_map_rule_cmd route_set_metric_cmd = {
468 "metric", route_set_metric, route_set_metric_compile,
469 route_set_metric_free,
470 };
471
472 /* `set ip next-hop IP_ADDRESS' */
473
474 /* Set nexthop to object. ojbect must be pointer to struct attr. */
475 static route_map_result_t route_set_ip_nexthop(void *rule,
476 const struct prefix *prefix,
477 route_map_object_t type,
478 void *object)
479 {
480 struct in_addr *address;
481 struct rip_info *rinfo;
482
483 if (type == RMAP_RIP) {
484 /* Fetch routemap's rule information. */
485 address = rule;
486 rinfo = object;
487
488 /* Set next hop value. */
489 rinfo->nexthop_out = *address;
490 }
491
492 return RMAP_OKAY;
493 }
494
495 /* Route map `ip nexthop' compile function. Given string is converted
496 to struct in_addr structure. */
497 static void *route_set_ip_nexthop_compile(const char *arg)
498 {
499 int ret;
500 struct in_addr *address;
501
502 address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr));
503
504 ret = inet_aton(arg, address);
505
506 if (ret == 0) {
507 XFREE(MTYPE_ROUTE_MAP_COMPILED, address);
508 return NULL;
509 }
510
511 return address;
512 }
513
514 /* Free route map's compiled `ip nexthop' value. */
515 static void route_set_ip_nexthop_free(void *rule)
516 {
517 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
518 }
519
520 /* Route map commands for ip nexthop set. */
521 static struct route_map_rule_cmd route_set_ip_nexthop_cmd = {
522 "ip next-hop", route_set_ip_nexthop, route_set_ip_nexthop_compile,
523 route_set_ip_nexthop_free};
524
525 /* `set tag TAG' */
526
527 /* Set tag to object. ojbect must be pointer to struct attr. */
528 static route_map_result_t route_set_tag(void *rule, const struct prefix *prefix,
529 route_map_object_t type, void *object)
530 {
531 route_tag_t *tag;
532 struct rip_info *rinfo;
533
534 if (type == RMAP_RIP) {
535 /* Fetch routemap's rule information. */
536 tag = rule;
537 rinfo = object;
538
539 /* Set next hop value. */
540 rinfo->tag_out = *tag;
541 }
542
543 return RMAP_OKAY;
544 }
545
546 /* Route map commands for tag set. */
547 static struct route_map_rule_cmd route_set_tag_cmd = {
548 "tag", route_set_tag, route_map_rule_tag_compile,
549 route_map_rule_tag_free};
550
551 #define MATCH_STR "Match values from routing table\n"
552 #define SET_STR "Set values in destination routing protocol\n"
553
554 /* Route-map init */
555 void rip_route_map_init(void)
556 {
557 route_map_init();
558
559 route_map_match_interface_hook(generic_match_add);
560 route_map_no_match_interface_hook(generic_match_delete);
561
562 route_map_match_ip_address_hook(generic_match_add);
563 route_map_no_match_ip_address_hook(generic_match_delete);
564
565 route_map_match_ip_address_prefix_list_hook(generic_match_add);
566 route_map_no_match_ip_address_prefix_list_hook(generic_match_delete);
567
568 route_map_match_ip_next_hop_hook(generic_match_add);
569 route_map_no_match_ip_next_hop_hook(generic_match_delete);
570
571 route_map_match_ip_next_hop_prefix_list_hook(generic_match_add);
572 route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete);
573
574 route_map_match_ip_next_hop_type_hook(generic_match_add);
575 route_map_no_match_ip_next_hop_type_hook(generic_match_delete);
576
577 route_map_match_metric_hook(generic_match_add);
578 route_map_no_match_metric_hook(generic_match_delete);
579
580 route_map_match_tag_hook(generic_match_add);
581 route_map_no_match_tag_hook(generic_match_delete);
582
583 route_map_set_ip_nexthop_hook(generic_set_add);
584 route_map_no_set_ip_nexthop_hook(generic_set_delete);
585
586 route_map_set_metric_hook(generic_set_add);
587 route_map_no_set_metric_hook(generic_set_delete);
588
589 route_map_set_tag_hook(generic_set_add);
590 route_map_no_set_tag_hook(generic_set_delete);
591
592 route_map_install_match(&route_match_metric_cmd);
593 route_map_install_match(&route_match_interface_cmd);
594 route_map_install_match(&route_match_ip_next_hop_cmd);
595 route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd);
596 route_map_install_match(&route_match_ip_next_hop_type_cmd);
597 route_map_install_match(&route_match_ip_address_cmd);
598 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
599 route_map_install_match(&route_match_tag_cmd);
600
601 route_map_install_set(&route_set_metric_cmd);
602 route_map_install_set(&route_set_ip_nexthop_cmd);
603 route_map_install_set(&route_set_tag_cmd);
604 }