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