]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_route.c
Merge pull request #2726 from sworleys/Netlink-Filter-AFI
[mirror_frr.git] / isisd / isis_route.c
CommitLineData
eb5d44eb 1/*
2 * IS-IS Rout(e)ing protocol - isis_route.c
3 * Copyright (C) 2001,2002 Sampo Saaristo
d62a17ae 4 * Tampere University of Technology
eb5d44eb 5 * Institute of Communications Engineering
6 *
7 * based on ../ospf6d/ospf6_route.[ch]
8 * by Yasuhiro Ohara
9 *
d62a17ae 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)
eb5d44eb 13 * any later version.
14 *
d62a17ae 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
eb5d44eb 18 * more details.
896014f4
DL
19 *
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
eb5d44eb 23 */
24
eb5d44eb 25#include <zebra.h>
eb5d44eb 26
27#include "thread.h"
28#include "linklist.h"
29#include "vty.h"
30#include "log.h"
31#include "memory.h"
32#include "prefix.h"
33#include "hash.h"
34#include "if.h"
35#include "table.h"
36
37#include "isis_constants.h"
38#include "isis_common.h"
3f045a08 39#include "isis_flags.h"
eb5d44eb 40#include "dict.h"
41#include "isisd.h"
42#include "isis_misc.h"
43#include "isis_adjacency.h"
44#include "isis_circuit.h"
eb5d44eb 45#include "isis_pdu.h"
46#include "isis_lsp.h"
47#include "isis_spf.h"
48#include "isis_route.h"
49#include "isis_zebra.h"
50
d62a17ae 51static struct isis_nexthop *isis_nexthop_create(struct in_addr *ip,
52 ifindex_t ifindex)
eb5d44eb 53{
d62a17ae 54 struct listnode *node;
55 struct isis_nexthop *nexthop;
f390d2c7 56
d62a17ae 57 for (ALL_LIST_ELEMENTS_RO(isis->nexthops, node, nexthop)) {
58 if (nexthop->ifindex != ifindex)
59 continue;
60 if (ip && memcmp(&nexthop->ip, ip, sizeof(struct in_addr)) != 0)
61 continue;
f390d2c7 62
d62a17ae 63 nexthop->lock++;
64 return nexthop;
65 }
f390d2c7 66
d62a17ae 67 nexthop = XCALLOC(MTYPE_ISIS_NEXTHOP, sizeof(struct isis_nexthop));
f390d2c7 68
d62a17ae 69 nexthop->ifindex = ifindex;
70 memcpy(&nexthop->ip, ip, sizeof(struct in_addr));
71 listnode_add(isis->nexthops, nexthop);
72 nexthop->lock++;
eb5d44eb 73
d62a17ae 74 return nexthop;
eb5d44eb 75}
76
d62a17ae 77static void isis_nexthop_delete(struct isis_nexthop *nexthop)
eb5d44eb 78{
d62a17ae 79 nexthop->lock--;
80 if (nexthop->lock == 0) {
81 listnode_delete(isis->nexthops, nexthop);
82 XFREE(MTYPE_ISIS_NEXTHOP, nexthop);
83 }
84
85 return;
eb5d44eb 86}
87
d62a17ae 88static int nexthoplookup(struct list *nexthops, struct in_addr *ip,
89 ifindex_t ifindex)
eb5d44eb 90{
d62a17ae 91 struct listnode *node;
92 struct isis_nexthop *nh;
eb5d44eb 93
d62a17ae 94 for (ALL_LIST_ELEMENTS_RO(nexthops, node, nh)) {
95 if (!(memcmp(ip, &nh->ip, sizeof(struct in_addr)))
96 && ifindex == nh->ifindex)
97 return 1;
98 }
eb5d44eb 99
d62a17ae 100 return 0;
eb5d44eb 101}
102
d62a17ae 103static struct isis_nexthop6 *isis_nexthop6_new(struct in6_addr *ip6,
104 ifindex_t ifindex)
eb5d44eb 105{
d62a17ae 106 struct isis_nexthop6 *nexthop6;
f390d2c7 107
d62a17ae 108 nexthop6 = XCALLOC(MTYPE_ISIS_NEXTHOP6, sizeof(struct isis_nexthop6));
f390d2c7 109
d62a17ae 110 nexthop6->ifindex = ifindex;
111 memcpy(&nexthop6->ip6, ip6, sizeof(struct in6_addr));
112 nexthop6->lock++;
eb5d44eb 113
d62a17ae 114 return nexthop6;
eb5d44eb 115}
116
d62a17ae 117static struct isis_nexthop6 *isis_nexthop6_create(struct in6_addr *ip6,
118 ifindex_t ifindex)
eb5d44eb 119{
d62a17ae 120 struct listnode *node;
121 struct isis_nexthop6 *nexthop6;
122
123 for (ALL_LIST_ELEMENTS_RO(isis->nexthops6, node, nexthop6)) {
124 if (nexthop6->ifindex != ifindex)
125 continue;
126 if (ip6
127 && memcmp(&nexthop6->ip6, ip6, sizeof(struct in6_addr))
128 != 0)
129 continue;
130
131 nexthop6->lock++;
132 return nexthop6;
133 }
f390d2c7 134
d62a17ae 135 nexthop6 = isis_nexthop6_new(ip6, ifindex);
eb5d44eb 136
d62a17ae 137 return nexthop6;
eb5d44eb 138}
139
d62a17ae 140static void isis_nexthop6_delete(struct isis_nexthop6 *nexthop6)
eb5d44eb 141{
142
d62a17ae 143 nexthop6->lock--;
144 if (nexthop6->lock == 0) {
145 listnode_delete(isis->nexthops6, nexthop6);
146 XFREE(MTYPE_ISIS_NEXTHOP6, nexthop6);
147 }
f390d2c7 148
d62a17ae 149 return;
eb5d44eb 150}
151
d62a17ae 152static int nexthop6lookup(struct list *nexthops6, struct in6_addr *ip6,
153 ifindex_t ifindex)
eb5d44eb 154{
d62a17ae 155 struct listnode *node;
156 struct isis_nexthop6 *nh6;
eb5d44eb 157
d62a17ae 158 for (ALL_LIST_ELEMENTS_RO(nexthops6, node, nh6)) {
159 if (!(memcmp(ip6, &nh6->ip6, sizeof(struct in6_addr)))
160 && ifindex == nh6->ifindex)
161 return 1;
162 }
eb5d44eb 163
d62a17ae 164 return 0;
eb5d44eb 165}
166
d62a17ae 167static void adjinfo2nexthop(struct list *nexthops, struct isis_adjacency *adj)
eb5d44eb 168{
d62a17ae 169 struct isis_nexthop *nh;
d62a17ae 170
0c1bd758
CF
171 for (unsigned int i = 0; i < adj->ipv4_address_count; i++) {
172 struct in_addr *ipv4_addr = &adj->ipv4_addresses[i];
d62a17ae 173 if (!nexthoplookup(nexthops, ipv4_addr,
174 adj->circuit->interface->ifindex)) {
175 nh = isis_nexthop_create(
176 ipv4_addr, adj->circuit->interface->ifindex);
177 nh->router_address = adj->router_address;
178 listnode_add(nexthops, nh);
74361f41 179 return;
d62a17ae 180 }
f390d2c7 181 }
eb5d44eb 182}
183
d62a17ae 184static void adjinfo2nexthop6(struct list *nexthops6, struct isis_adjacency *adj)
eb5d44eb 185{
d62a17ae 186 struct isis_nexthop6 *nh6;
187
0c1bd758
CF
188 for (unsigned int i = 0; i < adj->ipv6_address_count; i++) {
189 struct in6_addr *ipv6_addr = &adj->ipv6_addresses[i];
d62a17ae 190 if (!nexthop6lookup(nexthops6, ipv6_addr,
191 adj->circuit->interface->ifindex)) {
192 nh6 = isis_nexthop6_create(
193 ipv6_addr, adj->circuit->interface->ifindex);
194 nh6->router_address6 = adj->router_address6;
195 listnode_add(nexthops6, nh6);
74361f41 196 return;
d62a17ae 197 }
f390d2c7 198 }
eb5d44eb 199}
eb5d44eb 200
d62a17ae 201static struct isis_route_info *isis_route_info_new(struct prefix *prefix,
202 uint32_t cost,
203 uint32_t depth,
204 struct list *adjacencies)
eb5d44eb 205{
d62a17ae 206 struct isis_route_info *rinfo;
207 struct isis_adjacency *adj;
208 struct listnode *node;
209
210 rinfo = XCALLOC(MTYPE_ISIS_ROUTE_INFO, sizeof(struct isis_route_info));
211
212 if (prefix->family == AF_INET) {
213 rinfo->nexthops = list_new();
214 for (ALL_LIST_ELEMENTS_RO(adjacencies, node, adj)) {
215 /* check for force resync this route */
216 if (CHECK_FLAG(adj->circuit->flags,
217 ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
218 SET_FLAG(rinfo->flag,
219 ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
220 /* update neighbor router address */
221 if (depth == 2 && prefix->prefixlen == 32)
222 adj->router_address = prefix->u.prefix4;
223 adjinfo2nexthop(rinfo->nexthops, adj);
224 }
225 }
226 if (prefix->family == AF_INET6) {
227 rinfo->nexthops6 = list_new();
228 for (ALL_LIST_ELEMENTS_RO(adjacencies, node, adj)) {
229 /* check for force resync this route */
230 if (CHECK_FLAG(adj->circuit->flags,
231 ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
232 SET_FLAG(rinfo->flag,
233 ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
234 /* update neighbor router address */
235 if (depth == 2 && prefix->prefixlen == 128)
236 adj->router_address6 = prefix->u.prefix6;
237 adjinfo2nexthop6(rinfo->nexthops6, adj);
238 }
239 }
240
241 rinfo->cost = cost;
242 rinfo->depth = depth;
243
244 return rinfo;
eb5d44eb 245}
246
d62a17ae 247static void isis_route_info_delete(struct isis_route_info *route_info)
eb5d44eb 248{
d62a17ae 249 if (route_info->nexthops) {
250 route_info->nexthops->del =
251 (void (*)(void *))isis_nexthop_delete;
affe9e99 252 list_delete_and_null(&route_info->nexthops);
d62a17ae 253 }
254
255 if (route_info->nexthops6) {
256 route_info->nexthops6->del =
257 (void (*)(void *))isis_nexthop6_delete;
affe9e99 258 list_delete_and_null(&route_info->nexthops6);
d62a17ae 259 }
260
261 XFREE(MTYPE_ISIS_ROUTE_INFO, route_info);
eb5d44eb 262}
263
d62a17ae 264static int isis_route_info_same_attrib(struct isis_route_info *new,
265 struct isis_route_info *old)
eb5d44eb 266{
d62a17ae 267 if (new->cost != old->cost)
268 return 0;
269 if (new->depth != old->depth)
270 return 0;
f390d2c7 271
d62a17ae 272 return 1;
eb5d44eb 273}
274
d62a17ae 275static int isis_route_info_same(struct isis_route_info *new,
d7c0a89a 276 struct isis_route_info *old, uint8_t family)
eb5d44eb 277{
d62a17ae 278 struct listnode *node;
279 struct isis_nexthop *nexthop;
280 struct isis_nexthop6 *nexthop6;
281
282 if (!CHECK_FLAG(old->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
283 return 0;
284
285 if (CHECK_FLAG(new->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC))
286 return 0;
287
288 if (!isis_route_info_same_attrib(new, old))
289 return 0;
290
291 if (family == AF_INET) {
292 for (ALL_LIST_ELEMENTS_RO(new->nexthops, node, nexthop))
293 if (nexthoplookup(old->nexthops, &nexthop->ip,
294 nexthop->ifindex)
295 == 0)
296 return 0;
297
298 for (ALL_LIST_ELEMENTS_RO(old->nexthops, node, nexthop))
299 if (nexthoplookup(new->nexthops, &nexthop->ip,
300 nexthop->ifindex)
301 == 0)
302 return 0;
303 } else if (family == AF_INET6) {
304 for (ALL_LIST_ELEMENTS_RO(new->nexthops6, node, nexthop6))
305 if (nexthop6lookup(old->nexthops6, &nexthop6->ip6,
306 nexthop6->ifindex)
307 == 0)
308 return 0;
309
310 for (ALL_LIST_ELEMENTS_RO(old->nexthops6, node, nexthop6))
311 if (nexthop6lookup(new->nexthops6, &nexthop6->ip6,
312 nexthop6->ifindex)
313 == 0)
314 return 0;
315 }
316
317 return 1;
eb5d44eb 318}
319
d7c0a89a
QY
320struct isis_route_info *isis_route_create(struct prefix *prefix, uint32_t cost,
321 uint32_t depth,
d62a17ae 322 struct list *adjacencies,
323 struct isis_area *area, int level)
eb5d44eb 324{
d62a17ae 325 struct route_node *route_node;
326 struct isis_route_info *rinfo_new, *rinfo_old, *route_info = NULL;
327 char buff[PREFIX2STR_BUFFER];
d7c0a89a 328 uint8_t family;
d62a17ae 329
330 family = prefix->family;
331 /* for debugs */
332 prefix2str(prefix, buff, sizeof(buff));
333
334 rinfo_new = isis_route_info_new(prefix, cost, depth, adjacencies);
335
336 if (family == AF_INET)
337 route_node =
338 route_node_get(area->route_table[level - 1], prefix);
339 else if (family == AF_INET6)
340 route_node =
341 route_node_get(area->route_table6[level - 1], prefix);
342 else {
343 isis_route_info_delete(rinfo_new);
344 return NULL;
345 }
346
347 rinfo_old = route_node->info;
348 if (!rinfo_old) {
349 if (isis->debugs & DEBUG_RTE_EVENTS)
350 zlog_debug("ISIS-Rte (%s) route created: %s",
351 area->area_tag, buff);
352 route_info = rinfo_new;
353 UNSET_FLAG(route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
354 } else {
355 if (isis->debugs & DEBUG_RTE_EVENTS)
356 zlog_debug("ISIS-Rte (%s) route already exists: %s",
357 area->area_tag, buff);
358 if (isis_route_info_same(rinfo_new, rinfo_old, family)) {
359 if (isis->debugs & DEBUG_RTE_EVENTS)
360 zlog_debug("ISIS-Rte (%s) route unchanged: %s",
361 area->area_tag, buff);
362 isis_route_info_delete(rinfo_new);
363 route_info = rinfo_old;
364 } else {
365 if (isis->debugs & DEBUG_RTE_EVENTS)
366 zlog_debug("ISIS-Rte (%s) route changed: %s",
367 area->area_tag, buff);
368 isis_route_info_delete(rinfo_old);
369 route_info = rinfo_new;
370 UNSET_FLAG(route_info->flag,
371 ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
372 }
373 }
374
375 SET_FLAG(route_info->flag, ISIS_ROUTE_FLAG_ACTIVE);
376 route_node->info = route_info;
377
378 return route_info;
eb5d44eb 379}
380
d62a17ae 381static void isis_route_delete(struct prefix *prefix, struct route_table *table)
eb5d44eb 382{
d62a17ae 383 struct route_node *rode;
384 struct isis_route_info *rinfo;
385 char buff[PREFIX2STR_BUFFER];
386
387 /* for log */
388 prefix2str(prefix, buff, sizeof(buff));
389
390
391 rode = route_node_get(table, prefix);
392 rinfo = rode->info;
393
394 if (rinfo == NULL) {
395 if (isis->debugs & DEBUG_RTE_EVENTS)
396 zlog_debug(
397 "ISIS-Rte: tried to delete non-existant route %s",
398 buff);
399 return;
400 }
401
402 if (CHECK_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED)) {
403 UNSET_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
404 if (isis->debugs & DEBUG_RTE_EVENTS)
405 zlog_debug("ISIS-Rte: route delete %s", buff);
406 isis_zebra_route_update(prefix, rinfo);
407 }
408 isis_route_info_delete(rinfo);
409 rode->info = NULL;
410
411 return;
eb5d44eb 412}
413
fac1f7cc 414/* Validating routes in particular table. */
d62a17ae 415static void isis_route_validate_table(struct isis_area *area,
416 struct route_table *table)
eb5d44eb 417{
d62a17ae 418 struct route_node *rnode, *drnode;
419 struct isis_route_info *rinfo;
420 char buff[PREFIX2STR_BUFFER];
421
422 for (rnode = route_top(table); rnode; rnode = route_next(rnode)) {
423 if (rnode->info == NULL)
424 continue;
425 rinfo = rnode->info;
426
427 if (isis->debugs & DEBUG_RTE_EVENTS) {
428 prefix2str(&rnode->p, buff, sizeof(buff));
429 zlog_debug(
430 "ISIS-Rte (%s): route validate: %s %s %s %s",
431 area->area_tag,
432 (CHECK_FLAG(rinfo->flag,
433 ISIS_ROUTE_FLAG_ZEBRA_SYNCED)
434 ? "synced"
435 : "not-synced"),
436 (CHECK_FLAG(rinfo->flag,
437 ISIS_ROUTE_FLAG_ZEBRA_RESYNC)
438 ? "resync"
439 : "not-resync"),
440 (CHECK_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE)
441 ? "active"
442 : "inactive"),
443 buff);
444 }
445
446 isis_zebra_route_update(&rnode->p, rinfo);
447 if (!CHECK_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE)) {
448 /* Area is either L1 or L2 => we use level route tables
449 * directly for
450 * validating => no problems with deleting routes. */
451 if (area->is_type != IS_LEVEL_1_AND_2) {
452 isis_route_delete(&rnode->p, table);
453 continue;
454 }
455 /* If area is L1L2, we work with merge table and
456 * therefore must
457 * delete node from level tables as well before deleting
458 * route info.
459 * FIXME: Is it performance problem? There has to be the
460 * better way.
461 * Like not to deal with it here at all (see the next
462 * comment)? */
463 if (rnode->p.family == AF_INET) {
464 drnode = route_node_get(area->route_table[0],
465 &rnode->p);
466 if (drnode->info == rnode->info)
467 drnode->info = NULL;
468 drnode = route_node_get(area->route_table[1],
469 &rnode->p);
470 if (drnode->info == rnode->info)
471 drnode->info = NULL;
472 }
473
474 if (rnode->p.family == AF_INET6) {
475 drnode = route_node_get(area->route_table6[0],
476 &rnode->p);
477 if (drnode->info == rnode->info)
478 drnode->info = NULL;
479 drnode = route_node_get(area->route_table6[1],
480 &rnode->p);
481 if (drnode->info == rnode->info)
482 drnode->info = NULL;
483 }
484
485 isis_route_delete(&rnode->p, table);
486 }
f390d2c7 487 }
fac1f7cc 488}
489
490/* Function to validate route tables for L1L2 areas. In this case we can't use
491 * level route tables directly, we have to merge them at first. L1 routes are
492 * preferred over the L2 ones.
493 *
494 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
495 * merge table at first, then L2 paths are added if L1 path for same prefix
496 * doesn't already exists there.
497 *
498 * FIXME: Is it right place to do it at all? Maybe we should push both levels
499 * to the RIB with different zebra route types and let RIB handle this? */
d62a17ae 500static void isis_route_validate_merge(struct isis_area *area, int family)
fac1f7cc 501{
d62a17ae 502 struct route_table *table = NULL;
503 struct route_table *merge;
504 struct route_node *rnode, *mrnode;
505
506 merge = route_table_init();
507
508 if (family == AF_INET)
509 table = area->route_table[0];
510 else if (family == AF_INET6)
511 table = area->route_table6[0];
512 else {
513 zlog_warn("ISIS-Rte (%s) %s called for unknown family %d",
514 area->area_tag, __func__, family);
515 route_table_finish(merge);
516 return;
517 }
518
519 for (rnode = route_top(table); rnode; rnode = route_next(rnode)) {
520 if (rnode->info == NULL)
521 continue;
522 mrnode = route_node_get(merge, &rnode->p);
523 mrnode->info = rnode->info;
524 }
525
526 if (family == AF_INET)
527 table = area->route_table[1];
528 else if (family == AF_INET6)
529 table = area->route_table6[1];
530
531 for (rnode = route_top(table); rnode; rnode = route_next(rnode)) {
532 if (rnode->info == NULL)
533 continue;
534 mrnode = route_node_get(merge, &rnode->p);
535 if (mrnode->info != NULL)
536 continue;
537 mrnode->info = rnode->info;
538 }
539
540 isis_route_validate_table(area, merge);
541 route_table_finish(merge);
fac1f7cc 542}
543
544/* Walk through route tables and propagate necessary changes into RIB. In case
545 * of L1L2 area, level tables have to be merged at first. */
d62a17ae 546void isis_route_validate(struct isis_area *area)
fac1f7cc 547{
d62a17ae 548 struct listnode *node;
549 struct isis_circuit *circuit;
550
551 if (area->is_type == IS_LEVEL_1)
552 isis_route_validate_table(area, area->route_table[0]);
553 else if (area->is_type == IS_LEVEL_2)
554 isis_route_validate_table(area, area->route_table[1]);
555 else
556 isis_route_validate_merge(area, AF_INET);
557
558 if (area->is_type == IS_LEVEL_1)
559 isis_route_validate_table(area, area->route_table6[0]);
560 else if (area->is_type == IS_LEVEL_2)
561 isis_route_validate_table(area, area->route_table6[1]);
562 else
563 isis_route_validate_merge(area, AF_INET6);
564
565 if (!area->circuit_list) {
566 return;
567 }
568 /* walk all circuits and reset any spf specific flags */
569 for (ALL_LIST_ELEMENTS_RO(area->circuit_list, node, circuit))
570 UNSET_FLAG(circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF);
571
572 return;
3f045a08
JB
573}
574
d62a17ae 575void isis_route_invalidate_table(struct isis_area *area,
576 struct route_table *table)
3f045a08 577{
d62a17ae 578 struct route_node *rode;
579 struct isis_route_info *rinfo;
580 for (rode = route_top(table); rode; rode = route_next(rode)) {
581 if (rode->info == NULL)
582 continue;
583 rinfo = rode->info;
584
585 UNSET_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
586 }
3f045a08 587}
eb5d44eb 588
d62a17ae 589void isis_route_invalidate(struct isis_area *area)
3f045a08 590{
d62a17ae 591 if (area->is_type & IS_LEVEL_1)
592 isis_route_invalidate_table(area, area->route_table[0]);
593 if (area->is_type & IS_LEVEL_2)
594 isis_route_invalidate_table(area, area->route_table[1]);
eb5d44eb 595}