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; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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"
49 #include "isis_route.h"
50 #include "isis_zebra.h"
52 static struct isis_nexthop
*
53 isis_nexthop_create (struct in_addr
*ip
, ifindex_t ifindex
)
55 struct listnode
*node
;
56 struct isis_nexthop
*nexthop
;
58 for (ALL_LIST_ELEMENTS_RO (isis
->nexthops
, node
, nexthop
))
60 if (nexthop
->ifindex
!= ifindex
)
62 if (ip
&& memcmp (&nexthop
->ip
, ip
, sizeof (struct in_addr
)) != 0)
69 nexthop
= XCALLOC (MTYPE_ISIS_NEXTHOP
, sizeof (struct isis_nexthop
));
71 nexthop
->ifindex
= ifindex
;
72 memcpy (&nexthop
->ip
, ip
, sizeof (struct in_addr
));
73 listnode_add (isis
->nexthops
, nexthop
);
80 isis_nexthop_delete (struct isis_nexthop
*nexthop
)
83 if (nexthop
->lock
== 0)
85 listnode_delete (isis
->nexthops
, nexthop
);
86 XFREE (MTYPE_ISIS_NEXTHOP
, nexthop
);
93 nexthoplookup (struct list
*nexthops
, struct in_addr
*ip
,
96 struct listnode
*node
;
97 struct isis_nexthop
*nh
;
99 for (ALL_LIST_ELEMENTS_RO (nexthops
, node
, nh
))
101 if (!(memcmp (ip
, &nh
->ip
, sizeof (struct in_addr
))) &&
102 ifindex
== nh
->ifindex
)
111 nexthop_print (struct isis_nexthop
*nh
)
115 inet_ntop (AF_INET
, &nh
->ip
, (char *) buf
, BUFSIZ
);
117 zlog_debug (" %s %u", buf
, nh
->ifindex
);
121 nexthops_print (struct list
*nhs
)
123 struct listnode
*node
;
124 struct isis_nexthop
*nh
;
126 for (ALL_LIST_ELEMENTS_RO (nhs
, node
, nh
))
129 #endif /* EXTREME_DEBUG */
131 static struct isis_nexthop6
*
132 isis_nexthop6_new (struct in6_addr
*ip6
, ifindex_t ifindex
)
134 struct isis_nexthop6
*nexthop6
;
136 nexthop6
= XCALLOC (MTYPE_ISIS_NEXTHOP6
, sizeof (struct isis_nexthop6
));
138 nexthop6
->ifindex
= ifindex
;
139 memcpy (&nexthop6
->ip6
, ip6
, sizeof (struct in6_addr
));
145 static struct isis_nexthop6
*
146 isis_nexthop6_create (struct in6_addr
*ip6
, ifindex_t ifindex
)
148 struct listnode
*node
;
149 struct isis_nexthop6
*nexthop6
;
151 for (ALL_LIST_ELEMENTS_RO (isis
->nexthops6
, node
, nexthop6
))
153 if (nexthop6
->ifindex
!= ifindex
)
155 if (ip6
&& memcmp (&nexthop6
->ip6
, ip6
, sizeof (struct in6_addr
)) != 0)
162 nexthop6
= isis_nexthop6_new (ip6
, ifindex
);
168 isis_nexthop6_delete (struct isis_nexthop6
*nexthop6
)
172 if (nexthop6
->lock
== 0)
174 listnode_delete (isis
->nexthops6
, nexthop6
);
175 XFREE (MTYPE_ISIS_NEXTHOP6
, nexthop6
);
182 nexthop6lookup (struct list
*nexthops6
, struct in6_addr
*ip6
,
185 struct listnode
*node
;
186 struct isis_nexthop6
*nh6
;
188 for (ALL_LIST_ELEMENTS_RO (nexthops6
, node
, nh6
))
190 if (!(memcmp (ip6
, &nh6
->ip6
, sizeof (struct in6_addr
))) &&
191 ifindex
== nh6
->ifindex
)
200 nexthop6_print (struct isis_nexthop6
*nh6
)
204 inet_ntop (AF_INET6
, &nh6
->ip6
, (char *) buf
, BUFSIZ
);
206 zlog_debug (" %s %u", buf
, nh6
->ifindex
);
210 nexthops6_print (struct list
*nhs6
)
212 struct listnode
*node
;
213 struct isis_nexthop6
*nh6
;
215 for (ALL_LIST_ELEMENTS_RO (nhs6
, node
, nh6
))
216 nexthop6_print (nh6
);
218 #endif /* EXTREME_DEBUG */
221 adjinfo2nexthop (struct list
*nexthops
, struct isis_adjacency
*adj
)
223 struct isis_nexthop
*nh
;
224 struct listnode
*node
;
225 struct in_addr
*ipv4_addr
;
227 if (adj
->ipv4_addrs
== NULL
)
230 for (ALL_LIST_ELEMENTS_RO (adj
->ipv4_addrs
, node
, ipv4_addr
))
232 if (!nexthoplookup (nexthops
, ipv4_addr
,
233 adj
->circuit
->interface
->ifindex
))
235 nh
= isis_nexthop_create (ipv4_addr
,
236 adj
->circuit
->interface
->ifindex
);
237 nh
->router_address
= adj
->router_address
;
238 listnode_add (nexthops
, nh
);
244 adjinfo2nexthop6 (struct list
*nexthops6
, struct isis_adjacency
*adj
)
246 struct listnode
*node
;
247 struct in6_addr
*ipv6_addr
;
248 struct isis_nexthop6
*nh6
;
250 if (!adj
->ipv6_addrs
)
253 for (ALL_LIST_ELEMENTS_RO (adj
->ipv6_addrs
, node
, ipv6_addr
))
255 if (!nexthop6lookup (nexthops6
, ipv6_addr
,
256 adj
->circuit
->interface
->ifindex
))
258 nh6
= isis_nexthop6_create (ipv6_addr
,
259 adj
->circuit
->interface
->ifindex
);
260 nh6
->router_address6
= adj
->router_address6
;
261 listnode_add (nexthops6
, nh6
);
266 static struct isis_route_info
*
267 isis_route_info_new (struct prefix
*prefix
, uint32_t cost
, uint32_t depth
,
268 struct list
*adjacencies
)
270 struct isis_route_info
*rinfo
;
271 struct isis_adjacency
*adj
;
272 struct listnode
*node
;
274 rinfo
= XCALLOC (MTYPE_ISIS_ROUTE_INFO
, sizeof (struct isis_route_info
));
276 if (prefix
->family
== AF_INET
)
278 rinfo
->nexthops
= list_new ();
279 for (ALL_LIST_ELEMENTS_RO (adjacencies
, node
, adj
))
281 /* check for force resync this route */
282 if (CHECK_FLAG (adj
->circuit
->flags
, ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
283 SET_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
284 /* update neighbor router address */
285 if (depth
== 2 && prefix
->prefixlen
== 32)
286 adj
->router_address
= prefix
->u
.prefix4
;
287 adjinfo2nexthop (rinfo
->nexthops
, adj
);
290 if (prefix
->family
== AF_INET6
)
292 rinfo
->nexthops6
= list_new ();
293 for (ALL_LIST_ELEMENTS_RO (adjacencies
, node
, adj
))
295 /* check for force resync this route */
296 if (CHECK_FLAG (adj
->circuit
->flags
, ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
297 SET_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
298 /* update neighbor router address */
299 if (depth
== 2 && prefix
->prefixlen
== 128)
300 adj
->router_address6
= prefix
->u
.prefix6
;
301 adjinfo2nexthop6 (rinfo
->nexthops6
, adj
);
306 rinfo
->depth
= depth
;
312 isis_route_info_delete (struct isis_route_info
*route_info
)
314 if (route_info
->nexthops
)
316 route_info
->nexthops
->del
= (void (*)(void *)) isis_nexthop_delete
;
317 list_delete (route_info
->nexthops
);
320 if (route_info
->nexthops6
)
322 route_info
->nexthops6
->del
= (void (*)(void *)) isis_nexthop6_delete
;
323 list_delete (route_info
->nexthops6
);
326 XFREE (MTYPE_ISIS_ROUTE_INFO
, route_info
);
330 isis_route_info_same_attrib (struct isis_route_info
*new,
331 struct isis_route_info
*old
)
333 if (new->cost
!= old
->cost
)
335 if (new->depth
!= old
->depth
)
342 isis_route_info_same (struct isis_route_info
*new,
343 struct isis_route_info
*old
, u_char family
)
345 struct listnode
*node
;
346 struct isis_nexthop
*nexthop
;
347 struct isis_nexthop6
*nexthop6
;
349 if (!CHECK_FLAG (old
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
352 if (CHECK_FLAG (new->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
))
355 if (!isis_route_info_same_attrib (new, old
))
358 if (family
== AF_INET
)
360 for (ALL_LIST_ELEMENTS_RO (new->nexthops
, node
, nexthop
))
361 if (nexthoplookup (old
->nexthops
, &nexthop
->ip
, nexthop
->ifindex
)
365 for (ALL_LIST_ELEMENTS_RO (old
->nexthops
, node
, nexthop
))
366 if (nexthoplookup (new->nexthops
, &nexthop
->ip
, nexthop
->ifindex
)
370 else if (family
== AF_INET6
)
372 for (ALL_LIST_ELEMENTS_RO (new->nexthops6
, node
, nexthop6
))
373 if (nexthop6lookup (old
->nexthops6
, &nexthop6
->ip6
,
374 nexthop6
->ifindex
) == 0)
377 for (ALL_LIST_ELEMENTS_RO (old
->nexthops6
, node
, nexthop6
))
378 if (nexthop6lookup (new->nexthops6
, &nexthop6
->ip6
,
379 nexthop6
->ifindex
) == 0)
386 struct isis_route_info
*
387 isis_route_create (struct prefix
*prefix
, u_int32_t cost
, u_int32_t depth
,
388 struct list
*adjacencies
, struct isis_area
*area
,
391 struct route_node
*route_node
;
392 struct isis_route_info
*rinfo_new
, *rinfo_old
, *route_info
= NULL
;
393 char buff
[PREFIX2STR_BUFFER
];
396 family
= prefix
->family
;
398 prefix2str (prefix
, buff
, sizeof (buff
));
400 rinfo_new
= isis_route_info_new (prefix
, cost
, depth
, adjacencies
);
402 if (family
== AF_INET
)
403 route_node
= route_node_get (area
->route_table
[level
- 1], prefix
);
404 else if (family
== AF_INET6
)
405 route_node
= route_node_get (area
->route_table6
[level
- 1], prefix
);
408 isis_route_info_delete (rinfo_new
);
412 rinfo_old
= route_node
->info
;
415 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
416 zlog_debug ("ISIS-Rte (%s) route created: %s", area
->area_tag
, buff
);
417 route_info
= rinfo_new
;
418 UNSET_FLAG (route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
422 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
423 zlog_debug ("ISIS-Rte (%s) route already exists: %s", area
->area_tag
,
425 if (isis_route_info_same (rinfo_new
, rinfo_old
, family
))
427 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
428 zlog_debug ("ISIS-Rte (%s) route unchanged: %s", area
->area_tag
,
430 isis_route_info_delete (rinfo_new
);
431 route_info
= rinfo_old
;
435 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
436 zlog_debug ("ISIS-Rte (%s) route changed: %s", area
->area_tag
,
438 isis_route_info_delete (rinfo_old
);
439 route_info
= rinfo_new
;
440 UNSET_FLAG (route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
444 SET_FLAG (route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
445 route_node
->info
= route_info
;
451 isis_route_delete (struct prefix
*prefix
, struct route_table
*table
)
453 struct route_node
*rode
;
454 struct isis_route_info
*rinfo
;
455 char buff
[PREFIX2STR_BUFFER
];
458 prefix2str (prefix
, buff
, sizeof (buff
));
461 rode
= route_node_get (table
, prefix
);
466 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
467 zlog_debug ("ISIS-Rte: tried to delete non-existant route %s", buff
);
471 if (CHECK_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
473 UNSET_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
474 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
475 zlog_debug ("ISIS-Rte: route delete %s", buff
);
476 isis_zebra_route_update (prefix
, rinfo
);
478 isis_route_info_delete (rinfo
);
484 /* Validating routes in particular table. */
486 isis_route_validate_table (struct isis_area
*area
, struct route_table
*table
)
488 struct route_node
*rnode
, *drnode
;
489 struct isis_route_info
*rinfo
;
490 char buff
[PREFIX2STR_BUFFER
];
492 for (rnode
= route_top (table
); rnode
; rnode
= route_next (rnode
))
494 if (rnode
->info
== NULL
)
498 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
500 prefix2str (&rnode
->p
, buff
, sizeof (buff
));
501 zlog_debug ("ISIS-Rte (%s): route validate: %s %s %s %s",
503 (CHECK_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
) ?
504 "synced" : "not-synced"),
505 (CHECK_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
) ?
506 "resync" : "not-resync"),
507 (CHECK_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
) ?
508 "active" : "inactive"), buff
);
511 isis_zebra_route_update (&rnode
->p
, rinfo
);
512 if (!CHECK_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
))
514 /* Area is either L1 or L2 => we use level route tables directly for
515 * validating => no problems with deleting routes. */
516 if (area
->is_type
!= IS_LEVEL_1_AND_2
)
518 isis_route_delete (&rnode
->p
, table
);
521 /* If area is L1L2, we work with merge table and therefore must
522 * delete node from level tables as well before deleting route info.
523 * FIXME: Is it performance problem? There has to be the better way.
524 * Like not to deal with it here at all (see the next comment)? */
525 if (rnode
->p
.family
== AF_INET
)
527 drnode
= route_node_get (area
->route_table
[0], &rnode
->p
);
528 if (drnode
->info
== rnode
->info
)
530 drnode
= route_node_get (area
->route_table
[1], &rnode
->p
);
531 if (drnode
->info
== rnode
->info
)
535 if (rnode
->p
.family
== AF_INET6
)
537 drnode
= route_node_get (area
->route_table6
[0], &rnode
->p
);
538 if (drnode
->info
== rnode
->info
)
540 drnode
= route_node_get (area
->route_table6
[1], &rnode
->p
);
541 if (drnode
->info
== rnode
->info
)
545 isis_route_delete (&rnode
->p
, table
);
550 /* Function to validate route tables for L1L2 areas. In this case we can't use
551 * level route tables directly, we have to merge them at first. L1 routes are
552 * preferred over the L2 ones.
554 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
555 * merge table at first, then L2 paths are added if L1 path for same prefix
556 * doesn't already exists there.
558 * FIXME: Is it right place to do it at all? Maybe we should push both levels
559 * to the RIB with different zebra route types and let RIB handle this? */
561 isis_route_validate_merge (struct isis_area
*area
, int family
)
563 struct route_table
*table
= NULL
;
564 struct route_table
*merge
;
565 struct route_node
*rnode
, *mrnode
;
567 merge
= route_table_init ();
569 if (family
== AF_INET
)
570 table
= area
->route_table
[0];
571 else if (family
== AF_INET6
)
572 table
= area
->route_table6
[0];
575 zlog_warn ("ISIS-Rte (%s) %s called for unknown family %d",
576 area
->area_tag
, __func__
, family
);
577 route_table_finish(merge
);
581 for (rnode
= route_top (table
); rnode
; rnode
= route_next (rnode
))
583 if (rnode
->info
== NULL
)
585 mrnode
= route_node_get (merge
, &rnode
->p
);
586 mrnode
->info
= rnode
->info
;
589 if (family
== AF_INET
)
590 table
= area
->route_table
[1];
591 else if (family
== AF_INET6
)
592 table
= area
->route_table6
[1];
594 for (rnode
= route_top (table
); rnode
; rnode
= route_next (rnode
))
596 if (rnode
->info
== NULL
)
598 mrnode
= route_node_get (merge
, &rnode
->p
);
599 if (mrnode
->info
!= NULL
)
601 mrnode
->info
= rnode
->info
;
604 isis_route_validate_table (area
, merge
);
605 route_table_finish (merge
);
608 /* Walk through route tables and propagate necessary changes into RIB. In case
609 * of L1L2 area, level tables have to be merged at first. */
611 isis_route_validate (struct isis_area
*area
)
613 struct listnode
*node
;
614 struct isis_circuit
*circuit
;
616 if (area
->is_type
== IS_LEVEL_1
)
617 isis_route_validate_table (area
, area
->route_table
[0]);
618 else if (area
->is_type
== IS_LEVEL_2
)
619 isis_route_validate_table (area
, area
->route_table
[1]);
621 isis_route_validate_merge (area
, AF_INET
);
623 if (area
->is_type
== IS_LEVEL_1
)
624 isis_route_validate_table (area
, area
->route_table6
[0]);
625 else if (area
->is_type
== IS_LEVEL_2
)
626 isis_route_validate_table (area
, area
->route_table6
[1]);
628 isis_route_validate_merge (area
, AF_INET6
);
630 if (!area
->circuit_list
) {
633 /* walk all circuits and reset any spf specific flags */
634 for (ALL_LIST_ELEMENTS_RO (area
->circuit_list
, node
, circuit
))
635 UNSET_FLAG(circuit
->flags
, ISIS_CIRCUIT_FLAPPED_AFTER_SPF
);
641 isis_route_invalidate_table (struct isis_area
*area
, struct route_table
*table
)
643 struct route_node
*rode
;
644 struct isis_route_info
*rinfo
;
645 for (rode
= route_top (table
); rode
; rode
= route_next (rode
))
647 if (rode
->info
== NULL
)
651 UNSET_FLAG (rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
656 isis_route_invalidate (struct isis_area
*area
)
658 if (area
->is_type
& IS_LEVEL_1
)
659 isis_route_invalidate_table (area
, area
->route_table
[0]);
660 if (area
->is_type
& IS_LEVEL_2
)
661 isis_route_invalidate_table (area
, area
->route_table
[1]);