]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_spf.c
Merge pull request #10655 from donaldsharp/timers_warning_when_large
[mirror_frr.git] / ospfd / ospf_spf.c
1 /* OSPF SPF calculation.
2 * Copyright (C) 1999, 2000 Kunihiro Ishiguro, Toshiaki Takada
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "monotime.h"
24 #include "thread.h"
25 #include "memory.h"
26 #include "hash.h"
27 #include "linklist.h"
28 #include "prefix.h"
29 #include "if.h"
30 #include "table.h"
31 #include "log.h"
32 #include "sockunion.h" /* for inet_ntop () */
33
34 #include "ospfd/ospfd.h"
35 #include "ospfd/ospf_interface.h"
36 #include "ospfd/ospf_ism.h"
37 #include "ospfd/ospf_asbr.h"
38 #include "ospfd/ospf_lsa.h"
39 #include "ospfd/ospf_lsdb.h"
40 #include "ospfd/ospf_neighbor.h"
41 #include "ospfd/ospf_nsm.h"
42 #include "ospfd/ospf_spf.h"
43 #include "ospfd/ospf_route.h"
44 #include "ospfd/ospf_ia.h"
45 #include "ospfd/ospf_ase.h"
46 #include "ospfd/ospf_abr.h"
47 #include "ospfd/ospf_dump.h"
48 #include "ospfd/ospf_sr.h"
49 #include "ospfd/ospf_ti_lfa.h"
50 #include "ospfd/ospf_errors.h"
51
52 /* Variables to ensure a SPF scheduled log message is printed only once */
53
54 static unsigned int spf_reason_flags = 0;
55
56 /* dummy vertex to flag "in spftree" */
57 static const struct vertex vertex_in_spftree = {};
58 #define LSA_SPF_IN_SPFTREE (struct vertex *)&vertex_in_spftree
59 #define LSA_SPF_NOT_EXPLORED NULL
60
61 static void ospf_clear_spf_reason_flags(void)
62 {
63 spf_reason_flags = 0;
64 }
65
66 static void ospf_spf_set_reason(ospf_spf_reason_t reason)
67 {
68 spf_reason_flags |= 1 << reason;
69 }
70
71 static void ospf_vertex_free(void *);
72
73 /*
74 * Heap related functions, for the managment of the candidates, to
75 * be used with pqueue.
76 */
77 static int vertex_cmp(const struct vertex *v1, const struct vertex *v2)
78 {
79 if (v1->distance != v2->distance)
80 return v1->distance - v2->distance;
81
82 if (v1->type != v2->type) {
83 switch (v1->type) {
84 case OSPF_VERTEX_NETWORK:
85 return -1;
86 case OSPF_VERTEX_ROUTER:
87 return 1;
88 }
89 }
90 return 0;
91 }
92 DECLARE_SKIPLIST_NONUNIQ(vertex_pqueue, struct vertex, pqi, vertex_cmp);
93
94 static void lsdb_clean_stat(struct ospf_lsdb *lsdb)
95 {
96 struct route_table *table;
97 struct route_node *rn;
98 struct ospf_lsa *lsa;
99 int i;
100
101 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
102 table = lsdb->type[i].db;
103 for (rn = route_top(table); rn; rn = route_next(rn))
104 if ((lsa = (rn->info)) != NULL)
105 lsa->stat = LSA_SPF_NOT_EXPLORED;
106 }
107 }
108
109 static struct vertex_nexthop *vertex_nexthop_new(void)
110 {
111 return XCALLOC(MTYPE_OSPF_NEXTHOP, sizeof(struct vertex_nexthop));
112 }
113
114 static void vertex_nexthop_free(struct vertex_nexthop *nh)
115 {
116 XFREE(MTYPE_OSPF_NEXTHOP, nh);
117 }
118
119 /*
120 * Free the canonical nexthop objects for an area, ie the nexthop objects
121 * attached to the first-hop router vertices, and any intervening network
122 * vertices.
123 */
124 static void ospf_canonical_nexthops_free(struct vertex *root)
125 {
126 struct listnode *node, *nnode;
127 struct vertex *child;
128
129 for (ALL_LIST_ELEMENTS(root->children, node, nnode, child)) {
130 struct listnode *n2, *nn2;
131 struct vertex_parent *vp;
132
133 /*
134 * router vertices through an attached network each
135 * have a distinct (canonical / not inherited) nexthop
136 * which must be freed.
137 *
138 * A network vertex can only have router vertices as its
139 * children, so only one level of recursion is possible.
140 */
141 if (child->type == OSPF_VERTEX_NETWORK)
142 ospf_canonical_nexthops_free(child);
143
144 /* Free child nexthops pointing back to this root vertex */
145 for (ALL_LIST_ELEMENTS(child->parents, n2, nn2, vp)) {
146 if (vp->parent == root && vp->nexthop) {
147 vertex_nexthop_free(vp->nexthop);
148 vp->nexthop = NULL;
149 if (vp->local_nexthop) {
150 vertex_nexthop_free(vp->local_nexthop);
151 vp->local_nexthop = NULL;
152 }
153 }
154 }
155 }
156 }
157
158 /*
159 * TODO: Parent list should be excised, in favour of maintaining only
160 * vertex_nexthop, with refcounts.
161 */
162 static struct vertex_parent *vertex_parent_new(struct vertex *v, int backlink,
163 struct vertex_nexthop *hop,
164 struct vertex_nexthop *lhop)
165 {
166 struct vertex_parent *new;
167
168 new = XMALLOC(MTYPE_OSPF_VERTEX_PARENT, sizeof(struct vertex_parent));
169
170 new->parent = v;
171 new->backlink = backlink;
172 new->nexthop = hop;
173 new->local_nexthop = lhop;
174
175 return new;
176 }
177
178 static void vertex_parent_free(void *p)
179 {
180 XFREE(MTYPE_OSPF_VERTEX_PARENT, p);
181 }
182
183 int vertex_parent_cmp(void *aa, void *bb)
184 {
185 struct vertex_parent *a = aa, *b = bb;
186 return IPV4_ADDR_CMP(&a->nexthop->router, &b->nexthop->router);
187 }
188
189 static struct vertex *ospf_vertex_new(struct ospf_area *area,
190 struct ospf_lsa *lsa)
191 {
192 struct vertex *new;
193
194 new = XCALLOC(MTYPE_OSPF_VERTEX, sizeof(struct vertex));
195
196 new->flags = 0;
197 new->type = lsa->data->type;
198 new->id = lsa->data->id;
199 new->lsa = lsa->data;
200 new->children = list_new();
201 new->parents = list_new();
202 new->parents->del = vertex_parent_free;
203 new->parents->cmp = vertex_parent_cmp;
204 new->lsa_p = lsa;
205
206 lsa->stat = new;
207
208 listnode_add(area->spf_vertex_list, new);
209
210 if (IS_DEBUG_OSPF_EVENT)
211 zlog_debug("%s: Created %s vertex %pI4", __func__,
212 new->type == OSPF_VERTEX_ROUTER ? "Router"
213 : "Network",
214 &new->lsa->id);
215
216 return new;
217 }
218
219 static void ospf_vertex_free(void *data)
220 {
221 struct vertex *v = data;
222
223 if (IS_DEBUG_OSPF_EVENT)
224 zlog_debug("%s: Free %s vertex %pI4", __func__,
225 v->type == OSPF_VERTEX_ROUTER ? "Router" : "Network",
226 &v->lsa->id);
227
228 if (v->children)
229 list_delete(&v->children);
230
231 if (v->parents)
232 list_delete(&v->parents);
233
234 v->lsa = NULL;
235
236 XFREE(MTYPE_OSPF_VERTEX, v);
237 }
238
239 static void ospf_vertex_dump(const char *msg, struct vertex *v,
240 int print_parents, int print_children)
241 {
242 if (!IS_DEBUG_OSPF_EVENT)
243 return;
244
245 zlog_debug("%s %s vertex %pI4 distance %u flags %u", msg,
246 v->type == OSPF_VERTEX_ROUTER ? "Router" : "Network",
247 &v->lsa->id, v->distance, (unsigned int)v->flags);
248
249 if (print_parents) {
250 struct listnode *node;
251 struct vertex_parent *vp;
252
253 for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) {
254 if (vp) {
255 zlog_debug(
256 "parent %pI4 backlink %d nexthop %pI4 lsa pos %d",
257 &vp->parent->lsa->id, vp->backlink,
258 &vp->nexthop->router,
259 vp->nexthop->lsa_pos);
260 }
261 }
262 }
263
264 if (print_children) {
265 struct listnode *cnode;
266 struct vertex *cv;
267
268 for (ALL_LIST_ELEMENTS_RO(v->children, cnode, cv))
269 ospf_vertex_dump(" child:", cv, 0, 0);
270 }
271 }
272
273
274 /* Add a vertex to the list of children in each of its parents. */
275 static void ospf_vertex_add_parent(struct vertex *v)
276 {
277 struct vertex_parent *vp;
278 struct listnode *node;
279
280 assert(v && v->parents);
281
282 for (ALL_LIST_ELEMENTS_RO(v->parents, node, vp)) {
283 assert(vp->parent && vp->parent->children);
284
285 /* No need to add two links from the same parent. */
286 if (listnode_lookup(vp->parent->children, v) == NULL)
287 listnode_add(vp->parent->children, v);
288 }
289 }
290
291 /* Find a vertex according to its router id */
292 struct vertex *ospf_spf_vertex_find(struct in_addr id, struct list *vertex_list)
293 {
294 struct listnode *node;
295 struct vertex *found;
296
297 for (ALL_LIST_ELEMENTS_RO(vertex_list, node, found)) {
298 if (found->id.s_addr == id.s_addr)
299 return found;
300 }
301
302 return NULL;
303 }
304
305 /* Find a vertex parent according to its router id */
306 struct vertex_parent *ospf_spf_vertex_parent_find(struct in_addr id,
307 struct vertex *vertex)
308 {
309 struct listnode *node;
310 struct vertex_parent *found;
311
312 for (ALL_LIST_ELEMENTS_RO(vertex->parents, node, found)) {
313 if (found->parent->id.s_addr == id.s_addr)
314 return found;
315 }
316
317 return NULL;
318 }
319
320 struct vertex *ospf_spf_vertex_by_nexthop(struct vertex *root,
321 struct in_addr *nexthop)
322 {
323 struct listnode *node;
324 struct vertex *child;
325 struct vertex_parent *vertex_parent;
326
327 for (ALL_LIST_ELEMENTS_RO(root->children, node, child)) {
328 vertex_parent = ospf_spf_vertex_parent_find(root->id, child);
329 if (vertex_parent->nexthop->router.s_addr == nexthop->s_addr)
330 return child;
331 }
332
333 return NULL;
334 }
335
336 /* Create a deep copy of a SPF vertex without children and parents */
337 static struct vertex *ospf_spf_vertex_copy(struct vertex *vertex)
338 {
339 struct vertex *copy;
340
341 copy = XCALLOC(MTYPE_OSPF_VERTEX, sizeof(struct vertex));
342
343 memcpy(copy, vertex, sizeof(struct vertex));
344 copy->parents = list_new();
345 copy->parents->del = vertex_parent_free;
346 copy->parents->cmp = vertex_parent_cmp;
347 copy->children = list_new();
348
349 return copy;
350 }
351
352 /* Create a deep copy of a SPF vertex_parent */
353 static struct vertex_parent *
354 ospf_spf_vertex_parent_copy(struct vertex_parent *vertex_parent)
355 {
356 struct vertex_parent *vertex_parent_copy;
357 struct vertex_nexthop *nexthop_copy, *local_nexthop_copy;
358
359 vertex_parent_copy =
360 XCALLOC(MTYPE_OSPF_VERTEX, sizeof(struct vertex_parent));
361
362 nexthop_copy = vertex_nexthop_new();
363 local_nexthop_copy = vertex_nexthop_new();
364
365 memcpy(vertex_parent_copy, vertex_parent, sizeof(struct vertex_parent));
366 memcpy(nexthop_copy, vertex_parent->nexthop,
367 sizeof(struct vertex_nexthop));
368 memcpy(local_nexthop_copy, vertex_parent->local_nexthop,
369 sizeof(struct vertex_nexthop));
370
371 vertex_parent_copy->nexthop = nexthop_copy;
372 vertex_parent_copy->local_nexthop = local_nexthop_copy;
373
374 return vertex_parent_copy;
375 }
376
377 /* Create a deep copy of a SPF tree */
378 void ospf_spf_copy(struct vertex *vertex, struct list *vertex_list)
379 {
380 struct listnode *node;
381 struct vertex *vertex_copy, *child, *child_copy, *parent_copy;
382 struct vertex_parent *vertex_parent, *vertex_parent_copy;
383
384 /* First check if the node is already in the vertex list */
385 vertex_copy = ospf_spf_vertex_find(vertex->id, vertex_list);
386 if (!vertex_copy) {
387 vertex_copy = ospf_spf_vertex_copy(vertex);
388 listnode_add(vertex_list, vertex_copy);
389 }
390
391 /* Copy all parents, create parent nodes if necessary */
392 for (ALL_LIST_ELEMENTS_RO(vertex->parents, node, vertex_parent)) {
393 parent_copy = ospf_spf_vertex_find(vertex_parent->parent->id,
394 vertex_list);
395 if (!parent_copy) {
396 parent_copy =
397 ospf_spf_vertex_copy(vertex_parent->parent);
398 listnode_add(vertex_list, parent_copy);
399 }
400 vertex_parent_copy = ospf_spf_vertex_parent_copy(vertex_parent);
401 vertex_parent_copy->parent = parent_copy;
402 listnode_add(vertex_copy->parents, vertex_parent_copy);
403 }
404
405 /* Copy all children, create child nodes if necessary */
406 for (ALL_LIST_ELEMENTS_RO(vertex->children, node, child)) {
407 child_copy = ospf_spf_vertex_find(child->id, vertex_list);
408 if (!child_copy) {
409 child_copy = ospf_spf_vertex_copy(child);
410 listnode_add(vertex_list, child_copy);
411 }
412 listnode_add(vertex_copy->children, child_copy);
413 }
414
415 /* Finally continue copying with child nodes */
416 for (ALL_LIST_ELEMENTS_RO(vertex->children, node, child))
417 ospf_spf_copy(child, vertex_list);
418 }
419
420 static void ospf_spf_remove_branch(struct vertex_parent *vertex_parent,
421 struct vertex *child,
422 struct list *vertex_list)
423 {
424 struct listnode *node, *nnode, *inner_node, *inner_nnode;
425 struct vertex *grandchild;
426 struct vertex_parent *vertex_parent_found;
427 bool has_more_links = false;
428
429 /*
430 * First check if there are more nexthops for that parent to that child
431 */
432 for (ALL_LIST_ELEMENTS_RO(child->parents, node, vertex_parent_found)) {
433 if (vertex_parent_found->parent->id.s_addr
434 == vertex_parent->parent->id.s_addr
435 && vertex_parent_found->nexthop->router.s_addr
436 != vertex_parent->nexthop->router.s_addr)
437 has_more_links = true;
438 }
439
440 /*
441 * No more links from that parent? Then delete the child from its
442 * children list.
443 */
444 if (!has_more_links)
445 listnode_delete(vertex_parent->parent->children, child);
446
447 /*
448 * Delete the vertex_parent from the child parents list, this needs to
449 * be done anyway.
450 */
451 listnode_delete(child->parents, vertex_parent);
452
453 /*
454 * Are there actually more parents left? If not, then delete the child!
455 * This is done by recursively removing the links to the grandchildren,
456 * such that finally the child can be removed without leaving unused
457 * partial branches.
458 */
459 if (child->parents->count == 0) {
460 for (ALL_LIST_ELEMENTS(child->children, node, nnode,
461 grandchild)) {
462 for (ALL_LIST_ELEMENTS(grandchild->parents, inner_node,
463 inner_nnode,
464 vertex_parent_found)) {
465 ospf_spf_remove_branch(vertex_parent_found,
466 grandchild, vertex_list);
467 }
468 }
469 listnode_delete(vertex_list, child);
470 ospf_vertex_free(child);
471 }
472 }
473
474 static int ospf_spf_remove_link(struct vertex *vertex, struct list *vertex_list,
475 struct router_lsa_link *link)
476 {
477 struct listnode *node, *inner_node;
478 struct vertex *child;
479 struct vertex_parent *vertex_parent;
480
481 /*
482 * Identify the node who shares a subnet (given by the link) with a
483 * child and remove the branch of this particular child.
484 */
485 for (ALL_LIST_ELEMENTS_RO(vertex->children, node, child)) {
486 for (ALL_LIST_ELEMENTS_RO(child->parents, inner_node,
487 vertex_parent)) {
488 if ((vertex_parent->local_nexthop->router.s_addr
489 & link->link_data.s_addr)
490 == (link->link_id.s_addr
491 & link->link_data.s_addr)) {
492 ospf_spf_remove_branch(vertex_parent, child,
493 vertex_list);
494 return 0;
495 }
496 }
497 }
498
499 /* No link found yet, move on recursively */
500 for (ALL_LIST_ELEMENTS_RO(vertex->children, node, child)) {
501 if (ospf_spf_remove_link(child, vertex_list, link) == 0)
502 return 0;
503 }
504
505 /* link was not removed yet */
506 return 1;
507 }
508
509 void ospf_spf_remove_resource(struct vertex *vertex, struct list *vertex_list,
510 struct protected_resource *resource)
511 {
512 struct listnode *node, *nnode;
513 struct vertex *found;
514 struct vertex_parent *vertex_parent;
515
516 switch (resource->type) {
517 case OSPF_TI_LFA_LINK_PROTECTION:
518 ospf_spf_remove_link(vertex, vertex_list, resource->link);
519 break;
520 case OSPF_TI_LFA_NODE_PROTECTION:
521 found = ospf_spf_vertex_find(resource->router_id, vertex_list);
522 if (!found)
523 break;
524
525 /*
526 * Remove the node by removing all links from its parents. Note
527 * that the child is automatically removed here with the last
528 * link from a parent, hence no explicit removal of the node.
529 */
530 for (ALL_LIST_ELEMENTS(found->parents, node, nnode,
531 vertex_parent))
532 ospf_spf_remove_branch(vertex_parent, found,
533 vertex_list);
534
535 break;
536 default:
537 /* do nothing */
538 break;
539 }
540 }
541
542 static void ospf_spf_init(struct ospf_area *area, struct ospf_lsa *root_lsa,
543 bool is_dry_run, bool is_root_node)
544 {
545 struct list *vertex_list;
546 struct vertex *v;
547
548 /* Create vertex list */
549 vertex_list = list_new();
550 vertex_list->del = ospf_vertex_free;
551 area->spf_vertex_list = vertex_list;
552
553 /* Create root node. */
554 v = ospf_vertex_new(area, root_lsa);
555 area->spf = v;
556
557 area->spf_dry_run = is_dry_run;
558 area->spf_root_node = is_root_node;
559
560 /* Reset ABR and ASBR router counts. */
561 area->abr_count = 0;
562 area->asbr_count = 0;
563 }
564
565 /* return index of link back to V from W, or -1 if no link found */
566 static int ospf_lsa_has_link(struct lsa_header *w, struct lsa_header *v)
567 {
568 unsigned int i, length;
569 struct router_lsa *rl;
570 struct network_lsa *nl;
571
572 /* In case of W is Network LSA. */
573 if (w->type == OSPF_NETWORK_LSA) {
574 if (v->type == OSPF_NETWORK_LSA)
575 return -1;
576
577 nl = (struct network_lsa *)w;
578 length = (ntohs(w->length) - OSPF_LSA_HEADER_SIZE - 4) / 4;
579
580 for (i = 0; i < length; i++)
581 if (IPV4_ADDR_SAME(&nl->routers[i], &v->id))
582 return i;
583 return -1;
584 }
585
586 /* In case of W is Router LSA. */
587 if (w->type == OSPF_ROUTER_LSA) {
588 rl = (struct router_lsa *)w;
589
590 length = ntohs(w->length);
591
592 for (i = 0; i < ntohs(rl->links)
593 && length >= sizeof(struct router_lsa);
594 i++, length -= 12) {
595 switch (rl->link[i].type) {
596 case LSA_LINK_TYPE_POINTOPOINT:
597 case LSA_LINK_TYPE_VIRTUALLINK:
598 /* Router LSA ID. */
599 if (v->type == OSPF_ROUTER_LSA
600 && IPV4_ADDR_SAME(&rl->link[i].link_id,
601 &v->id)) {
602 return i;
603 }
604 break;
605 case LSA_LINK_TYPE_TRANSIT:
606 /* Network LSA ID. */
607 if (v->type == OSPF_NETWORK_LSA
608 && IPV4_ADDR_SAME(&rl->link[i].link_id,
609 &v->id)) {
610 return i;
611 }
612 break;
613 case LSA_LINK_TYPE_STUB:
614 /* Stub can't lead anywhere, carry on */
615 continue;
616 default:
617 break;
618 }
619 }
620 }
621 return -1;
622 }
623
624 /*
625 * Find the next link after prev_link from v to w. If prev_link is
626 * NULL, return the first link from v to w. Ignore stub and virtual links;
627 * these link types will never be returned.
628 */
629 static struct router_lsa_link *
630 ospf_get_next_link(struct vertex *v, struct vertex *w,
631 struct router_lsa_link *prev_link)
632 {
633 uint8_t *p;
634 uint8_t *lim;
635 uint8_t lsa_type = LSA_LINK_TYPE_TRANSIT;
636 struct router_lsa_link *l;
637
638 if (w->type == OSPF_VERTEX_ROUTER)
639 lsa_type = LSA_LINK_TYPE_POINTOPOINT;
640
641 if (prev_link == NULL)
642 p = ((uint8_t *)v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
643 else {
644 p = (uint8_t *)prev_link;
645 p += (OSPF_ROUTER_LSA_LINK_SIZE
646 + (prev_link->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
647 }
648
649 lim = ((uint8_t *)v->lsa) + ntohs(v->lsa->length);
650
651 while (p < lim) {
652 l = (struct router_lsa_link *)p;
653
654 p += (OSPF_ROUTER_LSA_LINK_SIZE
655 + (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
656
657 if (l->m[0].type != lsa_type)
658 continue;
659
660 if (IPV4_ADDR_SAME(&l->link_id, &w->id))
661 return l;
662 }
663
664 return NULL;
665 }
666
667 static void ospf_spf_flush_parents(struct vertex *w)
668 {
669 struct vertex_parent *vp;
670 struct listnode *ln, *nn;
671
672 /* delete the existing nexthops */
673 for (ALL_LIST_ELEMENTS(w->parents, ln, nn, vp)) {
674 list_delete_node(w->parents, ln);
675 vertex_parent_free(vp);
676 }
677 }
678
679 /*
680 * Consider supplied next-hop for inclusion to the supplied list of
681 * equal-cost next-hops, adjust list as neccessary.
682 */
683 static void ospf_spf_add_parent(struct vertex *v, struct vertex *w,
684 struct vertex_nexthop *newhop,
685 struct vertex_nexthop *newlhop,
686 unsigned int distance)
687 {
688 struct vertex_parent *vp, *wp;
689 struct listnode *node;
690
691 /* we must have a newhop, and a distance */
692 assert(v && w && newhop);
693 assert(distance);
694
695 /*
696 * IFF w has already been assigned a distance, then we shouldn't get
697 * here unless callers have determined V(l)->W is shortest /
698 * equal-shortest path (0 is a special case distance (no distance yet
699 * assigned)).
700 */
701 if (w->distance)
702 assert(distance <= w->distance);
703 else
704 w->distance = distance;
705
706 if (IS_DEBUG_OSPF_EVENT)
707 zlog_debug("%s: Adding %pI4 as parent of %pI4", __func__,
708 &v->lsa->id, &w->lsa->id);
709
710 /*
711 * Adding parent for a new, better path: flush existing parents from W.
712 */
713 if (distance < w->distance) {
714 if (IS_DEBUG_OSPF_EVENT)
715 zlog_debug(
716 "%s: distance %d better than %d, flushing existing parents",
717 __func__, distance, w->distance);
718 ospf_spf_flush_parents(w);
719 w->distance = distance;
720 }
721
722 /*
723 * new parent is <= existing parents, add it to parent list (if nexthop
724 * not on parent list)
725 */
726 for (ALL_LIST_ELEMENTS_RO(w->parents, node, wp)) {
727 if (memcmp(newhop, wp->nexthop, sizeof(*newhop)) == 0) {
728 if (IS_DEBUG_OSPF_EVENT)
729 zlog_debug(
730 "%s: ... nexthop already on parent list, skipping add",
731 __func__);
732 return;
733 }
734 }
735
736 vp = vertex_parent_new(v, ospf_lsa_has_link(w->lsa, v->lsa), newhop,
737 newlhop);
738 listnode_add_sort(w->parents, vp);
739
740 return;
741 }
742
743 static int match_stub_prefix(struct lsa_header *lsa, struct in_addr v_link_addr,
744 struct in_addr w_link_addr)
745 {
746 uint8_t *p, *lim;
747 struct router_lsa_link *l = NULL;
748 struct in_addr masked_lsa_addr;
749
750 if (lsa->type != OSPF_ROUTER_LSA)
751 return 0;
752
753 p = ((uint8_t *)lsa) + OSPF_LSA_HEADER_SIZE + 4;
754 lim = ((uint8_t *)lsa) + ntohs(lsa->length);
755
756 while (p < lim) {
757 l = (struct router_lsa_link *)p;
758 p += (OSPF_ROUTER_LSA_LINK_SIZE
759 + (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
760
761 if (l->m[0].type != LSA_LINK_TYPE_STUB)
762 continue;
763
764 masked_lsa_addr.s_addr =
765 (l->link_id.s_addr & l->link_data.s_addr);
766
767 /* check that both links belong to the same stub subnet */
768 if ((masked_lsa_addr.s_addr
769 == (v_link_addr.s_addr & l->link_data.s_addr))
770 && (masked_lsa_addr.s_addr
771 == (w_link_addr.s_addr & l->link_data.s_addr)))
772 return 1;
773 }
774
775 return 0;
776 }
777
778 /*
779 * 16.1.1. Calculate nexthop from root through V (parent) to
780 * vertex W (destination), with given distance from root->W.
781 *
782 * The link must be supplied if V is the root vertex. In all other cases
783 * it may be NULL.
784 *
785 * Note that this function may fail, hence the state of the destination
786 * vertex, W, should /not/ be modified in a dependent manner until
787 * this function returns. This function will update the W vertex with the
788 * provided distance as appropriate.
789 */
790 static unsigned int ospf_nexthop_calculation(struct ospf_area *area,
791 struct vertex *v, struct vertex *w,
792 struct router_lsa_link *l,
793 unsigned int distance, int lsa_pos)
794 {
795 struct listnode *node, *nnode;
796 struct vertex_nexthop *nh, *lnh;
797 struct vertex_parent *vp;
798 unsigned int added = 0;
799
800 if (IS_DEBUG_OSPF_EVENT) {
801 zlog_debug("ospf_nexthop_calculation(): Start");
802 ospf_vertex_dump("V (parent):", v, 1, 1);
803 ospf_vertex_dump("W (dest) :", w, 1, 1);
804 zlog_debug("V->W distance: %d", distance);
805 }
806
807 if (v == area->spf) {
808 /*
809 * 16.1.1 para 4. In the first case, the parent vertex (V) is
810 * the root (the calculating router itself). This means that
811 * the destination is either a directly connected network or
812 * directly connected router. The outgoing interface in this
813 * case is simply the OSPF interface connecting to the
814 * destination network/router.
815 */
816
817 /* we *must* be supplied with the link data */
818 assert(l != NULL);
819
820 if (IS_DEBUG_OSPF_EVENT)
821 zlog_debug(
822 "%s: considering link type:%d link_id:%pI4 link_data:%pI4",
823 __func__, l->m[0].type, &l->link_id,
824 &l->link_data);
825
826 if (w->type == OSPF_VERTEX_ROUTER) {
827 /*
828 * l is a link from v to w l2 will be link from w to v
829 */
830 struct router_lsa_link *l2 = NULL;
831
832 if (l->m[0].type == LSA_LINK_TYPE_POINTOPOINT) {
833 struct ospf_interface *oi = NULL;
834 struct in_addr nexthop = {.s_addr = 0};
835
836 if (area->spf_root_node) {
837 oi = ospf_if_lookup_by_lsa_pos(area,
838 lsa_pos);
839 if (!oi) {
840 zlog_debug(
841 "%s: OI not found in LSA: lsa_pos: %d link_id:%pI4 link_data:%pI4",
842 __func__, lsa_pos,
843 &l->link_id,
844 &l->link_data);
845 return 0;
846 }
847 }
848
849 /*
850 * If the destination is a router which connects
851 * to the calculating router via a
852 * Point-to-MultiPoint network, the
853 * destination's next hop IP address(es) can be
854 * determined by examining the destination's
855 * router-LSA: each link pointing back to the
856 * calculating router and having a Link Data
857 * field belonging to the Point-to-MultiPoint
858 * network provides an IP address of the next
859 * hop router.
860 *
861 * At this point l is a link from V to W, and V
862 * is the root ("us"). If it is a point-to-
863 * multipoint interface, then look through the
864 * links in the opposite direction (W to V).
865 * If any of them have an address that lands
866 * within the subnet declared by the PtMP link,
867 * then that link is a constituent of the PtMP
868 * link, and its address is a nexthop address
869 * for V.
870 *
871 * Note for point-to-point interfaces:
872 *
873 * Having nexthop = 0 (as proposed in the RFC)
874 * is tempting, but NOT acceptable. It breaks
875 * AS-External routes with a forwarding address,
876 * since ospf_ase_complete_direct_routes() will
877 * mistakenly assume we've reached the last hop
878 * and should place the forwarding address as
879 * nexthop. Also, users may configure multi-
880 * access links in p2p mode, so we need the IP
881 * to ARP the nexthop.
882 *
883 * If the calculating router is the SPF root
884 * node and the link is P2P then access the
885 * interface information directly. This can be
886 * crucial when e.g. IP unnumbered is used
887 * where 'correct' nexthop information are not
888 * available via Router LSAs.
889 *
890 * Otherwise handle P2P and P2MP the same way
891 * as described above using a reverse lookup to
892 * figure out the nexthop.
893 */
894
895 /*
896 * HACK: we don't know (yet) how to distinguish
897 * between P2P and P2MP interfaces by just
898 * looking at LSAs, which is important for
899 * TI-LFA since you want to do SPF calculations
900 * from the perspective of other nodes. Since
901 * TI-LFA is currently not implemented for P2MP
902 * we just check here if it is enabled and then
903 * blindly assume that P2P is used. Ultimately
904 * the interface code needs to be removed
905 * somehow.
906 */
907 if (area->ospf->ti_lfa_enabled
908 || (oi && oi->type == OSPF_IFTYPE_POINTOPOINT)
909 || (oi && oi->type == OSPF_IFTYPE_POINTOMULTIPOINT
910 && oi->address->prefixlen == IPV4_MAX_BITLEN)) {
911 struct ospf_neighbor *nbr_w = NULL;
912
913 /* Calculating node is root node, link
914 * is P2P */
915 if (area->spf_root_node) {
916 nbr_w = ospf_nbr_lookup_by_routerid(
917 oi->nbrs, &l->link_id);
918 if (nbr_w) {
919 added = 1;
920 nexthop = nbr_w->src;
921 }
922 }
923
924 /* Reverse lookup */
925 if (!added) {
926 while ((l2 = ospf_get_next_link(
927 w, v, l2))) {
928 if (match_stub_prefix(
929 v->lsa,
930 l->link_data,
931 l2->link_data)) {
932 added = 1;
933 nexthop =
934 l2->link_data;
935 break;
936 }
937 }
938 }
939 } else if (oi && oi->type
940 == OSPF_IFTYPE_POINTOMULTIPOINT) {
941 struct prefix_ipv4 la;
942
943 la.family = AF_INET;
944 la.prefixlen = oi->address->prefixlen;
945
946 /*
947 * V links to W on PtMP interface;
948 * find the interface address on W
949 */
950 while ((l2 = ospf_get_next_link(w, v,
951 l2))) {
952 la.prefix = l2->link_data;
953
954 if (prefix_cmp((struct prefix
955 *)&la,
956 oi->address)
957 != 0)
958 continue;
959 added = 1;
960 nexthop = l2->link_data;
961 break;
962 }
963 }
964
965 if (added) {
966 nh = vertex_nexthop_new();
967 nh->router = nexthop;
968 nh->lsa_pos = lsa_pos;
969
970 /*
971 * Since v is the root the nexthop and
972 * local nexthop are the same.
973 */
974 lnh = vertex_nexthop_new();
975 memcpy(lnh, nh,
976 sizeof(struct vertex_nexthop));
977
978 ospf_spf_add_parent(v, w, nh, lnh,
979 distance);
980 return 1;
981 } else
982 zlog_info(
983 "%s: could not determine nexthop for link %s",
984 __func__, oi ? oi->ifp->name : "");
985 } /* end point-to-point link from V to W */
986 else if (l->m[0].type == LSA_LINK_TYPE_VIRTUALLINK) {
987 /*
988 * VLink implementation limitations:
989 * a) vl_data can only reference one nexthop,
990 * so no ECMP to backbone through VLinks.
991 * Though transit-area summaries may be
992 * considered, and those can be ECMP.
993 * b) We can only use /one/ VLink, even if
994 * multiple ones exist this router through
995 * multiple transit-areas.
996 */
997
998 struct ospf_vl_data *vl_data;
999
1000 vl_data = ospf_vl_lookup(area->ospf, NULL,
1001 l->link_id);
1002
1003 if (vl_data
1004 && CHECK_FLAG(vl_data->flags,
1005 OSPF_VL_FLAG_APPROVED)) {
1006 nh = vertex_nexthop_new();
1007 nh->router = vl_data->nexthop.router;
1008 nh->lsa_pos = vl_data->nexthop.lsa_pos;
1009
1010 /*
1011 * Since v is the root the nexthop and
1012 * local nexthop are the same.
1013 */
1014 lnh = vertex_nexthop_new();
1015 memcpy(lnh, nh,
1016 sizeof(struct vertex_nexthop));
1017
1018 ospf_spf_add_parent(v, w, nh, lnh,
1019 distance);
1020 return 1;
1021 } else
1022 zlog_info(
1023 "ospf_nexthop_calculation(): vl_data for VL link not found");
1024 } /* end virtual-link from V to W */
1025 return 0;
1026 } /* end W is a Router vertex */
1027 else {
1028 assert(w->type == OSPF_VERTEX_NETWORK);
1029
1030 nh = vertex_nexthop_new();
1031 nh->router.s_addr = 0; /* Nexthop not required */
1032 nh->lsa_pos = lsa_pos;
1033
1034 /*
1035 * Since v is the root the nexthop and
1036 * local nexthop are the same.
1037 */
1038 lnh = vertex_nexthop_new();
1039 memcpy(lnh, nh, sizeof(struct vertex_nexthop));
1040
1041 ospf_spf_add_parent(v, w, nh, lnh, distance);
1042 return 1;
1043 }
1044 } /* end V is the root */
1045 /* Check if W's parent is a network connected to root. */
1046 else if (v->type == OSPF_VERTEX_NETWORK) {
1047 /* See if any of V's parents are the root. */
1048 for (ALL_LIST_ELEMENTS(v->parents, node, nnode, vp)) {
1049 if (vp->parent == area->spf) {
1050 /*
1051 * 16.1.1 para 5. ...the parent vertex is a
1052 * network that directly connects the
1053 * calculating router to the destination
1054 * router. The list of next hops is then
1055 * determined by examining the destination's
1056 * router-LSA ...
1057 */
1058
1059 assert(w->type == OSPF_VERTEX_ROUTER);
1060 while ((l = ospf_get_next_link(w, v, l))) {
1061 /*
1062 * ... For each link in the router-LSA
1063 * that points back to the parent
1064 * network, the link's Link Data field
1065 * provides the IP address of a next hop
1066 * router. The outgoing interface to use
1067 * can then be derived from the next
1068 * hop IP address (or it can be
1069 * inherited from the parent network).
1070 */
1071 nh = vertex_nexthop_new();
1072 nh->router = l->link_data;
1073 nh->lsa_pos = vp->nexthop->lsa_pos;
1074
1075 /*
1076 * Since v is the root the nexthop and
1077 * local nexthop are the same.
1078 */
1079 lnh = vertex_nexthop_new();
1080 memcpy(lnh, nh,
1081 sizeof(struct vertex_nexthop));
1082
1083 added = 1;
1084 ospf_spf_add_parent(v, w, nh, lnh,
1085 distance);
1086 }
1087 /*
1088 * Note lack of return is deliberate. See next
1089 * comment.
1090 */
1091 }
1092 }
1093 /*
1094 * NB: This code is non-trivial.
1095 *
1096 * E.g. it is not enough to know that V connects to the root. It
1097 * is also important that the while above, looping through all
1098 * links from W->V found at least one link, so that we know
1099 * there is bi-directional connectivity between V and W (which
1100 * need not be the case, e.g. when OSPF has not yet converged
1101 * fully). Otherwise, if we /always/ return here, without having
1102 * checked that root->V->-W actually resulted in a valid nexthop
1103 * being created, then we we will prevent SPF from finding/using
1104 * higher cost paths.
1105 *
1106 * It is important, if root->V->W has not been added, that we
1107 * continue through to the intervening-router nexthop code
1108 * below. So as to ensure other paths to V may be used. This
1109 * avoids unnecessary blackholes while OSPF is converging.
1110 *
1111 * I.e. we may have arrived at this function, examining V -> W,
1112 * via workable paths other than root -> V, and it's important
1113 * to avoid getting "confused" by non-working root->V->W path
1114 * - it's important to *not* lose the working non-root paths,
1115 * just because of a non-viable root->V->W.
1116 */
1117 if (added)
1118 return added;
1119 }
1120
1121 /*
1122 * 16.1.1 para 4. If there is at least one intervening router in the
1123 * current shortest path between the destination and the root, the
1124 * destination simply inherits the set of next hops from the
1125 * parent.
1126 */
1127 if (IS_DEBUG_OSPF_EVENT)
1128 zlog_debug("%s: Intervening routers, adding parent(s)",
1129 __func__);
1130
1131 for (ALL_LIST_ELEMENTS(v->parents, node, nnode, vp)) {
1132 added = 1;
1133
1134 /*
1135 * The nexthop is inherited, but the local nexthop still needs
1136 * to be created.
1137 */
1138 if (l) {
1139 lnh = vertex_nexthop_new();
1140 lnh->router = l->link_data;
1141 lnh->lsa_pos = lsa_pos;
1142 } else {
1143 lnh = NULL;
1144 }
1145
1146 ospf_spf_add_parent(v, w, vp->nexthop, lnh, distance);
1147 }
1148
1149 return added;
1150 }
1151
1152 static int ospf_spf_is_protected_resource(struct ospf_area *area,
1153 struct router_lsa_link *link,
1154 struct lsa_header *lsa)
1155 {
1156 uint8_t *p, *lim;
1157 struct router_lsa_link *p_link;
1158 struct router_lsa_link *l = NULL;
1159 struct in_addr router_id;
1160 int link_type;
1161
1162 if (!area->spf_protected_resource)
1163 return 0;
1164
1165 link_type = link->m[0].type;
1166
1167 switch (area->spf_protected_resource->type) {
1168 case OSPF_TI_LFA_LINK_PROTECTION:
1169 p_link = area->spf_protected_resource->link;
1170 if (!p_link)
1171 return 0;
1172
1173 /* For P2P: check if the link belongs to the same subnet */
1174 if (link_type == LSA_LINK_TYPE_POINTOPOINT
1175 && (p_link->link_id.s_addr & p_link->link_data.s_addr)
1176 == (link->link_data.s_addr
1177 & p_link->link_data.s_addr))
1178 return 1;
1179
1180 /* For stub: check if this the same subnet */
1181 if (link_type == LSA_LINK_TYPE_STUB
1182 && (p_link->link_id.s_addr == link->link_id.s_addr)
1183 && (p_link->link_data.s_addr == link->link_data.s_addr))
1184 return 1;
1185
1186 break;
1187 case OSPF_TI_LFA_NODE_PROTECTION:
1188 router_id = area->spf_protected_resource->router_id;
1189 if (router_id.s_addr == INADDR_ANY)
1190 return 0;
1191
1192 /* For P2P: check if the link leads to the protected node */
1193 if (link_type == LSA_LINK_TYPE_POINTOPOINT
1194 && link->link_id.s_addr == router_id.s_addr)
1195 return 1;
1196
1197 /* The rest is about stub links! */
1198 if (link_type != LSA_LINK_TYPE_STUB)
1199 return 0;
1200
1201 /*
1202 * Check if there's a P2P link in the router LSA with the
1203 * corresponding link data in the same subnet.
1204 */
1205
1206 p = ((uint8_t *)lsa) + OSPF_LSA_HEADER_SIZE + 4;
1207 lim = ((uint8_t *)lsa) + ntohs(lsa->length);
1208
1209 while (p < lim) {
1210 l = (struct router_lsa_link *)p;
1211 p += (OSPF_ROUTER_LSA_LINK_SIZE
1212 + (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
1213
1214 /* We only care about P2P with the proper link id */
1215 if ((l->m[0].type != LSA_LINK_TYPE_POINTOPOINT)
1216 || (l->link_id.s_addr != router_id.s_addr))
1217 continue;
1218
1219 /* Link data in the subnet given by the link? */
1220 if ((link->link_id.s_addr & link->link_data.s_addr)
1221 == (l->link_data.s_addr & link->link_data.s_addr))
1222 return 1;
1223 }
1224
1225 break;
1226 case OSPF_TI_LFA_UNDEFINED_PROTECTION:
1227 break;
1228 }
1229
1230 return 0;
1231 }
1232
1233 /*
1234 * For TI-LFA we need the reverse SPF for Q spaces. The reverse SPF is created
1235 * by honoring the weight of the reverse 'edge', e.g. the edge from W to V, and
1236 * NOT the weight of the 'edge' from V to W as usual. Hence we need to find the
1237 * corresponding link in the LSA of W and extract the particular weight.
1238 *
1239 * TODO: Only P2P supported by now!
1240 */
1241 static uint16_t get_reverse_distance(struct vertex *v,
1242 struct router_lsa_link *l,
1243 struct ospf_lsa *w_lsa)
1244 {
1245 uint8_t *p, *lim;
1246 struct router_lsa_link *w_link;
1247 uint16_t distance = 0;
1248
1249 assert(w_lsa && w_lsa->data);
1250
1251 p = ((uint8_t *)w_lsa->data) + OSPF_LSA_HEADER_SIZE + 4;
1252 lim = ((uint8_t *)w_lsa->data) + ntohs(w_lsa->data->length);
1253
1254 while (p < lim) {
1255 w_link = (struct router_lsa_link *)p;
1256 p += (OSPF_ROUTER_LSA_LINK_SIZE
1257 + (w_link->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
1258
1259 /* Only care about P2P with link ID equal to V's router id */
1260 if (w_link->m[0].type == LSA_LINK_TYPE_POINTOPOINT
1261 && w_link->link_id.s_addr == v->id.s_addr) {
1262 distance = ntohs(w_link->m[0].metric);
1263 break;
1264 }
1265 }
1266
1267 /*
1268 * This might happen if the LSA for W is not complete yet. In this
1269 * case we take the weight of the 'forward' link from V. When the LSA
1270 * for W is completed the reverse SPF is run again anyway.
1271 */
1272 if (distance == 0)
1273 distance = ntohs(l->m[0].metric);
1274
1275 if (IS_DEBUG_OSPF_EVENT)
1276 zlog_debug("%s: reversed distance is %u", __func__, distance);
1277
1278 return distance;
1279 }
1280
1281 /*
1282 * RFC2328 16.1 (2).
1283 * v is on the SPF tree. Examine the links in v's LSA. Update the list of
1284 * candidates with any vertices not already on the list. If a lower-cost path
1285 * is found to a vertex already on the candidate list, store the new cost.
1286 */
1287 static void ospf_spf_next(struct vertex *v, struct ospf_area *area,
1288 struct vertex_pqueue_head *candidate)
1289 {
1290 struct ospf_lsa *w_lsa = NULL;
1291 uint8_t *p;
1292 uint8_t *lim;
1293 struct router_lsa_link *l = NULL;
1294 struct in_addr *r;
1295 int type = 0, lsa_pos = -1, lsa_pos_next = 0;
1296 uint16_t link_distance;
1297
1298 /*
1299 * If this is a router-LSA, and bit V of the router-LSA (see Section
1300 * A.4.2:RFC2328) is set, set Area A's TransitCapability to true.
1301 */
1302 if (v->type == OSPF_VERTEX_ROUTER) {
1303 if (IS_ROUTER_LSA_VIRTUAL((struct router_lsa *)v->lsa))
1304 area->transit = OSPF_TRANSIT_TRUE;
1305 }
1306
1307 if (IS_DEBUG_OSPF_EVENT)
1308 zlog_debug("%s: Next vertex of %s vertex %pI4", __func__,
1309 v->type == OSPF_VERTEX_ROUTER ? "Router" : "Network",
1310 &v->lsa->id);
1311
1312 p = ((uint8_t *)v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
1313 lim = ((uint8_t *)v->lsa) + ntohs(v->lsa->length);
1314
1315 while (p < lim) {
1316 struct vertex *w;
1317 unsigned int distance;
1318
1319 /* In case of V is Router-LSA. */
1320 if (v->lsa->type == OSPF_ROUTER_LSA) {
1321 l = (struct router_lsa_link *)p;
1322
1323 lsa_pos = lsa_pos_next; /* LSA link position */
1324 lsa_pos_next++;
1325
1326 p += (OSPF_ROUTER_LSA_LINK_SIZE
1327 + (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
1328
1329 /*
1330 * (a) If this is a link to a stub network, examine the
1331 * next link in V's LSA. Links to stub networks will
1332 * be considered in the second stage of the shortest
1333 * path calculation.
1334 */
1335 if ((type = l->m[0].type) == LSA_LINK_TYPE_STUB)
1336 continue;
1337
1338 /*
1339 * Don't process TI-LFA protected resources.
1340 *
1341 * TODO: Replace this by a proper solution, e.g. remove
1342 * corresponding links from the LSDB and run the SPF
1343 * algo with the stripped-down LSDB.
1344 */
1345 if (ospf_spf_is_protected_resource(area, l, v->lsa))
1346 continue;
1347
1348 /*
1349 * (b) Otherwise, W is a transit vertex (router or
1350 * transit network). Look up the vertex W's LSA
1351 * (router-LSA or network-LSA) in Area A's link state
1352 * database.
1353 */
1354 switch (type) {
1355 case LSA_LINK_TYPE_POINTOPOINT:
1356 case LSA_LINK_TYPE_VIRTUALLINK:
1357 if (type == LSA_LINK_TYPE_VIRTUALLINK
1358 && IS_DEBUG_OSPF_EVENT)
1359 zlog_debug(
1360 "looking up LSA through VL: %pI4",
1361 &l->link_id);
1362 w_lsa = ospf_lsa_lookup(area->ospf, area,
1363 OSPF_ROUTER_LSA,
1364 l->link_id, l->link_id);
1365 if (w_lsa && IS_DEBUG_OSPF_EVENT)
1366 zlog_debug("found Router LSA %pI4",
1367 &l->link_id);
1368 break;
1369 case LSA_LINK_TYPE_TRANSIT:
1370 if (IS_DEBUG_OSPF_EVENT)
1371 zlog_debug(
1372 "Looking up Network LSA, ID: %pI4",
1373 &l->link_id);
1374 w_lsa = ospf_lsa_lookup_by_id(
1375 area, OSPF_NETWORK_LSA, l->link_id);
1376 if (w_lsa && IS_DEBUG_OSPF_EVENT)
1377 zlog_debug("found the LSA");
1378 break;
1379 default:
1380 flog_warn(EC_OSPF_LSA,
1381 "Invalid LSA link type %d", type);
1382 continue;
1383 }
1384
1385 /*
1386 * For TI-LFA we might need the reverse SPF.
1387 * Currently only works with P2P!
1388 */
1389 if (type == LSA_LINK_TYPE_POINTOPOINT
1390 && area->spf_reversed)
1391 link_distance =
1392 get_reverse_distance(v, l, w_lsa);
1393 else
1394 link_distance = ntohs(l->m[0].metric);
1395
1396 /* step (d) below */
1397 distance = v->distance + link_distance;
1398 } else {
1399 /* In case of V is Network-LSA. */
1400 r = (struct in_addr *)p;
1401 p += sizeof(struct in_addr);
1402
1403 /* Lookup the vertex W's LSA. */
1404 w_lsa = ospf_lsa_lookup_by_id(area, OSPF_ROUTER_LSA,
1405 *r);
1406 if (w_lsa && IS_DEBUG_OSPF_EVENT)
1407 zlog_debug("found Router LSA %pI4",
1408 &w_lsa->data->id);
1409
1410 /* step (d) below */
1411 distance = v->distance;
1412 }
1413
1414 /*
1415 * (b cont.) If the LSA does not exist, or its LS age is equal
1416 * to MaxAge, or it does not have a link back to vertex V,
1417 * examine the next link in V's LSA.[23]
1418 */
1419 if (w_lsa == NULL) {
1420 if (IS_DEBUG_OSPF_EVENT)
1421 zlog_debug("No LSA found");
1422 continue;
1423 }
1424
1425 if (IS_LSA_MAXAGE(w_lsa)) {
1426 if (IS_DEBUG_OSPF_EVENT)
1427 zlog_debug("LSA is MaxAge");
1428 continue;
1429 }
1430
1431 if (ospf_lsa_has_link(w_lsa->data, v->lsa) < 0) {
1432 if (IS_DEBUG_OSPF_EVENT)
1433 zlog_debug("The LSA doesn't have a link back");
1434 continue;
1435 }
1436
1437 /*
1438 * (c) If vertex W is already on the shortest-path tree, examine
1439 * the next link in the LSA.
1440 */
1441 if (w_lsa->stat == LSA_SPF_IN_SPFTREE) {
1442 if (IS_DEBUG_OSPF_EVENT)
1443 zlog_debug("The LSA is already in SPF");
1444 continue;
1445 }
1446
1447 /*
1448 * (d) Calculate the link state cost D of the resulting path
1449 * from the root to vertex W. D is equal to the sum of the link
1450 * state cost of the (already calculated) shortest path to
1451 * vertex V and the advertised cost of the link between vertices
1452 * V and W. If D is:
1453 */
1454
1455 /* calculate link cost D -- moved above */
1456
1457 /* Is there already vertex W in candidate list? */
1458 if (w_lsa->stat == LSA_SPF_NOT_EXPLORED) {
1459 /* prepare vertex W. */
1460 w = ospf_vertex_new(area, w_lsa);
1461
1462 /* Calculate nexthop to W. */
1463 if (ospf_nexthop_calculation(area, v, w, l, distance,
1464 lsa_pos))
1465 vertex_pqueue_add(candidate, w);
1466 else if (IS_DEBUG_OSPF_EVENT)
1467 zlog_debug("Nexthop Calc failed");
1468 } else if (w_lsa->stat != LSA_SPF_IN_SPFTREE) {
1469 w = w_lsa->stat;
1470 if (w->distance < distance) {
1471 continue;
1472 }
1473 else if (w->distance == distance) {
1474 /*
1475 * Found an equal-cost path to W.
1476 * Calculate nexthop of to W from V.
1477 */
1478 ospf_nexthop_calculation(area, v, w, l,
1479 distance, lsa_pos);
1480 }
1481 else {
1482 /*
1483 * Found a lower-cost path to W.
1484 * nexthop_calculation is conditional, if it
1485 * finds valid nexthop it will call
1486 * spf_add_parents, which will flush the old
1487 * parents.
1488 */
1489 vertex_pqueue_del(candidate, w);
1490 ospf_nexthop_calculation(area, v, w, l,
1491 distance, lsa_pos);
1492 vertex_pqueue_add(candidate, w);
1493 }
1494 } /* end W is already on the candidate list */
1495 } /* end loop over the links in V's LSA */
1496 }
1497
1498 static void ospf_spf_dump(struct vertex *v, int i)
1499 {
1500 struct listnode *cnode;
1501 struct listnode *nnode;
1502 struct vertex_parent *parent;
1503
1504 if (v->type == OSPF_VERTEX_ROUTER) {
1505 if (IS_DEBUG_OSPF_EVENT)
1506 zlog_debug("SPF Result: %d [R] %pI4", i,
1507 &v->lsa->id);
1508 } else {
1509 struct network_lsa *lsa = (struct network_lsa *)v->lsa;
1510 if (IS_DEBUG_OSPF_EVENT)
1511 zlog_debug("SPF Result: %d [N] %pI4/%d", i,
1512 &v->lsa->id,
1513 ip_masklen(lsa->mask));
1514 }
1515
1516 if (IS_DEBUG_OSPF_EVENT)
1517 for (ALL_LIST_ELEMENTS_RO(v->parents, nnode, parent)) {
1518 zlog_debug(" nexthop %p %pI4 %d",
1519 (void *)parent->nexthop,
1520 &parent->nexthop->router,
1521 parent->nexthop->lsa_pos);
1522 }
1523
1524 i++;
1525
1526 for (ALL_LIST_ELEMENTS_RO(v->children, cnode, v))
1527 ospf_spf_dump(v, i);
1528 }
1529
1530 void ospf_spf_print(struct vty *vty, struct vertex *v, int i)
1531 {
1532 struct listnode *cnode;
1533 struct listnode *nnode;
1534 struct vertex_parent *parent;
1535
1536 if (v->type == OSPF_VERTEX_ROUTER) {
1537 vty_out(vty, "SPF Result: depth %d [R] %pI4\n", i, &v->lsa->id);
1538 } else {
1539 struct network_lsa *lsa = (struct network_lsa *)v->lsa;
1540 vty_out(vty, "SPF Result: depth %d [N] %pI4/%d\n", i,
1541 &v->lsa->id, ip_masklen(lsa->mask));
1542 }
1543
1544 for (ALL_LIST_ELEMENTS_RO(v->parents, nnode, parent)) {
1545 vty_out(vty,
1546 " nexthop %pI4 lsa pos %d -- local nexthop %pI4 lsa pos %d\n",
1547 &parent->nexthop->router, parent->nexthop->lsa_pos,
1548 &parent->local_nexthop->router,
1549 parent->local_nexthop->lsa_pos);
1550 }
1551
1552 i++;
1553
1554 for (ALL_LIST_ELEMENTS_RO(v->children, cnode, v))
1555 ospf_spf_print(vty, v, i);
1556 }
1557
1558 /* Second stage of SPF calculation. */
1559 static void ospf_spf_process_stubs(struct ospf_area *area, struct vertex *v,
1560 struct route_table *rt, int parent_is_root)
1561 {
1562 struct listnode *cnode, *cnnode;
1563 struct vertex *child;
1564
1565 if (IS_DEBUG_OSPF_EVENT)
1566 zlog_debug("ospf_process_stub():processing stubs for area %pI4",
1567 &area->area_id);
1568
1569 if (v->type == OSPF_VERTEX_ROUTER) {
1570 uint8_t *p;
1571 uint8_t *lim;
1572 struct router_lsa_link *l;
1573 struct router_lsa *router_lsa;
1574 int lsa_pos = 0;
1575
1576 if (IS_DEBUG_OSPF_EVENT)
1577 zlog_debug(
1578 "ospf_process_stubs():processing router LSA, id: %pI4",
1579 &v->lsa->id);
1580
1581 router_lsa = (struct router_lsa *)v->lsa;
1582
1583 if (IS_DEBUG_OSPF_EVENT)
1584 zlog_debug(
1585 "ospf_process_stubs(): we have %d links to process",
1586 ntohs(router_lsa->links));
1587
1588 p = ((uint8_t *)v->lsa) + OSPF_LSA_HEADER_SIZE + 4;
1589 lim = ((uint8_t *)v->lsa) + ntohs(v->lsa->length);
1590
1591 while (p < lim) {
1592 l = (struct router_lsa_link *)p;
1593
1594 p += (OSPF_ROUTER_LSA_LINK_SIZE
1595 + (l->m[0].tos_count * OSPF_ROUTER_LSA_TOS_SIZE));
1596
1597 /* Don't process TI-LFA protected resources */
1598 if (l->m[0].type == LSA_LINK_TYPE_STUB
1599 && !ospf_spf_is_protected_resource(area, l, v->lsa))
1600 ospf_intra_add_stub(rt, l, v, area,
1601 parent_is_root, lsa_pos);
1602 lsa_pos++;
1603 }
1604 }
1605
1606 ospf_vertex_dump("ospf_process_stubs(): after examining links: ", v, 1,
1607 1);
1608
1609 for (ALL_LIST_ELEMENTS(v->children, cnode, cnnode, child)) {
1610 if (CHECK_FLAG(child->flags, OSPF_VERTEX_PROCESSED))
1611 continue;
1612
1613 /*
1614 * The first level of routers connected to the root
1615 * should have 'parent_is_root' set, including those
1616 * connected via a network vertex.
1617 */
1618 if (area->spf == v)
1619 parent_is_root = 1;
1620 else if (v->type == OSPF_VERTEX_ROUTER)
1621 parent_is_root = 0;
1622
1623 ospf_spf_process_stubs(area, child, rt, parent_is_root);
1624
1625 SET_FLAG(child->flags, OSPF_VERTEX_PROCESSED);
1626 }
1627 }
1628
1629 void ospf_rtrs_free(struct route_table *rtrs)
1630 {
1631 struct route_node *rn;
1632 struct list *or_list;
1633 struct ospf_route * or ;
1634 struct listnode *node, *nnode;
1635
1636 if (IS_DEBUG_OSPF_EVENT)
1637 zlog_debug("Route: Router Routing Table free");
1638
1639 for (rn = route_top(rtrs); rn; rn = route_next(rn))
1640 if ((or_list = rn->info) != NULL) {
1641 for (ALL_LIST_ELEMENTS(or_list, node, nnode, or))
1642 ospf_route_free(or);
1643
1644 list_delete(&or_list);
1645
1646 /* Unlock the node. */
1647 rn->info = NULL;
1648 route_unlock_node(rn);
1649 }
1650
1651 route_table_finish(rtrs);
1652 }
1653
1654 void ospf_spf_cleanup(struct vertex *spf, struct list *vertex_list)
1655 {
1656 /*
1657 * Free nexthop information, canonical versions of which are
1658 * attached the first level of router vertices attached to the
1659 * root vertex, see ospf_nexthop_calculation.
1660 */
1661 if (spf)
1662 ospf_canonical_nexthops_free(spf);
1663
1664 /* Free SPF vertices list with deconstructor ospf_vertex_free. */
1665 if (vertex_list)
1666 list_delete(&vertex_list);
1667 }
1668
1669 /* Calculating the shortest-path tree for an area, see RFC2328 16.1. */
1670 void ospf_spf_calculate(struct ospf_area *area, struct ospf_lsa *root_lsa,
1671 struct route_table *new_table,
1672 struct route_table *new_rtrs, bool is_dry_run,
1673 bool is_root_node)
1674 {
1675 struct vertex_pqueue_head candidate;
1676 struct vertex *v;
1677
1678 if (IS_DEBUG_OSPF_EVENT) {
1679 zlog_debug("ospf_spf_calculate: Start");
1680 zlog_debug("ospf_spf_calculate: running Dijkstra for area %pI4",
1681 &area->area_id);
1682 }
1683
1684 /*
1685 * If the router LSA of the root is not yet allocated, return this
1686 * area's calculation. In the 'usual' case the root_lsa is the
1687 * self-originated router LSA of the node itself.
1688 */
1689 if (!root_lsa) {
1690 if (IS_DEBUG_OSPF_EVENT)
1691 zlog_debug(
1692 "ospf_spf_calculate: Skip area %pI4's calculation due to empty root LSA",
1693 &area->area_id);
1694 return;
1695 }
1696
1697 /* Initialize the algorithm's data structures, see RFC2328 16.1. (1). */
1698
1699 /*
1700 * This function scans all the LSA database and set the stat field to
1701 * LSA_SPF_NOT_EXPLORED.
1702 */
1703 lsdb_clean_stat(area->lsdb);
1704
1705 /* Create a new heap for the candidates. */
1706 vertex_pqueue_init(&candidate);
1707
1708 /*
1709 * Initialize the shortest-path tree to only the root (which is usually
1710 * the router doing the calculation).
1711 */
1712 ospf_spf_init(area, root_lsa, is_dry_run, is_root_node);
1713
1714 /* Set Area A's TransitCapability to false. */
1715 area->transit = OSPF_TRANSIT_FALSE;
1716 area->shortcut_capability = 1;
1717
1718 /*
1719 * Use the root vertex for the start of the SPF algorithm and make it
1720 * part of the tree.
1721 */
1722 v = area->spf;
1723 v->lsa_p->stat = LSA_SPF_IN_SPFTREE;
1724
1725 for (;;) {
1726 /* RFC2328 16.1. (2). */
1727 ospf_spf_next(v, area, &candidate);
1728
1729 /* RFC2328 16.1. (3). */
1730 v = vertex_pqueue_pop(&candidate);
1731 if (!v)
1732 /* No more vertices left. */
1733 break;
1734
1735 v->lsa_p->stat = LSA_SPF_IN_SPFTREE;
1736
1737 ospf_vertex_add_parent(v);
1738
1739 /* RFC2328 16.1. (4). */
1740 if (v->type == OSPF_VERTEX_ROUTER)
1741 ospf_intra_add_router(new_rtrs, v, area);
1742 else
1743 ospf_intra_add_transit(new_table, v, area);
1744
1745 /* Iterate back to (2), see RFC2328 16.1. (5). */
1746 }
1747
1748 if (IS_DEBUG_OSPF_EVENT) {
1749 ospf_spf_dump(area->spf, 0);
1750 ospf_route_table_dump(new_table);
1751 }
1752
1753 /*
1754 * Second stage of SPF calculation procedure's, add leaves to the tree
1755 * for stub networks.
1756 */
1757 ospf_spf_process_stubs(area, area->spf, new_table, 0);
1758
1759 ospf_vertex_dump(__func__, area->spf, 0, 1);
1760
1761 /* Increment SPF Calculation Counter. */
1762 area->spf_calculation++;
1763
1764 monotime(&area->ospf->ts_spf);
1765 area->ts_spf = area->ospf->ts_spf;
1766
1767 if (IS_DEBUG_OSPF_EVENT)
1768 zlog_debug("ospf_spf_calculate: Stop. %zd vertices",
1769 mtype_stats_alloc(MTYPE_OSPF_VERTEX));
1770 }
1771
1772 void ospf_spf_calculate_area(struct ospf *ospf, struct ospf_area *area,
1773 struct route_table *new_table,
1774 struct route_table *new_rtrs)
1775 {
1776 ospf_spf_calculate(area, area->router_lsa_self, new_table, new_rtrs,
1777 false, true);
1778
1779 if (ospf->ti_lfa_enabled)
1780 ospf_ti_lfa_compute(area, new_table,
1781 ospf->ti_lfa_protection_type);
1782
1783 ospf_spf_cleanup(area->spf, area->spf_vertex_list);
1784
1785 area->spf = NULL;
1786 area->spf_vertex_list = NULL;
1787 }
1788
1789 void ospf_spf_calculate_areas(struct ospf *ospf, struct route_table *new_table,
1790 struct route_table *new_rtrs)
1791 {
1792 struct ospf_area *area;
1793 struct listnode *node, *nnode;
1794
1795 /* Calculate SPF for each area. */
1796 for (ALL_LIST_ELEMENTS(ospf->areas, node, nnode, area)) {
1797 /* Do backbone last, so as to first discover intra-area paths
1798 * for any back-bone virtual-links */
1799 if (ospf->backbone && ospf->backbone == area)
1800 continue;
1801
1802 ospf_spf_calculate_area(ospf, area, new_table, new_rtrs);
1803 }
1804
1805 /* SPF for backbone, if required */
1806 if (ospf->backbone)
1807 ospf_spf_calculate_area(ospf, ospf->backbone, new_table,
1808 new_rtrs);
1809 }
1810
1811 /* Worker for SPF calculation scheduler. */
1812 static void ospf_spf_calculate_schedule_worker(struct thread *thread)
1813 {
1814 struct ospf *ospf = THREAD_ARG(thread);
1815 struct route_table *new_table, *new_rtrs;
1816 struct timeval start_time, spf_start_time;
1817 unsigned long ia_time, prune_time, rt_time;
1818 unsigned long abr_time, total_spf_time, spf_time;
1819 char rbuf[32]; /* reason_buf */
1820
1821 if (IS_DEBUG_OSPF_EVENT)
1822 zlog_debug("SPF: Timer (SPF calculation expire)");
1823
1824 ospf->t_spf_calc = NULL;
1825
1826 ospf_vl_unapprove(ospf);
1827
1828 /* Execute SPF for each area including backbone, see RFC 2328 16.1. */
1829 monotime(&spf_start_time);
1830 new_table = route_table_init(); /* routing table */
1831 new_rtrs = route_table_init(); /* ABR/ASBR routing table */
1832 ospf_spf_calculate_areas(ospf, new_table, new_rtrs);
1833 spf_time = monotime_since(&spf_start_time, NULL);
1834
1835 ospf_vl_shut_unapproved(ospf);
1836
1837 /* Calculate inter-area routes, see RFC 2328 16.2. */
1838 monotime(&start_time);
1839 ospf_ia_routing(ospf, new_table, new_rtrs);
1840 ia_time = monotime_since(&start_time, NULL);
1841
1842 /* Get rid of transit networks and routers we cannot reach anyway. */
1843 monotime(&start_time);
1844 ospf_prune_unreachable_networks(new_table);
1845 ospf_prune_unreachable_routers(new_rtrs);
1846 prune_time = monotime_since(&start_time, NULL);
1847
1848 /* Note: RFC 2328 16.3. is apparently missing. */
1849
1850 /*
1851 * Calculate AS external routes, see RFC 2328 16.4.
1852 * There is a dedicated routing table for external routes which is not
1853 * handled here directly
1854 */
1855 ospf_ase_calculate_schedule(ospf);
1856 ospf_ase_calculate_timer_add(ospf);
1857
1858 if (IS_DEBUG_OSPF_EVENT)
1859 zlog_debug(
1860 "%s: ospf install new route, vrf %s id %u new_table count %lu",
1861 __func__, ospf_vrf_id_to_name(ospf->vrf_id),
1862 ospf->vrf_id, new_table->count);
1863
1864 /* Update routing table. */
1865 monotime(&start_time);
1866 ospf_route_install(ospf, new_table);
1867 rt_time = monotime_since(&start_time, NULL);
1868
1869 /* Free old ABR/ASBR routing table */
1870 if (ospf->old_rtrs)
1871 /* ospf_route_delete (ospf->old_rtrs); */
1872 ospf_rtrs_free(ospf->old_rtrs);
1873
1874 /* Update ABR/ASBR routing table */
1875 ospf->old_rtrs = ospf->new_rtrs;
1876 ospf->new_rtrs = new_rtrs;
1877
1878 /* ABRs may require additional changes, see RFC 2328 16.7. */
1879 monotime(&start_time);
1880 if (IS_OSPF_ABR(ospf)) {
1881 if (ospf->anyNSSA)
1882 ospf_abr_nssa_check_status(ospf);
1883 ospf_abr_task(ospf);
1884 }
1885 abr_time = monotime_since(&start_time, NULL);
1886
1887 /* Schedule Segment Routing update */
1888 ospf_sr_update_task(ospf);
1889
1890 total_spf_time =
1891 monotime_since(&spf_start_time, &ospf->ts_spf_duration);
1892
1893 rbuf[0] = '\0';
1894 if (spf_reason_flags) {
1895 if (spf_reason_flags & (1 << SPF_FLAG_ROUTER_LSA_INSTALL))
1896 strlcat(rbuf, "R, ", sizeof(rbuf));
1897 if (spf_reason_flags & (1 << SPF_FLAG_NETWORK_LSA_INSTALL))
1898 strlcat(rbuf, "N, ", sizeof(rbuf));
1899 if (spf_reason_flags & (1 << SPF_FLAG_SUMMARY_LSA_INSTALL))
1900 strlcat(rbuf, "S, ", sizeof(rbuf));
1901 if (spf_reason_flags & (1 << SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL))
1902 strlcat(rbuf, "AS, ", sizeof(rbuf));
1903 if (spf_reason_flags & (1 << SPF_FLAG_ABR_STATUS_CHANGE))
1904 strlcat(rbuf, "ABR, ", sizeof(rbuf));
1905 if (spf_reason_flags & (1 << SPF_FLAG_ASBR_STATUS_CHANGE))
1906 strlcat(rbuf, "ASBR, ", sizeof(rbuf));
1907 if (spf_reason_flags & (1 << SPF_FLAG_MAXAGE))
1908 strlcat(rbuf, "M, ", sizeof(rbuf));
1909 if (spf_reason_flags & (1 << SPF_FLAG_GR_FINISH))
1910 strlcat(rbuf, "GR, ", sizeof(rbuf));
1911
1912 size_t rbuflen = strlen(rbuf);
1913 if (rbuflen >= 2)
1914 rbuf[rbuflen - 2] = '\0'; /* skip the last ", " */
1915 else
1916 rbuf[0] = '\0';
1917 }
1918
1919 if (IS_DEBUG_OSPF_EVENT) {
1920 zlog_info("SPF Processing Time(usecs): %ld", total_spf_time);
1921 zlog_info(" SPF Time: %ld", spf_time);
1922 zlog_info(" InterArea: %ld", ia_time);
1923 zlog_info(" Prune: %ld", prune_time);
1924 zlog_info(" RouteInstall: %ld", rt_time);
1925 if (IS_OSPF_ABR(ospf))
1926 zlog_info(" ABR: %ld (%d areas)",
1927 abr_time, ospf->areas->count);
1928 zlog_info("Reason(s) for SPF: %s", rbuf);
1929 }
1930
1931 ospf_clear_spf_reason_flags();
1932 }
1933
1934 /*
1935 * Add schedule for SPF calculation. To avoid frequenst SPF calc, we set timer
1936 * for SPF calc.
1937 */
1938 void ospf_spf_calculate_schedule(struct ospf *ospf, ospf_spf_reason_t reason)
1939 {
1940 unsigned long delay, elapsed, ht;
1941
1942 if (IS_DEBUG_OSPF_EVENT)
1943 zlog_debug("SPF: calculation timer scheduled");
1944
1945 /* OSPF instance does not exist. */
1946 if (ospf == NULL)
1947 return;
1948
1949 ospf_spf_set_reason(reason);
1950
1951 /* SPF calculation timer is already scheduled. */
1952 if (ospf->t_spf_calc) {
1953 if (IS_DEBUG_OSPF_EVENT)
1954 zlog_debug(
1955 "SPF: calculation timer is already scheduled: %p",
1956 (void *)ospf->t_spf_calc);
1957 return;
1958 }
1959
1960 elapsed = monotime_since(&ospf->ts_spf, NULL) / 1000;
1961
1962 ht = ospf->spf_holdtime * ospf->spf_hold_multiplier;
1963
1964 if (ht > ospf->spf_max_holdtime)
1965 ht = ospf->spf_max_holdtime;
1966
1967 /* Get SPF calculation delay time. */
1968 if (elapsed < ht) {
1969 /*
1970 * Got an event within the hold time of last SPF. We need to
1971 * increase the hold_multiplier, if it's not already at/past
1972 * maximum value, and wasn't already increased.
1973 */
1974 if (ht < ospf->spf_max_holdtime)
1975 ospf->spf_hold_multiplier++;
1976
1977 /* always honour the SPF initial delay */
1978 if ((ht - elapsed) < ospf->spf_delay)
1979 delay = ospf->spf_delay;
1980 else
1981 delay = ht - elapsed;
1982 } else {
1983 /* Event is past required hold-time of last SPF */
1984 delay = ospf->spf_delay;
1985 ospf->spf_hold_multiplier = 1;
1986 }
1987
1988 if (IS_DEBUG_OSPF_EVENT)
1989 zlog_debug("SPF: calculation timer delay = %ld msec", delay);
1990
1991 ospf->t_spf_calc = NULL;
1992 thread_add_timer_msec(master, ospf_spf_calculate_schedule_worker, ospf,
1993 delay, &ospf->t_spf_calc);
1994 }
1995
1996 /* Restart OSPF SPF algorithm*/
1997 void ospf_restart_spf(struct ospf *ospf)
1998 {
1999 if (IS_DEBUG_OSPF_EVENT)
2000 zlog_debug("%s: Restart SPF.", __func__);
2001
2002 /* Handling inter area and intra area routes*/
2003 if (ospf->new_table) {
2004 ospf_route_delete(ospf, ospf->new_table);
2005 ospf_route_table_free(ospf->new_table);
2006 ospf->new_table = route_table_init();
2007 }
2008
2009 /* Handling of TYPE-5 lsa(external routes) */
2010 if (ospf->old_external_route) {
2011 ospf_route_delete(ospf, ospf->old_external_route);
2012 ospf_route_table_free(ospf->old_external_route);
2013 ospf->old_external_route = route_table_init();
2014 }
2015
2016 /* Trigger SPF */
2017 ospf_spf_calculate_schedule(ospf, SPF_FLAG_CONFIG_CHANGE);
2018 }