]>
Commit | Line | Data |
---|---|---|
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 | /* 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, | |
62 | const struct prefix *prefix, | |
63 | route_map_object_t type, | |
64 | void *object) | |
65 | { | |
66 | uint32_t *metric; | |
67 | uint32_t check; | |
68 | struct rip_info *rinfo; | |
69 | ||
70 | if (type == RMAP_RIP) { | |
71 | metric = rule; | |
72 | rinfo = object; | |
73 | ||
74 | /* If external metric is available, the route-map should | |
75 | work on this one (for redistribute purpose) */ | |
76 | check = (rinfo->external_metric) ? rinfo->external_metric | |
77 | : rinfo->metric; | |
78 | if (check == *metric) | |
79 | return RMAP_MATCH; | |
80 | else | |
81 | return RMAP_NOMATCH; | |
82 | } | |
83 | return RMAP_NOMATCH; | |
84 | } | |
85 | ||
86 | /* Route map `match metric' match statement. `arg' is METRIC value */ | |
87 | static void *route_match_metric_compile(const char *arg) | |
88 | { | |
89 | uint32_t *metric; | |
90 | ||
91 | metric = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(uint32_t)); | |
92 | *metric = atoi(arg); | |
93 | ||
94 | if (*metric > 0) | |
95 | return metric; | |
96 | ||
97 | XFREE(MTYPE_ROUTE_MAP_COMPILED, metric); | |
98 | return NULL; | |
99 | } | |
100 | ||
101 | /* Free route map's compiled `match metric' value. */ | |
102 | static void route_match_metric_free(void *rule) | |
103 | { | |
104 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
105 | } | |
106 | ||
107 | /* Route map commands for metric matching. */ | |
108 | struct route_map_rule_cmd route_match_metric_cmd = { | |
109 | "metric", route_match_metric, route_match_metric_compile, | |
110 | route_match_metric_free}; | |
111 | ||
112 | /* `match interface IFNAME' */ | |
113 | /* Match function return 1 if match is success else return zero. */ | |
114 | static route_map_result_t route_match_interface(void *rule, | |
115 | const struct prefix *prefix, | |
116 | route_map_object_t type, | |
117 | void *object) | |
118 | { | |
119 | struct rip_info *rinfo; | |
120 | struct interface *ifp; | |
121 | char *ifname; | |
122 | ||
123 | if (type == RMAP_RIP) { | |
124 | ifname = rule; | |
125 | ifp = if_lookup_by_name(ifname, VRF_DEFAULT); | |
126 | ||
127 | if (!ifp) | |
128 | return RMAP_NOMATCH; | |
129 | ||
130 | rinfo = object; | |
131 | ||
132 | if (rinfo->ifindex_out == ifp->ifindex | |
133 | || rinfo->nh.ifindex == ifp->ifindex) | |
134 | return RMAP_MATCH; | |
135 | else | |
136 | return RMAP_NOMATCH; | |
137 | } | |
138 | return RMAP_NOMATCH; | |
139 | } | |
140 | ||
141 | /* Route map `match interface' match statement. `arg' is IFNAME value */ | |
142 | /* XXX I don`t know if I need to check does interface exist? */ | |
143 | static void *route_match_interface_compile(const char *arg) | |
144 | { | |
145 | return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg); | |
146 | } | |
147 | ||
148 | /* Free route map's compiled `match interface' value. */ | |
149 | static void route_match_interface_free(void *rule) | |
150 | { | |
151 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
152 | } | |
153 | ||
154 | /* Route map commands for interface matching. */ | |
155 | struct route_map_rule_cmd route_match_interface_cmd = { | |
156 | "interface", route_match_interface, route_match_interface_compile, | |
157 | route_match_interface_free}; | |
158 | ||
159 | /* `match ip next-hop IP_ACCESS_LIST' */ | |
160 | ||
161 | /* Match function return 1 if match is success else return zero. */ | |
162 | static route_map_result_t route_match_ip_next_hop(void *rule, | |
163 | const struct prefix *prefix, | |
164 | route_map_object_t type, | |
165 | void *object) | |
166 | { | |
167 | struct access_list *alist; | |
168 | struct rip_info *rinfo; | |
169 | struct prefix_ipv4 p; | |
170 | ||
171 | if (type == RMAP_RIP) { | |
172 | rinfo = object; | |
173 | p.family = AF_INET; | |
174 | p.prefix = (rinfo->nh.gate.ipv4.s_addr) ? rinfo->nh.gate.ipv4 | |
175 | : 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, const 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 = (rinfo->nh.gate.ipv4.s_addr) ? rinfo->nh.gate.ipv4 | |
221 | : rinfo->from; | |
222 | p.prefixlen = IPV4_MAX_BITLEN; | |
223 | ||
224 | plist = prefix_list_lookup(AFI_IP, (char *)rule); | |
225 | if (plist == NULL) | |
226 | return RMAP_NOMATCH; | |
227 | ||
228 | return (prefix_list_apply(plist, &p) == PREFIX_DENY | |
229 | ? RMAP_NOMATCH | |
230 | : RMAP_MATCH); | |
231 | } | |
232 | return RMAP_NOMATCH; | |
233 | } | |
234 | ||
235 | static void *route_match_ip_next_hop_prefix_list_compile(const char *arg) | |
236 | { | |
237 | return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg); | |
238 | } | |
239 | ||
240 | static void route_match_ip_next_hop_prefix_list_free(void *rule) | |
241 | { | |
242 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
243 | } | |
244 | ||
245 | static struct route_map_rule_cmd route_match_ip_next_hop_prefix_list_cmd = { | |
246 | "ip next-hop prefix-list", route_match_ip_next_hop_prefix_list, | |
247 | route_match_ip_next_hop_prefix_list_compile, | |
248 | route_match_ip_next_hop_prefix_list_free}; | |
249 | ||
250 | /* `match ip address IP_ACCESS_LIST' */ | |
251 | ||
252 | /* Match function should return 1 if match is success else return | |
253 | zero. */ | |
254 | static route_map_result_t route_match_ip_address(void *rule, | |
255 | const struct prefix *prefix, | |
256 | route_map_object_t type, | |
257 | void *object) | |
258 | { | |
259 | struct access_list *alist; | |
260 | ||
261 | if (type == RMAP_RIP) { | |
262 | alist = access_list_lookup(AFI_IP, (char *)rule); | |
263 | if (alist == NULL) | |
264 | return RMAP_NOMATCH; | |
265 | ||
266 | return (access_list_apply(alist, prefix) == FILTER_DENY | |
267 | ? RMAP_NOMATCH | |
268 | : RMAP_MATCH); | |
269 | } | |
270 | return RMAP_NOMATCH; | |
271 | } | |
272 | ||
273 | /* Route map `ip address' match statement. `arg' should be | |
274 | access-list name. */ | |
275 | static void *route_match_ip_address_compile(const char *arg) | |
276 | { | |
277 | return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg); | |
278 | } | |
279 | ||
280 | /* Free route map's compiled `ip address' value. */ | |
281 | static void route_match_ip_address_free(void *rule) | |
282 | { | |
283 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
284 | } | |
285 | ||
286 | /* Route map commands for ip address matching. */ | |
287 | static struct route_map_rule_cmd route_match_ip_address_cmd = { | |
288 | "ip address", route_match_ip_address, route_match_ip_address_compile, | |
289 | route_match_ip_address_free}; | |
290 | ||
291 | /* `match ip address prefix-list PREFIX_LIST' */ | |
292 | ||
293 | static route_map_result_t | |
294 | route_match_ip_address_prefix_list(void *rule, const struct prefix *prefix, | |
295 | route_map_object_t type, void *object) | |
296 | { | |
297 | struct prefix_list *plist; | |
298 | ||
299 | if (type == RMAP_RIP) { | |
300 | plist = prefix_list_lookup(AFI_IP, (char *)rule); | |
301 | if (plist == NULL) | |
302 | return RMAP_NOMATCH; | |
303 | ||
304 | return (prefix_list_apply(plist, prefix) == PREFIX_DENY | |
305 | ? RMAP_NOMATCH | |
306 | : RMAP_MATCH); | |
307 | } | |
308 | return RMAP_NOMATCH; | |
309 | } | |
310 | ||
311 | static void *route_match_ip_address_prefix_list_compile(const char *arg) | |
312 | { | |
313 | return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg); | |
314 | } | |
315 | ||
316 | static void route_match_ip_address_prefix_list_free(void *rule) | |
317 | { | |
318 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
319 | } | |
320 | ||
321 | static struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd = { | |
322 | "ip address prefix-list", route_match_ip_address_prefix_list, | |
323 | route_match_ip_address_prefix_list_compile, | |
324 | route_match_ip_address_prefix_list_free}; | |
325 | ||
326 | /* `match tag TAG' */ | |
327 | /* Match function return 1 if match is success else return zero. */ | |
328 | static route_map_result_t route_match_tag(void *rule, const struct prefix *p, | |
329 | route_map_object_t type, void *object) | |
330 | { | |
331 | route_tag_t *tag; | |
332 | struct rip_info *rinfo; | |
333 | route_tag_t rinfo_tag; | |
334 | ||
335 | if (type == RMAP_RIP) { | |
336 | tag = rule; | |
337 | rinfo = object; | |
338 | ||
339 | /* The information stored by rinfo is host ordered. */ | |
340 | rinfo_tag = rinfo->tag; | |
341 | if (rinfo_tag == *tag) | |
342 | return RMAP_MATCH; | |
343 | else | |
344 | return RMAP_NOMATCH; | |
345 | } | |
346 | return RMAP_NOMATCH; | |
347 | } | |
348 | ||
349 | /* Route map commands for tag matching. */ | |
350 | static struct route_map_rule_cmd route_match_tag_cmd = { | |
351 | "tag", route_match_tag, route_map_rule_tag_compile, | |
352 | route_map_rule_tag_free, | |
353 | }; | |
354 | ||
355 | /* `set metric METRIC' */ | |
356 | ||
357 | /* Set metric to attribute. */ | |
358 | static route_map_result_t route_set_metric(void *rule, | |
359 | const 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( | |
431 | "%s: Metric specified: %ld is greater than RIP_METRIC_INFINITY, using INFINITY instead", | |
432 | __PRETTY_FUNCTION__, metric); | |
433 | mod->metric = RIP_METRIC_INFINITY; | |
434 | } else | |
435 | mod->metric = metric; | |
436 | ||
437 | mod->used = true; | |
438 | ||
439 | return mod; | |
440 | } | |
441 | ||
442 | /* Free route map's compiled `set metric' value. */ | |
443 | static void route_set_metric_free(void *rule) | |
444 | { | |
445 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
446 | } | |
447 | ||
448 | /* Set metric rule structure. */ | |
449 | static struct route_map_rule_cmd route_set_metric_cmd = { | |
450 | "metric", route_set_metric, route_set_metric_compile, | |
451 | route_set_metric_free, | |
452 | }; | |
453 | ||
454 | /* `set ip next-hop IP_ADDRESS' */ | |
455 | ||
456 | /* Set nexthop to object. ojbect must be pointer to struct attr. */ | |
457 | static route_map_result_t route_set_ip_nexthop(void *rule, | |
458 | const struct prefix *prefix, | |
459 | route_map_object_t type, | |
460 | void *object) | |
461 | { | |
462 | struct in_addr *address; | |
463 | struct rip_info *rinfo; | |
464 | ||
465 | if (type == RMAP_RIP) { | |
466 | /* Fetch routemap's rule information. */ | |
467 | address = rule; | |
468 | rinfo = object; | |
469 | ||
470 | /* Set next hop value. */ | |
471 | rinfo->nexthop_out = *address; | |
472 | } | |
473 | ||
474 | return RMAP_OKAY; | |
475 | } | |
476 | ||
477 | /* Route map `ip nexthop' compile function. Given string is converted | |
478 | to struct in_addr structure. */ | |
479 | static void *route_set_ip_nexthop_compile(const char *arg) | |
480 | { | |
481 | int ret; | |
482 | struct in_addr *address; | |
483 | ||
484 | address = XMALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(struct in_addr)); | |
485 | ||
486 | ret = inet_aton(arg, address); | |
487 | ||
488 | if (ret == 0) { | |
489 | XFREE(MTYPE_ROUTE_MAP_COMPILED, address); | |
490 | return NULL; | |
491 | } | |
492 | ||
493 | return address; | |
494 | } | |
495 | ||
496 | /* Free route map's compiled `ip nexthop' value. */ | |
497 | static void route_set_ip_nexthop_free(void *rule) | |
498 | { | |
499 | XFREE(MTYPE_ROUTE_MAP_COMPILED, rule); | |
500 | } | |
501 | ||
502 | /* Route map commands for ip nexthop set. */ | |
503 | static struct route_map_rule_cmd route_set_ip_nexthop_cmd = { | |
504 | "ip next-hop", route_set_ip_nexthop, route_set_ip_nexthop_compile, | |
505 | route_set_ip_nexthop_free}; | |
506 | ||
507 | /* `set tag TAG' */ | |
508 | ||
509 | /* Set tag to object. ojbect must be pointer to struct attr. */ | |
510 | static route_map_result_t route_set_tag(void *rule, const struct prefix *prefix, | |
511 | route_map_object_t type, void *object) | |
512 | { | |
513 | route_tag_t *tag; | |
514 | struct rip_info *rinfo; | |
515 | ||
516 | if (type == RMAP_RIP) { | |
517 | /* Fetch routemap's rule information. */ | |
518 | tag = rule; | |
519 | rinfo = object; | |
520 | ||
521 | /* Set next hop value. */ | |
522 | rinfo->tag_out = *tag; | |
523 | } | |
524 | ||
525 | return RMAP_OKAY; | |
526 | } | |
527 | ||
528 | /* Route map commands for tag set. */ | |
529 | static struct route_map_rule_cmd route_set_tag_cmd = { | |
530 | "tag", route_set_tag, route_map_rule_tag_compile, | |
531 | route_map_rule_tag_free}; | |
532 | ||
533 | #define MATCH_STR "Match values from routing table\n" | |
534 | #define SET_STR "Set values in destination routing protocol\n" | |
535 | ||
536 | void rip_route_map_reset() | |
537 | { | |
538 | ; | |
539 | } | |
540 | ||
541 | /* Route-map init */ | |
542 | void rip_route_map_init() | |
543 | { | |
544 | route_map_init(); | |
545 | ||
546 | route_map_add_hook(rip_route_map_update); | |
547 | route_map_delete_hook(rip_route_map_update); | |
548 | ||
549 | route_map_match_interface_hook(generic_match_add); | |
550 | route_map_no_match_interface_hook(generic_match_delete); | |
551 | ||
552 | route_map_match_ip_address_hook(generic_match_add); | |
553 | route_map_no_match_ip_address_hook(generic_match_delete); | |
554 | ||
555 | route_map_match_ip_address_prefix_list_hook(generic_match_add); | |
556 | route_map_no_match_ip_address_prefix_list_hook(generic_match_delete); | |
557 | ||
558 | route_map_match_ip_next_hop_hook(generic_match_add); | |
559 | route_map_no_match_ip_next_hop_hook(generic_match_delete); | |
560 | ||
561 | route_map_match_ip_next_hop_prefix_list_hook(generic_match_add); | |
562 | route_map_no_match_ip_next_hop_prefix_list_hook(generic_match_delete); | |
563 | ||
564 | route_map_match_metric_hook(generic_match_add); | |
565 | route_map_no_match_metric_hook(generic_match_delete); | |
566 | ||
567 | route_map_match_tag_hook(generic_match_add); | |
568 | route_map_no_match_tag_hook(generic_match_delete); | |
569 | ||
570 | route_map_set_ip_nexthop_hook(generic_set_add); | |
571 | route_map_no_set_ip_nexthop_hook(generic_set_delete); | |
572 | ||
573 | route_map_set_metric_hook(generic_set_add); | |
574 | route_map_no_set_metric_hook(generic_set_delete); | |
575 | ||
576 | route_map_set_tag_hook(generic_set_add); | |
577 | route_map_no_set_tag_hook(generic_set_delete); | |
578 | ||
579 | route_map_install_match(&route_match_metric_cmd); | |
580 | route_map_install_match(&route_match_interface_cmd); | |
581 | route_map_install_match(&route_match_ip_next_hop_cmd); | |
582 | route_map_install_match(&route_match_ip_next_hop_prefix_list_cmd); | |
583 | route_map_install_match(&route_match_ip_address_cmd); | |
584 | route_map_install_match(&route_match_ip_address_prefix_list_cmd); | |
585 | route_map_install_match(&route_match_tag_cmd); | |
586 | ||
587 | route_map_install_set(&route_set_metric_cmd); | |
588 | route_map_install_set(&route_set_ip_nexthop_cmd); | |
589 | route_map_install_set(&route_set_tag_cmd); | |
590 | } |