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"
55 get_metric (u_char
*metric
)
59 m
= (m
<< 8) + metric
[1];
60 m
= (m
<< 8) + metric
[2];
66 tv_adjust (struct timeval a
)
68 while (a
.tv_usec
>= 1000000)
84 tv_ceil (struct timeval a
)
88 return (a
.tv_usec
? a
.tv_sec
+ 1 : a
.tv_sec
);
92 tv_floor (struct timeval a
)
111 tv_add (struct timeval a
, struct timeval b
)
115 ret
.tv_sec
= a
.tv_sec
+ b
.tv_sec
;
116 ret
.tv_usec
= a
.tv_usec
+ b
.tv_usec
;
118 return tv_adjust (ret
);
122 tv_sub (struct timeval a
, struct timeval b
)
126 ret
.tv_sec
= a
.tv_sec
- b
.tv_sec
;
127 ret
.tv_usec
= a
.tv_usec
- b
.tv_usec
;
129 return tv_adjust (ret
);
133 tv_cmp (struct timeval a
, struct timeval b
)
135 return (a
.tv_sec
== b
.tv_sec
?
136 a
.tv_usec
- b
.tv_usec
: a
.tv_sec
- b
.tv_sec
);
140 ospf_lsa_refresh_delay (struct ospf_lsa
*lsa
)
142 struct timeval delta
, now
;
145 quagga_gettime (QUAGGA_CLK_MONOTONIC
, &now
);
146 delta
= tv_sub (now
, lsa
->tv_orig
);
148 if (tv_cmp (delta
, int2tv (OSPF_MIN_LS_INTERVAL
)) < 0)
150 delay
= tv_ceil (tv_sub (int2tv (OSPF_MIN_LS_INTERVAL
), delta
));
152 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
153 zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
154 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), delay
);
164 get_age (struct ospf_lsa
*lsa
)
168 age
= ntohs (lsa
->data
->ls_age
)
169 + tv_floor (tv_sub (recent_relative_time (), lsa
->tv_recv
));
175 /* Fletcher Checksum -- Refer to RFC1008. */
177 /* All the offsets are zero-based. The offsets in the RFC1008 are
180 ospf_lsa_checksum (struct lsa_header
*lsa
)
182 u_char
*buffer
= (u_char
*) &lsa
->options
;
183 int options_offset
= buffer
- (u_char
*) &lsa
->ls_age
; /* should be 2 */
185 /* Skip the AGE field */
186 u_int16_t len
= ntohs(lsa
->length
) - options_offset
;
188 /* Checksum offset starts from "options" field, not the beginning of the
189 lsa_header struct. The offset is 14, rather than 16. */
190 int checksum_offset
= (u_char
*) &lsa
->checksum
- buffer
;
192 return fletcher_checksum(buffer
, len
, checksum_offset
);
197 /* Create OSPF LSA. */
201 struct ospf_lsa
*new;
203 new = XCALLOC (MTYPE_OSPF_LSA
, sizeof (struct ospf_lsa
));
207 new->retransmit_counter
= 0;
208 new->tv_recv
= recent_relative_time ();
209 new->tv_orig
= new->tv_recv
;
210 new->refresh_list
= -1;
215 /* Duplicate OSPF LSA. */
217 ospf_lsa_dup (struct ospf_lsa
*lsa
)
219 struct ospf_lsa
*new;
224 new = XCALLOC (MTYPE_OSPF_LSA
, sizeof (struct ospf_lsa
));
226 memcpy (new, lsa
, sizeof (struct ospf_lsa
));
227 UNSET_FLAG (new->flags
, OSPF_LSA_DISCARD
);
229 new->retransmit_counter
= 0;
230 new->data
= ospf_lsa_data_dup (lsa
->data
);
232 /* kevinm: Clear the refresh_list, otherwise there are going
233 to be problems when we try to remove the LSA from the
234 queue (which it's not a member of.)
235 XXX: Should we add the LSA to the refresh_list queue? */
236 new->refresh_list
= -1;
238 if (IS_DEBUG_OSPF (lsa
, LSA
))
239 zlog_debug ("LSA: duplicated %p (new: %p)", lsa
, new);
246 ospf_lsa_free (struct ospf_lsa
*lsa
)
248 assert (lsa
->lock
== 0);
250 if (IS_DEBUG_OSPF (lsa
, LSA
))
251 zlog_debug ("LSA: freed %p", lsa
);
253 /* Delete LSA data. */
254 if (lsa
->data
!= NULL
)
255 ospf_lsa_data_free (lsa
->data
);
257 assert (lsa
->refresh_list
< 0);
259 memset (lsa
, 0, sizeof (struct ospf_lsa
));
260 XFREE (MTYPE_OSPF_LSA
, lsa
);
265 ospf_lsa_lock (struct ospf_lsa
*lsa
)
273 ospf_lsa_unlock (struct ospf_lsa
**lsa
)
275 /* This is sanity check. */
281 assert ((*lsa
)->lock
>= 0);
283 if ((*lsa
)->lock
== 0)
285 assert (CHECK_FLAG ((*lsa
)->flags
, OSPF_LSA_DISCARD
));
286 ospf_lsa_free (*lsa
);
291 /* Check discard flag. */
293 ospf_lsa_discard (struct ospf_lsa
*lsa
)
295 if (!CHECK_FLAG (lsa
->flags
, OSPF_LSA_DISCARD
))
297 SET_FLAG (lsa
->flags
, OSPF_LSA_DISCARD
);
298 ospf_lsa_unlock (&lsa
);
302 /* Create LSA data. */
304 ospf_lsa_data_new (size_t size
)
306 return XCALLOC (MTYPE_OSPF_LSA_DATA
, size
);
309 /* Duplicate LSA data. */
311 ospf_lsa_data_dup (struct lsa_header
*lsah
)
313 struct lsa_header
*new;
315 new = ospf_lsa_data_new (ntohs (lsah
->length
));
316 memcpy (new, lsah
, ntohs (lsah
->length
));
323 ospf_lsa_data_free (struct lsa_header
*lsah
)
325 if (IS_DEBUG_OSPF (lsa
, LSA
))
326 zlog_debug ("LSA[Type%d:%s]: data freed %p",
327 lsah
->type
, inet_ntoa (lsah
->id
), lsah
);
329 XFREE (MTYPE_OSPF_LSA_DATA
, lsah
);
333 /* LSA general functions. */
336 dump_lsa_key (struct ospf_lsa
*lsa
)
338 static char buf
[] = {
339 "Type255,id(255.255.255.255),ar(255.255.255.255)"
341 struct lsa_header
*lsah
;
343 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
)
345 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
346 strcpy (id
, inet_ntoa (lsah
->id
));
347 strcpy (ar
, inet_ntoa (lsah
->adv_router
));
349 sprintf (buf
, "Type%d,id(%s),ar(%s)", lsah
->type
, id
, ar
);
352 strcpy (buf
, "NULL");
358 lsa_seqnum_increment (struct ospf_lsa
*lsa
)
362 seqnum
= ntohl (lsa
->data
->ls_seqnum
) + 1;
364 return htonl (seqnum
);
368 lsa_header_set (struct stream
*s
, u_char options
,
369 u_char type
, struct in_addr id
, struct in_addr router_id
)
371 struct lsa_header
*lsah
;
373 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
375 lsah
->ls_age
= htons (OSPF_LSA_INITIAL_AGE
);
376 lsah
->options
= options
;
379 lsah
->adv_router
= router_id
;
380 lsah
->ls_seqnum
= htonl (OSPF_INITIAL_SEQUENCE_NUMBER
);
382 stream_forward_endp (s
, OSPF_LSA_HEADER_SIZE
);
386 /* router-LSA related functions. */
387 /* Get router-LSA flags. */
389 router_lsa_flags (struct ospf_area
*area
)
393 flags
= area
->ospf
->flags
;
395 /* Set virtual link flag. */
396 if (ospf_full_virtual_nbrs (area
))
397 SET_FLAG (flags
, ROUTER_LSA_VIRTUAL
);
399 /* Just sanity check */
400 UNSET_FLAG (flags
, ROUTER_LSA_VIRTUAL
);
402 /* Set Shortcut ABR behabiour flag. */
403 UNSET_FLAG (flags
, ROUTER_LSA_SHORTCUT
);
404 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
405 if (!OSPF_IS_AREA_BACKBONE (area
))
406 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
&&
407 area
->ospf
->backbone
== NULL
) ||
408 area
->shortcut_configured
== OSPF_SHORTCUT_ENABLE
)
409 SET_FLAG (flags
, ROUTER_LSA_SHORTCUT
);
411 /* ASBR can't exit in stub area. */
412 if (area
->external_routing
== OSPF_AREA_STUB
)
413 UNSET_FLAG (flags
, ROUTER_LSA_EXTERNAL
);
414 /* If ASBR set External flag */
415 else if (IS_OSPF_ASBR (area
->ospf
))
416 SET_FLAG (flags
, ROUTER_LSA_EXTERNAL
);
418 /* Set ABR dependent flags */
419 if (IS_OSPF_ABR (area
->ospf
))
421 SET_FLAG (flags
, ROUTER_LSA_BORDER
);
422 /* If Area is NSSA and we are both ABR and unconditional translator,
423 * set Nt bit to inform other routers.
425 if ( (area
->external_routing
== OSPF_AREA_NSSA
)
426 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
427 SET_FLAG (flags
, ROUTER_LSA_NT
);
432 /* Lookup neighbor other than myself.
433 And check neighbor count,
434 Point-to-Point link must have only 1 neighbor. */
435 struct ospf_neighbor
*
436 ospf_nbr_lookup_ptop (struct ospf_interface
*oi
)
438 struct ospf_neighbor
*nbr
= NULL
;
439 struct route_node
*rn
;
441 /* Search neighbor, there must be one of two nbrs. */
442 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
443 if ((nbr
= rn
->info
))
444 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
445 if (nbr
->state
== NSM_Full
)
447 route_unlock_node (rn
);
451 /* PtoP link must have only 1 neighbor. */
452 if (ospf_nbr_count (oi
, 0) > 1)
453 zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
458 /* Determine cost of link, taking RFC3137 stub-router support into
462 ospf_link_cost (struct ospf_interface
*oi
)
464 /* RFC3137 stub router support */
465 if (!CHECK_FLAG (oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
466 return oi
->output_cost
;
468 return OSPF_OUTPUT_COST_INFINITE
;
471 /* Set a link information. */
473 link_info_set (struct stream
*s
, struct in_addr id
,
474 struct in_addr data
, u_char type
, u_char tos
, u_int16_t cost
)
476 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
477 * vast majority of cases. Some rare routers with lots of links need more.
478 * we try accomodate those here.
480 if (STREAM_WRITEABLE(s
) < OSPF_ROUTER_LSA_LINK_SIZE
)
482 size_t ret
= OSPF_MAX_LSA_SIZE
;
484 /* Can we enlarge the stream still? */
485 if (STREAM_SIZE(s
) == OSPF_MAX_LSA_SIZE
)
487 /* we futz the size here for simplicity, really we need to account
489 * IP Header - (sizeof (struct ip))
490 * OSPF Header - OSPF_HEADER_SIZE
491 * LSA Header - OSPF_LSA_HEADER_SIZE
492 * MD5 auth data, if MD5 is configured - OSPF_AUTH_MD5_SIZE.
494 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
496 ret
= stream_resize (s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
499 if (ret
== OSPF_MAX_LSA_SIZE
)
501 zlog_warn ("%s: Out of space in LSA stream, left %zd, size %zd",
502 __func__
, STREAM_REMAIN (s
), STREAM_SIZE (s
));
507 /* TOS based routing is not supported. */
508 stream_put_ipv4 (s
, id
.s_addr
); /* Link ID. */
509 stream_put_ipv4 (s
, data
.s_addr
); /* Link Data. */
510 stream_putc (s
, type
); /* Link Type. */
511 stream_putc (s
, tos
); /* TOS = 0. */
512 stream_putw (s
, cost
); /* Link Cost. */
517 /* Describe Point-to-Point link (Section 12.4.1.1). */
519 lsa_link_ptop_set (struct stream
*s
, struct ospf_interface
*oi
)
522 struct ospf_neighbor
*nbr
;
523 struct in_addr id
, mask
;
524 u_int16_t cost
= ospf_link_cost (oi
);
526 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
527 zlog_debug ("LSA[Type1]: Set link Point-to-Point");
529 if ((nbr
= ospf_nbr_lookup_ptop (oi
)))
530 if (nbr
->state
== NSM_Full
)
532 /* For unnumbered point-to-point networks, the Link Data field
533 should specify the interface's MIB-II ifIndex value. */
534 links
+= link_info_set (s
, nbr
->router_id
, oi
->address
->u
.prefix4
,
535 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
538 /* Regardless of the state of the neighboring router, we must
539 add a Type 3 link (stub network).
540 N.B. Options 1 & 2 share basically the same logic. */
541 masklen2ip (oi
->address
->prefixlen
, &mask
);
542 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
& mask
.s_addr
;
543 links
+= link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
548 /* Describe Broadcast Link. */
550 lsa_link_broadcast_set (struct stream
*s
, struct ospf_interface
*oi
)
552 struct ospf_neighbor
*dr
;
553 struct in_addr id
, mask
;
554 u_int16_t cost
= ospf_link_cost (oi
);
556 /* Describe Type 3 Link. */
557 if (oi
->state
== ISM_Waiting
)
559 masklen2ip (oi
->address
->prefixlen
, &mask
);
560 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
561 return link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
565 dr
= ospf_nbr_lookup_by_addr (oi
->nbrs
, &DR (oi
));
566 /* Describe Type 2 link. */
567 if (dr
&& (dr
->state
== NSM_Full
||
568 IPV4_ADDR_SAME (&oi
->address
->u
.prefix4
, &DR (oi
))) &&
569 ospf_nbr_count (oi
, NSM_Full
) > 0)
571 return link_info_set (s
, DR (oi
), oi
->address
->u
.prefix4
,
572 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
574 /* Describe type 3 link. */
577 masklen2ip (oi
->address
->prefixlen
, &mask
);
578 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
579 return link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
585 lsa_link_loopback_set (struct stream
*s
, struct ospf_interface
*oi
)
587 struct in_addr id
, mask
;
589 /* Describe Type 3 Link. */
590 if (oi
->state
!= ISM_Loopback
)
593 mask
.s_addr
= 0xffffffff;
594 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
595 return link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, oi
->output_cost
);
598 /* Describe Virtual Link. */
600 lsa_link_virtuallink_set (struct stream
*s
, struct ospf_interface
*oi
)
602 struct ospf_neighbor
*nbr
;
603 u_int16_t cost
= ospf_link_cost (oi
);
605 if (oi
->state
== ISM_PointToPoint
)
606 if ((nbr
= ospf_nbr_lookup_ptop (oi
)))
607 if (nbr
->state
== NSM_Full
)
609 return link_info_set (s
, nbr
->router_id
, oi
->address
->u
.prefix4
,
610 LSA_LINK_TYPE_VIRTUALLINK
, 0, cost
);
616 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
618 /* this function add for support point-to-multipoint ,see rfc2328
620 /* from "edward rrr" <edward_rrr@hotmail.com>
621 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
623 lsa_link_ptomp_set (struct stream
*s
, struct ospf_interface
*oi
)
626 struct route_node
*rn
;
627 struct ospf_neighbor
*nbr
= NULL
;
628 struct in_addr id
, mask
;
629 u_int16_t cost
= ospf_link_cost (oi
);
631 mask
.s_addr
= 0xffffffff;
632 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
633 links
+= link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
635 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
636 zlog_debug ("PointToMultipoint: running ptomultip_set");
638 /* Search neighbor, */
639 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
640 if ((nbr
= rn
->info
) != NULL
)
642 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
643 if (nbr
->state
== NSM_Full
)
646 links
+= link_info_set (s
, nbr
->router_id
, oi
->address
->u
.prefix4
,
647 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
648 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
649 zlog_debug ("PointToMultipoint: set link to %s",
650 inet_ntoa(oi
->address
->u
.prefix4
));
656 /* Set router-LSA link information. */
658 router_lsa_link_set (struct stream
*s
, struct ospf_area
*area
)
660 struct listnode
*node
;
661 struct ospf_interface
*oi
;
664 for (ALL_LIST_ELEMENTS_RO (area
->oiflist
, node
, oi
))
666 struct interface
*ifp
= oi
->ifp
;
668 /* Check interface is up, OSPF is enable. */
669 if (if_is_operative (ifp
))
671 if (oi
->state
!= ISM_Down
)
673 /* Describe each link. */
676 case OSPF_IFTYPE_POINTOPOINT
:
677 links
+= lsa_link_ptop_set (s
, oi
);
679 case OSPF_IFTYPE_BROADCAST
:
680 links
+= lsa_link_broadcast_set (s
, oi
);
682 case OSPF_IFTYPE_NBMA
:
683 links
+= lsa_link_nbma_set (s
, oi
);
685 case OSPF_IFTYPE_POINTOMULTIPOINT
:
686 links
+= lsa_link_ptomp_set (s
, oi
);
688 case OSPF_IFTYPE_VIRTUALLINK
:
689 links
+= lsa_link_virtuallink_set (s
, oi
);
691 case OSPF_IFTYPE_LOOPBACK
:
692 links
+= lsa_link_loopback_set (s
, oi
);
701 /* Set router-LSA body. */
703 ospf_router_lsa_body_set (struct stream
*s
, struct ospf_area
*area
)
709 stream_putc (s
, router_lsa_flags (area
));
711 /* Set Zero fields. */
714 /* Keep pointer to # links. */
715 putp
= stream_get_endp(s
);
720 /* Set all link information. */
721 cnt
= router_lsa_link_set (s
, area
);
723 /* Set # of links here. */
724 stream_putw_at (s
, putp
, cnt
);
728 ospf_stub_router_timer (struct thread
*t
)
730 struct ospf_area
*area
= THREAD_ARG (t
);
732 area
->t_stub_router
= NULL
;
734 SET_FLAG (area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
736 /* clear stub route state and generate router-lsa refresh, don't
737 * clobber an administratively set stub-router state though.
739 if (CHECK_FLAG (area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
742 UNSET_FLAG (area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
744 ospf_router_lsa_update_area (area
);
750 ospf_stub_router_check (struct ospf_area
*area
)
752 /* area must either be administratively configured to be stub
753 * or startup-time stub-router must be configured and we must in a pre-stub
756 if (CHECK_FLAG (area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
758 SET_FLAG (area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
762 /* not admin-stubbed, check whether startup stubbing is configured and
763 * whether it's not been done yet
765 if (CHECK_FLAG (area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
))
768 if (area
->ospf
->stub_router_startup_time
== OSPF_STUB_ROUTER_UNCONFIGURED
)
770 /* stub-router is hence done forever for this area, even if someone
771 * tries configure it (take effect next restart).
773 SET_FLAG (area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
777 /* startup stub-router configured and not yet done */
778 SET_FLAG (area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
780 OSPF_AREA_TIMER_ON (area
->t_stub_router
, ospf_stub_router_timer
,
781 area
->ospf
->stub_router_startup_time
);
784 /* Create new router-LSA. */
785 static struct ospf_lsa
*
786 ospf_router_lsa_new (struct ospf_area
*area
)
788 struct ospf
*ospf
= area
->ospf
;
790 struct lsa_header
*lsah
;
791 struct ospf_lsa
*new;
794 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
795 zlog_debug ("LSA[Type1]: Create router-LSA instance");
797 /* check whether stub-router is desired, and if this is the first
800 ospf_stub_router_check (area
);
802 /* Create a stream for LSA. */
803 s
= stream_new (OSPF_MAX_LSA_SIZE
);
804 /* Set LSA common header fields. */
805 lsa_header_set (s
, LSA_OPTIONS_GET (area
) | LSA_OPTIONS_NSSA_GET (area
),
806 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
808 /* Set router-LSA body fields. */
809 ospf_router_lsa_body_set (s
, area
);
812 length
= stream_get_endp (s
);
813 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
814 lsah
->length
= htons (length
);
816 /* Now, create OSPF LSA instance. */
817 if ( (new = ospf_lsa_new ()) == NULL
)
819 zlog_err ("%s: Unable to create new lsa", __func__
);
824 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
826 /* Copy LSA data to store, discard stream. */
827 new->data
= ospf_lsa_data_new (length
);
828 memcpy (new->data
, lsah
, length
);
834 /* Originate Router-LSA. */
835 static struct ospf_lsa
*
836 ospf_router_lsa_originate (struct ospf_area
*area
)
838 struct ospf_lsa
*new;
840 /* Create new router-LSA instance. */
841 if ( (new = ospf_router_lsa_new (area
)) == NULL
)
843 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__
);
848 if (new->data
->adv_router
.s_addr
== 0)
850 if (IS_DEBUG_OSPF_EVENT
)
851 zlog_debug ("LSA[Type1]: AdvRouter is 0, discard");
852 ospf_lsa_discard (new);
856 /* Install LSA to LSDB. */
857 new = ospf_lsa_install (area
->ospf
, NULL
, new);
859 /* Update LSA origination count. */
860 area
->ospf
->lsa_originate_count
++;
862 /* Flooding new LSA through area. */
863 ospf_flood_through_area (area
, NULL
, new);
865 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
867 zlog_debug ("LSA[Type%d:%s]: Originate router-LSA %p",
868 new->data
->type
, inet_ntoa (new->data
->id
), new);
869 ospf_lsa_header_dump (new->data
);
875 /* Refresh router-LSA. */
876 static struct ospf_lsa
*
877 ospf_router_lsa_refresh (struct ospf_lsa
*lsa
)
879 struct ospf_area
*area
= lsa
->area
;
880 struct ospf_lsa
*new;
885 /* Delete LSA from neighbor retransmit-list. */
886 ospf_ls_retransmit_delete_nbr_area (area
, lsa
);
888 /* Unregister LSA from refresh-list */
889 ospf_refresher_unregister_lsa (area
->ospf
, lsa
);
891 /* Create new router-LSA instance. */
892 if ( (new = ospf_router_lsa_new (area
)) == NULL
)
894 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__
);
898 new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
900 ospf_lsa_install (area
->ospf
, NULL
, new);
902 /* Flood LSA through area. */
903 ospf_flood_through_area (area
, NULL
, new);
906 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
908 zlog_debug ("LSA[Type%d:%s]: router-LSA refresh",
909 new->data
->type
, inet_ntoa (new->data
->id
));
910 ospf_lsa_header_dump (new->data
);
917 ospf_router_lsa_update_area (struct ospf_area
*area
)
919 if (IS_DEBUG_OSPF_EVENT
)
920 zlog_debug ("[router-LSA]: (router-LSA area update)");
922 /* Now refresh router-LSA. */
923 if (area
->router_lsa_self
)
924 ospf_lsa_refresh (area
->ospf
, area
->router_lsa_self
);
925 /* Newly originate router-LSA. */
927 ospf_router_lsa_originate (area
);
933 ospf_router_lsa_update (struct ospf
*ospf
)
935 struct listnode
*node
, *nnode
;
936 struct ospf_area
*area
;
938 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
939 zlog_debug ("Timer[router-LSA Update]: (timer expire)");
941 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
943 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
944 struct router_lsa
*rl
;
945 const char *area_str
;
947 /* Keep Area ID string. */
948 area_str
= AREA_NAME (area
);
950 /* If LSA not exist in this Area, originate new. */
953 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
954 zlog_debug("LSA[Type1]: Create router-LSA for Area %s", area_str
);
956 ospf_router_lsa_originate (area
);
958 /* If router-ID is changed, Link ID must change.
959 First flush old LSA, then originate new. */
960 else if (!IPV4_ADDR_SAME (&lsa
->data
->id
, &ospf
->router_id
))
962 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
963 zlog_debug("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
964 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), area_str
);
965 ospf_refresher_unregister_lsa (ospf
, lsa
);
966 ospf_lsa_flush_area (lsa
, area
);
967 ospf_lsa_unlock (&area
->router_lsa_self
);
968 area
->router_lsa_self
= NULL
;
970 /* Refresh router-LSA, (not install) and flood through area. */
971 ospf_router_lsa_update_area (area
);
975 rl
= (struct router_lsa
*) lsa
->data
;
976 /* Refresh router-LSA, (not install) and flood through area. */
977 if (rl
->flags
!= ospf
->flags
)
978 ospf_router_lsa_update_area (area
);
986 /* network-LSA related functions. */
987 /* Originate Network-LSA. */
989 ospf_network_lsa_body_set (struct stream
*s
, struct ospf_interface
*oi
)
992 struct route_node
*rn
;
993 struct ospf_neighbor
*nbr
;
995 masklen2ip (oi
->address
->prefixlen
, &mask
);
996 stream_put_ipv4 (s
, mask
.s_addr
);
998 /* The network-LSA lists those routers that are fully adjacent to
999 the Designated Router; each fully adjacent router is identified by
1000 its OSPF Router ID. The Designated Router includes itself in this
1001 list. RFC2328, Section 12.4.2 */
1003 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
1004 if ((nbr
= rn
->info
) != NULL
)
1005 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
1006 stream_put_ipv4 (s
, nbr
->router_id
.s_addr
);
1009 static struct ospf_lsa
*
1010 ospf_network_lsa_new (struct ospf_interface
*oi
)
1013 struct ospf_lsa
*new;
1014 struct lsa_header
*lsah
;
1015 struct ospf_if_params
*oip
;
1018 /* If there are no neighbours on this network (the net is stub),
1019 the router does not originate network-LSA (see RFC 12.4.2) */
1020 if (oi
->full_nbrs
== 0)
1023 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1024 zlog_debug ("LSA[Type2]: Create network-LSA instance");
1026 /* Create new stream for LSA. */
1027 s
= stream_new (OSPF_MAX_LSA_SIZE
);
1028 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
1030 lsa_header_set (s
, (OPTIONS (oi
) | LSA_OPTIONS_GET (oi
->area
)),
1031 OSPF_NETWORK_LSA
, DR (oi
), oi
->ospf
->router_id
);
1033 /* Set network-LSA body fields. */
1034 ospf_network_lsa_body_set (s
, oi
);
1037 length
= stream_get_endp (s
);
1038 lsah
->length
= htons (length
);
1040 /* Create OSPF LSA instance. */
1041 if ( (new = ospf_lsa_new ()) == NULL
)
1043 zlog_err ("%s: ospf_lsa_new returned NULL", __func__
);
1047 new->area
= oi
->area
;
1048 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1050 /* Copy LSA to store. */
1051 new->data
= ospf_lsa_data_new (length
);
1052 memcpy (new->data
, lsah
, length
);
1055 /* Remember prior network LSA sequence numbers, even if we stop
1056 * originating one for this oi, to try avoid re-originating LSAs with a
1057 * prior sequence number, and thus speed up adjency forming & convergence.
1059 if ((oip
= ospf_lookup_if_params (oi
->ifp
, oi
->address
->u
.prefix4
)))
1061 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1062 new->data
->ls_seqnum
= lsa_seqnum_increment (new);
1066 oip
= ospf_get_if_params (oi
->ifp
, oi
->address
->u
.prefix4
);
1067 ospf_if_update_params (oi
->ifp
, oi
->address
->u
.prefix4
);
1069 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1074 /* Originate network-LSA. */
1076 ospf_network_lsa_update (struct ospf_interface
*oi
)
1078 struct ospf_lsa
*new;
1080 if (oi
->network_lsa_self
!= NULL
)
1082 ospf_lsa_refresh (oi
->ospf
, oi
->network_lsa_self
);
1086 /* Create new network-LSA instance. */
1087 new = ospf_network_lsa_new (oi
);
1091 /* Install LSA to LSDB. */
1092 new = ospf_lsa_install (oi
->ospf
, oi
, new);
1094 /* Update LSA origination count. */
1095 oi
->ospf
->lsa_originate_count
++;
1097 /* Flooding new LSA through area. */
1098 ospf_flood_through_area (oi
->area
, NULL
, new);
1100 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1102 zlog_debug ("LSA[Type%d:%s]: Originate network-LSA %p",
1103 new->data
->type
, inet_ntoa (new->data
->id
), new);
1104 ospf_lsa_header_dump (new->data
);
1110 static struct ospf_lsa
*
1111 ospf_network_lsa_refresh (struct ospf_lsa
*lsa
)
1113 struct ospf_area
*area
= lsa
->area
;
1114 struct ospf_lsa
*new, *new2
;
1115 struct ospf_if_params
*oip
;
1116 struct ospf_interface
*oi
;
1120 /* Retrieve the oi for the network LSA */
1121 oi
= ospf_if_lookup_by_local_addr (area
->ospf
, NULL
, lsa
->data
->id
);
1124 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1126 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh: "
1127 "no oi found, ick, ignoring.",
1128 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
1129 ospf_lsa_header_dump (lsa
->data
);
1133 /* Delete LSA from neighbor retransmit-list. */
1134 ospf_ls_retransmit_delete_nbr_area (area
, lsa
);
1136 /* Unregister LSA from refresh-list */
1137 ospf_refresher_unregister_lsa (area
->ospf
, lsa
);
1139 /* Create new network-LSA instance. */
1140 new = ospf_network_lsa_new (oi
);
1144 oip
= ospf_lookup_if_params (oi
->ifp
, oi
->address
->u
.prefix4
);
1145 assert (oip
!= NULL
);
1146 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
1148 new2
= ospf_lsa_install (area
->ospf
, oi
, new);
1150 assert (new2
== new);
1152 /* Flood LSA through aera. */
1153 ospf_flood_through_area (area
, NULL
, new);
1155 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1157 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh",
1158 new->data
->type
, inet_ntoa (new->data
->id
));
1159 ospf_lsa_header_dump (new->data
);
1166 stream_put_ospf_metric (struct stream
*s
, u_int32_t metric_value
)
1171 /* Put 0 metric. TOS metric is not supported. */
1172 metric
= htonl (metric_value
);
1173 mp
= (char *) &metric
;
1175 stream_put (s
, mp
, 3);
1178 /* summary-LSA related functions. */
1180 ospf_summary_lsa_body_set (struct stream
*s
, struct prefix
*p
,
1183 struct in_addr mask
;
1185 masklen2ip (p
->prefixlen
, &mask
);
1187 /* Put Network Mask. */
1188 stream_put_ipv4 (s
, mask
.s_addr
);
1191 stream_putc (s
, (u_char
) 0);
1194 stream_put_ospf_metric (s
, metric
);
1197 static struct ospf_lsa
*
1198 ospf_summary_lsa_new (struct ospf_area
*area
, struct prefix
*p
,
1199 u_int32_t metric
, struct in_addr id
)
1202 struct ospf_lsa
*new;
1203 struct lsa_header
*lsah
;
1206 if (id
.s_addr
== 0xffffffff)
1208 /* Maybe Link State ID not available. */
1209 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1210 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1215 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1216 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1218 /* Create new stream for LSA. */
1219 s
= stream_new (OSPF_MAX_LSA_SIZE
);
1220 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
1222 lsa_header_set (s
, LSA_OPTIONS_GET (area
), OSPF_SUMMARY_LSA
,
1223 id
, area
->ospf
->router_id
);
1225 /* Set summary-LSA body fields. */
1226 ospf_summary_lsa_body_set (s
, p
, metric
);
1229 length
= stream_get_endp (s
);
1230 lsah
->length
= htons (length
);
1232 /* Create OSPF LSA instance. */
1233 new = ospf_lsa_new ();
1235 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1237 /* Copy LSA to store. */
1238 new->data
= ospf_lsa_data_new (length
);
1239 memcpy (new->data
, lsah
, length
);
1245 /* Originate Summary-LSA. */
1247 ospf_summary_lsa_originate (struct prefix_ipv4
*p
, u_int32_t metric
,
1248 struct ospf_area
*area
)
1250 struct ospf_lsa
*new;
1253 id
= ospf_lsa_unique_id (area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
);
1255 if (id
.s_addr
== 0xffffffff)
1257 /* Maybe Link State ID not available. */
1258 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1259 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1264 /* Create new summary-LSA instance. */
1265 if ( !(new = ospf_summary_lsa_new (area
, (struct prefix
*) p
, metric
, id
)))
1268 /* Instlal LSA to LSDB. */
1269 new = ospf_lsa_install (area
->ospf
, NULL
, new);
1271 /* Update LSA origination count. */
1272 area
->ospf
->lsa_originate_count
++;
1274 /* Flooding new LSA through area. */
1275 ospf_flood_through_area (area
, NULL
, new);
1277 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1279 zlog_debug ("LSA[Type%d:%s]: Originate summary-LSA %p",
1280 new->data
->type
, inet_ntoa (new->data
->id
), new);
1281 ospf_lsa_header_dump (new->data
);
1287 static struct ospf_lsa
*
1288 ospf_summary_lsa_refresh (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
1290 struct ospf_lsa
*new;
1291 struct summary_lsa
*sl
;
1297 sl
= (struct summary_lsa
*)lsa
->data
;
1298 p
.prefixlen
= ip_masklen (sl
->mask
);
1299 new = ospf_summary_lsa_new (lsa
->area
, &p
, GET_METRIC (sl
->metric
),
1305 new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
1307 ospf_lsa_install (ospf
, NULL
, new);
1309 /* Flood LSA through AS. */
1310 ospf_flood_through_area (new->area
, NULL
, new);
1312 /* Debug logging. */
1313 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1315 zlog_debug ("LSA[Type%d:%s]: summary-LSA refresh",
1316 new->data
->type
, inet_ntoa (new->data
->id
));
1317 ospf_lsa_header_dump (new->data
);
1324 /* summary-ASBR-LSA related functions. */
1326 ospf_summary_asbr_lsa_body_set (struct stream
*s
, struct prefix
*p
,
1329 struct in_addr mask
;
1331 masklen2ip (p
->prefixlen
, &mask
);
1333 /* Put Network Mask. */
1334 stream_put_ipv4 (s
, mask
.s_addr
);
1337 stream_putc (s
, (u_char
) 0);
1340 stream_put_ospf_metric (s
, metric
);
1343 static struct ospf_lsa
*
1344 ospf_summary_asbr_lsa_new (struct ospf_area
*area
, struct prefix
*p
,
1345 u_int32_t metric
, struct in_addr id
)
1348 struct ospf_lsa
*new;
1349 struct lsa_header
*lsah
;
1352 if (id
.s_addr
== 0xffffffff)
1354 /* Maybe Link State ID not available. */
1355 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1356 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1357 OSPF_ASBR_SUMMARY_LSA
);
1361 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1362 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1364 /* Create new stream for LSA. */
1365 s
= stream_new (OSPF_MAX_LSA_SIZE
);
1366 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
1368 lsa_header_set (s
, LSA_OPTIONS_GET (area
), OSPF_ASBR_SUMMARY_LSA
,
1369 id
, area
->ospf
->router_id
);
1371 /* Set summary-LSA body fields. */
1372 ospf_summary_asbr_lsa_body_set (s
, p
, metric
);
1375 length
= stream_get_endp (s
);
1376 lsah
->length
= htons (length
);
1378 /* Create OSPF LSA instance. */
1379 new = ospf_lsa_new ();
1381 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1383 /* Copy LSA to store. */
1384 new->data
= ospf_lsa_data_new (length
);
1385 memcpy (new->data
, lsah
, length
);
1391 /* Originate summary-ASBR-LSA. */
1393 ospf_summary_asbr_lsa_originate (struct prefix_ipv4
*p
, u_int32_t metric
,
1394 struct ospf_area
*area
)
1396 struct ospf_lsa
*new;
1399 id
= ospf_lsa_unique_id (area
->ospf
, area
->lsdb
, OSPF_ASBR_SUMMARY_LSA
, p
);
1401 if (id
.s_addr
== 0xffffffff)
1403 /* Maybe Link State ID not available. */
1404 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1405 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1406 OSPF_ASBR_SUMMARY_LSA
);
1410 /* Create new summary-LSA instance. */
1411 new = ospf_summary_asbr_lsa_new (area
, (struct prefix
*) p
, metric
, id
);
1415 /* Install LSA to LSDB. */
1416 new = ospf_lsa_install (area
->ospf
, NULL
, new);
1418 /* Update LSA origination count. */
1419 area
->ospf
->lsa_originate_count
++;
1421 /* Flooding new LSA through area. */
1422 ospf_flood_through_area (area
, NULL
, new);
1424 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1426 zlog_debug ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1427 new->data
->type
, inet_ntoa (new->data
->id
), new);
1428 ospf_lsa_header_dump (new->data
);
1434 static struct ospf_lsa
*
1435 ospf_summary_asbr_lsa_refresh (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
1437 struct ospf_lsa
*new;
1438 struct summary_lsa
*sl
;
1444 sl
= (struct summary_lsa
*)lsa
->data
;
1445 p
.prefixlen
= ip_masklen (sl
->mask
);
1446 new = ospf_summary_asbr_lsa_new (lsa
->area
, &p
, GET_METRIC (sl
->metric
),
1451 new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
1453 ospf_lsa_install (ospf
, NULL
, new);
1455 /* Flood LSA through area. */
1456 ospf_flood_through_area (new->area
, NULL
, new);
1458 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1460 zlog_debug ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1461 new->data
->type
, inet_ntoa (new->data
->id
));
1462 ospf_lsa_header_dump (new->data
);
1468 /* AS-external-LSA related functions. */
1470 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1471 is connected, else 0*/
1472 static struct in_addr
1473 ospf_external_lsa_nexthop_get (struct ospf
*ospf
, struct in_addr nexthop
)
1477 struct listnode
*node
;
1478 struct ospf_interface
*oi
;
1482 if (!nexthop
.s_addr
)
1485 /* Check whether nexthop is covered by OSPF network. */
1486 nh
.family
= AF_INET
;
1487 nh
.u
.prefix4
= nexthop
;
1488 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1490 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1491 * better to make use of the per-ifp table of ois.
1493 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
1494 if (if_is_operative (oi
->ifp
))
1495 if (oi
->address
->family
== AF_INET
)
1496 if (prefix_match (oi
->address
, &nh
))
1502 /* NSSA-external-LSA related functions. */
1504 /* Get 1st IP connection for Forward Addr */
1507 ospf_get_ip_from_ifp (struct ospf_interface
*oi
)
1513 if (if_is_operative (oi
->ifp
))
1514 return oi
->address
->u
.prefix4
;
1519 /* Get 1st IP connection for Forward Addr */
1521 ospf_get_nssa_ip (struct ospf_area
*area
)
1524 struct in_addr best_default
;
1525 struct listnode
*node
;
1526 struct ospf_interface
*oi
;
1529 best_default
.s_addr
= 0;
1531 for (ALL_LIST_ELEMENTS_RO (area
->ospf
->oiflist
, node
, oi
))
1533 if (if_is_operative (oi
->ifp
))
1534 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1535 if (oi
->address
&& oi
->address
->family
== AF_INET
)
1537 if (best_default
.s_addr
== 0)
1538 best_default
= oi
->address
->u
.prefix4
;
1539 if (oi
->area
== area
)
1540 return oi
->address
->u
.prefix4
;
1543 if (best_default
.s_addr
!= 0)
1544 return best_default
;
1546 if (best_default
.s_addr
!= 0)
1547 return best_default
;
1552 #define DEFAULT_DEFAULT_METRIC 20
1553 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1554 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1556 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1559 metric_type (struct ospf
*ospf
, u_char src
)
1561 return (ospf
->dmetric
[src
].type
< 0 ?
1562 DEFAULT_METRIC_TYPE
: ospf
->dmetric
[src
].type
);
1566 metric_value (struct ospf
*ospf
, u_char src
)
1568 if (ospf
->dmetric
[src
].value
< 0)
1570 if (src
== DEFAULT_ROUTE
)
1572 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1573 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1575 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1577 else if (ospf
->default_metric
< 0)
1578 return DEFAULT_DEFAULT_METRIC
;
1580 return ospf
->default_metric
;
1583 return ospf
->dmetric
[src
].value
;
1586 /* Set AS-external-LSA body. */
1588 ospf_external_lsa_body_set (struct stream
*s
, struct external_info
*ei
,
1591 struct prefix_ipv4
*p
= &ei
->p
;
1592 struct in_addr mask
, fwd_addr
;
1597 /* Put Network Mask. */
1598 masklen2ip (p
->prefixlen
, &mask
);
1599 stream_put_ipv4 (s
, mask
.s_addr
);
1601 /* If prefix is default, specify DEFAULT_ROUTE. */
1602 type
= is_prefix_default (&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1604 mtype
= (ROUTEMAP_METRIC_TYPE (ei
) != -1) ?
1605 ROUTEMAP_METRIC_TYPE (ei
) : metric_type (ospf
, type
);
1607 mvalue
= (ROUTEMAP_METRIC (ei
) != -1) ?
1608 ROUTEMAP_METRIC (ei
) : metric_value (ospf
, type
);
1610 /* Put type of external metric. */
1611 stream_putc (s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1613 /* Put 0 metric. TOS metric is not supported. */
1614 stream_put_ospf_metric (s
, mvalue
);
1616 /* Get forwarding address to nexthop if on the Connection List, else 0. */
1617 fwd_addr
= ospf_external_lsa_nexthop_get (ospf
, ei
->nexthop
);
1619 /* Put forwarding address. */
1620 stream_put_ipv4 (s
, fwd_addr
.s_addr
);
1622 /* Put route tag -- This value should be introduced from configuration. */
1626 /* Create new external-LSA. */
1627 static struct ospf_lsa
*
1628 ospf_external_lsa_new (struct ospf
*ospf
,
1629 struct external_info
*ei
, struct in_addr
*old_id
)
1632 struct lsa_header
*lsah
;
1633 struct ospf_lsa
*new;
1639 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1640 zlog_debug ("LSA[Type5]: External info is NULL, can't originate");
1644 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1645 zlog_debug ("LSA[Type5]: Originate AS-external-LSA instance");
1647 /* If old Link State ID is specified, refresh LSA with same ID. */
1650 /* Get Link State with unique ID. */
1653 id
= ospf_lsa_unique_id (ospf
, ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
, &ei
->p
);
1654 if (id
.s_addr
== 0xffffffff)
1656 /* Maybe Link State ID not available. */
1657 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1658 zlog_debug ("LSA[Type5]: Link ID not available, can't originate");
1663 /* Create new stream for LSA. */
1664 s
= stream_new (OSPF_MAX_LSA_SIZE
);
1665 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
1667 /* Set LSA common header fields. */
1668 lsa_header_set (s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
,
1669 id
, ospf
->router_id
);
1671 /* Set AS-external-LSA body fields. */
1672 ospf_external_lsa_body_set (s
, ei
, ospf
);
1675 length
= stream_get_endp (s
);
1676 lsah
->length
= htons (length
);
1678 /* Now, create OSPF LSA instance. */
1679 new = ospf_lsa_new ();
1681 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1683 /* Copy LSA data to store, discard stream. */
1684 new->data
= ospf_lsa_data_new (length
);
1685 memcpy (new->data
, lsah
, length
);
1693 ospf_install_flood_nssa (struct ospf
*ospf
,
1694 struct ospf_lsa
*lsa
, struct external_info
*ei
)
1696 struct ospf_lsa
*new;
1697 struct as_external_lsa
*extlsa
;
1698 struct ospf_area
*area
;
1699 struct listnode
*node
, *nnode
;
1701 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1702 * which originated from an NSSA area. In which case it should not be
1703 * flooded back to NSSA areas.
1705 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1708 /* NSSA Originate or Refresh (If anyNSSA)
1710 LSA is self-originated. And just installed as Type-5.
1711 Additionally, install as Type-7 LSDB for every attached NSSA.
1713 P-Bit controls which ABR performs translation to outside world; If
1714 we are an ABR....do not set the P-bit, because we send the Type-5,
1715 not as the ABR Translator, but as the ASBR owner within the AS!
1717 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1718 elected ABR Translator will see the P-bit, Translate, and re-flood.
1720 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1721 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1723 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
1725 /* Don't install Type-7 LSA's into nonNSSA area */
1726 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1729 /* make lsa duplicate, lock=1 */
1730 new = ospf_lsa_dup (lsa
);
1732 new->data
->type
= OSPF_AS_NSSA_LSA
;
1734 /* set P-bit if not ABR */
1735 if (! IS_OSPF_ABR (ospf
))
1737 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1739 /* set non-zero FWD ADDR
1741 draft-ietf-ospf-nssa-update-09.txt
1743 if the network between the NSSA AS boundary router and the
1744 adjacent AS is advertised into OSPF as an internal OSPF route,
1745 the forwarding address should be the next op address as is cu
1746 currently done with type-5 LSAs. If the intervening network is
1747 not adversited into OSPF as an internal OSPF route and the
1748 type-7 LSA's P-bit is set a forwarding address should be
1749 selected from one of the router's active OSPF inteface addresses
1750 which belong to the NSSA. If no such addresses exist, then
1751 no type-7 LSA's with the P-bit set should originate from this
1754 /* kevinm: not updating lsa anymore, just new */
1755 extlsa
= (struct as_external_lsa
*)(new->data
);
1757 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0)
1758 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(area
); /* this NSSA area in ifp */
1760 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0)
1762 if (IS_DEBUG_OSPF_NSSA
)
1763 zlog_debug ("LSA[Type-7]: Could not build FWD-ADDR");
1764 ospf_lsa_discard (new);
1769 /* install also as Type-7 */
1770 ospf_lsa_install (ospf
, NULL
, new); /* Remove Old, Lock New = 2 */
1772 /* will send each copy, lock=2+n */
1773 ospf_flood_through_as (ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1777 static struct ospf_lsa
*
1778 ospf_lsa_translated_nssa_new (struct ospf
*ospf
,
1779 struct ospf_lsa
*type7
)
1782 struct ospf_lsa
*new;
1783 struct as_external_lsa
*ext
, *extnew
;
1784 struct external_info ei
;
1786 ext
= (struct as_external_lsa
*)(type7
->data
);
1788 /* need external_info struct, fill in bare minimum */
1789 ei
.p
.family
= AF_INET
;
1790 ei
.p
.prefix
= type7
->data
->id
;
1791 ei
.p
.prefixlen
= ip_masklen (ext
->mask
);
1792 ei
.type
= ZEBRA_ROUTE_OSPF
;
1793 ei
.nexthop
= ext
->header
.adv_router
;
1794 ei
.route_map_set
.metric
= -1;
1795 ei
.route_map_set
.metric_type
= -1;
1798 if ( (new = ospf_external_lsa_new (ospf
, &ei
, &type7
->data
->id
)) == NULL
)
1800 if (IS_DEBUG_OSPF_NSSA
)
1801 zlog_debug ("ospf_nssa_translate_originate(): Could not originate "
1802 "Translated Type-5 for %s",
1803 inet_ntoa (ei
.p
.prefix
));
1807 extnew
= (struct as_external_lsa
*)(new->data
);
1809 /* copy over Type-7 data to new */
1810 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1811 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1812 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1813 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1815 /* add translated flag, checksum and lock new lsa */
1816 SET_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1817 new = ospf_lsa_lock (new);
1822 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1824 ospf_translated_nssa_originate (struct ospf
*ospf
, struct ospf_lsa
*type7
)
1826 struct ospf_lsa
*new;
1827 struct as_external_lsa
*extnew
;
1829 /* we cant use ospf_external_lsa_originate() as we need to set
1830 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1833 if ( (new = ospf_lsa_translated_nssa_new (ospf
, type7
)) == NULL
)
1835 if (IS_DEBUG_OSPF_NSSA
)
1836 zlog_debug ("ospf_translated_nssa_originate(): Could not translate "
1837 "Type-7, Id %s, to Type-5",
1838 inet_ntoa (type7
->data
->id
));
1842 extnew
= (struct as_external_lsa
*)new;
1844 if (IS_DEBUG_OSPF_NSSA
)
1846 zlog_debug ("ospf_translated_nssa_originate(): "
1847 "translated Type 7, installed:");
1848 ospf_lsa_header_dump (new->data
);
1849 zlog_debug (" Network mask: %d",ip_masklen (extnew
->mask
));
1850 zlog_debug (" Forward addr: %s", inet_ntoa (extnew
->e
[0].fwd_addr
));
1853 if ( (new = ospf_lsa_install (ospf
, NULL
, new)) == NULL
)
1855 if (IS_DEBUG_OSPF_NSSA
);
1856 zlog_debug ("ospf_lsa_translated_nssa_originate(): "
1857 "Could not install LSA "
1858 "id %s", inet_ntoa (type7
->data
->id
));
1862 ospf
->lsa_originate_count
++;
1863 ospf_flood_through_as (ospf
, NULL
, new);
1868 /* Refresh Translated from NSSA AS-external-LSA. */
1870 ospf_translated_nssa_refresh (struct ospf
*ospf
, struct ospf_lsa
*type7
,
1871 struct ospf_lsa
*type5
)
1873 struct ospf_lsa
*new = NULL
;
1875 /* Sanity checks. */
1876 assert (type7
|| type5
);
1877 if (!(type7
|| type5
))
1880 assert (type7
->data
);
1882 assert (type5
->data
);
1883 assert (ospf
->anyNSSA
);
1885 /* get required data according to what has been given */
1886 if (type7
&& type5
== NULL
)
1888 /* find the translated Type-5 for this Type-7 */
1889 struct as_external_lsa
*ext
= (struct as_external_lsa
*)(type7
->data
);
1890 struct prefix_ipv4 p
=
1892 .prefix
= type7
->data
->id
,
1893 .prefixlen
= ip_masklen (ext
->mask
),
1897 type5
= ospf_external_info_find_lsa (ospf
, &p
);
1899 else if (type5
&& type7
== NULL
)
1901 /* find the type-7 from which supplied type-5 was translated,
1902 * ie find first type-7 with same LSA Id.
1904 struct listnode
*ln
, *lnn
;
1905 struct route_node
*rn
;
1906 struct ospf_lsa
*lsa
;
1907 struct ospf_area
*area
;
1909 for (ALL_LIST_ELEMENTS (ospf
->areas
, ln
, lnn
, area
))
1911 if (area
->external_routing
!= OSPF_AREA_NSSA
1915 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
1917 if (lsa
->data
->id
.s_addr
== type5
->data
->id
.s_addr
)
1926 /* do we have type7? */
1929 if (IS_DEBUG_OSPF_NSSA
)
1930 zlog_debug ("ospf_translated_nssa_refresh(): no Type-7 found for "
1932 inet_ntoa (type5
->data
->id
));
1936 /* do we have valid translated type5? */
1937 if (type5
== NULL
|| !CHECK_FLAG (type5
->flags
, OSPF_LSA_LOCAL_XLT
) )
1939 if (IS_DEBUG_OSPF_NSSA
)
1940 zlog_debug ("ospf_translated_nssa_refresh(): No translated Type-5 "
1941 "found for Type-7 with Id %s",
1942 inet_ntoa (type7
->data
->id
));
1946 /* Delete LSA from neighbor retransmit-list. */
1947 ospf_ls_retransmit_delete_nbr_as (ospf
, type5
);
1949 /* create new translated LSA */
1950 if ( (new = ospf_lsa_translated_nssa_new (ospf
, type7
)) == NULL
)
1952 if (IS_DEBUG_OSPF_NSSA
)
1953 zlog_debug ("ospf_translated_nssa_refresh(): Could not translate "
1954 "Type-7 for %s to Type-5",
1955 inet_ntoa (type7
->data
->id
));
1959 if ( !(new = ospf_lsa_install (ospf
, NULL
, new)) )
1961 if (IS_DEBUG_OSPF_NSSA
)
1962 zlog_debug ("ospf_translated_nssa_refresh(): Could not install "
1963 "translated LSA, Id %s",
1964 inet_ntoa (type7
->data
->id
));
1968 /* Flood LSA through area. */
1969 ospf_flood_through_as (ospf
, NULL
, new);
1975 is_prefix_default (struct prefix_ipv4
*p
)
1977 struct prefix_ipv4 q
;
1980 q
.prefix
.s_addr
= 0;
1983 return prefix_same ((struct prefix
*) p
, (struct prefix
*) &q
);
1986 /* Originate an AS-external-LSA, install and flood. */
1988 ospf_external_lsa_originate (struct ospf
*ospf
, struct external_info
*ei
)
1990 struct ospf_lsa
*new;
1992 /* Added for NSSA project....
1994 External LSAs are originated in ASBRs as usual, but for NSSA systems.
1995 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1996 every area. The Type-7's are flooded to every IR and every ABR; We
1997 install the Type-5 LSDB so that the normal "refresh" code operates
1998 as usual, and flag them as not used during ASE calculations. The
1999 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2000 Address of non-zero.
2002 If an ABR is the elected NSSA translator, following SPF and during
2003 the ABR task it will translate all the scanned Type-7's, with P-bit
2004 ON and not-self generated, and translate to Type-5's throughout the
2007 A difference in operation depends whether this ASBR is an ABR
2008 or not. If not an ABR, the P-bit is ON, to indicate that any
2009 elected NSSA-ABR can perform its translation.
2011 If an ABR, the P-bit is OFF; No ABR will perform translation and
2012 this ASBR will flood the Type-5 LSA as usual.
2014 For the case where this ASBR is not an ABR, the ASE calculations
2015 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2016 demonstrate to the user that there are LSA's that belong to any
2019 Finally, it just so happens that when the ABR is translating every
2020 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2021 approved Type-5 (translated from Type-7); at the end of translation
2022 if any Translated Type-5's remain unapproved, then they must be
2023 flushed from the AS.
2027 /* Check the AS-external-LSA should be originated. */
2028 if (!ospf_redistribute_check (ospf
, ei
, NULL
))
2031 /* Create new AS-external-LSA instance. */
2032 if ((new = ospf_external_lsa_new (ospf
, ei
, NULL
)) == NULL
)
2034 if (IS_DEBUG_OSPF_EVENT
)
2035 zlog_debug ("LSA[Type5:%s]: Could not originate AS-external-LSA",
2036 inet_ntoa (ei
->p
.prefix
));
2040 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2041 ospf_lsa_install (ospf
, NULL
, new);
2043 /* Update LSA origination count. */
2044 ospf
->lsa_originate_count
++;
2046 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2047 ospf_flood_through_as (ospf
, NULL
, new);
2049 /* If there is any attached NSSA, do special handling */
2050 if (ospf
->anyNSSA
&&
2051 /* stay away from translated LSAs! */
2052 !(CHECK_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
)))
2053 ospf_install_flood_nssa (ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2055 /* Debug logging. */
2056 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2058 zlog_debug ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2059 new->data
->type
, inet_ntoa (new->data
->id
), new);
2060 ospf_lsa_header_dump (new->data
);
2066 /* Originate AS-external-LSA from external info with initial flag. */
2068 ospf_external_lsa_originate_timer (struct thread
*thread
)
2070 struct ospf
*ospf
= THREAD_ARG (thread
);
2071 struct route_node
*rn
;
2072 struct external_info
*ei
;
2073 struct route_table
*rt
;
2074 int type
= THREAD_VAL (thread
);
2076 ospf
->t_external_lsa
= NULL
;
2078 /* Originate As-external-LSA from all type of distribute source. */
2079 if ((rt
= EXTERNAL_INFO (type
)))
2080 for (rn
= route_top (rt
); rn
; rn
= route_next (rn
))
2081 if ((ei
= rn
->info
) != NULL
)
2082 if (!is_prefix_default ((struct prefix_ipv4
*)&ei
->p
))
2083 if (!ospf_external_lsa_originate (ospf
, ei
))
2084 zlog_warn ("LSA: AS-external-LSA was not originated.");
2089 static struct external_info
*
2090 ospf_default_external_info (struct ospf
*ospf
)
2093 struct route_node
*rn
;
2094 struct prefix_ipv4 p
;
2097 p
.prefix
.s_addr
= 0;
2100 /* First, lookup redistributed default route. */
2101 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++)
2102 if (EXTERNAL_INFO (type
) && type
!= ZEBRA_ROUTE_OSPF
)
2104 rn
= route_node_lookup (EXTERNAL_INFO (type
), (struct prefix
*) &p
);
2107 route_unlock_node (rn
);
2109 if (ospf_redistribute_check (ospf
, rn
->info
, NULL
))
2118 ospf_default_originate_timer (struct thread
*thread
)
2120 struct prefix_ipv4 p
;
2121 struct in_addr nexthop
;
2122 struct external_info
*ei
;
2125 ospf
= THREAD_ARG (thread
);
2128 p
.prefix
.s_addr
= 0;
2131 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ALWAYS
)
2133 /* If there is no default route via redistribute,
2134 then originate AS-external-LSA with nexthop 0 (self). */
2136 ospf_external_info_add (DEFAULT_ROUTE
, p
, 0, nexthop
);
2139 if ((ei
= ospf_default_external_info (ospf
)))
2140 ospf_external_lsa_originate (ospf
, ei
);
2145 /* Flush any NSSA LSAs for given prefix */
2147 ospf_nssa_lsa_flush (struct ospf
*ospf
, struct prefix_ipv4
*p
)
2149 struct listnode
*node
, *nnode
;
2150 struct ospf_lsa
*lsa
;
2151 struct ospf_area
*area
;
2153 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
2155 if (area
->external_routing
== OSPF_AREA_NSSA
)
2157 if (!(lsa
= ospf_lsa_lookup (area
, OSPF_AS_NSSA_LSA
, p
->prefix
,
2160 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2161 zlog_debug ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2162 inet_ntoa (p
->prefix
), p
->prefixlen
);
2165 ospf_ls_retransmit_delete_nbr_area (area
, lsa
);
2166 if (!IS_LSA_MAXAGE (lsa
))
2168 ospf_refresher_unregister_lsa (ospf
, lsa
);
2169 ospf_lsa_flush_area (lsa
, area
);
2175 /* Flush an AS-external-LSA from LSDB and routing domain. */
2177 ospf_external_lsa_flush (struct ospf
*ospf
,
2178 u_char type
, struct prefix_ipv4
*p
,
2179 unsigned int ifindex
/*, struct in_addr nexthop */)
2181 struct ospf_lsa
*lsa
;
2183 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2184 zlog_debug ("LSA: Flushing AS-external-LSA %s/%d",
2185 inet_ntoa (p
->prefix
), p
->prefixlen
);
2187 /* First lookup LSA from LSDB. */
2188 if (!(lsa
= ospf_external_info_find_lsa (ospf
, p
)))
2190 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2191 zlog_debug ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
2192 inet_ntoa (p
->prefix
), p
->prefixlen
);
2196 /* If LSA is selforiginated, not a translated LSA, and there is
2197 * NSSA area, flush Type-7 LSA's at first.
2199 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2200 && !(CHECK_FLAG (lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2201 ospf_nssa_lsa_flush (ospf
, p
);
2203 /* Sweep LSA from Link State Retransmit List. */
2204 ospf_ls_retransmit_delete_nbr_as (ospf
, lsa
);
2206 /* There must be no self-originated LSA in rtrs_external. */
2208 /* Remove External route from Zebra. */
2209 ospf_zebra_delete ((struct prefix_ipv4
*) p
, &nexthop
);
2212 if (!IS_LSA_MAXAGE (lsa
))
2214 /* Unregister LSA from Refresh queue. */
2215 ospf_refresher_unregister_lsa (ospf
, lsa
);
2217 /* Flush AS-external-LSA through AS. */
2218 ospf_lsa_flush_as (ospf
, lsa
);
2221 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2222 zlog_debug ("ospf_external_lsa_flush(): stop");
2226 ospf_external_lsa_refresh_default (struct ospf
*ospf
)
2228 struct prefix_ipv4 p
;
2229 struct external_info
*ei
;
2230 struct ospf_lsa
*lsa
;
2234 p
.prefix
.s_addr
= 0;
2236 ei
= ospf_default_external_info (ospf
);
2237 lsa
= ospf_external_info_find_lsa (ospf
, &p
);
2243 if (IS_DEBUG_OSPF_EVENT
)
2244 zlog_debug ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p", lsa
);
2245 ospf_external_lsa_refresh (ospf
, lsa
, ei
, LSA_REFRESH_FORCE
);
2249 if (IS_DEBUG_OSPF_EVENT
)
2250 zlog_debug ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2251 ospf_external_lsa_originate (ospf
, ei
);
2258 if (IS_DEBUG_OSPF_EVENT
)
2259 zlog_debug ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2260 ospf_refresher_unregister_lsa (ospf
, lsa
);
2261 ospf_lsa_flush_as (ospf
, lsa
);
2267 ospf_external_lsa_refresh_type (struct ospf
*ospf
, u_char type
, int force
)
2269 struct route_node
*rn
;
2270 struct external_info
*ei
;
2272 if (type
!= DEFAULT_ROUTE
)
2273 if (EXTERNAL_INFO(type
))
2274 /* Refresh each redistributed AS-external-LSAs. */
2275 for (rn
= route_top (EXTERNAL_INFO (type
)); rn
; rn
= route_next (rn
))
2276 if ((ei
= rn
->info
))
2277 if (!is_prefix_default (&ei
->p
))
2279 struct ospf_lsa
*lsa
;
2281 if ((lsa
= ospf_external_info_find_lsa (ospf
, &ei
->p
)))
2282 ospf_external_lsa_refresh (ospf
, lsa
, ei
, force
);
2284 ospf_external_lsa_originate (ospf
, ei
);
2288 /* Refresh AS-external-LSA. */
2290 ospf_external_lsa_refresh (struct ospf
*ospf
, struct ospf_lsa
*lsa
,
2291 struct external_info
*ei
, int force
)
2293 struct ospf_lsa
*new;
2296 /* Check the AS-external-LSA should be originated. */
2297 if (!ospf_redistribute_check (ospf
, ei
, &changed
))
2299 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2300 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed, "
2301 "redist check fail",
2302 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2303 ospf_external_lsa_flush (ospf
, ei
->type
, &ei
->p
,
2304 ei
->ifindex
/*, ei->nexthop */);
2308 if (!changed
&& !force
)
2310 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2311 zlog_debug ("LSA[Type%d:%s]: Not refreshed, not changed/forced",
2312 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2316 /* Delete LSA from neighbor retransmit-list. */
2317 ospf_ls_retransmit_delete_nbr_as (ospf
, lsa
);
2319 /* Unregister AS-external-LSA from refresh-list. */
2320 ospf_refresher_unregister_lsa (ospf
, lsa
);
2322 new = ospf_external_lsa_new (ospf
, ei
, &lsa
->data
->id
);
2326 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2327 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed", lsa
->data
->type
,
2328 inet_ntoa (lsa
->data
->id
));
2332 new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
2334 ospf_lsa_install (ospf
, NULL
, new); /* As type-5. */
2336 /* Flood LSA through AS. */
2337 ospf_flood_through_as (ospf
, NULL
, new);
2339 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2340 if (ospf
->anyNSSA
&& !(CHECK_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
)))
2341 ospf_install_flood_nssa (ospf
, new, ei
); /* Install/Flood per new rules */
2343 /* Register self-originated LSA to refresh queue.
2344 * Translated LSAs should not be registered, but refreshed upon
2345 * refresh of the Type-7
2347 if ( !CHECK_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
) )
2348 ospf_refresher_register_lsa (ospf
, new);
2350 /* Debug logging. */
2351 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2353 zlog_debug ("LSA[Type%d:%s]: AS-external-LSA refresh",
2354 new->data
->type
, inet_ntoa (new->data
->id
));
2355 ospf_lsa_header_dump (new->data
);
2362 /* LSA installation functions. */
2364 /* Install router-LSA to an area. */
2365 static struct ospf_lsa
*
2366 ospf_router_lsa_install (struct ospf
*ospf
, struct ospf_lsa
*new,
2369 struct ospf_area
*area
= new->area
;
2371 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2372 The entire routing table must be recalculated, starting with
2373 the shortest path calculations for each area (not just the
2374 area whose link-state database has changed).
2377 if (IS_LSA_SELF (new))
2380 /* Only install LSA if it is originated/refreshed by us.
2381 * If LSA was received by flooding, the RECEIVED flag is set so do
2382 * not link the LSA */
2383 if (CHECK_FLAG (new->flags
, OSPF_LSA_RECEIVED
))
2384 return new; /* ignore stale LSA */
2386 /* Set self-originated router-LSA. */
2387 ospf_lsa_unlock (&area
->router_lsa_self
);
2388 area
->router_lsa_self
= ospf_lsa_lock (new);
2390 ospf_refresher_register_lsa (ospf
, new);
2393 ospf_spf_calculate_schedule (ospf
);
2398 #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
2400 (T) = thread_add_timer (master, (F), oi, (V))
2402 /* Install network-LSA to an area. */
2403 static struct ospf_lsa
*
2404 ospf_network_lsa_install (struct ospf
*ospf
,
2405 struct ospf_interface
*oi
,
2406 struct ospf_lsa
*new,
2410 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2411 The entire routing table must be recalculated, starting with
2412 the shortest path calculations for each area (not just the
2413 area whose link-state database has changed).
2415 if (IS_LSA_SELF (new))
2417 /* We supposed that when LSA is originated by us, we pass the int
2418 for which it was originated. If LSA was received by flooding,
2419 the RECEIVED flag is set, so we do not link the LSA to the int. */
2420 if (CHECK_FLAG (new->flags
, OSPF_LSA_RECEIVED
))
2421 return new; /* ignore stale LSA */
2423 ospf_lsa_unlock (&oi
->network_lsa_self
);
2424 oi
->network_lsa_self
= ospf_lsa_lock (new);
2425 ospf_refresher_register_lsa (ospf
, new);
2428 ospf_spf_calculate_schedule (ospf
);
2433 /* Install summary-LSA to an area. */
2434 static struct ospf_lsa
*
2435 ospf_summary_lsa_install (struct ospf
*ospf
, struct ospf_lsa
*new,
2438 if (rt_recalc
&& !IS_LSA_SELF (new))
2440 /* RFC 2328 Section 13.2 Summary-LSAs
2441 The best route to the destination described by the summary-
2442 LSA must be recalculated (see Section 16.5). If this
2443 destination is an AS boundary router, it may also be
2444 necessary to re-examine all the AS-external-LSAs.
2448 /* This doesn't exist yet... */
2449 ospf_summary_incremental_update(new); */
2451 ospf_spf_calculate_schedule (ospf
);
2454 if (IS_DEBUG_OSPF (lsa
, LSA_INSTALL
))
2455 zlog_debug ("ospf_summary_lsa_install(): SPF scheduled");
2458 if (IS_LSA_SELF (new))
2459 ospf_refresher_register_lsa (ospf
, new);
2464 /* Install ASBR-summary-LSA to an area. */
2465 static struct ospf_lsa
*
2466 ospf_summary_asbr_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.
2478 /* These don't exist yet... */
2479 ospf_summary_incremental_update(new);
2480 /* Isn't this done by the above call?
2481 - RFC 2328 Section 16.5 implies it should be */
2482 /* ospf_ase_calculate_schedule(); */
2484 ospf_spf_calculate_schedule (ospf
);
2488 /* register LSA to refresh-list. */
2489 if (IS_LSA_SELF (new))
2490 ospf_refresher_register_lsa (ospf
, new);
2495 /* Install AS-external-LSA. */
2496 static struct ospf_lsa
*
2497 ospf_external_lsa_install (struct ospf
*ospf
, struct ospf_lsa
*new,
2500 ospf_ase_register_external_lsa (new, ospf
);
2501 /* If LSA is not self-originated, calculate an external route. */
2504 /* RFC 2328 Section 13.2 AS-external-LSAs
2505 The best route to the destination described by the AS-
2506 external-LSA must be recalculated (see Section 16.6).
2509 if (!IS_LSA_SELF (new))
2510 ospf_ase_incremental_update (ospf
, new);
2513 if (new->data
->type
== OSPF_AS_NSSA_LSA
)
2515 /* There is no point to register selforiginate Type-7 LSA for
2516 * refreshing. We rely on refreshing Type-5 LSA's
2518 if (IS_LSA_SELF (new))
2522 /* Try refresh type-5 translated LSA for this LSA, if one exists.
2523 * New translations will be taken care of by the abr_task.
2525 ospf_translated_nssa_refresh (ospf
, new, NULL
);
2529 /* Register self-originated LSA to refresh queue.
2530 * Leave Translated LSAs alone if NSSA is enabled
2532 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
) )
2533 ospf_refresher_register_lsa (ospf
, new);
2539 ospf_discard_from_db (struct ospf
*ospf
,
2540 struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
2542 struct ospf_lsa
*old
;
2546 zlog_warn ("%s: Called with NULL lsdb!", __func__
);
2548 zlog_warn ("%s: and NULL LSA!", __func__
);
2550 zlog_warn ("LSA[Type%d:%s]: not associated with LSDB!",
2551 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2555 old
= ospf_lsdb_lookup (lsdb
, lsa
);
2560 if (old
->refresh_list
>= 0)
2561 ospf_refresher_unregister_lsa (ospf
, old
);
2563 switch (old
->data
->type
)
2565 case OSPF_AS_EXTERNAL_LSA
:
2566 ospf_ase_unregister_external_lsa (old
, ospf
);
2567 ospf_ls_retransmit_delete_nbr_as (ospf
, old
);
2569 #ifdef HAVE_OPAQUE_LSA
2570 case OSPF_OPAQUE_AS_LSA
:
2571 ospf_ls_retransmit_delete_nbr_as (ospf
, old
);
2573 #endif /* HAVE_OPAQUE_LSA */
2574 case OSPF_AS_NSSA_LSA
:
2575 ospf_ls_retransmit_delete_nbr_area (old
->area
, old
);
2576 ospf_ase_unregister_external_lsa (old
, ospf
);
2579 ospf_ls_retransmit_delete_nbr_area (old
->area
, old
);
2583 ospf_lsa_maxage_delete (ospf
, old
);
2584 ospf_lsa_discard (old
);
2588 ospf_lsa_install (struct ospf
*ospf
, struct ospf_interface
*oi
,
2589 struct ospf_lsa
*lsa
)
2591 struct ospf_lsa
*new = NULL
;
2592 struct ospf_lsa
*old
= NULL
;
2593 struct ospf_lsdb
*lsdb
= NULL
;
2597 switch (lsa
->data
->type
)
2600 case OSPF_AS_NSSA_LSA
:
2602 lsdb
= lsa
->area
->lsdb
;
2606 case OSPF_AS_EXTERNAL_LSA
:
2607 #ifdef HAVE_OPAQUE_LSA
2608 case OSPF_OPAQUE_AS_LSA
:
2609 #endif /* HAVE_OPAQUE_LSA */
2613 lsdb
= lsa
->area
->lsdb
;
2619 /* RFC 2328 13.2. Installing LSAs in the database
2621 Installing a new LSA in the database, either as the result of
2622 flooding or a newly self-originated LSA, may cause the OSPF
2623 routing table structure to be recalculated. The contents of the
2624 new LSA should be compared to the old instance, if present. If
2625 there is no difference, there is no need to recalculate the
2626 routing table. When comparing an LSA to its previous instance,
2627 the following are all considered to be differences in contents:
2629 o The LSA's Options field has changed.
2631 o One of the LSA instances has LS age set to MaxAge, and
2634 o The length field in the LSA header has changed.
2636 o The body of the LSA (i.e., anything outside the 20-byte
2637 LSA header) has changed. Note that this excludes changes
2638 in LS Sequence Number and LS Checksum.
2641 /* Look up old LSA and determine if any SPF calculation or incremental
2643 old
= ospf_lsdb_lookup (lsdb
, lsa
);
2645 /* Do comparision and record if recalc needed. */
2647 if ( old
== NULL
|| ospf_lsa_different(old
, lsa
))
2651 Sequence number check (Section 14.1 of rfc 2328)
2652 "Premature aging is used when it is time for a self-originated
2653 LSA's sequence number field to wrap. At this point, the current
2654 LSA instance (having LS sequence number MaxSequenceNumber) must
2655 be prematurely aged and flushed from the routing domain before a
2656 new instance with sequence number equal to InitialSequenceNumber
2657 can be originated. "
2660 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
)
2662 if (ospf_lsa_is_self_originated(ospf
, lsa
))
2664 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2666 if (!IS_LSA_MAXAGE(lsa
))
2667 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2668 lsa
->data
->ls_age
= htons (OSPF_LSA_MAXAGE
);
2670 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
2672 zlog_debug ("ospf_lsa_install() Premature Aging "
2673 "lsa 0x%p, seqnum 0x%x",
2674 lsa
, ntohl(lsa
->data
->ls_seqnum
));
2675 ospf_lsa_header_dump (lsa
->data
);
2680 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2682 zlog_debug ("ospf_lsa_install() got an lsa with seq 0x80000000 "
2683 "that was not self originated. Ignoring\n");
2684 ospf_lsa_header_dump (lsa
->data
);
2690 /* discard old LSA from LSDB */
2692 ospf_discard_from_db (ospf
, lsdb
, lsa
);
2694 /* Calculate Checksum if self-originated?. */
2695 if (IS_LSA_SELF (lsa
))
2696 ospf_lsa_checksum (lsa
->data
);
2698 /* Insert LSA to LSDB. */
2699 ospf_lsdb_add (lsdb
, lsa
);
2702 /* Do LSA specific installation process. */
2703 switch (lsa
->data
->type
)
2705 case OSPF_ROUTER_LSA
:
2706 new = ospf_router_lsa_install (ospf
, lsa
, rt_recalc
);
2708 case OSPF_NETWORK_LSA
:
2710 new = ospf_network_lsa_install (ospf
, oi
, lsa
, rt_recalc
);
2712 case OSPF_SUMMARY_LSA
:
2713 new = ospf_summary_lsa_install (ospf
, lsa
, rt_recalc
);
2715 case OSPF_ASBR_SUMMARY_LSA
:
2716 new = ospf_summary_asbr_lsa_install (ospf
, lsa
, rt_recalc
);
2718 case OSPF_AS_EXTERNAL_LSA
:
2719 new = ospf_external_lsa_install (ospf
, lsa
, rt_recalc
);
2721 #ifdef HAVE_OPAQUE_LSA
2722 case OSPF_OPAQUE_LINK_LSA
:
2723 if (IS_LSA_SELF (lsa
))
2724 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for this LSA. */
2726 ; /* Incoming "oi" for this LSA has set at LSUpd reception. */
2728 case OSPF_OPAQUE_AREA_LSA
:
2729 case OSPF_OPAQUE_AS_LSA
:
2730 new = ospf_opaque_lsa_install (lsa
, rt_recalc
);
2732 #endif /* HAVE_OPAQUE_LSA */
2733 case OSPF_AS_NSSA_LSA
:
2734 new = ospf_external_lsa_install (ospf
, lsa
, rt_recalc
);
2735 default: /* type-6,8,9....nothing special */
2740 return new; /* Installation failed, cannot proceed further -- endo. */
2743 if (IS_DEBUG_OSPF (lsa
, LSA_INSTALL
))
2745 char area_str
[INET_ADDRSTRLEN
];
2747 switch (lsa
->data
->type
)
2749 case OSPF_AS_EXTERNAL_LSA
:
2750 #ifdef HAVE_OPAQUE_LSA
2751 case OSPF_OPAQUE_AS_LSA
:
2752 #endif /* HAVE_OPAQUE_LSA */
2753 case OSPF_AS_NSSA_LSA
:
2754 zlog_debug ("LSA[%s]: Install %s",
2756 LOOKUP (ospf_lsa_type_msg
, new->data
->type
));
2759 strcpy (area_str
, inet_ntoa (new->area
->area_id
));
2760 zlog_debug ("LSA[%s]: Install %s to Area %s",
2762 LOOKUP (ospf_lsa_type_msg
, new->data
->type
), area_str
);
2768 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2769 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2771 if ((lsa
->flags
& OSPF_LSA_PREMATURE_AGE
) ||
2772 (IS_LSA_MAXAGE (new) && !IS_LSA_SELF (new)))
2774 if (IS_DEBUG_OSPF (lsa
, LSA_INSTALL
))
2775 zlog_debug ("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2777 inet_ntoa (new->data
->id
),
2779 ospf_lsa_flush (ospf
, lsa
);
2787 ospf_check_nbr_status (struct ospf
*ospf
)
2789 struct listnode
*node
, *nnode
;
2790 struct ospf_interface
*oi
;
2792 for (ALL_LIST_ELEMENTS (ospf
->oiflist
, node
, nnode
, oi
))
2794 struct route_node
*rn
;
2795 struct ospf_neighbor
*nbr
;
2797 if (ospf_if_is_enable (oi
))
2798 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
2799 if ((nbr
= rn
->info
) != NULL
)
2800 if (nbr
->state
== NSM_Exchange
|| nbr
->state
== NSM_Loading
)
2802 route_unlock_node (rn
);
2813 ospf_maxage_lsa_remover (struct thread
*thread
)
2815 struct ospf
*ospf
= THREAD_ARG (thread
);
2816 struct ospf_lsa
*lsa
;
2817 struct listnode
*node
, *nnode
;
2820 ospf
->t_maxage
= NULL
;
2822 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2823 zlog_debug ("LSA[MaxAge]: remover Start");
2825 reschedule
= !ospf_check_nbr_status (ospf
);
2828 for (ALL_LIST_ELEMENTS (ospf
->maxage_lsa
, node
, nnode
, lsa
))
2830 if (lsa
->retransmit_counter
> 0)
2836 /* TODO: maybe convert this function to a work-queue */
2837 if (thread_should_yield (thread
))
2838 OSPF_TIMER_ON (ospf
->t_maxage
, ospf_maxage_lsa_remover
, 0);
2840 /* Remove LSA from the LSDB */
2841 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF
))
2842 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2843 zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2844 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), (u_long
)lsa
);
2846 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2847 zlog_debug ("LSA[Type%d:%s]: MaxAge LSA removed from list",
2848 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2850 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_PREMATURE_AGE
))
2852 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2853 zlog_debug ("originating new lsa for lsa 0x%p\n", lsa
);
2854 ospf_lsa_refresh (ospf
, lsa
);
2857 /* Remove from lsdb. */
2860 ospf_discard_from_db (ospf
, lsa
->lsdb
, lsa
);
2861 ospf_lsdb_delete (lsa
->lsdb
, lsa
);
2864 zlog_warn ("%s: LSA[Type%d:%s]: No associated LSDB!", __func__
,
2865 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2868 /* A MaxAge LSA must be removed immediately from the router's link
2869 state database as soon as both a) it is no longer contained on any
2870 neighbor Link state retransmission lists and b) none of the router's
2871 neighbors are in states Exchange or Loading. */
2873 OSPF_TIMER_ON (ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2874 ospf
->maxage_delay
);
2880 ospf_lsa_maxage_delete (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2884 if ((n
= listnode_lookup (ospf
->maxage_lsa
, lsa
)))
2886 list_delete_node (ospf
->maxage_lsa
, n
);
2887 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2888 ospf_lsa_unlock (&lsa
); /* maxage_lsa */
2892 /* Add LSA onto the MaxAge list, and schedule for removal.
2893 * This does *not* lead to the LSA being flooded, that must be taken
2894 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2898 ospf_lsa_maxage (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2900 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2901 and schedule the MaxAge LSA remover. */
2902 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
))
2904 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2905 zlog_debug ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2906 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), lsa
);
2910 listnode_add (ospf
->maxage_lsa
, ospf_lsa_lock (lsa
));
2911 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2913 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2914 zlog_debug ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa
));
2916 OSPF_TIMER_ON (ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2917 ospf
->maxage_delay
);
2921 ospf_lsa_maxage_walker_remover (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2923 /* Stay away from any Local Translated Type-7 LSAs */
2924 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
2927 if (IS_LSA_MAXAGE (lsa
))
2928 /* Self-originated LSAs should NOT time-out instead,
2929 they're flushed and submitted to the max_age list explicitly. */
2930 if (!ospf_lsa_is_self_originated (ospf
, lsa
))
2932 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2933 zlog_debug("LSA[%s]: is MaxAge", dump_lsa_key (lsa
));
2935 switch (lsa
->data
->type
)
2937 #ifdef HAVE_OPAQUE_LSA
2938 case OSPF_OPAQUE_LINK_LSA
:
2939 case OSPF_OPAQUE_AREA_LSA
:
2940 case OSPF_OPAQUE_AS_LSA
:
2942 * As a general rule, whenever network topology has changed
2943 * (due to an LSA removal in this case), routing recalculation
2944 * should be triggered. However, this is not true for opaque
2945 * LSAs. Even if an opaque LSA instance is going to be removed
2946 * from the routing domain, it does not mean a change in network
2947 * topology, and thus, routing recalculation is not needed here.
2950 #endif /* HAVE_OPAQUE_LSA */
2951 case OSPF_AS_EXTERNAL_LSA
:
2952 case OSPF_AS_NSSA_LSA
:
2953 ospf_ase_incremental_update (ospf
, lsa
);
2956 ospf_spf_calculate_schedule (ospf
);
2959 ospf_lsa_maxage (ospf
, lsa
);
2962 if (IS_LSA_MAXAGE (lsa
) && !ospf_lsa_is_self_originated (ospf
, lsa
))
2963 if (LS_AGE (lsa
) > OSPF_LSA_MAXAGE
+ 30)
2964 printf ("Eek! Shouldn't happen!\n");
2969 /* Periodical check of MaxAge LSA. */
2971 ospf_lsa_maxage_walker (struct thread
*thread
)
2973 struct ospf
*ospf
= THREAD_ARG (thread
);
2974 struct route_node
*rn
;
2975 struct ospf_lsa
*lsa
;
2976 struct ospf_area
*area
;
2977 struct listnode
*node
, *nnode
;
2979 ospf
->t_maxage_walker
= NULL
;
2981 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
2983 LSDB_LOOP (ROUTER_LSDB (area
), rn
, lsa
)
2984 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
2985 LSDB_LOOP (NETWORK_LSDB (area
), rn
, lsa
)
2986 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
2987 LSDB_LOOP (SUMMARY_LSDB (area
), rn
, lsa
)
2988 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
2989 LSDB_LOOP (ASBR_SUMMARY_LSDB (area
), rn
, lsa
)
2990 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
2991 #ifdef HAVE_OPAQUE_LSA
2992 LSDB_LOOP (OPAQUE_AREA_LSDB (area
), rn
, lsa
)
2993 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
2994 LSDB_LOOP (OPAQUE_LINK_LSDB (area
), rn
, lsa
)
2995 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
2996 #endif /* HAVE_OPAQUE_LSA */
2997 LSDB_LOOP (NSSA_LSDB (area
), rn
, lsa
)
2998 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3001 /* for AS-external-LSAs. */
3004 LSDB_LOOP (EXTERNAL_LSDB (ospf
), rn
, lsa
)
3005 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3006 #ifdef HAVE_OPAQUE_LSA
3007 LSDB_LOOP (OPAQUE_AS_LSDB (ospf
), rn
, lsa
)
3008 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3009 #endif /* HAVE_OPAQUE_LSA */
3012 OSPF_TIMER_ON (ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3013 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3018 ospf_lsa_lookup_by_prefix (struct ospf_lsdb
*lsdb
, u_char type
,
3019 struct prefix_ipv4
*p
, struct in_addr router_id
)
3021 struct ospf_lsa
*lsa
;
3022 struct in_addr mask
, id
;
3023 struct lsa_header_mask
3025 struct lsa_header header
;
3026 struct in_addr mask
;
3029 lsa
= ospf_lsdb_lookup_by_id (lsdb
, type
, p
->prefix
, router_id
);
3033 masklen2ip (p
->prefixlen
, &mask
);
3035 hmask
= (struct lsa_header_mask
*) lsa
->data
;
3037 if (mask
.s_addr
!= hmask
->mask
.s_addr
)
3039 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3040 lsa
= ospf_lsdb_lookup_by_id (lsdb
, type
, id
, router_id
);
3049 ospf_lsa_lookup (struct ospf_area
*area
, u_int32_t type
,
3050 struct in_addr id
, struct in_addr adv_router
)
3052 struct ospf
*ospf
= ospf_lookup();
3057 case OSPF_ROUTER_LSA
:
3058 case OSPF_NETWORK_LSA
:
3059 case OSPF_SUMMARY_LSA
:
3060 case OSPF_ASBR_SUMMARY_LSA
:
3061 case OSPF_AS_NSSA_LSA
:
3062 #ifdef HAVE_OPAQUE_LSA
3063 case OSPF_OPAQUE_LINK_LSA
:
3064 case OSPF_OPAQUE_AREA_LSA
:
3065 #endif /* HAVE_OPAQUE_LSA */
3066 return ospf_lsdb_lookup_by_id (area
->lsdb
, type
, id
, adv_router
);
3067 case OSPF_AS_EXTERNAL_LSA
:
3068 #ifdef HAVE_OPAQUE_LSA
3069 case OSPF_OPAQUE_AS_LSA
:
3070 #endif /* HAVE_OPAQUE_LSA */
3071 return ospf_lsdb_lookup_by_id (ospf
->lsdb
, type
, id
, adv_router
);
3080 ospf_lsa_lookup_by_id (struct ospf_area
*area
, u_int32_t type
,
3083 struct ospf_lsa
*lsa
;
3084 struct route_node
*rn
;
3088 case OSPF_ROUTER_LSA
:
3089 return ospf_lsdb_lookup_by_id (area
->lsdb
, type
, id
, id
);
3090 case OSPF_NETWORK_LSA
:
3091 for (rn
= route_top (NETWORK_LSDB (area
)); rn
; rn
= route_next (rn
))
3092 if ((lsa
= rn
->info
))
3093 if (IPV4_ADDR_SAME (&lsa
->data
->id
, &id
))
3095 route_unlock_node (rn
);
3099 case OSPF_SUMMARY_LSA
:
3100 case OSPF_ASBR_SUMMARY_LSA
:
3101 /* Currently not used. */
3103 return ospf_lsdb_lookup_by_id (area
->lsdb
, type
, id
, id
);
3104 case OSPF_AS_EXTERNAL_LSA
:
3105 case OSPF_AS_NSSA_LSA
:
3106 #ifdef HAVE_OPAQUE_LSA
3107 case OSPF_OPAQUE_LINK_LSA
:
3108 case OSPF_OPAQUE_AREA_LSA
:
3109 case OSPF_OPAQUE_AS_LSA
:
3110 /* Currently not used. */
3112 #endif /* HAVE_OPAQUE_LSA */
3121 ospf_lsa_lookup_by_header (struct ospf_area
*area
, struct lsa_header
*lsah
)
3123 struct ospf_lsa
*match
;
3125 #ifdef HAVE_OPAQUE_LSA
3127 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3128 * is redefined to have two subfields; opaque-type and opaque-id.
3129 * However, it is harmless to treat the two sub fields together, as if
3130 * they two were forming a unique LSA-ID.
3132 #endif /* HAVE_OPAQUE_LSA */
3134 match
= ospf_lsa_lookup (area
, lsah
->type
, lsah
->id
, lsah
->adv_router
);
3137 if (IS_DEBUG_OSPF (lsa
, LSA
) == OSPF_DEBUG_LSA
)
3138 zlog_debug ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3139 lsah
->type
, inet_ntoa (lsah
->id
));
3144 /* return +n, l1 is more recent.
3145 return -n, l2 is more recent.
3146 return 0, l1 and l2 is identical. */
3148 ospf_lsa_more_recent (struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3153 if (l1
== NULL
&& l2
== NULL
)
3160 /* compare LS sequence number. */
3161 x
= (int) ntohl (l1
->data
->ls_seqnum
);
3162 y
= (int) ntohl (l2
->data
->ls_seqnum
);
3168 /* compare LS checksum. */
3169 r
= ntohs (l1
->data
->checksum
) - ntohs (l2
->data
->checksum
);
3173 /* compare LS age. */
3174 if (IS_LSA_MAXAGE (l1
) && !IS_LSA_MAXAGE (l2
))
3176 else if (!IS_LSA_MAXAGE (l1
) && IS_LSA_MAXAGE (l2
))
3179 /* compare LS age with MaxAgeDiff. */
3180 if (LS_AGE (l1
) - LS_AGE (l2
) > OSPF_LSA_MAXAGE_DIFF
)
3182 else if (LS_AGE (l2
) - LS_AGE (l1
) > OSPF_LSA_MAXAGE_DIFF
)
3185 /* LSAs are identical. */
3189 /* If two LSAs are different, return 1, otherwise return 0. */
3191 ospf_lsa_different (struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3199 if (l1
->data
->options
!= l2
->data
->options
)
3202 if (IS_LSA_MAXAGE (l1
) && !IS_LSA_MAXAGE (l2
))
3205 if (IS_LSA_MAXAGE (l2
) && !IS_LSA_MAXAGE (l1
))
3208 if (l1
->data
->length
!= l2
->data
->length
)
3211 if (l1
->data
->length
== 0)
3214 if (CHECK_FLAG ((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3215 return 1; /* May be a stale LSA in the LSBD */
3217 assert ( ntohs(l1
->data
->length
) > OSPF_LSA_HEADER_SIZE
);
3219 p1
= (char *) l1
->data
;
3220 p2
= (char *) l2
->data
;
3222 if (memcmp (p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3223 ntohs( l1
->data
->length
) - OSPF_LSA_HEADER_SIZE
) != 0)
3229 #ifdef ORIGINAL_CODING
3231 ospf_lsa_flush_self_originated (struct ospf_neighbor
*nbr
,
3232 struct ospf_lsa
*self
,
3233 struct ospf_lsa
*new)
3237 /* Adjust LS Sequence Number. */
3238 seqnum
= ntohl (new->data
->ls_seqnum
) + 1;
3239 self
->data
->ls_seqnum
= htonl (seqnum
);
3241 /* Recalculate LSA checksum. */
3242 ospf_lsa_checksum (self
->data
);
3244 /* Reflooding LSA. */
3245 /* RFC2328 Section 13.3
3246 On non-broadcast networks, separate Link State Update
3247 packets must be sent, as unicasts, to each adjacent neighbor
3248 (i.e., those in state Exchange or greater). The destination
3249 IP addresses for these packets are the neighbors' IP
3251 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
)
3253 struct route_node
*rn
;
3254 struct ospf_neighbor
*onbr
;
3256 for (rn
= route_top (nbr
->oi
->nbrs
); rn
; rn
= route_next (rn
))
3257 if ((onbr
= rn
->info
) != NULL
)
3258 if (onbr
!= nbr
->oi
->nbr_self
&& onbr
->status
>= NSM_Exchange
)
3259 ospf_ls_upd_send_lsa (onbr
, self
, OSPF_SEND_PACKET_DIRECT
);
3262 ospf_ls_upd_send_lsa (nbr
, self
, OSPF_SEND_PACKET_INDIRECT
);
3264 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
3265 zlog_debug ("LSA[Type%d:%s]: Flush self-originated LSA",
3266 self
->data
->type
, inet_ntoa (self
->data
->id
));
3268 #else /* ORIGINAL_CODING */
3270 ospf_lsa_flush_schedule (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3272 if (lsa
== NULL
|| !IS_LSA_SELF (lsa
))
3275 if (IS_DEBUG_OSPF_EVENT
)
3276 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
3278 /* Force given lsa's age to MaxAge. */
3279 lsa
->data
->ls_age
= htons (OSPF_LSA_MAXAGE
);
3281 switch (lsa
->data
->type
)
3283 #ifdef HAVE_OPAQUE_LSA
3284 /* Opaque wants to be notified of flushes */
3285 case OSPF_OPAQUE_LINK_LSA
:
3286 case OSPF_OPAQUE_AREA_LSA
:
3287 case OSPF_OPAQUE_AS_LSA
:
3288 ospf_opaque_lsa_refresh (lsa
);
3290 #endif /* HAVE_OPAQUE_LSA */
3292 ospf_refresher_unregister_lsa (ospf
, lsa
);
3293 ospf_lsa_flush (ospf
, lsa
);
3301 ospf_flush_self_originated_lsas_now (struct ospf
*ospf
)
3303 struct listnode
*node
, *nnode
;
3304 struct listnode
*node2
, *nnode2
;
3305 struct ospf_area
*area
;
3306 struct ospf_interface
*oi
;
3307 struct ospf_lsa
*lsa
;
3308 struct route_node
*rn
;
3309 int need_to_flush_ase
= 0;
3311 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
3313 if ((lsa
= area
->router_lsa_self
) != NULL
)
3315 if (IS_DEBUG_OSPF_EVENT
)
3316 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3317 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
3319 ospf_refresher_unregister_lsa (ospf
, lsa
);
3320 ospf_lsa_flush_area (lsa
, area
);
3321 ospf_lsa_unlock (&area
->router_lsa_self
);
3322 area
->router_lsa_self
= NULL
;
3325 for (ALL_LIST_ELEMENTS (area
->oiflist
, node2
, nnode2
, oi
))
3327 if ((lsa
= oi
->network_lsa_self
) != NULL
3328 && oi
->state
== ISM_DR
3329 && oi
->full_nbrs
> 0)
3331 if (IS_DEBUG_OSPF_EVENT
)
3332 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3333 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
3335 ospf_refresher_unregister_lsa (ospf
, oi
->network_lsa_self
);
3336 ospf_lsa_flush_area (oi
->network_lsa_self
, area
);
3337 ospf_lsa_unlock (&oi
->network_lsa_self
);
3338 oi
->network_lsa_self
= NULL
;
3341 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3342 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3343 need_to_flush_ase
= 1;
3346 LSDB_LOOP (SUMMARY_LSDB (area
), rn
, lsa
)
3347 ospf_lsa_flush_schedule (ospf
, lsa
);
3348 LSDB_LOOP (ASBR_SUMMARY_LSDB (area
), rn
, lsa
)
3349 ospf_lsa_flush_schedule (ospf
, lsa
);
3350 #ifdef HAVE_OPAQUE_LSA
3351 LSDB_LOOP (OPAQUE_LINK_LSDB (area
), rn
, lsa
)
3352 ospf_lsa_flush_schedule (ospf
, lsa
);
3353 LSDB_LOOP (OPAQUE_AREA_LSDB (area
), rn
, lsa
)
3354 ospf_lsa_flush_schedule (ospf
, lsa
);
3355 #endif /* HAVE_OPAQUE_LSA */
3358 if (need_to_flush_ase
)
3360 LSDB_LOOP (EXTERNAL_LSDB (ospf
), rn
, lsa
)
3361 ospf_lsa_flush_schedule (ospf
, lsa
);
3362 #ifdef HAVE_OPAQUE_LSA
3363 LSDB_LOOP (OPAQUE_AS_LSDB (ospf
), rn
, lsa
)
3364 ospf_lsa_flush_schedule (ospf
, lsa
);
3365 #endif /* HAVE_OPAQUE_LSA */
3369 * Make sure that the MaxAge LSA remover is executed immediately,
3370 * without conflicting to other threads.
3372 if (ospf
->t_maxage
!= NULL
)
3374 OSPF_TIMER_OFF (ospf
->t_maxage
);
3375 thread_execute (master
, ospf_maxage_lsa_remover
, ospf
, 0);
3380 #endif /* ORIGINAL_CODING */
3382 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3383 /* An interface-independent version of ospf_lsa_is_self_originated */
3385 ospf_lsa_is_self_originated (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3387 struct listnode
*node
;
3388 struct ospf_interface
*oi
;
3390 /* This LSA is already checked. */
3391 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3392 return CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3394 /* Make sure LSA is self-checked. */
3395 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3397 /* AdvRouter and Router ID is the same. */
3398 if (IPV4_ADDR_SAME (&lsa
->data
->adv_router
, &ospf
->router_id
))
3399 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3401 /* LSA is router-LSA. */
3402 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
&&
3403 IPV4_ADDR_SAME (&lsa
->data
->id
, &ospf
->router_id
))
3404 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3406 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3407 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3408 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
3410 /* Ignore virtual link. */
3411 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3412 if (oi
->address
->family
== AF_INET
)
3413 if (IPV4_ADDR_SAME (&lsa
->data
->id
, &oi
->address
->u
.prefix4
))
3415 /* to make it easier later */
3416 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3417 return CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3421 return CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3424 /* Get unique Link State ID. */
3426 ospf_lsa_unique_id (struct ospf
*ospf
,
3427 struct ospf_lsdb
*lsdb
, u_char type
, struct prefix_ipv4
*p
)
3429 struct ospf_lsa
*lsa
;
3430 struct in_addr mask
, id
;
3434 /* Check existence of LSA instance. */
3435 lsa
= ospf_lsdb_lookup_by_id (lsdb
, type
, id
, ospf
->router_id
);
3438 struct as_external_lsa
*al
= (struct as_external_lsa
*) lsa
->data
;
3439 if (ip_masklen (al
->mask
) == p
->prefixlen
)
3441 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
3442 zlog_debug ("ospf_lsa_unique_id(): "
3443 "Can't get Link State ID for %s/%d",
3444 inet_ntoa (p
->prefix
), p
->prefixlen
);
3445 /* id.s_addr = 0; */
3446 id
.s_addr
= 0xffffffff;
3449 /* Masklen differs, then apply wildcard mask to Link State ID. */
3452 masklen2ip (p
->prefixlen
, &mask
);
3454 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3455 lsa
= ospf_lsdb_lookup_by_id (ospf
->lsdb
, type
,
3456 id
, ospf
->router_id
);
3459 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
3460 zlog_debug ("ospf_lsa_unique_id(): "
3461 "Can't get Link State ID for %s/%d",
3462 inet_ntoa (p
->prefix
), p
->prefixlen
);
3463 /* id.s_addr = 0; */
3464 id
.s_addr
= 0xffffffff;
3474 #define LSA_ACTION_FLOOD_AREA 1
3475 #define LSA_ACTION_FLUSH_AREA 2
3480 struct ospf_area
*area
;
3481 struct ospf_lsa
*lsa
;
3485 ospf_lsa_action (struct thread
*t
)
3487 struct lsa_action
*data
;
3489 data
= THREAD_ARG (t
);
3491 if (IS_DEBUG_OSPF (lsa
, LSA
) == OSPF_DEBUG_LSA
)
3492 zlog_debug ("LSA[Action]: Performing scheduled LSA action: %d",
3495 switch (data
->action
)
3497 case LSA_ACTION_FLOOD_AREA
:
3498 ospf_flood_through_area (data
->area
, NULL
, data
->lsa
);
3500 case LSA_ACTION_FLUSH_AREA
:
3501 ospf_lsa_flush_area (data
->lsa
, data
->area
);
3505 ospf_lsa_unlock (&data
->lsa
); /* Message */
3506 XFREE (MTYPE_OSPF_MESSAGE
, data
);
3511 ospf_schedule_lsa_flood_area (struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3513 struct lsa_action
*data
;
3515 data
= XCALLOC (MTYPE_OSPF_MESSAGE
, sizeof (struct lsa_action
));
3516 data
->action
= LSA_ACTION_FLOOD_AREA
;
3518 data
->lsa
= ospf_lsa_lock (lsa
); /* Message / Flood area */
3520 thread_add_event (master
, ospf_lsa_action
, data
, 0);
3524 ospf_schedule_lsa_flush_area (struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3526 struct lsa_action
*data
;
3528 data
= XCALLOC (MTYPE_OSPF_MESSAGE
, sizeof (struct lsa_action
));
3529 data
->action
= LSA_ACTION_FLUSH_AREA
;
3531 data
->lsa
= ospf_lsa_lock (lsa
); /* Message / Flush area */
3533 thread_add_event (master
, ospf_lsa_action
, data
, 0);
3537 /* LSA Refreshment functions. */
3539 ospf_lsa_refresh (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3541 struct external_info
*ei
;
3542 struct ospf_lsa
*new = NULL
;
3543 assert (CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF
));
3544 assert (lsa
->lock
> 0);
3546 switch (lsa
->data
->type
)
3548 /* Router and Network LSAs are processed differently. */
3549 case OSPF_ROUTER_LSA
:
3550 new = ospf_router_lsa_refresh (lsa
);
3552 case OSPF_NETWORK_LSA
:
3553 new = ospf_network_lsa_refresh (lsa
);
3555 case OSPF_SUMMARY_LSA
:
3556 new = ospf_summary_lsa_refresh (ospf
, lsa
);
3558 case OSPF_ASBR_SUMMARY_LSA
:
3559 new = ospf_summary_asbr_lsa_refresh (ospf
, lsa
);
3561 case OSPF_AS_EXTERNAL_LSA
:
3562 /* Translated from NSSA Type-5s are refreshed when
3563 * from refresh of Type-7 - do not refresh these directly.
3565 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3567 ei
= ospf_external_info_check (lsa
);
3569 new = ospf_external_lsa_refresh (ospf
, lsa
, ei
, LSA_REFRESH_FORCE
);
3571 ospf_lsa_flush_as (ospf
, lsa
);
3573 #ifdef HAVE_OPAQUE_LSA
3574 case OSPF_OPAQUE_LINK_LSA
:
3575 case OSPF_OPAQUE_AREA_LSA
:
3576 case OSPF_OPAQUE_AS_LSA
:
3577 new = ospf_opaque_lsa_refresh (lsa
);
3579 #endif /* HAVE_OPAQUE_LSA */
3587 ospf_refresher_register_lsa (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3589 u_int16_t index
, current_index
;
3591 assert (lsa
->lock
> 0);
3592 assert (CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF
));
3594 if (lsa
->refresh_list
< 0)
3598 if (LS_AGE (lsa
) == 0 &&
3599 ntohl (lsa
->data
->ls_seqnum
) == OSPF_INITIAL_SEQUENCE_NUMBER
)
3600 /* Randomize first update by OSPF_LS_REFRESH_SHIFT factor */
3601 delay
= OSPF_LS_REFRESH_SHIFT
+ (random () % OSPF_LS_REFRESH_TIME
);
3603 /* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */
3604 delay
= OSPF_LS_REFRESH_TIME
- LS_AGE (lsa
) - OSPF_LS_REFRESH_JITTER
3605 + (random () % (2*OSPF_LS_REFRESH_JITTER
));
3610 current_index
= ospf
->lsa_refresh_queue
.index
+ (quagga_time (NULL
)
3611 - ospf
->lsa_refresher_started
)/OSPF_LSA_REFRESHER_GRANULARITY
;
3613 index
= (current_index
+ delay
/OSPF_LSA_REFRESHER_GRANULARITY
)
3614 % (OSPF_LSA_REFRESHER_SLOTS
);
3616 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3617 zlog_debug ("LSA[Refresh]: lsa %s with age %d added to index %d",
3618 inet_ntoa (lsa
->data
->id
), LS_AGE (lsa
), index
);
3619 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3620 ospf
->lsa_refresh_queue
.qs
[index
] = list_new ();
3621 listnode_add (ospf
->lsa_refresh_queue
.qs
[index
],
3622 ospf_lsa_lock (lsa
)); /* lsa_refresh_queue */
3623 lsa
->refresh_list
= index
;
3624 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3625 zlog_debug ("LSA[Refresh:%s]: ospf_refresher_register_lsa(): "
3626 "setting refresh_list on lsa %p (slod %d)",
3627 inet_ntoa (lsa
->data
->id
), lsa
, index
);
3632 ospf_refresher_unregister_lsa (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3634 assert (lsa
->lock
> 0);
3635 assert (CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF
));
3636 if (lsa
->refresh_list
>= 0)
3638 struct list
*refresh_list
= ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3639 listnode_delete (refresh_list
, lsa
);
3640 if (!listcount (refresh_list
))
3642 list_free (refresh_list
);
3643 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3645 ospf_lsa_unlock (&lsa
); /* lsa_refresh_queue */
3646 lsa
->refresh_list
= -1;
3651 ospf_lsa_refresh_walker (struct thread
*t
)
3653 struct list
*refresh_list
;
3654 struct listnode
*node
, *nnode
;
3655 struct ospf
*ospf
= THREAD_ARG (t
);
3656 struct ospf_lsa
*lsa
;
3658 struct list
*lsa_to_refresh
= list_new ();
3660 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3661 zlog_debug ("LSA[Refresh]:ospf_lsa_refresh_walker(): start");
3664 i
= ospf
->lsa_refresh_queue
.index
;
3666 /* Note: if clock has jumped backwards, then time change could be negative,
3667 so we are careful to cast the expression to unsigned before taking
3669 ospf
->lsa_refresh_queue
.index
=
3670 ((unsigned long)(ospf
->lsa_refresh_queue
.index
+
3671 (quagga_time (NULL
) - ospf
->lsa_refresher_started
)
3672 / OSPF_LSA_REFRESHER_GRANULARITY
))
3673 % OSPF_LSA_REFRESHER_SLOTS
;
3675 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3676 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3677 ospf
->lsa_refresh_queue
.index
);
3679 for (;i
!= ospf
->lsa_refresh_queue
.index
;
3680 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
)
3682 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3683 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): "
3684 "refresh index %d", i
);
3686 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3690 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3694 for (ALL_LIST_ELEMENTS (refresh_list
, node
, nnode
, lsa
))
3696 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3697 zlog_debug ("LSA[Refresh:%s]: ospf_lsa_refresh_walker(): "
3698 "refresh lsa %p (slot %d)",
3699 inet_ntoa (lsa
->data
->id
), lsa
, i
);
3701 assert (lsa
->lock
> 0);
3702 list_delete_node (refresh_list
, node
);
3703 lsa
->refresh_list
= -1;
3704 listnode_add (lsa_to_refresh
, lsa
);
3706 list_free (refresh_list
);
3710 ospf
->t_lsa_refresher
= thread_add_timer (master
, ospf_lsa_refresh_walker
,
3711 ospf
, ospf
->lsa_refresh_interval
);
3712 ospf
->lsa_refresher_started
= quagga_time (NULL
);
3714 for (ALL_LIST_ELEMENTS (lsa_to_refresh
, node
, nnode
, lsa
))
3716 ospf_lsa_refresh (ospf
, lsa
);
3717 assert (lsa
->lock
> 0);
3718 ospf_lsa_unlock (&lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3721 list_delete (lsa_to_refresh
);
3723 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3724 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");