ospfd: Tighten up the connected check for redistribution
[jleu-quagga.git] / ospfd / ospf_lsa.c
blobe708d5e232c05f67585470ad9bf795dfc1e703e6
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() */
35 #include "checksum.h"
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_lsdb.h"
43 #include "ospfd/ospf_neighbor.h"
44 #include "ospfd/ospf_nsm.h"
45 #include "ospfd/ospf_flood.h"
46 #include "ospfd/ospf_packet.h"
47 #include "ospfd/ospf_spf.h"
48 #include "ospfd/ospf_dump.h"
49 #include "ospfd/ospf_route.h"
50 #include "ospfd/ospf_ase.h"
51 #include "ospfd/ospf_zebra.h"
54 u_int32_t
55 get_metric (u_char *metric)
57 u_int32_t m;
58 m = metric[0];
59 m = (m << 8) + metric[1];
60 m = (m << 8) + metric[2];
61 return m;
65 struct timeval
66 tv_adjust (struct timeval a)
68 while (a.tv_usec >= 1000000)
70 a.tv_usec -= 1000000;
71 a.tv_sec++;
74 while (a.tv_usec < 0)
76 a.tv_usec += 1000000;
77 a.tv_sec--;
80 return a;
83 int
84 tv_ceil (struct timeval a)
86 a = tv_adjust (a);
88 return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec);
91 int
92 tv_floor (struct timeval a)
94 a = tv_adjust (a);
96 return a.tv_sec;
99 struct timeval
100 int2tv (int a)
102 struct timeval ret;
104 ret.tv_sec = a;
105 ret.tv_usec = 0;
107 return ret;
110 struct timeval
111 tv_add (struct timeval a, struct timeval b)
113 struct timeval ret;
115 ret.tv_sec = a.tv_sec + b.tv_sec;
116 ret.tv_usec = a.tv_usec + b.tv_usec;
118 return tv_adjust (ret);
121 struct timeval
122 tv_sub (struct timeval a, struct timeval b)
124 struct timeval ret;
126 ret.tv_sec = a.tv_sec - b.tv_sec;
127 ret.tv_usec = a.tv_usec - b.tv_usec;
129 return tv_adjust (ret);
133 tv_cmp (struct timeval a, struct timeval b)
135 return (a.tv_sec == b.tv_sec ?
136 a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
140 ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
142 struct timeval delta, now;
143 int delay = 0;
145 quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
146 delta = tv_sub (now, lsa->tv_orig);
148 if (tv_cmp (delta, int2tv (OSPF_MIN_LS_INTERVAL)) < 0)
150 delay = tv_ceil (tv_sub (int2tv (OSPF_MIN_LS_INTERVAL), delta));
152 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
153 zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
154 lsa->data->type, inet_ntoa (lsa->data->id), delay);
156 assert (delay > 0);
159 return delay;
164 get_age (struct ospf_lsa *lsa)
166 int age;
168 age = ntohs (lsa->data->ls_age)
169 + tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
171 return age;
175 /* Fletcher Checksum -- Refer to RFC1008. */
177 /* All the offsets are zero-based. The offsets in the RFC1008 are
178 one-based. */
179 u_int16_t
180 ospf_lsa_checksum (struct lsa_header *lsa)
182 u_char *buffer = (u_char *) &lsa->options;
183 int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
185 /* Skip the AGE field */
186 u_int16_t len = ntohs(lsa->length) - options_offset;
188 /* Checksum offset starts from "options" field, not the beginning of the
189 lsa_header struct. The offset is 14, rather than 16. */
190 int checksum_offset = (u_char *) &lsa->checksum - buffer;
192 return fletcher_checksum(buffer, len, checksum_offset);
197 /* Create OSPF LSA. */
198 struct ospf_lsa *
199 ospf_lsa_new ()
201 struct ospf_lsa *new;
203 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
205 new->flags = 0;
206 new->lock = 1;
207 new->retransmit_counter = 0;
208 new->tv_recv = recent_relative_time ();
209 new->tv_orig = new->tv_recv;
210 new->refresh_list = -1;
212 return new;
215 /* Duplicate OSPF LSA. */
216 struct ospf_lsa *
217 ospf_lsa_dup (struct ospf_lsa *lsa)
219 struct ospf_lsa *new;
221 if (lsa == NULL)
222 return NULL;
224 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
226 memcpy (new, lsa, sizeof (struct ospf_lsa));
227 UNSET_FLAG (new->flags, OSPF_LSA_DISCARD);
228 new->lock = 1;
229 new->retransmit_counter = 0;
230 new->data = ospf_lsa_data_dup (lsa->data);
232 /* kevinm: Clear the refresh_list, otherwise there are going
233 to be problems when we try to remove the LSA from the
234 queue (which it's not a member of.)
235 XXX: Should we add the LSA to the refresh_list queue? */
236 new->refresh_list = -1;
238 if (IS_DEBUG_OSPF (lsa, LSA))
239 zlog_debug ("LSA: duplicated %p (new: %p)", lsa, new);
241 return new;
244 /* Free OSPF LSA. */
245 void
246 ospf_lsa_free (struct ospf_lsa *lsa)
248 assert (lsa->lock == 0);
250 if (IS_DEBUG_OSPF (lsa, LSA))
251 zlog_debug ("LSA: freed %p", lsa);
253 /* Delete LSA data. */
254 if (lsa->data != NULL)
255 ospf_lsa_data_free (lsa->data);
257 assert (lsa->refresh_list < 0);
259 memset (lsa, 0, sizeof (struct ospf_lsa));
260 XFREE (MTYPE_OSPF_LSA, lsa);
263 /* Lock LSA. */
264 struct ospf_lsa *
265 ospf_lsa_lock (struct ospf_lsa *lsa)
267 lsa->lock++;
268 return lsa;
271 /* Unlock LSA. */
272 void
273 ospf_lsa_unlock (struct ospf_lsa **lsa)
275 /* This is sanity check. */
276 if (!lsa || !*lsa)
277 return;
279 (*lsa)->lock--;
281 assert ((*lsa)->lock >= 0);
283 if ((*lsa)->lock == 0)
285 assert (CHECK_FLAG ((*lsa)->flags, OSPF_LSA_DISCARD));
286 ospf_lsa_free (*lsa);
287 *lsa = NULL;
291 /* Check discard flag. */
292 void
293 ospf_lsa_discard (struct ospf_lsa *lsa)
295 if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
297 SET_FLAG (lsa->flags, OSPF_LSA_DISCARD);
298 ospf_lsa_unlock (&lsa);
302 /* Create LSA data. */
303 struct lsa_header *
304 ospf_lsa_data_new (size_t size)
306 return XCALLOC (MTYPE_OSPF_LSA_DATA, size);
309 /* Duplicate LSA data. */
310 struct lsa_header *
311 ospf_lsa_data_dup (struct lsa_header *lsah)
313 struct lsa_header *new;
315 new = ospf_lsa_data_new (ntohs (lsah->length));
316 memcpy (new, lsah, ntohs (lsah->length));
318 return new;
321 /* Free LSA data. */
322 void
323 ospf_lsa_data_free (struct lsa_header *lsah)
325 if (IS_DEBUG_OSPF (lsa, LSA))
326 zlog_debug ("LSA[Type%d:%s]: data freed %p",
327 lsah->type, inet_ntoa (lsah->id), lsah);
329 XFREE (MTYPE_OSPF_LSA_DATA, lsah);
333 /* LSA general functions. */
335 const char *
336 dump_lsa_key (struct ospf_lsa *lsa)
338 static char buf[] = {
339 "Type255,id(255.255.255.255),ar(255.255.255.255)"
341 struct lsa_header *lsah;
343 if (lsa != NULL && (lsah = lsa->data) != NULL)
345 char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
346 strcpy (id, inet_ntoa (lsah->id));
347 strcpy (ar, inet_ntoa (lsah->adv_router));
349 sprintf (buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar);
351 else
352 strcpy (buf, "NULL");
354 return buf;
357 u_int32_t
358 lsa_seqnum_increment (struct ospf_lsa *lsa)
360 u_int32_t seqnum;
362 seqnum = ntohl (lsa->data->ls_seqnum) + 1;
364 return htonl (seqnum);
367 void
368 lsa_header_set (struct stream *s, u_char options,
369 u_char type, struct in_addr id, struct in_addr router_id)
371 struct lsa_header *lsah;
373 lsah = (struct lsa_header *) STREAM_DATA (s);
375 lsah->ls_age = htons (0);
376 lsah->options = options;
377 lsah->type = type;
378 lsah->id = id;
379 lsah->adv_router = router_id;
380 lsah->ls_seqnum = htonl (OSPF_INITIAL_SEQUENCE_NUMBER);
382 stream_forward_endp (s, OSPF_LSA_HEADER_SIZE);
386 /* router-LSA related functions. */
387 /* Get router-LSA flags. */
388 static u_char
389 router_lsa_flags (struct ospf_area *area)
391 u_char flags;
393 flags = area->ospf->flags;
395 /* Set virtual link flag. */
396 if (ospf_full_virtual_nbrs (area))
397 SET_FLAG (flags, ROUTER_LSA_VIRTUAL);
398 else
399 /* Just sanity check */
400 UNSET_FLAG (flags, ROUTER_LSA_VIRTUAL);
402 /* Set Shortcut ABR behabiour flag. */
403 UNSET_FLAG (flags, ROUTER_LSA_SHORTCUT);
404 if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
405 if (!OSPF_IS_AREA_BACKBONE (area))
406 if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
407 area->ospf->backbone == NULL) ||
408 area->shortcut_configured == OSPF_SHORTCUT_ENABLE)
409 SET_FLAG (flags, ROUTER_LSA_SHORTCUT);
411 /* ASBR can't exit in stub area. */
412 if (area->external_routing == OSPF_AREA_STUB)
413 UNSET_FLAG (flags, ROUTER_LSA_EXTERNAL);
414 /* If ASBR set External flag */
415 else if (IS_OSPF_ASBR (area->ospf))
416 SET_FLAG (flags, ROUTER_LSA_EXTERNAL);
418 /* Set ABR dependent flags */
419 if (IS_OSPF_ABR (area->ospf))
421 SET_FLAG (flags, ROUTER_LSA_BORDER);
422 /* If Area is NSSA and we are both ABR and unconditional translator,
423 * set Nt bit to inform other routers.
425 if ( (area->external_routing == OSPF_AREA_NSSA)
426 && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
427 SET_FLAG (flags, ROUTER_LSA_NT);
429 return flags;
432 /* Lookup neighbor other than myself.
433 And check neighbor count,
434 Point-to-Point link must have only 1 neighbor. */
435 struct ospf_neighbor *
436 ospf_nbr_lookup_ptop (struct ospf_interface *oi)
438 struct ospf_neighbor *nbr = NULL;
439 struct route_node *rn;
441 /* Search neighbor, there must be one of two nbrs. */
442 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
443 if ((nbr = rn->info))
444 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
445 if (nbr->state == NSM_Full)
447 route_unlock_node (rn);
448 break;
451 /* PtoP link must have only 1 neighbor. */
452 if (ospf_nbr_count (oi, 0) > 1)
453 zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
455 return nbr;
458 /* Determine cost of link, taking RFC3137 stub-router support into
459 * consideration
461 static u_int16_t
462 ospf_link_cost (struct ospf_interface *oi)
464 /* RFC3137 stub router support */
465 if (!CHECK_FLAG (oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
466 return oi->output_cost;
467 else
468 return OSPF_OUTPUT_COST_INFINITE;
471 /* Set a link information. */
472 static char
473 link_info_set (struct stream *s, struct in_addr id,
474 struct in_addr data, u_char type, u_char tos, u_int16_t cost)
476 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
477 * vast majority of cases. Some rare routers with lots of links need more.
478 * we try accomodate those here.
480 if (STREAM_WRITEABLE(s) < OSPF_ROUTER_LSA_LINK_SIZE)
482 size_t ret = OSPF_MAX_LSA_SIZE;
484 /* Can we enlarge the stream still? */
485 if (STREAM_SIZE(s) == OSPF_MAX_LSA_SIZE)
487 /* we futz the size here for simplicity, really we need to account
488 * for just:
489 * IP Header - (sizeof (struct ip))
490 * OSPF Header - OSPF_HEADER_SIZE
491 * LSA Header - OSPF_LSA_HEADER_SIZE
492 * MD5 auth data, if MD5 is configured - OSPF_AUTH_MD5_SIZE.
494 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
496 ret = stream_resize (s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
499 if (ret == OSPF_MAX_LSA_SIZE)
501 zlog_warn ("%s: Out of space in LSA stream, left %zd, size %zd",
502 __func__, STREAM_REMAIN (s), STREAM_SIZE (s));
503 return 0;
507 /* TOS based routing is not supported. */
508 stream_put_ipv4 (s, id.s_addr); /* Link ID. */
509 stream_put_ipv4 (s, data.s_addr); /* Link Data. */
510 stream_putc (s, type); /* Link Type. */
511 stream_putc (s, tos); /* TOS = 0. */
512 stream_putw (s, cost); /* Link Cost. */
514 return 1;
517 /* Describe Point-to-Point link (Section 12.4.1.1). */
518 static int
519 lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi)
521 int links = 0;
522 struct ospf_neighbor *nbr;
523 struct in_addr id, mask;
524 u_int16_t cost = ospf_link_cost (oi);
526 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
527 zlog_debug ("LSA[Type1]: Set link Point-to-Point");
529 if ((nbr = ospf_nbr_lookup_ptop (oi)))
530 if (nbr->state == NSM_Full)
532 /* For unnumbered point-to-point networks, the Link Data field
533 should specify the interface's MIB-II ifIndex value. */
534 links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
535 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
538 /* Regardless of the state of the neighboring router, we must
539 add a Type 3 link (stub network).
540 N.B. Options 1 & 2 share basically the same logic. */
541 masklen2ip (oi->address->prefixlen, &mask);
542 id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr & mask.s_addr;
543 links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
544 oi->output_cost);
545 return links;
548 /* Describe Broadcast Link. */
549 static int
550 lsa_link_broadcast_set (struct stream *s, struct ospf_interface *oi)
552 struct ospf_neighbor *dr;
553 struct in_addr id, mask;
554 u_int16_t cost = ospf_link_cost (oi);
556 /* Describe Type 3 Link. */
557 if (oi->state == ISM_Waiting)
559 masklen2ip (oi->address->prefixlen, &mask);
560 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
561 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
562 oi->output_cost);
565 dr = ospf_nbr_lookup_by_addr (oi->nbrs, &DR (oi));
566 /* Describe Type 2 link. */
567 if (dr && (dr->state == NSM_Full ||
568 IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))) &&
569 ospf_nbr_count (oi, NSM_Full) > 0)
571 return link_info_set (s, DR (oi), oi->address->u.prefix4,
572 LSA_LINK_TYPE_TRANSIT, 0, cost);
574 /* Describe type 3 link. */
575 else
577 masklen2ip (oi->address->prefixlen, &mask);
578 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
579 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
580 oi->output_cost);
584 static int
585 lsa_link_loopback_set (struct stream *s, struct ospf_interface *oi)
587 struct in_addr id, mask;
589 /* Describe Type 3 Link. */
590 if (oi->state != ISM_Loopback)
591 return 0;
593 mask.s_addr = 0xffffffff;
594 id.s_addr = oi->address->u.prefix4.s_addr;
595 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
598 /* Describe Virtual Link. */
599 static int
600 lsa_link_virtuallink_set (struct stream *s, struct ospf_interface *oi)
602 struct ospf_neighbor *nbr;
603 u_int16_t cost = ospf_link_cost (oi);
605 if (oi->state == ISM_PointToPoint)
606 if ((nbr = ospf_nbr_lookup_ptop (oi)))
607 if (nbr->state == NSM_Full)
609 return link_info_set (s, nbr->router_id, oi->address->u.prefix4,
610 LSA_LINK_TYPE_VIRTUALLINK, 0, cost);
613 return 0;
616 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
618 /* this function add for support point-to-multipoint ,see rfc2328
619 12.4.1.4.*/
620 /* from "edward rrr" <edward_rrr@hotmail.com>
621 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
622 static int
623 lsa_link_ptomp_set (struct stream *s, struct ospf_interface *oi)
625 int links = 0;
626 struct route_node *rn;
627 struct ospf_neighbor *nbr = NULL;
628 struct in_addr id, mask;
629 u_int16_t cost = ospf_link_cost (oi);
631 mask.s_addr = 0xffffffff;
632 id.s_addr = oi->address->u.prefix4.s_addr;
633 links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
635 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
636 zlog_debug ("PointToMultipoint: running ptomultip_set");
638 /* Search neighbor, */
639 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
640 if ((nbr = rn->info) != NULL)
641 /* Ignore myself. */
642 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
643 if (nbr->state == NSM_Full)
646 links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
647 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
648 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
649 zlog_debug ("PointToMultipoint: set link to %s",
650 inet_ntoa(oi->address->u.prefix4));
653 return links;
656 /* Set router-LSA link information. */
657 static int
658 router_lsa_link_set (struct stream *s, struct ospf_area *area)
660 struct listnode *node;
661 struct ospf_interface *oi;
662 int links = 0;
664 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
666 struct interface *ifp = oi->ifp;
668 /* Check interface is up, OSPF is enable. */
669 if (if_is_operative (ifp))
671 if (oi->state != ISM_Down)
673 /* Describe each link. */
674 switch (oi->type)
676 case OSPF_IFTYPE_POINTOPOINT:
677 links += lsa_link_ptop_set (s, oi);
678 break;
679 case OSPF_IFTYPE_BROADCAST:
680 links += lsa_link_broadcast_set (s, oi);
681 break;
682 case OSPF_IFTYPE_NBMA:
683 links += lsa_link_nbma_set (s, oi);
684 break;
685 case OSPF_IFTYPE_POINTOMULTIPOINT:
686 links += lsa_link_ptomp_set (s, oi);
687 break;
688 case OSPF_IFTYPE_VIRTUALLINK:
689 links += lsa_link_virtuallink_set (s, oi);
690 break;
691 case OSPF_IFTYPE_LOOPBACK:
692 links += lsa_link_loopback_set (s, oi);
698 return links;
701 /* Set router-LSA body. */
702 static void
703 ospf_router_lsa_body_set (struct stream *s, struct ospf_area *area)
705 unsigned long putp;
706 u_int16_t cnt;
708 /* Set flags. */
709 stream_putc (s, router_lsa_flags (area));
711 /* Set Zero fields. */
712 stream_putc (s, 0);
714 /* Keep pointer to # links. */
715 putp = stream_get_endp(s);
717 /* Forward word */
718 stream_putw(s, 0);
720 /* Set all link information. */
721 cnt = router_lsa_link_set (s, area);
723 /* Set # of links here. */
724 stream_putw_at (s, putp, cnt);
727 static int
728 ospf_stub_router_timer (struct thread *t)
730 struct ospf_area *area = THREAD_ARG (t);
732 area->t_stub_router = NULL;
734 SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
736 /* clear stub route state and generate router-lsa refresh, don't
737 * clobber an administratively set stub-router state though.
739 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
740 return 0;
742 UNSET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
744 ospf_router_lsa_timer_add (area);
746 return 0;
749 inline static void
750 ospf_stub_router_check (struct ospf_area *area)
752 /* area must either be administratively configured to be stub
753 * or startup-time stub-router must be configured and we must in a pre-stub
754 * state.
756 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
758 SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
759 return;
762 /* not admin-stubbed, check whether startup stubbing is configured and
763 * whether it's not been done yet
765 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED))
766 return;
768 if (area->ospf->stub_router_startup_time == OSPF_STUB_ROUTER_UNCONFIGURED)
770 /* stub-router is hence done forever for this area, even if someone
771 * tries configure it (take effect next restart).
773 SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
774 return;
777 /* startup stub-router configured and not yet done */
778 SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
780 OSPF_AREA_TIMER_ON (area->t_stub_router, ospf_stub_router_timer,
781 area->ospf->stub_router_startup_time);
784 /* Create new router-LSA. */
785 static struct ospf_lsa *
786 ospf_router_lsa_new (struct ospf_area *area)
788 struct ospf *ospf = area->ospf;
789 struct stream *s;
790 struct lsa_header *lsah;
791 struct ospf_lsa *new;
792 int length;
794 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
795 zlog_debug ("LSA[Type1]: Create router-LSA instance");
797 /* check whether stub-router is desired, and if this is the first
798 * router LSA.
800 ospf_stub_router_check (area);
802 /* Create a stream for LSA. */
803 s = stream_new (OSPF_MAX_LSA_SIZE);
804 /* Set LSA common header fields. */
805 lsa_header_set (s, LSA_OPTIONS_GET (area) | LSA_OPTIONS_NSSA_GET (area),
806 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
808 /* Set router-LSA body fields. */
809 ospf_router_lsa_body_set (s, area);
811 /* Set length. */
812 length = stream_get_endp (s);
813 lsah = (struct lsa_header *) STREAM_DATA (s);
814 lsah->length = htons (length);
816 /* Now, create OSPF LSA instance. */
817 if ( (new = ospf_lsa_new ()) == NULL)
819 zlog_err ("%s: Unable to create new lsa", __func__);
820 return NULL;
823 new->area = area;
824 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
826 /* Copy LSA data to store, discard stream. */
827 new->data = ospf_lsa_data_new (length);
828 memcpy (new->data, lsah, length);
829 stream_free (s);
831 return new;
834 /* Originate Router-LSA. */
835 static struct ospf_lsa *
836 ospf_router_lsa_originate (struct ospf_area *area)
838 struct ospf_lsa *new;
840 /* Create new router-LSA instance. */
841 if ( (new = ospf_router_lsa_new (area)) == NULL)
843 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
844 return NULL;
847 /* Sanity check. */
848 if (new->data->adv_router.s_addr == 0)
850 if (IS_DEBUG_OSPF_EVENT)
851 zlog_debug ("LSA[Type1]: AdvRouter is 0, discard");
852 ospf_lsa_discard (new);
853 return NULL;
856 /* Install LSA to LSDB. */
857 new = ospf_lsa_install (area->ospf, NULL, new);
859 /* Update LSA origination count. */
860 area->ospf->lsa_originate_count++;
862 /* Flooding new LSA through area. */
863 ospf_flood_through_area (area, NULL, new);
865 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
867 zlog_debug ("LSA[Type%d:%s]: Originate router-LSA %p",
868 new->data->type, inet_ntoa (new->data->id), new);
869 ospf_lsa_header_dump (new->data);
872 return new;
875 /* Refresh router-LSA. */
876 static struct ospf_lsa *
877 ospf_router_lsa_refresh (struct ospf_lsa *lsa)
879 struct ospf_area *area = lsa->area;
880 struct ospf_lsa *new;
882 /* Sanity check. */
883 assert (lsa->data);
885 /* Delete LSA from neighbor retransmit-list. */
886 ospf_ls_retransmit_delete_nbr_area (area, lsa);
888 /* Create new router-LSA instance. */
889 if ( (new = ospf_router_lsa_new (area)) == NULL)
891 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
892 return NULL;
895 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
897 ospf_lsa_install (area->ospf, NULL, new);
899 /* Flood LSA through area. */
900 ospf_flood_through_area (area, NULL, new);
902 /* Debug logging. */
903 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
905 zlog_debug ("LSA[Type%d:%s]: router-LSA refresh",
906 new->data->type, inet_ntoa (new->data->id));
907 ospf_lsa_header_dump (new->data);
910 return NULL;
913 static int
914 ospf_router_lsa_timer (struct thread *t)
916 struct ospf_area *area;
918 if (IS_DEBUG_OSPF_EVENT)
919 zlog_debug ("Timer[router-LSA]: (router-LSA Refresh expire)");
921 area = THREAD_ARG (t);
922 area->t_router_lsa_self = NULL;
924 /* Now refresh router-LSA. */
925 if (area->router_lsa_self)
926 ospf_router_lsa_refresh (area->router_lsa_self);
927 /* Newly originate router-LSA. */
928 else
929 ospf_router_lsa_originate (area);
931 return 0;
934 void
935 ospf_router_lsa_timer_add (struct ospf_area *area)
937 /* Keep area's self-originated router-LSA. */
938 struct ospf_lsa *lsa = area->router_lsa_self;
940 /* Cancel previously scheduled router-LSA timer. */
941 if (area->t_router_lsa_self)
942 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
943 zlog_debug ("LSA[Type1]: Cancel previous router-LSA timer");
945 OSPF_TIMER_OFF (area->t_router_lsa_self);
947 /* If router-LSA is originated previously, check the interval time. */
948 if (lsa)
950 int delay;
951 if ((delay = ospf_lsa_refresh_delay (lsa)) > 0)
953 OSPF_AREA_TIMER_ON (area->t_router_lsa_self,
954 ospf_router_lsa_timer, delay);
955 return;
959 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
960 zlog_debug ("LSA[Type1]: Scheduling router-LSA origination right away");
962 /* Immediately refresh router-LSA. */
963 OSPF_AREA_TIMER_ON (area->t_router_lsa_self, ospf_router_lsa_timer, 0);
967 ospf_router_lsa_update_timer (struct thread *thread)
969 struct ospf *ospf = THREAD_ARG (thread);
970 struct listnode *node, *nnode;
971 struct ospf_area *area;
973 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
974 zlog_debug ("Timer[router-LSA Update]: (timer expire)");
976 ospf->t_router_lsa_update = NULL;
978 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
980 struct ospf_lsa *lsa = area->router_lsa_self;
981 struct router_lsa *rl;
982 const char *area_str;
984 /* Keep Area ID string. */
985 area_str = AREA_NAME (area);
987 /* If LSA not exist in this Area, originate new. */
988 if (lsa == NULL)
990 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
991 zlog_debug("LSA[Type1]: Create router-LSA for Area %s", area_str);
993 ospf_router_lsa_originate (area);
995 /* If router-ID is changed, Link ID must change.
996 First flush old LSA, then originate new. */
997 else if (!IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
999 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1000 zlog_debug("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
1001 lsa->data->type, inet_ntoa (lsa->data->id), area_str);
1002 ospf_lsa_flush_area (lsa, area);
1003 ospf_lsa_unlock (&area->router_lsa_self);
1004 area->router_lsa_self = NULL;
1006 /* Refresh router-LSA, (not install) and flood through area. */
1007 ospf_router_lsa_timer_add (area);
1009 else
1011 rl = (struct router_lsa *) lsa->data;
1012 /* Refresh router-LSA, (not install) and flood through area. */
1013 if (rl->flags != ospf->flags)
1014 ospf_router_lsa_timer_add (area);
1018 return 0;
1022 /* network-LSA related functions. */
1023 /* Originate Network-LSA. */
1024 static void
1025 ospf_network_lsa_body_set (struct stream *s, struct ospf_interface *oi)
1027 struct in_addr mask;
1028 struct route_node *rn;
1029 struct ospf_neighbor *nbr;
1031 masklen2ip (oi->address->prefixlen, &mask);
1032 stream_put_ipv4 (s, mask.s_addr);
1034 /* The network-LSA lists those routers that are fully adjacent to
1035 the Designated Router; each fully adjacent router is identified by
1036 its OSPF Router ID. The Designated Router includes itself in this
1037 list. RFC2328, Section 12.4.2 */
1039 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
1040 if ((nbr = rn->info) != NULL)
1041 if (nbr->state == NSM_Full || nbr == oi->nbr_self)
1042 stream_put_ipv4 (s, nbr->router_id.s_addr);
1045 static struct ospf_lsa *
1046 ospf_network_lsa_new (struct ospf_interface *oi)
1048 struct stream *s;
1049 struct ospf_lsa *new;
1050 struct lsa_header *lsah;
1051 int length;
1053 /* If there are no neighbours on this network (the net is stub),
1054 the router does not originate network-LSA (see RFC 12.4.2) */
1055 if (oi->full_nbrs == 0)
1056 return NULL;
1058 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1059 zlog_debug ("LSA[Type2]: Create network-LSA instance");
1061 /* Create new stream for LSA. */
1062 s = stream_new (OSPF_MAX_LSA_SIZE);
1063 lsah = (struct lsa_header *) STREAM_DATA (s);
1065 lsa_header_set (s, (OPTIONS (oi) | LSA_OPTIONS_GET (oi->area)),
1066 OSPF_NETWORK_LSA, DR (oi), oi->ospf->router_id);
1068 /* Set network-LSA body fields. */
1069 ospf_network_lsa_body_set (s, oi);
1071 /* Set length. */
1072 length = stream_get_endp (s);
1073 lsah->length = htons (length);
1075 /* Create OSPF LSA instance. */
1076 if ( (new = ospf_lsa_new ()) == NULL)
1078 zlog_err ("%s: ospf_lsa_new returned NULL", __func__);
1079 return NULL;
1082 new->area = oi->area;
1083 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1085 /* Copy LSA to store. */
1086 new->data = ospf_lsa_data_new (length);
1087 memcpy (new->data, lsah, length);
1088 stream_free (s);
1090 return new;
1093 /* Originate network-LSA. */
1094 static struct ospf_lsa *
1095 ospf_network_lsa_originate (struct ospf_interface *oi)
1097 struct ospf_lsa *new;
1099 /* Create new network-LSA instance. */
1100 new = ospf_network_lsa_new (oi);
1101 if (new == NULL)
1102 return NULL;
1104 /* Install LSA to LSDB. */
1105 new = ospf_lsa_install (oi->ospf, oi, new);
1107 /* Update LSA origination count. */
1108 oi->ospf->lsa_originate_count++;
1110 /* Flooding new LSA through area. */
1111 ospf_flood_through_area (oi->area, NULL, new);
1113 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1115 zlog_debug ("LSA[Type%d:%s]: Originate network-LSA %p",
1116 new->data->type, inet_ntoa (new->data->id), new);
1117 ospf_lsa_header_dump (new->data);
1120 return new;
1124 ospf_network_lsa_refresh (struct ospf_lsa *lsa, struct ospf_interface *oi)
1126 struct ospf_area *area = lsa->area;
1127 struct ospf_lsa *new;
1129 assert (lsa->data);
1131 /* Delete LSA from neighbor retransmit-list. */
1132 ospf_ls_retransmit_delete_nbr_area (area, lsa);
1134 /* Create new network-LSA instance. */
1135 new = ospf_network_lsa_new (oi);
1136 if (new == NULL)
1137 return -1;
1138 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1140 ospf_lsa_install (area->ospf, oi, new);
1142 /* Flood LSA through aera. */
1143 ospf_flood_through_area (area, NULL, new);
1145 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1147 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh",
1148 new->data->type, inet_ntoa (new->data->id));
1149 ospf_lsa_header_dump (new->data);
1152 return 0;
1155 static int
1156 ospf_network_lsa_refresh_timer (struct thread *t)
1158 struct ospf_interface *oi;
1160 oi = THREAD_ARG (t);
1161 oi->t_network_lsa_self = NULL;
1163 if (oi->network_lsa_self)
1164 /* Now refresh network-LSA. */
1165 ospf_network_lsa_refresh (oi->network_lsa_self, oi);
1166 else
1167 /* Newly create network-LSA. */
1168 ospf_network_lsa_originate (oi);
1170 return 0;
1173 void
1174 ospf_network_lsa_timer_add (struct ospf_interface *oi)
1176 /* Keep interface's self-originated network-LSA. */
1177 struct ospf_lsa *lsa = oi->network_lsa_self;
1179 /* Cancel previously schedules network-LSA timer. */
1180 if (oi->t_network_lsa_self)
1181 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1182 zlog_debug ("LSA[Type2]: Cancel previous network-LSA timer");
1183 OSPF_TIMER_OFF (oi->t_network_lsa_self);
1185 /* If network-LSA is originated previously, check the interval time. */
1186 if (lsa)
1188 int delay;
1189 if ((delay = ospf_lsa_refresh_delay (lsa)) > 0)
1191 oi->t_network_lsa_self =
1192 thread_add_timer (master, ospf_network_lsa_refresh_timer,
1193 oi, delay);
1194 return;
1198 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1199 zlog_debug ("Scheduling network-LSA origination right away");
1201 /* Immediately refresh network-LSA. */
1202 oi->t_network_lsa_self =
1203 thread_add_event (master, ospf_network_lsa_refresh_timer, oi, 0);
1207 static void
1208 stream_put_ospf_metric (struct stream *s, u_int32_t metric_value)
1210 u_int32_t metric;
1211 char *mp;
1213 /* Put 0 metric. TOS metric is not supported. */
1214 metric = htonl (metric_value);
1215 mp = (char *) &metric;
1216 mp++;
1217 stream_put (s, mp, 3);
1220 /* summary-LSA related functions. */
1221 static void
1222 ospf_summary_lsa_body_set (struct stream *s, struct prefix *p,
1223 u_int32_t metric)
1225 struct in_addr mask;
1227 masklen2ip (p->prefixlen, &mask);
1229 /* Put Network Mask. */
1230 stream_put_ipv4 (s, mask.s_addr);
1232 /* Set # TOS. */
1233 stream_putc (s, (u_char) 0);
1235 /* Set metric. */
1236 stream_put_ospf_metric (s, metric);
1239 static struct ospf_lsa *
1240 ospf_summary_lsa_new (struct ospf_area *area, struct prefix *p,
1241 u_int32_t metric, struct in_addr id)
1243 struct stream *s;
1244 struct ospf_lsa *new;
1245 struct lsa_header *lsah;
1246 int length;
1248 if (id.s_addr == 0xffffffff)
1250 /* Maybe Link State ID not available. */
1251 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1252 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1253 OSPF_SUMMARY_LSA);
1254 return NULL;
1257 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1258 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1260 /* Create new stream for LSA. */
1261 s = stream_new (OSPF_MAX_LSA_SIZE);
1262 lsah = (struct lsa_header *) STREAM_DATA (s);
1264 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_SUMMARY_LSA,
1265 id, area->ospf->router_id);
1267 /* Set summary-LSA body fields. */
1268 ospf_summary_lsa_body_set (s, p, metric);
1270 /* Set length. */
1271 length = stream_get_endp (s);
1272 lsah->length = htons (length);
1274 /* Create OSPF LSA instance. */
1275 new = ospf_lsa_new ();
1276 new->area = area;
1277 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1279 /* Copy LSA to store. */
1280 new->data = ospf_lsa_data_new (length);
1281 memcpy (new->data, lsah, length);
1282 stream_free (s);
1284 return new;
1287 /* Originate Summary-LSA. */
1288 struct ospf_lsa *
1289 ospf_summary_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1290 struct ospf_area *area)
1292 struct ospf_lsa *new;
1293 struct in_addr id;
1295 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
1297 if (id.s_addr == 0xffffffff)
1299 /* Maybe Link State ID not available. */
1300 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1301 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1302 OSPF_SUMMARY_LSA);
1303 return NULL;
1306 /* Create new summary-LSA instance. */
1307 if ( !(new = ospf_summary_lsa_new (area, (struct prefix *) p, metric, id)))
1308 return NULL;
1310 /* Instlal LSA to LSDB. */
1311 new = ospf_lsa_install (area->ospf, NULL, new);
1313 /* Update LSA origination count. */
1314 area->ospf->lsa_originate_count++;
1316 /* Flooding new LSA through area. */
1317 ospf_flood_through_area (area, NULL, new);
1319 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1321 zlog_debug ("LSA[Type%d:%s]: Originate summary-LSA %p",
1322 new->data->type, inet_ntoa (new->data->id), new);
1323 ospf_lsa_header_dump (new->data);
1326 return new;
1329 struct ospf_lsa*
1330 ospf_summary_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1332 struct ospf_lsa *new;
1333 struct summary_lsa *sl;
1334 struct prefix p;
1336 /* Sanity check. */
1337 assert (lsa->data);
1339 sl = (struct summary_lsa *)lsa->data;
1340 p.prefixlen = ip_masklen (sl->mask);
1341 new = ospf_summary_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1342 sl->header.id);
1344 if (!new)
1345 return NULL;
1347 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1349 ospf_lsa_install (ospf, NULL, new);
1351 /* Flood LSA through AS. */
1352 ospf_flood_through_area (new->area, NULL, new);
1354 /* Debug logging. */
1355 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1357 zlog_debug ("LSA[Type%d:%s]: summary-LSA refresh",
1358 new->data->type, inet_ntoa (new->data->id));
1359 ospf_lsa_header_dump (new->data);
1362 return new;
1366 /* summary-ASBR-LSA related functions. */
1367 static void
1368 ospf_summary_asbr_lsa_body_set (struct stream *s, struct prefix *p,
1369 u_int32_t metric)
1371 struct in_addr mask;
1373 masklen2ip (p->prefixlen, &mask);
1375 /* Put Network Mask. */
1376 stream_put_ipv4 (s, mask.s_addr);
1378 /* Set # TOS. */
1379 stream_putc (s, (u_char) 0);
1381 /* Set metric. */
1382 stream_put_ospf_metric (s, metric);
1385 static struct ospf_lsa *
1386 ospf_summary_asbr_lsa_new (struct ospf_area *area, struct prefix *p,
1387 u_int32_t metric, struct in_addr id)
1389 struct stream *s;
1390 struct ospf_lsa *new;
1391 struct lsa_header *lsah;
1392 int length;
1394 if (id.s_addr == 0xffffffff)
1396 /* Maybe Link State ID not available. */
1397 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1398 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1399 OSPF_ASBR_SUMMARY_LSA);
1400 return NULL;
1403 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1404 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1406 /* Create new stream for LSA. */
1407 s = stream_new (OSPF_MAX_LSA_SIZE);
1408 lsah = (struct lsa_header *) STREAM_DATA (s);
1410 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_ASBR_SUMMARY_LSA,
1411 id, area->ospf->router_id);
1413 /* Set summary-LSA body fields. */
1414 ospf_summary_asbr_lsa_body_set (s, p, metric);
1416 /* Set length. */
1417 length = stream_get_endp (s);
1418 lsah->length = htons (length);
1420 /* Create OSPF LSA instance. */
1421 new = ospf_lsa_new ();
1422 new->area = area;
1423 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1425 /* Copy LSA to store. */
1426 new->data = ospf_lsa_data_new (length);
1427 memcpy (new->data, lsah, length);
1428 stream_free (s);
1430 return new;
1433 /* Originate summary-ASBR-LSA. */
1434 struct ospf_lsa *
1435 ospf_summary_asbr_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1436 struct ospf_area *area)
1438 struct ospf_lsa *new;
1439 struct in_addr id;
1441 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA, p);
1443 if (id.s_addr == 0xffffffff)
1445 /* Maybe Link State ID not available. */
1446 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1447 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1448 OSPF_ASBR_SUMMARY_LSA);
1449 return NULL;
1452 /* Create new summary-LSA instance. */
1453 new = ospf_summary_asbr_lsa_new (area, (struct prefix *) p, metric, id);
1454 if (!new)
1455 return NULL;
1457 /* Install LSA to LSDB. */
1458 new = ospf_lsa_install (area->ospf, NULL, new);
1460 /* Update LSA origination count. */
1461 area->ospf->lsa_originate_count++;
1463 /* Flooding new LSA through area. */
1464 ospf_flood_through_area (area, NULL, new);
1466 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1468 zlog_debug ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1469 new->data->type, inet_ntoa (new->data->id), new);
1470 ospf_lsa_header_dump (new->data);
1473 return new;
1476 struct ospf_lsa*
1477 ospf_summary_asbr_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1479 struct ospf_lsa *new;
1480 struct summary_lsa *sl;
1481 struct prefix p;
1483 /* Sanity check. */
1484 assert (lsa->data);
1486 sl = (struct summary_lsa *)lsa->data;
1487 p.prefixlen = ip_masklen (sl->mask);
1488 new = ospf_summary_asbr_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1489 sl->header.id);
1490 if (!new)
1491 return NULL;
1493 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1495 ospf_lsa_install (ospf, NULL, new);
1497 /* Flood LSA through area. */
1498 ospf_flood_through_area (new->area, NULL, new);
1500 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1502 zlog_debug ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1503 new->data->type, inet_ntoa (new->data->id));
1504 ospf_lsa_header_dump (new->data);
1507 return new;
1510 /* AS-external-LSA related functions. */
1512 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1513 is connected, else 0*/
1514 static struct in_addr
1515 ospf_external_lsa_nexthop_get (struct ospf *ospf, struct in_addr nexthop)
1517 struct in_addr fwd;
1518 struct prefix nh;
1519 struct listnode *node;
1520 struct ospf_interface *oi;
1522 fwd.s_addr = 0;
1524 if (!nexthop.s_addr)
1525 return fwd;
1527 /* Check whether nexthop is covered by OSPF network. */
1528 nh.family = AF_INET;
1529 nh.u.prefix4 = nexthop;
1530 nh.prefixlen = IPV4_MAX_BITLEN;
1532 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1533 * better to make use of the per-ifp table of ois.
1535 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
1536 if (if_is_operative (oi->ifp))
1537 if (oi->address->family == AF_INET)
1538 if (prefix_match (oi->address, &nh))
1539 return nexthop;
1541 return fwd;
1544 /* NSSA-external-LSA related functions. */
1546 /* Get 1st IP connection for Forward Addr */
1548 struct in_addr
1549 ospf_get_ip_from_ifp (struct ospf_interface *oi)
1551 struct in_addr fwd;
1553 fwd.s_addr = 0;
1555 if (if_is_operative (oi->ifp))
1556 return oi->address->u.prefix4;
1558 return fwd;
1561 /* Get 1st IP connection for Forward Addr */
1562 struct in_addr
1563 ospf_get_nssa_ip (struct ospf_area *area)
1565 struct in_addr fwd;
1566 struct in_addr best_default;
1567 struct listnode *node;
1568 struct ospf_interface *oi;
1570 fwd.s_addr = 0;
1571 best_default.s_addr = 0;
1573 for (ALL_LIST_ELEMENTS_RO (area->ospf->oiflist, node, oi))
1575 if (if_is_operative (oi->ifp))
1576 if (oi->area->external_routing == OSPF_AREA_NSSA)
1577 if (oi->address && oi->address->family == AF_INET)
1579 if (best_default.s_addr == 0)
1580 best_default = oi->address->u.prefix4;
1581 if (oi->area == area)
1582 return oi->address->u.prefix4;
1585 if (best_default.s_addr != 0)
1586 return best_default;
1588 if (best_default.s_addr != 0)
1589 return best_default;
1591 return fwd;
1594 #define DEFAULT_DEFAULT_METRIC 20
1595 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1596 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1598 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1601 metric_type (struct ospf *ospf, u_char src)
1603 return (ospf->dmetric[src].type < 0 ?
1604 DEFAULT_METRIC_TYPE : ospf->dmetric[src].type);
1608 metric_value (struct ospf *ospf, u_char src)
1610 if (ospf->dmetric[src].value < 0)
1612 if (src == DEFAULT_ROUTE)
1614 if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1615 return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1616 else
1617 return DEFAULT_DEFAULT_ALWAYS_METRIC;
1619 else if (ospf->default_metric < 0)
1620 return DEFAULT_DEFAULT_METRIC;
1621 else
1622 return ospf->default_metric;
1625 return ospf->dmetric[src].value;
1628 /* Set AS-external-LSA body. */
1629 static void
1630 ospf_external_lsa_body_set (struct stream *s, struct external_info *ei,
1631 struct ospf *ospf)
1633 struct prefix_ipv4 *p = &ei->p;
1634 struct in_addr mask, fwd_addr;
1635 u_int32_t mvalue;
1636 int mtype;
1637 int type;
1639 /* Put Network Mask. */
1640 masklen2ip (p->prefixlen, &mask);
1641 stream_put_ipv4 (s, mask.s_addr);
1643 /* If prefix is default, specify DEFAULT_ROUTE. */
1644 type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
1646 mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ?
1647 ROUTEMAP_METRIC_TYPE (ei) : metric_type (ospf, type);
1649 mvalue = (ROUTEMAP_METRIC (ei) != -1) ?
1650 ROUTEMAP_METRIC (ei) : metric_value (ospf, type);
1652 /* Put type of external metric. */
1653 stream_putc (s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1655 /* Put 0 metric. TOS metric is not supported. */
1656 stream_put_ospf_metric (s, mvalue);
1658 /* Get forwarding address to nexthop if on the Connection List, else 0. */
1659 fwd_addr = ospf_external_lsa_nexthop_get (ospf, ei->nexthop);
1661 /* Put forwarding address. */
1662 stream_put_ipv4 (s, fwd_addr.s_addr);
1664 /* Put route tag -- This value should be introduced from configuration. */
1665 stream_putl (s, 0);
1668 /* Create new external-LSA. */
1669 static struct ospf_lsa *
1670 ospf_external_lsa_new (struct ospf *ospf,
1671 struct external_info *ei, struct in_addr *old_id)
1673 struct stream *s;
1674 struct lsa_header *lsah;
1675 struct ospf_lsa *new;
1676 struct in_addr id;
1677 int length;
1679 if (ei == NULL)
1681 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1682 zlog_debug ("LSA[Type5]: External info is NULL, could not originated");
1683 return NULL;
1686 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1687 zlog_debug ("LSA[Type5]: Originate AS-external-LSA instance");
1689 /* If old Link State ID is specified, refresh LSA with same ID. */
1690 if (old_id)
1691 id = *old_id;
1692 /* Get Link State with unique ID. */
1693 else
1695 id = ospf_lsa_unique_id (ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA, &ei->p);
1696 if (id.s_addr == 0xffffffff)
1698 /* Maybe Link State ID not available. */
1699 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1700 zlog_debug ("LSA[Type5]: Link ID not available, can't originate");
1701 return NULL;
1705 /* Create new stream for LSA. */
1706 s = stream_new (OSPF_MAX_LSA_SIZE);
1707 lsah = (struct lsa_header *) STREAM_DATA (s);
1709 /* Set LSA common header fields. */
1710 lsa_header_set (s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA,
1711 id, ospf->router_id);
1713 /* Set AS-external-LSA body fields. */
1714 ospf_external_lsa_body_set (s, ei, ospf);
1716 /* Set length. */
1717 length = stream_get_endp (s);
1718 lsah->length = htons (length);
1720 /* Now, create OSPF LSA instance. */
1721 new = ospf_lsa_new ();
1722 new->area = NULL;
1723 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
1725 /* Copy LSA data to store, discard stream. */
1726 new->data = ospf_lsa_data_new (length);
1727 memcpy (new->data, lsah, length);
1728 stream_free (s);
1730 return new;
1733 /* As Type-7 */
1734 static void
1735 ospf_install_flood_nssa (struct ospf *ospf,
1736 struct ospf_lsa *lsa, struct external_info *ei)
1738 struct ospf_lsa *new;
1739 struct as_external_lsa *extlsa;
1740 struct ospf_area *area;
1741 struct listnode *node, *nnode;
1743 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1744 * which originated from an NSSA area. In which case it should not be
1745 * flooded back to NSSA areas.
1747 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
1748 return;
1750 /* NSSA Originate or Refresh (If anyNSSA)
1752 LSA is self-originated. And just installed as Type-5.
1753 Additionally, install as Type-7 LSDB for every attached NSSA.
1755 P-Bit controls which ABR performs translation to outside world; If
1756 we are an ABR....do not set the P-bit, because we send the Type-5,
1757 not as the ABR Translator, but as the ASBR owner within the AS!
1759 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1760 elected ABR Translator will see the P-bit, Translate, and re-flood.
1762 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1763 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1765 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
1767 /* Don't install Type-7 LSA's into nonNSSA area */
1768 if (area->external_routing != OSPF_AREA_NSSA)
1769 continue;
1771 /* make lsa duplicate, lock=1 */
1772 new = ospf_lsa_dup (lsa);
1773 new->area = area;
1774 new->data->type = OSPF_AS_NSSA_LSA;
1776 /* set P-bit if not ABR */
1777 if (! IS_OSPF_ABR (ospf))
1779 SET_FLAG(new->data->options, OSPF_OPTION_NP);
1781 /* set non-zero FWD ADDR
1783 draft-ietf-ospf-nssa-update-09.txt
1785 if the network between the NSSA AS boundary router and the
1786 adjacent AS is advertised into OSPF as an internal OSPF route,
1787 the forwarding address should be the next op address as is cu
1788 currently done with type-5 LSAs. If the intervening network is
1789 not adversited into OSPF as an internal OSPF route and the
1790 type-7 LSA's P-bit is set a forwarding address should be
1791 selected from one of the router's active OSPF inteface addresses
1792 which belong to the NSSA. If no such addresses exist, then
1793 no type-7 LSA's with the P-bit set should originate from this
1794 router. */
1796 /* kevinm: not updating lsa anymore, just new */
1797 extlsa = (struct as_external_lsa *)(new->data);
1799 if (extlsa->e[0].fwd_addr.s_addr == 0)
1800 extlsa->e[0].fwd_addr = ospf_get_nssa_ip(area); /* this NSSA area in ifp */
1802 if (extlsa->e[0].fwd_addr.s_addr == 0)
1804 if (IS_DEBUG_OSPF_NSSA)
1805 zlog_debug ("LSA[Type-7]: Could not build FWD-ADDR");
1806 ospf_lsa_discard (new);
1807 return;
1811 /* install also as Type-7 */
1812 ospf_lsa_install (ospf, NULL, new); /* Remove Old, Lock New = 2 */
1814 /* will send each copy, lock=2+n */
1815 ospf_flood_through_as (ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
1819 static struct ospf_lsa *
1820 ospf_lsa_translated_nssa_new (struct ospf *ospf,
1821 struct ospf_lsa *type7)
1824 struct ospf_lsa *new;
1825 struct as_external_lsa *ext, *extnew;
1826 struct external_info ei;
1828 ext = (struct as_external_lsa *)(type7->data);
1830 /* need external_info struct, fill in bare minimum */
1831 ei.p.family = AF_INET;
1832 ei.p.prefix = type7->data->id;
1833 ei.p.prefixlen = ip_masklen (ext->mask);
1834 ei.type = ZEBRA_ROUTE_OSPF;
1835 ei.nexthop = ext->header.adv_router;
1836 ei.route_map_set.metric = -1;
1837 ei.route_map_set.metric_type = -1;
1838 ei.tag = 0;
1840 if ( (new = ospf_external_lsa_new (ospf, &ei, &type7->data->id)) == NULL)
1842 if (IS_DEBUG_OSPF_NSSA)
1843 zlog_debug ("ospf_nssa_translate_originate(): Could not originate "
1844 "Translated Type-5 for %s",
1845 inet_ntoa (ei.p.prefix));
1846 return NULL;
1849 extnew = (struct as_external_lsa *)(new->data);
1851 /* copy over Type-7 data to new */
1852 extnew->e[0].tos = ext->e[0].tos;
1853 extnew->e[0].route_tag = ext->e[0].route_tag;
1854 extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
1855 new->data->ls_seqnum = type7->data->ls_seqnum;
1857 /* add translated flag, checksum and lock new lsa */
1858 SET_FLAG (new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7 */
1859 new = ospf_lsa_lock (new);
1861 return new;
1864 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1865 struct ospf_lsa *
1866 ospf_translated_nssa_originate (struct ospf *ospf, struct ospf_lsa *type7)
1868 struct ospf_lsa *new;
1869 struct as_external_lsa *extnew;
1871 /* we cant use ospf_external_lsa_originate() as we need to set
1872 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1875 if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
1877 if (IS_DEBUG_OSPF_NSSA)
1878 zlog_debug ("ospf_translated_nssa_originate(): Could not translate "
1879 "Type-7, Id %s, to Type-5",
1880 inet_ntoa (type7->data->id));
1881 return NULL;
1884 extnew = (struct as_external_lsa *)new;
1886 if (IS_DEBUG_OSPF_NSSA)
1888 zlog_debug ("ospf_translated_nssa_originate(): "
1889 "translated Type 7, installed:");
1890 ospf_lsa_header_dump (new->data);
1891 zlog_debug (" Network mask: %d",ip_masklen (extnew->mask));
1892 zlog_debug (" Forward addr: %s", inet_ntoa (extnew->e[0].fwd_addr));
1895 if ( (new = ospf_lsa_install (ospf, NULL, new)) == NULL)
1897 if (IS_DEBUG_OSPF_NSSA);
1898 zlog_debug ("ospf_lsa_translated_nssa_originate(): "
1899 "Could not install LSA "
1900 "id %s", inet_ntoa (type7->data->id));
1901 return NULL;
1904 ospf->lsa_originate_count++;
1905 ospf_flood_through_as (ospf, NULL, new);
1907 return new;
1910 /* Refresh Translated from NSSA AS-external-LSA. */
1911 struct ospf_lsa *
1912 ospf_translated_nssa_refresh (struct ospf *ospf, struct ospf_lsa *type7,
1913 struct ospf_lsa *type5)
1915 struct ospf_lsa *new = NULL;
1917 /* Sanity checks. */
1918 assert (type7 || type5);
1919 if (!(type7 || type5))
1920 return NULL;
1921 if (type7)
1922 assert (type7->data);
1923 if (type5)
1924 assert (type5->data);
1925 assert (ospf->anyNSSA);
1927 /* get required data according to what has been given */
1928 if (type7 && type5 == NULL)
1930 /* find the translated Type-5 for this Type-7 */
1931 struct as_external_lsa *ext = (struct as_external_lsa *)(type7->data);
1932 struct prefix_ipv4 p =
1934 .prefix = type7->data->id,
1935 .prefixlen = ip_masklen (ext->mask),
1936 .family = AF_INET,
1939 type5 = ospf_external_info_find_lsa (ospf, &p);
1941 else if (type5 && type7 == NULL)
1943 /* find the type-7 from which supplied type-5 was translated,
1944 * ie find first type-7 with same LSA Id.
1946 struct listnode *ln, *lnn;
1947 struct route_node *rn;
1948 struct ospf_lsa *lsa;
1949 struct ospf_area *area;
1951 for (ALL_LIST_ELEMENTS (ospf->areas, ln, lnn, area))
1953 if (area->external_routing != OSPF_AREA_NSSA
1954 && !type7)
1955 continue;
1957 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
1959 if (lsa->data->id.s_addr == type5->data->id.s_addr)
1961 type7 = lsa;
1962 break;
1968 /* do we have type7? */
1969 if (!type7)
1971 if (IS_DEBUG_OSPF_NSSA)
1972 zlog_debug ("ospf_translated_nssa_refresh(): no Type-7 found for "
1973 "Type-5 LSA Id %s",
1974 inet_ntoa (type5->data->id));
1975 return NULL;
1978 /* do we have valid translated type5? */
1979 if (type5 == NULL || !CHECK_FLAG (type5->flags, OSPF_LSA_LOCAL_XLT) )
1981 if (IS_DEBUG_OSPF_NSSA)
1982 zlog_debug ("ospf_translated_nssa_refresh(): No translated Type-5 "
1983 "found for Type-7 with Id %s",
1984 inet_ntoa (type7->data->id));
1985 return NULL;
1988 /* Delete LSA from neighbor retransmit-list. */
1989 ospf_ls_retransmit_delete_nbr_as (ospf, type5);
1991 /* create new translated LSA */
1992 if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
1994 if (IS_DEBUG_OSPF_NSSA)
1995 zlog_debug ("ospf_translated_nssa_refresh(): Could not translate "
1996 "Type-7 for %s to Type-5",
1997 inet_ntoa (type7->data->id));
1998 return NULL;
2001 if ( !(new = ospf_lsa_install (ospf, NULL, new)) )
2003 if (IS_DEBUG_OSPF_NSSA)
2004 zlog_debug ("ospf_translated_nssa_refresh(): Could not install "
2005 "translated LSA, Id %s",
2006 inet_ntoa (type7->data->id));
2007 return NULL;
2010 /* Flood LSA through area. */
2011 ospf_flood_through_as (ospf, NULL, new);
2013 return new;
2017 is_prefix_default (struct prefix_ipv4 *p)
2019 struct prefix_ipv4 q;
2021 q.family = AF_INET;
2022 q.prefix.s_addr = 0;
2023 q.prefixlen = 0;
2025 return prefix_same ((struct prefix *) p, (struct prefix *) &q);
2028 /* Originate an AS-external-LSA, install and flood. */
2029 struct ospf_lsa *
2030 ospf_external_lsa_originate (struct ospf *ospf, struct external_info *ei)
2032 struct ospf_lsa *new;
2034 /* Added for NSSA project....
2036 External LSAs are originated in ASBRs as usual, but for NSSA systems.
2037 there is the global Type-5 LSDB and a Type-7 LSDB installed for
2038 every area. The Type-7's are flooded to every IR and every ABR; We
2039 install the Type-5 LSDB so that the normal "refresh" code operates
2040 as usual, and flag them as not used during ASE calculations. The
2041 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2042 Address of non-zero.
2044 If an ABR is the elected NSSA translator, following SPF and during
2045 the ABR task it will translate all the scanned Type-7's, with P-bit
2046 ON and not-self generated, and translate to Type-5's throughout the
2047 non-NSSA/STUB AS.
2049 A difference in operation depends whether this ASBR is an ABR
2050 or not. If not an ABR, the P-bit is ON, to indicate that any
2051 elected NSSA-ABR can perform its translation.
2053 If an ABR, the P-bit is OFF; No ABR will perform translation and
2054 this ASBR will flood the Type-5 LSA as usual.
2056 For the case where this ASBR is not an ABR, the ASE calculations
2057 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2058 demonstrate to the user that there are LSA's that belong to any
2059 attached NSSA.
2061 Finally, it just so happens that when the ABR is translating every
2062 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2063 approved Type-5 (translated from Type-7); at the end of translation
2064 if any Translated Type-5's remain unapproved, then they must be
2065 flushed from the AS.
2069 /* Check the AS-external-LSA should be originated. */
2070 if (!ospf_redistribute_check (ospf, ei, NULL))
2071 return NULL;
2073 /* Create new AS-external-LSA instance. */
2074 if ((new = ospf_external_lsa_new (ospf, ei, NULL)) == NULL)
2076 if (IS_DEBUG_OSPF_EVENT)
2077 zlog_debug ("LSA[Type5:%s]: Could not originate AS-external-LSA",
2078 inet_ntoa (ei->p.prefix));
2079 return NULL;
2082 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2083 ospf_lsa_install (ospf, NULL, new);
2085 /* Update LSA origination count. */
2086 ospf->lsa_originate_count++;
2088 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2089 ospf_flood_through_as (ospf, NULL, new);
2091 /* If there is any attached NSSA, do special handling */
2092 if (ospf->anyNSSA &&
2093 /* stay away from translated LSAs! */
2094 !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
2095 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
2097 /* Debug logging. */
2098 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2100 zlog_debug ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2101 new->data->type, inet_ntoa (new->data->id), new);
2102 ospf_lsa_header_dump (new->data);
2105 return new;
2108 /* Originate AS-external-LSA from external info with initial flag. */
2110 ospf_external_lsa_originate_timer (struct thread *thread)
2112 struct ospf *ospf = THREAD_ARG (thread);
2113 struct route_node *rn;
2114 struct external_info *ei;
2115 struct route_table *rt;
2116 int type = THREAD_VAL (thread);
2118 ospf->t_external_lsa = NULL;
2120 /* Originate As-external-LSA from all type of distribute source. */
2121 if ((rt = EXTERNAL_INFO (type)))
2122 for (rn = route_top (rt); rn; rn = route_next (rn))
2123 if ((ei = rn->info) != NULL)
2124 if (!is_prefix_default ((struct prefix_ipv4 *)&ei->p))
2125 if (!ospf_external_lsa_originate (ospf, ei))
2126 zlog_warn ("LSA: AS-external-LSA was not originated.");
2128 return 0;
2131 static struct external_info *
2132 ospf_default_external_info (struct ospf *ospf)
2134 int type;
2135 struct route_node *rn;
2136 struct prefix_ipv4 p;
2138 p.family = AF_INET;
2139 p.prefix.s_addr = 0;
2140 p.prefixlen = 0;
2142 /* First, lookup redistributed default route. */
2143 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
2144 if (EXTERNAL_INFO (type) && type != ZEBRA_ROUTE_OSPF)
2146 rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
2147 if (rn != NULL)
2149 route_unlock_node (rn);
2150 assert (rn->info);
2151 if (ospf_redistribute_check (ospf, rn->info, NULL))
2152 return rn->info;
2156 return NULL;
2160 ospf_default_originate_timer (struct thread *thread)
2162 struct prefix_ipv4 p;
2163 struct in_addr nexthop;
2164 struct external_info *ei;
2165 struct ospf *ospf;
2167 ospf = THREAD_ARG (thread);
2169 p.family = AF_INET;
2170 p.prefix.s_addr = 0;
2171 p.prefixlen = 0;
2173 if (ospf->default_originate == DEFAULT_ORIGINATE_ALWAYS)
2175 /* If there is no default route via redistribute,
2176 then originate AS-external-LSA with nexthop 0 (self). */
2177 nexthop.s_addr = 0;
2178 ospf_external_info_add (DEFAULT_ROUTE, p, 0, nexthop);
2181 if ((ei = ospf_default_external_info (ospf)))
2182 ospf_external_lsa_originate (ospf, ei);
2184 return 0;
2187 /* Flush any NSSA LSAs for given prefix */
2188 void
2189 ospf_nssa_lsa_flush (struct ospf *ospf, struct prefix_ipv4 *p)
2191 struct listnode *node, *nnode;
2192 struct ospf_lsa *lsa;
2193 struct ospf_area *area;
2195 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
2197 if (area->external_routing == OSPF_AREA_NSSA)
2199 if (!(lsa = ospf_lsa_lookup (area, OSPF_AS_NSSA_LSA, p->prefix,
2200 ospf->router_id)))
2202 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2203 zlog_debug ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2204 inet_ntoa (p->prefix), p->prefixlen);
2205 continue;
2207 ospf_ls_retransmit_delete_nbr_area (area, lsa);
2208 if (!IS_LSA_MAXAGE (lsa))
2210 ospf_refresher_unregister_lsa (ospf, lsa);
2211 ospf_lsa_flush_area (lsa, area);
2217 /* Flush an AS-external-LSA from LSDB and routing domain. */
2218 void
2219 ospf_external_lsa_flush (struct ospf *ospf,
2220 u_char type, struct prefix_ipv4 *p,
2221 unsigned int ifindex /*, struct in_addr nexthop */)
2223 struct ospf_lsa *lsa;
2225 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2226 zlog_debug ("LSA: Flushing AS-external-LSA %s/%d",
2227 inet_ntoa (p->prefix), p->prefixlen);
2229 /* First lookup LSA from LSDB. */
2230 if (!(lsa = ospf_external_info_find_lsa (ospf, p)))
2232 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2233 zlog_debug ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
2234 inet_ntoa (p->prefix), p->prefixlen);
2235 return;
2238 /* If LSA is selforiginated, not a translated LSA, and there is
2239 * NSSA area, flush Type-7 LSA's at first.
2241 if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
2242 && !(CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT)))
2243 ospf_nssa_lsa_flush (ospf, p);
2245 /* Sweep LSA from Link State Retransmit List. */
2246 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2248 /* There must be no self-originated LSA in rtrs_external. */
2249 #if 0
2250 /* Remove External route from Zebra. */
2251 ospf_zebra_delete ((struct prefix_ipv4 *) p, &nexthop);
2252 #endif
2254 if (!IS_LSA_MAXAGE (lsa))
2256 /* Unregister LSA from Refresh queue. */
2257 ospf_refresher_unregister_lsa (ospf, lsa);
2259 /* Flush AS-external-LSA through AS. */
2260 ospf_lsa_flush_as (ospf, lsa);
2263 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2264 zlog_debug ("ospf_external_lsa_flush(): stop");
2267 void
2268 ospf_external_lsa_refresh_default (struct ospf *ospf)
2270 struct prefix_ipv4 p;
2271 struct external_info *ei;
2272 struct ospf_lsa *lsa;
2274 p.family = AF_INET;
2275 p.prefixlen = 0;
2276 p.prefix.s_addr = 0;
2278 ei = ospf_default_external_info (ospf);
2279 lsa = ospf_external_info_find_lsa (ospf, &p);
2281 if (ei)
2283 if (lsa)
2285 if (IS_DEBUG_OSPF_EVENT)
2286 zlog_debug ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p", lsa);
2287 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
2289 else
2291 if (IS_DEBUG_OSPF_EVENT)
2292 zlog_debug ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2293 ospf_external_lsa_originate (ospf, ei);
2296 else
2298 if (lsa)
2300 if (IS_DEBUG_OSPF_EVENT)
2301 zlog_debug ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2302 ospf_lsa_flush_as (ospf, lsa);
2307 void
2308 ospf_external_lsa_refresh_type (struct ospf *ospf, u_char type, int force)
2310 struct route_node *rn;
2311 struct external_info *ei;
2313 if (type != DEFAULT_ROUTE)
2314 if (EXTERNAL_INFO(type))
2315 /* Refresh each redistributed AS-external-LSAs. */
2316 for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
2317 if ((ei = rn->info))
2318 if (!is_prefix_default (&ei->p))
2320 struct ospf_lsa *lsa;
2322 if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
2323 ospf_external_lsa_refresh (ospf, lsa, ei, force);
2324 else
2325 ospf_external_lsa_originate (ospf, ei);
2329 /* Refresh AS-external-LSA. */
2330 void
2331 ospf_external_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa,
2332 struct external_info *ei, int force)
2334 struct ospf_lsa *new;
2335 int changed;
2337 /* Check the AS-external-LSA should be originated. */
2338 if (!ospf_redistribute_check (ospf, ei, &changed))
2340 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2341 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed, "
2342 "redist check fail",
2343 lsa->data->type, inet_ntoa (lsa->data->id));
2344 ospf_external_lsa_flush (ospf, ei->type, &ei->p,
2345 ei->ifindex /*, ei->nexthop */);
2346 return;
2349 if (!changed && !force)
2351 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2352 zlog_debug ("LSA[Type%d:%s]: Not refreshed, not changed/forced",
2353 lsa->data->type, inet_ntoa (lsa->data->id));
2354 return;
2357 /* Delete LSA from neighbor retransmit-list. */
2358 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2360 /* Unregister AS-external-LSA from refresh-list. */
2361 ospf_refresher_unregister_lsa (ospf, lsa);
2363 new = ospf_external_lsa_new (ospf, ei, &lsa->data->id);
2365 if (new == NULL)
2367 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2368 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed", lsa->data->type,
2369 inet_ntoa (lsa->data->id));
2370 return;
2373 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
2375 ospf_lsa_install (ospf, NULL, new); /* As type-5. */
2377 /* Flood LSA through AS. */
2378 ospf_flood_through_as (ospf, NULL, new);
2380 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2381 if (ospf->anyNSSA && !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
2382 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood per new rules */
2384 /* Register self-originated LSA to refresh queue.
2385 * Translated LSAs should not be registered, but refreshed upon
2386 * refresh of the Type-7
2388 if ( !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT) )
2389 ospf_refresher_register_lsa (ospf, new);
2391 /* Debug logging. */
2392 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2394 zlog_debug ("LSA[Type%d:%s]: AS-external-LSA refresh",
2395 new->data->type, inet_ntoa (new->data->id));
2396 ospf_lsa_header_dump (new->data);
2399 return;
2403 /* LSA installation functions. */
2405 /* Install router-LSA to an area. */
2406 static struct ospf_lsa *
2407 ospf_router_lsa_install (struct ospf *ospf,
2408 struct ospf_lsa *new, int rt_recalc)
2410 struct ospf_area *area = new->area;
2412 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2413 The entire routing table must be recalculated, starting with
2414 the shortest path calculations for each area (not just the
2415 area whose link-state database has changed).
2418 if (IS_LSA_SELF (new))
2421 /* Only install LSA if it is originated/refreshed by us.
2422 * If LSA was received by flooding, the RECEIVED flag is set so do
2423 * not link the LSA */
2424 if (CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
2425 return new; /* ignore stale LSA */
2427 /* Set router-LSA refresh timer. */
2428 OSPF_TIMER_OFF (area->t_router_lsa_self);
2429 OSPF_AREA_TIMER_ON (area->t_router_lsa_self,
2430 ospf_router_lsa_timer, OSPF_LS_REFRESH_TIME);
2432 /* Set self-originated router-LSA. */
2433 ospf_lsa_unlock (&area->router_lsa_self);
2434 area->router_lsa_self = ospf_lsa_lock (new);
2437 if (rt_recalc)
2438 ospf_spf_calculate_schedule (ospf);
2440 return new;
2443 #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
2444 if (!(T)) \
2445 (T) = thread_add_timer (master, (F), oi, (V))
2447 /* Install network-LSA to an area. */
2448 static struct ospf_lsa *
2449 ospf_network_lsa_install (struct ospf *ospf,
2450 struct ospf_interface *oi,
2451 struct ospf_lsa *new,
2452 int rt_recalc)
2455 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2456 The entire routing table must be recalculated, starting with
2457 the shortest path calculations for each area (not just the
2458 area whose link-state database has changed).
2460 if (IS_LSA_SELF (new))
2462 /* We supposed that when LSA is originated by us, we pass the int
2463 for which it was originated. If LSA was received by flooding,
2464 the RECEIVED flag is set, so we do not link the LSA to the int. */
2465 if (CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
2466 return new; /* ignore stale LSA */
2468 /* Set LSRefresh timer. */
2469 OSPF_TIMER_OFF (oi->t_network_lsa_self);
2471 OSPF_INTERFACE_TIMER_ON (oi->t_network_lsa_self,
2472 ospf_network_lsa_refresh_timer,
2473 OSPF_LS_REFRESH_TIME);
2475 ospf_lsa_unlock (&oi->network_lsa_self);
2476 oi->network_lsa_self = ospf_lsa_lock (new);
2478 if (rt_recalc)
2479 ospf_spf_calculate_schedule (ospf);
2481 return new;
2484 /* Install summary-LSA to an area. */
2485 static struct ospf_lsa *
2486 ospf_summary_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2487 int rt_recalc)
2489 if (rt_recalc && !IS_LSA_SELF (new))
2491 /* RFC 2328 Section 13.2 Summary-LSAs
2492 The best route to the destination described by the summary-
2493 LSA must be recalculated (see Section 16.5). If this
2494 destination is an AS boundary router, it may also be
2495 necessary to re-examine all the AS-external-LSAs.
2498 #if 0
2499 /* This doesn't exist yet... */
2500 ospf_summary_incremental_update(new); */
2501 #else /* #if 0 */
2502 ospf_spf_calculate_schedule (ospf);
2503 #endif /* #if 0 */
2505 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2506 zlog_debug ("ospf_summary_lsa_install(): SPF scheduled");
2509 if (IS_LSA_SELF (new))
2510 ospf_refresher_register_lsa (ospf, new);
2512 return new;
2515 /* Install ASBR-summary-LSA to an area. */
2516 static struct ospf_lsa *
2517 ospf_summary_asbr_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2518 int rt_recalc)
2520 if (rt_recalc && !IS_LSA_SELF (new))
2522 /* RFC 2328 Section 13.2 Summary-LSAs
2523 The best route to the destination described by the summary-
2524 LSA must be recalculated (see Section 16.5). If this
2525 destination is an AS boundary router, it may also be
2526 necessary to re-examine all the AS-external-LSAs.
2528 #if 0
2529 /* These don't exist yet... */
2530 ospf_summary_incremental_update(new);
2531 /* Isn't this done by the above call?
2532 - RFC 2328 Section 16.5 implies it should be */
2533 /* ospf_ase_calculate_schedule(); */
2534 #else /* #if 0 */
2535 ospf_spf_calculate_schedule (ospf);
2536 #endif /* #if 0 */
2539 /* register LSA to refresh-list. */
2540 if (IS_LSA_SELF (new))
2541 ospf_refresher_register_lsa (ospf, new);
2543 return new;
2546 /* Install AS-external-LSA. */
2547 static struct ospf_lsa *
2548 ospf_external_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2549 int rt_recalc)
2551 ospf_ase_register_external_lsa (new, ospf);
2552 /* If LSA is not self-originated, calculate an external route. */
2553 if (rt_recalc)
2555 /* RFC 2328 Section 13.2 AS-external-LSAs
2556 The best route to the destination described by the AS-
2557 external-LSA must be recalculated (see Section 16.6).
2560 if (!IS_LSA_SELF (new))
2561 ospf_ase_incremental_update (ospf, new);
2564 if (new->data->type == OSPF_AS_NSSA_LSA)
2566 /* There is no point to register selforiginate Type-7 LSA for
2567 * refreshing. We rely on refreshing Type-5 LSA's
2569 if (IS_LSA_SELF (new))
2570 return new;
2571 else
2573 /* Try refresh type-5 translated LSA for this LSA, if one exists.
2574 * New translations will be taken care of by the abr_task.
2576 ospf_translated_nssa_refresh (ospf, new, NULL);
2580 /* Register self-originated LSA to refresh queue.
2581 * Leave Translated LSAs alone if NSSA is enabled
2583 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT ) )
2584 ospf_refresher_register_lsa (ospf, new);
2586 return new;
2589 void
2590 ospf_discard_from_db (struct ospf *ospf,
2591 struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
2593 struct ospf_lsa *old;
2595 if (!lsdb)
2597 zlog_warn ("%s: Called with NULL lsdb!", __func__);
2598 if (!lsa)
2599 zlog_warn ("%s: and NULL LSA!", __func__);
2600 else
2601 zlog_warn ("LSA[Type%d:%s]: not associated with LSDB!",
2602 lsa->data->type, inet_ntoa (lsa->data->id));
2603 return;
2606 old = ospf_lsdb_lookup (lsdb, lsa);
2608 if (!old)
2609 return;
2611 if (old->refresh_list >= 0)
2612 ospf_refresher_unregister_lsa (ospf, old);
2614 switch (old->data->type)
2616 case OSPF_AS_EXTERNAL_LSA:
2617 ospf_ase_unregister_external_lsa (old, ospf);
2618 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2619 break;
2620 #ifdef HAVE_OPAQUE_LSA
2621 case OSPF_OPAQUE_AS_LSA:
2622 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2623 break;
2624 #endif /* HAVE_OPAQUE_LSA */
2625 case OSPF_AS_NSSA_LSA:
2626 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2627 ospf_ase_unregister_external_lsa (old, ospf);
2628 break;
2629 default:
2630 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2631 break;
2634 ospf_lsa_maxage_delete (ospf, old);
2635 ospf_lsa_discard (old);
2638 struct ospf_lsa *
2639 ospf_lsa_install (struct ospf *ospf, struct ospf_interface *oi,
2640 struct ospf_lsa *lsa)
2642 struct ospf_lsa *new = NULL;
2643 struct ospf_lsa *old = NULL;
2644 struct ospf_lsdb *lsdb = NULL;
2645 int rt_recalc;
2647 /* Set LSDB. */
2648 switch (lsa->data->type)
2650 /* kevinm */
2651 case OSPF_AS_NSSA_LSA:
2652 if (lsa->area)
2653 lsdb = lsa->area->lsdb;
2654 else
2655 lsdb = ospf->lsdb;
2656 break;
2657 case OSPF_AS_EXTERNAL_LSA:
2658 #ifdef HAVE_OPAQUE_LSA
2659 case OSPF_OPAQUE_AS_LSA:
2660 #endif /* HAVE_OPAQUE_LSA */
2661 lsdb = ospf->lsdb;
2662 break;
2663 default:
2664 lsdb = lsa->area->lsdb;
2665 break;
2668 assert (lsdb);
2670 /* RFC 2328 13.2. Installing LSAs in the database
2672 Installing a new LSA in the database, either as the result of
2673 flooding or a newly self-originated LSA, may cause the OSPF
2674 routing table structure to be recalculated. The contents of the
2675 new LSA should be compared to the old instance, if present. If
2676 there is no difference, there is no need to recalculate the
2677 routing table. When comparing an LSA to its previous instance,
2678 the following are all considered to be differences in contents:
2680 o The LSA's Options field has changed.
2682 o One of the LSA instances has LS age set to MaxAge, and
2683 the other does not.
2685 o The length field in the LSA header has changed.
2687 o The body of the LSA (i.e., anything outside the 20-byte
2688 LSA header) has changed. Note that this excludes changes
2689 in LS Sequence Number and LS Checksum.
2692 /* Look up old LSA and determine if any SPF calculation or incremental
2693 update is needed */
2694 old = ospf_lsdb_lookup (lsdb, lsa);
2696 /* Do comparision and record if recalc needed. */
2697 rt_recalc = 0;
2698 if ( old == NULL || ospf_lsa_different(old, lsa))
2699 rt_recalc = 1;
2702 Sequence number check (Section 14.1 of rfc 2328)
2703 "Premature aging is used when it is time for a self-originated
2704 LSA's sequence number field to wrap. At this point, the current
2705 LSA instance (having LS sequence number MaxSequenceNumber) must
2706 be prematurely aged and flushed from the routing domain before a
2707 new instance with sequence number equal to InitialSequenceNumber
2708 can be originated. "
2711 if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER)
2713 if (ospf_lsa_is_self_originated(ospf, lsa))
2715 lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
2717 if (!IS_LSA_MAXAGE(lsa))
2718 lsa->flags |= OSPF_LSA_PREMATURE_AGE;
2719 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
2721 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
2723 zlog_debug ("ospf_lsa_install() Premature Aging "
2724 "lsa 0x%lx", (u_long)lsa);
2725 ospf_lsa_header_dump (lsa->data);
2728 else
2730 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2732 zlog_debug ("ospf_lsa_install() got an lsa with seq 0x80000000 "
2733 "that was not self originated. Ignoring\n");
2734 ospf_lsa_header_dump (lsa->data);
2736 return old;
2740 /* discard old LSA from LSDB */
2741 if (old != NULL)
2742 ospf_discard_from_db (ospf, lsdb, lsa);
2744 /* Calculate Checksum if self-originated?. */
2745 if (IS_LSA_SELF (lsa))
2746 ospf_lsa_checksum (lsa->data);
2748 /* Insert LSA to LSDB. */
2749 ospf_lsdb_add (lsdb, lsa);
2750 lsa->lsdb = lsdb;
2752 /* Do LSA specific installation process. */
2753 switch (lsa->data->type)
2755 case OSPF_ROUTER_LSA:
2756 new = ospf_router_lsa_install (ospf, lsa, rt_recalc);
2757 break;
2758 case OSPF_NETWORK_LSA:
2759 assert (oi);
2760 new = ospf_network_lsa_install (ospf, oi, lsa, rt_recalc);
2761 break;
2762 case OSPF_SUMMARY_LSA:
2763 new = ospf_summary_lsa_install (ospf, lsa, rt_recalc);
2764 break;
2765 case OSPF_ASBR_SUMMARY_LSA:
2766 new = ospf_summary_asbr_lsa_install (ospf, lsa, rt_recalc);
2767 break;
2768 case OSPF_AS_EXTERNAL_LSA:
2769 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2770 break;
2771 #ifdef HAVE_OPAQUE_LSA
2772 case OSPF_OPAQUE_LINK_LSA:
2773 if (IS_LSA_SELF (lsa))
2774 lsa->oi = oi; /* Specify outgoing ospf-interface for this LSA. */
2775 else
2776 ; /* Incoming "oi" for this LSA has set at LSUpd reception. */
2777 /* Fallthrough */
2778 case OSPF_OPAQUE_AREA_LSA:
2779 case OSPF_OPAQUE_AS_LSA:
2780 new = ospf_opaque_lsa_install (lsa, rt_recalc);
2781 break;
2782 #endif /* HAVE_OPAQUE_LSA */
2783 case OSPF_AS_NSSA_LSA:
2784 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2785 default: /* type-6,8,9....nothing special */
2786 break;
2789 if (new == NULL)
2790 return new; /* Installation failed, cannot proceed further -- endo. */
2792 /* Debug logs. */
2793 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2795 char area_str[INET_ADDRSTRLEN];
2797 switch (lsa->data->type)
2799 case OSPF_AS_EXTERNAL_LSA:
2800 #ifdef HAVE_OPAQUE_LSA
2801 case OSPF_OPAQUE_AS_LSA:
2802 #endif /* HAVE_OPAQUE_LSA */
2803 case OSPF_AS_NSSA_LSA:
2804 zlog_debug ("LSA[%s]: Install %s",
2805 dump_lsa_key (new),
2806 LOOKUP (ospf_lsa_type_msg, new->data->type));
2807 break;
2808 default:
2809 strcpy (area_str, inet_ntoa (new->area->area_id));
2810 zlog_debug ("LSA[%s]: Install %s to Area %s",
2811 dump_lsa_key (new),
2812 LOOKUP (ospf_lsa_type_msg, new->data->type), area_str);
2813 break;
2818 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2819 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2821 if ((lsa->flags & OSPF_LSA_PREMATURE_AGE) ||
2822 (IS_LSA_MAXAGE (new) && !IS_LSA_SELF (new)))
2824 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2825 zlog_debug ("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2826 new->data->type,
2827 inet_ntoa (new->data->id),
2828 lsa);
2829 ospf_lsa_maxage (ospf, lsa);
2832 return new;
2836 static int
2837 ospf_check_nbr_status (struct ospf *ospf)
2839 struct listnode *node, *nnode;
2840 struct ospf_interface *oi;
2842 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
2844 struct route_node *rn;
2845 struct ospf_neighbor *nbr;
2847 if (ospf_if_is_enable (oi))
2848 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
2849 if ((nbr = rn->info) != NULL)
2850 if (nbr->state == NSM_Exchange || nbr->state == NSM_Loading)
2852 route_unlock_node (rn);
2853 return 0;
2857 return 1;
2861 #ifdef ORIGINAL_CODING
2862 /* This function flood the maxaged LSA to DR. */
2863 void
2864 ospf_maxage_flood (struct ospf_lsa *lsa)
2866 switch (lsa->data->type)
2868 case OSPF_ROUTER_LSA:
2869 case OSPF_NETWORK_LSA:
2870 case OSPF_SUMMARY_LSA:
2871 case OSPF_ASBR_SUMMARY_LSA:
2872 case OSPF_AS_NSSA_LSA:
2873 #ifdef HAVE_OPAQUE_LSA
2874 case OSPF_OPAQUE_LINK_LSA:
2875 case OSPF_OPAQUE_AREA_LSA:
2876 #endif /* HAVE_OPAQUE_LSA */
2877 ospf_flood_through_area (lsa->area, NULL, lsa);
2878 break;
2879 case OSPF_AS_EXTERNAL_LSA:
2880 #ifdef HAVE_OPAQUE_LSA
2881 case OSPF_OPAQUE_AS_LSA:
2882 #endif /* HAVE_OPAQUE_LSA */
2883 ospf_flood_through_as (NULL, lsa);
2884 break;
2885 default:
2886 break;
2889 #endif /* ORIGINAL_CODING */
2891 static int
2892 ospf_maxage_lsa_remover (struct thread *thread)
2894 struct ospf *ospf = THREAD_ARG (thread);
2895 struct ospf_lsa *lsa;
2896 struct listnode *node, *nnode;
2897 int reschedule = 0;
2899 ospf->t_maxage = NULL;
2901 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2902 zlog_debug ("LSA[MaxAge]: remover Start");
2904 reschedule = !ospf_check_nbr_status (ospf);
2906 if (!reschedule)
2907 for (ALL_LIST_ELEMENTS (ospf->maxage_lsa, node, nnode, lsa))
2909 if (lsa->retransmit_counter > 0)
2911 reschedule = 1;
2912 continue;
2915 /* Remove LSA from the LSDB */
2916 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF))
2917 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2918 zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-oririnated: ",
2919 lsa->data->type, inet_ntoa (lsa->data->id), (u_long)lsa);
2921 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2922 zlog_debug ("LSA[Type%d:%s]: MaxAge LSA removed from list",
2923 lsa->data->type, inet_ntoa (lsa->data->id));
2925 /* Flood max age LSA. */
2926 #ifdef ORIGINAL_CODING
2927 ospf_maxage_flood (lsa);
2928 #else /* ORIGINAL_CODING */
2929 ospf_flood_through (ospf, NULL, lsa);
2930 #endif /* ORIGINAL_CODING */
2932 if (lsa->flags & OSPF_LSA_PREMATURE_AGE)
2934 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2935 zlog_debug ("originating new router lsa for lsa 0x%lx \n",
2936 (u_long)lsa);
2937 ospf_router_lsa_originate(lsa->area);
2940 /* Remove from lsdb. */
2941 if (lsa->lsdb)
2943 ospf_discard_from_db (ospf, lsa->lsdb, lsa);
2944 ospf_lsdb_delete (lsa->lsdb, lsa);
2946 else
2947 zlog_warn ("%s: LSA[Type%d:%s]: No associated LSDB!", __func__,
2948 lsa->data->type, inet_ntoa (lsa->data->id));
2951 /* A MaxAge LSA must be removed immediately from the router's link
2952 state database as soon as both a) it is no longer contained on any
2953 neighbor Link state retransmission lists and b) none of the router's
2954 neighbors are in states Exchange or Loading. */
2955 if (reschedule)
2956 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 2);
2958 return 0;
2961 void
2962 ospf_lsa_maxage_delete (struct ospf *ospf, struct ospf_lsa *lsa)
2964 struct listnode *n;
2966 if ((n = listnode_lookup (ospf->maxage_lsa, lsa)))
2968 list_delete_node (ospf->maxage_lsa, n);
2969 UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2970 ospf_lsa_unlock (&lsa); /* maxage_lsa */
2974 void
2975 ospf_lsa_maxage (struct ospf *ospf, struct ospf_lsa *lsa)
2977 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2978 and schedule the MaxAge LSA remover. */
2979 if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE))
2981 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2982 zlog_debug ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2983 lsa->data->type, inet_ntoa (lsa->data->id), lsa);
2984 return;
2987 listnode_add (ospf->maxage_lsa, ospf_lsa_lock (lsa));
2988 SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2990 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2991 zlog_debug ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa));
2993 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 2);
2996 static int
2997 ospf_lsa_maxage_walker_remover (struct ospf *ospf, struct ospf_lsa *lsa)
2999 /* Stay away from any Local Translated Type-7 LSAs */
3000 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
3001 return 0;
3003 if (IS_LSA_MAXAGE (lsa))
3004 /* Self-originated LSAs should NOT time-out instead,
3005 they're flushed and submitted to the max_age list explicitly. */
3006 if (!ospf_lsa_is_self_originated (ospf, lsa))
3008 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3009 zlog_debug("LSA[%s]: is MaxAge", dump_lsa_key (lsa));
3011 switch (lsa->data->type)
3013 #ifdef HAVE_OPAQUE_LSA
3014 case OSPF_OPAQUE_LINK_LSA:
3015 case OSPF_OPAQUE_AREA_LSA:
3016 case OSPF_OPAQUE_AS_LSA:
3018 * As a general rule, whenever network topology has changed
3019 * (due to an LSA removal in this case), routing recalculation
3020 * should be triggered. However, this is not true for opaque
3021 * LSAs. Even if an opaque LSA instance is going to be removed
3022 * from the routing domain, it does not mean a change in network
3023 * topology, and thus, routing recalculation is not needed here.
3025 break;
3026 #endif /* HAVE_OPAQUE_LSA */
3027 case OSPF_AS_EXTERNAL_LSA:
3028 case OSPF_AS_NSSA_LSA:
3029 ospf_ase_incremental_update (ospf, lsa);
3030 break;
3031 default:
3032 ospf_spf_calculate_schedule (ospf);
3033 break;
3035 ospf_lsa_maxage (ospf, lsa);
3038 return 0;
3041 /* Periodical check of MaxAge LSA. */
3043 ospf_lsa_maxage_walker (struct thread *thread)
3045 struct ospf *ospf = THREAD_ARG (thread);
3046 struct route_node *rn;
3047 struct ospf_lsa *lsa;
3048 struct ospf_area *area;
3049 struct listnode *node, *nnode;
3051 ospf->t_maxage_walker = NULL;
3053 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
3055 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
3056 ospf_lsa_maxage_walker_remover (ospf, lsa);
3057 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
3058 ospf_lsa_maxage_walker_remover (ospf, lsa);
3059 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
3060 ospf_lsa_maxage_walker_remover (ospf, lsa);
3061 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
3062 ospf_lsa_maxage_walker_remover (ospf, lsa);
3063 #ifdef HAVE_OPAQUE_LSA
3064 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
3065 ospf_lsa_maxage_walker_remover (ospf, lsa);
3066 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
3067 ospf_lsa_maxage_walker_remover (ospf, lsa);
3068 #endif /* HAVE_OPAQUE_LSA */
3069 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
3070 ospf_lsa_maxage_walker_remover (ospf, lsa);
3073 /* for AS-external-LSAs. */
3074 if (ospf->lsdb)
3076 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
3077 ospf_lsa_maxage_walker_remover (ospf, lsa);
3078 #ifdef HAVE_OPAQUE_LSA
3079 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
3080 ospf_lsa_maxage_walker_remover (ospf, lsa);
3081 #endif /* HAVE_OPAQUE_LSA */
3084 OSPF_TIMER_ON (ospf->t_maxage_walker, ospf_lsa_maxage_walker,
3085 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
3086 return 0;
3089 struct ospf_lsa *
3090 ospf_lsa_lookup_by_prefix (struct ospf_lsdb *lsdb, u_char type,
3091 struct prefix_ipv4 *p, struct in_addr router_id)
3093 struct ospf_lsa *lsa;
3094 struct in_addr mask, id;
3095 struct lsa_header_mask
3097 struct lsa_header header;
3098 struct in_addr mask;
3099 } *hmask;
3101 lsa = ospf_lsdb_lookup_by_id (lsdb, type, p->prefix, router_id);
3102 if (lsa == NULL)
3103 return NULL;
3105 masklen2ip (p->prefixlen, &mask);
3107 hmask = (struct lsa_header_mask *) lsa->data;
3109 if (mask.s_addr != hmask->mask.s_addr)
3111 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3112 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, router_id);
3113 if (!lsa)
3114 return NULL;
3117 return lsa;
3120 struct ospf_lsa *
3121 ospf_lsa_lookup (struct ospf_area *area, u_int32_t type,
3122 struct in_addr id, struct in_addr adv_router)
3124 struct ospf *ospf = ospf_lookup();
3125 assert(ospf);
3127 switch (type)
3129 case OSPF_ROUTER_LSA:
3130 case OSPF_NETWORK_LSA:
3131 case OSPF_SUMMARY_LSA:
3132 case OSPF_ASBR_SUMMARY_LSA:
3133 case OSPF_AS_NSSA_LSA:
3134 #ifdef HAVE_OPAQUE_LSA
3135 case OSPF_OPAQUE_LINK_LSA:
3136 case OSPF_OPAQUE_AREA_LSA:
3137 #endif /* HAVE_OPAQUE_LSA */
3138 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, adv_router);
3139 case OSPF_AS_EXTERNAL_LSA:
3140 #ifdef HAVE_OPAQUE_LSA
3141 case OSPF_OPAQUE_AS_LSA:
3142 #endif /* HAVE_OPAQUE_LSA */
3143 return ospf_lsdb_lookup_by_id (ospf->lsdb, type, id, adv_router);
3144 default:
3145 break;
3148 return NULL;
3151 struct ospf_lsa *
3152 ospf_lsa_lookup_by_id (struct ospf_area *area, u_int32_t type,
3153 struct in_addr id)
3155 struct ospf_lsa *lsa;
3156 struct route_node *rn;
3158 switch (type)
3160 case OSPF_ROUTER_LSA:
3161 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
3162 case OSPF_NETWORK_LSA:
3163 for (rn = route_top (NETWORK_LSDB (area)); rn; rn = route_next (rn))
3164 if ((lsa = rn->info))
3165 if (IPV4_ADDR_SAME (&lsa->data->id, &id))
3167 route_unlock_node (rn);
3168 return lsa;
3170 break;
3171 case OSPF_SUMMARY_LSA:
3172 case OSPF_ASBR_SUMMARY_LSA:
3173 /* Currently not used. */
3174 assert (1);
3175 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
3176 case OSPF_AS_EXTERNAL_LSA:
3177 case OSPF_AS_NSSA_LSA:
3178 #ifdef HAVE_OPAQUE_LSA
3179 case OSPF_OPAQUE_LINK_LSA:
3180 case OSPF_OPAQUE_AREA_LSA:
3181 case OSPF_OPAQUE_AS_LSA:
3182 /* Currently not used. */
3183 break;
3184 #endif /* HAVE_OPAQUE_LSA */
3185 default:
3186 break;
3189 return NULL;
3192 struct ospf_lsa *
3193 ospf_lsa_lookup_by_header (struct ospf_area *area, struct lsa_header *lsah)
3195 struct ospf_lsa *match;
3197 #ifdef HAVE_OPAQUE_LSA
3199 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3200 * is redefined to have two subfields; opaque-type and opaque-id.
3201 * However, it is harmless to treat the two sub fields together, as if
3202 * they two were forming a unique LSA-ID.
3204 #endif /* HAVE_OPAQUE_LSA */
3206 match = ospf_lsa_lookup (area, lsah->type, lsah->id, lsah->adv_router);
3208 if (match == NULL)
3209 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3210 zlog_debug ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3211 lsah->type, inet_ntoa (lsah->id));
3213 return match;
3216 /* return +n, l1 is more recent.
3217 return -n, l2 is more recent.
3218 return 0, l1 and l2 is identical. */
3220 ospf_lsa_more_recent (struct ospf_lsa *l1, struct ospf_lsa *l2)
3222 int r;
3223 int x, y;
3225 if (l1 == NULL && l2 == NULL)
3226 return 0;
3227 if (l1 == NULL)
3228 return -1;
3229 if (l2 == NULL)
3230 return 1;
3232 /* compare LS sequence number. */
3233 x = (int) ntohl (l1->data->ls_seqnum);
3234 y = (int) ntohl (l2->data->ls_seqnum);
3235 if (x > y)
3236 return 1;
3237 if (x < y)
3238 return -1;
3240 /* compare LS checksum. */
3241 r = ntohs (l1->data->checksum) - ntohs (l2->data->checksum);
3242 if (r)
3243 return r;
3245 /* compare LS age. */
3246 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
3247 return 1;
3248 else if (!IS_LSA_MAXAGE (l1) && IS_LSA_MAXAGE (l2))
3249 return -1;
3251 /* compare LS age with MaxAgeDiff. */
3252 if (LS_AGE (l1) - LS_AGE (l2) > OSPF_LSA_MAXAGE_DIFF)
3253 return -1;
3254 else if (LS_AGE (l2) - LS_AGE (l1) > OSPF_LSA_MAXAGE_DIFF)
3255 return 1;
3257 /* LSAs are identical. */
3258 return 0;
3261 /* If two LSAs are different, return 1, otherwise return 0. */
3263 ospf_lsa_different (struct ospf_lsa *l1, struct ospf_lsa *l2)
3265 char *p1, *p2;
3266 assert (l1);
3267 assert (l2);
3268 assert (l1->data);
3269 assert (l2->data);
3271 if (l1->data->options != l2->data->options)
3272 return 1;
3274 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
3275 return 1;
3277 if (IS_LSA_MAXAGE (l2) && !IS_LSA_MAXAGE (l1))
3278 return 1;
3280 if (l1->data->length != l2->data->length)
3281 return 1;
3283 if (l1->data->length == 0)
3284 return 1;
3286 if (CHECK_FLAG ((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
3287 return 1; /* May be a stale LSA in the LSBD */
3289 assert ( ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
3291 p1 = (char *) l1->data;
3292 p2 = (char *) l2->data;
3294 if (memcmp (p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
3295 ntohs( l1->data->length ) - OSPF_LSA_HEADER_SIZE) != 0)
3296 return 1;
3298 return 0;
3301 #ifdef ORIGINAL_CODING
3302 void
3303 ospf_lsa_flush_self_originated (struct ospf_neighbor *nbr,
3304 struct ospf_lsa *self,
3305 struct ospf_lsa *new)
3307 u_int32_t seqnum;
3309 /* Adjust LS Sequence Number. */
3310 seqnum = ntohl (new->data->ls_seqnum) + 1;
3311 self->data->ls_seqnum = htonl (seqnum);
3313 /* Recalculate LSA checksum. */
3314 ospf_lsa_checksum (self->data);
3316 /* Reflooding LSA. */
3317 /* RFC2328 Section 13.3
3318 On non-broadcast networks, separate Link State Update
3319 packets must be sent, as unicasts, to each adjacent neighbor
3320 (i.e., those in state Exchange or greater). The destination
3321 IP addresses for these packets are the neighbors' IP
3322 addresses. */
3323 if (nbr->oi->type == OSPF_IFTYPE_NBMA)
3325 struct route_node *rn;
3326 struct ospf_neighbor *onbr;
3328 for (rn = route_top (nbr->oi->nbrs); rn; rn = route_next (rn))
3329 if ((onbr = rn->info) != NULL)
3330 if (onbr != nbr->oi->nbr_self && onbr->status >= NSM_Exchange)
3331 ospf_ls_upd_send_lsa (onbr, self, OSPF_SEND_PACKET_DIRECT);
3333 else
3334 ospf_ls_upd_send_lsa (nbr, self, OSPF_SEND_PACKET_INDIRECT);
3336 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3337 zlog_debug ("LSA[Type%d:%s]: Flush self-originated LSA",
3338 self->data->type, inet_ntoa (self->data->id));
3340 #else /* ORIGINAL_CODING */
3341 static int
3342 ospf_lsa_flush_schedule (struct ospf *ospf, struct ospf_lsa *lsa)
3344 if (lsa == NULL || !IS_LSA_SELF (lsa))
3345 return 0;
3347 if (IS_DEBUG_OSPF_EVENT)
3348 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3350 /* Force given lsa's age to MaxAge. */
3351 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
3353 switch (lsa->data->type)
3355 #ifdef HAVE_OPAQUE_LSA
3356 case OSPF_OPAQUE_LINK_LSA:
3357 case OSPF_OPAQUE_AREA_LSA:
3358 case OSPF_OPAQUE_AS_LSA:
3359 ospf_opaque_lsa_refresh (lsa);
3360 break;
3361 #endif /* HAVE_OPAQUE_LSA */
3362 default:
3363 ospf_lsa_maxage (ospf, lsa);
3364 break;
3367 return 0;
3370 void
3371 ospf_flush_self_originated_lsas_now (struct ospf *ospf)
3373 struct listnode *node, *nnode;
3374 struct listnode *node2, *nnode2;
3375 struct ospf_area *area;
3376 struct ospf_interface *oi;
3377 struct ospf_lsa *lsa;
3378 struct route_node *rn;
3379 int need_to_flush_ase = 0;
3381 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
3383 if ((lsa = area->router_lsa_self) != NULL)
3385 if (IS_DEBUG_OSPF_EVENT)
3386 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3388 ospf_lsa_flush_area (lsa, area);
3389 ospf_lsa_unlock (&area->router_lsa_self);
3390 area->router_lsa_self = NULL;
3391 OSPF_TIMER_OFF (area->t_router_lsa_self);
3394 for (ALL_LIST_ELEMENTS (area->oiflist, node2, nnode2, oi))
3396 if ((lsa = oi->network_lsa_self) != NULL
3397 && oi->state == ISM_DR
3398 && oi->full_nbrs > 0)
3400 if (IS_DEBUG_OSPF_EVENT)
3401 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3403 ospf_lsa_flush_area (oi->network_lsa_self, area);
3404 ospf_lsa_unlock (&oi->network_lsa_self);
3405 oi->network_lsa_self = NULL;
3406 OSPF_TIMER_OFF (oi->t_network_lsa_self);
3409 if (oi->type != OSPF_IFTYPE_VIRTUALLINK
3410 && area->external_routing == OSPF_AREA_DEFAULT)
3411 need_to_flush_ase = 1;
3414 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
3415 ospf_lsa_flush_schedule (ospf, lsa);
3416 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
3417 ospf_lsa_flush_schedule (ospf, lsa);
3418 #ifdef HAVE_OPAQUE_LSA
3419 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
3420 ospf_lsa_flush_schedule (ospf, lsa);
3421 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
3422 ospf_lsa_flush_schedule (ospf, lsa);
3423 #endif /* HAVE_OPAQUE_LSA */
3426 if (need_to_flush_ase)
3428 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
3429 ospf_lsa_flush_schedule (ospf, lsa);
3430 #ifdef HAVE_OPAQUE_LSA
3431 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
3432 ospf_lsa_flush_schedule (ospf, lsa);
3433 #endif /* HAVE_OPAQUE_LSA */
3437 * Make sure that the MaxAge LSA remover is executed immediately,
3438 * without conflicting to other threads.
3440 if (ospf->t_maxage != NULL)
3442 OSPF_TIMER_OFF (ospf->t_maxage);
3443 thread_execute (master, ospf_maxage_lsa_remover, ospf, 0);
3446 return;
3448 #endif /* ORIGINAL_CODING */
3450 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3451 /* An interface-independent version of ospf_lsa_is_self_originated */
3452 int
3453 ospf_lsa_is_self_originated (struct ospf *ospf, struct ospf_lsa *lsa)
3455 struct listnode *node;
3456 struct ospf_interface *oi;
3458 /* This LSA is already checked. */
3459 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED))
3460 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3462 /* Make sure LSA is self-checked. */
3463 SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
3465 /* AdvRouter and Router ID is the same. */
3466 if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf->router_id))
3467 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3469 /* LSA is router-LSA. */
3470 else if (lsa->data->type == OSPF_ROUTER_LSA &&
3471 IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
3472 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3474 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3475 else if (lsa->data->type == OSPF_NETWORK_LSA)
3476 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
3478 /* Ignore virtual link. */
3479 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
3480 if (oi->address->family == AF_INET)
3481 if (IPV4_ADDR_SAME (&lsa->data->id, &oi->address->u.prefix4))
3483 /* to make it easier later */
3484 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3485 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3489 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3492 /* Get unique Link State ID. */
3493 struct in_addr
3494 ospf_lsa_unique_id (struct ospf *ospf,
3495 struct ospf_lsdb *lsdb, u_char type, struct prefix_ipv4 *p)
3497 struct ospf_lsa *lsa;
3498 struct in_addr mask, id;
3500 id = p->prefix;
3502 /* Check existence of LSA instance. */
3503 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, ospf->router_id);
3504 if (lsa)
3506 struct as_external_lsa *al = (struct as_external_lsa *) lsa->data;
3507 if (ip_masklen (al->mask) == p->prefixlen)
3509 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3510 zlog_debug ("ospf_lsa_unique_id(): "
3511 "Can't get Link State ID for %s/%d",
3512 inet_ntoa (p->prefix), p->prefixlen);
3513 /* id.s_addr = 0; */
3514 id.s_addr = 0xffffffff;
3515 return id;
3517 /* Masklen differs, then apply wildcard mask to Link State ID. */
3518 else
3520 masklen2ip (p->prefixlen, &mask);
3522 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3523 lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, type,
3524 id, ospf->router_id);
3525 if (lsa)
3527 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3528 zlog_debug ("ospf_lsa_unique_id(): "
3529 "Can't get Link State ID for %s/%d",
3530 inet_ntoa (p->prefix), p->prefixlen);
3531 /* id.s_addr = 0; */
3532 id.s_addr = 0xffffffff;
3533 return id;
3538 return id;
3542 #define LSA_ACTION_FLOOD_AREA 1
3543 #define LSA_ACTION_FLUSH_AREA 2
3545 struct lsa_action
3547 u_char action;
3548 struct ospf_area *area;
3549 struct ospf_lsa *lsa;
3552 static int
3553 ospf_lsa_action (struct thread *t)
3555 struct lsa_action *data;
3557 data = THREAD_ARG (t);
3559 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3560 zlog_debug ("LSA[Action]: Performing scheduled LSA action: %d",
3561 data->action);
3563 switch (data->action)
3565 case LSA_ACTION_FLOOD_AREA:
3566 ospf_flood_through_area (data->area, NULL, data->lsa);
3567 break;
3568 case LSA_ACTION_FLUSH_AREA:
3569 ospf_lsa_flush_area (data->lsa, data->area);
3570 break;
3573 ospf_lsa_unlock (&data->lsa); /* Message */
3574 XFREE (MTYPE_OSPF_MESSAGE, data);
3575 return 0;
3578 void
3579 ospf_schedule_lsa_flood_area (struct ospf_area *area, struct ospf_lsa *lsa)
3581 struct lsa_action *data;
3583 data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3584 data->action = LSA_ACTION_FLOOD_AREA;
3585 data->area = area;
3586 data->lsa = ospf_lsa_lock (lsa); /* Message / Flood area */
3588 thread_add_event (master, ospf_lsa_action, data, 0);
3591 void
3592 ospf_schedule_lsa_flush_area (struct ospf_area *area, struct ospf_lsa *lsa)
3594 struct lsa_action *data;
3596 data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3597 data->action = LSA_ACTION_FLUSH_AREA;
3598 data->area = area;
3599 data->lsa = ospf_lsa_lock (lsa); /* Message / Flush area */
3601 thread_add_event (master, ospf_lsa_action, data, 0);
3605 /* LSA Refreshment functions. */
3606 static void
3607 ospf_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
3609 struct external_info *ei;
3610 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3612 switch (lsa->data->type)
3614 /* Router and Network LSAs are processed differently. */
3615 case OSPF_ROUTER_LSA:
3616 case OSPF_NETWORK_LSA:
3617 break;
3618 case OSPF_SUMMARY_LSA:
3619 ospf_summary_lsa_refresh (ospf, lsa);
3620 break;
3621 case OSPF_ASBR_SUMMARY_LSA:
3622 ospf_summary_asbr_lsa_refresh (ospf, lsa);
3623 break;
3624 case OSPF_AS_EXTERNAL_LSA:
3625 /* Translated from NSSA Type-5s are refreshed when
3626 * from refresh of Type-7 - do not refresh these directly.
3628 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
3629 break;
3630 ei = ospf_external_info_check (lsa);
3631 if (ei)
3632 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
3633 else
3634 ospf_lsa_flush_as (ospf, lsa);
3635 break;
3636 #ifdef HAVE_OPAQUE_LSA
3637 case OSPF_OPAQUE_LINK_LSA:
3638 case OSPF_OPAQUE_AREA_LSA:
3639 case OSPF_OPAQUE_AS_LSA:
3640 ospf_opaque_lsa_refresh (lsa);
3641 break;
3642 #endif /* HAVE_OPAQUE_LSA */
3643 default:
3644 break;
3648 void
3649 ospf_refresher_register_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3651 u_int16_t index, current_index;
3653 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3655 if (lsa->refresh_list < 0)
3657 int delay;
3659 if (LS_AGE (lsa) == 0 &&
3660 ntohl (lsa->data->ls_seqnum) == OSPF_INITIAL_SEQUENCE_NUMBER)
3661 /* Randomize first update by OSPF_LS_REFRESH_SHIFT factor */
3662 delay = OSPF_LS_REFRESH_SHIFT + (random () % OSPF_LS_REFRESH_TIME);
3663 else
3664 /* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */
3665 delay = OSPF_LS_REFRESH_TIME - LS_AGE (lsa) - OSPF_LS_REFRESH_JITTER
3666 + (random () % (2*OSPF_LS_REFRESH_JITTER));
3668 if (delay < 0)
3669 delay = 0;
3671 current_index = ospf->lsa_refresh_queue.index +
3672 (quagga_time (NULL) - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
3674 index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY)
3675 % (OSPF_LSA_REFRESHER_SLOTS);
3677 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3678 zlog_debug ("LSA[Refresh]: lsa %s with age %d added to index %d",
3679 inet_ntoa (lsa->data->id), LS_AGE (lsa), index);
3680 if (!ospf->lsa_refresh_queue.qs[index])
3681 ospf->lsa_refresh_queue.qs[index] = list_new ();
3682 listnode_add (ospf->lsa_refresh_queue.qs[index],
3683 ospf_lsa_lock (lsa)); /* lsa_refresh_queue */
3684 lsa->refresh_list = index;
3685 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3686 zlog_debug ("LSA[Refresh:%s]: ospf_refresher_register_lsa(): "
3687 "setting refresh_list on lsa %p (slod %d)",
3688 inet_ntoa (lsa->data->id), lsa, index);
3692 void
3693 ospf_refresher_unregister_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3695 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3696 if (lsa->refresh_list >= 0)
3698 struct list *refresh_list = ospf->lsa_refresh_queue.qs[lsa->refresh_list];
3699 listnode_delete (refresh_list, lsa);
3700 if (!listcount (refresh_list))
3702 list_free (refresh_list);
3703 ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
3705 ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
3706 lsa->refresh_list = -1;
3711 ospf_lsa_refresh_walker (struct thread *t)
3713 struct list *refresh_list;
3714 struct listnode *node, *nnode;
3715 struct ospf *ospf = THREAD_ARG (t);
3716 struct ospf_lsa *lsa;
3717 int i;
3718 struct list *lsa_to_refresh = list_new ();
3720 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3721 zlog_debug ("LSA[Refresh]:ospf_lsa_refresh_walker(): start");
3724 i = ospf->lsa_refresh_queue.index;
3726 /* Note: if clock has jumped backwards, then time change could be negative,
3727 so we are careful to cast the expression to unsigned before taking
3728 modulus. */
3729 ospf->lsa_refresh_queue.index =
3730 ((unsigned long)(ospf->lsa_refresh_queue.index +
3731 (quagga_time (NULL) - ospf->lsa_refresher_started) /
3732 OSPF_LSA_REFRESHER_GRANULARITY)) % OSPF_LSA_REFRESHER_SLOTS;
3734 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3735 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3736 ospf->lsa_refresh_queue.index);
3738 for (;i != ospf->lsa_refresh_queue.index;
3739 i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS)
3741 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3742 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): "
3743 "refresh index %d", i);
3745 refresh_list = ospf->lsa_refresh_queue.qs [i];
3747 ospf->lsa_refresh_queue.qs [i] = NULL;
3749 if (refresh_list)
3751 for (ALL_LIST_ELEMENTS (refresh_list, node, nnode, lsa))
3753 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3754 zlog_debug ("LSA[Refresh:%s]: ospf_lsa_refresh_walker(): "
3755 "refresh lsa %p (slot %d)",
3756 inet_ntoa (lsa->data->id), lsa, i);
3758 list_delete_node (refresh_list, node);
3759 ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
3760 lsa->refresh_list = -1;
3761 listnode_add (lsa_to_refresh, lsa);
3763 list_free (refresh_list);
3767 ospf->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
3768 ospf, ospf->lsa_refresh_interval);
3769 ospf->lsa_refresher_started = quagga_time (NULL);
3771 for (ALL_LIST_ELEMENTS (lsa_to_refresh, node, nnode, lsa))
3772 ospf_lsa_refresh (ospf, lsa);
3774 list_delete (lsa_to_refresh);
3776 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3777 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3779 return 0;