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