]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_route.c
Merge pull request #1290 from qlyoung/doc-commit-msgs
[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 for (unsigned int i = 0; i < adj->ipv4_address_count; i++) {
212 struct in_addr *ipv4_addr = &adj->ipv4_addresses[i];
213 if (!nexthoplookup(nexthops, ipv4_addr,
214 adj->circuit->interface->ifindex)) {
215 nh = isis_nexthop_create(
216 ipv4_addr, adj->circuit->interface->ifindex);
217 nh->router_address = adj->router_address;
218 listnode_add(nexthops, nh);
219 return;
220 }
221 }
222 }
223
224 static void adjinfo2nexthop6(struct list *nexthops6, struct isis_adjacency *adj)
225 {
226 struct isis_nexthop6 *nh6;
227
228 for (unsigned int i = 0; i < adj->ipv6_address_count; i++) {
229 struct in6_addr *ipv6_addr = &adj->ipv6_addresses[i];
230 if (!nexthop6lookup(nexthops6, ipv6_addr,
231 adj->circuit->interface->ifindex)) {
232 nh6 = isis_nexthop6_create(
233 ipv6_addr, adj->circuit->interface->ifindex);
234 nh6->router_address6 = adj->router_address6;
235 listnode_add(nexthops6, nh6);
236 return;
237 }
238 }
239 }
240
241 static struct isis_route_info *isis_route_info_new(struct prefix *prefix,
242 uint32_t cost,
243 uint32_t depth,
244 struct list *adjacencies)
245 {
246 struct isis_route_info *rinfo;
247 struct isis_adjacency *adj;
248 struct listnode *node;
249
250 rinfo = XCALLOC(MTYPE_ISIS_ROUTE_INFO, sizeof(struct isis_route_info));
251
252 if (prefix->family == AF_INET) {
253 rinfo->nexthops = list_new();
254 for (ALL_LIST_ELEMENTS_RO(adjacencies, node, adj)) {
255 /* check for force resync this route */
256 if (CHECK_FLAG(adj->circuit->flags,
257 ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
258 SET_FLAG(rinfo->flag,
259 ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
260 /* update neighbor router address */
261 if (depth == 2 && prefix->prefixlen == 32)
262 adj->router_address = prefix->u.prefix4;
263 adjinfo2nexthop(rinfo->nexthops, adj);
264 }
265 }
266 if (prefix->family == AF_INET6) {
267 rinfo->nexthops6 = list_new();
268 for (ALL_LIST_ELEMENTS_RO(adjacencies, node, adj)) {
269 /* check for force resync this route */
270 if (CHECK_FLAG(adj->circuit->flags,
271 ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
272 SET_FLAG(rinfo->flag,
273 ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
274 /* update neighbor router address */
275 if (depth == 2 && prefix->prefixlen == 128)
276 adj->router_address6 = prefix->u.prefix6;
277 adjinfo2nexthop6(rinfo->nexthops6, adj);
278 }
279 }
280
281 rinfo->cost = cost;
282 rinfo->depth = depth;
283
284 return rinfo;
285 }
286
287 static void isis_route_info_delete(struct isis_route_info *route_info)
288 {
289 if (route_info->nexthops) {
290 route_info->nexthops->del =
291 (void (*)(void *))isis_nexthop_delete;
292 list_delete_and_null(&route_info->nexthops);
293 }
294
295 if (route_info->nexthops6) {
296 route_info->nexthops6->del =
297 (void (*)(void *))isis_nexthop6_delete;
298 list_delete_and_null(&route_info->nexthops6);
299 }
300
301 XFREE(MTYPE_ISIS_ROUTE_INFO, route_info);
302 }
303
304 static int isis_route_info_same_attrib(struct isis_route_info *new,
305 struct isis_route_info *old)
306 {
307 if (new->cost != old->cost)
308 return 0;
309 if (new->depth != old->depth)
310 return 0;
311
312 return 1;
313 }
314
315 static int isis_route_info_same(struct isis_route_info *new,
316 struct isis_route_info *old, u_char family)
317 {
318 struct listnode *node;
319 struct isis_nexthop *nexthop;
320 struct isis_nexthop6 *nexthop6;
321
322 if (!CHECK_FLAG(old->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
323 return 0;
324
325 if (CHECK_FLAG(new->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC))
326 return 0;
327
328 if (!isis_route_info_same_attrib(new, old))
329 return 0;
330
331 if (family == AF_INET) {
332 for (ALL_LIST_ELEMENTS_RO(new->nexthops, node, nexthop))
333 if (nexthoplookup(old->nexthops, &nexthop->ip,
334 nexthop->ifindex)
335 == 0)
336 return 0;
337
338 for (ALL_LIST_ELEMENTS_RO(old->nexthops, node, nexthop))
339 if (nexthoplookup(new->nexthops, &nexthop->ip,
340 nexthop->ifindex)
341 == 0)
342 return 0;
343 } else if (family == AF_INET6) {
344 for (ALL_LIST_ELEMENTS_RO(new->nexthops6, node, nexthop6))
345 if (nexthop6lookup(old->nexthops6, &nexthop6->ip6,
346 nexthop6->ifindex)
347 == 0)
348 return 0;
349
350 for (ALL_LIST_ELEMENTS_RO(old->nexthops6, node, nexthop6))
351 if (nexthop6lookup(new->nexthops6, &nexthop6->ip6,
352 nexthop6->ifindex)
353 == 0)
354 return 0;
355 }
356
357 return 1;
358 }
359
360 struct isis_route_info *isis_route_create(struct prefix *prefix, u_int32_t cost,
361 u_int32_t depth,
362 struct list *adjacencies,
363 struct isis_area *area, int level)
364 {
365 struct route_node *route_node;
366 struct isis_route_info *rinfo_new, *rinfo_old, *route_info = NULL;
367 char buff[PREFIX2STR_BUFFER];
368 u_char family;
369
370 family = prefix->family;
371 /* for debugs */
372 prefix2str(prefix, buff, sizeof(buff));
373
374 rinfo_new = isis_route_info_new(prefix, cost, depth, adjacencies);
375
376 if (family == AF_INET)
377 route_node =
378 route_node_get(area->route_table[level - 1], prefix);
379 else if (family == AF_INET6)
380 route_node =
381 route_node_get(area->route_table6[level - 1], prefix);
382 else {
383 isis_route_info_delete(rinfo_new);
384 return NULL;
385 }
386
387 rinfo_old = route_node->info;
388 if (!rinfo_old) {
389 if (isis->debugs & DEBUG_RTE_EVENTS)
390 zlog_debug("ISIS-Rte (%s) route created: %s",
391 area->area_tag, buff);
392 route_info = rinfo_new;
393 UNSET_FLAG(route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
394 } else {
395 if (isis->debugs & DEBUG_RTE_EVENTS)
396 zlog_debug("ISIS-Rte (%s) route already exists: %s",
397 area->area_tag, buff);
398 if (isis_route_info_same(rinfo_new, rinfo_old, family)) {
399 if (isis->debugs & DEBUG_RTE_EVENTS)
400 zlog_debug("ISIS-Rte (%s) route unchanged: %s",
401 area->area_tag, buff);
402 isis_route_info_delete(rinfo_new);
403 route_info = rinfo_old;
404 } else {
405 if (isis->debugs & DEBUG_RTE_EVENTS)
406 zlog_debug("ISIS-Rte (%s) route changed: %s",
407 area->area_tag, buff);
408 isis_route_info_delete(rinfo_old);
409 route_info = rinfo_new;
410 UNSET_FLAG(route_info->flag,
411 ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
412 }
413 }
414
415 SET_FLAG(route_info->flag, ISIS_ROUTE_FLAG_ACTIVE);
416 route_node->info = route_info;
417
418 return route_info;
419 }
420
421 static void isis_route_delete(struct prefix *prefix, struct route_table *table)
422 {
423 struct route_node *rode;
424 struct isis_route_info *rinfo;
425 char buff[PREFIX2STR_BUFFER];
426
427 /* for log */
428 prefix2str(prefix, buff, sizeof(buff));
429
430
431 rode = route_node_get(table, prefix);
432 rinfo = rode->info;
433
434 if (rinfo == NULL) {
435 if (isis->debugs & DEBUG_RTE_EVENTS)
436 zlog_debug(
437 "ISIS-Rte: tried to delete non-existant route %s",
438 buff);
439 return;
440 }
441
442 if (CHECK_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED)) {
443 UNSET_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
444 if (isis->debugs & DEBUG_RTE_EVENTS)
445 zlog_debug("ISIS-Rte: route delete %s", buff);
446 isis_zebra_route_update(prefix, rinfo);
447 }
448 isis_route_info_delete(rinfo);
449 rode->info = NULL;
450
451 return;
452 }
453
454 /* Validating routes in particular table. */
455 static void isis_route_validate_table(struct isis_area *area,
456 struct route_table *table)
457 {
458 struct route_node *rnode, *drnode;
459 struct isis_route_info *rinfo;
460 char buff[PREFIX2STR_BUFFER];
461
462 for (rnode = route_top(table); rnode; rnode = route_next(rnode)) {
463 if (rnode->info == NULL)
464 continue;
465 rinfo = rnode->info;
466
467 if (isis->debugs & DEBUG_RTE_EVENTS) {
468 prefix2str(&rnode->p, buff, sizeof(buff));
469 zlog_debug(
470 "ISIS-Rte (%s): route validate: %s %s %s %s",
471 area->area_tag,
472 (CHECK_FLAG(rinfo->flag,
473 ISIS_ROUTE_FLAG_ZEBRA_SYNCED)
474 ? "synced"
475 : "not-synced"),
476 (CHECK_FLAG(rinfo->flag,
477 ISIS_ROUTE_FLAG_ZEBRA_RESYNC)
478 ? "resync"
479 : "not-resync"),
480 (CHECK_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE)
481 ? "active"
482 : "inactive"),
483 buff);
484 }
485
486 isis_zebra_route_update(&rnode->p, rinfo);
487 if (!CHECK_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE)) {
488 /* Area is either L1 or L2 => we use level route tables
489 * directly for
490 * validating => no problems with deleting routes. */
491 if (area->is_type != IS_LEVEL_1_AND_2) {
492 isis_route_delete(&rnode->p, table);
493 continue;
494 }
495 /* If area is L1L2, we work with merge table and
496 * therefore must
497 * delete node from level tables as well before deleting
498 * route info.
499 * FIXME: Is it performance problem? There has to be the
500 * better way.
501 * Like not to deal with it here at all (see the next
502 * comment)? */
503 if (rnode->p.family == AF_INET) {
504 drnode = route_node_get(area->route_table[0],
505 &rnode->p);
506 if (drnode->info == rnode->info)
507 drnode->info = NULL;
508 drnode = route_node_get(area->route_table[1],
509 &rnode->p);
510 if (drnode->info == rnode->info)
511 drnode->info = NULL;
512 }
513
514 if (rnode->p.family == AF_INET6) {
515 drnode = route_node_get(area->route_table6[0],
516 &rnode->p);
517 if (drnode->info == rnode->info)
518 drnode->info = NULL;
519 drnode = route_node_get(area->route_table6[1],
520 &rnode->p);
521 if (drnode->info == rnode->info)
522 drnode->info = NULL;
523 }
524
525 isis_route_delete(&rnode->p, table);
526 }
527 }
528 }
529
530 /* Function to validate route tables for L1L2 areas. In this case we can't use
531 * level route tables directly, we have to merge them at first. L1 routes are
532 * preferred over the L2 ones.
533 *
534 * Merge algorithm is trivial (at least for now). All L1 paths are copied into
535 * merge table at first, then L2 paths are added if L1 path for same prefix
536 * doesn't already exists there.
537 *
538 * FIXME: Is it right place to do it at all? Maybe we should push both levels
539 * to the RIB with different zebra route types and let RIB handle this? */
540 static void isis_route_validate_merge(struct isis_area *area, int family)
541 {
542 struct route_table *table = NULL;
543 struct route_table *merge;
544 struct route_node *rnode, *mrnode;
545
546 merge = route_table_init();
547
548 if (family == AF_INET)
549 table = area->route_table[0];
550 else if (family == AF_INET6)
551 table = area->route_table6[0];
552 else {
553 zlog_warn("ISIS-Rte (%s) %s called for unknown family %d",
554 area->area_tag, __func__, family);
555 route_table_finish(merge);
556 return;
557 }
558
559 for (rnode = route_top(table); rnode; rnode = route_next(rnode)) {
560 if (rnode->info == NULL)
561 continue;
562 mrnode = route_node_get(merge, &rnode->p);
563 mrnode->info = rnode->info;
564 }
565
566 if (family == AF_INET)
567 table = area->route_table[1];
568 else if (family == AF_INET6)
569 table = area->route_table6[1];
570
571 for (rnode = route_top(table); rnode; rnode = route_next(rnode)) {
572 if (rnode->info == NULL)
573 continue;
574 mrnode = route_node_get(merge, &rnode->p);
575 if (mrnode->info != NULL)
576 continue;
577 mrnode->info = rnode->info;
578 }
579
580 isis_route_validate_table(area, merge);
581 route_table_finish(merge);
582 }
583
584 /* Walk through route tables and propagate necessary changes into RIB. In case
585 * of L1L2 area, level tables have to be merged at first. */
586 void isis_route_validate(struct isis_area *area)
587 {
588 struct listnode *node;
589 struct isis_circuit *circuit;
590
591 if (area->is_type == IS_LEVEL_1)
592 isis_route_validate_table(area, area->route_table[0]);
593 else if (area->is_type == IS_LEVEL_2)
594 isis_route_validate_table(area, area->route_table[1]);
595 else
596 isis_route_validate_merge(area, AF_INET);
597
598 if (area->is_type == IS_LEVEL_1)
599 isis_route_validate_table(area, area->route_table6[0]);
600 else if (area->is_type == IS_LEVEL_2)
601 isis_route_validate_table(area, area->route_table6[1]);
602 else
603 isis_route_validate_merge(area, AF_INET6);
604
605 if (!area->circuit_list) {
606 return;
607 }
608 /* walk all circuits and reset any spf specific flags */
609 for (ALL_LIST_ELEMENTS_RO(area->circuit_list, node, circuit))
610 UNSET_FLAG(circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF);
611
612 return;
613 }
614
615 void isis_route_invalidate_table(struct isis_area *area,
616 struct route_table *table)
617 {
618 struct route_node *rode;
619 struct isis_route_info *rinfo;
620 for (rode = route_top(table); rode; rode = route_next(rode)) {
621 if (rode->info == NULL)
622 continue;
623 rinfo = rode->info;
624
625 UNSET_FLAG(rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
626 }
627 }
628
629 void isis_route_invalidate(struct isis_area *area)
630 {
631 if (area->is_type & IS_LEVEL_1)
632 isis_route_invalidate_table(area, area->route_table[0]);
633 if (area->is_type & IS_LEVEL_2)
634 isis_route_invalidate_table(area, area->route_table[1]);
635 }