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"
33 #include "srcdest_table.h"
35 #include "isisd/dict.h"
36 #include "isisd/isis_constants.h"
37 #include "isisd/isis_common.h"
38 #include "isisd/isis_flags.h"
39 #include "isisd/isis_misc.h"
40 #include "isisd/isis_circuit.h"
41 #include "isisd/isisd.h"
42 #include "isisd/isis_lsp.h"
43 #include "isisd/isis_route.h"
44 #include "isisd/isis_zebra.h"
46 static int redist_protocol(int family
)
48 if (family
== AF_INET
)
50 if (family
== AF_INET6
)
53 assert(!"Unsupported address family!");
57 static afi_t
afi_for_redist_protocol(int protocol
)
64 assert(!"Unknown redist protocol!");
68 static struct route_table
*get_ext_info(struct isis
*i
, int family
)
70 int protocol
= redist_protocol(family
);
72 return i
->ext_info
[protocol
];
75 static struct isis_redist
*get_redist_settings(struct isis_area
*area
,
76 int family
, int type
, int level
)
78 int protocol
= redist_protocol(family
);
80 return &area
->redist_settings
[protocol
][type
][level
- 1];
83 struct route_table
*get_ext_reach(struct isis_area
*area
, int family
, int level
)
85 int protocol
= redist_protocol(family
);
87 return area
->ext_reach
[protocol
][level
- 1];
90 /* Install external reachability information into a
91 * specific area for a specific level.
92 * Schedule an lsp regenerate if necessary */
93 static void isis_redist_install(struct isis_area
*area
, int level
,
94 const struct prefix
*p
,
95 const struct prefix_ipv6
*src_p
,
96 struct isis_ext_info
*info
)
98 int family
= p
->family
;
99 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
100 struct route_node
*er_node
;
104 "%s: External reachability table of area %s"
105 " is not initialized.",
106 __func__
, area
->area_tag
);
110 er_node
= srcdest_rnode_get(er_table
, p
, src_p
);
112 route_unlock_node(er_node
);
114 /* Don't update/reschedule lsp generation if nothing changed. */
115 if (!memcmp(er_node
->info
, info
, sizeof(*info
)))
118 er_node
->info
= XMALLOC(MTYPE_ISIS_EXT_INFO
, sizeof(*info
));
121 memcpy(er_node
->info
, info
, sizeof(*info
));
122 lsp_regenerate_schedule(area
, level
, 0);
125 /* Remove external reachability information from a
126 * specific area for a specific level.
127 * Schedule an lsp regenerate if necessary. */
128 static void isis_redist_uninstall(struct isis_area
*area
, int level
,
129 const struct prefix
*p
,
130 const struct prefix_ipv6
*src_p
)
132 int family
= p
->family
;
133 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
134 struct route_node
*er_node
;
138 "%s: External reachability table of area %s"
139 " is not initialized.",
140 __func__
, area
->area_tag
);
144 er_node
= srcdest_rnode_lookup(er_table
, p
, src_p
);
148 route_unlock_node(er_node
);
153 XFREE(MTYPE_ISIS_EXT_INFO
, er_node
->info
);
154 route_unlock_node(er_node
);
155 lsp_regenerate_schedule(area
, level
, 0);
158 /* Update external reachability info of area for a given level
159 * and prefix, using the given redistribution settings. */
160 static void isis_redist_update_ext_reach(struct isis_area
*area
, int level
,
161 struct isis_redist
*redist
,
162 const struct prefix
*p
,
163 const struct prefix_ipv6
*src_p
,
164 struct isis_ext_info
*info
)
166 struct isis_ext_info area_info
;
167 route_map_result_t map_ret
;
169 memcpy(&area_info
, info
, sizeof(area_info
));
170 area_info
.metric
= redist
->metric
;
172 if (redist
->map_name
) {
174 route_map_apply(redist
->map
, p
, RMAP_ISIS
, &area_info
);
175 if (map_ret
== RMAP_DENYMATCH
)
176 area_info
.distance
= 255;
179 /* Allow synthesized default routes only on always orignate */
180 if (area_info
.origin
== DEFAULT_ROUTE
181 && redist
->redist
!= DEFAULT_ORIGINATE_ALWAYS
)
182 area_info
.distance
= 255;
184 if (area_info
.distance
< 255)
185 isis_redist_install(area
, level
, p
, src_p
, &area_info
);
187 isis_redist_uninstall(area
, level
, p
, src_p
);
190 static void isis_redist_ensure_default(struct isis
*isis
, int family
)
193 struct route_table
*ei_table
= get_ext_info(isis
, family
);
194 struct route_node
*ei_node
;
195 struct isis_ext_info
*info
;
197 if (family
== AF_INET
) {
200 memset(&p
.u
.prefix4
, 0, sizeof(p
.u
.prefix4
));
201 } else if (family
== AF_INET6
) {
204 memset(&p
.u
.prefix6
, 0, sizeof(p
.u
.prefix6
));
206 assert(!"Unknown family!");
208 ei_node
= srcdest_rnode_get(ei_table
, &p
, NULL
);
210 route_unlock_node(ei_node
);
215 XCALLOC(MTYPE_ISIS_EXT_INFO
, sizeof(struct isis_ext_info
));
217 info
= ei_node
->info
;
218 info
->origin
= DEFAULT_ROUTE
;
219 info
->distance
= 254;
220 info
->metric
= MAX_WIDE_PATH_METRIC
;
223 /* Handle notification about route being added */
224 void isis_redist_add(int type
, struct prefix
*p
, struct prefix_ipv6
*src_p
,
225 uint8_t distance
, uint32_t metric
)
227 int family
= p
->family
;
228 struct route_table
*ei_table
= get_ext_info(isis
, family
);
229 struct route_node
*ei_node
;
230 struct isis_ext_info
*info
;
231 struct listnode
*node
;
232 struct isis_area
*area
;
234 struct isis_redist
*redist
;
236 char debug_buf
[BUFSIZ
];
237 prefix2str(p
, debug_buf
, sizeof(debug_buf
));
239 zlog_debug("%s: New route %s from %s: distance %d.", __func__
,
240 debug_buf
, zebra_route_string(type
), distance
);
243 zlog_warn("%s: External information table not initialized.",
248 ei_node
= srcdest_rnode_get(ei_table
, p
, src_p
);
250 route_unlock_node(ei_node
);
252 ei_node
->info
= XCALLOC(MTYPE_ISIS_EXT_INFO
,
253 sizeof(struct isis_ext_info
));
255 info
= ei_node
->info
;
257 info
->distance
= distance
;
258 info
->metric
= metric
;
260 if (is_default_prefix(p
)
261 && (!src_p
|| !src_p
->prefixlen
)) {
262 type
= DEFAULT_ROUTE
;
265 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
266 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
267 redist
= get_redist_settings(area
, family
, type
, level
);
271 isis_redist_update_ext_reach(area
, level
, redist
, p
,
276 void isis_redist_delete(int type
, struct prefix
*p
, struct prefix_ipv6
*src_p
)
278 int family
= p
->family
;
279 struct route_table
*ei_table
= get_ext_info(isis
, family
);
280 struct route_node
*ei_node
;
281 struct listnode
*node
;
282 struct isis_area
*area
;
284 struct isis_redist
*redist
;
286 char debug_buf
[BUFSIZ
];
287 prefix2str(p
, debug_buf
, sizeof(debug_buf
));
289 zlog_debug("%s: Removing route %s from %s.", __func__
, debug_buf
,
290 zebra_route_string(type
));
292 if (is_default_prefix(p
)
293 && (!src_p
|| !src_p
->prefixlen
)) {
294 /* Don't remove default route but add synthetic route for use
295 * by "default-information originate always". Areas without the
296 * "always" setting will ignore routes with origin
298 isis_redist_add(DEFAULT_ROUTE
, p
, NULL
,
299 254, MAX_WIDE_PATH_METRIC
);
304 zlog_warn("%s: External information table not initialized.",
309 ei_node
= srcdest_rnode_lookup(ei_table
, p
, src_p
);
310 if (!ei_node
|| !ei_node
->info
) {
312 prefix2str(p
, buf
, sizeof(buf
));
314 "%s: Got a delete for %s route %s, but that route"
316 __func__
, zebra_route_string(type
), buf
);
318 route_unlock_node(ei_node
);
321 route_unlock_node(ei_node
);
323 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
324 for (level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
325 redist
= get_redist_settings(area
, family
, type
, level
);
329 isis_redist_uninstall(area
, level
, p
, src_p
);
332 XFREE(MTYPE_ISIS_EXT_INFO
, ei_node
->info
);
333 route_unlock_node(ei_node
);
336 static void isis_redist_routemap_set(struct isis_redist
*redist
,
337 const char *routemap
)
339 if (redist
->map_name
) {
340 XFREE(MTYPE_ISIS
, redist
->map_name
);
344 if (routemap
&& strlen(routemap
)) {
345 redist
->map_name
= XSTRDUP(MTYPE_ISIS
, routemap
);
346 redist
->map
= route_map_lookup_by_name(routemap
);
350 static void isis_redist_update_zebra_subscriptions(struct isis
*isis
)
352 struct listnode
*node
;
353 struct isis_area
*area
;
358 char do_subscribe
[REDIST_PROTOCOL_COUNT
][ZEBRA_ROUTE_MAX
+ 1];
360 memset(do_subscribe
, 0, sizeof(do_subscribe
));
362 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
363 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
364 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++)
365 for (level
= 0; level
< ISIS_LEVELS
; level
++)
366 if (area
->redist_settings
[protocol
]
369 do_subscribe
[protocol
][type
] =
372 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
373 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++) {
374 /* This field is actually controlling transmission of
376 * routes to Zebra and has nothing to do with
379 if (type
== PROTO_TYPE
)
382 afi_t afi
= afi_for_redist_protocol(protocol
);
384 if (do_subscribe
[protocol
][type
])
385 isis_zebra_redistribute_set(afi
, type
);
387 isis_zebra_redistribute_unset(afi
, type
);
391 static void isis_redist_set(struct isis_area
*area
, int level
, int family
,
392 int type
, uint32_t metric
, const char *routemap
,
395 int protocol
= redist_protocol(family
);
396 struct isis_redist
*redist
=
397 get_redist_settings(area
, family
, type
, level
);
399 struct route_table
*ei_table
;
400 struct route_node
*rn
;
401 struct isis_ext_info
*info
;
403 redist
->redist
= (type
== DEFAULT_ROUTE
) ? originate_type
: 1;
404 redist
->metric
= metric
;
405 isis_redist_routemap_set(redist
, routemap
);
407 if (!area
->ext_reach
[protocol
][level
- 1]) {
408 area
->ext_reach
[protocol
][level
- 1] = srcdest_table_init();
411 for (i
= 0; i
< REDIST_PROTOCOL_COUNT
; i
++) {
412 if (!area
->isis
->ext_info
[i
]) {
413 area
->isis
->ext_info
[i
] = srcdest_table_init();
417 isis_redist_update_zebra_subscriptions(area
->isis
);
419 if (type
== DEFAULT_ROUTE
&& originate_type
== DEFAULT_ORIGINATE_ALWAYS
)
420 isis_redist_ensure_default(area
->isis
, family
);
422 ei_table
= get_ext_info(area
->isis
, family
);
423 for (rn
= route_top(ei_table
); rn
; rn
= srcdest_route_next(rn
)) {
428 const struct prefix
*p
, *src_p
;
430 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
432 if (type
== DEFAULT_ROUTE
) {
433 if (!is_default_prefix(p
)
434 || (src_p
&& src_p
->prefixlen
)) {
438 if (info
->origin
!= type
)
442 isis_redist_update_ext_reach(area
, level
, redist
, p
,
443 (const struct prefix_ipv6
*)src_p
,
448 static void isis_redist_unset(struct isis_area
*area
, int level
, int family
,
451 struct isis_redist
*redist
=
452 get_redist_settings(area
, family
, type
, level
);
453 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
454 struct route_node
*rn
;
455 struct isis_ext_info
*info
;
462 zlog_warn("%s: External reachability table uninitialized.",
467 for (rn
= route_top(er_table
); rn
; rn
= srcdest_route_next(rn
)) {
472 const struct prefix
*p
, *src_p
;
473 srcdest_rnode_prefixes(rn
, &p
, &src_p
);
475 if (type
== DEFAULT_ROUTE
) {
476 if (!is_default_prefix(p
)
477 || (src_p
&& src_p
->prefixlen
)) {
481 if (info
->origin
!= type
)
485 XFREE(MTYPE_ISIS_EXT_INFO
, rn
->info
);
486 route_unlock_node(rn
);
489 lsp_regenerate_schedule(area
, level
, 0);
490 isis_redist_update_zebra_subscriptions(area
->isis
);
493 void isis_redist_area_finish(struct isis_area
*area
)
499 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
500 for (level
= 0; level
< ISIS_LEVELS
; level
++) {
501 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++) {
502 struct isis_redist
*redist
;
504 redist
= &area
->redist_settings
[protocol
][type
]
507 if (redist
->map_name
)
508 XFREE(MTYPE_ISIS
, redist
->map_name
);
510 route_table_finish(area
->ext_reach
[protocol
][level
]);
513 isis_redist_update_zebra_subscriptions(area
->isis
);
516 DEFUN (isis_redistribute
,
517 isis_redistribute_cmd
,
518 "redistribute <ipv4|ipv6> " PROTO_REDIST_STR
522 " [<metric (0-16777215)|route-map WORD>]",
524 "Redistribute IPv4 routes\n"
525 "Redistribute IPv6 routes\n"
528 "Redistribute into level-1\n"
529 "Redistribute into level-2\n"
531 "Metric for redistributed routes\n"
532 "ISIS default metric\n"
533 "Route map reference\n"
534 "Pointer to route-map entries\n")
537 int idx_protocol
= 2;
539 int idx_metric_rmap
= fabricd
? 3 : 4;
540 VTY_DECLVAR_CONTEXT(isis_area
, area
);
545 unsigned long metric
= 0;
546 const char *routemap
= NULL
;
548 family
= str2family(argv
[idx_afi
]->text
);
550 return CMD_WARNING_CONFIG_FAILED
;
552 afi
= family2afi(family
);
554 return CMD_WARNING_CONFIG_FAILED
;
556 type
= proto_redistnum(afi
, argv
[idx_protocol
]->text
);
558 return CMD_WARNING_CONFIG_FAILED
;
562 else if (!strcmp("level-1", argv
[idx_level
]->arg
))
564 else if (!strcmp("level-2", argv
[idx_level
]->arg
))
567 return CMD_WARNING_CONFIG_FAILED
;
569 if ((area
->is_type
& level
) != level
) {
570 vty_out(vty
, "Node is not a level-%d IS\n", level
);
571 return CMD_WARNING_CONFIG_FAILED
;
574 if (argc
> idx_metric_rmap
+ 1) {
575 if (argv
[idx_metric_rmap
+ 1]->arg
[0] == '\0')
576 return CMD_WARNING_CONFIG_FAILED
;
578 if (strmatch(argv
[idx_metric_rmap
]->text
, "metric")) {
580 metric
= strtoul(argv
[idx_metric_rmap
+ 1]->arg
, &endp
,
584 return CMD_WARNING_CONFIG_FAILED
;
586 routemap
= argv
[idx_metric_rmap
+ 1]->arg
;
590 isis_redist_set(area
, level
, family
, type
, metric
, routemap
, 0);
594 DEFUN (no_isis_redistribute
,
595 no_isis_redistribute_cmd
,
596 "no redistribute <ipv4|ipv6> " PROTO_REDIST_STR
602 "Redistribute IPv4 routes\n"
603 "Redistribute IPv6 routes\n"
606 "Redistribute into level-1\n"
607 "Redistribute into level-2\n"
612 int idx_protocol
= 3;
614 VTY_DECLVAR_CONTEXT(isis_area
, area
);
620 family
= str2family(argv
[idx_afi
]->arg
);
622 return CMD_WARNING_CONFIG_FAILED
;
624 afi
= family2afi(family
);
626 return CMD_WARNING_CONFIG_FAILED
;
628 type
= proto_redistnum(afi
, argv
[idx_protocol
]->text
);
630 return CMD_WARNING_CONFIG_FAILED
;
635 level
= strmatch("level-1", argv
[idx_level
]->text
) ? 1 : 2;
637 isis_redist_unset(area
, level
, family
, type
);
641 DEFUN (isis_default_originate
,
642 isis_default_originate_cmd
,
643 "default-information originate <ipv4|ipv6>"
647 " [always] [<metric (0-16777215)|route-map WORD>]",
648 "Control distribution of default information\n"
649 "Distribute a default route\n"
650 "Distribute default route for IPv4\n"
651 "Distribute default route for IPv6\n"
653 "Distribute default route into level-1\n"
654 "Distribute default route into level-2\n"
656 "Always advertise default route\n"
657 "Metric for default route\n"
658 "ISIS default metric\n"
659 "Route map reference\n"
660 "Pointer to route-map entries\n")
664 int idx_always
= fabricd
? 3 : 4;
665 int idx_metric_rmap
= fabricd
? 3 : 4;
666 VTY_DECLVAR_CONTEXT(isis_area
, area
);
668 int originate_type
= DEFAULT_ORIGINATE
;
670 unsigned long metric
= 0;
671 const char *routemap
= NULL
;
673 family
= str2family(argv
[idx_afi
]->text
);
675 return CMD_WARNING_CONFIG_FAILED
;
680 level
= strmatch("level-1", argv
[idx_level
]->text
) ? 1 : 2;
682 if ((area
->is_type
& level
) != level
) {
683 vty_out(vty
, "Node is not a level-%d IS\n", level
);
684 return CMD_WARNING_CONFIG_FAILED
;
687 if (argc
> idx_always
&& strmatch(argv
[idx_always
]->text
, "always")) {
688 originate_type
= DEFAULT_ORIGINATE_ALWAYS
;
692 if (argc
> idx_metric_rmap
) {
693 if (strmatch(argv
[idx_metric_rmap
]->text
, "metric"))
694 metric
= strtoul(argv
[idx_metric_rmap
+ 1]->arg
, NULL
,
697 routemap
= argv
[idx_metric_rmap
+ 1]->arg
;
700 if (family
== AF_INET6
&& originate_type
!= DEFAULT_ORIGINATE_ALWAYS
) {
702 "Zebra doesn't implement default-originate for IPv6 yet\n");
704 "so use with care or use default-originate always.\n");
707 isis_redist_set(area
, level
, family
, DEFAULT_ROUTE
, metric
, routemap
,
712 DEFUN (no_isis_default_originate
,
713 no_isis_default_originate_cmd
,
714 "no default-information originate <ipv4|ipv6>"
719 "Control distribution of default information\n"
720 "Distribute a default route\n"
721 "Distribute default route for IPv4\n"
722 "Distribute default route for IPv6\n"
724 "Distribute default route into level-1\n"
725 "Distribute default route into level-2\n"
731 VTY_DECLVAR_CONTEXT(isis_area
, area
);
735 family
= str2family(argv
[idx_afi
]->text
);
737 return CMD_WARNING_CONFIG_FAILED
;
741 else if (strmatch("level-1", argv
[idx_level
]->text
))
743 else if (strmatch("level-2", argv
[idx_level
]->text
))
746 return CMD_WARNING_CONFIG_FAILED
;
748 isis_redist_unset(area
, level
, family
, DEFAULT_ROUTE
);
752 int isis_redist_config_write(struct vty
*vty
, struct isis_area
*area
,
758 struct isis_redist
*redist
;
759 const char *family_str
;
761 if (family
== AF_INET
)
763 else if (family
== AF_INET6
)
768 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++) {
769 if (type
== PROTO_TYPE
)
772 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
773 redist
= get_redist_settings(area
, family
, type
, level
);
776 vty_out(vty
, " redistribute %s %s", family_str
,
777 zebra_route_string(type
));
779 vty_out(vty
, " level-%d", level
);
781 vty_out(vty
, " metric %u", redist
->metric
);
782 if (redist
->map_name
)
783 vty_out(vty
, " route-map %s", redist
->map_name
);
789 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
791 get_redist_settings(area
, family
, DEFAULT_ROUTE
, level
);
794 vty_out(vty
, " default-information originate %s",
797 vty_out(vty
, " level-%d", level
);
798 if (redist
->redist
== DEFAULT_ORIGINATE_ALWAYS
)
799 vty_out(vty
, " always");
801 vty_out(vty
, " metric %u", redist
->metric
);
802 if (redist
->map_name
)
803 vty_out(vty
, " route-map %s", redist
->map_name
);
811 void isis_redist_init(void)
813 install_element(ROUTER_NODE
, &isis_redistribute_cmd
);
814 install_element(ROUTER_NODE
, &no_isis_redistribute_cmd
);
815 install_element(ROUTER_NODE
, &isis_default_originate_cmd
);
816 install_element(ROUTER_NODE
, &no_isis_default_originate_cmd
);