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 int is_default(struct prefix
*p
)
69 if (p
->family
== AF_INET
)
70 if (p
->u
.prefix4
.s_addr
== 0 && p
->prefixlen
== 0)
72 if (p
->family
== AF_INET6
)
73 if (IN6_IS_ADDR_UNSPECIFIED(&p
->u
.prefix6
) && p
->prefixlen
== 0)
78 static struct route_table
*get_ext_info(struct isis
*i
, int family
)
80 int protocol
= redist_protocol(family
);
82 return i
->ext_info
[protocol
];
85 static struct isis_redist
*get_redist_settings(struct isis_area
*area
,
86 int family
, int type
, int level
)
88 int protocol
= redist_protocol(family
);
90 return &area
->redist_settings
[protocol
][type
][level
- 1];
93 struct route_table
*get_ext_reach(struct isis_area
*area
, int family
, int level
)
95 int protocol
= redist_protocol(family
);
97 return area
->ext_reach
[protocol
][level
- 1];
100 static struct route_node
*
101 isis_redist_route_node_create(route_table_delegate_t
*delegate
,
102 struct route_table
*table
)
104 struct route_node
*node
;
105 node
= XCALLOC(MTYPE_ISIS_EXT_ROUTE
, sizeof(*node
));
109 static void isis_redist_route_node_destroy(route_table_delegate_t
*delegate
,
110 struct route_table
*table
,
111 struct route_node
*node
)
114 XFREE(MTYPE_ISIS_EXT_INFO
, node
->info
);
115 XFREE(MTYPE_ISIS_EXT_ROUTE
, node
);
118 static route_table_delegate_t isis_redist_rt_delegate
= {
119 .create_node
= isis_redist_route_node_create
,
120 .destroy_node
= isis_redist_route_node_destroy
};
122 /* Install external reachability information into a
123 * specific area for a specific level.
124 * Schedule an lsp regenerate if necessary */
125 static void isis_redist_install(struct isis_area
*area
, int level
,
126 struct prefix
*p
, struct isis_ext_info
*info
)
128 int family
= p
->family
;
129 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
130 struct route_node
*er_node
;
134 "%s: External reachability table of area %s"
135 " is not initialized.",
136 __func__
, area
->area_tag
);
140 er_node
= route_node_get(er_table
, p
);
142 route_unlock_node(er_node
);
144 /* Don't update/reschedule lsp generation if nothing changed. */
145 if (!memcmp(er_node
->info
, info
, sizeof(*info
)))
148 er_node
->info
= XMALLOC(MTYPE_ISIS_EXT_INFO
, sizeof(*info
));
151 memcpy(er_node
->info
, info
, sizeof(*info
));
152 lsp_regenerate_schedule(area
, level
, 0);
155 /* Remove external reachability information from a
156 * specific area for a specific level.
157 * Schedule an lsp regenerate if necessary. */
158 static void isis_redist_uninstall(struct isis_area
*area
, int level
,
161 int family
= p
->family
;
162 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
163 struct route_node
*er_node
;
167 "%s: External reachability table of area %s"
168 " is not initialized.",
169 __func__
, area
->area_tag
);
173 er_node
= route_node_lookup(er_table
, p
);
177 route_unlock_node(er_node
);
182 XFREE(MTYPE_ISIS_EXT_INFO
, er_node
->info
);
183 route_unlock_node(er_node
);
184 lsp_regenerate_schedule(area
, level
, 0);
187 /* Update external reachability info of area for a given level
188 * and prefix, using the given redistribution settings. */
189 static void isis_redist_update_ext_reach(struct isis_area
*area
, int level
,
190 struct isis_redist
*redist
,
192 struct isis_ext_info
*info
)
194 struct isis_ext_info area_info
;
195 route_map_result_t map_ret
;
197 memcpy(&area_info
, info
, sizeof(area_info
));
198 if (redist
->metric
!= 0xffffffff)
199 area_info
.metric
= redist
->metric
;
201 if (redist
->map_name
) {
203 route_map_apply(redist
->map
, p
, RMAP_ISIS
, &area_info
);
204 if (map_ret
== RMAP_DENYMATCH
)
205 area_info
.distance
= 255;
208 /* Allow synthesized default routes only on always orignate */
209 if (area_info
.origin
== DEFAULT_ROUTE
210 && redist
->redist
!= DEFAULT_ORIGINATE_ALWAYS
)
211 area_info
.distance
= 255;
213 if (area_info
.distance
< 255)
214 isis_redist_install(area
, level
, p
, &area_info
);
216 isis_redist_uninstall(area
, level
, p
);
219 static void isis_redist_ensure_default(struct isis
*isis
, int family
)
222 struct route_table
*ei_table
= get_ext_info(isis
, family
);
223 struct route_node
*ei_node
;
224 struct isis_ext_info
*info
;
226 if (family
== AF_INET
) {
229 memset(&p
.u
.prefix4
, 0, sizeof(p
.u
.prefix4
));
230 } else if (family
== AF_INET6
) {
233 memset(&p
.u
.prefix6
, 0, sizeof(p
.u
.prefix6
));
235 assert(!"Unknown family!");
237 ei_node
= route_node_get(ei_table
, &p
);
239 route_unlock_node(ei_node
);
244 XCALLOC(MTYPE_ISIS_EXT_INFO
, sizeof(struct isis_ext_info
));
246 info
= ei_node
->info
;
247 info
->origin
= DEFAULT_ROUTE
;
248 info
->distance
= 254;
249 info
->metric
= MAX_WIDE_PATH_METRIC
;
252 /* Handle notification about route being added */
253 void isis_redist_add(int type
, struct prefix
*p
, u_char distance
,
256 int family
= p
->family
;
257 struct route_table
*ei_table
= get_ext_info(isis
, family
);
258 struct route_node
*ei_node
;
259 struct isis_ext_info
*info
;
260 struct listnode
*node
;
261 struct isis_area
*area
;
263 struct isis_redist
*redist
;
265 char debug_buf
[BUFSIZ
];
266 prefix2str(p
, debug_buf
, sizeof(debug_buf
));
268 zlog_debug("%s: New route %s from %s.", __func__
, debug_buf
,
269 zebra_route_string(type
));
272 zlog_warn("%s: External information table not initialized.",
277 ei_node
= route_node_get(ei_table
, p
);
279 route_unlock_node(ei_node
);
281 ei_node
->info
= XCALLOC(MTYPE_ISIS_EXT_INFO
,
282 sizeof(struct isis_ext_info
));
284 info
= ei_node
->info
;
286 info
->distance
= distance
;
287 info
->metric
= metric
;
290 type
= DEFAULT_ROUTE
;
292 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
293 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
294 redist
= get_redist_settings(area
, family
, type
, level
);
298 isis_redist_update_ext_reach(area
, level
, redist
, p
,
303 void isis_redist_delete(int type
, struct prefix
*p
)
305 int family
= p
->family
;
306 struct route_table
*ei_table
= get_ext_info(isis
, family
);
307 struct route_node
*ei_node
;
308 struct listnode
*node
;
309 struct isis_area
*area
;
311 struct isis_redist
*redist
;
313 char debug_buf
[BUFSIZ
];
314 prefix2str(p
, debug_buf
, sizeof(debug_buf
));
316 zlog_debug("%s: Removing route %s from %s.", __func__
, debug_buf
,
317 zebra_route_string(type
));
320 /* Don't remove default route but add synthetic route for use
321 * by "default-information originate always". Areas without the
322 * "always" setting will ignore routes with origin
324 isis_redist_add(DEFAULT_ROUTE
, p
, 254, MAX_WIDE_PATH_METRIC
);
329 zlog_warn("%s: External information table not initialized.",
334 ei_node
= route_node_lookup(ei_table
, p
);
335 if (!ei_node
|| !ei_node
->info
) {
337 prefix2str(p
, buf
, sizeof(buf
));
339 "%s: Got a delete for %s route %s, but that route"
341 __func__
, zebra_route_string(type
), buf
);
343 route_unlock_node(ei_node
);
346 route_unlock_node(ei_node
);
348 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
349 for (level
= 1; level
< ISIS_LEVELS
; level
++) {
350 redist
= get_redist_settings(area
, family
, type
, level
);
354 isis_redist_uninstall(area
, level
, p
);
357 XFREE(MTYPE_ISIS_EXT_INFO
, ei_node
->info
);
358 route_unlock_node(ei_node
);
361 static void isis_redist_routemap_set(struct isis_redist
*redist
,
362 const char *routemap
)
364 if (redist
->map_name
) {
365 XFREE(MTYPE_ISIS
, redist
->map_name
);
369 if (routemap
&& strlen(routemap
)) {
370 redist
->map_name
= XSTRDUP(MTYPE_ISIS
, routemap
);
371 redist
->map
= route_map_lookup_by_name(routemap
);
375 static void isis_redist_update_zebra_subscriptions(struct isis
*isis
)
377 struct listnode
*node
;
378 struct isis_area
*area
;
383 char do_subscribe
[REDIST_PROTOCOL_COUNT
][ZEBRA_ROUTE_MAX
+ 1];
385 memset(do_subscribe
, 0, sizeof(do_subscribe
));
387 for (ALL_LIST_ELEMENTS_RO(isis
->area_list
, node
, area
))
388 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
389 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++)
390 for (level
= 0; level
< ISIS_LEVELS
; level
++)
391 if (area
->redist_settings
[protocol
]
394 do_subscribe
[protocol
][type
] =
397 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
398 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++) {
399 /* This field is actually controlling transmission of
401 * routes to Zebra and has nothing to do with
404 if (type
== ZEBRA_ROUTE_ISIS
)
407 afi_t afi
= afi_for_redist_protocol(protocol
);
409 if (do_subscribe
[protocol
][type
])
410 isis_zebra_redistribute_set(afi
, type
);
412 isis_zebra_redistribute_unset(afi
, type
);
416 static void isis_redist_set(struct isis_area
*area
, int level
, int family
,
417 int type
, uint32_t metric
, const char *routemap
,
420 int protocol
= redist_protocol(family
);
421 struct isis_redist
*redist
=
422 get_redist_settings(area
, family
, type
, level
);
424 struct route_table
*ei_table
;
425 struct route_node
*rn
;
426 struct isis_ext_info
*info
;
428 redist
->redist
= (type
== DEFAULT_ROUTE
) ? originate_type
: 1;
429 redist
->metric
= metric
;
430 isis_redist_routemap_set(redist
, routemap
);
432 if (!area
->ext_reach
[protocol
][level
- 1]) {
433 area
->ext_reach
[protocol
][level
- 1] =
434 route_table_init_with_delegate(
435 &isis_redist_rt_delegate
);
438 for (i
= 0; i
< REDIST_PROTOCOL_COUNT
; i
++)
439 if (!area
->isis
->ext_info
[i
]) {
440 area
->isis
->ext_info
[i
] =
441 route_table_init_with_delegate(
442 &isis_redist_rt_delegate
);
445 isis_redist_update_zebra_subscriptions(area
->isis
);
447 if (type
== DEFAULT_ROUTE
&& originate_type
== DEFAULT_ORIGINATE_ALWAYS
)
448 isis_redist_ensure_default(area
->isis
, family
);
450 ei_table
= get_ext_info(area
->isis
, family
);
451 for (rn
= route_top(ei_table
); rn
; rn
= route_next(rn
)) {
456 if (type
== DEFAULT_ROUTE
) {
457 if (!is_default(&rn
->p
))
460 if (info
->origin
!= type
)
464 isis_redist_update_ext_reach(area
, level
, redist
, &rn
->p
, info
);
468 static void isis_redist_unset(struct isis_area
*area
, int level
, int family
,
471 struct isis_redist
*redist
=
472 get_redist_settings(area
, family
, type
, level
);
473 struct route_table
*er_table
= get_ext_reach(area
, family
, level
);
474 struct route_node
*rn
;
475 struct isis_ext_info
*info
;
482 zlog_warn("%s: External reachability table uninitialized.",
487 for (rn
= route_top(er_table
); rn
; rn
= route_next(rn
)) {
492 if (type
== DEFAULT_ROUTE
) {
493 if (!is_default(&rn
->p
))
496 if (info
->origin
!= type
)
500 XFREE(MTYPE_ISIS_EXT_INFO
, rn
->info
);
501 route_unlock_node(rn
);
504 lsp_regenerate_schedule(area
, level
, 0);
505 isis_redist_update_zebra_subscriptions(area
->isis
);
508 void isis_redist_area_finish(struct isis_area
*area
)
514 for (protocol
= 0; protocol
< REDIST_PROTOCOL_COUNT
; protocol
++)
515 for (level
= 0; level
< ISIS_LEVELS
; level
++) {
516 for (type
= 0; type
< ZEBRA_ROUTE_MAX
+ 1; type
++) {
517 struct isis_redist
*redist
;
519 redist
= &area
->redist_settings
[protocol
][type
]
522 if (redist
->map_name
)
523 XFREE(MTYPE_ISIS
, redist
->map_name
);
525 route_table_finish(area
->ext_reach
[protocol
][level
]);
528 isis_redist_update_zebra_subscriptions(area
->isis
);
531 DEFUN (isis_redistribute
,
532 isis_redistribute_cmd
,
533 "redistribute <ipv4|ipv6> " FRR_REDIST_STR_ISISD
" <level-1|level-2> [<metric (0-16777215)|route-map WORD>]",
535 "Redistribute IPv4 routes\n"
536 "Redistribute IPv6 routes\n"
537 FRR_REDIST_HELP_STR_ISISD
538 "Redistribute into level-1\n"
539 "Redistribute into level-2\n"
540 "Metric for redistributed routes\n"
541 "ISIS default metric\n"
542 "Route map reference\n"
543 "Pointer to route-map entries\n")
546 int idx_protocol
= 2;
548 int idx_metric_rmap
= 4;
549 VTY_DECLVAR_CONTEXT(isis_area
, area
);
554 unsigned long metric
;
555 const char *routemap
= NULL
;
557 family
= str2family(argv
[idx_afi
]->text
);
559 return CMD_WARNING_CONFIG_FAILED
;
561 afi
= family2afi(family
);
563 return CMD_WARNING_CONFIG_FAILED
;
565 type
= proto_redistnum(afi
, argv
[idx_protocol
]->text
);
567 return CMD_WARNING_CONFIG_FAILED
;
569 if (!strcmp("level-1", argv
[idx_level
]->arg
))
571 else if (!strcmp("level-2", argv
[idx_level
]->arg
))
574 return CMD_WARNING_CONFIG_FAILED
;
576 if ((area
->is_type
& level
) != level
) {
577 vty_out(vty
, "Node is not a level-%d IS\n", level
);
578 return CMD_WARNING_CONFIG_FAILED
;
584 if (argc
> idx_metric_rmap
+ 1) {
585 if (argv
[idx_metric_rmap
+ 1]->arg
[0] == '\0')
586 return CMD_WARNING_CONFIG_FAILED
;
588 if (strmatch(argv
[idx_metric_rmap
]->text
, "metric")) {
590 metric
= strtoul(argv
[idx_metric_rmap
+ 1]->arg
, &endp
,
594 return CMD_WARNING_CONFIG_FAILED
;
596 routemap
= argv
[idx_metric_rmap
+ 1]->arg
;
600 isis_redist_set(area
, level
, family
, type
, metric
, routemap
, 0);
604 DEFUN (no_isis_redistribute
,
605 no_isis_redistribute_cmd
,
606 "no redistribute <ipv4|ipv6> " FRR_REDIST_STR_ISISD
" <level-1|level-2>",
609 "Redistribute IPv4 routes\n"
610 "Redistribute IPv6 routes\n"
611 FRR_REDIST_HELP_STR_ISISD
612 "Redistribute into level-1\n"
613 "Redistribute into level-2\n")
616 int idx_protocol
= 3;
618 VTY_DECLVAR_CONTEXT(isis_area
, area
);
624 family
= str2family(argv
[idx_afi
]->arg
);
626 return CMD_WARNING_CONFIG_FAILED
;
628 afi
= family2afi(family
);
630 return CMD_WARNING_CONFIG_FAILED
;
632 type
= proto_redistnum(afi
, argv
[idx_protocol
]->text
);
634 return CMD_WARNING_CONFIG_FAILED
;
636 level
= strmatch("level-1", argv
[idx_level
]->text
) ? 1 : 2;
638 isis_redist_unset(area
, level
, family
, type
);
642 DEFUN (isis_default_originate
,
643 isis_default_originate_cmd
,
644 "default-information originate <ipv4|ipv6> <level-1|level-2> [always] [<metric (0-16777215)|route-map WORD>]",
645 "Control distribution of default information\n"
646 "Distribute a default route\n"
647 "Distribute default route for IPv4\n"
648 "Distribute default route for IPv6\n"
649 "Distribute default route into level-1\n"
650 "Distribute default route into level-2\n"
651 "Always advertise default route\n"
652 "Metric for default route\n"
653 "ISIS default metric\n"
654 "Route map reference\n"
655 "Pointer to route-map entries\n")
660 int idx_metric_rmap
= 4;
661 VTY_DECLVAR_CONTEXT(isis_area
, area
);
663 int originate_type
= DEFAULT_ORIGINATE
;
665 unsigned long metric
= 0xffffffff;
666 const char *routemap
= NULL
;
668 family
= str2family(argv
[idx_afi
]->text
);
670 return CMD_WARNING_CONFIG_FAILED
;
672 level
= strmatch("level-1", argv
[idx_level
]->text
) ? 1 : 2;
674 if ((area
->is_type
& level
) != level
) {
675 vty_out(vty
, "Node is not a level-%d IS\n", level
);
676 return CMD_WARNING_CONFIG_FAILED
;
679 if (argc
> idx_always
&& strmatch(argv
[idx_always
]->text
, "always")) {
680 originate_type
= DEFAULT_ORIGINATE_ALWAYS
;
684 if (argc
> idx_metric_rmap
) {
685 if (strmatch(argv
[idx_metric_rmap
]->text
, "metric"))
686 metric
= strtoul(argv
[idx_metric_rmap
+ 1]->arg
, NULL
,
689 routemap
= argv
[idx_metric_rmap
+ 1]->arg
;
692 if (family
== AF_INET6
&& originate_type
!= DEFAULT_ORIGINATE_ALWAYS
) {
694 "Zebra doesn't implement default-originate for IPv6 yet\n");
696 "so use with care or use default-originate always.\n");
699 isis_redist_set(area
, level
, family
, DEFAULT_ROUTE
, metric
, routemap
,
704 DEFUN (no_isis_default_originate
,
705 no_isis_default_originate_cmd
,
706 "no default-information originate <ipv4|ipv6> <level-1|level-2>",
708 "Control distribution of default information\n"
709 "Distribute a default route\n"
710 "Distribute default route for IPv4\n"
711 "Distribute default route for IPv6\n"
712 "Distribute default route into level-1\n"
713 "Distribute default route into level-2\n")
717 VTY_DECLVAR_CONTEXT(isis_area
, area
);
721 family
= str2family(argv
[idx_afi
]->text
);
723 return CMD_WARNING_CONFIG_FAILED
;
725 if (strmatch("level-1", argv
[idx_level
]->text
))
727 else if (strmatch("level-2", argv
[idx_level
]->text
))
730 return CMD_WARNING_CONFIG_FAILED
;
732 isis_redist_unset(area
, level
, family
, DEFAULT_ROUTE
);
736 int isis_redist_config_write(struct vty
*vty
, struct isis_area
*area
,
742 struct isis_redist
*redist
;
743 const char *family_str
;
745 if (family
== AF_INET
)
747 else if (family
== AF_INET6
)
752 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++) {
753 if (type
== ZEBRA_ROUTE_ISIS
)
756 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
757 redist
= get_redist_settings(area
, family
, type
, level
);
760 vty_out(vty
, " redistribute %s %s level-%d", family_str
,
761 zebra_route_string(type
), level
);
762 if (redist
->metric
!= 0xffffffff)
763 vty_out(vty
, " metric %u", redist
->metric
);
764 if (redist
->map_name
)
765 vty_out(vty
, " route-map %s", redist
->map_name
);
771 for (level
= 1; level
<= ISIS_LEVELS
; level
++) {
773 get_redist_settings(area
, family
, DEFAULT_ROUTE
, level
);
776 vty_out(vty
, " default-information originate %s level-%d",
778 if (redist
->redist
== DEFAULT_ORIGINATE_ALWAYS
)
779 vty_out(vty
, " always");
780 if (redist
->metric
!= 0xffffffff)
781 vty_out(vty
, " metric %u", redist
->metric
);
782 if (redist
->map_name
)
783 vty_out(vty
, " route-map %s", redist
->map_name
);
791 void isis_redist_init(void)
793 install_element(ISIS_NODE
, &isis_redistribute_cmd
);
794 install_element(ISIS_NODE
, &no_isis_redistribute_cmd
);
795 install_element(ISIS_NODE
, &isis_default_originate_cmd
);
796 install_element(ISIS_NODE
, &no_isis_default_originate_cmd
);