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