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_spf_private.h"
50 #include "isis_route.h"
51 #include "isis_zebra.h"
53 DEFINE_HOOK(isis_route_update_hook
,
54 (struct isis_area
* area
, struct prefix
*prefix
,
55 struct isis_route_info
*route_info
),
56 (area
, prefix
, route_info
))
58 static struct isis_nexthop
*nexthoplookup(struct list
*nexthops
, int family
,
59 union g_addr
*ip
, ifindex_t ifindex
);
60 static void isis_route_update(struct isis_area
*area
, struct prefix
*prefix
,
61 struct prefix_ipv6
*src_p
,
62 struct isis_route_info
*route_info
);
64 static struct isis_nexthop
*isis_nexthop_create(int family
, union g_addr
*ip
,
67 struct isis_nexthop
*nexthop
;
69 nexthop
= XCALLOC(MTYPE_ISIS_NEXTHOP
, sizeof(struct isis_nexthop
));
71 nexthop
->family
= family
;
72 nexthop
->ifindex
= ifindex
;
74 isis_sr_nexthop_reset(&nexthop
->sr
);
79 static void isis_nexthop_delete(struct isis_nexthop
*nexthop
)
81 XFREE(MTYPE_ISIS_NEXTHOP
, nexthop
);
84 static struct isis_nexthop
*nexthoplookup(struct list
*nexthops
, int family
,
85 union g_addr
*ip
, ifindex_t ifindex
)
87 struct listnode
*node
;
88 struct isis_nexthop
*nh
;
90 for (ALL_LIST_ELEMENTS_RO(nexthops
, node
, nh
)) {
91 if (nh
->family
!= family
)
93 if (nh
->ifindex
!= ifindex
)
98 if (IPV4_ADDR_CMP(&nh
->ip
.ipv4
, &ip
->ipv4
))
102 if (IPV6_ADDR_CMP(&nh
->ip
.ipv6
, &ip
->ipv6
))
106 flog_err(EC_LIB_DEVELOPMENT
,
107 "%s: unknown address family [%d]", __func__
,
118 static void adjinfo2nexthop(int family
, struct list
*nexthops
,
119 struct isis_adjacency
*adj
)
121 struct isis_nexthop
*nh
;
122 union g_addr ip
= {};
126 for (unsigned int i
= 0; i
< adj
->ipv4_address_count
; i
++) {
127 ip
.ipv4
= adj
->ipv4_addresses
[i
];
129 if (!nexthoplookup(nexthops
, AF_INET
, &ip
,
130 adj
->circuit
->interface
->ifindex
)) {
131 nh
= isis_nexthop_create(
133 adj
->circuit
->interface
->ifindex
);
134 memcpy(nh
->sysid
, adj
->sysid
, sizeof(nh
->sysid
));
135 listnode_add(nexthops
, nh
);
141 for (unsigned int i
= 0; i
< adj
->ipv6_address_count
; i
++) {
142 ip
.ipv6
= adj
->ipv6_addresses
[i
];
144 if (!nexthoplookup(nexthops
, AF_INET6
, &ip
,
145 adj
->circuit
->interface
->ifindex
)) {
146 nh
= isis_nexthop_create(
148 adj
->circuit
->interface
->ifindex
);
149 memcpy(nh
->sysid
, adj
->sysid
, sizeof(nh
->sysid
));
150 listnode_add(nexthops
, nh
);
156 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown address family [%d]",
162 static void isis_route_add_dummy_nexthops(struct isis_route_info
*rinfo
,
163 const uint8_t *sysid
)
165 struct isis_nexthop
*nh
;
167 nh
= XCALLOC(MTYPE_ISIS_NEXTHOP
, sizeof(struct isis_nexthop
));
168 memcpy(nh
->sysid
, sysid
, sizeof(nh
->sysid
));
169 isis_sr_nexthop_reset(&nh
->sr
);
170 listnode_add(rinfo
->nexthops
, nh
);
173 static struct isis_route_info
*isis_route_info_new(struct prefix
*prefix
,
174 struct prefix_ipv6
*src_p
,
177 struct list
*adjacencies
)
179 struct isis_route_info
*rinfo
;
180 struct isis_vertex_adj
*vadj
;
181 struct listnode
*node
;
183 rinfo
= XCALLOC(MTYPE_ISIS_ROUTE_INFO
, sizeof(struct isis_route_info
));
185 rinfo
->nexthops
= list_new();
186 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, vadj
)) {
187 struct isis_spf_adj
*sadj
= vadj
->sadj
;
188 struct isis_adjacency
*adj
= sadj
->adj
;
191 * Create dummy nexthops when running SPF on a testing
194 if (CHECK_FLAG(im
->options
, F_ISIS_UNIT_TEST
)) {
195 isis_route_add_dummy_nexthops(rinfo
, sadj
->id
);
199 /* check for force resync this route */
200 if (CHECK_FLAG(adj
->circuit
->flags
,
201 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
202 SET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
204 /* update neighbor router address */
205 switch (prefix
->family
) {
207 if (depth
== 2 && prefix
->prefixlen
== 32)
208 adj
->router_address
= prefix
->u
.prefix4
;
211 if (depth
== 2 && prefix
->prefixlen
== 128
212 && (!src_p
|| !src_p
->prefixlen
)) {
213 adj
->router_address6
= prefix
->u
.prefix6
;
217 flog_err(EC_LIB_DEVELOPMENT
,
218 "%s: unknown address family [%d]", __func__
,
222 adjinfo2nexthop(prefix
->family
, rinfo
->nexthops
, adj
);
226 rinfo
->depth
= depth
;
231 static void isis_route_info_delete(struct isis_route_info
*route_info
)
233 if (route_info
->nexthops
) {
234 route_info
->nexthops
->del
=
235 (void (*)(void *))isis_nexthop_delete
;
236 list_delete(&route_info
->nexthops
);
239 XFREE(MTYPE_ISIS_ROUTE_INFO
, route_info
);
242 static int isis_route_info_same_attrib(struct isis_route_info
*new,
243 struct isis_route_info
*old
)
245 if (new->cost
!= old
->cost
)
247 if (new->depth
!= old
->depth
)
253 static int isis_route_info_same(struct isis_route_info
*new,
254 struct isis_route_info
*old
, uint8_t family
)
256 struct listnode
*node
;
257 struct isis_nexthop
*nexthop
;
259 if (!CHECK_FLAG(old
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
262 if (CHECK_FLAG(new->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
))
265 if (!isis_route_info_same_attrib(new, old
))
268 for (ALL_LIST_ELEMENTS_RO(new->nexthops
, node
, nexthop
))
269 if (!nexthoplookup(old
->nexthops
, nexthop
->family
, &nexthop
->ip
,
273 for (ALL_LIST_ELEMENTS_RO(old
->nexthops
, node
, nexthop
))
274 if (!nexthoplookup(new->nexthops
, nexthop
->family
, &nexthop
->ip
,
281 struct isis_route_info
*isis_route_create(struct prefix
*prefix
,
282 struct prefix_ipv6
*src_p
,
285 struct list
*adjacencies
,
286 struct isis_area
*area
,
287 struct route_table
*table
)
289 struct route_node
*route_node
;
290 struct isis_route_info
*rinfo_new
, *rinfo_old
, *route_info
= NULL
;
291 char buff
[PREFIX2STR_BUFFER
];
294 family
= prefix
->family
;
296 prefix2str(prefix
, buff
, sizeof(buff
));
301 rinfo_new
= isis_route_info_new(prefix
, src_p
, cost
,
303 route_node
= srcdest_rnode_get(table
, prefix
, src_p
);
305 rinfo_old
= route_node
->info
;
307 if (IS_DEBUG_RTE_EVENTS
)
308 zlog_debug("ISIS-Rte (%s) route created: %s",
309 area
->area_tag
, buff
);
310 route_info
= rinfo_new
;
311 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
313 route_unlock_node(route_node
);
314 if (IS_DEBUG_RTE_EVENTS
)
315 zlog_debug("ISIS-Rte (%s) route already exists: %s",
316 area
->area_tag
, buff
);
317 if (isis_route_info_same(rinfo_new
, rinfo_old
, family
)) {
318 if (IS_DEBUG_RTE_EVENTS
)
319 zlog_debug("ISIS-Rte (%s) route unchanged: %s",
320 area
->area_tag
, buff
);
321 isis_route_info_delete(rinfo_new
);
322 route_info
= rinfo_old
;
324 if (IS_DEBUG_RTE_EVENTS
)
325 zlog_debug("ISIS-Rte (%s) route changed: %s",
326 area
->area_tag
, buff
);
327 isis_route_info_delete(rinfo_old
);
328 route_info
= rinfo_new
;
329 UNSET_FLAG(route_info
->flag
,
330 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
334 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
335 route_node
->info
= route_info
;
340 static void isis_route_delete(struct isis_area
*area
, struct route_node
*rode
,
341 struct route_table
*table
)
343 struct isis_route_info
*rinfo
;
344 char buff
[SRCDEST2STR_BUFFER
];
345 struct prefix
*prefix
;
346 struct prefix_ipv6
*src_p
;
349 srcdest_rnode2str(rode
, buff
, sizeof(buff
));
351 srcdest_rnode_prefixes(rode
, (const struct prefix
**)&prefix
,
352 (const struct prefix
**)&src_p
);
356 if (IS_DEBUG_RTE_EVENTS
)
358 "ISIS-Rte: tried to delete non-existant route %s",
363 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
364 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
365 if (IS_DEBUG_RTE_EVENTS
)
366 zlog_debug("ISIS-Rte: route delete %s", buff
);
367 isis_route_update(area
, prefix
, src_p
, rinfo
);
369 isis_route_info_delete(rinfo
);
371 route_unlock_node(rode
);
374 static void isis_route_update(struct isis_area
*area
, struct prefix
*prefix
,
375 struct prefix_ipv6
*src_p
,
376 struct isis_route_info
*route_info
)
378 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)) {
379 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
382 isis_zebra_route_add_route(area
->isis
, prefix
, src_p
, route_info
);
383 hook_call(isis_route_update_hook
, area
, prefix
, route_info
);
385 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
386 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
388 if (!CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
391 isis_zebra_route_del_route(area
->isis
, prefix
, src_p
, route_info
);
392 hook_call(isis_route_update_hook
, area
, prefix
, route_info
);
394 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
398 static void _isis_route_verify_table(struct isis_area
*area
,
399 struct route_table
*table
,
400 struct route_table
**tables
)
402 struct route_node
*rnode
, *drnode
;
403 struct isis_route_info
*rinfo
;
404 char buff
[SRCDEST2STR_BUFFER
];
406 for (rnode
= route_top(table
); rnode
;
407 rnode
= srcdest_route_next(rnode
)) {
408 if (rnode
->info
== NULL
)
412 struct prefix
*dst_p
;
413 struct prefix_ipv6
*src_p
;
415 srcdest_rnode_prefixes(rnode
,
416 (const struct prefix
**)&dst_p
,
417 (const struct prefix
**)&src_p
);
419 if (IS_DEBUG_RTE_EVENTS
) {
420 srcdest2str(dst_p
, src_p
, buff
, sizeof(buff
));
422 "ISIS-Rte (%s): route validate: %s %s %s %s",
424 (CHECK_FLAG(rinfo
->flag
,
425 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)
428 (CHECK_FLAG(rinfo
->flag
,
429 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)
432 (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)
438 isis_route_update(area
, dst_p
, src_p
, rinfo
);
440 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
))
443 /* Area is either L1 or L2 => we use level route tables
445 * validating => no problems with deleting routes. */
447 isis_route_delete(area
, rnode
, table
);
451 /* If area is L1L2, we work with merge table and
453 * delete node from level tables as well before deleting
455 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
456 drnode
= srcdest_rnode_lookup(tables
[level
- 1],
461 route_unlock_node(drnode
);
463 if (drnode
->info
!= rnode
->info
)
467 route_unlock_node(drnode
);
470 isis_route_delete(area
, rnode
, table
);
474 void isis_route_verify_table(struct isis_area
*area
, struct route_table
*table
)
476 _isis_route_verify_table(area
, table
, NULL
);
479 /* Function to validate route tables for L1L2 areas. In this case we can't use
480 * level route tables directly, we have to merge them at first. L1 routes are
481 * preferred over the L2 ones.
483 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
484 * merge table at first, then L2 paths are added if L1 path for same prefix
485 * doesn't already exists there.
487 * FIXME: Is it right place to do it at all? Maybe we should push both levels
488 * to the RIB with different zebra route types and let RIB handle this? */
489 void isis_route_verify_merge(struct isis_area
*area
,
490 struct route_table
*level1_table
,
491 struct route_table
*level2_table
)
493 struct route_table
*tables
[] = { level1_table
, level2_table
};
494 struct route_table
*merge
;
495 struct route_node
*rnode
, *mrnode
;
497 merge
= srcdest_table_init();
499 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
500 for (rnode
= route_top(tables
[level
- 1]); rnode
;
501 rnode
= srcdest_route_next(rnode
)) {
502 struct isis_route_info
*rinfo
= rnode
->info
;
506 struct prefix
*prefix
;
507 struct prefix_ipv6
*src_p
;
509 srcdest_rnode_prefixes(rnode
,
510 (const struct prefix
**)&prefix
,
511 (const struct prefix
**)&src_p
);
512 mrnode
= srcdest_rnode_get(merge
, prefix
, src_p
);
513 struct isis_route_info
*mrinfo
= mrnode
->info
;
515 route_unlock_node(mrnode
);
516 if (CHECK_FLAG(mrinfo
->flag
,
517 ISIS_ROUTE_FLAG_ACTIVE
)) {
518 /* Clear the ZEBRA_SYNCED flag on the
519 * L2 route when L1 wins, otherwise L2
520 * won't get reinstalled when L1
525 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
528 } else if (CHECK_FLAG(rinfo
->flag
,
529 ISIS_ROUTE_FLAG_ACTIVE
)) {
530 /* Clear the ZEBRA_SYNCED flag on the L1
531 * route when L2 wins, otherwise L1
532 * won't get reinstalled when it
537 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
542 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
546 mrnode
->info
= rnode
->info
;
550 _isis_route_verify_table(area
, merge
, tables
);
551 route_table_finish(merge
);
554 void isis_route_invalidate_table(struct isis_area
*area
,
555 struct route_table
*table
)
557 struct route_node
*rode
;
558 struct isis_route_info
*rinfo
;
559 for (rode
= route_top(table
); rode
; rode
= srcdest_route_next(rode
)) {
560 if (rode
->info
== NULL
)
564 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);