2 * IS-IS Rout(e)ing protocol - isis_redist.c
4 * Copyright (C) 2013-2015 Christian Franke <chris@opensourcerouting.org>
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)
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
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "isis_memory.h"
34 #include "isisd/dict.h"
35 #include "isisd/isis_constants.h"
36 #include "isisd/isis_common.h"
37 #include "isisd/isis_flags.h"
38 #include "isisd/isis_misc.h"
39 #include "isisd/isis_circuit.h"
40 #include "isisd/isisd.h"
41 #include "isisd/isis_lsp.h"
42 #include "isisd/isis_route.h"
43 #include "isisd/isis_zebra.h"
45 static int redist_protocol(int family
)
47 if (family
== AF_INET
)
49 if (family
== AF_INET6
)
52 assert(!"Unsupported address family!");
56 static afi_t
afi_for_redist_protocol(int protocol
)
63 assert(!"Unknown redist protocol!");
67 static struct route_table
*get_ext_info(struct isis
*i
, int family
)
69 int protocol
= redist_protocol(family
);
71 return i
->ext_info
[protocol
];
74 static struct isis_redist
*get_redist_settings(struct isis_area
*area
,
75 int family
, int type
, int level
)
77 int protocol
= redist_protocol(family
);
79 return &area
->redist_settings
[protocol
][type
][level
- 1];
82 struct route_table
*get_ext_reach(struct isis_area
*area
, int family
, int level
)
84 int protocol
= redist_protocol(family
);
86 return area
->ext_reach
[protocol
][level
- 1];
89 static struct route_node
*
90 isis_redist_route_node_create(route_table_delegate_t
*delegate
,
91 struct route_table
*table
)
93 struct route_node
*node
;
94 node
= XCALLOC(MTYPE_ISIS_EXT_ROUTE
, sizeof(*node
));
98 static void isis_redist_route_node_destroy(route_table_delegate_t
*delegate
,
99 struct route_table
*table
,
100 struct route_node
*node
)
103 XFREE(MTYPE_ISIS_EXT_INFO
, node
->info
);
104 XFREE(MTYPE_ISIS_EXT_ROUTE
, node
);
107 static route_table_delegate_t isis_redist_rt_delegate
= {
108 .create_node
= isis_redist_route_node_create
,
109 .destroy_node
= isis_redist_route_node_destroy
};
111 /* Install external reachability information into a
112 * specific area for a specific level.
113 * Schedule an lsp regenerate if necessary */
114 static void isis_redist_install(struct isis_area
*area
, int level
,
115 struct prefix
*p
, struct isis_ext_info
*info
)
117 int family
= p
->family
;
118 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
119 struct route_node
*er_node
;
123 "%s: External reachability table of area %s"
124 " is not initialized.",
125 __func__
, area
->area_tag
);
129 er_node
= route_node_get(er_table
, p
);
131 route_unlock_node(er_node
);
133 /* Don't update/reschedule lsp generation if nothing changed. */
134 if (!memcmp(er_node
->info
, info
, sizeof(*info
)))
137 er_node
->info
= XMALLOC(MTYPE_ISIS_EXT_INFO
, sizeof(*info
));
140 memcpy(er_node
->info
, info
, sizeof(*info
));
141 lsp_regenerate_schedule(area
, level
, 0);
144 /* Remove external reachability information from a
145 * specific area for a specific level.
146 * Schedule an lsp regenerate if necessary. */
147 static void isis_redist_uninstall(struct isis_area
*area
, int level
,
150 int family
= p
->family
;
151 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
152 struct route_node
*er_node
;
156 "%s: External reachability table of area %s"
157 " is not initialized.",
158 __func__
, area
->area_tag
);
162 er_node
= route_node_lookup(er_table
, p
);
166 route_unlock_node(er_node
);
171 XFREE(MTYPE_ISIS_EXT_INFO
, er_node
->info
);
172 route_unlock_node(er_node
);
173 lsp_regenerate_schedule(area
, level
, 0);
176 /* Update external reachability info of area for a given level
177 * and prefix, using the given redistribution settings. */
178 static void isis_redist_update_ext_reach(struct isis_area
*area
, int level
,
179 struct isis_redist
*redist
,
181 struct isis_ext_info
*info
)
183 struct isis_ext_info area_info
;
184 route_map_result_t map_ret
;
186 memcpy(&area_info
, info
, sizeof(area_info
));
187 area_info
.metric
= redist
->metric
;
189 if (redist
->map_name
) {
191 route_map_apply(redist
->map
, p
, RMAP_ISIS
, &area_info
);
192 if (map_ret
== RMAP_DENYMATCH
)
193 area_info
.distance
= 255;
196 /* Allow synthesized default routes only on always orignate */
197 if (area_info
.origin
== DEFAULT_ROUTE
198 && redist
->redist
!= DEFAULT_ORIGINATE_ALWAYS
)
199 area_info
.distance
= 255;
201 if (area_info
.distance
< 255)
202 isis_redist_install(area
, level
, p
, &area_info
);
204 isis_redist_uninstall(area
, level
, p
);
207 static void isis_redist_ensure_default(struct isis
*isis
, int family
)
210 struct route_table
*ei_table
= get_ext_info(isis
, family
);
211 struct route_node
*ei_node
;
212 struct isis_ext_info
*info
;
214 if (family
== AF_INET
) {
217 memset(&p
.u
.prefix4
, 0, sizeof(p
.u
.prefix4
));
218 } else if (family
== AF_INET6
) {
221 memset(&p
.u
.prefix6
, 0, sizeof(p
.u
.prefix6
));
223 assert(!"Unknown family!");
225 ei_node
= route_node_get(ei_table
, &p
);
227 route_unlock_node(ei_node
);
232 XCALLOC(MTYPE_ISIS_EXT_INFO
, sizeof(struct isis_ext_info
));
234 info
= ei_node
->info
;
235 info
->origin
= DEFAULT_ROUTE
;
236 info
->distance
= 254;
237 info
->metric
= MAX_WIDE_PATH_METRIC
;
240 /* Handle notification about route being added */
241 void isis_redist_add(int type
, struct prefix
*p
, uint8_t distance
,
244 int family
= p
->family
;
245 struct route_table
*ei_table
= get_ext_info(isis
, family
);
246 struct route_node
*ei_node
;
247 struct isis_ext_info
*info
;
248 struct listnode
*node
;
249 struct isis_area
*area
;
251 struct isis_redist
*redist
;
253 char debug_buf
[BUFSIZ
];
254 prefix2str(p
, debug_buf
, sizeof(debug_buf
));
256 zlog_debug("%s: New route %s from %s: distance %d.", __func__
,
257 debug_buf
, zebra_route_string(type
), distance
);
260 zlog_warn("%s: External information table not initialized.",
265 ei_node
= route_node_get(ei_table
, p
);
267 route_unlock_node(ei_node
);
269 ei_node
->info
= XCALLOC(MTYPE_ISIS_EXT_INFO
,
270 sizeof(struct isis_ext_info
));
272 info
= ei_node
->info
;
274 info
->distance
= distance
;
275 info
->metric
= metric
;
277 if (is_default_prefix(p
))
278 type
= DEFAULT_ROUTE
;
280 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
281 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
282 redist
= get_redist_settings(area
, family
, type
, level
);
286 isis_redist_update_ext_reach(area
, level
, redist
, p
,
291 void isis_redist_delete(int type
, struct prefix
*p
)
293 int family
= p
->family
;
294 struct route_table
*ei_table
= get_ext_info(isis
, family
);
295 struct route_node
*ei_node
;
296 struct listnode
*node
;
297 struct isis_area
*area
;
299 struct isis_redist
*redist
;
301 char debug_buf
[BUFSIZ
];
302 prefix2str(p
, debug_buf
, sizeof(debug_buf
));
304 zlog_debug("%s: Removing route %s from %s.", __func__
, debug_buf
,
305 zebra_route_string(type
));
307 if (is_default_prefix(p
)) {
308 /* Don't remove default route but add synthetic route for use
309 * by "default-information originate always". Areas without the
310 * "always" setting will ignore routes with origin
312 isis_redist_add(DEFAULT_ROUTE
, p
, 254, MAX_WIDE_PATH_METRIC
);
317 zlog_warn("%s: External information table not initialized.",
322 ei_node
= route_node_lookup(ei_table
, p
);
323 if (!ei_node
|| !ei_node
->info
) {
325 prefix2str(p
, buf
, sizeof(buf
));
327 "%s: Got a delete for %s route %s, but that route"
329 __func__
, zebra_route_string(type
), buf
);
331 route_unlock_node(ei_node
);
334 route_unlock_node(ei_node
);
336 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
337 for (level
= 1; level
< ISIS_LEVELS
; level
++) {
338 redist
= get_redist_settings(area
, family
, type
, level
);
342 isis_redist_uninstall(area
, level
, p
);
345 XFREE(MTYPE_ISIS_EXT_INFO
, ei_node
->info
);
346 route_unlock_node(ei_node
);
349 static void isis_redist_routemap_set(struct isis_redist
*redist
,
350 const char *routemap
)
352 if (redist
->map_name
) {
353 XFREE(MTYPE_ISIS
, redist
->map_name
);
357 if (routemap
&& strlen(routemap
)) {
358 redist
->map_name
= XSTRDUP(MTYPE_ISIS
, routemap
);
359 redist
->map
= route_map_lookup_by_name(routemap
);
363 static void isis_redist_update_zebra_subscriptions(struct isis
*isis
)
365 struct listnode
*node
;
366 struct isis_area
*area
;
371 char do_subscribe
[REDIST_PROTOCOL_COUNT
][ZEBRA_ROUTE_MAX
+ 1];
373 memset(do_subscribe
, 0, sizeof(do_subscribe
));
375 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
376 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
377 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++)
378 for (level
= 0; level
< ISIS_LEVELS
; level
++)
379 if (area
->redist_settings
[protocol
]
382 do_subscribe
[protocol
][type
] =
385 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
386 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++) {
387 /* This field is actually controlling transmission of
389 * routes to Zebra and has nothing to do with
392 if (type
== ZEBRA_ROUTE_ISIS
)
395 afi_t afi
= afi_for_redist_protocol(protocol
);
397 if (do_subscribe
[protocol
][type
])
398 isis_zebra_redistribute_set(afi
, type
);
400 isis_zebra_redistribute_unset(afi
, type
);
404 static void isis_redist_set(struct isis_area
*area
, int level
, int family
,
405 int type
, uint32_t metric
, const char *routemap
,
408 int protocol
= redist_protocol(family
);
409 struct isis_redist
*redist
=
410 get_redist_settings(area
, family
, type
, level
);
412 struct route_table
*ei_table
;
413 struct route_node
*rn
;
414 struct isis_ext_info
*info
;
416 redist
->redist
= (type
== DEFAULT_ROUTE
) ? originate_type
: 1;
417 redist
->metric
= metric
;
418 isis_redist_routemap_set(redist
, routemap
);
420 if (!area
->ext_reach
[protocol
][level
- 1]) {
421 area
->ext_reach
[protocol
][level
- 1] =
422 route_table_init_with_delegate(
423 &isis_redist_rt_delegate
);
426 for (i
= 0; i
< REDIST_PROTOCOL_COUNT
; i
++)
427 if (!area
->isis
->ext_info
[i
]) {
428 area
->isis
->ext_info
[i
] =
429 route_table_init_with_delegate(
430 &isis_redist_rt_delegate
);
433 isis_redist_update_zebra_subscriptions(area
->isis
);
435 if (type
== DEFAULT_ROUTE
&& originate_type
== DEFAULT_ORIGINATE_ALWAYS
)
436 isis_redist_ensure_default(area
->isis
, family
);
438 ei_table
= get_ext_info(area
->isis
, family
);
439 for (rn
= route_top(ei_table
); rn
; rn
= route_next(rn
)) {
444 if (type
== DEFAULT_ROUTE
) {
445 if (!is_default_prefix(&rn
->p
))
448 if (info
->origin
!= type
)
452 isis_redist_update_ext_reach(area
, level
, redist
, &rn
->p
, info
);
456 static void isis_redist_unset(struct isis_area
*area
, int level
, int family
,
459 struct isis_redist
*redist
=
460 get_redist_settings(area
, family
, type
, level
);
461 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
462 struct route_node
*rn
;
463 struct isis_ext_info
*info
;
470 zlog_warn("%s: External reachability table uninitialized.",
475 for (rn
= route_top(er_table
); rn
; rn
= route_next(rn
)) {
480 if (type
== DEFAULT_ROUTE
) {
481 if (!is_default_prefix(&rn
->p
))
484 if (info
->origin
!= type
)
488 XFREE(MTYPE_ISIS_EXT_INFO
, rn
->info
);
489 route_unlock_node(rn
);
492 lsp_regenerate_schedule(area
, level
, 0);
493 isis_redist_update_zebra_subscriptions(area
->isis
);
496 void isis_redist_area_finish(struct isis_area
*area
)
502 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
503 for (level
= 0; level
< ISIS_LEVELS
; level
++) {
504 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++) {
505 struct isis_redist
*redist
;
507 redist
= &area
->redist_settings
[protocol
][type
]
510 if (redist
->map_name
)
511 XFREE(MTYPE_ISIS
, redist
->map_name
);
513 route_table_finish(area
->ext_reach
[protocol
][level
]);
516 isis_redist_update_zebra_subscriptions(area
->isis
);
519 DEFUN (isis_redistribute
,
520 isis_redistribute_cmd
,
521 "redistribute <ipv4|ipv6> " FRR_REDIST_STR_ISISD
" <level-1|level-2> [<metric (0-16777215)|route-map WORD>]",
523 "Redistribute IPv4 routes\n"
524 "Redistribute IPv6 routes\n"
525 FRR_REDIST_HELP_STR_ISISD
526 "Redistribute into level-1\n"
527 "Redistribute into level-2\n"
528 "Metric for redistributed routes\n"
529 "ISIS default metric\n"
530 "Route map reference\n"
531 "Pointer to route-map entries\n")
534 int idx_protocol
= 2;
536 int idx_metric_rmap
= 4;
537 VTY_DECLVAR_CONTEXT(isis_area
, area
);
542 unsigned long metric
= 0;
543 const char *routemap
= NULL
;
545 family
= str2family(argv
[idx_afi
]->text
);
547 return CMD_WARNING_CONFIG_FAILED
;
549 afi
= family2afi(family
);
551 return CMD_WARNING_CONFIG_FAILED
;
553 type
= proto_redistnum(afi
, argv
[idx_protocol
]->text
);
555 return CMD_WARNING_CONFIG_FAILED
;
557 if (!strcmp("level-1", argv
[idx_level
]->arg
))
559 else if (!strcmp("level-2", argv
[idx_level
]->arg
))
562 return CMD_WARNING_CONFIG_FAILED
;
564 if ((area
->is_type
& level
) != level
) {
565 vty_out(vty
, "Node is not a level-%d IS\n", level
);
566 return CMD_WARNING_CONFIG_FAILED
;
569 if (argc
> idx_metric_rmap
+ 1) {
570 if (argv
[idx_metric_rmap
+ 1]->arg
[0] == '\0')
571 return CMD_WARNING_CONFIG_FAILED
;
573 if (strmatch(argv
[idx_metric_rmap
]->text
, "metric")) {
575 metric
= strtoul(argv
[idx_metric_rmap
+ 1]->arg
, &endp
,
579 return CMD_WARNING_CONFIG_FAILED
;
581 routemap
= argv
[idx_metric_rmap
+ 1]->arg
;
585 isis_redist_set(area
, level
, family
, type
, metric
, routemap
, 0);
589 DEFUN (no_isis_redistribute
,
590 no_isis_redistribute_cmd
,
591 "no redistribute <ipv4|ipv6> " FRR_REDIST_STR_ISISD
" <level-1|level-2>",
594 "Redistribute IPv4 routes\n"
595 "Redistribute IPv6 routes\n"
596 FRR_REDIST_HELP_STR_ISISD
597 "Redistribute into level-1\n"
598 "Redistribute into level-2\n")
601 int idx_protocol
= 3;
603 VTY_DECLVAR_CONTEXT(isis_area
, area
);
609 family
= str2family(argv
[idx_afi
]->arg
);
611 return CMD_WARNING_CONFIG_FAILED
;
613 afi
= family2afi(family
);
615 return CMD_WARNING_CONFIG_FAILED
;
617 type
= proto_redistnum(afi
, argv
[idx_protocol
]->text
);
619 return CMD_WARNING_CONFIG_FAILED
;
621 level
= strmatch("level-1", argv
[idx_level
]->text
) ? 1 : 2;
623 isis_redist_unset(area
, level
, family
, type
);
627 DEFUN (isis_default_originate
,
628 isis_default_originate_cmd
,
629 "default-information originate <ipv4|ipv6> <level-1|level-2> [always] [<metric (0-16777215)|route-map WORD>]",
630 "Control distribution of default information\n"
631 "Distribute a default route\n"
632 "Distribute default route for IPv4\n"
633 "Distribute default route for IPv6\n"
634 "Distribute default route into level-1\n"
635 "Distribute default route into level-2\n"
636 "Always advertise default route\n"
637 "Metric for default route\n"
638 "ISIS default metric\n"
639 "Route map reference\n"
640 "Pointer to route-map entries\n")
645 int idx_metric_rmap
= 4;
646 VTY_DECLVAR_CONTEXT(isis_area
, area
);
648 int originate_type
= DEFAULT_ORIGINATE
;
650 unsigned long metric
= 0;
651 const char *routemap
= NULL
;
653 family
= str2family(argv
[idx_afi
]->text
);
655 return CMD_WARNING_CONFIG_FAILED
;
657 level
= strmatch("level-1", argv
[idx_level
]->text
) ? 1 : 2;
659 if ((area
->is_type
& level
) != level
) {
660 vty_out(vty
, "Node is not a level-%d IS\n", level
);
661 return CMD_WARNING_CONFIG_FAILED
;
664 if (argc
> idx_always
&& strmatch(argv
[idx_always
]->text
, "always")) {
665 originate_type
= DEFAULT_ORIGINATE_ALWAYS
;
669 if (argc
> idx_metric_rmap
) {
670 if (strmatch(argv
[idx_metric_rmap
]->text
, "metric"))
671 metric
= strtoul(argv
[idx_metric_rmap
+ 1]->arg
, NULL
,
674 routemap
= argv
[idx_metric_rmap
+ 1]->arg
;
677 if (family
== AF_INET6
&& originate_type
!= DEFAULT_ORIGINATE_ALWAYS
) {
679 "Zebra doesn't implement default-originate for IPv6 yet\n");
681 "so use with care or use default-originate always.\n");
684 isis_redist_set(area
, level
, family
, DEFAULT_ROUTE
, metric
, routemap
,
689 DEFUN (no_isis_default_originate
,
690 no_isis_default_originate_cmd
,
691 "no default-information originate <ipv4|ipv6> <level-1|level-2>",
693 "Control distribution of default information\n"
694 "Distribute a default route\n"
695 "Distribute default route for IPv4\n"
696 "Distribute default route for IPv6\n"
697 "Distribute default route into level-1\n"
698 "Distribute default route into level-2\n")
702 VTY_DECLVAR_CONTEXT(isis_area
, area
);
706 family
= str2family(argv
[idx_afi
]->text
);
708 return CMD_WARNING_CONFIG_FAILED
;
710 if (strmatch("level-1", argv
[idx_level
]->text
))
712 else if (strmatch("level-2", argv
[idx_level
]->text
))
715 return CMD_WARNING_CONFIG_FAILED
;
717 isis_redist_unset(area
, level
, family
, DEFAULT_ROUTE
);
721 int isis_redist_config_write(struct vty
*vty
, struct isis_area
*area
,
727 struct isis_redist
*redist
;
728 const char *family_str
;
730 if (family
== AF_INET
)
732 else if (family
== AF_INET6
)
737 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++) {
738 if (type
== ZEBRA_ROUTE_ISIS
)
741 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
742 redist
= get_redist_settings(area
, family
, type
, level
);
745 vty_out(vty
, " redistribute %s %s level-%d", family_str
,
746 zebra_route_string(type
), level
);
748 vty_out(vty
, " metric %u", redist
->metric
);
749 if (redist
->map_name
)
750 vty_out(vty
, " route-map %s", redist
->map_name
);
756 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
758 get_redist_settings(area
, family
, DEFAULT_ROUTE
, level
);
761 vty_out(vty
, " default-information originate %s level-%d",
763 if (redist
->redist
== DEFAULT_ORIGINATE_ALWAYS
)
764 vty_out(vty
, " always");
766 vty_out(vty
, " metric %u", redist
->metric
);
767 if (redist
->map_name
)
768 vty_out(vty
, " route-map %s", redist
->map_name
);
776 void isis_redist_init(void)
778 install_element(ISIS_NODE
, &isis_redistribute_cmd
);
779 install_element(ISIS_NODE
, &no_isis_redistribute_cmd
);
780 install_element(ISIS_NODE
, &isis_default_originate_cmd
);
781 install_element(ISIS_NODE
, &no_isis_default_originate_cmd
);