]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_routemap.c
Merge remote-tracking branch 'origin/cmaster' into cmaster-next
[mirror_frr.git] / isisd / isis_routemap.c
1 /*
2 * IS-IS Rout(e)ing protocol - isis_routemap.c
3 *
4 * Copyright (C) 2013-2015 Christian Franke <chris@opensourcerouting.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
15
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "filter.h"
25 #include "hash.h"
26 #include "if.h"
27 #include "linklist.h"
28 #include "log.h"
29 #include "memory.h"
30 #include "prefix.h"
31 #include "plist.h"
32 #include "routemap.h"
33 #include "table.h"
34 #include "thread.h"
35 #include "vty.h"
36
37 #include "isis_constants.h"
38 #include "isis_common.h"
39 #include "isis_flags.h"
40 #include "dict.h"
41 #include "isisd.h"
42 #include "isis_misc.h"
43 #include "isis_adjacency.h"
44 #include "isis_circuit.h"
45 #include "isis_tlv.h"
46 #include "isis_pdu.h"
47 #include "isis_lsp.h"
48 #include "isis_spf.h"
49 #include "isis_route.h"
50 #include "isis_zebra.h"
51 #include "isis_routemap.h"
52
53 static route_map_result_t
54 route_match_ip_address(void *rule, struct prefix *prefix,
55 route_map_object_t type, void *object)
56 {
57 struct access_list *alist;
58
59 if (type != RMAP_ISIS)
60 return RMAP_NOMATCH;
61
62 alist = access_list_lookup(AFI_IP, (char*)rule);
63 if (access_list_apply(alist, prefix) != FILTER_DENY)
64 return RMAP_MATCH;
65
66 return RMAP_NOMATCH;
67 }
68
69 static void *
70 route_match_ip_address_compile(const char *arg)
71 {
72 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
73 }
74
75 static void
76 route_match_ip_address_free(void *rule)
77 {
78 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
79 }
80
81 static struct route_map_rule_cmd route_match_ip_address_cmd =
82 {
83 "ip address",
84 route_match_ip_address,
85 route_match_ip_address_compile,
86 route_match_ip_address_free
87 };
88
89 /* ------------------------------------------------------------*/
90
91 static route_map_result_t
92 route_match_ip_address_prefix_list(void *rule, struct prefix *prefix,
93 route_map_object_t type, void *object)
94 {
95 struct prefix_list *plist;
96
97 if (type != RMAP_ISIS)
98 return RMAP_NOMATCH;
99
100 plist = prefix_list_lookup(AFI_IP, (char*)rule);
101 if (prefix_list_apply(plist, prefix) != PREFIX_DENY)
102 return RMAP_MATCH;
103
104 return RMAP_NOMATCH;
105 }
106
107 static void *
108 route_match_ip_address_prefix_list_compile(const char *arg)
109 {
110 return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);
111 }
112
113 static void
114 route_match_ip_address_prefix_list_free (void *rule)
115 {
116 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
117 }
118
119 struct route_map_rule_cmd route_match_ip_address_prefix_list_cmd =
120 {
121 "ip address prefix-list",
122 route_match_ip_address_prefix_list,
123 route_match_ip_address_prefix_list_compile,
124 route_match_ip_address_prefix_list_free
125 };
126
127 /* ------------------------------------------------------------*/
128
129 static route_map_result_t
130 route_match_ipv6_address(void *rule, struct prefix *prefix,
131 route_map_object_t type, void *object)
132 {
133 struct access_list *alist;
134
135 if (type != RMAP_ISIS)
136 return RMAP_NOMATCH;
137
138 alist = access_list_lookup(AFI_IP6, (char*)rule);
139 if (access_list_apply(alist, prefix) != FILTER_DENY)
140 return RMAP_MATCH;
141
142 return RMAP_NOMATCH;
143 }
144
145 static void *
146 route_match_ipv6_address_compile(const char *arg)
147 {
148 return XSTRDUP(MTYPE_ROUTE_MAP_COMPILED, arg);
149 }
150
151 static void
152 route_match_ipv6_address_free(void *rule)
153 {
154 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
155 }
156
157 static struct route_map_rule_cmd route_match_ipv6_address_cmd =
158 {
159 "ipv6 address",
160 route_match_ipv6_address,
161 route_match_ipv6_address_compile,
162 route_match_ipv6_address_free
163 };
164
165 /* ------------------------------------------------------------*/
166
167 static route_map_result_t
168 route_match_ipv6_address_prefix_list(void *rule, struct prefix *prefix,
169 route_map_object_t type, void *object)
170 {
171 struct prefix_list *plist;
172
173 if (type != RMAP_ISIS)
174 return RMAP_NOMATCH;
175
176 plist = prefix_list_lookup(AFI_IP6, (char*)rule);
177 if (prefix_list_apply(plist, prefix) != PREFIX_DENY)
178 return RMAP_MATCH;
179
180 return RMAP_NOMATCH;
181 }
182
183 static void *
184 route_match_ipv6_address_prefix_list_compile(const char *arg)
185 {
186 return XSTRDUP (MTYPE_ROUTE_MAP_COMPILED, arg);
187 }
188
189 static void
190 route_match_ipv6_address_prefix_list_free (void *rule)
191 {
192 XFREE (MTYPE_ROUTE_MAP_COMPILED, rule);
193 }
194
195 struct route_map_rule_cmd route_match_ipv6_address_prefix_list_cmd =
196 {
197 "ipv6 address prefix-list",
198 route_match_ipv6_address_prefix_list,
199 route_match_ipv6_address_prefix_list_compile,
200 route_match_ipv6_address_prefix_list_free
201 };
202
203 /* ------------------------------------------------------------*/
204
205 static route_map_result_t
206 route_set_metric(void *rule, struct prefix *prefix,
207 route_map_object_t type, void *object)
208 {
209 uint32_t *metric;
210 struct isis_ext_info *info;
211
212 if (type == RMAP_ISIS)
213 {
214 metric = rule;
215 info = object;
216
217 info->metric = *metric;
218 }
219 return RMAP_OKAY;
220 }
221
222 static void *
223 route_set_metric_compile(const char *arg)
224 {
225 unsigned long metric;
226 char *endp;
227 uint32_t *ret;
228
229 metric = strtoul(arg, &endp, 10);
230 if (arg[0] == '\0' || *endp != '\0' || metric > MAX_WIDE_PATH_METRIC)
231 return NULL;
232
233 ret = XCALLOC(MTYPE_ROUTE_MAP_COMPILED, sizeof(*ret));
234 *ret = metric;
235
236 return ret;
237 }
238
239 static void
240 route_set_metric_free(void *rule)
241 {
242 XFREE(MTYPE_ROUTE_MAP_COMPILED, rule);
243 }
244
245 static struct route_map_rule_cmd route_set_metric_cmd =
246 {
247 "metric",
248 route_set_metric,
249 route_set_metric_compile,
250 route_set_metric_free
251 };
252
253 /* ------------------------------------------------------------*/
254
255 static int
256 isis_route_match_add(struct vty *vty,
257 const char *command, const char *arg)
258 {
259 VTY_DECLVAR_CONTEXT (route_map_index, index);
260 int ret;
261
262 ret = route_map_add_match (index, command, arg);
263 if (ret)
264 {
265 switch (ret)
266 {
267 case RMAP_RULE_MISSING:
268 vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE);
269 return CMD_WARNING;
270 case RMAP_COMPILE_ERROR:
271 vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE);
272 return CMD_WARNING;
273 }
274 }
275 return CMD_SUCCESS;
276 }
277
278 static int
279 isis_route_match_delete(struct vty *vty,
280 const char *command, const char *arg)
281 {
282 VTY_DECLVAR_CONTEXT (route_map_index, index);
283 int ret;
284
285 ret = route_map_delete_match (index, command, arg);
286 if (ret)
287 {
288 switch (ret)
289 {
290 case RMAP_RULE_MISSING:
291 vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE);
292 return CMD_WARNING;
293 case RMAP_COMPILE_ERROR:
294 vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE);
295 return CMD_WARNING;
296 }
297 }
298 return CMD_SUCCESS;
299 }
300
301 static int
302 isis_route_set_add(struct vty *vty,
303 const char *command, const char *arg)
304 {
305 VTY_DECLVAR_CONTEXT (route_map_index, index);
306 int ret;
307
308 ret = route_map_add_set(index, command, arg);
309 if (ret)
310 {
311 switch (ret)
312 {
313 case RMAP_RULE_MISSING:
314 vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE);
315 return CMD_WARNING;
316 case RMAP_COMPILE_ERROR:
317 vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE);
318 return CMD_WARNING;
319 }
320 }
321
322 return CMD_SUCCESS;
323 }
324
325 static int
326 isis_route_set_delete (struct vty *vty,
327 const char *command, const char *arg)
328 {
329 VTY_DECLVAR_CONTEXT (route_map_index, index);
330 int ret;
331
332 ret = route_map_delete_set (index, command, arg);
333 if (ret)
334 {
335 switch (ret)
336 {
337 case RMAP_RULE_MISSING:
338 vty_out (vty, "%% Can't find rule.%s", VTY_NEWLINE);
339 return CMD_WARNING;
340 case RMAP_COMPILE_ERROR:
341 vty_out (vty, "%% Argument is malformed.%s", VTY_NEWLINE);
342 return CMD_WARNING;
343 }
344 }
345
346 return CMD_SUCCESS;
347 }
348
349 /* ------------------------------------------------------------*/
350
351 DEFUN (match_ip_address,
352 match_ip_address_cmd,
353 "match ip address (<1-199>|<1300-2699>|WORD)",
354 MATCH_STR
355 IP_STR
356 "Match address of route\n"
357 "IP access-list number\n"
358 "IP access-list number (expanded range)\n"
359 "IP Access-list name\n")
360 {
361 return isis_route_match_add(vty, "ip address", argv[0]);
362 }
363
364 DEFUN (no_match_ip_address,
365 no_match_ip_address_val_cmd,
366 "no match ip address (<1-199>|<1300-2699>|WORD)",
367 NO_STR
368 MATCH_STR
369 IP_STR
370 "Match address of route\n"
371 "IP access-list number\n"
372 "IP access-list number (expanded range)\n"
373 "IP Access-list name\n")
374 {
375 if (argc == 0)
376 return isis_route_match_delete(vty, "ip address", NULL);
377 return isis_route_match_delete(vty, "ip address", argv[0]);
378 }
379
380 ALIAS (no_match_ip_address,
381 no_match_ip_address_cmd,
382 "no match ip address",
383 NO_STR
384 MATCH_STR
385 IP_STR
386 "Match address of route\n")
387
388 /* ------------------------------------------------------------*/
389
390 DEFUN (match_ip_address_prefix_list,
391 match_ip_address_prefix_list_cmd,
392 "match ip address prefix-list WORD",
393 MATCH_STR
394 IP_STR
395 "Match address of route\n"
396 "Match entries of prefix-lists\n"
397 "IP prefix-list name\n")
398 {
399 return isis_route_match_add(vty, "ip address prefix-list", argv[0]);
400 }
401
402 DEFUN (no_match_ip_address_prefix_list,
403 no_match_ip_address_prefix_list_cmd,
404 "no match ip address prefix-list",
405 NO_STR
406 MATCH_STR
407 IP_STR
408 "Match address of route\n"
409 "Match entries of prefix-lists\n")
410 {
411 if (argc == 0)
412 return isis_route_match_delete (vty, "ip address prefix-list", NULL);
413 return isis_route_match_delete (vty, "ip address prefix-list", argv[0]);
414 }
415
416 ALIAS (no_match_ip_address_prefix_list,
417 no_match_ip_address_prefix_list_val_cmd,
418 "no match ip address prefix-list WORD",
419 NO_STR
420 MATCH_STR
421 IP_STR
422 "Match address of route\n"
423 "Match entries of prefix-lists\n"
424 "IP prefix-list name\n")
425
426 /* ------------------------------------------------------------*/
427
428 DEFUN (match_ipv6_address,
429 match_ipv6_address_cmd,
430 "match ipv6 address WORD",
431 MATCH_STR
432 IPV6_STR
433 "Match IPv6 address of route\n"
434 "IPv6 access-list name\n")
435 {
436 return isis_route_match_add(vty, "ipv6 address", argv[0]);
437 }
438
439 DEFUN (no_match_ipv6_address,
440 no_match_ipv6_address_val_cmd,
441 "no match ipv6 address WORD",
442 NO_STR
443 MATCH_STR
444 IPV6_STR
445 "Match IPv6 address of route\n"
446 "IPv6 access-list name\n")
447 {
448 if (argc == 0)
449 return isis_route_match_delete(vty, "ipv6 address", NULL);
450 return isis_route_match_delete(vty, "ipv6 address", argv[0]);
451 }
452
453 ALIAS (no_match_ipv6_address,
454 no_match_ipv6_address_cmd,
455 "no match ipv6 address",
456 NO_STR
457 MATCH_STR
458 IPV6_STR
459 "Match IPv6 address of route\n")
460
461 /* ------------------------------------------------------------*/
462
463 DEFUN (match_ipv6_address_prefix_list,
464 match_ipv6_address_prefix_list_cmd,
465 "match ipv6 address prefix-list WORD",
466 MATCH_STR
467 IPV6_STR
468 "Match address of route\n"
469 "Match entries of prefix-lists\n"
470 "IP prefix-list name\n")
471 {
472 return isis_route_match_add(vty, "ipv6 address prefix-list", argv[0]);
473 }
474
475 DEFUN (no_match_ipv6_address_prefix_list,
476 no_match_ipv6_address_prefix_list_cmd,
477 "no match ipv6 address prefix-list",
478 NO_STR
479 MATCH_STR
480 IPV6_STR
481 "Match address of route\n"
482 "Match entries of prefix-lists\n")
483 {
484 if (argc == 0)
485 return isis_route_match_delete (vty, "ipv6 address prefix-list", NULL);
486 return isis_route_match_delete (vty, "ipv6 address prefix-list", argv[0]);
487 }
488
489 ALIAS (no_match_ipv6_address_prefix_list,
490 no_match_ipv6_address_prefix_list_val_cmd,
491 "no match ipv6 address prefix-list WORD",
492 NO_STR
493 MATCH_STR
494 IPV6_STR
495 "Match address of route\n"
496 "Match entries of prefix-lists\n"
497 "IP prefix-list name\n")
498
499 /* ------------------------------------------------------------*/
500
501 /* set metric already exists e.g. in the ospf routemap. vtysh doesn't cope well with different
502 * commands at the same node, therefore add set metric with the same 32-bit range as ospf and
503 * verify that the input is a valid isis metric */
504 DEFUN (set_metric,
505 set_metric_cmd,
506 "set metric <0-4294967295>",
507 SET_STR
508 "Metric vale for destination routing protocol\n"
509 "Metric value\n")
510 {
511 return isis_route_set_add(vty, "metric", argv[0]);
512 }
513
514 DEFUN (no_set_metric,
515 no_set_metric_val_cmd,
516 "no set metric <0-4294967295>",
517 NO_STR
518 SET_STR
519 "Metric value for destination routing protocol\n"
520 "Metric value\n")
521 {
522 if (argc == 0)
523 return isis_route_set_delete(vty, "metric", NULL);
524 return isis_route_set_delete(vty, "metric", argv[0]);
525 }
526
527 ALIAS (no_set_metric,
528 no_set_metric_cmd,
529 "no set metric",
530 NO_STR
531 SET_STR
532 "Metric vale for destination routing protocol\n");
533
534 void
535 isis_route_map_init(void)
536 {
537 route_map_init();
538
539 route_map_install_match(&route_match_ip_address_cmd);
540 install_element(RMAP_NODE, &match_ip_address_cmd);
541 install_element(RMAP_NODE, &no_match_ip_address_val_cmd);
542 install_element(RMAP_NODE, &no_match_ip_address_cmd);
543
544 route_map_install_match(&route_match_ip_address_prefix_list_cmd);
545 install_element(RMAP_NODE, &match_ip_address_prefix_list_cmd);
546 install_element(RMAP_NODE, &no_match_ip_address_prefix_list_val_cmd);
547 install_element(RMAP_NODE, &no_match_ip_address_prefix_list_cmd);
548
549 route_map_install_match(&route_match_ipv6_address_cmd);
550 install_element(RMAP_NODE, &match_ipv6_address_cmd);
551 install_element(RMAP_NODE, &no_match_ipv6_address_val_cmd);
552 install_element(RMAP_NODE, &no_match_ipv6_address_cmd);
553
554 route_map_install_match(&route_match_ipv6_address_prefix_list_cmd);
555 install_element(RMAP_NODE, &match_ipv6_address_prefix_list_cmd);
556 install_element(RMAP_NODE, &no_match_ipv6_address_prefix_list_val_cmd);
557 install_element(RMAP_NODE, &no_match_ipv6_address_prefix_list_cmd);
558
559 route_map_install_set(&route_set_metric_cmd);
560 install_element(RMAP_NODE, &set_metric_cmd);
561 install_element(RMAP_NODE, &no_set_metric_val_cmd);
562 install_element(RMAP_NODE, &no_set_metric_cmd);
563 }