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
;
77 static void isis_nexthop_delete(struct isis_nexthop
*nexthop
)
79 XFREE(MTYPE_ISIS_NEXTHOP
, nexthop
);
82 static struct isis_nexthop
*nexthoplookup(struct list
*nexthops
, int family
,
83 union g_addr
*ip
, ifindex_t ifindex
)
85 struct listnode
*node
;
86 struct isis_nexthop
*nh
;
88 for (ALL_LIST_ELEMENTS_RO(nexthops
, node
, nh
)) {
89 if (nh
->family
!= family
)
91 if (nh
->ifindex
!= ifindex
)
96 if (IPV4_ADDR_CMP(&nh
->ip
.ipv4
, &ip
->ipv4
))
100 if (IPV6_ADDR_CMP(&nh
->ip
.ipv6
, &ip
->ipv6
))
104 flog_err(EC_LIB_DEVELOPMENT
,
105 "%s: unknown address family [%d]", __func__
,
116 static void adjinfo2nexthop(int family
, struct list
*nexthops
,
117 struct isis_adjacency
*adj
)
119 struct isis_nexthop
*nh
;
120 union g_addr ip
= {};
124 for (unsigned int i
= 0; i
< adj
->ipv4_address_count
; i
++) {
125 ip
.ipv4
= adj
->ipv4_addresses
[i
];
127 if (!nexthoplookup(nexthops
, AF_INET
, &ip
,
128 adj
->circuit
->interface
->ifindex
)) {
129 nh
= isis_nexthop_create(
131 adj
->circuit
->interface
->ifindex
);
132 listnode_add(nexthops
, nh
);
138 for (unsigned int i
= 0; i
< adj
->ipv6_address_count
; i
++) {
139 ip
.ipv6
= adj
->ipv6_addresses
[i
];
141 if (!nexthoplookup(nexthops
, AF_INET6
, &ip
,
142 adj
->circuit
->interface
->ifindex
)) {
143 nh
= isis_nexthop_create(
145 adj
->circuit
->interface
->ifindex
);
146 listnode_add(nexthops
, nh
);
152 flog_err(EC_LIB_DEVELOPMENT
, "%s: unknown address family [%d]",
158 static struct isis_route_info
*isis_route_info_new(struct prefix
*prefix
,
159 struct prefix_ipv6
*src_p
,
162 struct list
*adjacencies
)
164 struct isis_route_info
*rinfo
;
165 struct isis_adjacency
*adj
;
166 struct listnode
*node
;
168 rinfo
= XCALLOC(MTYPE_ISIS_ROUTE_INFO
, sizeof(struct isis_route_info
));
170 rinfo
->nexthops
= list_new();
171 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, adj
)) {
172 /* check for force resync this route */
173 if (CHECK_FLAG(adj
->circuit
->flags
,
174 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
175 SET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
177 /* update neighbor router address */
178 switch (prefix
->family
) {
180 if (depth
== 2 && prefix
->prefixlen
== 32)
181 adj
->router_address
= prefix
->u
.prefix4
;
184 if (depth
== 2 && prefix
->prefixlen
== 128
185 && (!src_p
|| !src_p
->prefixlen
)) {
186 adj
->router_address6
= prefix
->u
.prefix6
;
190 flog_err(EC_LIB_DEVELOPMENT
,
191 "%s: unknown address family [%d]", __func__
,
195 adjinfo2nexthop(prefix
->family
, rinfo
->nexthops
, adj
);
199 rinfo
->depth
= depth
;
204 static void isis_route_info_delete(struct isis_route_info
*route_info
)
206 if (route_info
->nexthops
) {
207 route_info
->nexthops
->del
=
208 (void (*)(void *))isis_nexthop_delete
;
209 list_delete(&route_info
->nexthops
);
212 XFREE(MTYPE_ISIS_ROUTE_INFO
, route_info
);
215 static int isis_route_info_same_attrib(struct isis_route_info
*new,
216 struct isis_route_info
*old
)
218 if (new->cost
!= old
->cost
)
220 if (new->depth
!= old
->depth
)
226 static int isis_route_info_same(struct isis_route_info
*new,
227 struct isis_route_info
*old
, uint8_t family
)
229 struct listnode
*node
;
230 struct isis_nexthop
*nexthop
;
232 if (!CHECK_FLAG(old
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
235 if (CHECK_FLAG(new->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
))
238 if (!isis_route_info_same_attrib(new, old
))
241 for (ALL_LIST_ELEMENTS_RO(new->nexthops
, node
, nexthop
))
242 if (!nexthoplookup(old
->nexthops
, nexthop
->family
, &nexthop
->ip
,
246 for (ALL_LIST_ELEMENTS_RO(old
->nexthops
, node
, nexthop
))
247 if (!nexthoplookup(new->nexthops
, nexthop
->family
, &nexthop
->ip
,
254 struct isis_route_info
*isis_route_create(struct prefix
*prefix
,
255 struct prefix_ipv6
*src_p
,
258 struct list
*adjacencies
,
259 struct isis_area
*area
,
260 struct route_table
*table
)
262 struct route_node
*route_node
;
263 struct isis_route_info
*rinfo_new
, *rinfo_old
, *route_info
= NULL
;
264 char buff
[PREFIX2STR_BUFFER
];
267 family
= prefix
->family
;
269 prefix2str(prefix
, buff
, sizeof(buff
));
274 rinfo_new
= isis_route_info_new(prefix
, src_p
, cost
,
276 route_node
= srcdest_rnode_get(table
, prefix
, src_p
);
278 rinfo_old
= route_node
->info
;
280 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
281 zlog_debug("ISIS-Rte (%s) route created: %s",
282 area
->area_tag
, buff
);
283 route_info
= rinfo_new
;
284 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
286 route_unlock_node(route_node
);
287 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
288 zlog_debug("ISIS-Rte (%s) route already exists: %s",
289 area
->area_tag
, buff
);
290 if (isis_route_info_same(rinfo_new
, rinfo_old
, family
)) {
291 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
292 zlog_debug("ISIS-Rte (%s) route unchanged: %s",
293 area
->area_tag
, buff
);
294 isis_route_info_delete(rinfo_new
);
295 route_info
= rinfo_old
;
297 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
298 zlog_debug("ISIS-Rte (%s) route changed: %s",
299 area
->area_tag
, buff
);
300 isis_route_info_delete(rinfo_old
);
301 route_info
= rinfo_new
;
302 UNSET_FLAG(route_info
->flag
,
303 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
307 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
308 route_node
->info
= route_info
;
313 static void isis_route_delete(struct isis_area
*area
, struct route_node
*rode
,
314 struct route_table
*table
)
316 struct isis_route_info
*rinfo
;
317 char buff
[SRCDEST2STR_BUFFER
];
318 struct prefix
*prefix
;
319 struct prefix_ipv6
*src_p
;
322 srcdest_rnode2str(rode
, buff
, sizeof(buff
));
324 srcdest_rnode_prefixes(rode
, (const struct prefix
**)&prefix
,
325 (const struct prefix
**)&src_p
);
329 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
331 "ISIS-Rte: tried to delete non-existant route %s",
336 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
337 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
338 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
339 zlog_debug("ISIS-Rte: route delete %s", buff
);
340 isis_route_update(area
, prefix
, src_p
, rinfo
);
342 isis_route_info_delete(rinfo
);
344 route_unlock_node(rode
);
347 static void isis_route_update(struct isis_area
*area
, struct prefix
*prefix
,
348 struct prefix_ipv6
*src_p
,
349 struct isis_route_info
*route_info
)
351 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)) {
352 if (CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
355 isis_zebra_route_add_route(prefix
, src_p
, route_info
);
356 hook_call(isis_route_update_hook
, area
, prefix
, route_info
);
358 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
359 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
361 if (!CHECK_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
364 isis_zebra_route_del_route(prefix
, src_p
, route_info
);
365 hook_call(isis_route_update_hook
, area
, prefix
, route_info
);
367 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
371 static void _isis_route_verify_table(struct isis_area
*area
,
372 struct route_table
*table
,
373 struct route_table
**tables
)
375 struct route_node
*rnode
, *drnode
;
376 struct isis_route_info
*rinfo
;
377 char buff
[SRCDEST2STR_BUFFER
];
379 for (rnode
= route_top(table
); rnode
;
380 rnode
= srcdest_route_next(rnode
)) {
381 if (rnode
->info
== NULL
)
385 struct prefix
*dst_p
;
386 struct prefix_ipv6
*src_p
;
388 srcdest_rnode_prefixes(rnode
,
389 (const struct prefix
**)&dst_p
,
390 (const struct prefix
**)&src_p
);
392 if (isis
->debugs
& DEBUG_RTE_EVENTS
) {
393 srcdest2str(dst_p
, src_p
, buff
, sizeof(buff
));
395 "ISIS-Rte (%s): route validate: %s %s %s %s",
397 (CHECK_FLAG(rinfo
->flag
,
398 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)
401 (CHECK_FLAG(rinfo
->flag
,
402 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)
405 (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)
411 isis_route_update(area
, dst_p
, src_p
, rinfo
);
413 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
))
416 /* Area is either L1 or L2 => we use level route tables
418 * validating => no problems with deleting routes. */
420 isis_route_delete(area
, rnode
, table
);
424 /* If area is L1L2, we work with merge table and
426 * delete node from level tables as well before deleting
428 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
429 drnode
= srcdest_rnode_lookup(tables
[level
- 1],
434 route_unlock_node(drnode
);
436 if (drnode
->info
!= rnode
->info
)
440 route_unlock_node(drnode
);
443 isis_route_delete(area
, rnode
, table
);
447 void isis_route_verify_table(struct isis_area
*area
, struct route_table
*table
)
449 _isis_route_verify_table(area
, table
, NULL
);
452 /* Function to validate route tables for L1L2 areas. In this case we can't use
453 * level route tables directly, we have to merge them at first. L1 routes are
454 * preferred over the L2 ones.
456 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
457 * merge table at first, then L2 paths are added if L1 path for same prefix
458 * doesn't already exists there.
460 * FIXME: Is it right place to do it at all? Maybe we should push both levels
461 * to the RIB with different zebra route types and let RIB handle this? */
462 void isis_route_verify_merge(struct isis_area
*area
,
463 struct route_table
*level1_table
,
464 struct route_table
*level2_table
)
466 struct route_table
*tables
[] = { level1_table
, level2_table
};
467 struct route_table
*merge
;
468 struct route_node
*rnode
, *mrnode
;
470 merge
= srcdest_table_init();
472 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
473 for (rnode
= route_top(tables
[level
- 1]); rnode
;
474 rnode
= srcdest_route_next(rnode
)) {
475 struct isis_route_info
*rinfo
= rnode
->info
;
479 struct prefix
*prefix
;
480 struct prefix_ipv6
*src_p
;
482 srcdest_rnode_prefixes(rnode
,
483 (const struct prefix
**)&prefix
,
484 (const struct prefix
**)&src_p
);
485 mrnode
= srcdest_rnode_get(merge
, prefix
, src_p
);
486 struct isis_route_info
*mrinfo
= mrnode
->info
;
488 route_unlock_node(mrnode
);
489 if (CHECK_FLAG(mrinfo
->flag
,
490 ISIS_ROUTE_FLAG_ACTIVE
)) {
491 /* Clear the ZEBRA_SYNCED flag on the
492 * L2 route when L1 wins, otherwise L2
493 * won't get reinstalled when L1
498 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
501 } else if (CHECK_FLAG(rinfo
->flag
,
502 ISIS_ROUTE_FLAG_ACTIVE
)) {
503 /* Clear the ZEBRA_SYNCED flag on the L1
504 * route when L2 wins, otherwise L1
505 * won't get reinstalled when it
510 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
515 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
519 mrnode
->info
= rnode
->info
;
523 _isis_route_verify_table(area
, merge
, tables
);
524 route_table_finish(merge
);
527 void isis_route_invalidate_table(struct isis_area
*area
,
528 struct route_table
*table
)
530 struct route_node
*rode
;
531 struct isis_route_info
*rinfo
;
532 for (rode
= route_top(table
); rode
; rode
= srcdest_route_next(rode
)) {
533 if (rode
->info
== NULL
)
537 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);