Merge branch 'Teaman-ND' into Teaman-RT
[tomato.git] / release / src / router / zebra / ospfd / ospf_lsa.c
blob75490d6b6e0d197fd31ef2319ea9935242566ed9
1 /*
2 * OSPF Link State Advertisement
3 * Copyright (C) 1999, 2000 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 "linklist.h"
26 #include "prefix.h"
27 #include "if.h"
28 #include "table.h"
29 #include "memory.h"
30 #include "stream.h"
31 #include "log.h"
32 #include "thread.h"
33 #include "hash.h"
34 #include "sockunion.h" /* for inet_aton() */
36 #include "ospfd/ospfd.h"
37 #include "ospfd/ospf_interface.h"
38 #include "ospfd/ospf_ism.h"
39 #include "ospfd/ospf_asbr.h"
40 #include "ospfd/ospf_lsa.h"
41 #include "ospfd/ospf_lsdb.h"
42 #include "ospfd/ospf_neighbor.h"
43 #include "ospfd/ospf_nsm.h"
44 #include "ospfd/ospf_flood.h"
45 #include "ospfd/ospf_packet.h"
46 #include "ospfd/ospf_spf.h"
47 #include "ospfd/ospf_dump.h"
48 #include "ospfd/ospf_route.h"
49 #include "ospfd/ospf_ase.h"
50 #include "ospfd/ospf_zebra.h"
53 u_int32_t
54 get_metric (u_char *metric)
56 u_int32_t m;
57 m = metric[0];
58 m = (m << 8) + metric[1];
59 m = (m << 8) + metric[2];
60 return m;
64 struct timeval
65 tv_adjust (struct timeval a)
67 while (a.tv_usec >= 1000000)
69 a.tv_usec -= 1000000;
70 a.tv_sec++;
73 while (a.tv_usec < 0)
75 a.tv_usec += 1000000;
76 a.tv_sec--;
79 return a;
82 int
83 tv_ceil (struct timeval a)
85 a = tv_adjust (a);
87 return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec);
90 int
91 tv_floor (struct timeval a)
93 a = tv_adjust (a);
95 return a.tv_sec;
98 struct timeval
99 int2tv (int a)
101 struct timeval ret;
103 ret.tv_sec = a;
104 ret.tv_usec = 0;
106 return ret;
109 struct timeval
110 tv_add (struct timeval a, struct timeval b)
112 struct timeval ret;
114 ret.tv_sec = a.tv_sec + b.tv_sec;
115 ret.tv_usec = a.tv_usec + b.tv_usec;
117 return tv_adjust (ret);
120 struct timeval
121 tv_sub (struct timeval a, struct timeval b)
123 struct timeval ret;
125 ret.tv_sec = a.tv_sec - b.tv_sec;
126 ret.tv_usec = a.tv_usec - b.tv_usec;
128 return tv_adjust (ret);
132 tv_cmp (struct timeval a, struct timeval b)
134 return (a.tv_sec == b.tv_sec ?
135 a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
139 ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
141 struct timeval delta, now;
142 int delay = 0;
144 gettimeofday (&now, NULL);
145 delta = tv_sub (now, lsa->tv_orig);
147 if (tv_cmp (delta, int2tv (OSPF_MIN_LS_INTERVAL)) < 0)
149 delay = tv_ceil (tv_sub (int2tv (OSPF_MIN_LS_INTERVAL), delta));
151 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
152 zlog_info ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
153 lsa->data->type, inet_ntoa (lsa->data->id), delay);
155 assert (delay > 0);
158 return delay;
163 get_age (struct ospf_lsa *lsa)
165 int age;
166 struct timeval now;
168 gettimeofday (&now, NULL);
169 age = ntohs (lsa->data->ls_age) + tv_floor (tv_sub (now, lsa->tv_recv));
171 return age;
175 /* Fletcher Checksum -- Refer to RFC1008. */
176 #define MODX 4102
177 #define LSA_CHECKSUM_OFFSET 15
179 u_int16_t
180 ospf_lsa_checksum (struct lsa_header *lsa)
182 u_char *sp, *ep, *p, *q;
183 int c0 = 0, c1 = 0;
184 int x, y;
185 u_int16_t length;
187 lsa->checksum = 0;
188 length = ntohs (lsa->length) - 2;
189 sp = (char *) &lsa->options;
191 for (ep = sp + length; sp < ep; sp = q)
193 q = sp + MODX;
194 if (q > ep)
195 q = ep;
196 for (p = sp; p < q; p++)
198 c0 += *p;
199 c1 += c0;
201 c0 %= 255;
202 c1 %= 255;
205 x = ((length - LSA_CHECKSUM_OFFSET) * c0 - c1) % 255;
206 if (x <= 0)
207 x += 255;
208 y = 510 - c0 - x;
209 if (y > 255)
210 y -= 255;
212 /* take care endian issue. */
213 lsa->checksum = htons ((x << 8) + y);
215 return (lsa->checksum);
220 /* Create OSPF LSA. */
221 struct ospf_lsa *
222 ospf_lsa_new ()
224 struct ospf_lsa *new;
226 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
227 memset (new, 0, sizeof (struct ospf_lsa));
229 new->flags = 0;
230 new->lock = 1;
231 new->retransmit_counter = 0;
232 gettimeofday (&new->tv_recv, NULL);
233 new->tv_orig = new->tv_recv;
234 new->refresh_list = -1;
236 return new;
239 /* Duplicate OSPF LSA. */
240 struct ospf_lsa *
241 ospf_lsa_dup (struct ospf_lsa *lsa)
243 struct ospf_lsa *new;
245 if (lsa == NULL)
246 return NULL;
248 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
250 memcpy (new, lsa, sizeof (struct ospf_lsa));
251 UNSET_FLAG (new->flags, OSPF_LSA_DISCARD);
252 new->lock = 1;
253 new->retransmit_counter = 0;
254 new->data = ospf_lsa_data_dup (lsa->data);
256 /* kevinm: Clear the refresh_list, otherwise there are going
257 to be problems when we try to remove the LSA from the
258 queue (which it's not a member of.)
259 XXX: Should we add the LSA to the refresh_list queue? */
260 new->refresh_list = -1;
262 if (IS_DEBUG_OSPF (lsa, LSA))
263 zlog_info ("LSA: duplicated %p (new: %p)", lsa, new);
265 return new;
268 /* Free OSPF LSA. */
269 void
270 ospf_lsa_free (struct ospf_lsa *lsa)
272 assert (lsa->lock == 0);
274 if (IS_DEBUG_OSPF (lsa, LSA))
275 zlog_info ("LSA: freed %p", lsa);
277 /* Delete LSA data. */
278 if (lsa->data != NULL)
279 ospf_lsa_data_free (lsa->data);
281 assert (lsa->refresh_list < 0);
283 memset (lsa, 0, sizeof (struct ospf_lsa));
284 XFREE (MTYPE_OSPF_LSA, lsa);
287 /* Lock LSA. */
288 struct ospf_lsa *
289 ospf_lsa_lock (struct ospf_lsa *lsa)
291 lsa->lock++;
292 return lsa;
295 /* Unlock LSA. */
296 void
297 ospf_lsa_unlock (struct ospf_lsa *lsa)
299 /* This is sanity check. */
300 if (!lsa)
301 return;
303 lsa->lock--;
305 assert (lsa->lock >= 0);
307 if (lsa->lock == 0)
309 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD));
310 ospf_lsa_free (lsa);
314 /* Check discard flag. */
315 void
316 ospf_lsa_discard (struct ospf_lsa *lsa)
318 if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
320 SET_FLAG (lsa->flags, OSPF_LSA_DISCARD);
321 ospf_lsa_unlock (lsa);
325 /* Create LSA data. */
326 struct lsa_header *
327 ospf_lsa_data_new (size_t size)
329 struct lsa_header *new;
331 new = (struct lsa_header *) XMALLOC (MTYPE_OSPF_LSA_DATA, size);
332 memset (new, 0, size);
334 return new;
337 /* Duplicate LSA data. */
338 struct lsa_header *
339 ospf_lsa_data_dup (struct lsa_header *lsah)
341 struct lsa_header *new;
343 new = ospf_lsa_data_new (ntohs (lsah->length));
344 memcpy (new, lsah, ntohs (lsah->length));
346 return new;
349 /* Free LSA data. */
350 void
351 ospf_lsa_data_free (struct lsa_header *lsah)
353 if (IS_DEBUG_OSPF (lsa, LSA))
354 zlog_info ("LSA[Type%d:%s]: data freed %p",
355 lsah->type, inet_ntoa (lsah->id), lsah);
357 XFREE (MTYPE_OSPF_LSA_DATA, lsah);
361 /* LSA general functions. */
363 const char *
364 dump_lsa_key (struct ospf_lsa *lsa)
366 static char buf[] = {
367 "Type255,id(255.255.255.255),ar(255.255.255.255)",
369 struct lsa_header *lsah;
371 if (lsa != NULL && (lsah = lsa->data) != NULL)
373 char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
374 strcpy (id, inet_ntoa (lsah->id));
375 strcpy (ar, inet_ntoa (lsah->adv_router));
377 sprintf (buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar);
379 else
380 strcpy (buf, "NULL");
382 return buf;
385 u_int32_t
386 lsa_seqnum_increment (struct ospf_lsa *lsa)
388 u_int32_t seqnum;
390 seqnum = ntohl (lsa->data->ls_seqnum) + 1;
392 return htonl (seqnum);
395 void
396 lsa_header_set (struct stream *s, u_char options,
397 u_char type, struct in_addr id, struct in_addr router_id)
399 struct lsa_header *lsah;
401 lsah = (struct lsa_header *) STREAM_DATA (s);
403 lsah->ls_age = htons (0);
404 lsah->options = options;
405 lsah->type = type;
406 lsah->id = id;
407 lsah->adv_router = router_id;
408 lsah->ls_seqnum = htonl (OSPF_INITIAL_SEQUENCE_NUMBER);
410 ospf_output_forward (s, OSPF_LSA_HEADER_SIZE);
414 /* router-LSA related functions. */
415 /* Get router-LSA flags. */
416 u_char
417 router_lsa_flags (struct ospf_area *area)
419 u_char flags;
421 flags = area->ospf->flags;
423 /* Set virtual link flag. */
424 if (ospf_full_virtual_nbrs (area))
425 SET_FLAG (flags, ROUTER_LSA_VIRTUAL);
426 else
427 /* Just sanity check */
428 UNSET_FLAG (flags, ROUTER_LSA_VIRTUAL);
430 /* Set Shortcut ABR behabiour flag. */
431 UNSET_FLAG (flags, ROUTER_LSA_SHORTCUT);
432 if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
433 if (!OSPF_IS_AREA_BACKBONE (area))
434 if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
435 area->ospf->backbone == NULL) ||
436 area->shortcut_configured == OSPF_SHORTCUT_ENABLE)
437 SET_FLAG (flags, ROUTER_LSA_SHORTCUT);
439 /* ASBR can't exit in stub area. */
440 if (area->external_routing == OSPF_AREA_STUB)
441 UNSET_FLAG (flags, OSPF_FLAG_ASBR);
443 return flags;
446 /* Lookup neighbor other than myself.
447 And check neighbor count,
448 Point-to-Point link must have only 1 neighbor. */
449 struct ospf_neighbor *
450 ospf_nbr_lookup_ptop (struct ospf_interface *oi)
452 struct ospf_neighbor *nbr = NULL;
453 struct route_node *rn;
455 /* Search neighbor, there must be one of two nbrs. */
456 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
457 if ((nbr = rn->info))
458 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
459 if (nbr->state == NSM_Full)
461 route_unlock_node (rn);
462 break;
465 /* PtoP link must have only 1 neighbor. */
466 if (ospf_nbr_count (oi, 0) > 1)
467 zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
469 return nbr;
472 /* Set a link information. */
473 void
474 link_info_set (struct stream *s, struct in_addr id,
475 struct in_addr data, u_char type, u_char tos, u_int16_t cost)
477 /* TOS based routing is not supported. */
478 stream_put_ipv4 (s, id.s_addr); /* Link ID. */
479 stream_put_ipv4 (s, data.s_addr); /* Link Data. */
480 stream_putc (s, type); /* Link Type. */
481 stream_putc (s, tos); /* TOS = 0. */
482 stream_putw (s, cost); /* Link Cost. */
485 /* Describe Point-to-Point link. */
487 lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi)
489 int links = 0;
490 struct ospf_neighbor *nbr;
491 struct in_addr id, mask;
493 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
494 zlog_info ("LSA[Type1]: Set link Point-to-Point");
496 if ((nbr = ospf_nbr_lookup_ptop (oi)))
497 if (nbr->state == NSM_Full)
499 /* For unnumbered point-to-point networks, the Link Data field
500 should specify the interface's MIB-II ifIndex value. */
501 link_info_set (s, nbr->router_id, oi->address->u.prefix4,
502 LSA_LINK_TYPE_POINTOPOINT, 0, oi->output_cost);
503 links++;
506 if (oi->connected->destination != NULL)
508 /* Option 1:
509 link_type = LSA_LINK_TYPE_STUB;
510 link_id = nbr->address.u.prefix4;
511 link_data.s_addr = 0xffffffff;
512 link_cost = o->output_cost; */
514 id.s_addr = oi->connected->destination->u.prefix4.s_addr;
515 mask.s_addr = 0xffffffff;
516 link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
518 else
520 /* Option 2: We need to include link to a stub
521 network regardless of the state of the neighbor */
522 masklen2ip (oi->address->prefixlen, &mask);
523 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
524 link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
526 links++;
528 return links;
531 /* Describe Broadcast Link. */
533 lsa_link_broadcast_set (struct stream *s, struct ospf_interface *oi)
535 struct ospf_neighbor *dr;
536 struct in_addr id, mask;
538 /* Describe Type 3 Link. */
539 if (oi->state == ISM_Waiting)
541 masklen2ip (oi->address->prefixlen, &mask);
542 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
543 link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
544 return 1;
547 dr = ospf_nbr_lookup_by_addr (oi->nbrs, &DR (oi));
548 /* Describe Type 2 link. */
549 if (dr && (dr->state == NSM_Full ||
550 IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))) &&
551 ospf_nbr_count (oi, NSM_Full) > 0)
553 link_info_set (s, DR (oi), oi->address->u.prefix4,
554 LSA_LINK_TYPE_TRANSIT, 0, oi->output_cost);
556 /* Describe type 3 link. */
557 else
559 masklen2ip (oi->address->prefixlen, &mask);
560 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
561 link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
563 return 1;
567 lsa_link_loopback_set (struct stream *s, struct ospf_interface *oi)
569 struct in_addr id, mask;
571 /* Describe Type 3 Link. */
572 if (oi->state != ISM_Loopback)
573 return 0;
575 mask.s_addr = 0xffffffff;
576 id.s_addr = oi->address->u.prefix4.s_addr;
577 link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
578 return 1;
581 /* Describe Virtual Link. */
583 lsa_link_virtuallink_set (struct stream *s, struct ospf_interface *oi)
585 struct ospf_neighbor *nbr;
587 if (oi->state == ISM_PointToPoint)
588 if ((nbr = ospf_nbr_lookup_ptop (oi)))
589 if (nbr->state == NSM_Full)
591 link_info_set (s, nbr->router_id, oi->address->u.prefix4,
592 LSA_LINK_TYPE_VIRTUALLINK, 0, oi->output_cost);
593 return 1;
596 return 0;
599 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
601 /* This function add for support point-to-multipoint ,see RFC2328
602 12.4.1.4.*/
604 lsa_link_ptomp_set (struct stream *s, struct ospf_interface *oi)
606 int links = 0;
607 struct route_node *rn;
608 struct ospf_neighbor *nbr = NULL;
609 struct in_addr id, mask;
611 mask.s_addr = 0xffffffff;
612 id.s_addr = oi->address->u.prefix4.s_addr;
613 link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
614 links++;
616 zlog_info ("PointToMultipoint: running ptomultip_set");
618 /* Search neighbor, */
619 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
620 if ((nbr = rn->info) != NULL)
621 /* Ignore myself. */
622 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
623 if (nbr->state == NSM_Full)
626 link_info_set (s, nbr->router_id, oi->address->u.prefix4,
627 LSA_LINK_TYPE_POINTOPOINT, 0, oi->output_cost);
628 links++;
629 zlog_info ("PointToMultipoint: set link to %s",
630 inet_ntoa(oi->address->u.prefix4));
633 return links;
636 /* Set router-LSA link information. */
638 router_lsa_link_set (struct stream *s, struct ospf_area *area)
640 listnode node;
641 int links = 0;
643 for (node = listhead (area->oiflist); node; node = nextnode (node))
645 struct ospf_interface *oi = node->data;
646 struct interface *ifp = oi->ifp;
648 /* Check interface is up, OSPF is enable. */
649 if (if_is_up (ifp))
651 if (oi->state != ISM_Down)
653 /* Describe each link. */
654 switch (oi->type)
656 case OSPF_IFTYPE_POINTOPOINT:
657 links += lsa_link_ptop_set (s, oi);
658 break;
659 case OSPF_IFTYPE_BROADCAST:
660 links += lsa_link_broadcast_set (s, oi);
661 break;
662 case OSPF_IFTYPE_NBMA:
663 links += lsa_link_nbma_set (s, oi);
664 break;
665 case OSPF_IFTYPE_POINTOMULTIPOINT:
666 links += lsa_link_ptomp_set (s, oi);
667 break;
668 case OSPF_IFTYPE_VIRTUALLINK:
669 links += lsa_link_virtuallink_set (s, oi);
670 break;
671 case OSPF_IFTYPE_LOOPBACK:
672 links += lsa_link_loopback_set (s, oi);
678 return links;
681 /* Set router-LSA body. */
682 void
683 ospf_router_lsa_body_set (struct stream *s, struct ospf_area *area)
685 unsigned long putp;
686 u_int16_t cnt;
688 /* Set flags. */
689 stream_putc (s, router_lsa_flags (area));
691 /* Set Zero fields. */
692 stream_putc (s, 0);
694 /* Keep pointer to # links. */
695 putp = s->putp;
697 /* Forward word */
698 stream_putw(s, 0);
700 /* Set all link information. */
701 cnt = router_lsa_link_set (s, area);
703 /* Set # of links here. */
704 stream_putw_at (s, putp, cnt);
707 /* Create new router-LSA. */
708 struct ospf_lsa *
709 ospf_router_lsa_new (struct ospf_area *area)
711 struct ospf *ospf = area->ospf;
712 struct stream *s;
713 struct lsa_header *lsah;
714 struct ospf_lsa *new;
715 int length;
717 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
718 zlog_info ("LSA[Type1]: Create router-LSA instance");
720 /* Create a stream for LSA. */
721 s = stream_new (OSPF_MAX_LSA_SIZE);
722 lsah = (struct lsa_header *) STREAM_DATA (s);
724 #ifdef HAVE_NSSA
725 /* Set LSA common header fields. */
726 lsa_header_set (s, LSA_OPTIONS_GET (area) | LSA_NSSA_GET (area),
727 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
728 #else /* ! HAVE_NSSA */
729 /* Set LSA common header fields. */
730 lsa_header_set (s, LSA_OPTIONS_GET (area),
731 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
732 #endif /* HAVE_NSSA */
734 /* Set router-LSA body fields. */
735 ospf_router_lsa_body_set (s, area);
737 /* Set length. */
738 length = stream_get_endp (s);
739 lsah->length = htons (length);
741 /* Now, create OSPF LSA instance. */
742 new = ospf_lsa_new ();
743 new->area = area;
744 SET_FLAG (new->flags, OSPF_LSA_SELF);
746 /* Copy LSA data to store, discard stream. */
747 new->data = ospf_lsa_data_new (length);
748 memcpy (new->data, lsah, length);
749 stream_free (s);
751 return new;
754 /* Originate Router-LSA. */
755 struct ospf_lsa *
756 ospf_router_lsa_originate (struct ospf_area *area)
758 struct ospf_lsa *new;
760 /* Create new router-LSA instance. */
761 new = ospf_router_lsa_new (area);
763 /* Sanity check. */
764 if (new->data->adv_router.s_addr == 0)
766 if (IS_DEBUG_OSPF_EVENT)
767 zlog_info ("LSA[Type1]: AdvRouter is 0, discard");
768 ospf_lsa_discard (new);
769 return NULL;
772 /* Install LSA to LSDB. */
773 new = ospf_lsa_install (area->ospf, NULL, new);
775 /* Update LSA origination count. */
776 area->ospf->lsa_originate_count++;
778 /* Flooding new LSA through area. */
779 ospf_flood_through_area (area, NULL, new);
781 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
783 zlog_info ("LSA[Type%d:%s]: Originate router-LSA %p",
784 new->data->type, inet_ntoa (new->data->id), new);
785 ospf_lsa_header_dump (new->data);
788 return new;
791 /* Refresh router-LSA. */
792 struct ospf_lsa *
793 ospf_router_lsa_refresh (struct ospf_lsa *lsa)
795 struct ospf_area *area = lsa->area;
796 struct ospf_lsa *new;
798 /* Sanity check. */
799 assert (lsa->data);
801 /* Delete LSA from neighbor retransmit-list. */
802 ospf_ls_retransmit_delete_nbr_area (area, lsa);
804 /* Create new router-LSA instance. */
805 new = ospf_router_lsa_new (area);
806 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
808 ospf_lsa_install (area->ospf, NULL, new);
810 /* Flood LSA through area. */
811 ospf_flood_through_area (area, NULL, new);
813 /* Debug logging. */
814 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
816 zlog_info ("LSA[Type%d:%s]: router-LSA refresh",
817 new->data->type, inet_ntoa (new->data->id));
818 ospf_lsa_header_dump (new->data);
821 return NULL;
825 ospf_router_lsa_timer (struct thread *t)
827 struct ospf_area *area;
829 if (IS_DEBUG_OSPF_EVENT)
830 zlog_info ("Timer[router-LSA]: (router-LSA Refresh expire)");
832 area = THREAD_ARG (t);
833 area->t_router_lsa_self = NULL;
835 /* Now refresh router-LSA. */
836 if (area->router_lsa_self)
837 ospf_router_lsa_refresh (area->router_lsa_self);
838 /* Newly originate router-LSA. */
839 else
840 ospf_router_lsa_originate (area);
842 return 0;
845 void
846 ospf_router_lsa_timer_add (struct ospf_area *area)
848 /* Keep area's self-originated router-LSA. */
849 struct ospf_lsa *lsa = area->router_lsa_self;
851 /* Cancel previously scheduled router-LSA timer. */
852 if (area->t_router_lsa_self)
853 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
854 zlog_info ("LSA[Type1]: Cancel previous router-LSA timer");
856 OSPF_TIMER_OFF (area->t_router_lsa_self);
858 /* If router-LSA is originated previously, check the interval time. */
859 if (lsa)
861 int delay;
862 if ((delay = ospf_lsa_refresh_delay (lsa)) > 0)
864 OSPF_AREA_TIMER_ON (area->t_router_lsa_self,
865 ospf_router_lsa_timer, delay);
866 return;
870 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
871 zlog_info ("LSA[Type1]: Scheduling router-LSA origination right away");
873 /* Immediately refresh router-LSA. */
874 OSPF_AREA_TIMER_ON (area->t_router_lsa_self, ospf_router_lsa_timer, 0);
878 ospf_router_lsa_update_timer (struct thread *thread)
880 struct ospf *ospf = THREAD_ARG (thread);
881 listnode node;
883 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
884 zlog_info ("Timer[router-LSA Update]: (timer expire)");
886 ospf->t_router_lsa_update = NULL;
888 for (node = listhead (ospf->areas); node; nextnode (node))
890 struct ospf_area *area = getdata (node);
891 struct ospf_lsa *lsa = area->router_lsa_self;
892 struct router_lsa *rl;
893 char *area_str;
895 /* Keep Area ID string. */
896 area_str = AREA_NAME (area);
898 /* If LSA not exist in this Area, originate new. */
899 if (lsa == NULL)
901 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
902 zlog_info("LSA[Type1]: Create router-LSA for Area %s", area_str);
904 ospf_router_lsa_originate (area);
906 /* If router-ID is changed, Link ID must change.
907 First flush old LSA, then originate new. */
908 else if (!IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
910 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
911 zlog_info("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
912 lsa->data->type, inet_ntoa (lsa->data->id), area_str);
913 ospf_lsa_flush_area (lsa, area);
914 ospf_lsa_unlock (area->router_lsa_self);
915 area->router_lsa_self = NULL;
917 /* Refresh router-LSA, (not install) and flood through area. */
918 ospf_router_lsa_timer_add (area);
920 else
922 rl = (struct router_lsa *) lsa->data;
923 /* Refresh router-LSA, (not install) and flood through area. */
924 if (rl->flags != ospf->flags)
925 ospf_router_lsa_timer_add (area);
929 return 0;
933 /* network-LSA related functions. */
934 /* Originate Network-LSA. */
935 void
936 ospf_network_lsa_body_set (struct stream *s, struct ospf_interface *oi)
938 struct in_addr mask;
939 struct route_node *rn;
940 struct ospf_neighbor *nbr;
942 masklen2ip (oi->address->prefixlen, &mask);
943 stream_put_ipv4 (s, mask.s_addr);
945 /* The network-LSA lists those routers that are fully adjacent to
946 the Designated Router; each fully adjacent router is identified by
947 its OSPF Router ID. The Designated Router includes itself in this
948 list. RFC2328, Section 12.4.2 */
950 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
951 if ((nbr = rn->info) != NULL)
952 if (nbr->state == NSM_Full || nbr == oi->nbr_self)
953 stream_put_ipv4 (s, nbr->router_id.s_addr);
956 struct ospf_lsa *
957 ospf_network_lsa_new (struct ospf_interface *oi)
959 struct stream *s;
960 struct ospf_lsa *new;
961 struct lsa_header *lsah;
962 int length;
964 /* If there are no neighbours on this network (the net is stub),
965 the router does not originate network-LSA (see RFC 12.4.2) */
966 if (oi->full_nbrs == 0)
967 return NULL;
969 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
970 zlog_info ("LSA[Type2]: Create network-LSA instance");
972 /* Create new stream for LSA. */
973 s = stream_new (OSPF_MAX_LSA_SIZE);
974 lsah = (struct lsa_header *) STREAM_DATA (s);
976 lsa_header_set (s, (OPTIONS (oi) | LSA_OPTIONS_GET (oi->area)),
977 OSPF_NETWORK_LSA, DR (oi), oi->ospf->router_id);
979 /* Set network-LSA body fields. */
980 ospf_network_lsa_body_set (s, oi);
982 /* Set length. */
983 length = stream_get_endp (s);
984 lsah->length = htons (length);
986 /* Create OSPF LSA instance. */
987 new = ospf_lsa_new ();
988 new->area = oi->area;
989 SET_FLAG (new->flags, OSPF_LSA_SELF);
991 /* Copy LSA to store. */
992 new->data = ospf_lsa_data_new (length);
993 memcpy (new->data, lsah, length);
994 stream_free (s);
996 return new;
999 /* Originate network-LSA. */
1000 struct ospf_lsa *
1001 ospf_network_lsa_originate (struct ospf_interface *oi)
1003 struct ospf_lsa *new;
1005 /* Create new network-LSA instance. */
1006 new = ospf_network_lsa_new (oi);
1007 if (new == NULL)
1008 return NULL;
1010 /* Install LSA to LSDB. */
1011 new = ospf_lsa_install (oi->ospf, oi, new);
1013 /* Update LSA origination count. */
1014 oi->ospf->lsa_originate_count++;
1016 /* Flooding new LSA through area. */
1017 ospf_flood_through_area (oi->area, NULL, new);
1019 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1021 zlog_info ("LSA[Type%d:%s]: Originate network-LSA %p",
1022 new->data->type, inet_ntoa (new->data->id), new);
1023 ospf_lsa_header_dump (new->data);
1026 return new;
1030 ospf_network_lsa_refresh (struct ospf_lsa *lsa, struct ospf_interface *oi)
1032 struct ospf_area *area = lsa->area;
1033 struct ospf_lsa *new;
1035 assert (lsa->data);
1037 /* Delete LSA from neighbor retransmit-list. */
1038 ospf_ls_retransmit_delete_nbr_area (area, lsa);
1040 /* Create new network-LSA instance. */
1041 new = ospf_network_lsa_new (oi);
1042 if (new == NULL)
1043 return -1;
1044 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1046 ospf_lsa_install (area->ospf, oi, new);
1048 /* Flood LSA through aera. */
1049 ospf_flood_through_area (area, NULL, new);
1051 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1053 zlog_info ("LSA[Type%d:%s]: network-LSA refresh",
1054 new->data->type, inet_ntoa (new->data->id));
1055 ospf_lsa_header_dump (new->data);
1058 return 0;
1062 ospf_network_lsa_refresh_timer (struct thread *t)
1064 struct ospf_interface *oi;
1066 oi = THREAD_ARG (t);
1067 oi->t_network_lsa_self = NULL;
1069 if (oi->network_lsa_self)
1070 /* Now refresh network-LSA. */
1071 ospf_network_lsa_refresh (oi->network_lsa_self, oi);
1072 else
1073 /* Newly create network-LSA. */
1074 ospf_network_lsa_originate (oi);
1076 return 0;
1079 void
1080 ospf_network_lsa_timer_add (struct ospf_interface *oi)
1082 /* Keep interface's self-originated network-LSA. */
1083 struct ospf_lsa *lsa = oi->network_lsa_self;
1085 /* Cancel previously schedules network-LSA timer. */
1086 if (oi->t_network_lsa_self)
1087 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1088 zlog_info ("LSA[Type2]: Cancel previous network-LSA timer");
1089 OSPF_TIMER_OFF (oi->t_network_lsa_self);
1091 /* If network-LSA is originated previously, check the interval time. */
1092 if (lsa)
1094 int delay;
1095 if ((delay = ospf_lsa_refresh_delay (lsa)) > 0)
1097 oi->t_network_lsa_self =
1098 thread_add_timer (master, ospf_network_lsa_refresh_timer,
1099 oi, delay);
1100 return;
1104 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1105 zlog_info ("Scheduling network-LSA origination right away");
1107 /* Immediately refresh network-LSA. */
1108 oi->t_network_lsa_self =
1109 thread_add_event (master, ospf_network_lsa_refresh_timer, oi, 0);
1113 void
1114 stream_put_ospf_metric (struct stream *s, u_int32_t metric_value)
1116 u_int32_t metric;
1117 char *mp;
1119 /* Put 0 metric. TOS metric is not supported. */
1120 metric = htonl (metric_value);
1121 mp = (char *) &metric;
1122 mp++;
1123 stream_put (s, mp, 3);
1126 /* summary-LSA related functions. */
1127 void
1128 ospf_summary_lsa_body_set (struct stream *s, struct prefix *p,
1129 u_int32_t metric)
1131 struct in_addr mask;
1133 masklen2ip (p->prefixlen, &mask);
1135 /* Put Network Mask. */
1136 stream_put_ipv4 (s, mask.s_addr);
1138 /* Set # TOS. */
1139 stream_putc (s, (u_char) 0);
1141 /* Set metric. */
1142 stream_put_ospf_metric (s, metric);
1145 struct ospf_lsa *
1146 ospf_summary_lsa_new (struct ospf_area *area, struct prefix *p,
1147 u_int32_t metric, struct in_addr id)
1149 struct stream *s;
1150 struct ospf_lsa *new;
1151 struct lsa_header *lsah;
1152 int length;
1154 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1155 zlog_info ("LSA[Type3]: Create summary-LSA instance");
1157 /* Create new stream for LSA. */
1158 s = stream_new (OSPF_MAX_LSA_SIZE);
1159 lsah = (struct lsa_header *) STREAM_DATA (s);
1161 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_SUMMARY_LSA,
1162 id, area->ospf->router_id);
1164 /* Set summary-LSA body fields. */
1165 ospf_summary_lsa_body_set (s, p, metric);
1167 /* Set length. */
1168 length = stream_get_endp (s);
1169 lsah->length = htons (length);
1171 /* Create OSPF LSA instance. */
1172 new = ospf_lsa_new ();
1173 new->area = area;
1174 SET_FLAG (new->flags, OSPF_LSA_SELF);
1176 /* Copy LSA to store. */
1177 new->data = ospf_lsa_data_new (length);
1178 memcpy (new->data, lsah, length);
1179 stream_free (s);
1181 return new;
1184 /* Originate Summary-LSA. */
1185 struct ospf_lsa *
1186 ospf_summary_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1187 struct ospf_area *area)
1189 struct ospf_lsa *new;
1190 struct in_addr id;
1192 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
1194 /* Create new summary-LSA instance. */
1195 new = ospf_summary_lsa_new (area, (struct prefix *) p, metric, id);
1197 /* Instlal LSA to LSDB. */
1198 new = ospf_lsa_install (area->ospf, NULL, new);
1200 /* Update LSA origination count. */
1201 area->ospf->lsa_originate_count++;
1203 /* Flooding new LSA through area. */
1204 ospf_flood_through_area (area, NULL, new);
1206 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1208 zlog_info ("LSA[Type%d:%s]: Originate summary-LSA %p",
1209 new->data->type, inet_ntoa (new->data->id), new);
1210 ospf_lsa_header_dump (new->data);
1213 return new;
1216 struct ospf_lsa*
1217 ospf_summary_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1219 struct ospf_lsa *new;
1220 struct summary_lsa *sl;
1221 struct prefix p;
1223 /* Sanity check. */
1224 assert (lsa->data);
1226 sl = (struct summary_lsa *)lsa->data;
1227 p.prefixlen = ip_masklen (sl->mask);
1228 new = ospf_summary_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1229 sl->header.id);
1231 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1233 /* Re-calculate checksum. */
1234 ospf_lsa_checksum (new->data);
1236 ospf_lsa_install (ospf, NULL, new);
1238 /* Flood LSA through AS. */
1239 ospf_flood_through_area (new->area, NULL, new);
1241 /* Debug logging. */
1242 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1244 zlog_info ("LSA[Type%d:%s]: summary-LSA refresh",
1245 new->data->type, inet_ntoa (new->data->id));
1246 ospf_lsa_header_dump (new->data);
1249 return new;
1253 /* summary-ASBR-LSA related functions. */
1254 void
1255 ospf_summary_asbr_lsa_body_set (struct stream *s, struct prefix *p,
1256 u_int32_t metric)
1258 struct in_addr mask;
1260 masklen2ip (p->prefixlen, &mask);
1262 /* Put Network Mask. */
1263 stream_put_ipv4 (s, mask.s_addr);
1265 /* Set # TOS. */
1266 stream_putc (s, (u_char) 0);
1268 /* Set metric. */
1269 stream_put_ospf_metric (s, metric);
1272 struct ospf_lsa *
1273 ospf_summary_asbr_lsa_new (struct ospf_area *area, struct prefix *p,
1274 u_int32_t metric, struct in_addr id)
1276 struct stream *s;
1277 struct ospf_lsa *new;
1278 struct lsa_header *lsah;
1279 int length;
1281 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1282 zlog_info ("LSA[Type3]: Create summary-LSA instance");
1284 /* Create new stream for LSA. */
1285 s = stream_new (OSPF_MAX_LSA_SIZE);
1286 lsah = (struct lsa_header *) STREAM_DATA (s);
1288 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_ASBR_SUMMARY_LSA,
1289 id, area->ospf->router_id);
1291 /* Set summary-LSA body fields. */
1292 ospf_summary_asbr_lsa_body_set (s, p, metric);
1294 /* Set length. */
1295 length = stream_get_endp (s);
1296 lsah->length = htons (length);
1298 /* Create OSPF LSA instance. */
1299 new = ospf_lsa_new ();
1300 new->area = area;
1301 SET_FLAG (new->flags, OSPF_LSA_SELF);
1303 /* Copy LSA to store. */
1304 new->data = ospf_lsa_data_new (length);
1305 memcpy (new->data, lsah, length);
1306 stream_free (s);
1308 return new;
1311 /* Originate summary-ASBR-LSA. */
1312 struct ospf_lsa *
1313 ospf_summary_asbr_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1314 struct ospf_area *area)
1316 struct ospf_lsa *new;
1317 struct in_addr id;
1319 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA, p);
1321 /* Create new summary-LSA instance. */
1322 new = ospf_summary_asbr_lsa_new (area, (struct prefix *) p, metric, id);
1324 /* Install LSA to LSDB. */
1325 new = ospf_lsa_install (area->ospf, NULL, new);
1327 /* Update LSA origination count. */
1328 area->ospf->lsa_originate_count++;
1330 /* Flooding new LSA through area. */
1331 ospf_flood_through_area (area, NULL, new);
1333 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1335 zlog_info ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1336 new->data->type, inet_ntoa (new->data->id), new);
1337 ospf_lsa_header_dump (new->data);
1340 return new;
1343 struct ospf_lsa*
1344 ospf_summary_asbr_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1346 struct ospf_lsa *new;
1347 struct summary_lsa *sl;
1348 struct prefix p;
1350 /* Sanity check. */
1351 assert (lsa->data);
1353 sl = (struct summary_lsa *)lsa->data;
1354 p.prefixlen = ip_masklen (sl->mask);
1355 new = ospf_summary_asbr_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1356 sl->header.id);
1358 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1360 /* Re-calculate checksum. */
1361 ospf_lsa_checksum (new->data);
1363 ospf_lsa_install (ospf, NULL, new);
1365 /* Flood LSA through area. */
1366 ospf_flood_through_area (new->area, NULL, new);
1368 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1370 zlog_info ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1371 new->data->type, inet_ntoa (new->data->id));
1372 ospf_lsa_header_dump (new->data);
1375 return new;
1378 /* AS-external-LSA related functions. */
1380 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1381 is connected, else 0*/
1382 struct in_addr
1383 ospf_external_lsa_nexthop_get (struct ospf *ospf, struct in_addr nexthop)
1385 struct in_addr fwd;
1386 struct prefix nh;
1387 listnode n1;
1389 fwd.s_addr = 0;
1391 if (!nexthop.s_addr)
1392 return fwd;
1394 /* Check whether nexthop is covered by OSPF network. */
1395 nh.family = AF_INET;
1396 nh.u.prefix4 = nexthop;
1397 nh.prefixlen = IPV4_MAX_BITLEN;
1399 for (n1 = listhead (ospf->oiflist); n1; nextnode (n1))
1401 struct ospf_interface *oi = getdata (n1);
1403 if (if_is_up (oi->ifp))
1404 if (oi->address->family == AF_INET)
1405 if (prefix_match (oi->address, &nh))
1406 return nexthop;
1409 return fwd;
1412 #ifdef HAVE_NSSA
1413 /* NSSA-external-LSA related functions. */
1415 /* Get 1st IP connection for Forward Addr */
1417 struct in_addr
1418 ospf_get_ip_from_ifp (struct ospf_interface *oi)
1420 struct in_addr fwd;
1422 fwd.s_addr = 0;
1424 if (if_is_up (oi->ifp))
1425 return oi->address->u.prefix4;
1427 return fwd;
1430 /* Get 1st IP connection for Forward Addr */
1431 struct in_addr
1432 ospf_get_nssa_ip (struct ospf_area *area)
1434 struct in_addr fwd;
1435 struct in_addr best_default;
1436 listnode n1;
1438 fwd.s_addr = 0;
1439 best_default.s_addr = 0;
1441 for (n1 = listhead (area->ospf->oiflist); n1; nextnode (n1))
1443 struct ospf_interface *oi = getdata (n1);
1445 if (if_is_up (oi->ifp))
1446 if (oi->area->external_routing == OSPF_AREA_NSSA)
1447 if (oi->address && oi->address->family == AF_INET)
1449 if (best_default.s_addr == 0)
1450 best_default = oi->address->u.prefix4;
1451 if (oi->area == area)
1452 return oi->address->u.prefix4;
1456 if (best_default.s_addr != 0)
1457 return best_default;
1459 return fwd;
1461 #endif /* HAVE_NSSA */
1463 #define DEFAULT_DEFAULT_METRIC 20
1464 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1465 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1467 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1470 metric_type (struct ospf *ospf, u_char src)
1472 return (ospf->dmetric[src].type < 0 ?
1473 DEFAULT_METRIC_TYPE : ospf->dmetric[src].type);
1477 metric_value (struct ospf *ospf, u_char src)
1479 if (ospf->dmetric[src].value < 0)
1481 if (src == DEFAULT_ROUTE)
1483 if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1484 return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1485 else
1486 return DEFAULT_DEFAULT_ALWAYS_METRIC;
1488 else if (ospf->default_metric < 0)
1489 return DEFAULT_DEFAULT_METRIC;
1490 else
1491 return ospf->default_metric;
1494 return ospf->dmetric[src].value;
1497 /* Set AS-external-LSA body. */
1498 void
1499 ospf_external_lsa_body_set (struct stream *s, struct external_info *ei,
1500 struct ospf *ospf)
1502 struct prefix_ipv4 *p = &ei->p;
1503 struct in_addr mask, fwd_addr;
1504 u_int32_t mvalue;
1505 int mtype;
1506 int type;
1508 /* Put Network Mask. */
1509 masklen2ip (p->prefixlen, &mask);
1510 stream_put_ipv4 (s, mask.s_addr);
1512 /* If prefix is default, specify DEFAULT_ROUTE. */
1513 type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
1515 mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ?
1516 ROUTEMAP_METRIC_TYPE (ei) : metric_type (ospf, type);
1518 mvalue = (ROUTEMAP_METRIC (ei) != -1) ?
1519 ROUTEMAP_METRIC (ei) : metric_value (ospf, type);
1521 /* Put type of external metric. */
1522 stream_putc (s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1524 /* Put 0 metric. TOS metric is not supported. */
1525 stream_put_ospf_metric (s, mvalue);
1527 /* Get forwarding address to nexthop if on the Connection List, else 0. */
1528 fwd_addr = ospf_external_lsa_nexthop_get (ospf, ei->nexthop);
1530 /* Put forwarding address. */
1531 stream_put_ipv4 (s, fwd_addr.s_addr);
1533 /* Put route tag -- This value should be introduced from configuration. */
1534 stream_putl (s, 0);
1537 /* Create new external-LSA. */
1538 struct ospf_lsa *
1539 ospf_external_lsa_new (struct ospf *ospf,
1540 struct external_info *ei, struct in_addr *old_id)
1542 struct stream *s;
1543 struct lsa_header *lsah;
1544 struct ospf_lsa *new;
1545 struct in_addr id;
1546 int length;
1548 if (ei == NULL)
1550 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1551 zlog_warn ("LSA[Type5]: External info is NULL, could not originated");
1552 return NULL;
1555 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1556 zlog_info ("LSA[Type5]: Originate AS-external-LSA instance");
1558 /* If old Link State ID is specified, refresh LSA with same ID. */
1559 if (old_id)
1560 id = *old_id;
1561 /* Get Link State with unique ID. */
1562 else
1564 id = ospf_lsa_unique_id (ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA, &ei->p);
1565 if (id.s_addr == 0xffffffff)
1567 /* Maybe Link State ID not available. */
1568 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1569 zlog_info ("LSA[Type5]: Link ID not available, can't originate");
1570 return NULL;
1574 /* Create new stream for LSA. */
1575 s = stream_new (OSPF_MAX_LSA_SIZE);
1576 lsah = (struct lsa_header *) STREAM_DATA (s);
1578 /* Set LSA common header fields. */
1579 lsa_header_set (s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA,
1580 id, ospf->router_id);
1582 /* Set AS-external-LSA body fields. */
1583 ospf_external_lsa_body_set (s, ei, ospf);
1585 /* Set length. */
1586 length = stream_get_endp (s);
1587 lsah->length = htons (length);
1589 /* Now, create OSPF LSA instance. */
1590 new = ospf_lsa_new ();
1591 new->area = NULL;
1592 SET_FLAG (new->flags, OSPF_LSA_SELF|OSPF_LSA_APPROVED);
1594 /* Copy LSA data to store, discard stream. */
1595 new->data = ospf_lsa_data_new (length);
1596 memcpy (new->data, lsah, length);
1597 stream_free (s);
1599 return new;
1602 #ifdef HAVE_NSSA
1603 /* As Type-7 */
1604 void
1605 ospf_install_flood_nssa (struct ospf *ospf,
1606 struct ospf_lsa *lsa, struct external_info *ei)
1608 struct ospf_lsa *new2;
1609 struct as_external_lsa *extlsa;
1610 listnode node;
1612 /* NSSA Originate or Refresh (If anyNSSA)
1614 LSA is self-originated. And just installed as Type-5.
1615 Additionally, install as Type-7 LSDB for every attached NSSA.
1617 P-Bit controls which ABR performs translation to outside world; If
1618 we are an ABR....do not set the P-bit, because we send the Type-5,
1619 not as the ABR Translator, but as the ASBR owner within the AS!
1621 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1622 elected ABR Translator will see the P-bit, Translate, and re-flood.
1624 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1625 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1627 for (node = listhead (ospf->areas); node; nextnode (node))
1629 struct ospf_area *area = getdata (node);
1631 /* make lsa duplicate, lock=1 */
1632 new2 = ospf_lsa_dup (lsa);
1634 new2->area = area;
1635 new2->data->type = OSPF_AS_NSSA_LSA;
1637 /* set P-bit if not ABR */
1638 if (! IS_OSPF_ABR (ospf))
1640 SET_FLAG(new2->data->options, OSPF_OPTION_NP);
1642 /* set non-zero FWD ADDR
1644 draft-ietf-ospf-nssa-update-09.txt
1646 if the network between the NSSA AS boundary router and the
1647 adjacent AS is advertised into OSPF as an internal OSPF route,
1648 the forwarding address should be the next op address as is cu
1649 currently done with type-5 LSAs. If the intervening network is
1650 not adversited into OSPF as an internal OSPF route and the
1651 type-7 LSA's P-bit is set a forwarding address should be
1652 selected from one of the router's active OSPF inteface addresses
1653 which belong to the NSSA. If no such addresses exist, then
1654 no type-7 LSA's with the P-bit set should originate from this
1655 router. */
1657 /* kevinm: not updating lsa anymore, just new2 */
1658 extlsa = (struct as_external_lsa *)(new2->data);
1660 if (extlsa->e[0].fwd_addr.s_addr == 0)
1661 extlsa->e[0].fwd_addr = ospf_get_nssa_ip(area); /* this NSSA area in ifp */
1663 if (extlsa->e[0].fwd_addr.s_addr == 0)
1665 if (IS_DEBUG_OSPF_NSSA)
1666 zlog_info ("LSA[Type-7]: Could not build FWD-ADDR");
1667 ospf_lsa_discard(new2);
1668 return;
1671 /* Re-calculate checksum. */
1672 ospf_lsa_checksum (new2->data);
1674 /* install also as Type-7 */
1675 ospf_lsa_install (ospf, NULL, new2); /* Remove Old, Lock New = 2 */
1677 /* will send each copy, lock=2+n */
1678 ospf_flood_through_as (ospf, NULL, new2); /* all attached NSSA's, no AS/STUBs */
1681 #endif /* HAVE_NSSA */
1684 is_prefix_default (struct prefix_ipv4 *p)
1686 struct prefix_ipv4 q;
1688 q.family = AF_INET;
1689 q.prefix.s_addr = 0;
1690 q.prefixlen = 0;
1692 return prefix_same ((struct prefix *) p, (struct prefix *) &q);
1695 /* Originate an AS-external-LSA, install and flood. */
1696 struct ospf_lsa *
1697 ospf_external_lsa_originate (struct ospf *ospf, struct external_info *ei)
1699 struct ospf_lsa *new;
1701 /* Added for NSSA project....
1703 External LSAs are originated in ASBRs as usual, but for NSSA systems.
1704 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1705 every area. The Type-7's are flooded to every IR and every ABR; We
1706 install the Type-5 LSDB so that the normal "refresh" code operates
1707 as usual, and flag them as not used during ASE calculations. The
1708 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
1709 Address of non-zero.
1711 If an ABR is the elected NSSA translator, following SPF and during
1712 the ABR task it will translate all the scanned Type-7's, with P-bit
1713 ON and not-self generated, and translate to Type-5's throughout the
1714 non-NSSA/STUB AS.
1716 A difference in operation depends whether this ASBR is an ABR
1717 or not. If not an ABR, the P-bit is ON, to indicate that any
1718 elected NSSA-ABR can perform its translation.
1720 If an ABR, the P-bit is OFF; No ABR will perform translation and
1721 this ASBR will flood the Type-5 LSA as usual.
1723 For the case where this ASBR is not an ABR, the ASE calculations
1724 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
1725 demonstrate to the user that there are LSA's that belong to any
1726 attached NSSA.
1728 Finally, it just so happens that when the ABR is translating every
1729 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
1730 approved Type-5 (translated from Type-7); at the end of translation
1731 if any Translated Type-5's remain unapproved, then they must be
1732 flushed from the AS.
1736 /* Check the AS-external-LSA should be originated. */
1737 if (!ospf_redistribute_check (ospf, ei, NULL))
1738 return NULL;
1740 /* Create new AS-external-LSA instance. */
1741 if ((new = ospf_external_lsa_new (ospf, ei, NULL)) == NULL)
1743 if (IS_DEBUG_OSPF_EVENT)
1744 zlog_info ("LSA[Type5:%s]: Could not originate AS-external-LSA",
1745 inet_ntoa (ei->p.prefix));
1746 return NULL;
1749 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
1750 ospf_lsa_install (ospf, NULL, new);
1752 /* Update LSA origination count. */
1753 ospf->lsa_originate_count++;
1755 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
1756 ospf_flood_through_as (ospf, NULL, new);
1758 #ifdef HAVE_NSSA
1759 /* If there is any attached NSSA, do special handling */
1760 if (ospf->anyNSSA)
1761 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
1762 #endif /* HAVE_NSSA */
1764 /* Debug logging. */
1765 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1767 zlog_info ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
1768 new->data->type, inet_ntoa (new->data->id), new);
1769 ospf_lsa_header_dump (new->data);
1772 return new;
1775 /* Originate AS-external-LSA from external info with initial flag. */
1777 ospf_external_lsa_originate_timer (struct thread *thread)
1779 struct ospf *ospf = THREAD_ARG (thread);
1780 struct route_node *rn;
1781 struct external_info *ei;
1782 struct route_table *rt;
1783 int type = THREAD_VAL (thread);
1785 ospf->t_external_lsa = NULL;
1787 /* Originate As-external-LSA from all type of distribute source. */
1788 if ((rt = EXTERNAL_INFO (type)))
1789 for (rn = route_top (rt); rn; rn = route_next (rn))
1790 if ((ei = rn->info) != NULL)
1791 if (!is_prefix_default ((struct prefix_ipv4 *)&ei->p))
1792 if (!ospf_external_lsa_originate (ospf, ei))
1793 zlog_warn ("LSA: AS-external-LSA was not originated.");
1795 return 0;
1798 struct external_info *
1799 ospf_default_external_info (struct ospf *ospf)
1801 int type;
1802 struct route_node *rn;
1803 struct prefix_ipv4 p;
1805 p.family = AF_INET;
1806 p.prefix.s_addr = 0;
1807 p.prefixlen = 0;
1809 /* First, lookup redistributed default route. */
1810 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
1811 if (EXTERNAL_INFO (type) && type != ZEBRA_ROUTE_OSPF)
1813 rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
1814 if (rn != NULL)
1816 route_unlock_node (rn);
1817 assert (rn->info);
1818 if (ospf_redistribute_check (ospf, rn->info, NULL))
1819 return rn->info;
1823 return NULL;
1827 ospf_default_originate_timer (struct thread *thread)
1829 int *origin;
1830 struct prefix_ipv4 p;
1831 struct in_addr nexthop;
1832 struct external_info *ei;
1833 struct ospf *ospf;
1835 ospf = ospf_lookup ();
1837 /* Get originate flags. */
1838 origin = THREAD_ARG (thread);
1840 p.family = AF_INET;
1841 p.prefix.s_addr = 0;
1842 p.prefixlen = 0;
1844 if (*origin == DEFAULT_ORIGINATE_ALWAYS)
1846 /* If there is no default route via redistribute,
1847 then originate AS-external-LSA with nexthop 0 (self). */
1848 nexthop.s_addr = 0;
1849 ospf_external_info_add (DEFAULT_ROUTE, p, 0, nexthop);
1852 if ((ei = ospf_default_external_info (ospf)))
1853 ospf_external_lsa_originate (ospf, ei);
1855 return 0;
1858 #ifdef HAVE_NSSA
1859 /* Flush any NSSA LSAs for given prefix */
1860 void
1861 ospf_nssa_lsa_flush (struct ospf *ospf, struct prefix_ipv4 *p)
1863 struct listnode *node;
1864 struct ospf_lsa *lsa;
1865 struct ospf_area *area;
1867 for (node = listhead (ospf->areas); node; nextnode (node))
1869 if (((area = getdata (node)) != NULL)
1870 && (area->external_routing == OSPF_AREA_NSSA))
1872 if (!(lsa = ospf_lsa_lookup (area, OSPF_AS_NSSA_LSA, p->prefix,
1873 ospf->router_id)))
1875 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
1876 zlog_warn ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
1877 inet_ntoa (p->prefix), p->prefixlen);
1878 continue;
1880 ospf_ls_retransmit_delete_nbr_area (area, lsa);
1881 if (!IS_LSA_MAXAGE (lsa))
1883 ospf_refresher_unregister_lsa (ospf, lsa);
1884 ospf_lsa_flush_area (lsa, area);
1889 #endif /* HAVE_NSSA */
1891 /* Flush an AS-external-LSA from LSDB and routing domain. */
1892 void
1893 ospf_external_lsa_flush (struct ospf *ospf,
1894 u_char type, struct prefix_ipv4 *p,
1895 unsigned int ifindex, struct in_addr nexthop)
1897 struct ospf_lsa *lsa;
1899 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
1900 zlog_info ("LSA: Flushing AS-external-LSA %s/%d",
1901 inet_ntoa (p->prefix), p->prefixlen);
1903 /* First lookup LSA from LSDB. */
1904 if (!(lsa = ospf_external_info_find_lsa (ospf, p)))
1906 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
1907 zlog_warn ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
1908 inet_ntoa (p->prefix), p->prefixlen);
1909 return;
1911 #ifdef HAVE_NSSA
1912 /* If LSA is selforiginated and there is NSSA area, flush
1913 * Type-7 LSA's at first. */
1915 if (IS_LSA_SELF(lsa) && (ospf->anyNSSA))
1916 ospf_nssa_lsa_flush (ospf, p);
1917 #endif /* HAVE_NSSA */
1919 /* Sweep LSA from Link State Retransmit List. */
1920 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
1922 if (!IS_LSA_MAXAGE (lsa))
1924 /* Unregister LSA from Refresh queue. */
1925 ospf_refresher_unregister_lsa (ospf, lsa);
1927 /* Flush AS-external-LSA through AS. */
1928 ospf_lsa_flush_as (ospf, lsa);
1931 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
1932 zlog_info ("ospf_external_lsa_flush(): stop");
1935 void
1936 ospf_external_lsa_refresh_default (struct ospf *ospf)
1938 struct prefix_ipv4 p;
1939 struct external_info *ei;
1940 struct ospf_lsa *lsa;
1942 p.family = AF_INET;
1943 p.prefixlen = 0;
1944 p.prefix.s_addr = 0;
1946 ei = ospf_default_external_info (ospf);
1947 lsa = ospf_external_info_find_lsa (ospf, &p);
1949 if (ei)
1951 if (lsa)
1953 if (IS_DEBUG_OSPF_EVENT)
1954 zlog_info ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p", lsa);
1955 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
1957 else
1959 if (IS_DEBUG_OSPF_EVENT)
1960 zlog_info ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
1961 ospf_external_lsa_originate (ospf, ei);
1964 else
1966 if (lsa)
1968 if (IS_DEBUG_OSPF_EVENT)
1969 zlog_info ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
1970 ospf_lsa_flush_as (ospf, lsa);
1975 void
1976 ospf_external_lsa_refresh_type (struct ospf *ospf, u_char type, int force)
1978 struct route_node *rn;
1979 struct external_info *ei;
1981 if (type != DEFAULT_ROUTE)
1982 if (EXTERNAL_INFO(type))
1983 /* Refresh each redistributed AS-external-LSAs. */
1984 for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
1985 if ((ei = rn->info))
1986 if (!is_prefix_default (&ei->p))
1988 struct ospf_lsa *lsa;
1990 if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
1991 ospf_external_lsa_refresh (ospf, lsa, ei, force);
1992 else
1993 ospf_external_lsa_originate (ospf, ei);
1997 /* Refresh AS-external-LSA. */
1998 void
1999 ospf_external_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa,
2000 struct external_info *ei, int force)
2002 struct ospf_lsa *new;
2003 int changed;
2005 /* Check the AS-external-LSA should be originated. */
2006 if (!ospf_redistribute_check (ospf, ei, &changed))
2008 ospf_external_lsa_flush (ospf, ei->type, &ei->p,
2009 ei->ifindex, ei->nexthop);
2010 return;
2013 if (!changed && !force)
2014 return;
2016 /* Delete LSA from neighbor retransmit-list. */
2017 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2019 /* Unregister AS-external-LSA from refresh-list. */
2020 ospf_refresher_unregister_lsa (ospf, lsa);
2022 new = ospf_external_lsa_new (ospf, ei, &lsa->data->id);
2024 if (new == NULL)
2026 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2027 zlog_warn ("LSA[Type%d:%s]: Could not be refreshed", lsa->data->type,
2028 inet_ntoa (lsa->data->id));
2029 return;
2032 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
2034 /* Record timestamp. */
2035 gettimeofday (&new->tv_orig, NULL);
2037 /* Re-calculate checksum. */
2038 ospf_lsa_checksum (new->data);
2040 ospf_lsa_install (ospf, NULL, new); /* As type-5. */
2042 /* Flood LSA through AS. */
2043 ospf_flood_through_as (ospf, NULL, new);
2045 #ifdef HAVE_NSSA
2046 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2047 if (ospf->anyNSSA)
2048 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood per new rules */
2049 #endif /* HAVE_NSSA */
2051 /* Register slef-originated LSA to refresh queue. */
2052 ospf_refresher_register_lsa (ospf, new);
2054 /* Debug logging. */
2055 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2057 zlog_info ("LSA[Type%d:%s]: AS-external-LSA refresh",
2058 new->data->type, inet_ntoa (new->data->id));
2059 ospf_lsa_header_dump (new->data);
2062 return;
2066 /* LSA installation functions. */
2068 /* Install router-LSA to an area. */
2069 struct ospf_lsa *
2070 ospf_router_lsa_install (struct ospf *ospf,
2071 struct ospf_lsa *new, int rt_recalc)
2073 struct ospf_area *area = new->area;
2075 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2076 The entire routing table must be recalculated, starting with
2077 the shortest path calculations for each area (not just the
2078 area whose link-state database has changed).
2080 if (rt_recalc)
2081 ospf_spf_calculate_schedule (ospf);
2083 if (IS_LSA_SELF (new))
2085 /* Set router-LSA refresh timer. */
2086 OSPF_TIMER_OFF (area->t_router_lsa_self);
2087 OSPF_AREA_TIMER_ON (area->t_router_lsa_self,
2088 ospf_router_lsa_timer, OSPF_LS_REFRESH_TIME);
2090 /* Set self-originated router-LSA. */
2091 ospf_lsa_unlock (area->router_lsa_self);
2092 area->router_lsa_self = ospf_lsa_lock (new);
2094 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2095 zlog_info("LSA[Type%d]: ID %s is self-originated",
2096 new->data->type, inet_ntoa (new->data->id));
2099 return new;
2102 #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
2103 if (!(T)) \
2104 (T) = thread_add_timer (master, (F), oi, (V))
2106 /* Install network-LSA to an area. */
2107 struct ospf_lsa *
2108 ospf_network_lsa_install (struct ospf *ospf,
2109 struct ospf_interface *oi,
2110 struct ospf_lsa *new,
2111 int rt_recalc)
2114 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2115 The entire routing table must be recalculated, starting with
2116 the shortest path calculations for each area (not just the
2117 area whose link-state database has changed).
2119 if (rt_recalc)
2120 ospf_spf_calculate_schedule (ospf);
2122 /* We supposed that when LSA is originated by us, we pass the int
2123 for which it was originated. If LSA was received by flooding,
2124 the RECEIVED flag is set, so we do not link the LSA to the int. */
2125 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
2127 /* Set LSRefresh timer. */
2128 OSPF_TIMER_OFF (oi->t_network_lsa_self);
2130 OSPF_INTERFACE_TIMER_ON (oi->t_network_lsa_self,
2131 ospf_network_lsa_refresh_timer,
2132 OSPF_LS_REFRESH_TIME);
2134 ospf_lsa_unlock (oi->network_lsa_self);
2135 oi->network_lsa_self = ospf_lsa_lock (new);
2138 return new;
2141 /* Install summary-LSA to an area. */
2142 struct ospf_lsa *
2143 ospf_summary_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2144 int rt_recalc)
2146 if (rt_recalc && !IS_LSA_SELF (new))
2148 /* RFC 2328 Section 13.2 Summary-LSAs
2149 The best route to the destination described by the summary-
2150 LSA must be recalculated (see Section 16.5). If this
2151 destination is an AS boundary router, it may also be
2152 necessary to re-examine all the AS-external-LSAs.
2154 ospf_spf_calculate_schedule (ospf);
2156 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2157 zlog_info ("ospf_summary_lsa_install(): SPF scheduled");
2160 if (IS_LSA_SELF (new))
2161 ospf_refresher_register_lsa (ospf, new);
2163 return new;
2166 /* Install ASBR-summary-LSA to an area. */
2167 struct ospf_lsa *
2168 ospf_summary_asbr_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2169 int rt_recalc)
2171 if (rt_recalc && !IS_LSA_SELF (new))
2173 /* RFC 2328 Section 13.2 Summary-LSAs
2174 The best route to the destination described by the summary-
2175 LSA must be recalculated (see Section 16.5). If this
2176 destination is an AS boundary router, it may also be
2177 necessary to re-examine all the AS-external-LSAs.
2179 ospf_spf_calculate_schedule (ospf);
2182 /* register LSA to refresh-list. */
2183 if (IS_LSA_SELF (new))
2184 ospf_refresher_register_lsa (ospf, new);
2186 return new;
2189 /* Install AS-external-LSA. */
2190 struct ospf_lsa *
2191 ospf_external_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2192 int rt_recalc)
2194 ospf_ase_register_external_lsa (new, ospf);
2195 /* If LSA is not self-originated, calculate an external route. */
2196 if (rt_recalc)
2198 /* RFC 2328 Section 13.2 AS-external-LSAs
2199 The best route to the destination described by the AS-
2200 external-LSA must be recalculated (see Section 16.6).
2203 if (!IS_LSA_SELF (new))
2204 ospf_ase_incremental_update (ospf, new);
2207 #ifdef HAVE_NSSA
2208 /* There is no point to register selforiginate Type-7 LSA for
2209 * refreshing. We rely on refreshing Type-5 LSA's */
2210 if (IS_LSA_SELF (new) && (new->data->type == OSPF_AS_NSSA_LSA))
2211 return new;
2212 #endif /* HAVE_NSSA */
2214 /* Register self-originated LSA to refresh queue. */
2215 if (IS_LSA_SELF (new))
2216 ospf_refresher_register_lsa (ospf, new);
2218 return new;
2221 void
2222 ospf_discard_from_db (struct ospf *ospf,
2223 struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
2225 struct ospf_lsa *old;
2227 old = ospf_lsdb_lookup (lsdb, lsa);
2229 if (!old)
2230 return;
2232 if (old->refresh_list >= 0)
2233 ospf_refresher_unregister_lsa (ospf, old);
2235 switch (old->data->type)
2237 case OSPF_AS_EXTERNAL_LSA:
2238 #ifdef HAVE_OPAQUE_LSA
2239 case OSPF_OPAQUE_AS_LSA:
2240 #endif /* HAVE_OPAQUE_LSA */
2241 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2242 ospf_ase_unregister_external_lsa (old, ospf);
2243 break;
2244 #ifdef HAVE_NSSA
2245 case OSPF_AS_NSSA_LSA:
2246 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2247 ospf_ase_unregister_external_lsa (old, ospf);
2248 break;
2249 #endif /* HAVE_NSSA */
2250 default:
2251 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2252 break;
2255 ospf_lsa_maxage_delete (ospf, old);
2256 ospf_lsa_discard (old);
2259 struct ospf_lsa *
2260 ospf_lsa_install (struct ospf *ospf, struct ospf_interface *oi,
2261 struct ospf_lsa *lsa)
2263 struct ospf_lsa *new = NULL;
2264 struct ospf_lsa *old = NULL;
2265 struct ospf_lsdb *lsdb = NULL;
2266 int rt_recalc;
2268 /* Set LSDB. */
2269 switch (lsa->data->type)
2271 #ifdef HAVE_NSSA
2272 /* kevinm */
2273 case OSPF_AS_NSSA_LSA:
2274 if (lsa->area)
2275 lsdb = lsa->area->lsdb;
2276 else
2277 lsdb = ospf->lsdb;
2278 break;
2279 #endif /* HAVE_NSSA */
2280 case OSPF_AS_EXTERNAL_LSA:
2281 #ifdef HAVE_OPAQUE_LSA
2282 case OSPF_OPAQUE_AS_LSA:
2283 #endif /* HAVE_OPAQUE_LSA */
2284 lsdb = ospf->lsdb;
2285 break;
2286 default:
2287 lsdb = lsa->area->lsdb;
2288 break;
2291 assert (lsdb);
2293 /* RFC 2328 13.2. Installing LSAs in the database
2295 Installing a new LSA in the database, either as the result of
2296 flooding or a newly self-originated LSA, may cause the OSPF
2297 routing table structure to be recalculated. The contents of the
2298 new LSA should be compared to the old instance, if present. If
2299 there is no difference, there is no need to recalculate the
2300 routing table. When comparing an LSA to its previous instance,
2301 the following are all considered to be differences in contents:
2303 o The LSA's Options field has changed.
2305 o One of the LSA instances has LS age set to MaxAge, and
2306 the other does not.
2308 o The length field in the LSA header has changed.
2310 o The body of the LSA (i.e., anything outside the 20-byte
2311 LSA header) has changed. Note that this excludes changes
2312 in LS Sequence Number and LS Checksum.
2315 /* Look up old LSA and determine if any SPF calculation or incremental
2316 update is needed */
2317 old = ospf_lsdb_lookup (lsdb, lsa);
2319 /* Do comparision and record if recalc needed. */
2320 rt_recalc = 0;
2321 if ( old == NULL || ospf_lsa_different(old, lsa))
2322 rt_recalc = 1;
2324 /* discard old LSA from LSDB */
2325 if (old != NULL)
2326 ospf_discard_from_db (ospf, lsdb, lsa);
2328 /* Insert LSA to LSDB. */
2329 ospf_lsdb_add (lsdb, lsa);
2330 lsa->lsdb = lsdb;
2332 /* Calculate Checksum if self-originated?. */
2333 if (IS_LSA_SELF (lsa))
2334 ospf_lsa_checksum (lsa->data);
2336 /* Do LSA specific installation process. */
2337 switch (lsa->data->type)
2339 case OSPF_ROUTER_LSA:
2340 new = ospf_router_lsa_install (ospf, lsa, rt_recalc);
2341 break;
2342 case OSPF_NETWORK_LSA:
2343 assert (oi);
2344 new = ospf_network_lsa_install (ospf, oi, lsa, rt_recalc);
2345 break;
2346 case OSPF_SUMMARY_LSA:
2347 new = ospf_summary_lsa_install (ospf, lsa, rt_recalc);
2348 break;
2349 case OSPF_ASBR_SUMMARY_LSA:
2350 new = ospf_summary_asbr_lsa_install (ospf, lsa, rt_recalc);
2351 break;
2352 case OSPF_AS_EXTERNAL_LSA:
2353 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2354 break;
2355 #ifdef HAVE_OPAQUE_LSA
2356 case OSPF_OPAQUE_LINK_LSA:
2357 if (IS_LSA_SELF (lsa))
2358 lsa->oi = oi; /* Specify outgoing ospf-interface for this LSA. */
2359 else
2360 ; /* Incoming "oi" for this LSA has set at LSUpd reception. */
2361 /* Fallthrough */
2362 case OSPF_OPAQUE_AREA_LSA:
2363 case OSPF_OPAQUE_AS_LSA:
2364 new = ospf_opaque_lsa_install (lsa, rt_recalc);
2365 break;
2366 #endif /* HAVE_OPAQUE_LSA */
2367 default: /* NSSA, or type-6,8,9....nothing special */
2368 #ifdef HAVE_NSSA
2369 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2370 #endif /* HAVE_NSSA */
2371 break;
2374 if (new == NULL)
2375 return new; /* Installation failed, cannot proceed further -- endo. */
2377 /* Debug logs. */
2378 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2380 char area_str[INET_ADDRSTRLEN];
2382 switch (lsa->data->type)
2384 case OSPF_AS_EXTERNAL_LSA:
2385 #ifdef HAVE_OPAQUE_LSA
2386 case OSPF_OPAQUE_AS_LSA:
2387 #endif /* HAVE_OPAQUE_LSA */
2388 #ifdef HAVE_NSSA
2389 case OSPF_AS_NSSA_LSA:
2390 #endif /* HAVE_NSSA */
2391 zlog_info ("LSA[%s]: Install %s",
2392 dump_lsa_key (new),
2393 LOOKUP (ospf_lsa_type_msg, new->data->type));
2394 break;
2395 default:
2396 strcpy (area_str, inet_ntoa (new->area->area_id));
2397 zlog_info ("LSA[%s]: Install %s to Area %s",
2398 dump_lsa_key (new),
2399 LOOKUP (ospf_lsa_type_msg, new->data->type), area_str);
2400 break;
2404 /* If received LSA' ls_age is MaxAge, set LSA on MaxAge LSA list. */
2405 if (IS_LSA_MAXAGE (new) && !IS_LSA_SELF (new))
2407 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2408 zlog_info ("LSA[Type%d:%s]: Install LSA, MaxAge",
2409 new->data->type, inet_ntoa (new->data->id));
2410 ospf_lsa_maxage (ospf, lsa);
2413 return new;
2418 ospf_check_nbr_status (struct ospf *ospf)
2420 listnode node;
2422 for (node = listhead (ospf->oiflist); node; node = nextnode (node))
2424 struct ospf_interface *oi = getdata (node);
2425 struct route_node *rn;
2426 struct ospf_neighbor *nbr;
2428 if (ospf_if_is_enable (oi))
2429 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
2430 if ((nbr = rn->info) != NULL)
2431 if (nbr->state == NSM_Exchange || nbr->state == NSM_Loading)
2433 route_unlock_node (rn);
2434 return 0;
2438 return 1;
2443 ospf_maxage_lsa_remover (struct thread *thread)
2445 struct ospf *ospf = THREAD_ARG (thread);
2446 listnode node;
2447 listnode next;
2448 int reschedule = 0;
2450 ospf->t_maxage = NULL;
2452 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2453 zlog_info ("LSA[MaxAge]: remover Start");
2455 reschedule = !ospf_check_nbr_status (ospf);
2457 if (!reschedule)
2458 for (node = listhead (ospf->maxage_lsa); node; node = next)
2460 struct ospf_lsa *lsa = getdata (node);
2461 next = node->next;
2463 if (lsa->retransmit_counter > 0)
2465 reschedule = 1;
2466 continue;
2469 /* Remove LSA from the LSDB */
2470 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF))
2471 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2472 zlog_info ("LSA[Type%d:%s]: This LSA is self-originated: ",
2473 lsa->data->type, inet_ntoa (lsa->data->id));
2475 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2476 zlog_info ("LSA[Type%d:%s]: MaxAge LSA removed from list",
2477 lsa->data->type, inet_ntoa (lsa->data->id));
2479 /* Flood max age LSA. */
2480 ospf_flood_through (ospf, NULL, lsa);
2482 /* Remove from lsdb. */
2483 ospf_discard_from_db (ospf, lsa->lsdb, lsa);
2484 ospf_lsdb_delete (lsa->lsdb, lsa);
2487 /* A MaxAge LSA must be removed immediately from the router's link
2488 state database as soon as both a) it is no longer contained on any
2489 neighbor Link state retransmission lists and b) none of the router's
2490 neighbors are in states Exchange or Loading. */
2491 if (reschedule)
2492 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 2);
2494 return 0;
2498 ospf_lsa_maxage_exist (struct ospf *ospf, struct ospf_lsa *new)
2500 listnode node;
2502 for (node = listhead (ospf->maxage_lsa); node; nextnode (node))
2503 if (((struct ospf_lsa *) node->data) == new)
2504 return 1;
2506 return 0;
2509 void
2510 ospf_lsa_maxage_delete (struct ospf *ospf, struct ospf_lsa *lsa)
2512 listnode n;
2514 if ((n = listnode_lookup (ospf->maxage_lsa, lsa)))
2516 list_delete_node (ospf->maxage_lsa, n);
2517 ospf_lsa_unlock (lsa);
2521 void
2522 ospf_lsa_maxage (struct ospf *ospf, struct ospf_lsa *lsa)
2524 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2525 and schedule the MaxAge LSA remover. */
2526 if (ospf_lsa_maxage_exist (ospf, lsa))
2528 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2529 zlog_info ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2530 lsa->data->type, inet_ntoa (lsa->data->id), lsa);
2531 return;
2534 listnode_add (ospf->maxage_lsa, ospf_lsa_lock (lsa));
2536 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2537 zlog_info ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa));
2539 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 2);
2543 ospf_lsa_maxage_walker_remover (struct ospf *ospf, struct ospf_lsa *lsa)
2545 #ifdef HAVE_NSSA
2546 /* Stay away from any Local Translated Type-7 LSAs */
2547 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
2548 return 0;
2549 #endif /* HAVE_NSSA */
2551 if (IS_LSA_MAXAGE (lsa))
2552 /* Self-originated LSAs should NOT time-out instead,
2553 they're flushed and submitted to the max_age list explicitly. */
2554 if (!ospf_lsa_is_self_originated (ospf, lsa))
2556 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2557 zlog_info("LSA[%s]: is MaxAge", dump_lsa_key (lsa));
2559 switch (lsa->data->type)
2561 #ifdef HAVE_OPAQUE_LSA
2562 case OSPF_OPAQUE_LINK_LSA:
2563 case OSPF_OPAQUE_AREA_LSA:
2564 case OSPF_OPAQUE_AS_LSA:
2566 * As a general rule, whenever network topology has changed
2567 * (due to an LSA removal in this case), routing recalculation
2568 * should be triggered. However, this is not true for opaque
2569 * LSAs. Even if an opaque LSA instance is going to be removed
2570 * from the routing domain, it does not mean a change in network
2571 * topology, and thus, routing recalculation is not needed here.
2573 break;
2574 #endif /* HAVE_OPAQUE_LSA */
2575 case OSPF_AS_EXTERNAL_LSA:
2576 #ifdef HAVE_NSSA
2577 case OSPF_AS_NSSA_LSA:
2578 #endif /* HAVE_NSSA */
2579 ospf_ase_incremental_update (ospf, lsa);
2580 break;
2581 default:
2582 ospf_spf_calculate_schedule (ospf);
2583 break;
2586 ospf_lsa_maxage (ospf, lsa);
2589 return 0;
2592 /* Periodical check of MaxAge LSA. */
2594 ospf_lsa_maxage_walker (struct thread *thread)
2596 struct ospf *ospf = THREAD_ARG (thread);
2597 struct route_node *rn;
2598 struct ospf_lsa *lsa;
2599 listnode node;
2601 ospf->t_maxage_walker = NULL;
2603 for (node = listhead (ospf->areas); node; nextnode (node))
2605 struct ospf_area *area = node->data;
2607 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
2608 ospf_lsa_maxage_walker_remover (ospf, lsa);
2609 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
2610 ospf_lsa_maxage_walker_remover (ospf, lsa);
2611 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
2612 ospf_lsa_maxage_walker_remover (ospf, lsa);
2613 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
2614 ospf_lsa_maxage_walker_remover (ospf, lsa);
2615 #ifdef HAVE_OPAQUE_LSA
2616 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
2617 ospf_lsa_maxage_walker_remover (ospf, lsa);
2618 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
2619 ospf_lsa_maxage_walker_remover (ospf, lsa);
2620 #endif /* HAVE_OPAQUE_LSA */
2621 #ifdef HAVE_NSSA
2622 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
2623 ospf_lsa_maxage_walker_remover (ospf, lsa);
2624 #endif /* HAVE_NSSA */
2628 /* for AS-eternal-LSAs. */
2629 if (ospf->lsdb)
2631 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
2632 ospf_lsa_maxage_walker_remover (ospf, lsa);
2633 #ifdef HAVE_OPAQUE_LSA
2634 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
2635 ospf_lsa_maxage_walker_remover (ospf, lsa);
2636 #endif /* HAVE_OPAQUE_LSA */
2639 OSPF_TIMER_ON (ospf->t_maxage_walker, ospf_lsa_maxage_walker,
2640 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
2641 return 0;
2644 struct ospf_lsa *
2645 ospf_lsa_lookup_by_prefix (struct ospf_lsdb *lsdb, u_char type,
2646 struct prefix_ipv4 *p, struct in_addr router_id)
2648 struct ospf_lsa *lsa;
2649 struct in_addr mask, id;
2650 struct lsa_header_mask
2652 struct lsa_header header;
2653 struct in_addr mask;
2654 } *hmask;
2656 lsa = ospf_lsdb_lookup_by_id (lsdb, type, p->prefix, router_id);
2657 if (lsa == NULL)
2658 return NULL;
2660 masklen2ip (p->prefixlen, &mask);
2662 hmask = (struct lsa_header_mask *) lsa->data;
2664 if (mask.s_addr != hmask->mask.s_addr)
2666 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
2667 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, router_id);
2668 if (!lsa)
2669 return NULL;
2672 return lsa;
2675 struct ospf_lsa *
2676 ospf_lsa_lookup (struct ospf_area *area, u_int32_t type,
2677 struct in_addr id, struct in_addr adv_router)
2679 switch (type)
2681 case OSPF_ROUTER_LSA:
2682 case OSPF_NETWORK_LSA:
2683 case OSPF_SUMMARY_LSA:
2684 case OSPF_ASBR_SUMMARY_LSA:
2685 #ifdef HAVE_NSSA
2686 case OSPF_AS_NSSA_LSA:
2687 #endif /* HAVE_NSSA */
2688 #ifdef HAVE_OPAQUE_LSA
2689 case OSPF_OPAQUE_LINK_LSA:
2690 case OSPF_OPAQUE_AREA_LSA:
2691 #endif /* HAVE_OPAQUE_LSA */
2692 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, adv_router);
2693 break;
2694 case OSPF_AS_EXTERNAL_LSA:
2695 #ifdef HAVE_OPAQUE_LSA
2696 case OSPF_OPAQUE_AS_LSA:
2697 #endif /* HAVE_OPAQUE_LSA */
2698 return ospf_lsdb_lookup_by_id (area->ospf->lsdb, type, id, adv_router);
2699 break;
2700 default:
2701 break;
2704 return NULL;
2707 struct ospf_lsa *
2708 ospf_lsa_lookup_by_id (struct ospf_area *area, u_int32_t type,
2709 struct in_addr id)
2711 struct ospf_lsa *lsa;
2712 struct route_node *rn;
2714 switch (type)
2716 case OSPF_ROUTER_LSA:
2717 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
2718 break;
2719 case OSPF_NETWORK_LSA:
2720 for (rn = route_top (NETWORK_LSDB (area)); rn; rn = route_next (rn))
2721 if ((lsa = rn->info))
2722 if (IPV4_ADDR_SAME (&lsa->data->id, &id))
2724 route_unlock_node (rn);
2725 return lsa;
2727 break;
2728 case OSPF_SUMMARY_LSA:
2729 case OSPF_ASBR_SUMMARY_LSA:
2730 /* Currently not used. */
2731 assert (1);
2732 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
2733 break;
2734 case OSPF_AS_EXTERNAL_LSA:
2735 #ifdef HAVE_OPAQUE_LSA
2736 case OSPF_OPAQUE_LINK_LSA:
2737 case OSPF_OPAQUE_AREA_LSA:
2738 case OSPF_OPAQUE_AS_LSA:
2739 /* Currently not used. */
2740 break;
2741 #endif /* HAVE_OPAQUE_LSA */
2742 default:
2743 break;
2746 return NULL;
2749 struct ospf_lsa *
2750 ospf_lsa_lookup_by_header (struct ospf_area *area, struct lsa_header *lsah)
2752 struct ospf_lsa *match;
2754 #ifdef HAVE_OPAQUE_LSA
2756 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
2757 * is redefined to have two subfields; opaque-type and opaque-id.
2758 * However, it is harmless to treat the two sub fields together, as if
2759 * they two were forming a unique LSA-ID.
2761 #endif /* HAVE_OPAQUE_LSA */
2763 match = ospf_lsa_lookup (area, lsah->type, lsah->id, lsah->adv_router);
2765 if (match == NULL)
2766 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
2767 zlog_info ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
2768 lsah->type, inet_ntoa (lsah->id));
2770 return match;
2773 /* return +n, l1 is more recent.
2774 return -n, l2 is more recent.
2775 return 0, l1 and l2 is identical. */
2777 ospf_lsa_more_recent (struct ospf_lsa *l1, struct ospf_lsa *l2)
2779 int r;
2780 int x, y;
2782 if (l1 == NULL && l2 == NULL)
2783 return 0;
2784 if (l1 == NULL)
2785 return -1;
2786 if (l2 == NULL)
2787 return 1;
2789 /* compare LS sequence number. */
2790 x = (int) ntohl (l1->data->ls_seqnum);
2791 y = (int) ntohl (l2->data->ls_seqnum);
2792 if (x > y)
2793 return 1;
2794 if (x < y)
2795 return -1;
2797 /* compare LS checksum. */
2798 r = ntohs (l1->data->checksum) - ntohs (l2->data->checksum);
2799 if (r)
2800 return r;
2802 /* compare LS age. */
2803 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
2804 return 1;
2805 else if (!IS_LSA_MAXAGE (l1) && IS_LSA_MAXAGE (l2))
2806 return -1;
2808 /* compare LS age with MaxAgeDiff. */
2809 if (LS_AGE (l1) - LS_AGE (l2) > OSPF_LSA_MAXAGE_DIFF)
2810 return -1;
2811 else if (LS_AGE (l2) - LS_AGE (l1) > OSPF_LSA_MAXAGE_DIFF)
2812 return 1;
2814 /* LSAs are identical. */
2815 return 0;
2818 /* If two LSAs are different, return 1, otherwise return 0. */
2820 ospf_lsa_different (struct ospf_lsa *l1, struct ospf_lsa *l2)
2822 char *p1, *p2;
2823 assert (l1);
2824 assert (l2);
2825 assert (l1->data);
2826 assert (l2->data);
2828 if (l1->data->options != l2->data->options)
2829 return 1;
2831 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
2832 return 1;
2834 if (IS_LSA_MAXAGE (l2) && !IS_LSA_MAXAGE (l1))
2835 return 1;
2837 if (l1->data->length != l2->data->length)
2838 return 1;
2840 if (l1->data->length == 0)
2841 return 1;
2843 assert (ntohs (l1->data->length) > OSPF_LSA_HEADER_SIZE);
2845 p1 = (char *) l1->data;
2846 p2 = (char *) l2->data;
2848 if (memcmp (p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
2849 ntohs (l1->data->length) - OSPF_LSA_HEADER_SIZE) != 0)
2850 return 1;
2852 return 0;
2855 static int
2856 ospf_lsa_flush_schedule (struct ospf *ospf, struct ospf_lsa *lsa)
2858 if (lsa == NULL || !IS_LSA_SELF (lsa))
2859 return 0;
2861 if (IS_DEBUG_OSPF_EVENT)
2862 zlog_info ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
2864 /* Force given lsa's age to MaxAge. */
2865 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
2867 switch (lsa->data->type)
2869 #ifdef HAVE_OPAQUE_LSA
2870 case OSPF_OPAQUE_LINK_LSA:
2871 case OSPF_OPAQUE_AREA_LSA:
2872 case OSPF_OPAQUE_AS_LSA:
2873 ospf_opaque_lsa_refresh (lsa);
2874 break;
2875 #endif /* HAVE_OPAQUE_LSA */
2876 default:
2877 ospf_lsa_maxage (ospf, lsa);
2878 break;
2881 return 0;
2884 void
2885 ospf_flush_self_originated_lsas_now (struct ospf *ospf)
2887 listnode n1, n2;
2888 struct ospf_area *area;
2889 struct ospf_interface *oi;
2890 struct ospf_lsa *lsa;
2891 struct route_node *rn;
2892 int need_to_flush_ase = 0;
2894 for (n1 = listhead (ospf->areas); n1; nextnode (n1))
2896 if ((area = getdata (n1)) == NULL)
2897 continue;
2899 if ((lsa = area->router_lsa_self) != NULL)
2901 if (IS_DEBUG_OSPF_EVENT)
2902 zlog_info ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
2904 ospf_lsa_flush_area (lsa, area);
2905 ospf_lsa_unlock (area->router_lsa_self);
2906 area->router_lsa_self = NULL;
2907 OSPF_TIMER_OFF (area->t_router_lsa_self);
2910 for (n2 = listhead (area->oiflist); n2; nextnode (n2))
2912 if ((oi = getdata (n2)) == NULL)
2913 continue;
2915 if ((lsa = oi->network_lsa_self) != NULL
2916 && oi->state == ISM_DR
2917 && oi->full_nbrs > 0)
2919 if (IS_DEBUG_OSPF_EVENT)
2920 zlog_info ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
2922 ospf_lsa_flush_area (oi->network_lsa_self, area);
2923 ospf_lsa_unlock (oi->network_lsa_self);
2924 oi->network_lsa_self = NULL;
2925 OSPF_TIMER_OFF (oi->t_network_lsa_self);
2928 if (oi->type != OSPF_IFTYPE_VIRTUALLINK
2929 && area->external_routing == OSPF_AREA_DEFAULT)
2930 need_to_flush_ase = 1;
2933 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
2934 ospf_lsa_flush_schedule (ospf, lsa);
2935 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
2936 ospf_lsa_flush_schedule (ospf, lsa);
2937 #ifdef HAVE_OPAQUE_LSA
2938 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
2939 ospf_lsa_flush_schedule (ospf, lsa);
2940 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
2941 ospf_lsa_flush_schedule (ospf, lsa);
2942 #endif /* HAVE_OPAQUE_LSA */
2945 if (need_to_flush_ase)
2947 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
2948 ospf_lsa_flush_schedule (ospf, lsa);
2949 #ifdef HAVE_OPAQUE_LSA
2950 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
2951 ospf_lsa_flush_schedule (ospf, lsa);
2952 #endif /* HAVE_OPAQUE_LSA */
2956 * Make sure that the MaxAge LSA remover is executed immediately,
2957 * without conflicting to other threads.
2959 if (ospf->t_maxage != NULL)
2961 OSPF_TIMER_OFF (ospf->t_maxage);
2962 thread_execute (master, ospf_maxage_lsa_remover, ospf, 0);
2965 return;
2968 /* If there is self-originated LSA, then return 1, otherwise return 0. */
2969 /* An interface-independent version of ospf_lsa_is_self_originated */
2970 int
2971 ospf_lsa_is_self_originated (struct ospf *ospf, struct ospf_lsa *lsa)
2973 listnode node;
2975 /* This LSA is already checked. */
2976 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED))
2977 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
2979 /* Make sure LSA is self-checked. */
2980 SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
2982 /* AdvRouter and Router ID is the same. */
2983 if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf->router_id))
2984 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
2986 /* LSA is router-LSA. */
2987 else if (lsa->data->type == OSPF_ROUTER_LSA &&
2988 IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
2989 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
2991 /* LSA is network-LSA. Compare Link ID with all interfaces. */
2992 else if (lsa->data->type == OSPF_NETWORK_LSA)
2993 for (node = listhead (ospf->oiflist); node; nextnode (node))
2995 struct ospf_interface *oi = getdata (node);
2997 /* Ignore virtual link. */
2998 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
2999 if (oi->address->family == AF_INET)
3000 if (IPV4_ADDR_SAME (&lsa->data->id, &oi->address->u.prefix4))
3002 /* to make it easier later */
3003 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3004 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3008 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3011 /* Get unique Link State ID. */
3012 struct in_addr
3013 ospf_lsa_unique_id (struct ospf *ospf,
3014 struct ospf_lsdb *lsdb, u_char type, struct prefix_ipv4 *p)
3016 struct ospf_lsa *lsa;
3017 struct in_addr mask, id;
3019 id = p->prefix;
3021 /* Check existence of LSA instance. */
3022 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, ospf->router_id);
3023 if (lsa)
3025 struct as_external_lsa *al = (struct as_external_lsa *) lsa->data;
3026 if (ip_masklen (al->mask) == p->prefixlen)
3028 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3029 zlog_warn ("ospf_lsa_unique_id(): "
3030 "Can't get Link State ID for %s/%d",
3031 inet_ntoa (p->prefix), p->prefixlen);
3032 /* id.s_addr = 0; */
3033 id.s_addr = 0xffffffff;
3034 return id;
3036 /* Masklen differs, then apply wildcard mask to Link State ID. */
3037 else
3039 masklen2ip (p->prefixlen, &mask);
3041 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3042 lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, type,
3043 id, ospf->router_id);
3044 if (lsa)
3046 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3047 zlog_warn ("ospf_lsa_unique_id(): "
3048 "Can't get Link State ID for %s/%d",
3049 inet_ntoa (p->prefix), p->prefixlen);
3050 /* id.s_addr = 0; */
3051 id.s_addr = 0xffffffff;
3052 return id;
3057 return id;
3061 #define LSA_ACTION_ORIGN_RTR 1
3062 #define LSA_ACTION_ORIGN_NET 2
3063 #define LSA_ACTION_FLOOD_AREA 3
3064 #define LSA_ACTION_FLOOD_AS 4
3065 #define LSA_ACTION_FLUSH_AREA 5
3066 #define LSA_ACTION_FLUSH_AS 6
3068 struct lsa_action
3070 u_char action;
3071 struct ospf_area *area;
3072 struct ospf_interface *oi;
3073 struct ospf_lsa *lsa;
3077 ospf_lsa_action (struct thread *t)
3079 struct lsa_action *data;
3080 struct ospf *ospf;
3082 ospf = ospf_lookup ();
3084 data = THREAD_ARG (t);
3086 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3087 zlog_info ("LSA[Action]: Performing scheduled LSA action: %d",
3088 data->action);
3090 switch (data->action)
3092 case LSA_ACTION_ORIGN_RTR:
3093 ospf_router_lsa_refresh (data->area->router_lsa_self);
3094 break;
3095 case LSA_ACTION_ORIGN_NET:
3096 ospf_network_lsa_originate (data->oi);
3097 break;
3098 case LSA_ACTION_FLOOD_AREA:
3099 ospf_flood_through_area (data->area, NULL, data->lsa);
3100 break;
3101 case LSA_ACTION_FLOOD_AS:
3102 ospf_flood_through_as (ospf, NULL, data->lsa);
3103 break;
3104 case LSA_ACTION_FLUSH_AREA:
3105 ospf_lsa_flush_area (data->lsa, data->area);
3106 break;
3107 case LSA_ACTION_FLUSH_AS:
3108 ospf_lsa_flush_as (ospf, data->lsa);
3109 break;
3112 ospf_lsa_unlock (data->lsa);
3113 XFREE (MTYPE_OSPF_MESSAGE, data);
3114 return 0;
3117 void
3118 ospf_schedule_lsa_flood_area (struct ospf_area *area, struct ospf_lsa *lsa)
3120 struct lsa_action *data;
3122 data = XMALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3123 memset (data, 0, sizeof (struct lsa_action));
3125 data->action = LSA_ACTION_FLOOD_AREA;
3126 data->area = area;
3127 data->lsa = ospf_lsa_lock (lsa);
3129 thread_add_event (master, ospf_lsa_action, data, 0);
3132 void
3133 ospf_schedule_lsa_flush_area (struct ospf_area *area, struct ospf_lsa *lsa)
3135 struct lsa_action *data;
3137 data = XMALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3138 memset (data, 0, sizeof (struct lsa_action));
3140 data->action = LSA_ACTION_FLUSH_AREA;
3141 data->area = area;
3142 data->lsa = ospf_lsa_lock (lsa);
3144 thread_add_event (master, ospf_lsa_action, data, 0);
3148 /* LSA Refreshment functions. */
3149 void
3150 ospf_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
3152 struct external_info *ei;
3153 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3155 switch (lsa->data->type)
3157 /* Router and Network LSAs are processed differently. */
3158 case OSPF_ROUTER_LSA:
3159 case OSPF_NETWORK_LSA:
3160 break;
3161 case OSPF_SUMMARY_LSA:
3162 ospf_summary_lsa_refresh (ospf, lsa);
3163 break;
3164 case OSPF_ASBR_SUMMARY_LSA:
3165 ospf_summary_asbr_lsa_refresh (ospf, lsa);
3166 break;
3167 case OSPF_AS_EXTERNAL_LSA:
3168 ei = ospf_external_info_check (lsa);
3169 if (ei)
3170 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
3171 else
3172 ospf_lsa_flush_as (ospf, lsa);
3173 break;
3174 #ifdef HAVE_OPAQUE_LSA
3175 case OSPF_OPAQUE_LINK_LSA:
3176 case OSPF_OPAQUE_AREA_LSA:
3177 case OSPF_OPAQUE_AS_LSA:
3178 ospf_opaque_lsa_refresh (lsa);
3179 break;
3180 #endif /* HAVE_OPAQUE_LSA */
3181 default:
3182 break;
3186 void
3187 ospf_refresher_register_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3189 u_int16_t index, current_index;
3191 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3193 if (lsa->refresh_list < 0)
3195 int delay;
3197 if (LS_AGE (lsa) == 0 &&
3198 ntohl (lsa->data->ls_seqnum) == OSPF_INITIAL_SEQUENCE_NUMBER)
3199 /* Randomize first update by OSPF_LS_REFRESH_SHIFT factor */
3200 delay = OSPF_LS_REFRESH_SHIFT + (random () % OSPF_LS_REFRESH_TIME);
3201 else
3202 /* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */
3203 delay = OSPF_LS_REFRESH_TIME - LS_AGE (lsa) - OSPF_LS_REFRESH_JITTER
3204 + (random () % (2*OSPF_LS_REFRESH_JITTER));
3206 if (delay < 0)
3207 delay = 0;
3209 current_index = ospf->lsa_refresh_queue.index +
3210 (time (NULL) - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
3212 index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY)
3213 % (OSPF_LSA_REFRESHER_SLOTS);
3215 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3216 zlog_info ("LSA[Refresh]: lsa with age %d added to index %d",
3217 LS_AGE (lsa), index);
3218 if (!ospf->lsa_refresh_queue.qs[index])
3219 ospf->lsa_refresh_queue.qs[index] = list_new ();
3220 listnode_add (ospf->lsa_refresh_queue.qs[index], ospf_lsa_lock (lsa));
3221 lsa->refresh_list = index;
3225 void
3226 ospf_refresher_unregister_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3228 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3229 if (lsa->refresh_list >= 0)
3231 list refresh_list = ospf->lsa_refresh_queue.qs[lsa->refresh_list];
3232 listnode_delete (refresh_list, lsa);
3233 if (!listcount (refresh_list))
3235 list_free (refresh_list);
3236 ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
3238 ospf_lsa_unlock (lsa);
3239 lsa->refresh_list = -1;
3244 ospf_lsa_refresh_walker (struct thread *t)
3246 list refresh_list;
3247 listnode node;
3248 struct ospf *ospf = THREAD_ARG (t);
3249 int i;
3250 list lsa_to_refresh = list_new ();
3252 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3253 zlog_info ("LSA[Refresh]:ospf_lsa_refresh_walker(): start");
3256 i = ospf->lsa_refresh_queue.index;
3258 ospf->lsa_refresh_queue.index =
3259 (ospf->lsa_refresh_queue.index +
3260 (time (NULL) - ospf->lsa_refresher_started) / OSPF_LSA_REFRESHER_GRANULARITY)
3261 % OSPF_LSA_REFRESHER_SLOTS;
3263 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3264 zlog_info ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3265 ospf->lsa_refresh_queue.index);
3267 for (;i != ospf->lsa_refresh_queue.index;
3268 i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS)
3270 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3271 zlog_info ("LSA[Refresh]: ospf_lsa_refresh_walker(): refresh index %d", i);
3273 refresh_list = ospf->lsa_refresh_queue.qs [i];
3275 ospf->lsa_refresh_queue.qs [i] = NULL;
3277 if (refresh_list)
3279 for (node = listhead (refresh_list); node;)
3281 listnode next;
3282 struct ospf_lsa *lsa = getdata (node);
3283 next = node->next;
3285 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3286 zlog_info ("LSA[Refresh]: ospf_lsa_refresh_walker(): refresh lsa %p", lsa);
3288 list_delete_node (refresh_list, node);
3289 ospf_lsa_unlock (lsa);
3290 lsa->refresh_list = -1;
3291 listnode_add (lsa_to_refresh, lsa);
3292 node = next;
3294 list_free (refresh_list);
3298 ospf->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
3299 ospf, ospf->lsa_refresh_interval);
3300 ospf->lsa_refresher_started = time (NULL);
3302 for (node = listhead (lsa_to_refresh); node; nextnode (node))
3303 ospf_lsa_refresh (ospf, getdata (node));
3305 list_delete (lsa_to_refresh);
3307 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3308 zlog_info ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3310 return 0;