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