ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / ospfd / ospf_ase.c
blob7bddf3f4db2a17d549463ca4f1855ce280dd9182
1 /*
2 * OSPF AS external route calculation.
3 * Copyright (C) 1999, 2000 Alex Zinin, Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
23 #include <zebra.h>
25 #include "thread.h"
26 #include "memory.h"
27 #include "hash.h"
28 #include "linklist.h"
29 #include "prefix.h"
30 #include "if.h"
31 #include "table.h"
32 #include "vty.h"
33 #include "log.h"
35 #include "ospfd/ospfd.h"
36 #include "ospfd/ospf_interface.h"
37 #include "ospfd/ospf_ism.h"
38 #include "ospfd/ospf_asbr.h"
39 #include "ospfd/ospf_lsa.h"
40 #include "ospfd/ospf_lsdb.h"
41 #include "ospfd/ospf_neighbor.h"
42 #include "ospfd/ospf_nsm.h"
43 #include "ospfd/ospf_spf.h"
44 #include "ospfd/ospf_route.h"
45 #include "ospfd/ospf_ase.h"
46 #include "ospfd/ospf_zebra.h"
47 #include "ospfd/ospf_dump.h"
49 struct ospf_route *
50 ospf_find_asbr_route (struct ospf *ospf,
51 struct route_table *rtrs, struct prefix_ipv4 *asbr)
53 struct route_node *rn;
54 struct ospf_route *or, *best = NULL;
55 struct listnode *node;
56 struct list *chosen;
58 /* Sanity check. */
59 if (rtrs == NULL)
60 return NULL;
62 rn = route_node_lookup (rtrs, (struct prefix *) asbr);
63 if (! rn)
64 return NULL;
66 route_unlock_node (rn);
68 chosen = list_new ();
70 /* First try to find intra-area non-bb paths. */
71 if (!CHECK_FLAG (ospf->config, OSPF_RFC1583_COMPATIBLE))
72 for (ALL_LIST_ELEMENTS_RO ((struct list *) rn->info, node, or))
73 if (or->cost < OSPF_LS_INFINITY)
74 if (!OSPF_IS_AREA_ID_BACKBONE (or->u.std.area_id) &&
75 or->path_type == OSPF_PATH_INTRA_AREA)
76 listnode_add (chosen, or);
78 /* If none is found -- look through all. */
79 if (listcount (chosen) == 0)
81 list_free (chosen);
82 chosen = rn->info;
85 /* Now find the route with least cost. */
86 for (ALL_LIST_ELEMENTS_RO (chosen, node, or))
87 if (or->cost < OSPF_LS_INFINITY)
89 if (best == NULL)
90 best = or;
91 else if (best->cost > or->cost)
92 best = or;
93 else if (best->cost == or->cost &&
94 IPV4_ADDR_CMP (&best->u.std.area_id,
95 &or->u.std.area_id) < 0)
96 best = or;
99 if (chosen != rn->info)
100 list_delete (chosen);
102 return best;
105 struct ospf_route *
106 ospf_find_asbr_route_through_area (struct route_table *rtrs,
107 struct prefix_ipv4 *asbr,
108 struct ospf_area *area)
110 struct route_node *rn;
112 /* Sanity check. */
113 if (rtrs == NULL)
114 return NULL;
116 rn = route_node_lookup (rtrs, (struct prefix *) asbr);
118 if (rn)
120 struct listnode *node;
121 struct ospf_route *or;
123 route_unlock_node (rn);
125 for (ALL_LIST_ELEMENTS_RO ((struct list *) rn->info, node, or))
126 if (IPV4_ADDR_SAME (&or->u.std.area_id, &area->area_id))
127 return or;
130 return NULL;
133 static void
134 ospf_ase_complete_direct_routes (struct ospf_route *ro, struct in_addr nexthop)
136 struct listnode *node;
137 struct ospf_path *op;
138 struct interface *ifp;
140 for (ALL_LIST_ELEMENTS_RO (ro->paths, node, op))
141 if (op->nexthop.s_addr == 0)
142 op->nexthop.s_addr = nexthop.s_addr;
145 static int
146 ospf_ase_forward_address_check (struct ospf *ospf, struct in_addr fwd_addr)
148 struct listnode *ifn;
149 struct ospf_interface *oi;
151 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, ifn, oi))
152 if (if_is_operative (oi->ifp))
153 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
154 if (IPV4_ADDR_SAME (&oi->address->u.prefix4, &fwd_addr))
155 return 0;
157 return 1;
160 /* Calculate ASBR route. */
161 static struct ospf_route *
162 ospf_ase_calculate_asbr_route (struct ospf *ospf,
163 struct route_table *rt_network,
164 struct route_table *rt_router,
165 struct as_external_lsa *al)
167 struct prefix_ipv4 asbr;
168 struct ospf_route *asbr_route;
169 struct route_node *rn;
171 /* Find ASBR route from Router routing table. */
172 asbr.family = AF_INET;
173 asbr.prefix = al->header.adv_router;
174 asbr.prefixlen = IPV4_MAX_BITLEN;
175 apply_mask_ipv4 (&asbr);
177 asbr_route = ospf_find_asbr_route (ospf, rt_router, &asbr);
179 if (asbr_route == NULL)
181 if (IS_DEBUG_OSPF (lsa, LSA))
182 zlog_debug ("ospf_ase_calculate(): Route to ASBR %s not found",
183 inet_ntoa (asbr.prefix));
184 return NULL;
187 if (!(asbr_route->u.std.flags & ROUTER_LSA_EXTERNAL))
189 if (IS_DEBUG_OSPF (lsa, LSA))
190 zlog_debug ("ospf_ase_calculate(): Originating router is not an ASBR");
191 return NULL;
194 if (al->e[0].fwd_addr.s_addr != 0)
196 if (IS_DEBUG_OSPF (lsa, LSA))
197 zlog_debug ("ospf_ase_calculate(): "
198 "Forwarding address is not 0.0.0.0.");
200 if (! ospf_ase_forward_address_check (ospf, al->e[0].fwd_addr))
202 if (IS_DEBUG_OSPF (lsa, LSA))
203 zlog_debug ("ospf_ase_calculate(): "
204 "Forwarding address is one of our addresses, Ignore.");
205 return NULL;
208 if (IS_DEBUG_OSPF (lsa, LSA))
209 zlog_debug ("ospf_ase_calculate(): "
210 "Looking up in the Network Routing Table.");
212 /* Looking up the path to the fwd_addr from Network route. */
213 asbr.family = AF_INET;
214 asbr.prefix = al->e[0].fwd_addr;
215 asbr.prefixlen = IPV4_MAX_BITLEN;
217 rn = route_node_match (rt_network, (struct prefix *) &asbr);
219 if (rn == NULL)
221 if (IS_DEBUG_OSPF (lsa, LSA))
222 zlog_debug ("ospf_ase_calculate(): "
223 "Couldn't find a route to the forwarding address.");
224 return NULL;
227 route_unlock_node (rn);
229 if ((asbr_route = rn->info) == NULL)
231 if (IS_DEBUG_OSPF (lsa, LSA))
232 zlog_debug ("ospf_ase_calculate(): "
233 "Somehow OSPF route to ASBR is lost");
234 return NULL;
238 return asbr_route;
241 static struct ospf_route *
242 ospf_ase_calculate_new_route (struct ospf_lsa *lsa,
243 struct ospf_route *asbr_route, u_int32_t metric)
245 struct as_external_lsa *al;
246 struct ospf_route *new;
248 al = (struct as_external_lsa *) lsa->data;
250 new = ospf_route_new ();
252 /* Set redistributed type -- does make sense? */
253 /* new->type = type; */
254 new->id = al->header.id;
255 new->mask = al->mask;
257 if (!IS_EXTERNAL_METRIC (al->e[0].tos))
259 if (IS_DEBUG_OSPF (lsa, LSA))
260 zlog_debug ("Route[External]: type-1 created.");
261 new->path_type = OSPF_PATH_TYPE1_EXTERNAL;
262 new->cost = asbr_route->cost + metric; /* X + Y */
264 else
266 if (IS_DEBUG_OSPF (lsa, LSA))
267 zlog_debug ("Route[External]: type-2 created.");
268 new->path_type = OSPF_PATH_TYPE2_EXTERNAL;
269 new->cost = asbr_route->cost; /* X */
270 new->u.ext.type2_cost = metric; /* Y */
273 new->type = OSPF_DESTINATION_NETWORK;
274 new->u.ext.origin = lsa;
275 new->u.ext.tag = ntohl (al->e[0].route_tag);
276 new->u.ext.asbr = asbr_route;
278 assert (new != asbr_route);
280 return new;
283 #define OSPF_ASE_CALC_INTERVAL 1
286 ospf_ase_calculate_route (struct ospf *ospf, struct ospf_lsa * lsa)
288 u_int32_t metric;
289 struct as_external_lsa *al;
290 struct ospf_route *asbr_route;
291 struct prefix_ipv4 asbr, p;
292 struct route_node *rn;
293 struct ospf_route *new, *or;
294 int ret;
296 assert (lsa);
297 al = (struct as_external_lsa *) lsa->data;
299 if (lsa->data->type == OSPF_AS_NSSA_LSA)
300 if (IS_DEBUG_OSPF_NSSA)
301 zlog_debug ("ospf_ase_calc(): Processing Type-7");
303 /* Stay away from any Local Translated Type-7 LSAs */
304 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
306 if (IS_DEBUG_OSPF_NSSA)
307 zlog_debug ("ospf_ase_calc(): Rejecting Local Xlt'd");
308 return 0;
311 if (IS_DEBUG_OSPF (lsa, LSA))
312 zlog_debug ("Route[External]: Calculate AS-external-LSA to %s/%d",
313 inet_ntoa (al->header.id), ip_masklen (al->mask));
314 /* (1) If the cost specified by the LSA is LSInfinity, or if the
315 LSA's LS age is equal to MaxAge, then examine the next LSA. */
316 if ((metric = GET_METRIC (al->e[0].metric)) >= OSPF_LS_INFINITY)
318 if (IS_DEBUG_OSPF (lsa, LSA))
319 zlog_debug ("Route[External]: Metric is OSPF_LS_INFINITY");
320 return 0;
322 if (IS_LSA_MAXAGE (lsa))
324 if (IS_DEBUG_OSPF (lsa, LSA))
325 zlog_debug ("Route[External]: AS-external-LSA is MAXAGE");
326 return 0;
329 /* (2) If the LSA was originated by the calculating router itself,
330 examine the next LSA. */
331 if (IS_LSA_SELF (lsa))
333 if (IS_DEBUG_OSPF (lsa, LSA))
334 zlog_debug ("Route[External]: AS-external-LSA is self originated");
335 return 0;
338 /* (3) Call the destination described by the LSA N. N's address is
339 obtained by masking the LSA's Link State ID with the
340 network/subnet mask contained in the body of the LSA. Look
341 up the routing table entries (potentially one per attached
342 area) for the AS boundary router (ASBR) that originated the
343 LSA. If no entries exist for router ASBR (i.e., ASBR is
344 unreachable), do nothing with this LSA and consider the next
345 in the list. */
347 asbr.family = AF_INET;
348 asbr.prefix = al->header.adv_router;
349 asbr.prefixlen = IPV4_MAX_BITLEN;
350 apply_mask_ipv4 (&asbr);
352 asbr_route = ospf_find_asbr_route (ospf, ospf->new_rtrs, &asbr);
353 if (asbr_route == NULL)
355 if (IS_DEBUG_OSPF (lsa, LSA))
356 zlog_debug ("Route[External]: Can't find originating ASBR route");
357 return 0;
359 if (!(asbr_route->u.std.flags & ROUTER_LSA_EXTERNAL))
361 if (IS_DEBUG_OSPF (lsa, LSA))
362 zlog_debug ("Route[External]: Originating router is not an ASBR");
363 return 0;
366 /* Else, this LSA describes an AS external path to destination
367 N. Examine the forwarding address specified in the AS-
368 external-LSA. This indicates the IP address to which
369 packets for the destination should be forwarded. */
371 if (al->e[0].fwd_addr.s_addr == 0)
373 /* If the forwarding address is set to 0.0.0.0, packets should
374 be sent to the ASBR itself. Among the multiple routing table
375 entries for the ASBR, select the preferred entry as follows.
376 If RFC1583Compatibility is set to "disabled", prune the set
377 of routing table entries for the ASBR as described in
378 Section 16.4.1. In any case, among the remaining routing
379 table entries, select the routing table entry with the least
380 cost; when there are multiple least cost routing table
381 entries the entry whose associated area has the largest OSPF
382 Area ID (when considered as an unsigned 32-bit integer) is
383 chosen. */
385 /* asbr_route already contains the requested route */
387 else
389 /* If the forwarding address is non-zero, look up the
390 forwarding address in the routing table.[24] The matching
391 routing table entry must specify an intra-area or inter-area
392 path; if no such path exists, do nothing with the LSA and
393 consider the next in the list. */
394 if (! ospf_ase_forward_address_check (ospf, al->e[0].fwd_addr))
396 if (IS_DEBUG_OSPF (lsa, LSA))
397 zlog_debug ("Route[External]: Forwarding address is our router "
398 "address");
399 return 0;
402 asbr.family = AF_INET;
403 asbr.prefix = al->e[0].fwd_addr;
404 asbr.prefixlen = IPV4_MAX_BITLEN;
406 rn = route_node_match (ospf->new_table, (struct prefix *) &asbr);
408 if (rn == NULL || (asbr_route = rn->info) == NULL)
410 if (IS_DEBUG_OSPF (lsa, LSA))
411 zlog_debug ("Route[External]: Can't find route to forwarding "
412 "address");
413 if (rn)
414 route_unlock_node (rn);
415 return 0;
418 route_unlock_node (rn);
421 /* (4) Let X be the cost specified by the preferred routing table
422 entry for the ASBR/forwarding address, and Y the cost
423 specified in the LSA. X is in terms of the link state
424 metric, and Y is a type 1 or 2 external metric. */
427 /* (5) Look up the routing table entry for the destination N. If
428 no entry exists for N, install the AS external path to N,
429 with next hop equal to the list of next hops to the
430 forwarding address, and advertising router equal to ASBR.
431 If the external metric type is 1, then the path-type is set
432 to type 1 external and the cost is equal to X+Y. If the
433 external metric type is 2, the path-type is set to type 2
434 external, the link state component of the route's cost is X,
435 and the type 2 cost is Y. */
436 new = ospf_ase_calculate_new_route (lsa, asbr_route, metric);
438 /* (6) Compare the AS external path described by the LSA with the
439 existing paths in N's routing table entry, as follows. If
440 the new path is preferred, it replaces the present paths in
441 N's routing table entry. If the new path is of equal
442 preference, it is added to N's routing table entry's list of
443 paths. */
445 /* Set prefix. */
446 p.family = AF_INET;
447 p.prefix = al->header.id;
448 p.prefixlen = ip_masklen (al->mask);
450 /* if there is a Intra/Inter area route to the N
451 do not install external route */
452 if (rn = route_node_lookup (ospf->new_table,
453 (struct prefix *) &p))
455 route_unlock_node(rn);
456 if (rn->info == NULL)
457 zlog_info ("Route[External]: rn->info NULL");
458 if (new)
459 ospf_route_free (new);
460 return 0;
462 /* Find a route to the same dest */
463 /* If there is no route, create new one. */
464 if (rn = route_node_lookup (ospf->new_external_route,
465 (struct prefix *) &p))
466 route_unlock_node(rn);
468 if (!rn || (or = rn->info) == NULL)
470 if (IS_DEBUG_OSPF (lsa, LSA))
471 zlog_debug ("Route[External]: Adding a new route %s/%d",
472 inet_ntoa (p.prefix), p.prefixlen);
474 ospf_route_add (ospf->new_external_route, &p, new, asbr_route);
476 if (al->e[0].fwd_addr.s_addr)
477 ospf_ase_complete_direct_routes (new, al->e[0].fwd_addr);
478 return 0;
480 else
482 /* (a) Intra-area and inter-area paths are always preferred
483 over AS external paths.
485 (b) Type 1 external paths are always preferred over type 2
486 external paths. When all paths are type 2 external
487 paths, the paths with the smallest advertised type 2
488 metric are always preferred. */
489 ret = ospf_route_cmp (ospf, new, or);
491 /* (c) If the new AS external path is still indistinguishable
492 from the current paths in the N's routing table entry,
493 and RFC1583Compatibility is set to "disabled", select
494 the preferred paths based on the intra-AS paths to the
495 ASBR/forwarding addresses, as specified in Section
496 16.4.1.
498 (d) If the new AS external path is still indistinguishable
499 from the current paths in the N's routing table entry,
500 select the preferred path based on a least cost
501 comparison. Type 1 external paths are compared by
502 looking at the sum of the distance to the forwarding
503 address and the advertised type 1 metric (X+Y). Type 2
504 external paths advertising equal type 2 metrics are
505 compared by looking at the distance to the forwarding
506 addresses.
508 /* New route is better */
509 if (ret < 0)
511 if (IS_DEBUG_OSPF (lsa, LSA))
512 zlog_debug ("Route[External]: New route is better");
513 ospf_route_subst (rn, new, asbr_route);
514 if (al->e[0].fwd_addr.s_addr)
515 ospf_ase_complete_direct_routes (new, al->e[0].fwd_addr);
516 or = new;
517 new = NULL;
519 /* Old route is better */
520 else if (ret > 0)
522 if (IS_DEBUG_OSPF (lsa, LSA))
523 zlog_debug ("Route[External]: Old route is better");
524 /* do nothing */
526 /* Routes are equal */
527 else
529 if (IS_DEBUG_OSPF (lsa, LSA))
530 zlog_debug ("Route[External]: Routes are equal");
531 ospf_route_copy_nexthops (or, asbr_route->paths);
532 if (al->e[0].fwd_addr.s_addr)
533 ospf_ase_complete_direct_routes (or, al->e[0].fwd_addr);
536 /* Make sure setting newly calculated ASBR route.*/
537 or->u.ext.asbr = asbr_route;
538 if (new)
539 ospf_route_free (new);
541 lsa->route = or;
542 return 0;
545 static int
546 ospf_ase_route_match_same (struct route_table *rt, struct prefix *prefix,
547 struct ospf_route *newor)
549 struct route_node *rn;
550 struct ospf_route *or;
551 struct ospf_path *op;
552 struct ospf_path *newop;
553 struct listnode *n1;
554 struct listnode *n2;
556 if (! rt || ! prefix)
557 return 0;
559 rn = route_node_lookup (rt, prefix);
560 if (! rn)
561 return 0;
563 route_unlock_node (rn);
565 or = rn->info;
566 if (or->path_type != newor->path_type)
567 return 0;
569 switch (or->path_type)
571 case OSPF_PATH_TYPE1_EXTERNAL:
572 if (or->cost != newor->cost)
573 return 0;
574 break;
575 case OSPF_PATH_TYPE2_EXTERNAL:
576 if ((or->cost != newor->cost) ||
577 (or->u.ext.type2_cost != newor->u.ext.type2_cost))
578 return 0;
579 break;
580 default:
581 assert (0);
582 return 0;
585 if (or->paths->count != newor->paths->count)
586 return 0;
588 /* Check each path. */
589 for (n1 = listhead (or->paths), n2 = listhead (newor->paths);
590 n1 && n2; n1 = listnextnode (n1), n2 = listnextnode (n2))
592 op = listgetdata (n1);
593 newop = listgetdata (n2);
595 if (! IPV4_ADDR_SAME (&op->nexthop, &newop->nexthop))
596 return 0;
597 if (op->ifindex != newop->ifindex)
598 return 0;
600 return 1;
603 static int
604 ospf_ase_compare_tables (struct route_table *new_external_route,
605 struct route_table *old_external_route)
607 struct route_node *rn, *new_rn;
608 struct ospf_route *or;
610 /* Remove deleted routes */
611 for (rn = route_top (old_external_route); rn; rn = route_next (rn))
612 if ((or = rn->info))
614 if (! (new_rn = route_node_lookup (new_external_route, &rn->p)))
615 ospf_zebra_delete ((struct prefix_ipv4 *) &rn->p, or);
616 else
617 route_unlock_node (new_rn);
621 /* Install new routes */
622 for (rn = route_top (new_external_route); rn; rn = route_next (rn))
623 if ((or = rn->info) != NULL)
624 if (! ospf_ase_route_match_same (old_external_route, &rn->p, or))
625 ospf_zebra_add ((struct prefix_ipv4 *) &rn->p, or);
627 return 0;
630 static int
631 ospf_ase_calculate_timer (struct thread *t)
633 struct ospf *ospf;
634 struct ospf_lsa *lsa;
635 struct route_node *rn;
636 struct listnode *node;
637 struct ospf_area *area;
639 ospf = THREAD_ARG (t);
640 ospf->t_ase_calc = NULL;
642 if (ospf->ase_calc)
644 ospf->ase_calc = 0;
646 /* Calculate external route for each AS-external-LSA */
647 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
648 ospf_ase_calculate_route (ospf, lsa);
650 /* This version simple adds to the table all NSSA areas */
651 if (ospf->anyNSSA)
652 for (ALL_LIST_ELEMENTS_RO (ospf->areas, node, area))
654 if (IS_DEBUG_OSPF_NSSA)
655 zlog_debug ("ospf_ase_calculate_timer(): looking at area %s",
656 inet_ntoa (area->area_id));
658 if (area->external_routing == OSPF_AREA_NSSA)
659 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
660 ospf_ase_calculate_route (ospf, lsa);
662 /* kevinm: And add the NSSA routes in ospf_top */
663 LSDB_LOOP (NSSA_LSDB (ospf),rn,lsa)
664 ospf_ase_calculate_route(ospf,lsa);
666 /* Compare old and new external routing table and install the
667 difference info zebra/kernel */
668 ospf_ase_compare_tables (ospf->new_external_route,
669 ospf->old_external_route);
671 /* Delete old external routing table */
672 ospf_route_table_free (ospf->old_external_route);
673 ospf->old_external_route = ospf->new_external_route;
674 ospf->new_external_route = route_table_init ();
676 return 0;
679 void
680 ospf_ase_calculate_schedule (struct ospf *ospf)
682 if (ospf == NULL)
683 return;
685 ospf->ase_calc = 1;
688 void
689 ospf_ase_calculate_timer_add (struct ospf *ospf)
691 if (ospf == NULL)
692 return;
694 if (! ospf->t_ase_calc)
695 ospf->t_ase_calc = thread_add_timer (master, ospf_ase_calculate_timer,
696 ospf, OSPF_ASE_CALC_INTERVAL);
699 void
700 ospf_ase_register_external_lsa (struct ospf_lsa *lsa, struct ospf *top)
702 struct route_node *rn;
703 struct prefix_ipv4 p;
704 struct list *lst;
705 struct as_external_lsa *al;
707 al = (struct as_external_lsa *) lsa->data;
708 p.family = AF_INET;
709 p.prefix = lsa->data->id;
710 p.prefixlen = ip_masklen (al->mask);
711 apply_mask_ipv4 (&p);
713 rn = route_node_get (top->external_lsas, (struct prefix *) &p);
714 if ((lst = rn->info) == NULL)
715 rn->info = lst = list_new();
717 /* We assume that if LSA is deleted from DB
718 is is also deleted from this RT */
720 listnode_add (lst, ospf_lsa_lock (lsa)); /* external_lsas lst */
723 void
724 ospf_ase_unregister_external_lsa (struct ospf_lsa *lsa, struct ospf *top)
726 struct route_node *rn;
727 struct prefix_ipv4 p;
728 struct list *lst;
729 struct as_external_lsa *al;
731 al = (struct as_external_lsa *) lsa->data;
732 p.family = AF_INET;
733 p.prefix = lsa->data->id;
734 p.prefixlen = ip_masklen (al->mask);
735 apply_mask_ipv4 (&p);
737 rn = route_node_get (top->external_lsas, (struct prefix *) &p);
738 lst = rn->info;
740 /* XXX lst can be NULL */
741 if (lst) {
742 listnode_delete (lst, lsa);
743 ospf_lsa_unlock (&lsa); /* external_lsas list */
747 void
748 ospf_ase_external_lsas_finish (struct route_table *rt)
750 struct route_node *rn;
751 struct ospf_lsa *lsa;
752 struct list *lst;
753 struct listnode *node, *nnode;
755 for (rn = route_top (rt); rn; rn = route_next (rn))
756 if ((lst = rn->info) != NULL)
758 for (ALL_LIST_ELEMENTS (lst, node, nnode, lsa))
759 ospf_lsa_unlock (&lsa); /* external_lsas lst */
760 list_delete (lst);
763 route_table_finish (rt);
766 void
767 ospf_ase_incremental_update (struct ospf *ospf, struct ospf_lsa *lsa)
769 struct list *lsas;
770 struct listnode *node;
771 struct route_node *rn, *rn2;
772 struct prefix_ipv4 p;
773 struct route_table *tmp_old;
774 struct as_external_lsa *al;
776 al = (struct as_external_lsa *) lsa->data;
777 p.family = AF_INET;
778 p.prefix = lsa->data->id;
779 p.prefixlen = ip_masklen (al->mask);
780 apply_mask_ipv4 (&p);
782 /* if new_table is NULL, there was no spf calculation, thus
783 incremental update is unneeded */
784 if (!ospf->new_table)
785 return;
787 /* If there is already an intra-area or inter-area route
788 to the destination, no recalculation is necessary
789 (internal routes take precedence). */
791 rn = route_node_lookup (ospf->new_table, (struct prefix *) &p);
792 if (rn)
794 route_unlock_node (rn);
795 if (rn->info)
796 return;
799 rn = route_node_lookup (ospf->external_lsas, (struct prefix *) &p);
800 assert (rn && rn->info);
801 lsas = rn->info;
802 route_unlock_node (rn);
804 for (ALL_LIST_ELEMENTS_RO (lsas, node, lsa))
805 ospf_ase_calculate_route (ospf, lsa);
807 /* prepare temporary old routing table for compare */
808 tmp_old = route_table_init ();
809 rn = route_node_lookup (ospf->old_external_route, (struct prefix *) &p);
810 if (rn && rn->info)
812 rn2 = route_node_get (tmp_old, (struct prefix *) &p);
813 rn2->info = rn->info;
816 /* install changes to zebra */
817 ospf_ase_compare_tables (ospf->new_external_route, tmp_old);
819 /* update ospf->old_external_route table */
820 if (rn && rn->info)
821 ospf_route_free ((struct ospf_route *) rn->info);
823 rn2 = route_node_lookup (ospf->new_external_route, (struct prefix *) &p);
824 /* if new route exists, install it to ospf->old_external_route */
825 if (rn2 && rn2->info)
827 if (!rn)
828 rn = route_node_get (ospf->old_external_route, (struct prefix *) &p);
829 rn->info = rn2->info;
831 else
833 /* remove route node from ospf->old_external_route */
834 if (rn)
836 rn->info = NULL;
837 route_unlock_node (rn);
838 route_unlock_node (rn);
842 if (rn2)
844 /* rn2->info is stored in route node of ospf->old_external_route */
845 rn2->info = NULL;
846 route_unlock_node (rn2);
847 route_unlock_node (rn2);
850 route_table_finish (tmp_old);