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
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
34 #include "sockunion.h" /* for inet_aton() */
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"
52 #include "ospfd/ospf_abr.h"
56 get_metric (u_char *metric)
60 m = (m << 8) + metric[1];
61 m = (m << 8) + metric[2];
67 tv_adjust (struct timeval a)
69 while (a.tv_usec >= 1000000)
85 tv_ceil (struct timeval a)
89 return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec);
93 tv_floor (struct timeval a)
117 ret.tv_usec = a * 1000;
119 return tv_adjust (ret);
123 tv_add (struct timeval a, struct timeval b)
127 ret.tv_sec = a.tv_sec + b.tv_sec;
128 ret.tv_usec = a.tv_usec + b.tv_usec;
130 return tv_adjust (ret);
134 tv_sub (struct timeval a, struct timeval b)
138 ret.tv_sec = a.tv_sec - b.tv_sec;
139 ret.tv_usec = a.tv_usec - b.tv_usec;
141 return tv_adjust (ret);
145 tv_cmp (struct timeval a, struct timeval b)
147 return (a.tv_sec == b.tv_sec ?
148 a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
152 ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
154 struct timeval delta, now;
157 quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
158 delta = tv_sub (now, lsa->tv_orig);
160 if (tv_cmp (delta, msec2tv (OSPF_MIN_LS_INTERVAL)) < 0)
162 delay = tv_ceil (tv_sub (msec2tv (OSPF_MIN_LS_INTERVAL), delta));
164 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
165 zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
166 lsa->data->type, inet_ntoa (lsa->data->id), delay);
176 get_age (struct ospf_lsa *lsa)
180 age = ntohs (lsa->data->ls_age)
181 + tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
187 /* Fletcher Checksum -- Refer to RFC1008. */
189 /* All the offsets are zero-based. The offsets in the RFC1008 are
192 ospf_lsa_checksum (struct lsa_header *lsa)
194 u_char *buffer = (u_char *) &lsa->options;
195 int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
197 /* Skip the AGE field */
198 u_int16_t len = ntohs(lsa->length) - options_offset;
200 /* Checksum offset starts from "options" field, not the beginning of the
201 lsa_header struct. The offset is 14, rather than 16. */
202 int checksum_offset = (u_char *) &lsa->checksum - buffer;
204 return fletcher_checksum(buffer, len, checksum_offset);
208 ospf_lsa_checksum_valid (struct lsa_header *lsa)
210 u_char *buffer = (u_char *) &lsa->options;
211 int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
213 /* Skip the AGE field */
214 u_int16_t len = ntohs(lsa->length) - options_offset;
216 return(fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE) == 0);
221 /* Create OSPF LSA. */
225 struct ospf_lsa *new;
227 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
231 new->retransmit_counter = 0;
232 new->tv_recv = recent_relative_time ();
233 new->tv_orig = new->tv_recv;
234 new->refresh_list = -1;
239 /* Duplicate OSPF LSA. */
241 ospf_lsa_dup (struct ospf_lsa *lsa)
243 struct ospf_lsa *new;
248 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
250 memcpy (new, lsa, sizeof (struct ospf_lsa));
251 UNSET_FLAG (new->flags, OSPF_LSA_DISCARD);
253 new->retransmit_counter = 0;
254 new->data = ospf_lsa_data_dup (lsa->data);
256 /* kevinm: Clear the refresh_list, otherwise there are going
257 to be problems when we try to remove the LSA from the
258 queue (which it's not a member of.)
259 XXX: Should we add the LSA to the refresh_list queue? */
260 new->refresh_list = -1;
262 if (IS_DEBUG_OSPF (lsa, LSA))
263 zlog_debug ("LSA: duplicated %p (new: %p)", (void *)lsa, (void *)new);
270 ospf_lsa_free (struct ospf_lsa *lsa)
272 assert (lsa->lock == 0);
274 if (IS_DEBUG_OSPF (lsa, LSA))
275 zlog_debug ("LSA: freed %p", (void *)lsa);
277 /* Delete LSA data. */
278 if (lsa->data != NULL)
279 ospf_lsa_data_free (lsa->data);
281 assert (lsa->refresh_list < 0);
283 memset (lsa, 0, sizeof (struct ospf_lsa));
284 XFREE (MTYPE_OSPF_LSA, lsa);
289 ospf_lsa_lock (struct ospf_lsa *lsa)
297 ospf_lsa_unlock (struct ospf_lsa **lsa)
299 /* This is sanity check. */
305 assert ((*lsa)->lock >= 0);
307 if ((*lsa)->lock == 0)
309 assert (CHECK_FLAG ((*lsa)->flags, OSPF_LSA_DISCARD));
310 ospf_lsa_free (*lsa);
315 /* Check discard flag. */
317 ospf_lsa_discard (struct ospf_lsa *lsa)
319 if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
321 SET_FLAG (lsa->flags, OSPF_LSA_DISCARD);
322 ospf_lsa_unlock (&lsa);
326 /* Create LSA data. */
328 ospf_lsa_data_new (size_t size)
330 return XCALLOC (MTYPE_OSPF_LSA_DATA, size);
333 /* Duplicate LSA data. */
335 ospf_lsa_data_dup (struct lsa_header *lsah)
337 struct lsa_header *new;
339 new = ospf_lsa_data_new (ntohs (lsah->length));
340 memcpy (new, lsah, ntohs (lsah->length));
347 ospf_lsa_data_free (struct lsa_header *lsah)
349 if (IS_DEBUG_OSPF (lsa, LSA))
350 zlog_debug ("LSA[Type%d:%s]: data freed %p",
351 lsah->type, inet_ntoa (lsah->id), (void *)lsah);
353 XFREE (MTYPE_OSPF_LSA_DATA, lsah);
357 /* LSA general functions. */
360 dump_lsa_key (struct ospf_lsa *lsa)
362 static char buf[] = {
363 "Type255,id(255.255.255.255),ar(255.255.255.255)"
365 struct lsa_header *lsah;
367 if (lsa != NULL && (lsah = lsa->data) != NULL)
369 char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
370 strcpy (id, inet_ntoa (lsah->id));
371 strcpy (ar, inet_ntoa (lsah->adv_router));
373 sprintf (buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar);
376 strcpy (buf, "NULL");
382 lsa_seqnum_increment (struct ospf_lsa *lsa)
386 seqnum = ntohl (lsa->data->ls_seqnum) + 1;
388 return htonl (seqnum);
392 lsa_header_set (struct stream *s, u_char options,
393 u_char type, struct in_addr id, struct in_addr router_id)
395 struct lsa_header *lsah;
397 lsah = (struct lsa_header *) STREAM_DATA (s);
399 lsah->ls_age = htons (OSPF_LSA_INITIAL_AGE);
400 lsah->options = options;
403 lsah->adv_router = router_id;
404 lsah->ls_seqnum = htonl (OSPF_INITIAL_SEQUENCE_NUMBER);
406 stream_forward_endp (s, OSPF_LSA_HEADER_SIZE);
410 /* router-LSA related functions. */
411 /* Get router-LSA flags. */
413 router_lsa_flags (struct ospf_area *area)
417 flags = area->ospf->flags;
419 /* Set virtual link flag. */
420 if (ospf_full_virtual_nbrs (area))
421 SET_FLAG (flags, ROUTER_LSA_VIRTUAL);
423 /* Just sanity check */
424 UNSET_FLAG (flags, ROUTER_LSA_VIRTUAL);
426 /* Set Shortcut ABR behabiour flag. */
427 UNSET_FLAG (flags, ROUTER_LSA_SHORTCUT);
428 if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
429 if (!OSPF_IS_AREA_BACKBONE (area))
430 if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
431 area->ospf->backbone == NULL) ||
432 area->shortcut_configured == OSPF_SHORTCUT_ENABLE)
433 SET_FLAG (flags, ROUTER_LSA_SHORTCUT);
435 /* ASBR can't exit in stub area. */
436 if (area->external_routing == OSPF_AREA_STUB)
437 UNSET_FLAG (flags, ROUTER_LSA_EXTERNAL);
438 /* If ASBR set External flag */
439 else if (IS_OSPF_ASBR (area->ospf))
440 SET_FLAG (flags, ROUTER_LSA_EXTERNAL);
442 /* Set ABR dependent flags */
443 if (IS_OSPF_ABR (area->ospf))
445 SET_FLAG (flags, ROUTER_LSA_BORDER);
446 /* If Area is NSSA and we are both ABR and unconditional translator,
447 * set Nt bit to inform other routers.
449 if ( (area->external_routing == OSPF_AREA_NSSA)
450 && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
451 SET_FLAG (flags, ROUTER_LSA_NT);
456 /* Lookup neighbor other than myself.
457 And check neighbor count,
458 Point-to-Point link must have only 1 neighbor. */
459 struct ospf_neighbor *
460 ospf_nbr_lookup_ptop (struct ospf_interface *oi)
462 struct ospf_neighbor *nbr = NULL;
463 struct route_node *rn;
465 /* Search neighbor, there must be one of two nbrs. */
466 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
467 if ((nbr = rn->info))
468 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
469 if (nbr->state == NSM_Full)
471 route_unlock_node (rn);
475 /* PtoP link must have only 1 neighbor. */
476 if (ospf_nbr_count (oi, 0) > 1)
477 zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
482 /* Determine cost of link, taking RFC3137 stub-router support into
486 ospf_link_cost (struct ospf_interface *oi)
488 /* RFC3137 stub router support */
489 if (!CHECK_FLAG (oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
490 return oi->output_cost;
492 return OSPF_OUTPUT_COST_INFINITE;
495 /* Set a link information. */
497 link_info_set (struct stream *s, struct in_addr id,
498 struct in_addr data, u_char type, u_char tos, u_int16_t cost)
500 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
501 * vast majority of cases. Some rare routers with lots of links need more.
502 * we try accomodate those here.
504 if (STREAM_WRITEABLE(s) < OSPF_ROUTER_LSA_LINK_SIZE)
506 size_t ret = OSPF_MAX_LSA_SIZE;
508 /* Can we enlarge the stream still? */
509 if (STREAM_SIZE(s) == OSPF_MAX_LSA_SIZE)
511 /* we futz the size here for simplicity, really we need to account
513 * IP Header - (sizeof (struct ip))
514 * OSPF Header - OSPF_HEADER_SIZE
515 * LSA Header - OSPF_LSA_HEADER_SIZE
516 * MD5 auth data, if MD5 is configured - OSPF_AUTH_MD5_SIZE.
518 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
520 ret = stream_resize (s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
523 if (ret == OSPF_MAX_LSA_SIZE)
525 zlog_warn ("%s: Out of space in LSA stream, left %zd, size %zd",
526 __func__, STREAM_REMAIN (s), STREAM_SIZE (s));
531 /* TOS based routing is not supported. */
532 stream_put_ipv4 (s, id.s_addr); /* Link ID. */
533 stream_put_ipv4 (s, data.s_addr); /* Link Data. */
534 stream_putc (s, type); /* Link Type. */
535 stream_putc (s, tos); /* TOS = 0. */
536 stream_putw (s, cost); /* Link Cost. */
541 /* Describe Point-to-Point link (Section 12.4.1.1). */
543 lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi)
546 struct ospf_neighbor *nbr;
547 struct in_addr id, mask;
548 u_int16_t cost = ospf_link_cost (oi);
550 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
551 zlog_debug ("LSA[Type1]: Set link Point-to-Point");
553 if ((nbr = ospf_nbr_lookup_ptop (oi)))
554 if (nbr->state == NSM_Full)
556 /* For unnumbered point-to-point networks, the Link Data field
557 should specify the interface's MIB-II ifIndex value. */
558 links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
559 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
562 /* Regardless of the state of the neighboring router, we must
563 add a Type 3 link (stub network).
564 N.B. Options 1 & 2 share basically the same logic. */
565 masklen2ip (oi->address->prefixlen, &mask);
566 id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr & mask.s_addr;
567 links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
572 /* Describe Broadcast Link. */
574 lsa_link_broadcast_set (struct stream *s, struct ospf_interface *oi)
576 struct ospf_neighbor *dr;
577 struct in_addr id, mask;
578 u_int16_t cost = ospf_link_cost (oi);
580 /* Describe Type 3 Link. */
581 if (oi->state == ISM_Waiting)
583 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
584 zlog_debug ("LSA[Type1]: Interface %s is in state Waiting. "
585 "Adding stub interface", oi->ifp->name);
586 masklen2ip (oi->address->prefixlen, &mask);
587 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
588 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
592 dr = ospf_nbr_lookup_by_addr (oi->nbrs, &DR (oi));
593 /* Describe Type 2 link. */
594 if (dr && (dr->state == NSM_Full ||
595 IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))) &&
596 ospf_nbr_count (oi, NSM_Full) > 0)
598 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
599 zlog_debug ("LSA[Type1]: Interface %s has a DR. "
600 "Adding transit interface", oi->ifp->name);
601 return link_info_set (s, DR (oi), oi->address->u.prefix4,
602 LSA_LINK_TYPE_TRANSIT, 0, cost);
604 /* Describe type 3 link. */
607 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
608 zlog_debug ("LSA[Type1]: Interface %s has no DR. "
609 "Adding stub interface", oi->ifp->name);
610 masklen2ip (oi->address->prefixlen, &mask);
611 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
612 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
618 lsa_link_loopback_set (struct stream *s, struct ospf_interface *oi)
620 struct in_addr id, mask;
622 /* Describe Type 3 Link. */
623 if (oi->state != ISM_Loopback)
626 mask.s_addr = 0xffffffff;
627 id.s_addr = oi->address->u.prefix4.s_addr;
628 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
631 /* Describe Virtual Link. */
633 lsa_link_virtuallink_set (struct stream *s, struct ospf_interface *oi)
635 struct ospf_neighbor *nbr;
636 u_int16_t cost = ospf_link_cost (oi);
638 if (oi->state == ISM_PointToPoint)
639 if ((nbr = ospf_nbr_lookup_ptop (oi)))
640 if (nbr->state == NSM_Full)
642 return link_info_set (s, nbr->router_id, oi->address->u.prefix4,
643 LSA_LINK_TYPE_VIRTUALLINK, 0, cost);
649 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
651 /* this function add for support point-to-multipoint ,see rfc2328
653 /* from "edward rrr" <edward_rrr@hotmail.com>
654 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
656 lsa_link_ptomp_set (struct stream *s, struct ospf_interface *oi)
659 struct route_node *rn;
660 struct ospf_neighbor *nbr = NULL;
661 struct in_addr id, mask;
662 u_int16_t cost = ospf_link_cost (oi);
664 mask.s_addr = 0xffffffff;
665 id.s_addr = oi->address->u.prefix4.s_addr;
666 links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
668 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
669 zlog_debug ("PointToMultipoint: running ptomultip_set");
671 /* Search neighbor, */
672 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
673 if ((nbr = rn->info) != NULL)
675 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
676 if (nbr->state == NSM_Full)
679 links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
680 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
681 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
682 zlog_debug ("PointToMultipoint: set link to %s",
683 inet_ntoa(oi->address->u.prefix4));
689 /* Set router-LSA link information. */
691 router_lsa_link_set (struct stream *s, struct ospf_area *area)
693 struct listnode *node;
694 struct ospf_interface *oi;
697 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
699 struct interface *ifp = oi->ifp;
701 /* Check interface is up, OSPF is enable. */
702 if (if_is_operative (ifp))
704 if (oi->state != ISM_Down)
706 oi->lsa_pos_beg = links;
707 /* Describe each link. */
710 case OSPF_IFTYPE_POINTOPOINT:
711 links += lsa_link_ptop_set (s, oi);
713 case OSPF_IFTYPE_BROADCAST:
714 links += lsa_link_broadcast_set (s, oi);
716 case OSPF_IFTYPE_NBMA:
717 links += lsa_link_nbma_set (s, oi);
719 case OSPF_IFTYPE_POINTOMULTIPOINT:
720 links += lsa_link_ptomp_set (s, oi);
722 case OSPF_IFTYPE_VIRTUALLINK:
723 links += lsa_link_virtuallink_set (s, oi);
725 case OSPF_IFTYPE_LOOPBACK:
726 links += lsa_link_loopback_set (s, oi);
728 oi->lsa_pos_end = links;
736 /* Set router-LSA body. */
738 ospf_router_lsa_body_set (struct stream *s, struct ospf_area *area)
744 stream_putc (s, router_lsa_flags (area));
746 /* Set Zero fields. */
749 /* Keep pointer to # links. */
750 putp = stream_get_endp(s);
755 /* Set all link information. */
756 cnt = router_lsa_link_set (s, area);
758 /* Set # of links here. */
759 stream_putw_at (s, putp, cnt);
763 ospf_stub_router_timer (struct thread *t)
765 struct ospf_area *area = THREAD_ARG (t);
767 area->t_stub_router = NULL;
769 SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
771 /* clear stub route state and generate router-lsa refresh, don't
772 * clobber an administratively set stub-router state though.
774 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
777 UNSET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
779 ospf_router_lsa_update_area (area);
785 ospf_stub_router_check (struct ospf_area *area)
787 /* area must either be administratively configured to be stub
788 * or startup-time stub-router must be configured and we must in a pre-stub
791 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
793 SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
797 /* not admin-stubbed, check whether startup stubbing is configured and
798 * whether it's not been done yet
800 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED))
803 if (area->ospf->stub_router_startup_time == OSPF_STUB_ROUTER_UNCONFIGURED)
805 /* stub-router is hence done forever for this area, even if someone
806 * tries configure it (take effect next restart).
808 SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
812 /* startup stub-router configured and not yet done */
813 SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
815 OSPF_AREA_TIMER_ON (area->t_stub_router, ospf_stub_router_timer,
816 area->ospf->stub_router_startup_time);
819 /* Create new router-LSA. */
820 static struct ospf_lsa *
821 ospf_router_lsa_new (struct ospf_area *area)
823 struct ospf *ospf = area->ospf;
825 struct lsa_header *lsah;
826 struct ospf_lsa *new;
829 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
830 zlog_debug ("LSA[Type1]: Create router-LSA instance");
832 /* check whether stub-router is desired, and if this is the first
835 ospf_stub_router_check (area);
837 /* Create a stream for LSA. */
838 s = stream_new (OSPF_MAX_LSA_SIZE);
839 /* Set LSA common header fields. */
840 lsa_header_set (s, LSA_OPTIONS_GET (area) | LSA_OPTIONS_NSSA_GET (area),
841 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
843 /* Set router-LSA body fields. */
844 ospf_router_lsa_body_set (s, area);
847 length = stream_get_endp (s);
848 lsah = (struct lsa_header *) STREAM_DATA (s);
849 lsah->length = htons (length);
851 /* Now, create OSPF LSA instance. */
852 if ( (new = ospf_lsa_new ()) == NULL)
854 zlog_err ("%s: Unable to create new lsa", __func__);
859 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
861 /* Copy LSA data to store, discard stream. */
862 new->data = ospf_lsa_data_new (length);
863 memcpy (new->data, lsah, length);
869 /* Originate Router-LSA. */
870 static struct ospf_lsa *
871 ospf_router_lsa_originate (struct ospf_area *area)
873 struct ospf_lsa *new;
875 /* Create new router-LSA instance. */
876 if ( (new = ospf_router_lsa_new (area)) == NULL)
878 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
883 if (new->data->adv_router.s_addr == 0)
885 if (IS_DEBUG_OSPF_EVENT)
886 zlog_debug ("LSA[Type1]: AdvRouter is 0, discard");
887 ospf_lsa_discard (new);
891 /* Install LSA to LSDB. */
892 new = ospf_lsa_install (area->ospf, NULL, new);
894 /* Update LSA origination count. */
895 area->ospf->lsa_originate_count++;
897 /* Flooding new LSA through area. */
898 ospf_flood_through_area (area, NULL, new);
900 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
902 zlog_debug ("LSA[Type%d:%s]: Originate router-LSA %p",
903 new->data->type, inet_ntoa (new->data->id), (void *)new);
904 ospf_lsa_header_dump (new->data);
910 /* Refresh router-LSA. */
911 static struct ospf_lsa *
912 ospf_router_lsa_refresh (struct ospf_lsa *lsa)
914 struct ospf_area *area = lsa->area;
915 struct ospf_lsa *new;
920 /* Delete LSA from neighbor retransmit-list. */
921 ospf_ls_retransmit_delete_nbr_area (area, lsa);
923 /* Unregister LSA from refresh-list */
924 ospf_refresher_unregister_lsa (area->ospf, lsa);
926 /* Create new router-LSA instance. */
927 if ( (new = ospf_router_lsa_new (area)) == NULL)
929 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
933 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
935 ospf_lsa_install (area->ospf, NULL, new);
937 /* Flood LSA through area. */
938 ospf_flood_through_area (area, NULL, new);
941 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
943 zlog_debug ("LSA[Type%d:%s]: router-LSA refresh",
944 new->data->type, inet_ntoa (new->data->id));
945 ospf_lsa_header_dump (new->data);
952 ospf_router_lsa_update_area (struct ospf_area *area)
954 if (IS_DEBUG_OSPF_EVENT)
955 zlog_debug ("[router-LSA]: (router-LSA area update)");
957 /* Now refresh router-LSA. */
958 if (area->router_lsa_self)
959 ospf_lsa_refresh (area->ospf, area->router_lsa_self);
960 /* Newly originate router-LSA. */
962 ospf_router_lsa_originate (area);
968 ospf_router_lsa_update (struct ospf *ospf)
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 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
978 struct ospf_lsa *lsa = area->router_lsa_self;
979 struct router_lsa *rl;
980 const char *area_str;
982 /* Keep Area ID string. */
983 area_str = AREA_NAME (area);
985 /* If LSA not exist in this Area, originate new. */
988 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
989 zlog_debug("LSA[Type1]: Create router-LSA for Area %s", area_str);
991 ospf_router_lsa_originate (area);
993 /* If router-ID is changed, Link ID must change.
994 First flush old LSA, then originate new. */
995 else if (!IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
997 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
998 zlog_debug("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
999 lsa->data->type, inet_ntoa (lsa->data->id), area_str);
1000 ospf_refresher_unregister_lsa (ospf, lsa);
1001 ospf_lsa_flush_area (lsa, area);
1002 ospf_lsa_unlock (&area->router_lsa_self);
1003 area->router_lsa_self = NULL;
1005 /* Refresh router-LSA, (not install) and flood through area. */
1006 ospf_router_lsa_update_area (area);
1010 rl = (struct router_lsa *) lsa->data;
1011 /* Refresh router-LSA, (not install) and flood through area. */
1012 if (rl->flags != ospf->flags)
1013 ospf_router_lsa_update_area (area);
1021 /* network-LSA related functions. */
1022 /* Originate Network-LSA. */
1024 ospf_network_lsa_body_set (struct stream *s, struct ospf_interface *oi)
1026 struct in_addr mask;
1027 struct route_node *rn;
1028 struct ospf_neighbor *nbr;
1030 masklen2ip (oi->address->prefixlen, &mask);
1031 stream_put_ipv4 (s, mask.s_addr);
1033 /* The network-LSA lists those routers that are fully adjacent to
1034 the Designated Router; each fully adjacent router is identified by
1035 its OSPF Router ID. The Designated Router includes itself in this
1036 list. RFC2328, Section 12.4.2 */
1038 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
1039 if ((nbr = rn->info) != NULL)
1040 if (nbr->state == NSM_Full || nbr == oi->nbr_self)
1041 stream_put_ipv4 (s, nbr->router_id.s_addr);
1044 static struct ospf_lsa *
1045 ospf_network_lsa_new (struct ospf_interface *oi)
1048 struct ospf_lsa *new;
1049 struct lsa_header *lsah;
1050 struct ospf_if_params *oip;
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)
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);
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__);
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);
1090 /* Remember prior network LSA sequence numbers, even if we stop
1091 * originating one for this oi, to try avoid re-originating LSAs with a
1092 * prior sequence number, and thus speed up adjency forming & convergence.
1094 if ((oip = ospf_lookup_if_params (oi->ifp, oi->address->u.prefix4)))
1096 new->data->ls_seqnum = oip->network_lsa_seqnum;
1097 new->data->ls_seqnum = lsa_seqnum_increment (new);
1101 oip = ospf_get_if_params (oi->ifp, oi->address->u.prefix4);
1102 ospf_if_update_params (oi->ifp, oi->address->u.prefix4);
1104 oip->network_lsa_seqnum = new->data->ls_seqnum;
1109 /* Originate network-LSA. */
1111 ospf_network_lsa_update (struct ospf_interface *oi)
1113 struct ospf_lsa *new;
1115 if (oi->network_lsa_self != NULL)
1117 ospf_lsa_refresh (oi->ospf, oi->network_lsa_self);
1121 /* Create new network-LSA instance. */
1122 new = ospf_network_lsa_new (oi);
1126 /* Install LSA to LSDB. */
1127 new = ospf_lsa_install (oi->ospf, oi, new);
1129 /* Update LSA origination count. */
1130 oi->ospf->lsa_originate_count++;
1132 /* Flooding new LSA through area. */
1133 ospf_flood_through_area (oi->area, NULL, new);
1135 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1137 zlog_debug ("LSA[Type%d:%s]: Originate network-LSA %p",
1138 new->data->type, inet_ntoa (new->data->id), (void *)new);
1139 ospf_lsa_header_dump (new->data);
1145 static struct ospf_lsa *
1146 ospf_network_lsa_refresh (struct ospf_lsa *lsa)
1148 struct ospf_area *area = lsa->area;
1149 struct ospf_lsa *new, *new2;
1150 struct ospf_if_params *oip;
1151 struct ospf_interface *oi;
1155 /* Retrieve the oi for the network LSA */
1156 oi = ospf_if_lookup_by_local_addr (area->ospf, NULL, lsa->data->id);
1159 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1161 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh: "
1162 "no oi found, ick, ignoring.",
1163 lsa->data->type, inet_ntoa (lsa->data->id));
1164 ospf_lsa_header_dump (lsa->data);
1168 /* Delete LSA from neighbor retransmit-list. */
1169 ospf_ls_retransmit_delete_nbr_area (area, lsa);
1171 /* Unregister LSA from refresh-list */
1172 ospf_refresher_unregister_lsa (area->ospf, lsa);
1174 /* Create new network-LSA instance. */
1175 new = ospf_network_lsa_new (oi);
1179 oip = ospf_lookup_if_params (oi->ifp, oi->address->u.prefix4);
1180 assert (oip != NULL);
1181 oip->network_lsa_seqnum = new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1183 new2 = ospf_lsa_install (area->ospf, oi, new);
1185 assert (new2 == new);
1187 /* Flood LSA through aera. */
1188 ospf_flood_through_area (area, NULL, new);
1190 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1192 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh",
1193 new->data->type, inet_ntoa (new->data->id));
1194 ospf_lsa_header_dump (new->data);
1201 stream_put_ospf_metric (struct stream *s, u_int32_t metric_value)
1206 /* Put 0 metric. TOS metric is not supported. */
1207 metric = htonl (metric_value);
1208 mp = (char *) &metric;
1210 stream_put (s, mp, 3);
1213 /* summary-LSA related functions. */
1215 ospf_summary_lsa_body_set (struct stream *s, struct prefix *p,
1218 struct in_addr mask;
1220 masklen2ip (p->prefixlen, &mask);
1222 /* Put Network Mask. */
1223 stream_put_ipv4 (s, mask.s_addr);
1226 stream_putc (s, (u_char) 0);
1229 stream_put_ospf_metric (s, metric);
1232 static struct ospf_lsa *
1233 ospf_summary_lsa_new (struct ospf_area *area, struct prefix *p,
1234 u_int32_t metric, struct in_addr id)
1237 struct ospf_lsa *new;
1238 struct lsa_header *lsah;
1241 if (id.s_addr == 0xffffffff)
1243 /* Maybe Link State ID not available. */
1244 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1245 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1250 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1251 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1253 /* Create new stream for LSA. */
1254 s = stream_new (OSPF_MAX_LSA_SIZE);
1255 lsah = (struct lsa_header *) STREAM_DATA (s);
1257 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_SUMMARY_LSA,
1258 id, area->ospf->router_id);
1260 /* Set summary-LSA body fields. */
1261 ospf_summary_lsa_body_set (s, p, metric);
1264 length = stream_get_endp (s);
1265 lsah->length = htons (length);
1267 /* Create OSPF LSA instance. */
1268 new = ospf_lsa_new ();
1270 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1272 /* Copy LSA to store. */
1273 new->data = ospf_lsa_data_new (length);
1274 memcpy (new->data, lsah, length);
1280 /* Originate Summary-LSA. */
1282 ospf_summary_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1283 struct ospf_area *area)
1285 struct ospf_lsa *new;
1288 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
1290 if (id.s_addr == 0xffffffff)
1292 /* Maybe Link State ID not available. */
1293 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1294 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1299 /* Create new summary-LSA instance. */
1300 if ( !(new = ospf_summary_lsa_new (area, (struct prefix *) p, metric, id)))
1303 /* Instlal LSA to LSDB. */
1304 new = ospf_lsa_install (area->ospf, NULL, new);
1306 /* Update LSA origination count. */
1307 area->ospf->lsa_originate_count++;
1309 /* Flooding new LSA through area. */
1310 ospf_flood_through_area (area, NULL, new);
1312 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1314 zlog_debug ("LSA[Type%d:%s]: Originate summary-LSA %p",
1315 new->data->type, inet_ntoa (new->data->id), (void *)new);
1316 ospf_lsa_header_dump (new->data);
1322 static struct ospf_lsa*
1323 ospf_summary_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1325 struct ospf_lsa *new;
1326 struct summary_lsa *sl;
1332 sl = (struct summary_lsa *)lsa->data;
1333 p.prefixlen = ip_masklen (sl->mask);
1334 new = ospf_summary_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1340 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1342 ospf_lsa_install (ospf, NULL, new);
1344 /* Flood LSA through AS. */
1345 ospf_flood_through_area (new->area, NULL, new);
1347 /* Debug logging. */
1348 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1350 zlog_debug ("LSA[Type%d:%s]: summary-LSA refresh",
1351 new->data->type, inet_ntoa (new->data->id));
1352 ospf_lsa_header_dump (new->data);
1359 /* summary-ASBR-LSA related functions. */
1361 ospf_summary_asbr_lsa_body_set (struct stream *s, struct prefix *p,
1364 /* Put Network Mask. */
1365 stream_put_ipv4 (s, (u_int32_t) 0);
1368 stream_putc (s, (u_char) 0);
1371 stream_put_ospf_metric (s, metric);
1374 static struct ospf_lsa *
1375 ospf_summary_asbr_lsa_new (struct ospf_area *area, struct prefix *p,
1376 u_int32_t metric, struct in_addr id)
1379 struct ospf_lsa *new;
1380 struct lsa_header *lsah;
1383 if (id.s_addr == 0xffffffff)
1385 /* Maybe Link State ID not available. */
1386 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1387 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1388 OSPF_ASBR_SUMMARY_LSA);
1392 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1393 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1395 /* Create new stream for LSA. */
1396 s = stream_new (OSPF_MAX_LSA_SIZE);
1397 lsah = (struct lsa_header *) STREAM_DATA (s);
1399 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_ASBR_SUMMARY_LSA,
1400 id, area->ospf->router_id);
1402 /* Set summary-LSA body fields. */
1403 ospf_summary_asbr_lsa_body_set (s, p, metric);
1406 length = stream_get_endp (s);
1407 lsah->length = htons (length);
1409 /* Create OSPF LSA instance. */
1410 new = ospf_lsa_new ();
1412 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
1414 /* Copy LSA to store. */
1415 new->data = ospf_lsa_data_new (length);
1416 memcpy (new->data, lsah, length);
1422 /* Originate summary-ASBR-LSA. */
1424 ospf_summary_asbr_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1425 struct ospf_area *area)
1427 struct ospf_lsa *new;
1430 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA, p);
1432 if (id.s_addr == 0xffffffff)
1434 /* Maybe Link State ID not available. */
1435 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1436 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1437 OSPF_ASBR_SUMMARY_LSA);
1441 /* Create new summary-LSA instance. */
1442 new = ospf_summary_asbr_lsa_new (area, (struct prefix *) p, metric, id);
1446 /* Install LSA to LSDB. */
1447 new = ospf_lsa_install (area->ospf, NULL, new);
1449 /* Update LSA origination count. */
1450 area->ospf->lsa_originate_count++;
1452 /* Flooding new LSA through area. */
1453 ospf_flood_through_area (area, NULL, new);
1455 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1457 zlog_debug ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1458 new->data->type, inet_ntoa (new->data->id), (void *)new);
1459 ospf_lsa_header_dump (new->data);
1465 static struct ospf_lsa*
1466 ospf_summary_asbr_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1468 struct ospf_lsa *new;
1469 struct summary_lsa *sl;
1475 sl = (struct summary_lsa *)lsa->data;
1476 p.prefixlen = ip_masklen (sl->mask);
1477 new = ospf_summary_asbr_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1482 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1484 ospf_lsa_install (ospf, NULL, new);
1486 /* Flood LSA through area. */
1487 ospf_flood_through_area (new->area, NULL, new);
1489 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1491 zlog_debug ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1492 new->data->type, inet_ntoa (new->data->id));
1493 ospf_lsa_header_dump (new->data);
1499 /* AS-external-LSA related functions. */
1501 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1502 is connected, else 0*/
1503 static struct in_addr
1504 ospf_external_lsa_nexthop_get (struct ospf *ospf, struct in_addr nexthop)
1508 struct listnode *node;
1509 struct ospf_interface *oi;
1513 if (!nexthop.s_addr)
1516 /* Check whether nexthop is covered by OSPF network. */
1517 nh.family = AF_INET;
1518 nh.u.prefix4 = nexthop;
1519 nh.prefixlen = IPV4_MAX_BITLEN;
1521 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1522 * better to make use of the per-ifp table of ois.
1524 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
1525 if (if_is_operative (oi->ifp))
1526 if (oi->address->family == AF_INET)
1527 if (prefix_match (oi->address, &nh))
1533 /* NSSA-external-LSA related functions. */
1535 /* Get 1st IP connection for Forward Addr */
1538 ospf_get_ip_from_ifp (struct ospf_interface *oi)
1544 if (if_is_operative (oi->ifp))
1545 return oi->address->u.prefix4;
1550 /* Get 1st IP connection for Forward Addr */
1552 ospf_get_nssa_ip (struct ospf_area *area)
1555 struct in_addr best_default;
1556 struct listnode *node;
1557 struct ospf_interface *oi;
1560 best_default.s_addr = 0;
1562 for (ALL_LIST_ELEMENTS_RO (area->ospf->oiflist, node, oi))
1564 if (if_is_operative (oi->ifp))
1565 if (oi->area->external_routing == OSPF_AREA_NSSA)
1566 if (oi->address && oi->address->family == AF_INET)
1568 if (best_default.s_addr == 0)
1569 best_default = oi->address->u.prefix4;
1570 if (oi->area == area)
1571 return oi->address->u.prefix4;
1574 if (best_default.s_addr != 0)
1575 return best_default;
1577 if (best_default.s_addr != 0)
1578 return best_default;
1583 #define DEFAULT_DEFAULT_METRIC 20
1584 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1585 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1587 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1590 metric_type (struct ospf *ospf, u_char src)
1592 return (ospf->dmetric[src].type < 0 ?
1593 DEFAULT_METRIC_TYPE : ospf->dmetric[src].type);
1597 metric_value (struct ospf *ospf, u_char src)
1599 if (ospf->dmetric[src].value < 0)
1601 if (src == DEFAULT_ROUTE)
1603 if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1604 return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1606 return DEFAULT_DEFAULT_ALWAYS_METRIC;
1608 else if (ospf->default_metric < 0)
1609 return DEFAULT_DEFAULT_METRIC;
1611 return ospf->default_metric;
1614 return ospf->dmetric[src].value;
1617 /* Set AS-external-LSA body. */
1619 ospf_external_lsa_body_set (struct stream *s, struct external_info *ei,
1622 struct prefix_ipv4 *p = &ei->p;
1623 struct in_addr mask, fwd_addr;
1628 /* Put Network Mask. */
1629 masklen2ip (p->prefixlen, &mask);
1630 stream_put_ipv4 (s, mask.s_addr);
1632 /* If prefix is default, specify DEFAULT_ROUTE. */
1633 type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
1635 mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ?
1636 ROUTEMAP_METRIC_TYPE (ei) : metric_type (ospf, type);
1638 mvalue = (ROUTEMAP_METRIC (ei) != -1) ?
1639 ROUTEMAP_METRIC (ei) : metric_value (ospf, type);
1641 /* Put type of external metric. */
1642 stream_putc (s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1644 /* Put 0 metric. TOS metric is not supported. */
1645 stream_put_ospf_metric (s, mvalue);
1647 /* Get forwarding address to nexthop if on the Connection List, else 0. */
1648 fwd_addr = ospf_external_lsa_nexthop_get (ospf, ei->nexthop);
1650 /* Put forwarding address. */
1651 stream_put_ipv4 (s, fwd_addr.s_addr);
1654 stream_putl (s, ei->tag);
1657 /* Create new external-LSA. */
1658 static struct ospf_lsa *
1659 ospf_external_lsa_new (struct ospf *ospf,
1660 struct external_info *ei, struct in_addr *old_id)
1663 struct lsa_header *lsah;
1664 struct ospf_lsa *new;
1670 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1671 zlog_debug ("LSA[Type5]: External info is NULL, can't originate");
1675 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1676 zlog_debug ("LSA[Type5]: Originate AS-external-LSA instance");
1678 /* If old Link State ID is specified, refresh LSA with same ID. */
1681 /* Get Link State with unique ID. */
1684 id = ospf_lsa_unique_id (ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA, &ei->p);
1685 if (id.s_addr == 0xffffffff)
1687 /* Maybe Link State ID not available. */
1688 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1689 zlog_debug ("LSA[Type5]: Link ID not available, can't originate");
1694 /* Create new stream for LSA. */
1695 s = stream_new (OSPF_MAX_LSA_SIZE);
1696 lsah = (struct lsa_header *) STREAM_DATA (s);
1698 /* Set LSA common header fields. */
1699 lsa_header_set (s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA,
1700 id, ospf->router_id);
1702 /* Set AS-external-LSA body fields. */
1703 ospf_external_lsa_body_set (s, ei, ospf);
1706 length = stream_get_endp (s);
1707 lsah->length = htons (length);
1709 /* Now, create OSPF LSA instance. */
1710 new = ospf_lsa_new ();
1712 SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
1714 /* Copy LSA data to store, discard stream. */
1715 new->data = ospf_lsa_data_new (length);
1716 memcpy (new->data, lsah, length);
1724 ospf_install_flood_nssa (struct ospf *ospf,
1725 struct ospf_lsa *lsa, struct external_info *ei)
1727 struct ospf_lsa *new;
1728 struct as_external_lsa *extlsa;
1729 struct ospf_area *area;
1730 struct listnode *node, *nnode;
1732 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1733 * which originated from an NSSA area. In which case it should not be
1734 * flooded back to NSSA areas.
1736 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
1739 /* NSSA Originate or Refresh (If anyNSSA)
1741 LSA is self-originated. And just installed as Type-5.
1742 Additionally, install as Type-7 LSDB for every attached NSSA.
1744 P-Bit controls which ABR performs translation to outside world; If
1745 we are an ABR....do not set the P-bit, because we send the Type-5,
1746 not as the ABR Translator, but as the ASBR owner within the AS!
1748 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1749 elected ABR Translator will see the P-bit, Translate, and re-flood.
1751 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1752 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1754 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
1756 /* Don't install Type-7 LSA's into nonNSSA area */
1757 if (area->external_routing != OSPF_AREA_NSSA)
1760 /* make lsa duplicate, lock=1 */
1761 new = ospf_lsa_dup (lsa);
1763 new->data->type = OSPF_AS_NSSA_LSA;
1765 /* set P-bit if not ABR */
1766 if (! IS_OSPF_ABR (ospf))
1768 SET_FLAG(new->data->options, OSPF_OPTION_NP);
1770 /* set non-zero FWD ADDR
1772 draft-ietf-ospf-nssa-update-09.txt
1774 if the network between the NSSA AS boundary router and the
1775 adjacent AS is advertised into OSPF as an internal OSPF route,
1776 the forwarding address should be the next op address as is cu
1777 currently done with type-5 LSAs. If the intervening network is
1778 not adversited into OSPF as an internal OSPF route and the
1779 type-7 LSA's P-bit is set a forwarding address should be
1780 selected from one of the router's active OSPF inteface addresses
1781 which belong to the NSSA. If no such addresses exist, then
1782 no type-7 LSA's with the P-bit set should originate from this
1785 /* kevinm: not updating lsa anymore, just new */
1786 extlsa = (struct as_external_lsa *)(new->data);
1788 if (extlsa->e[0].fwd_addr.s_addr == 0)
1789 extlsa->e[0].fwd_addr = ospf_get_nssa_ip(area); /* this NSSA area in ifp */
1791 if (extlsa->e[0].fwd_addr.s_addr == 0)
1793 if (IS_DEBUG_OSPF_NSSA)
1794 zlog_debug ("LSA[Type-7]: Could not build FWD-ADDR");
1795 ospf_lsa_discard (new);
1800 /* install also as Type-7 */
1801 ospf_lsa_install (ospf, NULL, new); /* Remove Old, Lock New = 2 */
1803 /* will send each copy, lock=2+n */
1804 ospf_flood_through_as (ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
1808 static struct ospf_lsa *
1809 ospf_lsa_translated_nssa_new (struct ospf *ospf,
1810 struct ospf_lsa *type7)
1813 struct ospf_lsa *new;
1814 struct as_external_lsa *ext, *extnew;
1815 struct external_info ei;
1817 ext = (struct as_external_lsa *)(type7->data);
1819 /* need external_info struct, fill in bare minimum */
1820 ei.p.family = AF_INET;
1821 ei.p.prefix = type7->data->id;
1822 ei.p.prefixlen = ip_masklen (ext->mask);
1823 ei.type = ZEBRA_ROUTE_OSPF;
1824 ei.nexthop = ext->header.adv_router;
1825 ei.route_map_set.metric = -1;
1826 ei.route_map_set.metric_type = -1;
1829 if ( (new = ospf_external_lsa_new (ospf, &ei, &type7->data->id)) == NULL)
1831 if (IS_DEBUG_OSPF_NSSA)
1832 zlog_debug ("ospf_nssa_translate_originate(): Could not originate "
1833 "Translated Type-5 for %s",
1834 inet_ntoa (ei.p.prefix));
1838 extnew = (struct as_external_lsa *)(new->data);
1840 /* copy over Type-7 data to new */
1841 extnew->e[0].tos = ext->e[0].tos;
1842 extnew->e[0].route_tag = ext->e[0].route_tag;
1843 extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
1844 new->data->ls_seqnum = type7->data->ls_seqnum;
1846 /* add translated flag, checksum and lock new lsa */
1847 SET_FLAG (new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7 */
1848 new = ospf_lsa_lock (new);
1853 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1855 ospf_translated_nssa_originate (struct ospf *ospf, struct ospf_lsa *type7)
1857 struct ospf_lsa *new;
1858 struct as_external_lsa *extnew;
1860 /* we cant use ospf_external_lsa_originate() as we need to set
1861 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1864 if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
1866 if (IS_DEBUG_OSPF_NSSA)
1867 zlog_debug ("ospf_translated_nssa_originate(): Could not translate "
1868 "Type-7, Id %s, to Type-5",
1869 inet_ntoa (type7->data->id));
1873 extnew = (struct as_external_lsa *)new;
1875 if (IS_DEBUG_OSPF_NSSA)
1877 zlog_debug ("ospf_translated_nssa_originate(): "
1878 "translated Type 7, installed:");
1879 ospf_lsa_header_dump (new->data);
1880 zlog_debug (" Network mask: %d",ip_masklen (extnew->mask));
1881 zlog_debug (" Forward addr: %s", inet_ntoa (extnew->e[0].fwd_addr));
1884 if ( (new = ospf_lsa_install (ospf, NULL, new)) == NULL)
1886 if (IS_DEBUG_OSPF_NSSA)
1887 zlog_debug ("ospf_lsa_translated_nssa_originate(): "
1888 "Could not install LSA "
1889 "id %s", inet_ntoa (type7->data->id));
1893 ospf->lsa_originate_count++;
1894 ospf_flood_through_as (ospf, NULL, new);
1899 /* Refresh Translated from NSSA AS-external-LSA. */
1901 ospf_translated_nssa_refresh (struct ospf *ospf, struct ospf_lsa *type7,
1902 struct ospf_lsa *type5)
1904 struct ospf_lsa *new = NULL;
1906 /* Sanity checks. */
1907 assert (type7 || type5);
1908 if (!(type7 || type5))
1911 assert (type7->data);
1913 assert (type5->data);
1914 assert (ospf->anyNSSA);
1916 /* get required data according to what has been given */
1917 if (type7 && type5 == NULL)
1919 /* find the translated Type-5 for this Type-7 */
1920 struct as_external_lsa *ext = (struct as_external_lsa *)(type7->data);
1921 struct prefix_ipv4 p =
1923 .prefix = type7->data->id,
1924 .prefixlen = ip_masklen (ext->mask),
1928 type5 = ospf_external_info_find_lsa (ospf, &p);
1930 else if (type5 && type7 == NULL)
1932 /* find the type-7 from which supplied type-5 was translated,
1933 * ie find first type-7 with same LSA Id.
1935 struct listnode *ln, *lnn;
1936 struct route_node *rn;
1937 struct ospf_lsa *lsa;
1938 struct ospf_area *area;
1940 for (ALL_LIST_ELEMENTS (ospf->areas, ln, lnn, area))
1942 if (area->external_routing != OSPF_AREA_NSSA
1946 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
1948 if (lsa->data->id.s_addr == type5->data->id.s_addr)
1957 /* do we have type7? */
1960 if (IS_DEBUG_OSPF_NSSA)
1961 zlog_debug ("ospf_translated_nssa_refresh(): no Type-7 found for "
1963 inet_ntoa (type5->data->id));
1967 /* do we have valid translated type5? */
1968 if (type5 == NULL || !CHECK_FLAG (type5->flags, OSPF_LSA_LOCAL_XLT) )
1970 if (IS_DEBUG_OSPF_NSSA)
1971 zlog_debug ("ospf_translated_nssa_refresh(): No translated Type-5 "
1972 "found for Type-7 with Id %s",
1973 inet_ntoa (type7->data->id));
1977 /* Delete LSA from neighbor retransmit-list. */
1978 ospf_ls_retransmit_delete_nbr_as (ospf, type5);
1980 /* create new translated LSA */
1981 if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
1983 if (IS_DEBUG_OSPF_NSSA)
1984 zlog_debug ("ospf_translated_nssa_refresh(): Could not translate "
1985 "Type-7 for %s to Type-5",
1986 inet_ntoa (type7->data->id));
1990 if ( !(new = ospf_lsa_install (ospf, NULL, new)) )
1992 if (IS_DEBUG_OSPF_NSSA)
1993 zlog_debug ("ospf_translated_nssa_refresh(): Could not install "
1994 "translated LSA, Id %s",
1995 inet_ntoa (type7->data->id));
1999 /* Flood LSA through area. */
2000 ospf_flood_through_as (ospf, NULL, new);
2006 is_prefix_default (struct prefix_ipv4 *p)
2008 struct prefix_ipv4 q;
2011 q.prefix.s_addr = 0;
2014 return prefix_same ((struct prefix *) p, (struct prefix *) &q);
2017 /* Originate an AS-external-LSA, install and flood. */
2019 ospf_external_lsa_originate (struct ospf *ospf, struct external_info *ei)
2021 struct ospf_lsa *new;
2023 /* Added for NSSA project....
2025 External LSAs are originated in ASBRs as usual, but for NSSA systems.
2026 there is the global Type-5 LSDB and a Type-7 LSDB installed for
2027 every area. The Type-7's are flooded to every IR and every ABR; We
2028 install the Type-5 LSDB so that the normal "refresh" code operates
2029 as usual, and flag them as not used during ASE calculations. The
2030 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2031 Address of non-zero.
2033 If an ABR is the elected NSSA translator, following SPF and during
2034 the ABR task it will translate all the scanned Type-7's, with P-bit
2035 ON and not-self generated, and translate to Type-5's throughout the
2038 A difference in operation depends whether this ASBR is an ABR
2039 or not. If not an ABR, the P-bit is ON, to indicate that any
2040 elected NSSA-ABR can perform its translation.
2042 If an ABR, the P-bit is OFF; No ABR will perform translation and
2043 this ASBR will flood the Type-5 LSA as usual.
2045 For the case where this ASBR is not an ABR, the ASE calculations
2046 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2047 demonstrate to the user that there are LSA's that belong to any
2050 Finally, it just so happens that when the ABR is translating every
2051 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2052 approved Type-5 (translated from Type-7); at the end of translation
2053 if any Translated Type-5's remain unapproved, then they must be
2054 flushed from the AS.
2058 /* Check the AS-external-LSA should be originated. */
2059 if (!ospf_redistribute_check (ospf, ei, NULL))
2062 /* Create new AS-external-LSA instance. */
2063 if ((new = ospf_external_lsa_new (ospf, ei, NULL)) == NULL)
2065 if (IS_DEBUG_OSPF_EVENT)
2066 zlog_debug ("LSA[Type5:%s]: Could not originate AS-external-LSA",
2067 inet_ntoa (ei->p.prefix));
2071 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2072 ospf_lsa_install (ospf, NULL, new);
2074 /* Update LSA origination count. */
2075 ospf->lsa_originate_count++;
2077 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2078 ospf_flood_through_as (ospf, NULL, new);
2080 /* If there is any attached NSSA, do special handling */
2081 if (ospf->anyNSSA &&
2082 /* stay away from translated LSAs! */
2083 !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
2084 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
2086 /* Debug logging. */
2087 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2089 zlog_debug ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2090 new->data->type, inet_ntoa (new->data->id), (void *)new);
2091 ospf_lsa_header_dump (new->data);
2097 /* Originate AS-external-LSA from external info with initial flag. */
2099 ospf_external_lsa_originate_timer (struct thread *thread)
2101 struct ospf *ospf = THREAD_ARG (thread);
2102 struct route_node *rn;
2103 struct external_info *ei;
2104 struct route_table *rt;
2105 int type = THREAD_VAL (thread);
2107 ospf->t_external_lsa = NULL;
2109 /* Originate As-external-LSA from all type of distribute source. */
2110 if ((rt = EXTERNAL_INFO (type)))
2111 for (rn = route_top (rt); rn; rn = route_next (rn))
2112 if ((ei = rn->info) != NULL)
2113 if (!is_prefix_default ((struct prefix_ipv4 *)&ei->p))
2114 if (!ospf_external_lsa_originate (ospf, ei))
2115 zlog_warn ("LSA: AS-external-LSA was not originated.");
2120 static struct external_info *
2121 ospf_default_external_info (struct ospf *ospf)
2124 struct route_node *rn;
2125 struct prefix_ipv4 p;
2128 p.prefix.s_addr = 0;
2131 /* First, lookup redistributed default route. */
2132 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
2133 if (EXTERNAL_INFO (type) && type != ZEBRA_ROUTE_OSPF)
2135 rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
2138 route_unlock_node (rn);
2140 if (ospf_redistribute_check (ospf, rn->info, NULL))
2149 ospf_default_originate_timer (struct thread *thread)
2151 struct prefix_ipv4 p;
2152 struct in_addr nexthop;
2153 struct external_info *ei;
2156 ospf = THREAD_ARG (thread);
2159 p.prefix.s_addr = 0;
2162 if (ospf->default_originate == DEFAULT_ORIGINATE_ALWAYS)
2164 /* If there is no default route via redistribute,
2165 then originate AS-external-LSA with nexthop 0 (self). */
2167 ospf_external_info_add (DEFAULT_ROUTE, p, 0, nexthop, 0);
2170 if ((ei = ospf_default_external_info (ospf)))
2171 ospf_external_lsa_originate (ospf, ei);
2176 /* Flush any NSSA LSAs for given prefix */
2178 ospf_nssa_lsa_flush (struct ospf *ospf, struct prefix_ipv4 *p)
2180 struct listnode *node, *nnode;
2181 struct ospf_lsa *lsa;
2182 struct ospf_area *area;
2184 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
2186 if (area->external_routing == OSPF_AREA_NSSA)
2188 if (!(lsa = ospf_lsa_lookup (area, OSPF_AS_NSSA_LSA, p->prefix,
2191 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2192 zlog_debug ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2193 inet_ntoa (p->prefix), p->prefixlen);
2196 ospf_ls_retransmit_delete_nbr_area (area, lsa);
2197 if (!IS_LSA_MAXAGE (lsa))
2199 ospf_refresher_unregister_lsa (ospf, lsa);
2200 ospf_lsa_flush_area (lsa, area);
2206 /* Flush an AS-external-LSA from LSDB and routing domain. */
2208 ospf_external_lsa_flush (struct ospf *ospf,
2209 u_char type, struct prefix_ipv4 *p,
2210 ifindex_t ifindex /*, struct in_addr nexthop */)
2212 struct ospf_lsa *lsa;
2214 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2215 zlog_debug ("LSA: Flushing AS-external-LSA %s/%d",
2216 inet_ntoa (p->prefix), p->prefixlen);
2218 /* First lookup LSA from LSDB. */
2219 if (!(lsa = ospf_external_info_find_lsa (ospf, p)))
2221 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2222 zlog_debug ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
2223 inet_ntoa (p->prefix), p->prefixlen);
2227 /* If LSA is selforiginated, not a translated LSA, and there is
2228 * NSSA area, flush Type-7 LSA's at first.
2230 if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
2231 && !(CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT)))
2232 ospf_nssa_lsa_flush (ospf, p);
2234 /* Sweep LSA from Link State Retransmit List. */
2235 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2237 /* There must be no self-originated LSA in rtrs_external. */
2239 /* Remove External route from Zebra. */
2240 ospf_zebra_delete ((struct prefix_ipv4 *) p, &nexthop);
2243 if (!IS_LSA_MAXAGE (lsa))
2245 /* Unregister LSA from Refresh queue. */
2246 ospf_refresher_unregister_lsa (ospf, lsa);
2248 /* Flush AS-external-LSA through AS. */
2249 ospf_lsa_flush_as (ospf, lsa);
2252 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2253 zlog_debug ("ospf_external_lsa_flush(): stop");
2257 ospf_external_lsa_refresh_default (struct ospf *ospf)
2259 struct prefix_ipv4 p;
2260 struct external_info *ei;
2261 struct ospf_lsa *lsa;
2265 p.prefix.s_addr = 0;
2267 ei = ospf_default_external_info (ospf);
2268 lsa = ospf_external_info_find_lsa (ospf, &p);
2274 if (IS_DEBUG_OSPF_EVENT)
2275 zlog_debug ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2277 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
2281 if (IS_DEBUG_OSPF_EVENT)
2282 zlog_debug ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2283 ospf_external_lsa_originate (ospf, ei);
2290 if (IS_DEBUG_OSPF_EVENT)
2291 zlog_debug ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2292 ospf_refresher_unregister_lsa (ospf, lsa);
2293 ospf_lsa_flush_as (ospf, lsa);
2299 ospf_external_lsa_refresh_type (struct ospf *ospf, u_char type, int force)
2301 struct route_node *rn;
2302 struct external_info *ei;
2304 if (type != DEFAULT_ROUTE)
2305 if (EXTERNAL_INFO(type))
2306 /* Refresh each redistributed AS-external-LSAs. */
2307 for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
2308 if ((ei = rn->info))
2309 if (!is_prefix_default (&ei->p))
2311 struct ospf_lsa *lsa;
2313 if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
2314 ospf_external_lsa_refresh (ospf, lsa, ei, force);
2316 ospf_external_lsa_originate (ospf, ei);
2320 /* Refresh AS-external-LSA. */
2322 ospf_external_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa,
2323 struct external_info *ei, int force)
2325 struct ospf_lsa *new;
2328 /* Check the AS-external-LSA should be originated. */
2329 if (!ospf_redistribute_check (ospf, ei, &changed))
2331 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2332 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed, "
2333 "redist check fail",
2334 lsa->data->type, inet_ntoa (lsa->data->id));
2335 ospf_external_lsa_flush (ospf, ei->type, &ei->p,
2336 ei->ifindex /*, ei->nexthop */);
2340 if (!changed && !force)
2342 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2343 zlog_debug ("LSA[Type%d:%s]: Not refreshed, not changed/forced",
2344 lsa->data->type, inet_ntoa (lsa->data->id));
2348 /* Delete LSA from neighbor retransmit-list. */
2349 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2351 /* Unregister AS-external-LSA from refresh-list. */
2352 ospf_refresher_unregister_lsa (ospf, lsa);
2354 new = ospf_external_lsa_new (ospf, ei, &lsa->data->id);
2358 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2359 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed", lsa->data->type,
2360 inet_ntoa (lsa->data->id));
2364 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
2366 ospf_lsa_install (ospf, NULL, new); /* As type-5. */
2368 /* Flood LSA through AS. */
2369 ospf_flood_through_as (ospf, NULL, new);
2371 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2372 if (ospf->anyNSSA && !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
2373 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood per new rules */
2375 /* Register self-originated LSA to refresh queue.
2376 * Translated LSAs should not be registered, but refreshed upon
2377 * refresh of the Type-7
2379 if ( !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT) )
2380 ospf_refresher_register_lsa (ospf, new);
2382 /* Debug logging. */
2383 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2385 zlog_debug ("LSA[Type%d:%s]: AS-external-LSA refresh",
2386 new->data->type, inet_ntoa (new->data->id));
2387 ospf_lsa_header_dump (new->data);
2394 /* LSA installation functions. */
2396 /* Install router-LSA to an area. */
2397 static struct ospf_lsa *
2398 ospf_router_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2401 struct ospf_area *area = new->area;
2403 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2404 The entire routing table must be recalculated, starting with
2405 the shortest path calculations for each area (not just the
2406 area whose link-state database has changed).
2409 if (IS_LSA_SELF (new))
2412 /* Only install LSA if it is originated/refreshed by us.
2413 * If LSA was received by flooding, the RECEIVED flag is set so do
2414 * not link the LSA */
2415 if (CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
2416 return new; /* ignore stale LSA */
2418 /* Set self-originated router-LSA. */
2419 ospf_lsa_unlock (&area->router_lsa_self);
2420 area->router_lsa_self = ospf_lsa_lock (new);
2422 ospf_refresher_register_lsa (ospf, new);
2425 ospf_spf_calculate_schedule (ospf, SPF_FLAG_ROUTER_LSA_INSTALL);
2429 #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
2431 (T) = thread_add_timer (master, (F), oi, (V))
2433 /* Install network-LSA to an area. */
2434 static struct ospf_lsa *
2435 ospf_network_lsa_install (struct ospf *ospf,
2436 struct ospf_interface *oi,
2437 struct ospf_lsa *new,
2441 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2442 The entire routing table must be recalculated, starting with
2443 the shortest path calculations for each area (not just the
2444 area whose link-state database has changed).
2446 if (IS_LSA_SELF (new))
2448 /* We supposed that when LSA is originated by us, we pass the int
2449 for which it was originated. If LSA was received by flooding,
2450 the RECEIVED flag is set, so we do not link the LSA to the int. */
2451 if (CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
2452 return new; /* ignore stale LSA */
2454 ospf_lsa_unlock (&oi->network_lsa_self);
2455 oi->network_lsa_self = ospf_lsa_lock (new);
2456 ospf_refresher_register_lsa (ospf, new);
2459 ospf_spf_calculate_schedule (ospf, SPF_FLAG_NETWORK_LSA_INSTALL);
2464 /* Install summary-LSA to an area. */
2465 static struct ospf_lsa *
2466 ospf_summary_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2469 if (rt_recalc && !IS_LSA_SELF (new))
2471 /* RFC 2328 Section 13.2 Summary-LSAs
2472 The best route to the destination described by the summary-
2473 LSA must be recalculated (see Section 16.5). If this
2474 destination is an AS boundary router, it may also be
2475 necessary to re-examine all the AS-external-LSAs.
2479 /* This doesn't exist yet... */
2480 ospf_summary_incremental_update(new); */
2482 ospf_spf_calculate_schedule (ospf, SPF_FLAG_SUMMARY_LSA_INSTALL);
2487 if (IS_LSA_SELF (new))
2488 ospf_refresher_register_lsa (ospf, new);
2493 /* Install ASBR-summary-LSA to an area. */
2494 static struct ospf_lsa *
2495 ospf_summary_asbr_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2498 if (rt_recalc && !IS_LSA_SELF (new))
2500 /* RFC 2328 Section 13.2 Summary-LSAs
2501 The best route to the destination described by the summary-
2502 LSA must be recalculated (see Section 16.5). If this
2503 destination is an AS boundary router, it may also be
2504 necessary to re-examine all the AS-external-LSAs.
2507 /* These don't exist yet... */
2508 ospf_summary_incremental_update(new);
2509 /* Isn't this done by the above call?
2510 - RFC 2328 Section 16.5 implies it should be */
2511 /* ospf_ase_calculate_schedule(); */
2513 ospf_spf_calculate_schedule (ospf, SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL);
2517 /* register LSA to refresh-list. */
2518 if (IS_LSA_SELF (new))
2519 ospf_refresher_register_lsa (ospf, new);
2524 /* Install AS-external-LSA. */
2525 static struct ospf_lsa *
2526 ospf_external_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2529 ospf_ase_register_external_lsa (new, ospf);
2530 /* If LSA is not self-originated, calculate an external route. */
2533 /* RFC 2328 Section 13.2 AS-external-LSAs
2534 The best route to the destination described by the AS-
2535 external-LSA must be recalculated (see Section 16.6).
2538 if (!IS_LSA_SELF (new))
2539 ospf_ase_incremental_update (ospf, new);
2542 if (new->data->type == OSPF_AS_NSSA_LSA)
2544 /* There is no point to register selforiginate Type-7 LSA for
2545 * refreshing. We rely on refreshing Type-5 LSA's
2547 if (IS_LSA_SELF (new))
2551 /* Try refresh type-5 translated LSA for this LSA, if one exists.
2552 * New translations will be taken care of by the abr_task.
2554 ospf_translated_nssa_refresh (ospf, new, NULL);
2555 ospf_schedule_abr_task(ospf);
2559 /* Register self-originated LSA to refresh queue.
2560 * Leave Translated LSAs alone if NSSA is enabled
2562 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT ) )
2563 ospf_refresher_register_lsa (ospf, new);
2569 ospf_discard_from_db (struct ospf *ospf,
2570 struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
2572 struct ospf_lsa *old;
2576 zlog_warn ("%s: Called with NULL lsdb!", __func__);
2578 zlog_warn ("%s: and NULL LSA!", __func__);
2580 zlog_warn ("LSA[Type%d:%s]: not associated with LSDB!",
2581 lsa->data->type, inet_ntoa (lsa->data->id));
2585 old = ospf_lsdb_lookup (lsdb, lsa);
2590 if (old->refresh_list >= 0)
2591 ospf_refresher_unregister_lsa (ospf, old);
2593 switch (old->data->type)
2595 case OSPF_AS_EXTERNAL_LSA:
2596 ospf_ase_unregister_external_lsa (old, ospf);
2597 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2599 case OSPF_OPAQUE_AS_LSA:
2600 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2602 case OSPF_AS_NSSA_LSA:
2603 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2604 ospf_ase_unregister_external_lsa (old, ospf);
2607 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2611 ospf_lsa_maxage_delete (ospf, old);
2612 ospf_lsa_discard (old);
2616 ospf_lsa_install (struct ospf *ospf, struct ospf_interface *oi,
2617 struct ospf_lsa *lsa)
2619 struct ospf_lsa *new = NULL;
2620 struct ospf_lsa *old = NULL;
2621 struct ospf_lsdb *lsdb = NULL;
2625 switch (lsa->data->type)
2628 case OSPF_AS_NSSA_LSA:
2630 lsdb = lsa->area->lsdb;
2634 case OSPF_AS_EXTERNAL_LSA:
2635 case OSPF_OPAQUE_AS_LSA:
2639 lsdb = lsa->area->lsdb;
2645 /* RFC 2328 13.2. Installing LSAs in the database
2647 Installing a new LSA in the database, either as the result of
2648 flooding or a newly self-originated LSA, may cause the OSPF
2649 routing table structure to be recalculated. The contents of the
2650 new LSA should be compared to the old instance, if present. If
2651 there is no difference, there is no need to recalculate the
2652 routing table. When comparing an LSA to its previous instance,
2653 the following are all considered to be differences in contents:
2655 o The LSA's Options field has changed.
2657 o One of the LSA instances has LS age set to MaxAge, and
2660 o The length field in the LSA header has changed.
2662 o The body of the LSA (i.e., anything outside the 20-byte
2663 LSA header) has changed. Note that this excludes changes
2664 in LS Sequence Number and LS Checksum.
2667 /* Look up old LSA and determine if any SPF calculation or incremental
2669 old = ospf_lsdb_lookup (lsdb, lsa);
2671 /* Do comparision and record if recalc needed. */
2673 if ( old == NULL || ospf_lsa_different(old, lsa))
2677 Sequence number check (Section 14.1 of rfc 2328)
2678 "Premature aging is used when it is time for a self-originated
2679 LSA's sequence number field to wrap. At this point, the current
2680 LSA instance (having LS sequence number MaxSequenceNumber) must
2681 be prematurely aged and flushed from the routing domain before a
2682 new instance with sequence number equal to InitialSequenceNumber
2683 can be originated. "
2686 if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER)
2688 if (ospf_lsa_is_self_originated(ospf, lsa))
2690 lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
2692 if (!IS_LSA_MAXAGE(lsa))
2693 lsa->flags |= OSPF_LSA_PREMATURE_AGE;
2694 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
2696 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
2698 zlog_debug ("ospf_lsa_install() Premature Aging "
2699 "lsa 0x%p, seqnum 0x%x",
2700 (void *)lsa, ntohl(lsa->data->ls_seqnum));
2701 ospf_lsa_header_dump (lsa->data);
2706 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2708 zlog_debug ("ospf_lsa_install() got an lsa with seq 0x80000000 "
2709 "that was not self originated. Ignoring\n");
2710 ospf_lsa_header_dump (lsa->data);
2716 /* discard old LSA from LSDB */
2718 ospf_discard_from_db (ospf, lsdb, lsa);
2720 /* Calculate Checksum if self-originated?. */
2721 if (IS_LSA_SELF (lsa))
2722 ospf_lsa_checksum (lsa->data);
2724 /* Insert LSA to LSDB. */
2725 ospf_lsdb_add (lsdb, lsa);
2728 /* Do LSA specific installation process. */
2729 switch (lsa->data->type)
2731 case OSPF_ROUTER_LSA:
2732 new = ospf_router_lsa_install (ospf, lsa, rt_recalc);
2734 case OSPF_NETWORK_LSA:
2736 new = ospf_network_lsa_install (ospf, oi, lsa, rt_recalc);
2738 case OSPF_SUMMARY_LSA:
2739 new = ospf_summary_lsa_install (ospf, lsa, rt_recalc);
2741 case OSPF_ASBR_SUMMARY_LSA:
2742 new = ospf_summary_asbr_lsa_install (ospf, lsa, rt_recalc);
2744 case OSPF_AS_EXTERNAL_LSA:
2745 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2747 case OSPF_OPAQUE_LINK_LSA:
2748 if (IS_LSA_SELF (lsa))
2749 lsa->oi = oi; /* Specify outgoing ospf-interface for this LSA. */
2752 /* Incoming "oi" for this LSA has set at LSUpd reception. */
2755 case OSPF_OPAQUE_AREA_LSA:
2756 case OSPF_OPAQUE_AS_LSA:
2757 new = ospf_opaque_lsa_install (lsa, rt_recalc);
2759 case OSPF_AS_NSSA_LSA:
2760 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2761 default: /* type-6,8,9....nothing special */
2766 return new; /* Installation failed, cannot proceed further -- endo. */
2769 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2771 char area_str[INET_ADDRSTRLEN];
2773 switch (lsa->data->type)
2775 case OSPF_AS_EXTERNAL_LSA:
2776 case OSPF_OPAQUE_AS_LSA:
2777 case OSPF_AS_NSSA_LSA:
2778 zlog_debug ("LSA[%s]: Install %s",
2780 LOOKUP (ospf_lsa_type_msg, new->data->type));
2783 strcpy (area_str, inet_ntoa (new->area->area_id));
2784 zlog_debug ("LSA[%s]: Install %s to Area %s",
2786 LOOKUP (ospf_lsa_type_msg, new->data->type), area_str);
2792 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2793 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2795 if (IS_LSA_MAXAGE (new))
2797 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2798 zlog_debug ("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2800 inet_ntoa (new->data->id),
2802 ospf_lsa_maxage (ospf, lsa);
2810 ospf_check_nbr_status (struct ospf *ospf)
2812 struct listnode *node, *nnode;
2813 struct ospf_interface *oi;
2815 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
2817 struct route_node *rn;
2818 struct ospf_neighbor *nbr;
2820 if (ospf_if_is_enable (oi))
2821 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
2822 if ((nbr = rn->info) != NULL)
2823 if (nbr->state == NSM_Exchange || nbr->state == NSM_Loading)
2825 route_unlock_node (rn);
2836 ospf_maxage_lsa_remover (struct thread *thread)
2838 struct ospf *ospf = THREAD_ARG (thread);
2839 struct ospf_lsa *lsa;
2840 struct route_node *rn;
2843 ospf->t_maxage = NULL;
2845 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2846 zlog_debug ("LSA[MaxAge]: remover Start");
2848 reschedule = !ospf_check_nbr_status (ospf);
2851 for (rn = route_top(ospf->maxage_lsa); rn; rn = route_next(rn))
2853 if ((lsa = rn->info) == NULL)
2858 /* There is at least one neighbor from which we still await an ack
2859 * for that LSA, so we are not allowed to remove it from our lsdb yet
2860 * as per RFC 2328 section 14 para 4 a) */
2861 if (lsa->retransmit_counter > 0)
2867 /* TODO: maybe convert this function to a work-queue */
2868 if (thread_should_yield (thread))
2870 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 0);
2871 route_unlock_node(rn); /* route_top/route_next */
2875 /* Remove LSA from the LSDB */
2876 if (IS_LSA_SELF (lsa))
2877 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2878 zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2879 lsa->data->type, inet_ntoa (lsa->data->id), (u_long)lsa);
2881 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2882 zlog_debug ("LSA[Type%d:%s]: MaxAge LSA removed from list",
2883 lsa->data->type, inet_ntoa (lsa->data->id));
2885 if (CHECK_FLAG (lsa->flags, OSPF_LSA_PREMATURE_AGE))
2887 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2888 zlog_debug ("originating new lsa for lsa 0x%p\n", (void *)lsa);
2889 ospf_lsa_refresh (ospf, lsa);
2892 /* Remove from lsdb. */
2895 ospf_discard_from_db (ospf, lsa->lsdb, lsa);
2896 ospf_lsdb_delete (lsa->lsdb, lsa);
2899 zlog_warn ("%s: LSA[Type%d:%s]: No associated LSDB!", __func__,
2900 lsa->data->type, inet_ntoa (lsa->data->id));
2903 /* A MaxAge LSA must be removed immediately from the router's link
2904 state database as soon as both a) it is no longer contained on any
2905 neighbor Link state retransmission lists and b) none of the router's
2906 neighbors are in states Exchange or Loading. */
2908 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover,
2909 ospf->maxage_delay);
2915 ospf_lsa_maxage_delete (struct ospf *ospf, struct ospf_lsa *lsa)
2917 struct route_node *rn;
2918 struct prefix_ptr lsa_prefix;
2920 lsa_prefix.family = 0;
2921 lsa_prefix.prefixlen = sizeof(lsa_prefix.prefix) * CHAR_BIT;
2922 lsa_prefix.prefix = (uintptr_t) lsa;
2924 if ((rn = route_node_lookup(ospf->maxage_lsa,
2925 (struct prefix *)&lsa_prefix)))
2927 if (rn->info == lsa)
2929 UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2930 ospf_lsa_unlock (&lsa); /* maxage_lsa */
2932 route_unlock_node (rn); /* unlock node because lsa is deleted */
2934 route_unlock_node (rn); /* route_node_lookup */
2938 /* Add LSA onto the MaxAge list, and schedule for removal.
2939 * This does *not* lead to the LSA being flooded, that must be taken
2940 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2944 ospf_lsa_maxage (struct ospf *ospf, struct ospf_lsa *lsa)
2946 struct prefix_ptr lsa_prefix;
2947 struct route_node *rn;
2949 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2950 and schedule the MaxAge LSA remover. */
2951 if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE))
2953 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2954 zlog_debug ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2955 lsa->data->type, inet_ntoa (lsa->data->id), (void *)lsa);
2959 lsa_prefix.family = 0;
2960 lsa_prefix.prefixlen = sizeof(lsa_prefix.prefix) * CHAR_BIT;
2961 lsa_prefix.prefix = (uintptr_t) lsa;
2963 if ((rn = route_node_get (ospf->maxage_lsa,
2964 (struct prefix *)&lsa_prefix)) != NULL)
2966 if (rn->info != NULL)
2968 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2969 zlog_debug ("LSA[%s]: found LSA (%p) in table for LSA %p %d",
2970 dump_lsa_key (lsa), rn->info, (void *)lsa,
2971 lsa_prefix.prefixlen);
2972 route_unlock_node (rn);
2976 rn->info = ospf_lsa_lock(lsa);
2977 SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
2982 zlog_err("Unable to allocate memory for maxage lsa\n");
2986 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2987 zlog_debug ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa));
2989 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover,
2990 ospf->maxage_delay);
2994 ospf_lsa_maxage_walker_remover (struct ospf *ospf, struct ospf_lsa *lsa)
2996 /* Stay away from any Local Translated Type-7 LSAs */
2997 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
3000 if (IS_LSA_MAXAGE (lsa))
3001 /* Self-originated LSAs should NOT time-out instead,
3002 they're flushed and submitted to the max_age list explicitly. */
3003 if (!ospf_lsa_is_self_originated (ospf, lsa))
3005 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3006 zlog_debug("LSA[%s]: is MaxAge", dump_lsa_key (lsa));
3008 switch (lsa->data->type)
3010 case OSPF_OPAQUE_LINK_LSA:
3011 case OSPF_OPAQUE_AREA_LSA:
3012 case OSPF_OPAQUE_AS_LSA:
3014 * As a general rule, whenever network topology has changed
3015 * (due to an LSA removal in this case), routing recalculation
3016 * should be triggered. However, this is not true for opaque
3017 * LSAs. Even if an opaque LSA instance is going to be removed
3018 * from the routing domain, it does not mean a change in network
3019 * topology, and thus, routing recalculation is not needed here.
3022 case OSPF_AS_EXTERNAL_LSA:
3023 case OSPF_AS_NSSA_LSA:
3024 ospf_ase_incremental_update (ospf, lsa);
3027 ospf_spf_calculate_schedule (ospf, SPF_FLAG_MAXAGE);
3030 ospf_lsa_maxage (ospf, lsa);
3033 if (IS_LSA_MAXAGE (lsa) && !ospf_lsa_is_self_originated (ospf, lsa))
3034 if (LS_AGE (lsa) > OSPF_LSA_MAXAGE + 30)
3035 printf ("Eek! Shouldn't happen!\n");
3040 /* Periodical check of MaxAge LSA. */
3042 ospf_lsa_maxage_walker (struct thread *thread)
3044 struct ospf *ospf = THREAD_ARG (thread);
3045 struct route_node *rn;
3046 struct ospf_lsa *lsa;
3047 struct ospf_area *area;
3048 struct listnode *node, *nnode;
3050 ospf->t_maxage_walker = NULL;
3052 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
3054 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
3055 ospf_lsa_maxage_walker_remover (ospf, lsa);
3056 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
3057 ospf_lsa_maxage_walker_remover (ospf, lsa);
3058 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
3059 ospf_lsa_maxage_walker_remover (ospf, lsa);
3060 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
3061 ospf_lsa_maxage_walker_remover (ospf, lsa);
3062 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
3063 ospf_lsa_maxage_walker_remover (ospf, lsa);
3064 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
3065 ospf_lsa_maxage_walker_remover (ospf, lsa);
3066 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
3067 ospf_lsa_maxage_walker_remover (ospf, lsa);
3070 /* for AS-external-LSAs. */
3073 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
3074 ospf_lsa_maxage_walker_remover (ospf, lsa);
3075 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
3076 ospf_lsa_maxage_walker_remover (ospf, lsa);
3079 OSPF_TIMER_ON (ospf->t_maxage_walker, ospf_lsa_maxage_walker,
3080 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
3085 ospf_lsa_lookup_by_prefix (struct ospf_lsdb *lsdb, u_char type,
3086 struct prefix_ipv4 *p, struct in_addr router_id)
3088 struct ospf_lsa *lsa;
3089 struct in_addr mask, id;
3090 struct lsa_header_mask
3092 struct lsa_header header;
3093 struct in_addr mask;
3096 lsa = ospf_lsdb_lookup_by_id (lsdb, type, p->prefix, router_id);
3100 masklen2ip (p->prefixlen, &mask);
3102 hmask = (struct lsa_header_mask *) lsa->data;
3104 if (mask.s_addr != hmask->mask.s_addr)
3106 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3107 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, router_id);
3116 ospf_lsa_lookup (struct ospf_area *area, u_int32_t type,
3117 struct in_addr id, struct in_addr adv_router)
3119 struct ospf *ospf = ospf_lookup();
3124 case OSPF_ROUTER_LSA:
3125 case OSPF_NETWORK_LSA:
3126 case OSPF_SUMMARY_LSA:
3127 case OSPF_ASBR_SUMMARY_LSA:
3128 case OSPF_AS_NSSA_LSA:
3129 case OSPF_OPAQUE_LINK_LSA:
3130 case OSPF_OPAQUE_AREA_LSA:
3131 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, adv_router);
3132 case OSPF_AS_EXTERNAL_LSA:
3133 case OSPF_OPAQUE_AS_LSA:
3134 return ospf_lsdb_lookup_by_id (ospf->lsdb, type, id, adv_router);
3143 ospf_lsa_lookup_by_id (struct ospf_area *area, u_int32_t type,
3146 struct ospf_lsa *lsa;
3147 struct route_node *rn;
3151 case OSPF_ROUTER_LSA:
3152 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
3153 case OSPF_NETWORK_LSA:
3154 for (rn = route_top (NETWORK_LSDB (area)); rn; rn = route_next (rn))
3155 if ((lsa = rn->info))
3156 if (IPV4_ADDR_SAME (&lsa->data->id, &id))
3158 route_unlock_node (rn);
3162 case OSPF_SUMMARY_LSA:
3163 case OSPF_ASBR_SUMMARY_LSA:
3164 /* Currently not used. */
3166 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
3167 case OSPF_AS_EXTERNAL_LSA:
3168 case OSPF_AS_NSSA_LSA:
3169 case OSPF_OPAQUE_LINK_LSA:
3170 case OSPF_OPAQUE_AREA_LSA:
3171 case OSPF_OPAQUE_AS_LSA:
3172 /* Currently not used. */
3182 ospf_lsa_lookup_by_header (struct ospf_area *area, struct lsa_header *lsah)
3184 struct ospf_lsa *match;
3187 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3188 * is redefined to have two subfields; opaque-type and opaque-id.
3189 * However, it is harmless to treat the two sub fields together, as if
3190 * they two were forming a unique LSA-ID.
3193 match = ospf_lsa_lookup (area, lsah->type, lsah->id, lsah->adv_router);
3196 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3197 zlog_debug ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3198 lsah->type, inet_ntoa (lsah->id));
3203 /* return +n, l1 is more recent.
3204 return -n, l2 is more recent.
3205 return 0, l1 and l2 is identical. */
3207 ospf_lsa_more_recent (struct ospf_lsa *l1, struct ospf_lsa *l2)
3212 if (l1 == NULL && l2 == NULL)
3219 /* compare LS sequence number. */
3220 x = (int) ntohl (l1->data->ls_seqnum);
3221 y = (int) ntohl (l2->data->ls_seqnum);
3227 /* compare LS checksum. */
3228 r = ntohs (l1->data->checksum) - ntohs (l2->data->checksum);
3232 /* compare LS age. */
3233 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
3235 else if (!IS_LSA_MAXAGE (l1) && IS_LSA_MAXAGE (l2))
3238 /* compare LS age with MaxAgeDiff. */
3239 if (LS_AGE (l1) - LS_AGE (l2) > OSPF_LSA_MAXAGE_DIFF)
3241 else if (LS_AGE (l2) - LS_AGE (l1) > OSPF_LSA_MAXAGE_DIFF)
3244 /* LSAs are identical. */
3248 /* If two LSAs are different, return 1, otherwise return 0. */
3250 ospf_lsa_different (struct ospf_lsa *l1, struct ospf_lsa *l2)
3258 if (l1->data->options != l2->data->options)
3261 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
3264 if (IS_LSA_MAXAGE (l2) && !IS_LSA_MAXAGE (l1))
3267 if (l1->data->length != l2->data->length)
3270 if (l1->data->length == 0)
3273 if (CHECK_FLAG ((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
3274 return 1; /* May be a stale LSA in the LSBD */
3276 assert ( ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
3278 p1 = (char *) l1->data;
3279 p2 = (char *) l2->data;
3281 if (memcmp (p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
3282 ntohs( l1->data->length ) - OSPF_LSA_HEADER_SIZE) != 0)
3288 #ifdef ORIGINAL_CODING
3290 ospf_lsa_flush_self_originated (struct ospf_neighbor *nbr,
3291 struct ospf_lsa *self,
3292 struct ospf_lsa *new)
3296 /* Adjust LS Sequence Number. */
3297 seqnum = ntohl (new->data->ls_seqnum) + 1;
3298 self->data->ls_seqnum = htonl (seqnum);
3300 /* Recalculate LSA checksum. */
3301 ospf_lsa_checksum (self->data);
3303 /* Reflooding LSA. */
3304 /* RFC2328 Section 13.3
3305 On non-broadcast networks, separate Link State Update
3306 packets must be sent, as unicasts, to each adjacent neighbor
3307 (i.e., those in state Exchange or greater). The destination
3308 IP addresses for these packets are the neighbors' IP
3310 if (nbr->oi->type == OSPF_IFTYPE_NBMA)
3312 struct route_node *rn;
3313 struct ospf_neighbor *onbr;
3315 for (rn = route_top (nbr->oi->nbrs); rn; rn = route_next (rn))
3316 if ((onbr = rn->info) != NULL)
3317 if (onbr != nbr->oi->nbr_self && onbr->status >= NSM_Exchange)
3318 ospf_ls_upd_send_lsa (onbr, self, OSPF_SEND_PACKET_DIRECT);
3321 ospf_ls_upd_send_lsa (nbr, self, OSPF_SEND_PACKET_INDIRECT);
3323 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3324 zlog_debug ("LSA[Type%d:%s]: Flush self-originated LSA",
3325 self->data->type, inet_ntoa (self->data->id));
3327 #else /* ORIGINAL_CODING */
3329 ospf_lsa_flush_schedule (struct ospf *ospf, struct ospf_lsa *lsa)
3331 if (lsa == NULL || !IS_LSA_SELF (lsa))
3334 if (IS_DEBUG_OSPF_EVENT)
3335 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3337 /* Force given lsa's age to MaxAge. */
3338 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
3340 switch (lsa->data->type)
3342 /* Opaque wants to be notified of flushes */
3343 case OSPF_OPAQUE_LINK_LSA:
3344 case OSPF_OPAQUE_AREA_LSA:
3345 case OSPF_OPAQUE_AS_LSA:
3346 ospf_opaque_lsa_refresh (lsa);
3349 ospf_refresher_unregister_lsa (ospf, lsa);
3350 ospf_lsa_flush (ospf, lsa);
3358 ospf_flush_self_originated_lsas_now (struct ospf *ospf)
3360 struct listnode *node, *nnode;
3361 struct listnode *node2, *nnode2;
3362 struct ospf_area *area;
3363 struct ospf_interface *oi;
3364 struct ospf_lsa *lsa;
3365 struct route_node *rn;
3366 int need_to_flush_ase = 0;
3368 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
3370 if ((lsa = area->router_lsa_self) != NULL)
3372 if (IS_DEBUG_OSPF_EVENT)
3373 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3374 lsa->data->type, inet_ntoa (lsa->data->id));
3376 ospf_refresher_unregister_lsa (ospf, lsa);
3377 ospf_lsa_flush_area (lsa, area);
3378 ospf_lsa_unlock (&area->router_lsa_self);
3379 area->router_lsa_self = NULL;
3382 for (ALL_LIST_ELEMENTS (area->oiflist, node2, nnode2, oi))
3384 if ((lsa = oi->network_lsa_self) != NULL
3385 && oi->state == ISM_DR
3386 && oi->full_nbrs > 0)
3388 if (IS_DEBUG_OSPF_EVENT)
3389 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3390 lsa->data->type, inet_ntoa (lsa->data->id));
3392 ospf_refresher_unregister_lsa (ospf, oi->network_lsa_self);
3393 ospf_lsa_flush_area (oi->network_lsa_self, area);
3394 ospf_lsa_unlock (&oi->network_lsa_self);
3395 oi->network_lsa_self = NULL;
3398 if (oi->type != OSPF_IFTYPE_VIRTUALLINK
3399 && area->external_routing == OSPF_AREA_DEFAULT)
3400 need_to_flush_ase = 1;
3403 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
3404 ospf_lsa_flush_schedule (ospf, lsa);
3405 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
3406 ospf_lsa_flush_schedule (ospf, lsa);
3407 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
3408 ospf_lsa_flush_schedule (ospf, lsa);
3409 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
3410 ospf_lsa_flush_schedule (ospf, lsa);
3413 if (need_to_flush_ase)
3415 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
3416 ospf_lsa_flush_schedule (ospf, lsa);
3417 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
3418 ospf_lsa_flush_schedule (ospf, lsa);
3422 * Make sure that the MaxAge LSA remover is executed immediately,
3423 * without conflicting to other threads.
3425 if (ospf->t_maxage != NULL)
3427 OSPF_TIMER_OFF (ospf->t_maxage);
3428 thread_execute (master, ospf_maxage_lsa_remover, ospf, 0);
3433 #endif /* ORIGINAL_CODING */
3435 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3436 /* An interface-independent version of ospf_lsa_is_self_originated */
3438 ospf_lsa_is_self_originated (struct ospf *ospf, struct ospf_lsa *lsa)
3440 struct listnode *node;
3441 struct ospf_interface *oi;
3443 /* This LSA is already checked. */
3444 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED))
3445 return IS_LSA_SELF (lsa);
3447 /* Make sure LSA is self-checked. */
3448 SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
3450 /* AdvRouter and Router ID is the same. */
3451 if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf->router_id))
3452 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3454 /* LSA is router-LSA. */
3455 else if (lsa->data->type == OSPF_ROUTER_LSA &&
3456 IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
3457 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3459 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3460 else if (lsa->data->type == OSPF_NETWORK_LSA)
3461 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
3463 /* Ignore virtual link. */
3464 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
3465 if (oi->address->family == AF_INET)
3466 if (IPV4_ADDR_SAME (&lsa->data->id, &oi->address->u.prefix4))
3468 /* to make it easier later */
3469 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3470 return IS_LSA_SELF (lsa);
3474 return IS_LSA_SELF (lsa);
3477 /* Get unique Link State ID. */
3479 ospf_lsa_unique_id (struct ospf *ospf,
3480 struct ospf_lsdb *lsdb, u_char type, struct prefix_ipv4 *p)
3482 struct ospf_lsa *lsa;
3483 struct in_addr mask, id;
3487 /* Check existence of LSA instance. */
3488 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, ospf->router_id);
3491 struct as_external_lsa *al = (struct as_external_lsa *) lsa->data;
3492 if (ip_masklen (al->mask) == p->prefixlen)
3494 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3495 zlog_debug ("ospf_lsa_unique_id(): "
3496 "Can't get Link State ID for %s/%d",
3497 inet_ntoa (p->prefix), p->prefixlen);
3498 /* id.s_addr = 0; */
3499 id.s_addr = 0xffffffff;
3502 /* Masklen differs, then apply wildcard mask to Link State ID. */
3505 masklen2ip (p->prefixlen, &mask);
3507 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3508 lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, type,
3509 id, ospf->router_id);
3512 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3513 zlog_debug ("ospf_lsa_unique_id(): "
3514 "Can't get Link State ID for %s/%d",
3515 inet_ntoa (p->prefix), p->prefixlen);
3516 /* id.s_addr = 0; */
3517 id.s_addr = 0xffffffff;
3527 #define LSA_ACTION_FLOOD_AREA 1
3528 #define LSA_ACTION_FLUSH_AREA 2
3533 struct ospf_area *area;
3534 struct ospf_lsa *lsa;
3538 ospf_lsa_action (struct thread *t)
3540 struct lsa_action *data;
3542 data = THREAD_ARG (t);
3544 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3545 zlog_debug ("LSA[Action]: Performing scheduled LSA action: %d",
3548 switch (data->action)
3550 case LSA_ACTION_FLOOD_AREA:
3551 ospf_flood_through_area (data->area, NULL, data->lsa);
3553 case LSA_ACTION_FLUSH_AREA:
3554 ospf_lsa_flush_area (data->lsa, data->area);
3558 ospf_lsa_unlock (&data->lsa); /* Message */
3559 XFREE (MTYPE_OSPF_MESSAGE, data);
3564 ospf_schedule_lsa_flood_area (struct ospf_area *area, struct ospf_lsa *lsa)
3566 struct lsa_action *data;
3568 data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3569 data->action = LSA_ACTION_FLOOD_AREA;
3571 data->lsa = ospf_lsa_lock (lsa); /* Message / Flood area */
3573 thread_add_event (master, ospf_lsa_action, data, 0);
3577 ospf_schedule_lsa_flush_area (struct ospf_area *area, struct ospf_lsa *lsa)
3579 struct lsa_action *data;
3581 data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3582 data->action = LSA_ACTION_FLUSH_AREA;
3584 data->lsa = ospf_lsa_lock (lsa); /* Message / Flush area */
3586 thread_add_event (master, ospf_lsa_action, data, 0);
3590 /* LSA Refreshment functions. */
3592 ospf_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
3594 struct external_info *ei;
3595 struct ospf_lsa *new = NULL;
3596 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3597 assert (IS_LSA_SELF (lsa));
3598 assert (lsa->lock > 0);
3600 switch (lsa->data->type)
3602 /* Router and Network LSAs are processed differently. */
3603 case OSPF_ROUTER_LSA:
3604 new = ospf_router_lsa_refresh (lsa);
3606 case OSPF_NETWORK_LSA:
3607 new = ospf_network_lsa_refresh (lsa);
3609 case OSPF_SUMMARY_LSA:
3610 new = ospf_summary_lsa_refresh (ospf, lsa);
3612 case OSPF_ASBR_SUMMARY_LSA:
3613 new = ospf_summary_asbr_lsa_refresh (ospf, lsa);
3615 case OSPF_AS_EXTERNAL_LSA:
3616 /* Translated from NSSA Type-5s are refreshed when
3617 * from refresh of Type-7 - do not refresh these directly.
3619 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
3621 ei = ospf_external_info_check (lsa);
3623 new = ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
3625 ospf_lsa_flush_as (ospf, lsa);
3627 case OSPF_OPAQUE_LINK_LSA:
3628 case OSPF_OPAQUE_AREA_LSA:
3629 case OSPF_OPAQUE_AS_LSA:
3630 new = ospf_opaque_lsa_refresh (lsa);
3639 ospf_refresher_register_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3641 u_int16_t index, current_index;
3643 assert (lsa->lock > 0);
3644 assert (IS_LSA_SELF (lsa));
3646 if (lsa->refresh_list < 0)
3650 if (LS_AGE (lsa) == 0 &&
3651 ntohl (lsa->data->ls_seqnum) == OSPF_INITIAL_SEQUENCE_NUMBER)
3652 /* Randomize first update by OSPF_LS_REFRESH_SHIFT factor */
3653 delay = OSPF_LS_REFRESH_SHIFT + (random () % OSPF_LS_REFRESH_TIME);
3655 /* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */
3656 delay = OSPF_LS_REFRESH_TIME - LS_AGE (lsa) - OSPF_LS_REFRESH_JITTER
3657 + (random () % (2*OSPF_LS_REFRESH_JITTER));
3662 current_index = ospf->lsa_refresh_queue.index + (quagga_time (NULL)
3663 - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
3665 index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY)
3666 % (OSPF_LSA_REFRESHER_SLOTS);
3668 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3669 zlog_debug ("LSA[Refresh]: lsa %s with age %d added to index %d",
3670 inet_ntoa (lsa->data->id), LS_AGE (lsa), index);
3671 if (!ospf->lsa_refresh_queue.qs[index])
3672 ospf->lsa_refresh_queue.qs[index] = list_new ();
3673 listnode_add (ospf->lsa_refresh_queue.qs[index],
3674 ospf_lsa_lock (lsa)); /* lsa_refresh_queue */
3675 lsa->refresh_list = index;
3676 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3677 zlog_debug ("LSA[Refresh:%s]: ospf_refresher_register_lsa(): "
3678 "setting refresh_list on lsa %p (slod %d)",
3679 inet_ntoa (lsa->data->id), (void *)lsa, index);
3684 ospf_refresher_unregister_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3686 assert (lsa->lock > 0);
3687 assert (IS_LSA_SELF (lsa));
3688 if (lsa->refresh_list >= 0)
3690 struct list *refresh_list = ospf->lsa_refresh_queue.qs[lsa->refresh_list];
3691 listnode_delete (refresh_list, lsa);
3692 if (!listcount (refresh_list))
3694 list_free (refresh_list);
3695 ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
3697 ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
3698 lsa->refresh_list = -1;
3703 ospf_lsa_refresh_walker (struct thread *t)
3705 struct list *refresh_list;
3706 struct listnode *node, *nnode;
3707 struct ospf *ospf = THREAD_ARG (t);
3708 struct ospf_lsa *lsa;
3710 struct list *lsa_to_refresh = list_new ();
3712 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3713 zlog_debug ("LSA[Refresh]:ospf_lsa_refresh_walker(): start");
3716 i = ospf->lsa_refresh_queue.index;
3718 /* Note: if clock has jumped backwards, then time change could be negative,
3719 so we are careful to cast the expression to unsigned before taking
3721 ospf->lsa_refresh_queue.index =
3722 ((unsigned long)(ospf->lsa_refresh_queue.index +
3723 (quagga_time (NULL) - ospf->lsa_refresher_started)
3724 / OSPF_LSA_REFRESHER_GRANULARITY))
3725 % OSPF_LSA_REFRESHER_SLOTS;
3727 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3728 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3729 ospf->lsa_refresh_queue.index);
3731 for (;i != ospf->lsa_refresh_queue.index;
3732 i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS)
3734 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3735 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): "
3736 "refresh index %d", i);
3738 refresh_list = ospf->lsa_refresh_queue.qs [i];
3742 ospf->lsa_refresh_queue.qs [i] = NULL;
3746 for (ALL_LIST_ELEMENTS (refresh_list, node, nnode, lsa))
3748 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3749 zlog_debug ("LSA[Refresh:%s]: ospf_lsa_refresh_walker(): "
3750 "refresh lsa %p (slot %d)",
3751 inet_ntoa (lsa->data->id), (void *)lsa, i);
3753 assert (lsa->lock > 0);
3754 list_delete_node (refresh_list, node);
3755 lsa->refresh_list = -1;
3756 listnode_add (lsa_to_refresh, lsa);
3758 list_free (refresh_list);
3762 ospf->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
3763 ospf, ospf->lsa_refresh_interval);
3764 ospf->lsa_refresher_started = quagga_time (NULL);
3766 for (ALL_LIST_ELEMENTS (lsa_to_refresh, node, nnode, lsa))
3768 ospf_lsa_refresh (ospf, lsa);
3769 assert (lsa->lock > 0);
3770 ospf_lsa_unlock (&lsa); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3773 list_delete (lsa_to_refresh);
3775 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3776 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");