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 for (unsigned int i
= 0; i
< adj
->ipv4_address_count
; i
++) {
212 struct in_addr
*ipv4_addr
= &adj
->ipv4_addresses
[i
];
213 if (!nexthoplookup(nexthops
, ipv4_addr
,
214 adj
->circuit
->interface
->ifindex
)) {
215 nh
= isis_nexthop_create(
216 ipv4_addr
, adj
->circuit
->interface
->ifindex
);
217 nh
->router_address
= adj
->router_address
;
218 listnode_add(nexthops
, nh
);
224 static void adjinfo2nexthop6(struct list
*nexthops6
, struct isis_adjacency
*adj
)
226 struct isis_nexthop6
*nh6
;
228 for (unsigned int i
= 0; i
< adj
->ipv6_address_count
; i
++) {
229 struct in6_addr
*ipv6_addr
= &adj
->ipv6_addresses
[i
];
230 if (!nexthop6lookup(nexthops6
, ipv6_addr
,
231 adj
->circuit
->interface
->ifindex
)) {
232 nh6
= isis_nexthop6_create(
233 ipv6_addr
, adj
->circuit
->interface
->ifindex
);
234 nh6
->router_address6
= adj
->router_address6
;
235 listnode_add(nexthops6
, nh6
);
241 static struct isis_route_info
*isis_route_info_new(struct prefix
*prefix
,
244 struct list
*adjacencies
)
246 struct isis_route_info
*rinfo
;
247 struct isis_adjacency
*adj
;
248 struct listnode
*node
;
250 rinfo
= XCALLOC(MTYPE_ISIS_ROUTE_INFO
, sizeof(struct isis_route_info
));
252 if (prefix
->family
== AF_INET
) {
253 rinfo
->nexthops
= list_new();
254 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, adj
)) {
255 /* check for force resync this route */
256 if (CHECK_FLAG(adj
->circuit
->flags
,
257 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
258 SET_FLAG(rinfo
->flag
,
259 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
260 /* update neighbor router address */
261 if (depth
== 2 && prefix
->prefixlen
== 32)
262 adj
->router_address
= prefix
->u
.prefix4
;
263 adjinfo2nexthop(rinfo
->nexthops
, adj
);
266 if (prefix
->family
== AF_INET6
) {
267 rinfo
->nexthops6
= list_new();
268 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, adj
)) {
269 /* check for force resync this route */
270 if (CHECK_FLAG(adj
->circuit
->flags
,
271 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
272 SET_FLAG(rinfo
->flag
,
273 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
274 /* update neighbor router address */
275 if (depth
== 2 && prefix
->prefixlen
== 128)
276 adj
->router_address6
= prefix
->u
.prefix6
;
277 adjinfo2nexthop6(rinfo
->nexthops6
, adj
);
282 rinfo
->depth
= depth
;
287 static void isis_route_info_delete(struct isis_route_info
*route_info
)
289 if (route_info
->nexthops
) {
290 route_info
->nexthops
->del
=
291 (void (*)(void *))isis_nexthop_delete
;
292 list_delete_and_null(&route_info
->nexthops
);
295 if (route_info
->nexthops6
) {
296 route_info
->nexthops6
->del
=
297 (void (*)(void *))isis_nexthop6_delete
;
298 list_delete_and_null(&route_info
->nexthops6
);
301 XFREE(MTYPE_ISIS_ROUTE_INFO
, route_info
);
304 static int isis_route_info_same_attrib(struct isis_route_info
*new,
305 struct isis_route_info
*old
)
307 if (new->cost
!= old
->cost
)
309 if (new->depth
!= old
->depth
)
315 static int isis_route_info_same(struct isis_route_info
*new,
316 struct isis_route_info
*old
, u_char family
)
318 struct listnode
*node
;
319 struct isis_nexthop
*nexthop
;
320 struct isis_nexthop6
*nexthop6
;
322 if (!CHECK_FLAG(old
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
325 if (CHECK_FLAG(new->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
))
328 if (!isis_route_info_same_attrib(new, old
))
331 if (family
== AF_INET
) {
332 for (ALL_LIST_ELEMENTS_RO(new->nexthops
, node
, nexthop
))
333 if (nexthoplookup(old
->nexthops
, &nexthop
->ip
,
338 for (ALL_LIST_ELEMENTS_RO(old
->nexthops
, node
, nexthop
))
339 if (nexthoplookup(new->nexthops
, &nexthop
->ip
,
343 } else if (family
== AF_INET6
) {
344 for (ALL_LIST_ELEMENTS_RO(new->nexthops6
, node
, nexthop6
))
345 if (nexthop6lookup(old
->nexthops6
, &nexthop6
->ip6
,
350 for (ALL_LIST_ELEMENTS_RO(old
->nexthops6
, node
, nexthop6
))
351 if (nexthop6lookup(new->nexthops6
, &nexthop6
->ip6
,
360 struct isis_route_info
*isis_route_create(struct prefix
*prefix
, u_int32_t cost
,
362 struct list
*adjacencies
,
363 struct isis_area
*area
, int level
)
365 struct route_node
*route_node
;
366 struct isis_route_info
*rinfo_new
, *rinfo_old
, *route_info
= NULL
;
367 char buff
[PREFIX2STR_BUFFER
];
370 family
= prefix
->family
;
372 prefix2str(prefix
, buff
, sizeof(buff
));
374 rinfo_new
= isis_route_info_new(prefix
, cost
, depth
, adjacencies
);
376 if (family
== AF_INET
)
378 route_node_get(area
->route_table
[level
- 1], prefix
);
379 else if (family
== AF_INET6
)
381 route_node_get(area
->route_table6
[level
- 1], prefix
);
383 isis_route_info_delete(rinfo_new
);
387 rinfo_old
= route_node
->info
;
389 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
390 zlog_debug("ISIS-Rte (%s) route created: %s",
391 area
->area_tag
, buff
);
392 route_info
= rinfo_new
;
393 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
395 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
396 zlog_debug("ISIS-Rte (%s) route already exists: %s",
397 area
->area_tag
, buff
);
398 if (isis_route_info_same(rinfo_new
, rinfo_old
, family
)) {
399 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
400 zlog_debug("ISIS-Rte (%s) route unchanged: %s",
401 area
->area_tag
, buff
);
402 isis_route_info_delete(rinfo_new
);
403 route_info
= rinfo_old
;
405 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
406 zlog_debug("ISIS-Rte (%s) route changed: %s",
407 area
->area_tag
, buff
);
408 isis_route_info_delete(rinfo_old
);
409 route_info
= rinfo_new
;
410 UNSET_FLAG(route_info
->flag
,
411 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
415 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
416 route_node
->info
= route_info
;
421 static void isis_route_delete(struct prefix
*prefix
, struct route_table
*table
)
423 struct route_node
*rode
;
424 struct isis_route_info
*rinfo
;
425 char buff
[PREFIX2STR_BUFFER
];
428 prefix2str(prefix
, buff
, sizeof(buff
));
431 rode
= route_node_get(table
, prefix
);
435 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
437 "ISIS-Rte: tried to delete non-existant route %s",
442 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
443 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
444 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
445 zlog_debug("ISIS-Rte: route delete %s", buff
);
446 isis_zebra_route_update(prefix
, rinfo
);
448 isis_route_info_delete(rinfo
);
454 /* Validating routes in particular table. */
455 static void isis_route_validate_table(struct isis_area
*area
,
456 struct route_table
*table
)
458 struct route_node
*rnode
, *drnode
;
459 struct isis_route_info
*rinfo
;
460 char buff
[PREFIX2STR_BUFFER
];
462 for (rnode
= route_top(table
); rnode
; rnode
= route_next(rnode
)) {
463 if (rnode
->info
== NULL
)
467 if (isis
->debugs
& DEBUG_RTE_EVENTS
) {
468 prefix2str(&rnode
->p
, buff
, sizeof(buff
));
470 "ISIS-Rte (%s): route validate: %s %s %s %s",
472 (CHECK_FLAG(rinfo
->flag
,
473 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)
476 (CHECK_FLAG(rinfo
->flag
,
477 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)
480 (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)
486 isis_zebra_route_update(&rnode
->p
, rinfo
);
487 if (!CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)) {
488 /* Area is either L1 or L2 => we use level route tables
490 * validating => no problems with deleting routes. */
491 if (area
->is_type
!= IS_LEVEL_1_AND_2
) {
492 isis_route_delete(&rnode
->p
, table
);
495 /* If area is L1L2, we work with merge table and
497 * delete node from level tables as well before deleting
499 * FIXME: Is it performance problem? There has to be the
501 * Like not to deal with it here at all (see the next
503 if (rnode
->p
.family
== AF_INET
) {
504 drnode
= route_node_get(area
->route_table
[0],
506 if (drnode
->info
== rnode
->info
)
508 drnode
= route_node_get(area
->route_table
[1],
510 if (drnode
->info
== rnode
->info
)
514 if (rnode
->p
.family
== AF_INET6
) {
515 drnode
= route_node_get(area
->route_table6
[0],
517 if (drnode
->info
== rnode
->info
)
519 drnode
= route_node_get(area
->route_table6
[1],
521 if (drnode
->info
== rnode
->info
)
525 isis_route_delete(&rnode
->p
, table
);
530 /* Function to validate route tables for L1L2 areas. In this case we can't use
531 * level route tables directly, we have to merge them at first. L1 routes are
532 * preferred over the L2 ones.
534 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
535 * merge table at first, then L2 paths are added if L1 path for same prefix
536 * doesn't already exists there.
538 * FIXME: Is it right place to do it at all? Maybe we should push both levels
539 * to the RIB with different zebra route types and let RIB handle this? */
540 static void isis_route_validate_merge(struct isis_area
*area
, int family
)
542 struct route_table
*table
= NULL
;
543 struct route_table
*merge
;
544 struct route_node
*rnode
, *mrnode
;
546 merge
= route_table_init();
548 if (family
== AF_INET
)
549 table
= area
->route_table
[0];
550 else if (family
== AF_INET6
)
551 table
= area
->route_table6
[0];
553 zlog_warn("ISIS-Rte (%s) %s called for unknown family %d",
554 area
->area_tag
, __func__
, family
);
555 route_table_finish(merge
);
559 for (rnode
= route_top(table
); rnode
; rnode
= route_next(rnode
)) {
560 if (rnode
->info
== NULL
)
562 mrnode
= route_node_get(merge
, &rnode
->p
);
563 mrnode
->info
= rnode
->info
;
566 if (family
== AF_INET
)
567 table
= area
->route_table
[1];
568 else if (family
== AF_INET6
)
569 table
= area
->route_table6
[1];
571 for (rnode
= route_top(table
); rnode
; rnode
= route_next(rnode
)) {
572 if (rnode
->info
== NULL
)
574 mrnode
= route_node_get(merge
, &rnode
->p
);
575 if (mrnode
->info
!= NULL
)
577 mrnode
->info
= rnode
->info
;
580 isis_route_validate_table(area
, merge
);
581 route_table_finish(merge
);
584 /* Walk through route tables and propagate necessary changes into RIB. In case
585 * of L1L2 area, level tables have to be merged at first. */
586 void isis_route_validate(struct isis_area
*area
)
588 struct listnode
*node
;
589 struct isis_circuit
*circuit
;
591 if (area
->is_type
== IS_LEVEL_1
)
592 isis_route_validate_table(area
, area
->route_table
[0]);
593 else if (area
->is_type
== IS_LEVEL_2
)
594 isis_route_validate_table(area
, area
->route_table
[1]);
596 isis_route_validate_merge(area
, AF_INET
);
598 if (area
->is_type
== IS_LEVEL_1
)
599 isis_route_validate_table(area
, area
->route_table6
[0]);
600 else if (area
->is_type
== IS_LEVEL_2
)
601 isis_route_validate_table(area
, area
->route_table6
[1]);
603 isis_route_validate_merge(area
, AF_INET6
);
605 if (!area
->circuit_list
) {
608 /* walk all circuits and reset any spf specific flags */
609 for (ALL_LIST_ELEMENTS_RO(area
->circuit_list
, node
, circuit
))
610 UNSET_FLAG(circuit
->flags
, ISIS_CIRCUIT_FLAPPED_AFTER_SPF
);
615 void isis_route_invalidate_table(struct isis_area
*area
,
616 struct route_table
*table
)
618 struct route_node
*rode
;
619 struct isis_route_info
*rinfo
;
620 for (rode
= route_top(table
); rode
; rode
= route_next(rode
)) {
621 if (rode
->info
== NULL
)
625 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
629 void isis_route_invalidate(struct isis_area
*area
)
631 if (area
->is_type
& IS_LEVEL_1
)
632 isis_route_invalidate_table(area
, area
->route_table
[0]);
633 if (area
->is_type
& IS_LEVEL_2
)
634 isis_route_invalidate_table(area
, area
->route_table
[1]);