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
37 #include "isis_constants.h"
38 #include "isis_common.h"
39 #include "isis_flags.h"
42 #include "isis_misc.h"
43 #include "isis_adjacency.h"
44 #include "isis_circuit.h"
48 #include "isis_route.h"
49 #include "isis_zebra.h"
51 static struct isis_nexthop
*isis_nexthop_create(struct in_addr
*ip
,
54 struct listnode
*node
;
55 struct isis_nexthop
*nexthop
;
57 for (ALL_LIST_ELEMENTS_RO(isis
->nexthops
, node
, nexthop
)) {
58 if (nexthop
->ifindex
!= ifindex
)
60 if (ip
&& memcmp(&nexthop
->ip
, ip
, sizeof(struct in_addr
)) != 0)
67 nexthop
= XCALLOC(MTYPE_ISIS_NEXTHOP
, sizeof(struct isis_nexthop
));
69 nexthop
->ifindex
= ifindex
;
70 memcpy(&nexthop
->ip
, ip
, sizeof(struct in_addr
));
71 listnode_add(isis
->nexthops
, nexthop
);
77 static void isis_nexthop_delete(struct isis_nexthop
*nexthop
)
80 if (nexthop
->lock
== 0) {
81 listnode_delete(isis
->nexthops
, nexthop
);
82 XFREE(MTYPE_ISIS_NEXTHOP
, nexthop
);
88 static int nexthoplookup(struct list
*nexthops
, struct in_addr
*ip
,
91 struct listnode
*node
;
92 struct isis_nexthop
*nh
;
94 for (ALL_LIST_ELEMENTS_RO(nexthops
, node
, nh
)) {
95 if (!(memcmp(ip
, &nh
->ip
, sizeof(struct in_addr
)))
96 && ifindex
== nh
->ifindex
)
104 static void nexthop_print(struct isis_nexthop
*nh
)
108 inet_ntop(AF_INET
, &nh
->ip
, (char *)buf
, BUFSIZ
);
110 zlog_debug(" %s %u", buf
, nh
->ifindex
);
113 static void nexthops_print(struct list
*nhs
)
115 struct listnode
*node
;
116 struct isis_nexthop
*nh
;
118 for (ALL_LIST_ELEMENTS_RO(nhs
, node
, nh
))
121 #endif /* EXTREME_DEBUG */
123 static struct isis_nexthop6
*isis_nexthop6_new(struct in6_addr
*ip6
,
126 struct isis_nexthop6
*nexthop6
;
128 nexthop6
= XCALLOC(MTYPE_ISIS_NEXTHOP6
, sizeof(struct isis_nexthop6
));
130 nexthop6
->ifindex
= ifindex
;
131 memcpy(&nexthop6
->ip6
, ip6
, sizeof(struct in6_addr
));
137 static struct isis_nexthop6
*isis_nexthop6_create(struct in6_addr
*ip6
,
140 struct listnode
*node
;
141 struct isis_nexthop6
*nexthop6
;
143 for (ALL_LIST_ELEMENTS_RO(isis
->nexthops6
, node
, nexthop6
)) {
144 if (nexthop6
->ifindex
!= ifindex
)
147 && memcmp(&nexthop6
->ip6
, ip6
, sizeof(struct in6_addr
))
155 nexthop6
= isis_nexthop6_new(ip6
, ifindex
);
160 static void isis_nexthop6_delete(struct isis_nexthop6
*nexthop6
)
164 if (nexthop6
->lock
== 0) {
165 listnode_delete(isis
->nexthops6
, nexthop6
);
166 XFREE(MTYPE_ISIS_NEXTHOP6
, nexthop6
);
172 static int nexthop6lookup(struct list
*nexthops6
, struct in6_addr
*ip6
,
175 struct listnode
*node
;
176 struct isis_nexthop6
*nh6
;
178 for (ALL_LIST_ELEMENTS_RO(nexthops6
, node
, nh6
)) {
179 if (!(memcmp(ip6
, &nh6
->ip6
, sizeof(struct in6_addr
)))
180 && ifindex
== nh6
->ifindex
)
188 static void nexthop6_print(struct isis_nexthop6
*nh6
)
192 inet_ntop(AF_INET6
, &nh6
->ip6
, (char *)buf
, BUFSIZ
);
194 zlog_debug(" %s %u", buf
, nh6
->ifindex
);
197 static void nexthops6_print(struct list
*nhs6
)
199 struct listnode
*node
;
200 struct isis_nexthop6
*nh6
;
202 for (ALL_LIST_ELEMENTS_RO(nhs6
, node
, nh6
))
205 #endif /* EXTREME_DEBUG */
207 static void adjinfo2nexthop(struct list
*nexthops
, struct isis_adjacency
*adj
)
209 struct isis_nexthop
*nh
;
211 if (!adj
->ipv4_address_count
)
214 for (unsigned int i
= 0; i
< adj
->ipv4_address_count
; i
++) {
215 struct in_addr
*ipv4_addr
= &adj
->ipv4_addresses
[i
];
216 if (!nexthoplookup(nexthops
, ipv4_addr
,
217 adj
->circuit
->interface
->ifindex
)) {
218 nh
= isis_nexthop_create(
219 ipv4_addr
, adj
->circuit
->interface
->ifindex
);
220 nh
->router_address
= adj
->router_address
;
221 listnode_add(nexthops
, nh
);
226 static void adjinfo2nexthop6(struct list
*nexthops6
, struct isis_adjacency
*adj
)
228 struct isis_nexthop6
*nh6
;
230 if (!adj
->ipv6_address_count
)
233 for (unsigned int i
= 0; i
< adj
->ipv6_address_count
; i
++) {
234 struct in6_addr
*ipv6_addr
= &adj
->ipv6_addresses
[i
];
235 if (!nexthop6lookup(nexthops6
, ipv6_addr
,
236 adj
->circuit
->interface
->ifindex
)) {
237 nh6
= isis_nexthop6_create(
238 ipv6_addr
, adj
->circuit
->interface
->ifindex
);
239 nh6
->router_address6
= adj
->router_address6
;
240 listnode_add(nexthops6
, nh6
);
245 static struct isis_route_info
*isis_route_info_new(struct prefix
*prefix
,
248 struct list
*adjacencies
)
250 struct isis_route_info
*rinfo
;
251 struct isis_adjacency
*adj
;
252 struct listnode
*node
;
254 rinfo
= XCALLOC(MTYPE_ISIS_ROUTE_INFO
, sizeof(struct isis_route_info
));
256 if (prefix
->family
== AF_INET
) {
257 rinfo
->nexthops
= list_new();
258 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, adj
)) {
259 /* check for force resync this route */
260 if (CHECK_FLAG(adj
->circuit
->flags
,
261 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
262 SET_FLAG(rinfo
->flag
,
263 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
264 /* update neighbor router address */
265 if (depth
== 2 && prefix
->prefixlen
== 32)
266 adj
->router_address
= prefix
->u
.prefix4
;
267 adjinfo2nexthop(rinfo
->nexthops
, adj
);
270 if (prefix
->family
== AF_INET6
) {
271 rinfo
->nexthops6
= list_new();
272 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, adj
)) {
273 /* check for force resync this route */
274 if (CHECK_FLAG(adj
->circuit
->flags
,
275 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
276 SET_FLAG(rinfo
->flag
,
277 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
278 /* update neighbor router address */
279 if (depth
== 2 && prefix
->prefixlen
== 128)
280 adj
->router_address6
= prefix
->u
.prefix6
;
281 adjinfo2nexthop6(rinfo
->nexthops6
, adj
);
286 rinfo
->depth
= depth
;
291 static void isis_route_info_delete(struct isis_route_info
*route_info
)
293 if (route_info
->nexthops
) {
294 route_info
->nexthops
->del
=
295 (void (*)(void *))isis_nexthop_delete
;
296 list_delete(route_info
->nexthops
);
299 if (route_info
->nexthops6
) {
300 route_info
->nexthops6
->del
=
301 (void (*)(void *))isis_nexthop6_delete
;
302 list_delete(route_info
->nexthops6
);
305 XFREE(MTYPE_ISIS_ROUTE_INFO
, route_info
);
308 static int isis_route_info_same_attrib(struct isis_route_info
*new,
309 struct isis_route_info
*old
)
311 if (new->cost
!= old
->cost
)
313 if (new->depth
!= old
->depth
)
319 static int isis_route_info_same(struct isis_route_info
*new,
320 struct isis_route_info
*old
, u_char family
)
322 struct listnode
*node
;
323 struct isis_nexthop
*nexthop
;
324 struct isis_nexthop6
*nexthop6
;
326 if (!CHECK_FLAG(old
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
329 if (CHECK_FLAG(new->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
))
332 if (!isis_route_info_same_attrib(new, old
))
335 if (family
== AF_INET
) {
336 for (ALL_LIST_ELEMENTS_RO(new->nexthops
, node
, nexthop
))
337 if (nexthoplookup(old
->nexthops
, &nexthop
->ip
,
342 for (ALL_LIST_ELEMENTS_RO(old
->nexthops
, node
, nexthop
))
343 if (nexthoplookup(new->nexthops
, &nexthop
->ip
,
347 } else if (family
== AF_INET6
) {
348 for (ALL_LIST_ELEMENTS_RO(new->nexthops6
, node
, nexthop6
))
349 if (nexthop6lookup(old
->nexthops6
, &nexthop6
->ip6
,
354 for (ALL_LIST_ELEMENTS_RO(old
->nexthops6
, node
, nexthop6
))
355 if (nexthop6lookup(new->nexthops6
, &nexthop6
->ip6
,
364 struct isis_route_info
*isis_route_create(struct prefix
*prefix
, u_int32_t cost
,
366 struct list
*adjacencies
,
367 struct isis_area
*area
, int level
)
369 struct route_node
*route_node
;
370 struct isis_route_info
*rinfo_new
, *rinfo_old
, *route_info
= NULL
;
371 char buff
[PREFIX2STR_BUFFER
];
374 family
= prefix
->family
;
376 prefix2str(prefix
, buff
, sizeof(buff
));
378 rinfo_new
= isis_route_info_new(prefix
, cost
, depth
, adjacencies
);
380 if (family
== AF_INET
)
382 route_node_get(area
->route_table
[level
- 1], prefix
);
383 else if (family
== AF_INET6
)
385 route_node_get(area
->route_table6
[level
- 1], prefix
);
387 isis_route_info_delete(rinfo_new
);
391 rinfo_old
= route_node
->info
;
393 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
394 zlog_debug("ISIS-Rte (%s) route created: %s",
395 area
->area_tag
, buff
);
396 route_info
= rinfo_new
;
397 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
399 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
400 zlog_debug("ISIS-Rte (%s) route already exists: %s",
401 area
->area_tag
, buff
);
402 if (isis_route_info_same(rinfo_new
, rinfo_old
, family
)) {
403 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
404 zlog_debug("ISIS-Rte (%s) route unchanged: %s",
405 area
->area_tag
, buff
);
406 isis_route_info_delete(rinfo_new
);
407 route_info
= rinfo_old
;
409 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
410 zlog_debug("ISIS-Rte (%s) route changed: %s",
411 area
->area_tag
, buff
);
412 isis_route_info_delete(rinfo_old
);
413 route_info
= rinfo_new
;
414 UNSET_FLAG(route_info
->flag
,
415 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
419 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
420 route_node
->info
= route_info
;
425 static void isis_route_delete(struct prefix
*prefix
, struct route_table
*table
)
427 struct route_node
*rode
;
428 struct isis_route_info
*rinfo
;
429 char buff
[PREFIX2STR_BUFFER
];
432 prefix2str(prefix
, buff
, sizeof(buff
));
435 rode
= route_node_get(table
, prefix
);
439 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
441 "ISIS-Rte: tried to delete non-existant route %s",
446 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
447 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
448 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
449 zlog_debug("ISIS-Rte: route delete %s", buff
);
450 isis_zebra_route_update(prefix
, rinfo
);
452 isis_route_info_delete(rinfo
);
458 /* Validating routes in particular table. */
459 static void isis_route_validate_table(struct isis_area
*area
,
460 struct route_table
*table
)
462 struct route_node
*rnode
, *drnode
;
463 struct isis_route_info
*rinfo
;
464 char buff
[PREFIX2STR_BUFFER
];
466 for (rnode
= route_top(table
); rnode
; rnode
= route_next(rnode
)) {
467 if (rnode
->info
== NULL
)
471 if (isis
->debugs
& DEBUG_RTE_EVENTS
) {
472 prefix2str(&rnode
->p
, buff
, sizeof(buff
));
474 "ISIS-Rte (%s): route validate: %s %s %s %s",
476 (CHECK_FLAG(rinfo
->flag
,
477 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)
480 (CHECK_FLAG(rinfo
->flag
,
481 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)
484 (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)
490 isis_zebra_route_update(&rnode
->p
, rinfo
);
491 if (!CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)) {
492 /* Area is either L1 or L2 => we use level route tables
494 * validating => no problems with deleting routes. */
495 if (area
->is_type
!= IS_LEVEL_1_AND_2
) {
496 isis_route_delete(&rnode
->p
, table
);
499 /* If area is L1L2, we work with merge table and
501 * delete node from level tables as well before deleting
503 * FIXME: Is it performance problem? There has to be the
505 * Like not to deal with it here at all (see the next
507 if (rnode
->p
.family
== AF_INET
) {
508 drnode
= route_node_get(area
->route_table
[0],
510 if (drnode
->info
== rnode
->info
)
512 drnode
= route_node_get(area
->route_table
[1],
514 if (drnode
->info
== rnode
->info
)
518 if (rnode
->p
.family
== AF_INET6
) {
519 drnode
= route_node_get(area
->route_table6
[0],
521 if (drnode
->info
== rnode
->info
)
523 drnode
= route_node_get(area
->route_table6
[1],
525 if (drnode
->info
== rnode
->info
)
529 isis_route_delete(&rnode
->p
, table
);
534 /* Function to validate route tables for L1L2 areas. In this case we can't use
535 * level route tables directly, we have to merge them at first. L1 routes are
536 * preferred over the L2 ones.
538 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
539 * merge table at first, then L2 paths are added if L1 path for same prefix
540 * doesn't already exists there.
542 * FIXME: Is it right place to do it at all? Maybe we should push both levels
543 * to the RIB with different zebra route types and let RIB handle this? */
544 static void isis_route_validate_merge(struct isis_area
*area
, int family
)
546 struct route_table
*table
= NULL
;
547 struct route_table
*merge
;
548 struct route_node
*rnode
, *mrnode
;
550 merge
= route_table_init();
552 if (family
== AF_INET
)
553 table
= area
->route_table
[0];
554 else if (family
== AF_INET6
)
555 table
= area
->route_table6
[0];
557 zlog_warn("ISIS-Rte (%s) %s called for unknown family %d",
558 area
->area_tag
, __func__
, family
);
559 route_table_finish(merge
);
563 for (rnode
= route_top(table
); rnode
; rnode
= route_next(rnode
)) {
564 if (rnode
->info
== NULL
)
566 mrnode
= route_node_get(merge
, &rnode
->p
);
567 mrnode
->info
= rnode
->info
;
570 if (family
== AF_INET
)
571 table
= area
->route_table
[1];
572 else if (family
== AF_INET6
)
573 table
= area
->route_table6
[1];
575 for (rnode
= route_top(table
); rnode
; rnode
= route_next(rnode
)) {
576 if (rnode
->info
== NULL
)
578 mrnode
= route_node_get(merge
, &rnode
->p
);
579 if (mrnode
->info
!= NULL
)
581 mrnode
->info
= rnode
->info
;
584 isis_route_validate_table(area
, merge
);
585 route_table_finish(merge
);
588 /* Walk through route tables and propagate necessary changes into RIB. In case
589 * of L1L2 area, level tables have to be merged at first. */
590 void isis_route_validate(struct isis_area
*area
)
592 struct listnode
*node
;
593 struct isis_circuit
*circuit
;
595 if (area
->is_type
== IS_LEVEL_1
)
596 isis_route_validate_table(area
, area
->route_table
[0]);
597 else if (area
->is_type
== IS_LEVEL_2
)
598 isis_route_validate_table(area
, area
->route_table
[1]);
600 isis_route_validate_merge(area
, AF_INET
);
602 if (area
->is_type
== IS_LEVEL_1
)
603 isis_route_validate_table(area
, area
->route_table6
[0]);
604 else if (area
->is_type
== IS_LEVEL_2
)
605 isis_route_validate_table(area
, area
->route_table6
[1]);
607 isis_route_validate_merge(area
, AF_INET6
);
609 if (!area
->circuit_list
) {
612 /* walk all circuits and reset any spf specific flags */
613 for (ALL_LIST_ELEMENTS_RO(area
->circuit_list
, node
, circuit
))
614 UNSET_FLAG(circuit
->flags
, ISIS_CIRCUIT_FLAPPED_AFTER_SPF
);
619 void isis_route_invalidate_table(struct isis_area
*area
,
620 struct route_table
*table
)
622 struct route_node
*rode
;
623 struct isis_route_info
*rinfo
;
624 for (rode
= route_top(table
); rode
; rode
= route_next(rode
)) {
625 if (rode
->info
== NULL
)
629 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
633 void isis_route_invalidate(struct isis_area
*area
)
635 if (area
->is_type
& IS_LEVEL_1
)
636 isis_route_invalidate_table(area
, area
->route_table
[0]);
637 if (area
->is_type
& IS_LEVEL_2
)
638 isis_route_invalidate_table(area
, area
->route_table
[1]);