blob: d799ea0e1dbc058be3d5aa94436a03541098e49a [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
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
6 *
7 * based on ../ospf6d/ospf6_route.[ch]
8 * by Yasuhiro Ohara
9 *
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)
13 * any later version.
14 *
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
18 * more details.
19
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.
23 */
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <zebra.h>
jardineb5d44e2003-12-23 08:09:43 +000028
29#include "thread.h"
30#include "linklist.h"
31#include "vty.h"
32#include "log.h"
33#include "memory.h"
34#include "prefix.h"
35#include "hash.h"
36#include "if.h"
37#include "table.h"
38
39#include "isis_constants.h"
40#include "isis_common.h"
41#include "dict.h"
42#include "isisd.h"
43#include "isis_misc.h"
44#include "isis_adjacency.h"
45#include "isis_circuit.h"
46#include "isis_tlv.h"
47#include "isis_pdu.h"
48#include "isis_lsp.h"
49#include "isis_spf.h"
50#include "isis_route.h"
51#include "isis_zebra.h"
52
53extern struct isis *isis;
54extern struct thread_master *master;
55
hasso92365882005-01-18 13:53:33 +000056static struct isis_nexthop *
jardineb5d44e2003-12-23 08:09:43 +000057isis_nexthop_create (struct in_addr *ip, unsigned int ifindex)
jardineb5d44e2003-12-23 08:09:43 +000058{
59 struct listnode *node;
60 struct isis_nexthop *nexthop;
jardineb5d44e2003-12-23 08:09:43 +000061
paul1eb8ef22005-04-07 07:30:20 +000062 for (ALL_LIST_ELEMENTS_RO (isis->nexthops, node, nexthop))
hassof390d2c2004-09-10 20:48:21 +000063 {
hassof390d2c2004-09-10 20:48:21 +000064 if (nexthop->ifindex != ifindex)
65 continue;
66 if (ip && memcmp (&nexthop->ip, ip, sizeof (struct in_addr)) != 0)
67 continue;
68
69 nexthop->lock++;
70 return nexthop;
71 }
72
hasso3fdb2dd2005-09-28 18:45:54 +000073 nexthop = XCALLOC (MTYPE_ISIS_NEXTHOP, sizeof (struct isis_nexthop));
hassof390d2c2004-09-10 20:48:21 +000074 if (!nexthop)
75 {
76 zlog_err ("ISIS-Rte: isis_nexthop_create: out of memory!");
77 }
78
jardineb5d44e2003-12-23 08:09:43 +000079 nexthop->ifindex = ifindex;
80 memcpy (&nexthop->ip, ip, sizeof (struct in_addr));
81 listnode_add (isis->nexthops, nexthop);
82 nexthop->lock++;
83
84 return nexthop;
85}
86
hasso92365882005-01-18 13:53:33 +000087static void
jardineb5d44e2003-12-23 08:09:43 +000088isis_nexthop_delete (struct isis_nexthop *nexthop)
89{
90 nexthop->lock--;
hassof390d2c2004-09-10 20:48:21 +000091 if (nexthop->lock == 0)
92 {
93 listnode_delete (isis->nexthops, nexthop);
94 XFREE (MTYPE_ISIS_NEXTHOP, nexthop);
95 }
96
jardineb5d44e2003-12-23 08:09:43 +000097 return;
98}
99
hasso92365882005-01-18 13:53:33 +0000100static int
hassof390d2c2004-09-10 20:48:21 +0000101nexthoplookup (struct list *nexthops, struct in_addr *ip,
102 unsigned int ifindex)
jardineb5d44e2003-12-23 08:09:43 +0000103{
104 struct listnode *node;
105 struct isis_nexthop *nh;
106
paul1eb8ef22005-04-07 07:30:20 +0000107 for (ALL_LIST_ELEMENTS_RO (nexthops, node, nh))
hassof390d2c2004-09-10 20:48:21 +0000108 {
hassof390d2c2004-09-10 20:48:21 +0000109 if (!(memcmp (ip, &nh->ip, sizeof (struct in_addr))) &&
110 ifindex == nh->ifindex)
111 return 1;
112 }
jardineb5d44e2003-12-23 08:09:43 +0000113
114 return 0;
115}
116
hasso3d549272005-09-21 18:52:14 +0000117#ifdef EXTREME_DEBUG
hasso92365882005-01-18 13:53:33 +0000118static void
jardineb5d44e2003-12-23 08:09:43 +0000119nexthop_print (struct isis_nexthop *nh)
120{
121 u_char buf[BUFSIZ];
hassof390d2c2004-09-10 20:48:21 +0000122
hassof7c43dc2004-09-26 16:24:14 +0000123 inet_ntop (AF_INET, &nh->ip, (char *) buf, BUFSIZ);
hassof390d2c2004-09-10 20:48:21 +0000124
hasso529d65b2004-12-24 00:14:50 +0000125 zlog_debug (" %s %u", buf, nh->ifindex);
jardineb5d44e2003-12-23 08:09:43 +0000126}
127
hasso92365882005-01-18 13:53:33 +0000128static void
jardineb5d44e2003-12-23 08:09:43 +0000129nexthops_print (struct list *nhs)
130{
131 struct listnode *node;
hassof390d2c2004-09-10 20:48:21 +0000132
paul1eb8ef22005-04-07 07:30:20 +0000133 for (ALL_LIST_ELEMENTS_RO (nhs, node, nh))
134 nexthop_print (nh);
jardineb5d44e2003-12-23 08:09:43 +0000135}
hasso3d549272005-09-21 18:52:14 +0000136#endif /* EXTREME_DEBUG */
jardineb5d44e2003-12-23 08:09:43 +0000137
138#ifdef HAVE_IPV6
hasso92365882005-01-18 13:53:33 +0000139static struct isis_nexthop6 *
hassof390d2c2004-09-10 20:48:21 +0000140isis_nexthop6_new (struct in6_addr *ip6, unsigned int ifindex)
jardineb5d44e2003-12-23 08:09:43 +0000141{
hassof390d2c2004-09-10 20:48:21 +0000142
jardineb5d44e2003-12-23 08:09:43 +0000143 struct isis_nexthop6 *nexthop6;
hassof390d2c2004-09-10 20:48:21 +0000144
hasso3fdb2dd2005-09-28 18:45:54 +0000145 nexthop6 = XCALLOC (MTYPE_ISIS_NEXTHOP6, sizeof (struct isis_nexthop6));
hassof390d2c2004-09-10 20:48:21 +0000146 if (!nexthop6)
147 {
148 zlog_err ("ISIS-Rte: isis_nexthop_create6: out of memory!");
149 }
150
jardineb5d44e2003-12-23 08:09:43 +0000151 nexthop6->ifindex = ifindex;
152 memcpy (&nexthop6->ip6, ip6, sizeof (struct in6_addr));
153 nexthop6->lock++;
154
155 return nexthop6;
156}
157
hasso92365882005-01-18 13:53:33 +0000158static struct isis_nexthop6 *
jardineb5d44e2003-12-23 08:09:43 +0000159isis_nexthop6_create (struct in6_addr *ip6, unsigned int ifindex)
jardineb5d44e2003-12-23 08:09:43 +0000160{
161 struct listnode *node;
162 struct isis_nexthop6 *nexthop6;
jardineb5d44e2003-12-23 08:09:43 +0000163
paul1eb8ef22005-04-07 07:30:20 +0000164 for (ALL_LIST_ELEMENTS_RO (isis->nexthops6, node, nexthop6))
hassof390d2c2004-09-10 20:48:21 +0000165 {
hassof390d2c2004-09-10 20:48:21 +0000166 if (nexthop6->ifindex != ifindex)
167 continue;
168 if (ip6 && memcmp (&nexthop6->ip6, ip6, sizeof (struct in6_addr)) != 0)
169 continue;
170
171 nexthop6->lock++;
172 return nexthop6;
173 }
174
jardineb5d44e2003-12-23 08:09:43 +0000175 nexthop6 = isis_nexthop6_new (ip6, ifindex);
176
177 return nexthop6;
178}
179
hasso92365882005-01-18 13:53:33 +0000180static void
jardineb5d44e2003-12-23 08:09:43 +0000181isis_nexthop6_delete (struct isis_nexthop6 *nexthop6)
182{
183
184 nexthop6->lock--;
hassof390d2c2004-09-10 20:48:21 +0000185 if (nexthop6->lock == 0)
186 {
187 listnode_delete (isis->nexthops6, nexthop6);
188 XFREE (MTYPE_ISIS_NEXTHOP6, nexthop6);
189 }
190
jardineb5d44e2003-12-23 08:09:43 +0000191 return;
192}
193
hasso92365882005-01-18 13:53:33 +0000194static int
hassof390d2c2004-09-10 20:48:21 +0000195nexthop6lookup (struct list *nexthops6, struct in6_addr *ip6,
196 unsigned int ifindex)
jardineb5d44e2003-12-23 08:09:43 +0000197{
198 struct listnode *node;
199 struct isis_nexthop6 *nh6;
200
paul1eb8ef22005-04-07 07:30:20 +0000201 for (ALL_LIST_ELEMENTS_RO (nexthops6, node, nh6))
hassof390d2c2004-09-10 20:48:21 +0000202 {
hassof390d2c2004-09-10 20:48:21 +0000203 if (!(memcmp (ip6, &nh6->ip6, sizeof (struct in6_addr))) &&
204 ifindex == nh6->ifindex)
205 return 1;
206 }
jardineb5d44e2003-12-23 08:09:43 +0000207
208 return 0;
209}
210
hasso92365882005-01-18 13:53:33 +0000211#ifdef EXTREME_DEBUG
212static void
jardineb5d44e2003-12-23 08:09:43 +0000213nexthop6_print (struct isis_nexthop6 *nh6)
214{
215 u_char buf[BUFSIZ];
hassof390d2c2004-09-10 20:48:21 +0000216
hassof7c43dc2004-09-26 16:24:14 +0000217 inet_ntop (AF_INET6, &nh6->ip6, (char *) buf, BUFSIZ);
hassof390d2c2004-09-10 20:48:21 +0000218
hasso529d65b2004-12-24 00:14:50 +0000219 zlog_debug (" %s %u", buf, nh6->ifindex);
jardineb5d44e2003-12-23 08:09:43 +0000220}
221
hasso92365882005-01-18 13:53:33 +0000222static void
jardineb5d44e2003-12-23 08:09:43 +0000223nexthops6_print (struct list *nhs6)
224{
225 struct listnode *node;
jardin10fc9182005-10-01 00:09:39 +0000226 struct isis_nexthop6 *nh6;
hassof390d2c2004-09-10 20:48:21 +0000227
paul1eb8ef22005-04-07 07:30:20 +0000228 for (ALL_LIST_ELEMENTS_RO (nhs6, node, nh6))
229 nexthop6_print (nh6);
jardineb5d44e2003-12-23 08:09:43 +0000230}
hasso92365882005-01-18 13:53:33 +0000231#endif /* EXTREME_DEBUG */
jardineb5d44e2003-12-23 08:09:43 +0000232#endif /* HAVE_IPV6 */
233
hasso92365882005-01-18 13:53:33 +0000234static void
jardineb5d44e2003-12-23 08:09:43 +0000235adjinfo2nexthop (struct list *nexthops, struct isis_adjacency *adj)
236{
237 struct isis_nexthop *nh;
hasso3fdb2dd2005-09-28 18:45:54 +0000238 struct listnode *node;
jardineb5d44e2003-12-23 08:09:43 +0000239 struct in_addr *ipv4_addr;
240
241 if (adj->ipv4_addrs == NULL)
242 return;
paul1eb8ef22005-04-07 07:30:20 +0000243
hasso3fdb2dd2005-09-28 18:45:54 +0000244 for (ALL_LIST_ELEMENTS_RO (adj->ipv4_addrs, node, ipv4_addr))
hassof390d2c2004-09-10 20:48:21 +0000245 {
hassof390d2c2004-09-10 20:48:21 +0000246 if (!nexthoplookup (nexthops, ipv4_addr,
247 adj->circuit->interface->ifindex))
248 {
249 nh = isis_nexthop_create (ipv4_addr,
250 adj->circuit->interface->ifindex);
251 listnode_add (nexthops, nh);
252 }
jardineb5d44e2003-12-23 08:09:43 +0000253 }
jardineb5d44e2003-12-23 08:09:43 +0000254}
255
256#ifdef HAVE_IPV6
hasso92365882005-01-18 13:53:33 +0000257static void
jardineb5d44e2003-12-23 08:09:43 +0000258adjinfo2nexthop6 (struct list *nexthops6, struct isis_adjacency *adj)
259{
hasso3fdb2dd2005-09-28 18:45:54 +0000260 struct listnode *node;
jardineb5d44e2003-12-23 08:09:43 +0000261 struct in6_addr *ipv6_addr;
262 struct isis_nexthop6 *nh6;
hassof390d2c2004-09-10 20:48:21 +0000263
jardineb5d44e2003-12-23 08:09:43 +0000264 if (!adj->ipv6_addrs)
265 return;
266
hasso3fdb2dd2005-09-28 18:45:54 +0000267 for (ALL_LIST_ELEMENTS_RO (adj->ipv6_addrs, node, ipv6_addr))
hassof390d2c2004-09-10 20:48:21 +0000268 {
hassof390d2c2004-09-10 20:48:21 +0000269 if (!nexthop6lookup (nexthops6, ipv6_addr,
270 adj->circuit->interface->ifindex))
271 {
272 nh6 = isis_nexthop6_create (ipv6_addr,
273 adj->circuit->interface->ifindex);
274 listnode_add (nexthops6, nh6);
275 }
jardineb5d44e2003-12-23 08:09:43 +0000276 }
jardineb5d44e2003-12-23 08:09:43 +0000277}
278#endif /* HAVE_IPV6 */
279
hasso92365882005-01-18 13:53:33 +0000280static struct isis_route_info *
hassof390d2c2004-09-10 20:48:21 +0000281isis_route_info_new (uint32_t cost, uint32_t depth, u_char family,
282 struct list *adjacencies)
jardineb5d44e2003-12-23 08:09:43 +0000283{
284 struct isis_route_info *rinfo;
285 struct isis_adjacency *adj;
hasso3fdb2dd2005-09-28 18:45:54 +0000286 struct listnode *node;
hassof390d2c2004-09-10 20:48:21 +0000287
hasso3fdb2dd2005-09-28 18:45:54 +0000288 rinfo = XCALLOC (MTYPE_ISIS_ROUTE_INFO, sizeof (struct isis_route_info));
hassof390d2c2004-09-10 20:48:21 +0000289 if (!rinfo)
290 {
291 zlog_err ("ISIS-Rte: isis_route_info_new: out of memory!");
292 return NULL;
293 }
jardineb5d44e2003-12-23 08:09:43 +0000294
hassof390d2c2004-09-10 20:48:21 +0000295 if (family == AF_INET)
296 {
297 rinfo->nexthops = list_new ();
hasso3fdb2dd2005-09-28 18:45:54 +0000298 for (ALL_LIST_ELEMENTS_RO (adjacencies, node, adj))
paul1eb8ef22005-04-07 07:30:20 +0000299 adjinfo2nexthop (rinfo->nexthops, adj);
jardineb5d44e2003-12-23 08:09:43 +0000300 }
jardineb5d44e2003-12-23 08:09:43 +0000301#ifdef HAVE_IPV6
hassof390d2c2004-09-10 20:48:21 +0000302 if (family == AF_INET6)
303 {
304 rinfo->nexthops6 = list_new ();
hasso3fdb2dd2005-09-28 18:45:54 +0000305 for (ALL_LIST_ELEMENTS_RO (adjacencies, node, adj))
paul1eb8ef22005-04-07 07:30:20 +0000306 adjinfo2nexthop6 (rinfo->nexthops6, adj);
jardineb5d44e2003-12-23 08:09:43 +0000307 }
hassof390d2c2004-09-10 20:48:21 +0000308
jardineb5d44e2003-12-23 08:09:43 +0000309#endif /* HAVE_IPV6 */
310
311 rinfo->cost = cost;
312 rinfo->depth = depth;
hassof390d2c2004-09-10 20:48:21 +0000313
jardineb5d44e2003-12-23 08:09:43 +0000314 return rinfo;
315}
316
hasso92365882005-01-18 13:53:33 +0000317static void
jardineb5d44e2003-12-23 08:09:43 +0000318isis_route_info_delete (struct isis_route_info *route_info)
319{
hassof390d2c2004-09-10 20:48:21 +0000320 if (route_info->nexthops)
321 {
hassof7c43dc2004-09-26 16:24:14 +0000322 route_info->nexthops->del = (void (*)(void *)) isis_nexthop_delete;
hassof390d2c2004-09-10 20:48:21 +0000323 list_delete (route_info->nexthops);
324 }
325
jardineb5d44e2003-12-23 08:09:43 +0000326#ifdef HAVE_IPV6
hassof390d2c2004-09-10 20:48:21 +0000327 if (route_info->nexthops6)
328 {
hassof7c43dc2004-09-26 16:24:14 +0000329 route_info->nexthops6->del = (void (*)(void *)) isis_nexthop6_delete;
jardineb5d44e2003-12-23 08:09:43 +0000330 list_delete (route_info->nexthops6);
hassof390d2c2004-09-10 20:48:21 +0000331 }
jardineb5d44e2003-12-23 08:09:43 +0000332#endif /* HAVE_IPV6 */
hassof390d2c2004-09-10 20:48:21 +0000333
jardineb5d44e2003-12-23 08:09:43 +0000334 XFREE (MTYPE_ISIS_ROUTE_INFO, route_info);
335}
336
hasso92365882005-01-18 13:53:33 +0000337static int
hassof390d2c2004-09-10 20:48:21 +0000338isis_route_info_same_attrib (struct isis_route_info *new,
339 struct isis_route_info *old)
jardineb5d44e2003-12-23 08:09:43 +0000340{
341 if (new->cost != old->cost)
342 return 0;
343 if (new->depth != old->depth)
344 return 0;
hassof390d2c2004-09-10 20:48:21 +0000345
jardineb5d44e2003-12-23 08:09:43 +0000346 return 1;
347}
348
hasso92365882005-01-18 13:53:33 +0000349static int
hassof390d2c2004-09-10 20:48:21 +0000350isis_route_info_same (struct isis_route_info *new,
351 struct isis_route_info *old, u_char family)
jardineb5d44e2003-12-23 08:09:43 +0000352{
hasso3fdb2dd2005-09-28 18:45:54 +0000353 struct listnode *node;
jardineb5d44e2003-12-23 08:09:43 +0000354 struct isis_nexthop *nexthop;
355#ifdef HAVE_IPV6
356 struct isis_nexthop6 *nexthop6;
357#endif /* HAVE_IPV6 */
358 if (!isis_route_info_same_attrib (new, old))
359 return 0;
hassof390d2c2004-09-10 20:48:21 +0000360
361 if (family == AF_INET)
362 {
hasso3fdb2dd2005-09-28 18:45:54 +0000363 for (ALL_LIST_ELEMENTS_RO (new->nexthops, node, nexthop))
paul1eb8ef22005-04-07 07:30:20 +0000364 if (nexthoplookup (old->nexthops, &nexthop->ip, nexthop->ifindex)
365 == 0)
366 return 0;
hassof390d2c2004-09-10 20:48:21 +0000367
hasso3fdb2dd2005-09-28 18:45:54 +0000368 for (ALL_LIST_ELEMENTS_RO (old->nexthops, node, nexthop))
paul1eb8ef22005-04-07 07:30:20 +0000369 if (nexthoplookup (new->nexthops, &nexthop->ip, nexthop->ifindex)
370 == 0)
371 return 0;
jardineb5d44e2003-12-23 08:09:43 +0000372 }
jardineb5d44e2003-12-23 08:09:43 +0000373#ifdef HAVE_IPV6
hassof390d2c2004-09-10 20:48:21 +0000374 else if (family == AF_INET6)
375 {
hasso3fdb2dd2005-09-28 18:45:54 +0000376 for (ALL_LIST_ELEMENTS_RO (new->nexthops6, node, nexthop6))
paul1eb8ef22005-04-07 07:30:20 +0000377 if (nexthop6lookup (old->nexthops6, &nexthop6->ip6,
378 nexthop6->ifindex) == 0)
379 return 0;
hassof390d2c2004-09-10 20:48:21 +0000380
hasso3fdb2dd2005-09-28 18:45:54 +0000381 for (ALL_LIST_ELEMENTS_RO (old->nexthops6, node, nexthop6))
paul1eb8ef22005-04-07 07:30:20 +0000382 if (nexthop6lookup (new->nexthops6, &nexthop6->ip6,
383 nexthop6->ifindex) == 0)
384 return 0;
jardineb5d44e2003-12-23 08:09:43 +0000385 }
jardineb5d44e2003-12-23 08:09:43 +0000386#endif /* HAVE_IPV6 */
387
388 return 1;
389}
390
hasso92365882005-01-18 13:53:33 +0000391static void
jardineb5d44e2003-12-23 08:09:43 +0000392isis_nexthops_merge (struct list *new, struct list *old)
393{
hasso3fdb2dd2005-09-28 18:45:54 +0000394 struct listnode *node;
jardineb5d44e2003-12-23 08:09:43 +0000395 struct isis_nexthop *nexthop;
396
hasso3fdb2dd2005-09-28 18:45:54 +0000397 for (ALL_LIST_ELEMENTS_RO (new, node, nexthop))
hassof390d2c2004-09-10 20:48:21 +0000398 {
hassof390d2c2004-09-10 20:48:21 +0000399 if (nexthoplookup (old, &nexthop->ip, nexthop->ifindex))
400 continue;
401 listnode_add (old, nexthop);
402 nexthop->lock++;
403 }
jardineb5d44e2003-12-23 08:09:43 +0000404}
405
jardineb5d44e2003-12-23 08:09:43 +0000406#ifdef HAVE_IPV6
hasso92365882005-01-18 13:53:33 +0000407static void
jardineb5d44e2003-12-23 08:09:43 +0000408isis_nexthops6_merge (struct list *new, struct list *old)
409{
hasso3fdb2dd2005-09-28 18:45:54 +0000410 struct listnode *node;
jardineb5d44e2003-12-23 08:09:43 +0000411 struct isis_nexthop6 *nexthop6;
412
hasso3fdb2dd2005-09-28 18:45:54 +0000413 for (ALL_LIST_ELEMENTS_RO (new, node, nexthop6))
hassof390d2c2004-09-10 20:48:21 +0000414 {
hassof390d2c2004-09-10 20:48:21 +0000415 if (nexthop6lookup (old, &nexthop6->ip6, nexthop6->ifindex))
416 continue;
417 listnode_add (old, nexthop6);
418 nexthop6->lock++;
419 }
jardineb5d44e2003-12-23 08:09:43 +0000420}
421#endif /* HAVE_IPV6 */
422
hasso92365882005-01-18 13:53:33 +0000423static void
hassof390d2c2004-09-10 20:48:21 +0000424isis_route_info_merge (struct isis_route_info *new,
425 struct isis_route_info *old, u_char family)
jardineb5d44e2003-12-23 08:09:43 +0000426{
hassof390d2c2004-09-10 20:48:21 +0000427 if (family == AF_INET)
jardineb5d44e2003-12-23 08:09:43 +0000428 isis_nexthops_merge (new->nexthops, old->nexthops);
429#ifdef HAVE_IPV6
hassof390d2c2004-09-10 20:48:21 +0000430 else if (family == AF_INET6)
jardineb5d44e2003-12-23 08:09:43 +0000431 isis_nexthops6_merge (new->nexthops6, old->nexthops6);
432#endif /* HAVE_IPV6 */
hassof390d2c2004-09-10 20:48:21 +0000433
jardineb5d44e2003-12-23 08:09:43 +0000434 return;
435}
436
hasso92365882005-01-18 13:53:33 +0000437static int
hassof390d2c2004-09-10 20:48:21 +0000438isis_route_info_prefer_new (struct isis_route_info *new,
439 struct isis_route_info *old)
jardineb5d44e2003-12-23 08:09:43 +0000440{
jardineb5d44e2003-12-23 08:09:43 +0000441 if (!CHECK_FLAG (old->flag, ISIS_ROUTE_FLAG_ACTIVE))
442 return 1;
443
444 if (new->cost < old->cost)
445 return 1;
hassof390d2c2004-09-10 20:48:21 +0000446
jardineb5d44e2003-12-23 08:09:43 +0000447 return 0;
448}
449
jardineb5d44e2003-12-23 08:09:43 +0000450struct isis_route_info *
451isis_route_create (struct prefix *prefix, u_int32_t cost, u_int32_t depth,
hassofac1f7c2005-09-26 18:26:26 +0000452 struct list *adjacencies, struct isis_area *area,
453 int level)
jardineb5d44e2003-12-23 08:09:43 +0000454{
455 struct route_node *route_node;
456 struct isis_route_info *rinfo_new, *rinfo_old, *route_info = NULL;
457 u_char buff[BUFSIZ];
458 u_char family;
hassof390d2c2004-09-10 20:48:21 +0000459
jardineb5d44e2003-12-23 08:09:43 +0000460 family = prefix->family;
461 /* for debugs */
hassof7c43dc2004-09-26 16:24:14 +0000462 prefix2str (prefix, (char *) buff, BUFSIZ);
hassof390d2c2004-09-10 20:48:21 +0000463
jardineb5d44e2003-12-23 08:09:43 +0000464 rinfo_new = isis_route_info_new (cost, depth, family, adjacencies);
hassof390d2c2004-09-10 20:48:21 +0000465 if (!rinfo_new)
466 {
467 zlog_err ("ISIS-Rte (%s): isis_route_create: out of memory!",
468 area->area_tag);
469 return NULL;
470 }
471
jardineb5d44e2003-12-23 08:09:43 +0000472 if (family == AF_INET)
hassofac1f7c2005-09-26 18:26:26 +0000473 route_node = route_node_get (area->route_table[level - 1], prefix);
jardineb5d44e2003-12-23 08:09:43 +0000474#ifdef HAVE_IPV6
475 else if (family == AF_INET6)
hassofac1f7c2005-09-26 18:26:26 +0000476 route_node = route_node_get (area->route_table6[level - 1], prefix);
jardineb5d44e2003-12-23 08:09:43 +0000477#endif /* HAVE_IPV6 */
hassof390d2c2004-09-10 20:48:21 +0000478 else
jardineb5d44e2003-12-23 08:09:43 +0000479 return NULL;
hassof390d2c2004-09-10 20:48:21 +0000480 rinfo_old = route_node->info;
481 if (!rinfo_old)
482 {
483 if (isis->debugs & DEBUG_RTE_EVENTS)
hasso529d65b2004-12-24 00:14:50 +0000484 zlog_debug ("ISIS-Rte (%s) route created: %s", area->area_tag, buff);
hassof390d2c2004-09-10 20:48:21 +0000485 SET_FLAG (rinfo_new->flag, ISIS_ROUTE_FLAG_ACTIVE);
486 route_node->info = rinfo_new;
487 return rinfo_new;
488 }
489
jardineb5d44e2003-12-23 08:09:43 +0000490 if (isis->debugs & DEBUG_RTE_EVENTS)
hasso529d65b2004-12-24 00:14:50 +0000491 zlog_debug ("ISIS-Rte (%s) route already exists: %s", area->area_tag,
hassof390d2c2004-09-10 20:48:21 +0000492 buff);
493
494 if (isis_route_info_same (rinfo_new, rinfo_old, family))
495 {
jardineb5d44e2003-12-23 08:09:43 +0000496 if (isis->debugs & DEBUG_RTE_EVENTS)
hasso529d65b2004-12-24 00:14:50 +0000497 zlog_debug ("ISIS-Rte (%s) route unchanged: %s", area->area_tag, buff);
jardineb5d44e2003-12-23 08:09:43 +0000498 isis_route_info_delete (rinfo_new);
499 route_info = rinfo_old;
500 }
hassof390d2c2004-09-10 20:48:21 +0000501 else if (isis_route_info_same_attrib (rinfo_new, rinfo_old))
502 {
503 /* merge the nexthop lists */
504 if (isis->debugs & DEBUG_RTE_EVENTS)
hasso529d65b2004-12-24 00:14:50 +0000505 zlog_debug ("ISIS-Rte (%s) route changed (same attribs): %s",
hassof390d2c2004-09-10 20:48:21 +0000506 area->area_tag, buff);
507#ifdef EXTREME_DEBUG
hasso3d549272005-09-21 18:52:14 +0000508 if (family == AF_INET)
509 {
510 zlog_debug ("Old nexthops");
511 nexthops_print (rinfo_old->nexthops);
512 zlog_debug ("New nexthops");
513 nexthops_print (rinfo_new->nexthops);
514 }
515 else if (family == AF_INET6)
516 {
517 zlog_debug ("Old nexthops");
518 nexthops6_print (rinfo_old->nexthops6);
519 zlog_debug ("New nexthops");
520 nexthops6_print (rinfo_new->nexthops6);
521 }
hassof390d2c2004-09-10 20:48:21 +0000522#endif /* EXTREME_DEBUG */
523 isis_route_info_merge (rinfo_new, rinfo_old, family);
524 isis_route_info_delete (rinfo_new);
525 route_info = rinfo_old;
526 }
527 else
528 {
529 if (isis_route_info_prefer_new (rinfo_new, rinfo_old))
530 {
531 if (isis->debugs & DEBUG_RTE_EVENTS)
hasso529d65b2004-12-24 00:14:50 +0000532 zlog_debug ("ISIS-Rte (%s) route changed: %s", area->area_tag,
533 buff);
hassof390d2c2004-09-10 20:48:21 +0000534 isis_route_info_delete (rinfo_old);
535 route_info = rinfo_new;
536 }
537 else
538 {
539 if (isis->debugs & DEBUG_RTE_EVENTS)
hasso529d65b2004-12-24 00:14:50 +0000540 zlog_debug ("ISIS-Rte (%s) route rejected: %s", area->area_tag,
541 buff);
hassof390d2c2004-09-10 20:48:21 +0000542 isis_route_info_delete (rinfo_new);
543 route_info = rinfo_old;
544 }
545 }
546
jardineb5d44e2003-12-23 08:09:43 +0000547 SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ACTIVE);
548 route_node->info = route_info;
hassof390d2c2004-09-10 20:48:21 +0000549
jardineb5d44e2003-12-23 08:09:43 +0000550 return route_info;
551}
552
hasso92365882005-01-18 13:53:33 +0000553static void
jardineb5d44e2003-12-23 08:09:43 +0000554isis_route_delete (struct prefix *prefix, struct route_table *table)
555{
556 struct route_node *rode;
557 struct isis_route_info *rinfo;
558 char buff[BUFSIZ];
559
560 /* for log */
561 prefix2str (prefix, buff, BUFSIZ);
562
563
564 rode = route_node_get (table, prefix);
565 rinfo = rode->info;
566
hassof390d2c2004-09-10 20:48:21 +0000567 if (rinfo == NULL)
568 {
569 if (isis->debugs & DEBUG_RTE_EVENTS)
hasso529d65b2004-12-24 00:14:50 +0000570 zlog_debug ("ISIS-Rte: tried to delete non-existant route %s", buff);
hassof390d2c2004-09-10 20:48:21 +0000571 return;
572 }
jardineb5d44e2003-12-23 08:09:43 +0000573
hassof390d2c2004-09-10 20:48:21 +0000574 if (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC))
575 {
576 UNSET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
577 if (isis->debugs & DEBUG_RTE_EVENTS)
hasso529d65b2004-12-24 00:14:50 +0000578 zlog_debug ("ISIS-Rte: route delete %s", buff);
hassof390d2c2004-09-10 20:48:21 +0000579 isis_zebra_route_update (prefix, rinfo);
580 }
jardineb5d44e2003-12-23 08:09:43 +0000581 isis_route_info_delete (rinfo);
582 rode->info = NULL;
hassof390d2c2004-09-10 20:48:21 +0000583
jardineb5d44e2003-12-23 08:09:43 +0000584 return;
585}
586
hassofac1f7c2005-09-26 18:26:26 +0000587/* Validating routes in particular table. */
588static void
589isis_route_validate_table (struct isis_area *area, struct route_table *table)
jardineb5d44e2003-12-23 08:09:43 +0000590{
hassofac1f7c2005-09-26 18:26:26 +0000591 struct route_node *rnode, *drnode;
jardineb5d44e2003-12-23 08:09:43 +0000592 struct isis_route_info *rinfo;
593 u_char buff[BUFSIZ];
hassofac1f7c2005-09-26 18:26:26 +0000594
595 for (rnode = route_top (table); rnode; rnode = route_next (rnode))
hassof390d2c2004-09-10 20:48:21 +0000596 {
hassofac1f7c2005-09-26 18:26:26 +0000597 if (rnode->info == NULL)
hassof390d2c2004-09-10 20:48:21 +0000598 continue;
hassofac1f7c2005-09-26 18:26:26 +0000599 rinfo = rnode->info;
hassof390d2c2004-09-10 20:48:21 +0000600
601 if (isis->debugs & DEBUG_RTE_EVENTS)
602 {
hassofac1f7c2005-09-26 18:26:26 +0000603 prefix2str (&rnode->p, (char *) buff, BUFSIZ);
hasso529d65b2004-12-24 00:14:50 +0000604 zlog_debug ("ISIS-Rte (%s): route validate: %s %s %s",
605 area->area_tag,
606 (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC) ?
hassof390d2c2004-09-10 20:48:21 +0000607 "sync'ed" : "nosync"),
hasso529d65b2004-12-24 00:14:50 +0000608 (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE) ?
hassof390d2c2004-09-10 20:48:21 +0000609 "active" : "inactive"), buff);
610 }
611
hassofac1f7c2005-09-26 18:26:26 +0000612 isis_zebra_route_update (&rnode->p, rinfo);
hassof390d2c2004-09-10 20:48:21 +0000613 if (!CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE))
hassofac1f7c2005-09-26 18:26:26 +0000614 {
615 /* Area is either L1 or L2 => we use level route tables directly for
616 * validating => no problems with deleting routes. */
617 if (area->is_type != IS_LEVEL_1_AND_2)
618 {
619 isis_route_delete (&rnode->p, table);
620 continue;
621 }
622 /* If area is L1L2, we work with merge table and therefore must
623 * delete node from level tables as well before deleting route info.
624 * FIXME: Is it performance problem? There has to be the better way.
625 * Like not to deal with it here at all (see the next comment)? */
626 if (rnode->p.family == AF_INET)
627 {
628 drnode = route_node_get (area->route_table[0], &rnode->p);
629 if (drnode->info == rnode->info)
630 drnode->info = NULL;
631 drnode = route_node_get (area->route_table[1], &rnode->p);
632 if (drnode->info == rnode->info)
633 drnode->info = NULL;
634 }
635 if (rnode->p.family == AF_INET6)
636 {
637 drnode = route_node_get (area->route_table6[0], &rnode->p);
638 if (drnode->info == rnode->info)
639 drnode->info = NULL;
640 drnode = route_node_get (area->route_table6[1], &rnode->p);
641 if (drnode->info == rnode->info)
642 drnode->info = NULL;
643 }
644
645 isis_route_delete (&rnode->p, table);
646 }
jardineb5d44e2003-12-23 08:09:43 +0000647 }
hassofac1f7c2005-09-26 18:26:26 +0000648}
649
650/* Function to validate route tables for L1L2 areas. In this case we can't use
651 * level route tables directly, we have to merge them at first. L1 routes are
652 * preferred over the L2 ones.
653 *
654 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
655 * merge table at first, then L2 paths are added if L1 path for same prefix
656 * doesn't already exists there.
657 *
658 * FIXME: Is it right place to do it at all? Maybe we should push both levels
659 * to the RIB with different zebra route types and let RIB handle this? */
660static void
661isis_route_validate_merge (struct isis_area *area, int family)
662{
663 struct route_table *table = NULL;
664 struct route_table *merge;
665 struct route_node *rnode, *mrnode;
666
667 merge = route_table_init ();
668
669 if (family == AF_INET)
670 table = area->route_table[0];
671 else if (family == AF_INET6)
672 table = area->route_table6[0];
673
674 for (rnode = route_top (table); rnode; rnode = route_next (rnode))
675 {
676 if (rnode->info == NULL)
677 continue;
678 mrnode = route_node_get (merge, &rnode->p);
679 mrnode->info = rnode->info;
680 }
681
682 if (family == AF_INET)
683 table = area->route_table[1];
684 else if (family == AF_INET6)
685 table = area->route_table6[1];
686
687 for (rnode = route_top (table); rnode; rnode = route_next (rnode))
688 {
689 if (rnode->info == NULL)
690 continue;
691 mrnode = route_node_get (merge, &rnode->p);
692 if (mrnode->info != NULL)
693 continue;
694 mrnode->info = rnode->info;
695 }
696
697 isis_route_validate_table (area, merge);
698 route_table_finish (merge);
699}
700
701/* Walk through route tables and propagate necessary changes into RIB. In case
702 * of L1L2 area, level tables have to be merged at first. */
703int
704isis_route_validate (struct thread *thread)
705{
706 struct isis_area *area;
707
708 area = THREAD_ARG (thread);
709
710 if (area->is_type == IS_LEVEL_1)
711 {
712 isis_route_validate_table (area, area->route_table[0]);
713 goto validate_ipv6;
714 }
715 if (area->is_type == IS_LEVEL_2)
716 {
717 isis_route_validate_table (area, area->route_table[1]);
718 goto validate_ipv6;
719 }
720
721 isis_route_validate_merge (area, AF_INET);
722
jardineb5d44e2003-12-23 08:09:43 +0000723#ifdef HAVE_IPV6
hassofac1f7c2005-09-26 18:26:26 +0000724validate_ipv6:
725 if (area->is_type == IS_LEVEL_1)
726 {
727 isis_route_validate_table (area, area->route_table6[0]);
728 return ISIS_OK;
729 }
730 if (area->is_type == IS_LEVEL_2)
731 {
732 isis_route_validate_table (area, area->route_table6[1]);
733 return ISIS_OK;
734 }
735
736 isis_route_validate_merge (area, AF_INET6);
jardineb5d44e2003-12-23 08:09:43 +0000737#endif
738
739 return ISIS_OK;
740}