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
36 #include "srcdest_table.h"
38 #include "isis_constants.h"
39 #include "isis_common.h"
40 #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 static struct isis_nexthop
*isis_nexthop_create(struct in_addr
*ip
,
55 struct listnode
*node
;
56 struct isis_nexthop
*nexthop
;
58 for (ALL_LIST_ELEMENTS_RO(isis
->nexthops
, node
, nexthop
)) {
59 if (nexthop
->ifindex
!= ifindex
)
61 if (ip
&& memcmp(&nexthop
->ip
, ip
, sizeof(struct in_addr
)) != 0)
68 nexthop
= XCALLOC(MTYPE_ISIS_NEXTHOP
, sizeof(struct isis_nexthop
));
70 nexthop
->ifindex
= ifindex
;
71 memcpy(&nexthop
->ip
, ip
, sizeof(struct in_addr
));
72 listnode_add(isis
->nexthops
, nexthop
);
78 static void isis_nexthop_delete(struct isis_nexthop
*nexthop
)
81 if (nexthop
->lock
== 0) {
82 listnode_delete(isis
->nexthops
, nexthop
);
83 XFREE(MTYPE_ISIS_NEXTHOP
, nexthop
);
89 static int nexthoplookup(struct list
*nexthops
, struct in_addr
*ip
,
92 struct listnode
*node
;
93 struct isis_nexthop
*nh
;
95 for (ALL_LIST_ELEMENTS_RO(nexthops
, node
, nh
)) {
96 if (!(memcmp(ip
, &nh
->ip
, sizeof(struct in_addr
)))
97 && ifindex
== nh
->ifindex
)
104 static struct isis_nexthop6
*isis_nexthop6_new(struct in6_addr
*ip6
,
107 struct isis_nexthop6
*nexthop6
;
109 nexthop6
= XCALLOC(MTYPE_ISIS_NEXTHOP6
, sizeof(struct isis_nexthop6
));
111 nexthop6
->ifindex
= ifindex
;
112 memcpy(&nexthop6
->ip6
, ip6
, sizeof(struct in6_addr
));
118 static struct isis_nexthop6
*isis_nexthop6_create(struct in6_addr
*ip6
,
121 struct listnode
*node
;
122 struct isis_nexthop6
*nexthop6
;
124 for (ALL_LIST_ELEMENTS_RO(isis
->nexthops6
, node
, nexthop6
)) {
125 if (nexthop6
->ifindex
!= ifindex
)
128 && memcmp(&nexthop6
->ip6
, ip6
, sizeof(struct in6_addr
))
136 nexthop6
= isis_nexthop6_new(ip6
, ifindex
);
141 static void isis_nexthop6_delete(struct isis_nexthop6
*nexthop6
)
145 if (nexthop6
->lock
== 0) {
146 listnode_delete(isis
->nexthops6
, nexthop6
);
147 XFREE(MTYPE_ISIS_NEXTHOP6
, nexthop6
);
153 static int nexthop6lookup(struct list
*nexthops6
, struct in6_addr
*ip6
,
156 struct listnode
*node
;
157 struct isis_nexthop6
*nh6
;
159 for (ALL_LIST_ELEMENTS_RO(nexthops6
, node
, nh6
)) {
160 if (!(memcmp(ip6
, &nh6
->ip6
, sizeof(struct in6_addr
)))
161 && ifindex
== nh6
->ifindex
)
168 static void adjinfo2nexthop(struct list
*nexthops
, struct isis_adjacency
*adj
)
170 struct isis_nexthop
*nh
;
172 for (unsigned int i
= 0; i
< adj
->ipv4_address_count
; i
++) {
173 struct in_addr
*ipv4_addr
= &adj
->ipv4_addresses
[i
];
174 if (!nexthoplookup(nexthops
, ipv4_addr
,
175 adj
->circuit
->interface
->ifindex
)) {
176 nh
= isis_nexthop_create(
177 ipv4_addr
, adj
->circuit
->interface
->ifindex
);
178 nh
->router_address
= adj
->router_address
;
179 listnode_add(nexthops
, nh
);
185 static void adjinfo2nexthop6(struct list
*nexthops6
, struct isis_adjacency
*adj
)
187 struct isis_nexthop6
*nh6
;
189 for (unsigned int i
= 0; i
< adj
->ipv6_address_count
; i
++) {
190 struct in6_addr
*ipv6_addr
= &adj
->ipv6_addresses
[i
];
191 if (!nexthop6lookup(nexthops6
, ipv6_addr
,
192 adj
->circuit
->interface
->ifindex
)) {
193 nh6
= isis_nexthop6_create(
194 ipv6_addr
, adj
->circuit
->interface
->ifindex
);
195 nh6
->router_address6
= adj
->router_address6
;
196 listnode_add(nexthops6
, nh6
);
202 static struct isis_route_info
*isis_route_info_new(struct prefix
*prefix
,
203 struct prefix_ipv6
*src_p
,
206 struct list
*adjacencies
)
208 struct isis_route_info
*rinfo
;
209 struct isis_adjacency
*adj
;
210 struct listnode
*node
;
212 rinfo
= XCALLOC(MTYPE_ISIS_ROUTE_INFO
, sizeof(struct isis_route_info
));
214 if (prefix
->family
== AF_INET
) {
215 rinfo
->nexthops
= list_new();
216 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, adj
)) {
217 /* check for force resync this route */
218 if (CHECK_FLAG(adj
->circuit
->flags
,
219 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
220 SET_FLAG(rinfo
->flag
,
221 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
222 /* update neighbor router address */
223 if (depth
== 2 && prefix
->prefixlen
== 32)
224 adj
->router_address
= prefix
->u
.prefix4
;
225 adjinfo2nexthop(rinfo
->nexthops
, adj
);
228 if (prefix
->family
== AF_INET6
) {
229 rinfo
->nexthops6
= list_new();
230 for (ALL_LIST_ELEMENTS_RO(adjacencies
, node
, adj
)) {
231 /* check for force resync this route */
232 if (CHECK_FLAG(adj
->circuit
->flags
,
233 ISIS_CIRCUIT_FLAPPED_AFTER_SPF
))
234 SET_FLAG(rinfo
->flag
,
235 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
);
236 /* update neighbor router address */
237 if (depth
== 2 && prefix
->prefixlen
== 128
238 && (!src_p
|| !src_p
->prefixlen
)) {
239 adj
->router_address6
= prefix
->u
.prefix6
;
241 adjinfo2nexthop6(rinfo
->nexthops6
, adj
);
246 rinfo
->depth
= depth
;
251 static void isis_route_info_delete(struct isis_route_info
*route_info
)
253 if (route_info
->nexthops
) {
254 route_info
->nexthops
->del
=
255 (void (*)(void *))isis_nexthop_delete
;
256 list_delete(&route_info
->nexthops
);
259 if (route_info
->nexthops6
) {
260 route_info
->nexthops6
->del
=
261 (void (*)(void *))isis_nexthop6_delete
;
262 list_delete(&route_info
->nexthops6
);
265 XFREE(MTYPE_ISIS_ROUTE_INFO
, route_info
);
268 static int isis_route_info_same_attrib(struct isis_route_info
*new,
269 struct isis_route_info
*old
)
271 if (new->cost
!= old
->cost
)
273 if (new->depth
!= old
->depth
)
279 static int isis_route_info_same(struct isis_route_info
*new,
280 struct isis_route_info
*old
, uint8_t family
)
282 struct listnode
*node
;
283 struct isis_nexthop
*nexthop
;
284 struct isis_nexthop6
*nexthop6
;
286 if (!CHECK_FLAG(old
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
))
289 if (CHECK_FLAG(new->flag
, ISIS_ROUTE_FLAG_ZEBRA_RESYNC
))
292 if (!isis_route_info_same_attrib(new, old
))
295 if (family
== AF_INET
) {
296 for (ALL_LIST_ELEMENTS_RO(new->nexthops
, node
, nexthop
))
297 if (nexthoplookup(old
->nexthops
, &nexthop
->ip
,
302 for (ALL_LIST_ELEMENTS_RO(old
->nexthops
, node
, nexthop
))
303 if (nexthoplookup(new->nexthops
, &nexthop
->ip
,
307 } else if (family
== AF_INET6
) {
308 for (ALL_LIST_ELEMENTS_RO(new->nexthops6
, node
, nexthop6
))
309 if (nexthop6lookup(old
->nexthops6
, &nexthop6
->ip6
,
314 for (ALL_LIST_ELEMENTS_RO(old
->nexthops6
, node
, nexthop6
))
315 if (nexthop6lookup(new->nexthops6
, &nexthop6
->ip6
,
324 struct isis_route_info
*isis_route_create(struct prefix
*prefix
,
325 struct prefix_ipv6
*src_p
,
328 struct list
*adjacencies
,
329 struct isis_area
*area
,
330 struct route_table
*table
)
332 struct route_node
*route_node
;
333 struct isis_route_info
*rinfo_new
, *rinfo_old
, *route_info
= NULL
;
334 char buff
[PREFIX2STR_BUFFER
];
337 family
= prefix
->family
;
339 prefix2str(prefix
, buff
, sizeof(buff
));
344 rinfo_new
= isis_route_info_new(prefix
, src_p
, cost
,
346 route_node
= srcdest_rnode_get(table
, prefix
, src_p
);
348 rinfo_old
= route_node
->info
;
350 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
351 zlog_debug("ISIS-Rte (%s) route created: %s",
352 area
->area_tag
, buff
);
353 route_info
= rinfo_new
;
354 UNSET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
356 route_unlock_node(route_node
);
357 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
358 zlog_debug("ISIS-Rte (%s) route already exists: %s",
359 area
->area_tag
, buff
);
360 if (isis_route_info_same(rinfo_new
, rinfo_old
, family
)) {
361 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
362 zlog_debug("ISIS-Rte (%s) route unchanged: %s",
363 area
->area_tag
, buff
);
364 isis_route_info_delete(rinfo_new
);
365 route_info
= rinfo_old
;
367 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
368 zlog_debug("ISIS-Rte (%s) route changed: %s",
369 area
->area_tag
, buff
);
370 isis_route_info_delete(rinfo_old
);
371 route_info
= rinfo_new
;
372 UNSET_FLAG(route_info
->flag
,
373 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
);
377 SET_FLAG(route_info
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
378 route_node
->info
= route_info
;
383 static void isis_route_delete(struct route_node
*rode
,
384 struct route_table
*table
)
386 struct isis_route_info
*rinfo
;
387 char buff
[SRCDEST2STR_BUFFER
];
388 struct prefix
*prefix
;
389 struct prefix_ipv6
*src_p
;
392 srcdest_rnode2str(rode
, buff
, sizeof(buff
));
394 srcdest_rnode_prefixes(rode
, (const struct prefix
**)&prefix
,
395 (const struct prefix
**)&src_p
);
399 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
401 "ISIS-Rte: tried to delete non-existant route %s",
406 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)) {
407 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);
408 if (isis
->debugs
& DEBUG_RTE_EVENTS
)
409 zlog_debug("ISIS-Rte: route delete %s", buff
);
410 isis_zebra_route_update(prefix
, src_p
, rinfo
);
412 isis_route_info_delete(rinfo
);
414 route_unlock_node(rode
);
417 static void _isis_route_verify_table(struct isis_area
*area
,
418 struct route_table
*table
,
419 struct route_table
**tables
)
421 struct route_node
*rnode
, *drnode
;
422 struct isis_route_info
*rinfo
;
423 char buff
[SRCDEST2STR_BUFFER
];
425 for (rnode
= route_top(table
); rnode
;
426 rnode
= srcdest_route_next(rnode
)) {
427 if (rnode
->info
== NULL
)
431 struct prefix
*dst_p
;
432 struct prefix_ipv6
*src_p
;
434 srcdest_rnode_prefixes(rnode
,
435 (const struct prefix
**)&dst_p
,
436 (const struct prefix
**)&src_p
);
438 if (isis
->debugs
& DEBUG_RTE_EVENTS
) {
439 srcdest2str(dst_p
, src_p
, buff
, sizeof(buff
));
441 "ISIS-Rte (%s): route validate: %s %s %s %s",
443 (CHECK_FLAG(rinfo
->flag
,
444 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
)
447 (CHECK_FLAG(rinfo
->flag
,
448 ISIS_ROUTE_FLAG_ZEBRA_RESYNC
)
451 (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
)
457 isis_zebra_route_update(dst_p
, src_p
, rinfo
);
459 if (CHECK_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
))
462 /* Area is either L1 or L2 => we use level route tables
464 * validating => no problems with deleting routes. */
466 isis_route_delete(rnode
, table
);
470 /* If area is L1L2, we work with merge table and
472 * delete node from level tables as well before deleting
474 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
475 drnode
= srcdest_rnode_lookup(tables
[level
- 1],
480 route_unlock_node(drnode
);
482 if (drnode
->info
!= rnode
->info
)
486 route_unlock_node(drnode
);
489 isis_route_delete(rnode
, table
);
493 void isis_route_verify_table(struct isis_area
*area
, struct route_table
*table
)
495 return _isis_route_verify_table(area
, table
, NULL
);
498 /* Function to validate route tables for L1L2 areas. In this case we can't use
499 * level route tables directly, we have to merge them at first. L1 routes are
500 * preferred over the L2 ones.
502 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
503 * merge table at first, then L2 paths are added if L1 path for same prefix
504 * doesn't already exists there.
506 * FIXME: Is it right place to do it at all? Maybe we should push both levels
507 * to the RIB with different zebra route types and let RIB handle this? */
508 void isis_route_verify_merge(struct isis_area
*area
,
509 struct route_table
*level1_table
,
510 struct route_table
*level2_table
)
512 struct route_table
*tables
[] = { level1_table
, level2_table
};
513 struct route_table
*merge
;
514 struct route_node
*rnode
, *mrnode
;
516 merge
= srcdest_table_init();
518 for (int level
= ISIS_LEVEL1
; level
<= ISIS_LEVEL2
; level
++) {
519 for (rnode
= route_top(tables
[level
- 1]); rnode
;
520 rnode
= srcdest_route_next(rnode
)) {
521 struct isis_route_info
*rinfo
= rnode
->info
;
525 struct prefix
*prefix
;
526 struct prefix_ipv6
*src_p
;
528 srcdest_rnode_prefixes(rnode
,
529 (const struct prefix
**)&prefix
,
530 (const struct prefix
**)&src_p
);
531 mrnode
= srcdest_rnode_get(merge
, prefix
, src_p
);
532 struct isis_route_info
*mrinfo
= mrnode
->info
;
534 route_unlock_node(mrnode
);
535 if (CHECK_FLAG(mrinfo
->flag
,
536 ISIS_ROUTE_FLAG_ACTIVE
)) {
537 /* Clear the ZEBRA_SYNCED flag on the
538 * L2 route when L1 wins, otherwise L2
539 * won't get reinstalled when L1
544 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
548 /* Clear the ZEBRA_SYNCED flag on the L1
549 * route when L2 wins, otherwise L1
550 * won't get reinstalled when it
555 ISIS_ROUTE_FLAG_ZEBRA_SYNCED
559 mrnode
->info
= rnode
->info
;
563 _isis_route_verify_table(area
, merge
, tables
);
564 route_table_finish(merge
);
567 void isis_route_invalidate_table(struct isis_area
*area
,
568 struct route_table
*table
)
570 struct route_node
*rode
;
571 struct isis_route_info
*rinfo
;
572 for (rode
= route_top(table
); rode
; rode
= srcdest_route_next(rode
)) {
573 if (rode
->info
== NULL
)
577 UNSET_FLAG(rinfo
->flag
, ISIS_ROUTE_FLAG_ACTIVE
);