2 * IS-IS Rout(e)ing protocol - isis_route.c
3 * Copyright (C) 2001,2002 Sampo Saaristo
4 * Tampere University of Technology
5 * Institute of Communications Engineering
7 * based on ../ospf6d/ospf6_route.[ch]
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public Licenseas published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
15 * This program is distributed in the hope that it will be useful,but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along
21 * with this program; see the file COPYING; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "lib_errors.h"
37 #include "srcdest_table.h"
39 #include "isis_constants.h"
40 #include "isis_common.h"
41 #include "isis_flags.h"
43 #include "isis_misc.h"
44 #include "isis_adjacency.h"
45 #include "isis_circuit.h"
49 #include "isis_route.h"
50 #include "isis_zebra.h"
52 DEFINE_HOOK(isis_route_update_hook
,
53 (struct isis_area
* area
, struct prefix
*prefix
,
54 struct isis_route_info
*route_info
),
55 (area
, prefix
, route_info
))
57 static struct isis_nexthop
*nexthoplookup(struct list
*nexthops
, int family
,
58 union g_addr
*ip
, ifindex_t ifindex
);
59 static void isis_route_update(struct isis_area
*area
, struct prefix
*prefix
,
60 struct prefix_ipv6
*src_p
,
61 struct isis_route_info
*route_info
);
63 static struct isis_nexthop
*isis_nexthop_create(int family
, union g_addr
*ip
,
66 struct isis_nexthop
*nexthop
;
68 nexthop
= XCALLOC(MTYPE_ISIS_NEXTHOP
, sizeof(struct isis_nexthop
));
70 nexthop
->family
= family
;
71 nexthop
->ifindex
= ifindex
;
73 isis_sr_nexthop_reset(&nexthop
->sr
);
78 static void isis_nexthop_delete(struct isis_nexthop
*nexthop
)
80 XFREE(MTYPE_ISIS_NEXTHOP
, nexthop
);
83 static struct isis_nexthop
*nexthoplookup(struct list
*nexthops
, int family
,
84 union g_addr
*ip
, ifindex_t ifindex
)
86 struct listnode
*node
;
87 struct isis_nexthop
*nh
;
89 for (ALL_LIST_ELEMENTS_RO(nexthops
, node
, nh
)) {
90 if (nh
->family
!= family
)
92 if (nh
->ifindex
!= ifindex
)
97 if (IPV4_ADDR_CMP(&nh
->ip
.ipv4
, &ip
->ipv4
))
101 if (IPV6_ADDR_CMP(&nh
->ip
.ipv6
, &ip
->ipv6
))
105 flog_err(EC_LIB_DEVELOPMENT
,
106 "%s: unknown address family [%d]", __func__
,
117 static void adjinfo2nexthop(int family
, struct list
*nexthops
,
118 struct isis_adjacency
*adj
)
120 struct isis_nexthop
*nh
;
121 union g_addr ip
= {};
125 for (unsigned int i
= 0; i
< adj
->ipv4_address_count
; i
++) {
126 ip
.ipv4
= adj
->ipv4_addresses
[i
];
128 if (!nexthoplookup(nexthops
, AF_INET
, &ip
,
129 adj
->circuit
->interface
->ifindex
)) {
130 nh
= isis_nexthop_create(
132 adj
->circuit
->interface
->ifindex
);
133 memcpy(nh
->sysid
, adj
->sysid
, sizeof(nh
->sysid
));
134 listnode_add(nexthops
, nh
);
140 for (unsigned int i
= 0; i
< adj
->ipv6_address_count
; i
++) {
141 ip
.ipv6
= adj
->ipv6_addresses
[i
];
143 if (!nexthoplookup(nexthops
, AF_INET6
, &ip
,
144 adj
->circuit
->interface
->ifindex
)) {
145 nh
= isis_nexthop_create(
147 adj
->circuit
->interface
->ifindex
);
148 memcpy(nh
->sysid
, adj
->sysid
, sizeof(nh
->sysid
));
149 listnode_add(nexthops
, nh
);
155 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown address family [%d]",
161 static struct isis_route_info
*isis_route_info_new(struct prefix
*prefix
,
162 struct prefix_ipv6
*src_p
,
165 struct list
*adjacencies
)
167 struct isis_route_info
*rinfo
;
168 struct isis_adjacency
*adj
;
169 struct listnode
*node
;
171 rinfo
= XCALLOC(MTYPE_ISIS_ROUTE_INFO
, sizeof(struct isis_route_info
));
173 rinfo
->nexthops
= list_new();
174 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, adj
)) {
175 /* check for force resync this route */
176 if (CHECK_FLAG(adj
->circuit
->flags
,
177 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
178 SET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
180 /* update neighbor router address */
181 switch (prefix
->family
) {
183 if (depth
== 2 && prefix
->prefixlen
== 32)
184 adj
->router_address
= prefix
->u
.prefix4
;
187 if (depth
== 2 && prefix
->prefixlen
== 128
188 && (!src_p
|| !src_p
->prefixlen
)) {
189 adj
->router_address6
= prefix
->u
.prefix6
;
193 flog_err(EC_LIB_DEVELOPMENT
,
194 "%s: unknown address family [%d]", __func__
,
198 adjinfo2nexthop(prefix
->family
, rinfo
->nexthops
, adj
);
202 rinfo
->depth
= depth
;
207 static void isis_route_info_delete(struct isis_route_info
*route_info
)
209 if (route_info
->nexthops
) {
210 route_info
->nexthops
->del
=
211 (void (*)(void *))isis_nexthop_delete
;
212 list_delete(&route_info
->nexthops
);
215 XFREE(MTYPE_ISIS_ROUTE_INFO
, route_info
);
218 static int isis_route_info_same_attrib(struct isis_route_info
*new,
219 struct isis_route_info
*old
)
221 if (new->cost
!= old
->cost
)
223 if (new->depth
!= old
->depth
)
229 static int isis_route_info_same(struct isis_route_info
*new,
230 struct isis_route_info
*old
, uint8_t family
)
232 struct listnode
*node
;
233 struct isis_nexthop
*nexthop
;
235 if (!CHECK_FLAG(old
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
238 if (CHECK_FLAG(new->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
))
241 if (!isis_route_info_same_attrib(new, old
))
244 for (ALL_LIST_ELEMENTS_RO(new->nexthops
, node
, nexthop
))
245 if (!nexthoplookup(old
->nexthops
, nexthop
->family
, &nexthop
->ip
,
249 for (ALL_LIST_ELEMENTS_RO(old
->nexthops
, node
, nexthop
))
250 if (!nexthoplookup(new->nexthops
, nexthop
->family
, &nexthop
->ip
,
257 struct isis_route_info
*isis_route_create(struct prefix
*prefix
,
258 struct prefix_ipv6
*src_p
,
261 struct list
*adjacencies
,
262 struct isis_area
*area
,
263 struct route_table
*table
)
265 struct route_node
*route_node
;
266 struct isis_route_info
*rinfo_new
, *rinfo_old
, *route_info
= NULL
;
267 char buff
[PREFIX2STR_BUFFER
];
270 family
= prefix
->family
;
272 prefix2str(prefix
, buff
, sizeof(buff
));
277 rinfo_new
= isis_route_info_new(prefix
, src_p
, cost
,
279 route_node
= srcdest_rnode_get(table
, prefix
, src_p
);
281 rinfo_old
= route_node
->info
;
283 if (IS_DEBUG_RTE_EVENTS
)
284 zlog_debug("ISIS-Rte (%s) route created: %s",
285 area
->area_tag
, buff
);
286 route_info
= rinfo_new
;
287 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
289 route_unlock_node(route_node
);
290 if (IS_DEBUG_RTE_EVENTS
)
291 zlog_debug("ISIS-Rte (%s) route already exists: %s",
292 area
->area_tag
, buff
);
293 if (isis_route_info_same(rinfo_new
, rinfo_old
, family
)) {
294 if (IS_DEBUG_RTE_EVENTS
)
295 zlog_debug("ISIS-Rte (%s) route unchanged: %s",
296 area
->area_tag
, buff
);
297 isis_route_info_delete(rinfo_new
);
298 route_info
= rinfo_old
;
300 if (IS_DEBUG_RTE_EVENTS
)
301 zlog_debug("ISIS-Rte (%s) route changed: %s",
302 area
->area_tag
, buff
);
303 isis_route_info_delete(rinfo_old
);
304 route_info
= rinfo_new
;
305 UNSET_FLAG(route_info
->flag
,
306 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
310 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
311 route_node
->info
= route_info
;
316 static void isis_route_delete(struct isis_area
*area
, struct route_node
*rode
,
317 struct route_table
*table
)
319 struct isis_route_info
*rinfo
;
320 char buff
[SRCDEST2STR_BUFFER
];
321 struct prefix
*prefix
;
322 struct prefix_ipv6
*src_p
;
325 srcdest_rnode2str(rode
, buff
, sizeof(buff
));
327 srcdest_rnode_prefixes(rode
, (const struct prefix
**)&prefix
,
328 (const struct prefix
**)&src_p
);
332 if (IS_DEBUG_RTE_EVENTS
)
334 "ISIS-Rte: tried to delete non-existant route %s",
339 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
340 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
341 if (IS_DEBUG_RTE_EVENTS
)
342 zlog_debug("ISIS-Rte: route delete %s", buff
);
343 isis_route_update(area
, prefix
, src_p
, rinfo
);
345 isis_route_info_delete(rinfo
);
347 route_unlock_node(rode
);
350 static void isis_route_update(struct isis_area
*area
, struct prefix
*prefix
,
351 struct prefix_ipv6
*src_p
,
352 struct isis_route_info
*route_info
)
354 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)) {
355 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
358 isis_zebra_route_add_route(area
->isis
, prefix
, src_p
, route_info
);
359 hook_call(isis_route_update_hook
, area
, prefix
, route_info
);
361 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
362 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
364 if (!CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
367 isis_zebra_route_del_route(area
->isis
, prefix
, src_p
, route_info
);
368 hook_call(isis_route_update_hook
, area
, prefix
, route_info
);
370 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
374 static void _isis_route_verify_table(struct isis_area
*area
,
375 struct route_table
*table
,
376 struct route_table
**tables
)
378 struct route_node
*rnode
, *drnode
;
379 struct isis_route_info
*rinfo
;
380 char buff
[SRCDEST2STR_BUFFER
];
382 for (rnode
= route_top(table
); rnode
;
383 rnode
= srcdest_route_next(rnode
)) {
384 if (rnode
->info
== NULL
)
388 struct prefix
*dst_p
;
389 struct prefix_ipv6
*src_p
;
391 srcdest_rnode_prefixes(rnode
,
392 (const struct prefix
**)&dst_p
,
393 (const struct prefix
**)&src_p
);
395 if (IS_DEBUG_RTE_EVENTS
) {
396 srcdest2str(dst_p
, src_p
, buff
, sizeof(buff
));
398 "ISIS-Rte (%s): route validate: %s %s %s %s",
400 (CHECK_FLAG(rinfo
->flag
,
401 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)
404 (CHECK_FLAG(rinfo
->flag
,
405 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)
408 (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)
414 isis_route_update(area
, dst_p
, src_p
, rinfo
);
416 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
))
419 /* Area is either L1 or L2 => we use level route tables
421 * validating => no problems with deleting routes. */
423 isis_route_delete(area
, rnode
, table
);
427 /* If area is L1L2, we work with merge table and
429 * delete node from level tables as well before deleting
431 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
432 drnode
= srcdest_rnode_lookup(tables
[level
- 1],
437 route_unlock_node(drnode
);
439 if (drnode
->info
!= rnode
->info
)
443 route_unlock_node(drnode
);
446 isis_route_delete(area
, rnode
, table
);
450 void isis_route_verify_table(struct isis_area
*area
, struct route_table
*table
)
452 _isis_route_verify_table(area
, table
, NULL
);
455 /* Function to validate route tables for L1L2 areas. In this case we can't use
456 * level route tables directly, we have to merge them at first. L1 routes are
457 * preferred over the L2 ones.
459 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
460 * merge table at first, then L2 paths are added if L1 path for same prefix
461 * doesn't already exists there.
463 * FIXME: Is it right place to do it at all? Maybe we should push both levels
464 * to the RIB with different zebra route types and let RIB handle this? */
465 void isis_route_verify_merge(struct isis_area
*area
,
466 struct route_table
*level1_table
,
467 struct route_table
*level2_table
)
469 struct route_table
*tables
[] = { level1_table
, level2_table
};
470 struct route_table
*merge
;
471 struct route_node
*rnode
, *mrnode
;
473 merge
= srcdest_table_init();
475 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
476 for (rnode
= route_top(tables
[level
- 1]); rnode
;
477 rnode
= srcdest_route_next(rnode
)) {
478 struct isis_route_info
*rinfo
= rnode
->info
;
482 struct prefix
*prefix
;
483 struct prefix_ipv6
*src_p
;
485 srcdest_rnode_prefixes(rnode
,
486 (const struct prefix
**)&prefix
,
487 (const struct prefix
**)&src_p
);
488 mrnode
= srcdest_rnode_get(merge
, prefix
, src_p
);
489 struct isis_route_info
*mrinfo
= mrnode
->info
;
491 route_unlock_node(mrnode
);
492 if (CHECK_FLAG(mrinfo
->flag
,
493 ISIS_ROUTE_FLAG_ACTIVE
)) {
494 /* Clear the ZEBRA_SYNCED flag on the
495 * L2 route when L1 wins, otherwise L2
496 * won't get reinstalled when L1
501 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
504 } else if (CHECK_FLAG(rinfo
->flag
,
505 ISIS_ROUTE_FLAG_ACTIVE
)) {
506 /* Clear the ZEBRA_SYNCED flag on the L1
507 * route when L2 wins, otherwise L1
508 * won't get reinstalled when it
513 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
518 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
522 mrnode
->info
= rnode
->info
;
526 _isis_route_verify_table(area
, merge
, tables
);
527 route_table_finish(merge
);
530 void isis_route_invalidate_table(struct isis_area
*area
,
531 struct route_table
*table
)
533 struct route_node
*rode
;
534 struct isis_route_info
*rinfo
;
535 for (rode
= route_top(table
); rode
; rode
= srcdest_route_next(rode
)) {
536 if (rode
->info
== NULL
)
540 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);