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 */
132 static struct isis_nexthop6 *
133 isis_nexthop6_new (struct in6_addr *ip6, ifindex_t ifindex)
135 struct isis_nexthop6 *nexthop6;
137 nexthop6 = XCALLOC (MTYPE_ISIS_NEXTHOP6, sizeof (struct isis_nexthop6));
139 nexthop6->ifindex = ifindex;
140 memcpy (&nexthop6->ip6, ip6, sizeof (struct in6_addr));
146 static struct isis_nexthop6 *
147 isis_nexthop6_create (struct in6_addr *ip6, ifindex_t ifindex)
149 struct listnode *node;
150 struct isis_nexthop6 *nexthop6;
152 for (ALL_LIST_ELEMENTS_RO (isis->nexthops6, node, nexthop6))
154 if (nexthop6->ifindex != ifindex)
156 if (ip6 && memcmp (&nexthop6->ip6, ip6, sizeof (struct in6_addr)) != 0)
163 nexthop6 = isis_nexthop6_new (ip6, ifindex);
169 isis_nexthop6_delete (struct isis_nexthop6 *nexthop6)
173 if (nexthop6->lock == 0)
175 listnode_delete (isis->nexthops6, nexthop6);
176 XFREE (MTYPE_ISIS_NEXTHOP6, nexthop6);
183 nexthop6lookup (struct list *nexthops6, struct in6_addr *ip6,
186 struct listnode *node;
187 struct isis_nexthop6 *nh6;
189 for (ALL_LIST_ELEMENTS_RO (nexthops6, node, nh6))
191 if (!(memcmp (ip6, &nh6->ip6, sizeof (struct in6_addr))) &&
192 ifindex == nh6->ifindex)
201 nexthop6_print (struct isis_nexthop6 *nh6)
205 inet_ntop (AF_INET6, &nh6->ip6, (char *) buf, BUFSIZ);
207 zlog_debug (" %s %u", buf, nh6->ifindex);
211 nexthops6_print (struct list *nhs6)
213 struct listnode *node;
214 struct isis_nexthop6 *nh6;
216 for (ALL_LIST_ELEMENTS_RO (nhs6, node, nh6))
217 nexthop6_print (nh6);
219 #endif /* EXTREME_DEBUG */
220 #endif /* HAVE_IPV6 */
223 adjinfo2nexthop (struct list *nexthops, struct isis_adjacency *adj)
225 struct isis_nexthop *nh;
226 struct listnode *node;
227 struct in_addr *ipv4_addr;
229 if (adj->ipv4_addrs == NULL)
232 for (ALL_LIST_ELEMENTS_RO (adj->ipv4_addrs, node, ipv4_addr))
234 if (!nexthoplookup (nexthops, ipv4_addr,
235 adj->circuit->interface->ifindex))
237 nh = isis_nexthop_create (ipv4_addr,
238 adj->circuit->interface->ifindex);
239 nh->router_address = adj->router_address;
240 listnode_add (nexthops, nh);
247 adjinfo2nexthop6 (struct list *nexthops6, struct isis_adjacency *adj)
249 struct listnode *node;
250 struct in6_addr *ipv6_addr;
251 struct isis_nexthop6 *nh6;
253 if (!adj->ipv6_addrs)
256 for (ALL_LIST_ELEMENTS_RO (adj->ipv6_addrs, node, ipv6_addr))
258 if (!nexthop6lookup (nexthops6, ipv6_addr,
259 adj->circuit->interface->ifindex))
261 nh6 = isis_nexthop6_create (ipv6_addr,
262 adj->circuit->interface->ifindex);
263 nh6->router_address6 = adj->router_address6;
264 listnode_add (nexthops6, nh6);
268 #endif /* HAVE_IPV6 */
270 static struct isis_route_info *
271 isis_route_info_new (struct prefix *prefix, uint32_t cost, uint32_t depth,
272 struct list *adjacencies)
274 struct isis_route_info *rinfo;
275 struct isis_adjacency *adj;
276 struct listnode *node;
278 rinfo = XCALLOC (MTYPE_ISIS_ROUTE_INFO, sizeof (struct isis_route_info));
280 if (prefix->family == AF_INET)
282 rinfo->nexthops = list_new ();
283 for (ALL_LIST_ELEMENTS_RO (adjacencies, node, adj))
285 /* check for force resync this route */
286 if (CHECK_FLAG (adj->circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
287 SET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
288 /* update neighbor router address */
289 if (depth == 2 && prefix->prefixlen == 32)
290 adj->router_address = prefix->u.prefix4;
291 adjinfo2nexthop (rinfo->nexthops, adj);
295 if (prefix->family == AF_INET6)
297 rinfo->nexthops6 = list_new ();
298 for (ALL_LIST_ELEMENTS_RO (adjacencies, node, adj))
300 /* check for force resync this route */
301 if (CHECK_FLAG (adj->circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
302 SET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
303 /* update neighbor router address */
304 if (depth == 2 && prefix->prefixlen == 128)
305 adj->router_address6 = prefix->u.prefix6;
306 adjinfo2nexthop6 (rinfo->nexthops6, adj);
310 #endif /* HAVE_IPV6 */
313 rinfo->depth = depth;
319 isis_route_info_delete (struct isis_route_info *route_info)
321 if (route_info->nexthops)
323 route_info->nexthops->del = (void (*)(void *)) isis_nexthop_delete;
324 list_delete (route_info->nexthops);
328 if (route_info->nexthops6)
330 route_info->nexthops6->del = (void (*)(void *)) isis_nexthop6_delete;
331 list_delete (route_info->nexthops6);
333 #endif /* HAVE_IPV6 */
335 XFREE (MTYPE_ISIS_ROUTE_INFO, route_info);
339 isis_route_info_same_attrib (struct isis_route_info *new,
340 struct isis_route_info *old)
342 if (new->cost != old->cost)
344 if (new->depth != old->depth)
351 isis_route_info_same (struct isis_route_info *new,
352 struct isis_route_info *old, u_char family)
354 struct listnode *node;
355 struct isis_nexthop *nexthop;
357 struct isis_nexthop6 *nexthop6;
358 #endif /* HAVE_IPV6 */
360 if (!CHECK_FLAG (old->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
363 if (CHECK_FLAG (new->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC))
366 if (!isis_route_info_same_attrib (new, old))
369 if (family == AF_INET)
371 for (ALL_LIST_ELEMENTS_RO (new->nexthops, node, nexthop))
372 if (nexthoplookup (old->nexthops, &nexthop->ip, nexthop->ifindex)
376 for (ALL_LIST_ELEMENTS_RO (old->nexthops, node, nexthop))
377 if (nexthoplookup (new->nexthops, &nexthop->ip, nexthop->ifindex)
382 else if (family == AF_INET6)
384 for (ALL_LIST_ELEMENTS_RO (new->nexthops6, node, nexthop6))
385 if (nexthop6lookup (old->nexthops6, &nexthop6->ip6,
386 nexthop6->ifindex) == 0)
389 for (ALL_LIST_ELEMENTS_RO (old->nexthops6, node, nexthop6))
390 if (nexthop6lookup (new->nexthops6, &nexthop6->ip6,
391 nexthop6->ifindex) == 0)
394 #endif /* HAVE_IPV6 */
399 struct isis_route_info *
400 isis_route_create (struct prefix *prefix, u_int32_t cost, u_int32_t depth,
401 struct list *adjacencies, struct isis_area *area,
404 struct route_node *route_node;
405 struct isis_route_info *rinfo_new, *rinfo_old, *route_info = NULL;
409 family = prefix->family;
411 prefix2str (prefix, (char *) buff, BUFSIZ);
413 rinfo_new = isis_route_info_new (prefix, cost, depth, adjacencies);
415 if (family == AF_INET)
416 route_node = route_node_get (area->route_table[level - 1], prefix);
418 else if (family == AF_INET6)
419 route_node = route_node_get (area->route_table6[level - 1], prefix);
420 #endif /* HAVE_IPV6 */
423 isis_route_info_delete (rinfo_new);
427 rinfo_old = route_node->info;
430 if (isis->debugs & DEBUG_RTE_EVENTS)
431 zlog_debug ("ISIS-Rte (%s) route created: %s", area->area_tag, buff);
432 route_info = rinfo_new;
433 UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
437 if (isis->debugs & DEBUG_RTE_EVENTS)
438 zlog_debug ("ISIS-Rte (%s) route already exists: %s", area->area_tag,
440 if (isis_route_info_same (rinfo_new, rinfo_old, family))
442 if (isis->debugs & DEBUG_RTE_EVENTS)
443 zlog_debug ("ISIS-Rte (%s) route unchanged: %s", area->area_tag,
445 isis_route_info_delete (rinfo_new);
446 route_info = rinfo_old;
450 if (isis->debugs & DEBUG_RTE_EVENTS)
451 zlog_debug ("ISIS-Rte (%s) route changed: %s", area->area_tag,
453 isis_route_info_delete (rinfo_old);
454 route_info = rinfo_new;
455 UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
459 SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ACTIVE);
460 route_node->info = route_info;
466 isis_route_delete (struct prefix *prefix, struct route_table *table)
468 struct route_node *rode;
469 struct isis_route_info *rinfo;
473 prefix2str (prefix, buff, BUFSIZ);
476 rode = route_node_get (table, prefix);
481 if (isis->debugs & DEBUG_RTE_EVENTS)
482 zlog_debug ("ISIS-Rte: tried to delete non-existant route %s", buff);
486 if (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
488 UNSET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
489 if (isis->debugs & DEBUG_RTE_EVENTS)
490 zlog_debug ("ISIS-Rte: route delete %s", buff);
491 isis_zebra_route_update (prefix, rinfo);
493 isis_route_info_delete (rinfo);
499 /* Validating routes in particular table. */
501 isis_route_validate_table (struct isis_area *area, struct route_table *table)
503 struct route_node *rnode, *drnode;
504 struct isis_route_info *rinfo;
507 for (rnode = route_top (table); rnode; rnode = route_next (rnode))
509 if (rnode->info == NULL)
513 if (isis->debugs & DEBUG_RTE_EVENTS)
515 prefix2str (&rnode->p, (char *) buff, BUFSIZ);
516 zlog_debug ("ISIS-Rte (%s): route validate: %s %s %s %s",
518 (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED) ?
519 "synced" : "not-synced"),
520 (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC) ?
521 "resync" : "not-resync"),
522 (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE) ?
523 "active" : "inactive"), buff);
526 isis_zebra_route_update (&rnode->p, rinfo);
527 if (!CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE))
529 /* Area is either L1 or L2 => we use level route tables directly for
530 * validating => no problems with deleting routes. */
531 if (area->is_type != IS_LEVEL_1_AND_2)
533 isis_route_delete (&rnode->p, table);
536 /* If area is L1L2, we work with merge table and therefore must
537 * delete node from level tables as well before deleting route info.
538 * FIXME: Is it performance problem? There has to be the better way.
539 * Like not to deal with it here at all (see the next comment)? */
540 if (rnode->p.family == AF_INET)
542 drnode = route_node_get (area->route_table[0], &rnode->p);
543 if (drnode->info == rnode->info)
545 drnode = route_node_get (area->route_table[1], &rnode->p);
546 if (drnode->info == rnode->info)
551 if (rnode->p.family == AF_INET6)
553 drnode = route_node_get (area->route_table6[0], &rnode->p);
554 if (drnode->info == rnode->info)
556 drnode = route_node_get (area->route_table6[1], &rnode->p);
557 if (drnode->info == rnode->info)
562 isis_route_delete (&rnode->p, table);
567 /* Function to validate route tables for L1L2 areas. In this case we can't use
568 * level route tables directly, we have to merge them at first. L1 routes are
569 * preferred over the L2 ones.
571 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
572 * merge table at first, then L2 paths are added if L1 path for same prefix
573 * doesn't already exists there.
575 * FIXME: Is it right place to do it at all? Maybe we should push both levels
576 * to the RIB with different zebra route types and let RIB handle this? */
578 isis_route_validate_merge (struct isis_area *area, int family)
580 struct route_table *table = NULL;
581 struct route_table *merge;
582 struct route_node *rnode, *mrnode;
584 merge = route_table_init ();
586 if (family == AF_INET)
587 table = area->route_table[0];
589 else if (family == AF_INET6)
590 table = area->route_table6[0];
593 for (rnode = route_top (table); rnode; rnode = route_next (rnode))
595 if (rnode->info == NULL)
597 mrnode = route_node_get (merge, &rnode->p);
598 mrnode->info = rnode->info;
601 if (family == AF_INET)
602 table = area->route_table[1];
604 else if (family == AF_INET6)
605 table = area->route_table6[1];
608 for (rnode = route_top (table); rnode; rnode = route_next (rnode))
610 if (rnode->info == NULL)
612 mrnode = route_node_get (merge, &rnode->p);
613 if (mrnode->info != NULL)
615 mrnode->info = rnode->info;
618 isis_route_validate_table (area, merge);
619 route_table_finish (merge);
622 /* Walk through route tables and propagate necessary changes into RIB. In case
623 * of L1L2 area, level tables have to be merged at first. */
625 isis_route_validate (struct isis_area *area)
627 struct listnode *node;
628 struct isis_circuit *circuit;
630 if (area->is_type == IS_LEVEL_1)
631 isis_route_validate_table (area, area->route_table[0]);
632 else if (area->is_type == IS_LEVEL_2)
633 isis_route_validate_table (area, area->route_table[1]);
635 isis_route_validate_merge (area, AF_INET);
638 if (area->is_type == IS_LEVEL_1)
639 isis_route_validate_table (area, area->route_table6[0]);
640 else if (area->is_type == IS_LEVEL_2)
641 isis_route_validate_table (area, area->route_table6[1]);
643 isis_route_validate_merge (area, AF_INET6);
646 if (!area->circuit_list) {
649 /* walk all circuits and reset any spf specific flags */
650 for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
651 UNSET_FLAG(circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF);
657 isis_route_invalidate_table (struct isis_area *area, struct route_table *table)
659 struct route_node *rode;
660 struct isis_route_info *rinfo;
661 for (rode = route_top (table); rode; rode = route_next (rode))
663 if (rode->info == NULL)
667 UNSET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
672 isis_route_invalidate (struct isis_area *area)
674 if (area->is_type & IS_LEVEL_1)
675 isis_route_invalidate_table (area, area->route_table[0]);
676 if (area->is_type & IS_LEVEL_2)
677 isis_route_invalidate_table (area, area->route_table[1]);