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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h" /* for inet_aton() */
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_lsdb.h"
43 #include "ospfd/ospf_neighbor.h"
44 #include "ospfd/ospf_nsm.h"
45 #include "ospfd/ospf_flood.h"
46 #include "ospfd/ospf_packet.h"
47 #include "ospfd/ospf_spf.h"
48 #include "ospfd/ospf_dump.h"
49 #include "ospfd/ospf_route.h"
50 #include "ospfd/ospf_ase.h"
51 #include "ospfd/ospf_zebra.h"
52 #include "ospfd/ospf_abr.h"
55 u_int32_t
get_metric(u_char
*metric
)
59 m
= (m
<< 8) + metric
[1];
60 m
= (m
<< 8) + metric
[2];
65 struct timeval
int2tv(int a
)
75 struct timeval
msec2tv(int a
)
79 ret
.tv_sec
= a
/ 1000;
80 ret
.tv_usec
= (a
% 1000) * 1000;
85 int ospf_lsa_refresh_delay(struct ospf_lsa
*lsa
)
90 if (monotime_since(&lsa
->tv_orig
, &delta
)
91 < OSPF_MIN_LS_INTERVAL
* 1000LL) {
92 struct timeval minv
= msec2tv(OSPF_MIN_LS_INTERVAL
);
93 timersub(&minv
, &delta
, &minv
);
95 /* TBD: remove padding to full sec, return timeval instead */
96 delay
= minv
.tv_sec
+ !!minv
.tv_usec
;
98 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
100 "LSA[Type%d:%s]: Refresh timer delay %d seconds",
101 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
111 int get_age(struct ospf_lsa
*lsa
)
115 monotime_since(&lsa
->tv_recv
, &rel
);
116 return ntohs(lsa
->data
->ls_age
) + rel
.tv_sec
;
120 /* Fletcher Checksum -- Refer to RFC1008. */
122 /* All the offsets are zero-based. The offsets in the RFC1008 are
124 u_int16_t
ospf_lsa_checksum(struct lsa_header
*lsa
)
126 u_char
*buffer
= (u_char
*)&lsa
->options
;
127 int options_offset
= buffer
- (u_char
*)&lsa
->ls_age
; /* should be 2 */
129 /* Skip the AGE field */
130 u_int16_t len
= ntohs(lsa
->length
) - options_offset
;
132 /* Checksum offset starts from "options" field, not the beginning of the
133 lsa_header struct. The offset is 14, rather than 16. */
134 int checksum_offset
= (u_char
*)&lsa
->checksum
- buffer
;
136 return fletcher_checksum(buffer
, len
, checksum_offset
);
139 int ospf_lsa_checksum_valid(struct lsa_header
*lsa
)
141 u_char
*buffer
= (u_char
*)&lsa
->options
;
142 int options_offset
= buffer
- (u_char
*)&lsa
->ls_age
; /* should be 2 */
144 /* Skip the AGE field */
145 u_int16_t len
= ntohs(lsa
->length
) - options_offset
;
147 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
152 /* Create OSPF LSA. */
153 struct ospf_lsa
*ospf_lsa_new()
155 struct ospf_lsa
*new;
157 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
161 new->retransmit_counter
= 0;
162 monotime(&new->tv_recv
);
163 new->tv_orig
= new->tv_recv
;
164 new->refresh_list
= -1;
165 new->vrf_id
= VRF_DEFAULT
;
170 /* Duplicate OSPF LSA. */
171 struct ospf_lsa
*ospf_lsa_dup(struct ospf_lsa
*lsa
)
173 struct ospf_lsa
*new;
178 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
180 memcpy(new, lsa
, sizeof(struct ospf_lsa
));
181 UNSET_FLAG(new->flags
, OSPF_LSA_DISCARD
);
183 new->retransmit_counter
= 0;
184 new->data
= ospf_lsa_data_dup(lsa
->data
);
186 /* kevinm: Clear the refresh_list, otherwise there are going
187 to be problems when we try to remove the LSA from the
188 queue (which it's not a member of.)
189 XXX: Should we add the LSA to the refresh_list queue? */
190 new->refresh_list
= -1;
192 if (IS_DEBUG_OSPF(lsa
, LSA
))
193 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa
,
200 void ospf_lsa_free(struct ospf_lsa
*lsa
)
202 assert(lsa
->lock
== 0);
204 if (IS_DEBUG_OSPF(lsa
, LSA
))
205 zlog_debug("LSA: freed %p", (void *)lsa
);
207 /* Delete LSA data. */
208 if (lsa
->data
!= NULL
)
209 ospf_lsa_data_free(lsa
->data
);
211 assert(lsa
->refresh_list
< 0);
213 memset(lsa
, 0, sizeof(struct ospf_lsa
));
214 XFREE(MTYPE_OSPF_LSA
, lsa
);
218 struct ospf_lsa
*ospf_lsa_lock(struct ospf_lsa
*lsa
)
225 void ospf_lsa_unlock(struct ospf_lsa
**lsa
)
227 /* This is sanity check. */
233 assert((*lsa
)->lock
>= 0);
235 if ((*lsa
)->lock
== 0) {
236 assert(CHECK_FLAG((*lsa
)->flags
, OSPF_LSA_DISCARD
));
242 /* Check discard flag. */
243 void ospf_lsa_discard(struct ospf_lsa
*lsa
)
245 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
246 SET_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
);
247 ospf_lsa_unlock(&lsa
);
251 /* Create LSA data. */
252 struct lsa_header
*ospf_lsa_data_new(size_t size
)
254 return XCALLOC(MTYPE_OSPF_LSA_DATA
, size
);
257 /* Duplicate LSA data. */
258 struct lsa_header
*ospf_lsa_data_dup(struct lsa_header
*lsah
)
260 struct lsa_header
*new;
262 new = ospf_lsa_data_new(ntohs(lsah
->length
));
263 memcpy(new, lsah
, ntohs(lsah
->length
));
269 void ospf_lsa_data_free(struct lsa_header
*lsah
)
271 if (IS_DEBUG_OSPF(lsa
, LSA
))
272 zlog_debug("LSA[Type%d:%s]: data freed %p", lsah
->type
,
273 inet_ntoa(lsah
->id
), (void *)lsah
);
275 XFREE(MTYPE_OSPF_LSA_DATA
, lsah
);
279 /* LSA general functions. */
281 const char *dump_lsa_key(struct ospf_lsa
*lsa
)
283 static char buf
[] = {"Type255,id(255.255.255.255),ar(255.255.255.255)"};
284 struct lsa_header
*lsah
;
286 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
) {
287 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
288 strlcpy(id
, inet_ntoa(lsah
->id
), sizeof(id
));
289 strlcpy(ar
, inet_ntoa(lsah
->adv_router
), sizeof(ar
));
291 sprintf(buf
, "Type%d,id(%s),ar(%s)", lsah
->type
, id
, ar
);
293 strlcpy(buf
, "NULL", sizeof(buf
));
298 u_int32_t
lsa_seqnum_increment(struct ospf_lsa
*lsa
)
302 seqnum
= ntohl(lsa
->data
->ls_seqnum
) + 1;
304 return htonl(seqnum
);
307 void lsa_header_set(struct stream
*s
, u_char options
, u_char type
,
308 struct in_addr id
, struct in_addr router_id
)
310 struct lsa_header
*lsah
;
312 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
314 lsah
->ls_age
= htons(OSPF_LSA_INITIAL_AGE
);
315 lsah
->options
= options
;
318 lsah
->adv_router
= router_id
;
319 lsah
->ls_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
321 stream_forward_endp(s
, OSPF_LSA_HEADER_SIZE
);
325 /* router-LSA related functions. */
326 /* Get router-LSA flags. */
327 static u_char
router_lsa_flags(struct ospf_area
*area
)
331 flags
= area
->ospf
->flags
;
333 /* Set virtual link flag. */
334 if (ospf_full_virtual_nbrs(area
))
335 SET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
337 /* Just sanity check */
338 UNSET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
340 /* Set Shortcut ABR behabiour flag. */
341 UNSET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
342 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
343 if (!OSPF_IS_AREA_BACKBONE(area
))
344 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
345 && area
->ospf
->backbone
== NULL
)
346 || area
->shortcut_configured
347 == OSPF_SHORTCUT_ENABLE
)
348 SET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
350 /* ASBR can't exit in stub area. */
351 if (area
->external_routing
== OSPF_AREA_STUB
)
352 UNSET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
353 /* If ASBR set External flag */
354 else if (IS_OSPF_ASBR(area
->ospf
))
355 SET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
357 /* Set ABR dependent flags */
358 if (IS_OSPF_ABR(area
->ospf
)) {
359 SET_FLAG(flags
, ROUTER_LSA_BORDER
);
360 /* If Area is NSSA and we are both ABR and unconditional
362 * set Nt bit to inform other routers.
364 if ((area
->external_routing
== OSPF_AREA_NSSA
)
365 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
366 SET_FLAG(flags
, ROUTER_LSA_NT
);
371 /* Lookup neighbor other than myself.
372 And check neighbor count,
373 Point-to-Point link must have only 1 neighbor. */
374 struct ospf_neighbor
*ospf_nbr_lookup_ptop(struct ospf_interface
*oi
)
376 struct ospf_neighbor
*nbr
= NULL
;
377 struct route_node
*rn
;
379 /* Search neighbor, there must be one of two nbrs. */
380 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
381 if ((nbr
= rn
->info
))
382 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
383 &oi
->ospf
->router_id
))
384 if (nbr
->state
== NSM_Full
) {
385 route_unlock_node(rn
);
389 /* PtoP link must have only 1 neighbor. */
390 if (ospf_nbr_count(oi
, 0) > 1)
391 zlog_warn("Point-to-Point link has more than 1 neighobrs.");
396 /* Determine cost of link, taking RFC3137 stub-router support into
399 static u_int16_t
ospf_link_cost(struct ospf_interface
*oi
)
401 /* RFC3137 stub router support */
402 if (!CHECK_FLAG(oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
403 return oi
->output_cost
;
405 return OSPF_OUTPUT_COST_INFINITE
;
408 /* Set a link information. */
409 static char link_info_set(struct stream
*s
, struct in_addr id
,
410 struct in_addr data
, u_char type
, u_char tos
,
413 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
414 * vast majority of cases. Some rare routers with lots of links need
416 * we try accomodate those here.
418 if (STREAM_WRITEABLE(s
) < OSPF_ROUTER_LSA_LINK_SIZE
) {
419 size_t ret
= OSPF_MAX_LSA_SIZE
;
421 /* Can we enlarge the stream still? */
422 if (STREAM_SIZE(s
) == OSPF_MAX_LSA_SIZE
) {
423 /* we futz the size here for simplicity, really we need
426 * IP Header - (sizeof (struct ip))
427 * OSPF Header - OSPF_HEADER_SIZE
428 * LSA Header - OSPF_LSA_HEADER_SIZE
429 * MD5 auth data, if MD5 is configured -
430 * OSPF_AUTH_MD5_SIZE.
432 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
435 s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
438 if (ret
== OSPF_MAX_LSA_SIZE
) {
440 "%s: Out of space in LSA stream, left %zd, size %zd",
441 __func__
, STREAM_WRITEABLE(s
), STREAM_SIZE(s
));
446 /* TOS based routing is not supported. */
447 stream_put_ipv4(s
, id
.s_addr
); /* Link ID. */
448 stream_put_ipv4(s
, data
.s_addr
); /* Link Data. */
449 stream_putc(s
, type
); /* Link Type. */
450 stream_putc(s
, tos
); /* TOS = 0. */
451 stream_putw(s
, cost
); /* Link Cost. */
456 /* Describe Point-to-Point link (Section 12.4.1.1). */
457 static int lsa_link_ptop_set(struct stream
*s
, struct ospf_interface
*oi
)
460 struct ospf_neighbor
*nbr
;
461 struct in_addr id
, mask
, data
;
462 u_int16_t cost
= ospf_link_cost(oi
);
464 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
465 zlog_debug("LSA[Type1]: Set link Point-to-Point");
467 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
468 if (nbr
->state
== NSM_Full
) {
469 if (CHECK_FLAG(oi
->connected
->flags
,
470 ZEBRA_IFA_UNNUMBERED
)) {
471 /* For unnumbered point-to-point networks, the
473 should specify the interface's MIB-II ifIndex
475 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
476 links
+= link_info_set(
477 s
, nbr
->router_id
, data
,
478 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
480 links
+= link_info_set(
482 oi
->address
->u
.prefix4
,
483 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
487 /* no need for a stub link for unnumbered interfaces */
488 if (!CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)) {
489 /* Regardless of the state of the neighboring router, we must
490 add a Type 3 link (stub network).
491 N.B. Options 1 & 2 share basically the same logic. */
492 masklen2ip(oi
->address
->prefixlen
, &mask
);
493 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
495 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
502 /* Describe Broadcast Link. */
503 static int lsa_link_broadcast_set(struct stream
*s
, struct ospf_interface
*oi
)
505 struct ospf_neighbor
*dr
;
506 struct in_addr id
, mask
;
507 u_int16_t cost
= ospf_link_cost(oi
);
509 /* Describe Type 3 Link. */
510 if (oi
->state
== ISM_Waiting
) {
511 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
513 "LSA[Type1]: Interface %s is in state Waiting. "
514 "Adding stub interface",
516 masklen2ip(oi
->address
->prefixlen
, &mask
);
517 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
518 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
522 dr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &DR(oi
));
523 /* Describe Type 2 link. */
524 if (dr
&& (dr
->state
== NSM_Full
525 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
)))
526 && ospf_nbr_count(oi
, NSM_Full
) > 0) {
527 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
529 "LSA[Type1]: Interface %s has a DR. "
530 "Adding transit interface",
532 return link_info_set(s
, DR(oi
), oi
->address
->u
.prefix4
,
533 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
535 /* Describe type 3 link. */
537 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
539 "LSA[Type1]: Interface %s has no DR. "
540 "Adding stub interface",
542 masklen2ip(oi
->address
->prefixlen
, &mask
);
543 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
544 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
549 static int lsa_link_loopback_set(struct stream
*s
, struct ospf_interface
*oi
)
551 struct in_addr id
, mask
;
553 /* Describe Type 3 Link. */
554 if (oi
->state
!= ISM_Loopback
)
557 mask
.s_addr
= 0xffffffff;
558 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
559 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
562 /* Describe Virtual Link. */
563 static int lsa_link_virtuallink_set(struct stream
*s
, struct ospf_interface
*oi
)
565 struct ospf_neighbor
*nbr
;
566 u_int16_t cost
= ospf_link_cost(oi
);
568 if (oi
->state
== ISM_PointToPoint
)
569 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
570 if (nbr
->state
== NSM_Full
) {
571 return link_info_set(s
, nbr
->router_id
,
572 oi
->address
->u
.prefix4
,
573 LSA_LINK_TYPE_VIRTUALLINK
,
580 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
582 /* this function add for support point-to-multipoint ,see rfc2328
584 /* from "edward rrr" <edward_rrr@hotmail.com>
585 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
586 static int lsa_link_ptomp_set(struct stream
*s
, struct ospf_interface
*oi
)
589 struct route_node
*rn
;
590 struct ospf_neighbor
*nbr
= NULL
;
591 struct in_addr id
, mask
;
592 u_int16_t cost
= ospf_link_cost(oi
);
594 mask
.s_addr
= 0xffffffff;
595 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
596 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
598 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
599 zlog_debug("PointToMultipoint: running ptomultip_set");
601 /* Search neighbor, */
602 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
603 if ((nbr
= rn
->info
) != NULL
)
605 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
606 &oi
->ospf
->router_id
))
607 if (nbr
->state
== NSM_Full
)
610 links
+= link_info_set(
612 oi
->address
->u
.prefix4
,
613 LSA_LINK_TYPE_POINTOPOINT
, 0,
615 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
617 "PointToMultipoint: set link to %s",
626 /* Set router-LSA link information. */
627 static int router_lsa_link_set(struct stream
*s
, struct ospf_area
*area
)
629 struct listnode
*node
;
630 struct ospf_interface
*oi
;
633 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
634 struct interface
*ifp
= oi
->ifp
;
636 /* Check interface is up, OSPF is enable. */
637 if (if_is_operative(ifp
)) {
638 if (oi
->state
!= ISM_Down
) {
639 oi
->lsa_pos_beg
= links
;
640 /* Describe each link. */
642 case OSPF_IFTYPE_POINTOPOINT
:
643 links
+= lsa_link_ptop_set(s
, oi
);
645 case OSPF_IFTYPE_BROADCAST
:
646 links
+= lsa_link_broadcast_set(s
, oi
);
648 case OSPF_IFTYPE_NBMA
:
649 links
+= lsa_link_nbma_set(s
, oi
);
651 case OSPF_IFTYPE_POINTOMULTIPOINT
:
652 links
+= lsa_link_ptomp_set(s
, oi
);
654 case OSPF_IFTYPE_VIRTUALLINK
:
656 lsa_link_virtuallink_set(s
, oi
);
658 case OSPF_IFTYPE_LOOPBACK
:
659 links
+= lsa_link_loopback_set(s
, oi
);
661 oi
->lsa_pos_end
= links
;
669 /* Set router-LSA body. */
670 static void ospf_router_lsa_body_set(struct stream
*s
, struct ospf_area
*area
)
676 stream_putc(s
, router_lsa_flags(area
));
678 /* Set Zero fields. */
681 /* Keep pointer to # links. */
682 putp
= stream_get_endp(s
);
687 /* Set all link information. */
688 cnt
= router_lsa_link_set(s
, area
);
690 /* Set # of links here. */
691 stream_putw_at(s
, putp
, cnt
);
694 static int ospf_stub_router_timer(struct thread
*t
)
696 struct ospf_area
*area
= THREAD_ARG(t
);
698 area
->t_stub_router
= NULL
;
700 SET_FLAG(area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
702 /* clear stub route state and generate router-lsa refresh, don't
703 * clobber an administratively set stub-router state though.
705 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
708 UNSET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
710 ospf_router_lsa_update_area(area
);
715 static void ospf_stub_router_check(struct ospf_area
*area
)
717 /* area must either be administratively configured to be stub
718 * or startup-time stub-router must be configured and we must in a
722 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
)) {
723 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
727 /* not admin-stubbed, check whether startup stubbing is configured and
728 * whether it's not been done yet
730 if (CHECK_FLAG(area
->stub_router_state
,
731 OSPF_AREA_WAS_START_STUB_ROUTED
))
734 if (area
->ospf
->stub_router_startup_time
735 == OSPF_STUB_ROUTER_UNCONFIGURED
) {
736 /* stub-router is hence done forever for this area, even if
738 * tries configure it (take effect next restart).
740 SET_FLAG(area
->stub_router_state
,
741 OSPF_AREA_WAS_START_STUB_ROUTED
);
745 /* startup stub-router configured and not yet done */
746 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
748 OSPF_AREA_TIMER_ON(area
->t_stub_router
, ospf_stub_router_timer
,
749 area
->ospf
->stub_router_startup_time
);
752 /* Create new router-LSA. */
753 static struct ospf_lsa
*ospf_router_lsa_new(struct ospf_area
*area
)
755 struct ospf
*ospf
= area
->ospf
;
757 struct lsa_header
*lsah
;
758 struct ospf_lsa
*new;
761 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
762 zlog_debug("LSA[Type1]: Create router-LSA instance");
764 /* check whether stub-router is desired, and if this is the first
767 ospf_stub_router_check(area
);
769 /* Create a stream for LSA. */
770 s
= stream_new(OSPF_MAX_LSA_SIZE
);
771 /* Set LSA common header fields. */
772 lsa_header_set(s
, LSA_OPTIONS_GET(area
) | LSA_OPTIONS_NSSA_GET(area
),
773 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
775 /* Set router-LSA body fields. */
776 ospf_router_lsa_body_set(s
, area
);
779 length
= stream_get_endp(s
);
780 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
781 lsah
->length
= htons(length
);
783 /* Now, create OSPF LSA instance. */
784 if ((new = ospf_lsa_new()) == NULL
) {
785 zlog_err("%s: Unable to create new lsa", __func__
);
790 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
791 new->vrf_id
= area
->ospf
->vrf_id
;
793 /* Copy LSA data to store, discard stream. */
794 new->data
= ospf_lsa_data_new(length
);
795 memcpy(new->data
, lsah
, length
);
801 /* Originate Router-LSA. */
802 static struct ospf_lsa
*ospf_router_lsa_originate(struct ospf_area
*area
)
804 struct ospf_lsa
*new;
806 /* Create new router-LSA instance. */
807 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
808 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
813 if (new->data
->adv_router
.s_addr
== 0) {
814 if (IS_DEBUG_OSPF_EVENT
)
815 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
816 ospf_lsa_discard(new);
820 /* Install LSA to LSDB. */
821 new = ospf_lsa_install(area
->ospf
, NULL
, new);
823 /* Update LSA origination count. */
824 area
->ospf
->lsa_originate_count
++;
826 /* Flooding new LSA through area. */
827 ospf_flood_through_area(area
, NULL
, new);
829 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
830 zlog_debug("LSA[Type%d:%s]: Originate router-LSA %p",
831 new->data
->type
, inet_ntoa(new->data
->id
),
833 ospf_lsa_header_dump(new->data
);
839 /* Refresh router-LSA. */
840 static struct ospf_lsa
*ospf_router_lsa_refresh(struct ospf_lsa
*lsa
)
842 struct ospf_area
*area
= lsa
->area
;
843 struct ospf_lsa
*new;
848 /* Delete LSA from neighbor retransmit-list. */
849 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
851 /* Unregister LSA from refresh-list */
852 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
854 /* Create new router-LSA instance. */
855 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
856 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
860 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
862 ospf_lsa_install(area
->ospf
, NULL
, new);
864 /* Flood LSA through area. */
865 ospf_flood_through_area(area
, NULL
, new);
868 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
869 zlog_debug("LSA[Type%d:%s]: router-LSA refresh",
870 new->data
->type
, inet_ntoa(new->data
->id
));
871 ospf_lsa_header_dump(new->data
);
877 int ospf_router_lsa_update_area(struct ospf_area
*area
)
879 if (IS_DEBUG_OSPF_EVENT
)
880 zlog_debug("[router-LSA]: (router-LSA area update)");
882 /* Now refresh router-LSA. */
883 if (area
->router_lsa_self
)
884 ospf_lsa_refresh(area
->ospf
, area
->router_lsa_self
);
885 /* Newly originate router-LSA. */
887 ospf_router_lsa_originate(area
);
892 int ospf_router_lsa_update(struct ospf
*ospf
)
894 struct listnode
*node
, *nnode
;
895 struct ospf_area
*area
;
897 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
898 zlog_debug("Timer[router-LSA Update]: (timer expire)");
900 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
901 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
902 struct router_lsa
*rl
;
903 const char *area_str
;
905 /* Keep Area ID string. */
906 area_str
= AREA_NAME(area
);
908 /* If LSA not exist in this Area, originate new. */
910 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
912 "LSA[Type1]: Create router-LSA for Area %s",
915 ospf_router_lsa_originate(area
);
917 /* If router-ID is changed, Link ID must change.
918 First flush old LSA, then originate new. */
919 else if (!IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
)) {
920 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
922 "LSA[Type%d:%s]: Refresh router-LSA for Area %s",
924 inet_ntoa(lsa
->data
->id
), area_str
);
925 ospf_refresher_unregister_lsa(ospf
, lsa
);
926 ospf_lsa_flush_area(lsa
, area
);
927 ospf_lsa_unlock(&area
->router_lsa_self
);
928 area
->router_lsa_self
= NULL
;
930 /* Refresh router-LSA, (not install) and flood through
932 ospf_router_lsa_update_area(area
);
934 rl
= (struct router_lsa
*)lsa
->data
;
935 /* Refresh router-LSA, (not install) and flood through
937 if (rl
->flags
!= ospf
->flags
)
938 ospf_router_lsa_update_area(area
);
946 /* network-LSA related functions. */
947 /* Originate Network-LSA. */
948 static void ospf_network_lsa_body_set(struct stream
*s
,
949 struct ospf_interface
*oi
)
952 struct route_node
*rn
;
953 struct ospf_neighbor
*nbr
;
955 masklen2ip(oi
->address
->prefixlen
, &mask
);
956 stream_put_ipv4(s
, mask
.s_addr
);
958 /* The network-LSA lists those routers that are fully adjacent to
959 the Designated Router; each fully adjacent router is identified by
960 its OSPF Router ID. The Designated Router includes itself in this
961 list. RFC2328, Section 12.4.2 */
963 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
964 if ((nbr
= rn
->info
) != NULL
)
965 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
966 stream_put_ipv4(s
, nbr
->router_id
.s_addr
);
969 static struct ospf_lsa
*ospf_network_lsa_new(struct ospf_interface
*oi
)
972 struct ospf_lsa
*new;
973 struct lsa_header
*lsah
;
974 struct ospf_if_params
*oip
;
977 /* If there are no neighbours on this network (the net is stub),
978 the router does not originate network-LSA (see RFC 12.4.2) */
979 if (oi
->full_nbrs
== 0)
982 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
983 zlog_debug("LSA[Type2]: Create network-LSA instance");
985 /* Create new stream for LSA. */
986 s
= stream_new(OSPF_MAX_LSA_SIZE
);
987 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
989 lsa_header_set(s
, (OPTIONS(oi
) | LSA_OPTIONS_GET(oi
->area
)),
990 OSPF_NETWORK_LSA
, DR(oi
), oi
->ospf
->router_id
);
992 /* Set network-LSA body fields. */
993 ospf_network_lsa_body_set(s
, oi
);
996 length
= stream_get_endp(s
);
997 lsah
->length
= htons(length
);
999 /* Create OSPF LSA instance. */
1000 if ((new = ospf_lsa_new()) == NULL
) {
1001 zlog_err("%s: ospf_lsa_new returned NULL", __func__
);
1005 new->area
= oi
->area
;
1006 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1007 new->vrf_id
= oi
->ospf
->vrf_id
;
1009 /* Copy LSA to store. */
1010 new->data
= ospf_lsa_data_new(length
);
1011 memcpy(new->data
, lsah
, length
);
1014 /* Remember prior network LSA sequence numbers, even if we stop
1015 * originating one for this oi, to try avoid re-originating LSAs with a
1016 * prior sequence number, and thus speed up adjency forming &
1019 if ((oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
))) {
1020 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1021 new->data
->ls_seqnum
= lsa_seqnum_increment(new);
1023 oip
= ospf_get_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1024 ospf_if_update_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1026 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1031 /* Originate network-LSA. */
1032 void ospf_network_lsa_update(struct ospf_interface
*oi
)
1034 struct ospf_lsa
*new;
1036 if (oi
->network_lsa_self
!= NULL
) {
1037 ospf_lsa_refresh(oi
->ospf
, oi
->network_lsa_self
);
1041 /* Create new network-LSA instance. */
1042 new = ospf_network_lsa_new(oi
);
1046 /* Install LSA to LSDB. */
1047 new = ospf_lsa_install(oi
->ospf
, oi
, new);
1049 /* Update LSA origination count. */
1050 oi
->ospf
->lsa_originate_count
++;
1052 /* Flooding new LSA through area. */
1053 ospf_flood_through_area(oi
->area
, NULL
, new);
1055 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1056 zlog_debug("LSA[Type%d:%s]: Originate network-LSA %p",
1057 new->data
->type
, inet_ntoa(new->data
->id
),
1059 ospf_lsa_header_dump(new->data
);
1065 static struct ospf_lsa
*ospf_network_lsa_refresh(struct ospf_lsa
*lsa
)
1067 struct ospf_area
*area
= lsa
->area
;
1068 struct ospf_lsa
*new, *new2
;
1069 struct ospf_if_params
*oip
;
1070 struct ospf_interface
*oi
;
1074 /* Retrieve the oi for the network LSA */
1075 oi
= ospf_if_lookup_by_local_addr(area
->ospf
, NULL
, lsa
->data
->id
);
1077 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1079 "LSA[Type%d:%s]: network-LSA refresh: "
1080 "no oi found, ick, ignoring.",
1081 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
1082 ospf_lsa_header_dump(lsa
->data
);
1086 /* Delete LSA from neighbor retransmit-list. */
1087 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
1089 /* Unregister LSA from refresh-list */
1090 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
1092 /* Create new network-LSA instance. */
1093 new = ospf_network_lsa_new(oi
);
1097 oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1098 assert(oip
!= NULL
);
1099 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
=
1100 lsa_seqnum_increment(lsa
);
1102 new2
= ospf_lsa_install(area
->ospf
, oi
, new);
1104 assert(new2
== new);
1106 /* Flood LSA through aera. */
1107 ospf_flood_through_area(area
, NULL
, new);
1109 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1110 zlog_debug("LSA[Type%d:%s]: network-LSA refresh",
1111 new->data
->type
, inet_ntoa(new->data
->id
));
1112 ospf_lsa_header_dump(new->data
);
1118 static void stream_put_ospf_metric(struct stream
*s
, u_int32_t metric_value
)
1123 /* Put 0 metric. TOS metric is not supported. */
1124 metric
= htonl(metric_value
);
1125 mp
= (char *)&metric
;
1127 stream_put(s
, mp
, 3);
1130 /* summary-LSA related functions. */
1131 static void ospf_summary_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1134 struct in_addr mask
;
1136 masklen2ip(p
->prefixlen
, &mask
);
1138 /* Put Network Mask. */
1139 stream_put_ipv4(s
, mask
.s_addr
);
1142 stream_putc(s
, (u_char
)0);
1145 stream_put_ospf_metric(s
, metric
);
1148 static struct ospf_lsa
*ospf_summary_lsa_new(struct ospf_area
*area
,
1149 struct prefix
*p
, u_int32_t metric
,
1153 struct ospf_lsa
*new;
1154 struct lsa_header
*lsah
;
1157 if (id
.s_addr
== 0xffffffff) {
1158 /* Maybe Link State ID not available. */
1159 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1161 "LSA[Type%d]: Link ID not available, can't originate",
1166 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1167 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1169 /* Create new stream for LSA. */
1170 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1171 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1173 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_SUMMARY_LSA
, id
,
1174 area
->ospf
->router_id
);
1176 /* Set summary-LSA body fields. */
1177 ospf_summary_lsa_body_set(s
, p
, metric
);
1180 length
= stream_get_endp(s
);
1181 lsah
->length
= htons(length
);
1183 /* Create OSPF LSA instance. */
1184 new = ospf_lsa_new();
1186 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1187 new->vrf_id
= area
->ospf
->vrf_id
;
1189 /* Copy LSA to store. */
1190 new->data
= ospf_lsa_data_new(length
);
1191 memcpy(new->data
, lsah
, length
);
1197 /* Originate Summary-LSA. */
1198 struct ospf_lsa
*ospf_summary_lsa_originate(struct prefix_ipv4
*p
,
1200 struct ospf_area
*area
)
1202 struct ospf_lsa
*new;
1205 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
);
1207 if (id
.s_addr
== 0xffffffff) {
1208 /* Maybe Link State ID not available. */
1209 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1211 "LSA[Type%d]: Link ID not available, can't originate",
1216 /* Create new summary-LSA instance. */
1217 if (!(new = ospf_summary_lsa_new(area
, (struct prefix
*)p
, metric
, id
)))
1220 /* Instlal LSA to LSDB. */
1221 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1223 /* Update LSA origination count. */
1224 area
->ospf
->lsa_originate_count
++;
1226 /* Flooding new LSA through area. */
1227 ospf_flood_through_area(area
, NULL
, new);
1229 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1230 zlog_debug("LSA[Type%d:%s]: Originate summary-LSA %p",
1231 new->data
->type
, inet_ntoa(new->data
->id
),
1233 ospf_lsa_header_dump(new->data
);
1239 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
1240 struct ospf_lsa
*lsa
)
1242 struct ospf_lsa
*new;
1243 struct summary_lsa
*sl
;
1249 sl
= (struct summary_lsa
*)lsa
->data
;
1250 p
.prefixlen
= ip_masklen(sl
->mask
);
1251 new = ospf_summary_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1257 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1259 ospf_lsa_install(ospf
, NULL
, new);
1261 /* Flood LSA through AS. */
1262 ospf_flood_through_area(new->area
, NULL
, new);
1264 /* Debug logging. */
1265 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1266 zlog_debug("LSA[Type%d:%s]: summary-LSA refresh",
1267 new->data
->type
, inet_ntoa(new->data
->id
));
1268 ospf_lsa_header_dump(new->data
);
1275 /* summary-ASBR-LSA related functions. */
1276 static void ospf_summary_asbr_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1279 /* Put Network Mask. */
1280 stream_put_ipv4(s
, (u_int32_t
)0);
1283 stream_putc(s
, (u_char
)0);
1286 stream_put_ospf_metric(s
, metric
);
1289 static struct ospf_lsa
*ospf_summary_asbr_lsa_new(struct ospf_area
*area
,
1295 struct ospf_lsa
*new;
1296 struct lsa_header
*lsah
;
1299 if (id
.s_addr
== 0xffffffff) {
1300 /* Maybe Link State ID not available. */
1301 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1303 "LSA[Type%d]: Link ID not available, can't originate",
1304 OSPF_ASBR_SUMMARY_LSA
);
1308 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1309 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1311 /* Create new stream for LSA. */
1312 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1313 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1315 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_ASBR_SUMMARY_LSA
, id
,
1316 area
->ospf
->router_id
);
1318 /* Set summary-LSA body fields. */
1319 ospf_summary_asbr_lsa_body_set(s
, p
, metric
);
1322 length
= stream_get_endp(s
);
1323 lsah
->length
= htons(length
);
1325 /* Create OSPF LSA instance. */
1326 new = ospf_lsa_new();
1328 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1329 new->vrf_id
= area
->ospf
->vrf_id
;
1331 /* Copy LSA to store. */
1332 new->data
= ospf_lsa_data_new(length
);
1333 memcpy(new->data
, lsah
, length
);
1339 /* Originate summary-ASBR-LSA. */
1340 struct ospf_lsa
*ospf_summary_asbr_lsa_originate(struct prefix_ipv4
*p
,
1342 struct ospf_area
*area
)
1344 struct ospf_lsa
*new;
1347 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_ASBR_SUMMARY_LSA
,
1350 if (id
.s_addr
== 0xffffffff) {
1351 /* Maybe Link State ID not available. */
1352 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1354 "LSA[Type%d]: Link ID not available, can't originate",
1355 OSPF_ASBR_SUMMARY_LSA
);
1359 /* Create new summary-LSA instance. */
1360 new = ospf_summary_asbr_lsa_new(area
, (struct prefix
*)p
, metric
, id
);
1364 /* Install LSA to LSDB. */
1365 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1367 /* Update LSA origination count. */
1368 area
->ospf
->lsa_originate_count
++;
1370 /* Flooding new LSA through area. */
1371 ospf_flood_through_area(area
, NULL
, new);
1373 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1374 zlog_debug("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1375 new->data
->type
, inet_ntoa(new->data
->id
),
1377 ospf_lsa_header_dump(new->data
);
1383 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
1384 struct ospf_lsa
*lsa
)
1386 struct ospf_lsa
*new;
1387 struct summary_lsa
*sl
;
1393 sl
= (struct summary_lsa
*)lsa
->data
;
1394 p
.prefixlen
= ip_masklen(sl
->mask
);
1395 new = ospf_summary_asbr_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1400 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1402 ospf_lsa_install(ospf
, NULL
, new);
1404 /* Flood LSA through area. */
1405 ospf_flood_through_area(new->area
, NULL
, new);
1407 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1408 zlog_debug("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1409 new->data
->type
, inet_ntoa(new->data
->id
));
1410 ospf_lsa_header_dump(new->data
);
1416 /* AS-external-LSA related functions. */
1418 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1419 is connected, else 0*/
1420 static struct in_addr
ospf_external_lsa_nexthop_get(struct ospf
*ospf
,
1421 struct in_addr nexthop
)
1425 struct listnode
*node
;
1426 struct ospf_interface
*oi
;
1430 if (!nexthop
.s_addr
)
1433 /* Check whether nexthop is covered by OSPF network. */
1434 nh
.family
= AF_INET
;
1435 nh
.u
.prefix4
= nexthop
;
1436 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1438 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1439 * better to make use of the per-ifp table of ois.
1441 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1442 if (if_is_operative(oi
->ifp
))
1443 if (oi
->address
->family
== AF_INET
)
1444 if (prefix_match(oi
->address
, &nh
))
1450 /* NSSA-external-LSA related functions. */
1452 /* Get 1st IP connection for Forward Addr */
1454 struct in_addr
ospf_get_ip_from_ifp(struct ospf_interface
*oi
)
1460 if (if_is_operative(oi
->ifp
))
1461 return oi
->address
->u
.prefix4
;
1466 /* Get 1st IP connection for Forward Addr */
1467 struct in_addr
ospf_get_nssa_ip(struct ospf_area
*area
)
1470 struct in_addr best_default
;
1471 struct listnode
*node
;
1472 struct ospf_interface
*oi
;
1475 best_default
.s_addr
= 0;
1477 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->oiflist
, node
, oi
)) {
1478 if (if_is_operative(oi
->ifp
))
1479 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1481 && oi
->address
->family
== AF_INET
) {
1482 if (best_default
.s_addr
== 0)
1484 oi
->address
->u
.prefix4
;
1485 if (oi
->area
== area
)
1486 return oi
->address
->u
.prefix4
;
1489 if (best_default
.s_addr
!= 0)
1490 return best_default
;
1492 if (best_default
.s_addr
!= 0)
1493 return best_default
;
1498 #define DEFAULT_DEFAULT_METRIC 20
1499 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1500 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1502 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1504 int metric_type(struct ospf
*ospf
, u_char src
, u_short instance
)
1506 struct ospf_redist
*red
;
1508 red
= ospf_redist_lookup(ospf
, src
, instance
);
1510 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
1511 : red
->dmetric
.type
);
1514 int metric_value(struct ospf
*ospf
, u_char src
, u_short instance
)
1516 struct ospf_redist
*red
;
1518 red
= ospf_redist_lookup(ospf
, src
, instance
);
1519 if (!red
|| red
->dmetric
.value
< 0) {
1520 if (src
== DEFAULT_ROUTE
) {
1521 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1522 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1524 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1525 } else if (ospf
->default_metric
< 0)
1526 return DEFAULT_DEFAULT_METRIC
;
1528 return ospf
->default_metric
;
1531 return red
->dmetric
.value
;
1534 /* Set AS-external-LSA body. */
1535 static void ospf_external_lsa_body_set(struct stream
*s
,
1536 struct external_info
*ei
,
1539 struct prefix_ipv4
*p
= &ei
->p
;
1540 struct in_addr mask
, fwd_addr
;
1546 /* Put Network Mask. */
1547 masklen2ip(p
->prefixlen
, &mask
);
1548 stream_put_ipv4(s
, mask
.s_addr
);
1550 /* If prefix is default, specify DEFAULT_ROUTE. */
1551 type
= is_prefix_default(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1552 instance
= is_prefix_default(&ei
->p
) ? 0 : ei
->instance
;
1554 mtype
= (ROUTEMAP_METRIC_TYPE(ei
) != -1)
1555 ? ROUTEMAP_METRIC_TYPE(ei
)
1556 : metric_type(ospf
, type
, instance
);
1558 mvalue
= (ROUTEMAP_METRIC(ei
) != -1)
1559 ? ROUTEMAP_METRIC(ei
)
1560 : metric_value(ospf
, type
, instance
);
1562 /* Put type of external metric. */
1563 stream_putc(s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1565 /* Put 0 metric. TOS metric is not supported. */
1566 stream_put_ospf_metric(s
, mvalue
);
1568 /* Get forwarding address to nexthop if on the Connection List, else 0.
1570 fwd_addr
= ospf_external_lsa_nexthop_get(ospf
, ei
->nexthop
);
1572 /* Put forwarding address. */
1573 stream_put_ipv4(s
, fwd_addr
.s_addr
);
1576 stream_putl(s
, ei
->tag
);
1579 /* Create new external-LSA. */
1580 static struct ospf_lsa
*ospf_external_lsa_new(struct ospf
*ospf
,
1581 struct external_info
*ei
,
1582 struct in_addr
*old_id
)
1585 struct lsa_header
*lsah
;
1586 struct ospf_lsa
*new;
1591 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1593 "LSA[Type5]: External info is NULL, can't originate");
1597 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1598 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1600 /* If old Link State ID is specified, refresh LSA with same ID. */
1603 /* Get Link State with unique ID. */
1605 id
= ospf_lsa_unique_id(ospf
, ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
,
1607 if (id
.s_addr
== 0xffffffff) {
1608 /* Maybe Link State ID not available. */
1609 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1611 "LSA[Type5]: Link ID not available, can't originate");
1616 /* Create new stream for LSA. */
1617 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1618 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1620 /* Set LSA common header fields. */
1621 lsa_header_set(s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
, id
,
1624 /* Set AS-external-LSA body fields. */
1625 ospf_external_lsa_body_set(s
, ei
, ospf
);
1628 length
= stream_get_endp(s
);
1629 lsah
->length
= htons(length
);
1631 /* Now, create OSPF LSA instance. */
1632 new = ospf_lsa_new();
1634 SET_FLAG(new->flags
,
1635 OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1636 new->vrf_id
= ospf
->vrf_id
;
1638 /* Copy LSA data to store, discard stream. */
1639 new->data
= ospf_lsa_data_new(length
);
1640 memcpy(new->data
, lsah
, length
);
1647 static void ospf_install_flood_nssa(struct ospf
*ospf
, struct ospf_lsa
*lsa
,
1648 struct external_info
*ei
)
1650 struct ospf_lsa
*new;
1651 struct as_external_lsa
*extlsa
;
1652 struct ospf_area
*area
;
1653 struct listnode
*node
, *nnode
;
1655 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1656 * which originated from an NSSA area. In which case it should not be
1657 * flooded back to NSSA areas.
1659 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1662 /* NSSA Originate or Refresh (If anyNSSA)
1664 LSA is self-originated. And just installed as Type-5.
1665 Additionally, install as Type-7 LSDB for every attached NSSA.
1667 P-Bit controls which ABR performs translation to outside world; If
1668 we are an ABR....do not set the P-bit, because we send the Type-5,
1669 not as the ABR Translator, but as the ASBR owner within the AS!
1671 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1672 elected ABR Translator will see the P-bit, Translate, and re-flood.
1674 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1675 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1677 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
1678 /* Don't install Type-7 LSA's into nonNSSA area */
1679 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1682 /* make lsa duplicate, lock=1 */
1683 new = ospf_lsa_dup(lsa
);
1685 new->data
->type
= OSPF_AS_NSSA_LSA
;
1687 /* set P-bit if not ABR */
1688 if (!IS_OSPF_ABR(ospf
)) {
1689 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1691 /* set non-zero FWD ADDR
1693 draft-ietf-ospf-nssa-update-09.txt
1695 if the network between the NSSA AS boundary router and
1697 adjacent AS is advertised into OSPF as an internal OSPF
1699 the forwarding address should be the next op address as
1701 currently done with type-5 LSAs. If the intervening
1703 not adversited into OSPF as an internal OSPF route and
1705 type-7 LSA's P-bit is set a forwarding address should be
1706 selected from one of the router's active OSPF interface
1708 which belong to the NSSA. If no such addresses exist,
1710 no type-7 LSA's with the P-bit set should originate from
1714 /* kevinm: not updating lsa anymore, just new */
1715 extlsa
= (struct as_external_lsa
*)(new->data
);
1717 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0)
1718 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(
1719 area
); /* this NSSA area in ifp */
1721 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0) {
1722 if (IS_DEBUG_OSPF_NSSA
)
1724 "LSA[Type-7]: Could not build FWD-ADDR");
1725 ospf_lsa_discard(new);
1730 /* install also as Type-7 */
1731 ospf_lsa_install(ospf
, NULL
,
1732 new); /* Remove Old, Lock New = 2 */
1734 /* will send each copy, lock=2+n */
1735 ospf_flood_through_as(
1736 ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1740 static struct ospf_lsa
*ospf_lsa_translated_nssa_new(struct ospf
*ospf
,
1741 struct ospf_lsa
*type7
)
1744 struct ospf_lsa
*new;
1745 struct as_external_lsa
*ext
, *extnew
;
1746 struct external_info ei
;
1748 ext
= (struct as_external_lsa
*)(type7
->data
);
1750 /* need external_info struct, fill in bare minimum */
1751 ei
.p
.family
= AF_INET
;
1752 ei
.p
.prefix
= type7
->data
->id
;
1753 ei
.p
.prefixlen
= ip_masklen(ext
->mask
);
1754 ei
.type
= ZEBRA_ROUTE_OSPF
;
1755 ei
.nexthop
= ext
->header
.adv_router
;
1756 ei
.route_map_set
.metric
= -1;
1757 ei
.route_map_set
.metric_type
= -1;
1760 if ((new = ospf_external_lsa_new(ospf
, &ei
, &type7
->data
->id
))
1762 if (IS_DEBUG_OSPF_NSSA
)
1764 "ospf_nssa_translate_originate(): Could not originate "
1765 "Translated Type-5 for %s",
1766 inet_ntoa(ei
.p
.prefix
));
1770 extnew
= (struct as_external_lsa
*)(new->data
);
1772 /* copy over Type-7 data to new */
1773 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1774 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1775 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1776 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1778 /* add translated flag, checksum and lock new lsa */
1779 SET_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1780 new = ospf_lsa_lock(new);
1785 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1786 struct ospf_lsa
*ospf_translated_nssa_originate(struct ospf
*ospf
,
1787 struct ospf_lsa
*type7
)
1789 struct ospf_lsa
*new;
1790 struct as_external_lsa
*extnew
;
1792 /* we cant use ospf_external_lsa_originate() as we need to set
1793 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1796 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1797 if (IS_DEBUG_OSPF_NSSA
)
1799 "ospf_translated_nssa_originate(): Could not translate "
1800 "Type-7, Id %s, to Type-5",
1801 inet_ntoa(type7
->data
->id
));
1805 extnew
= (struct as_external_lsa
*)new;
1807 if (IS_DEBUG_OSPF_NSSA
) {
1809 "ospf_translated_nssa_originate(): "
1810 "translated Type 7, installed:");
1811 ospf_lsa_header_dump(new->data
);
1812 zlog_debug(" Network mask: %d", ip_masklen(extnew
->mask
));
1813 zlog_debug(" Forward addr: %s",
1814 inet_ntoa(extnew
->e
[0].fwd_addr
));
1817 if ((new = ospf_lsa_install(ospf
, NULL
, new)) == NULL
) {
1818 if (IS_DEBUG_OSPF_NSSA
)
1820 "ospf_lsa_translated_nssa_originate(): "
1821 "Could not install LSA "
1823 inet_ntoa(type7
->data
->id
));
1827 ospf
->lsa_originate_count
++;
1828 ospf_flood_through_as(ospf
, NULL
, new);
1833 /* Refresh Translated from NSSA AS-external-LSA. */
1834 struct ospf_lsa
*ospf_translated_nssa_refresh(struct ospf
*ospf
,
1835 struct ospf_lsa
*type7
,
1836 struct ospf_lsa
*type5
)
1838 struct ospf_lsa
*new = NULL
;
1840 /* Sanity checks. */
1841 assert(type7
|| type5
);
1842 if (!(type7
|| type5
))
1845 assert(type7
->data
);
1847 assert(type5
->data
);
1848 assert(ospf
->anyNSSA
);
1850 /* get required data according to what has been given */
1851 if (type7
&& type5
== NULL
) {
1852 /* find the translated Type-5 for this Type-7 */
1853 struct as_external_lsa
*ext
=
1854 (struct as_external_lsa
*)(type7
->data
);
1855 struct prefix_ipv4 p
= {
1856 .prefix
= type7
->data
->id
,
1857 .prefixlen
= ip_masklen(ext
->mask
),
1861 type5
= ospf_external_info_find_lsa(ospf
, &p
);
1862 } else if (type5
&& type7
== NULL
) {
1863 /* find the type-7 from which supplied type-5 was translated,
1864 * ie find first type-7 with same LSA Id.
1866 struct listnode
*ln
, *lnn
;
1867 struct route_node
*rn
;
1868 struct ospf_lsa
*lsa
;
1869 struct ospf_area
*area
;
1871 for (ALL_LIST_ELEMENTS(ospf
->areas
, ln
, lnn
, area
)) {
1872 if (area
->external_routing
!= OSPF_AREA_NSSA
&& !type7
)
1875 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
) {
1876 if (lsa
->data
->id
.s_addr
1877 == type5
->data
->id
.s_addr
) {
1885 /* do we have type7? */
1887 if (IS_DEBUG_OSPF_NSSA
)
1889 "ospf_translated_nssa_refresh(): no Type-7 found for "
1891 inet_ntoa(type5
->data
->id
));
1895 /* do we have valid translated type5? */
1896 if (type5
== NULL
|| !CHECK_FLAG(type5
->flags
, OSPF_LSA_LOCAL_XLT
)) {
1897 if (IS_DEBUG_OSPF_NSSA
)
1899 "ospf_translated_nssa_refresh(): No translated Type-5 "
1900 "found for Type-7 with Id %s",
1901 inet_ntoa(type7
->data
->id
));
1905 /* Delete LSA from neighbor retransmit-list. */
1906 ospf_ls_retransmit_delete_nbr_as(ospf
, type5
);
1908 /* create new translated LSA */
1909 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1910 if (IS_DEBUG_OSPF_NSSA
)
1912 "ospf_translated_nssa_refresh(): Could not translate "
1913 "Type-7 for %s to Type-5",
1914 inet_ntoa(type7
->data
->id
));
1918 if (!(new = ospf_lsa_install(ospf
, NULL
, new))) {
1919 if (IS_DEBUG_OSPF_NSSA
)
1921 "ospf_translated_nssa_refresh(): Could not install "
1922 "translated LSA, Id %s",
1923 inet_ntoa(type7
->data
->id
));
1927 /* Flood LSA through area. */
1928 ospf_flood_through_as(ospf
, NULL
, new);
1933 int is_prefix_default(struct prefix_ipv4
*p
)
1935 struct prefix_ipv4 q
;
1938 q
.prefix
.s_addr
= 0;
1941 return prefix_same((struct prefix
*)p
, (struct prefix
*)&q
);
1944 /* Originate an AS-external-LSA, install and flood. */
1945 struct ospf_lsa
*ospf_external_lsa_originate(struct ospf
*ospf
,
1946 struct external_info
*ei
)
1948 struct ospf_lsa
*new;
1950 /* Added for NSSA project....
1952 External LSAs are originated in ASBRs as usual, but for NSSA
1954 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1955 every area. The Type-7's are flooded to every IR and every ABR; We
1956 install the Type-5 LSDB so that the normal "refresh" code operates
1957 as usual, and flag them as not used during ASE calculations. The
1958 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
1959 Address of non-zero.
1961 If an ABR is the elected NSSA translator, following SPF and during
1962 the ABR task it will translate all the scanned Type-7's, with P-bit
1963 ON and not-self generated, and translate to Type-5's throughout the
1966 A difference in operation depends whether this ASBR is an ABR
1967 or not. If not an ABR, the P-bit is ON, to indicate that any
1968 elected NSSA-ABR can perform its translation.
1970 If an ABR, the P-bit is OFF; No ABR will perform translation and
1971 this ASBR will flood the Type-5 LSA as usual.
1973 For the case where this ASBR is not an ABR, the ASE calculations
1974 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
1975 demonstrate to the user that there are LSA's that belong to any
1978 Finally, it just so happens that when the ABR is translating every
1979 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
1980 approved Type-5 (translated from Type-7); at the end of translation
1981 if any Translated Type-5's remain unapproved, then they must be
1982 flushed from the AS.
1986 /* Check the AS-external-LSA should be originated. */
1987 if (!ospf_redistribute_check(ospf
, ei
, NULL
))
1990 /* Create new AS-external-LSA instance. */
1991 if ((new = ospf_external_lsa_new(ospf
, ei
, NULL
)) == NULL
) {
1992 if (IS_DEBUG_OSPF_EVENT
)
1994 "LSA[Type5:%s]: Could not originate AS-external-LSA",
1995 inet_ntoa(ei
->p
.prefix
));
1999 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2000 ospf_lsa_install(ospf
, NULL
, new);
2002 /* Update LSA origination count. */
2003 ospf
->lsa_originate_count
++;
2005 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2006 ospf_flood_through_as(ospf
, NULL
, new);
2008 /* If there is any attached NSSA, do special handling */
2009 if (ospf
->anyNSSA
&&
2010 /* stay away from translated LSAs! */
2011 !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2012 ospf_install_flood_nssa(
2013 ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2015 /* Debug logging. */
2016 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2017 zlog_debug("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2018 new->data
->type
, inet_ntoa(new->data
->id
),
2020 ospf_lsa_header_dump(new->data
);
2026 /* Originate AS-external-LSA from external info with initial flag. */
2027 int ospf_external_lsa_originate_timer(struct thread
*thread
)
2029 struct ospf
*ospf
= THREAD_ARG(thread
);
2030 struct route_node
*rn
;
2031 struct external_info
*ei
;
2032 struct route_table
*rt
;
2033 int type
= THREAD_VAL(thread
);
2034 struct list
*ext_list
;
2035 struct listnode
*node
;
2036 struct ospf_external
*ext
;
2038 ospf
->t_external_lsa
= NULL
;
2040 ext_list
= ospf
->external
[type
];
2044 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
2045 /* Originate As-external-LSA from all type of distribute source.
2047 if ((rt
= ext
->external_info
))
2048 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
))
2049 if ((ei
= rn
->info
) != NULL
)
2050 if (!is_prefix_default(
2051 (struct prefix_ipv4
*)&ei
2053 if (!ospf_external_lsa_originate(
2056 "LSA: AS-external-LSA was not originated.");
2061 static struct external_info
*ospf_default_external_info(struct ospf
*ospf
)
2064 struct route_node
*rn
;
2065 struct prefix_ipv4 p
;
2068 p
.prefix
.s_addr
= 0;
2071 /* First, lookup redistributed default route. */
2072 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
2073 struct list
*ext_list
;
2074 struct listnode
*node
;
2075 struct ospf_external
*ext
;
2077 if (type
== ZEBRA_ROUTE_OSPF
)
2080 ext_list
= ospf
->external
[type
];
2084 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2085 rn
= route_node_lookup(ext
->external_info
,
2086 (struct prefix
*)&p
);
2088 route_unlock_node(rn
);
2090 if (ospf_redistribute_check(ospf
, rn
->info
,
2100 int ospf_default_originate_timer(struct thread
*thread
)
2102 struct prefix_ipv4 p
;
2103 struct in_addr nexthop
;
2104 struct external_info
*ei
;
2107 ospf
= THREAD_ARG(thread
);
2110 p
.prefix
.s_addr
= 0;
2113 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ALWAYS
) {
2114 /* If there is no default route via redistribute,
2115 then originate AS-external-LSA with nexthop 0 (self). */
2117 ospf_external_info_add(ospf
, DEFAULT_ROUTE
, 0, p
, 0,
2121 if ((ei
= ospf_default_external_info(ospf
)))
2122 ospf_external_lsa_originate(ospf
, ei
);
2127 /* Flush any NSSA LSAs for given prefix */
2128 void ospf_nssa_lsa_flush(struct ospf
*ospf
, struct prefix_ipv4
*p
)
2130 struct listnode
*node
, *nnode
;
2131 struct ospf_lsa
*lsa
= NULL
;
2132 struct ospf_area
*area
;
2134 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2135 if (area
->external_routing
== OSPF_AREA_NSSA
) {
2136 lsa
= ospf_lsa_lookup(ospf
, area
,
2137 OSPF_AS_NSSA_LSA
, p
->prefix
,
2140 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2142 "LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2143 inet_ntoa(p
->prefix
),
2147 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
2148 if (!IS_LSA_MAXAGE(lsa
)) {
2149 ospf_refresher_unregister_lsa(ospf
, lsa
);
2150 ospf_lsa_flush_area(lsa
, area
);
2156 /* Flush an AS-external-LSA from LSDB and routing domain. */
2157 void ospf_external_lsa_flush(struct ospf
*ospf
, u_char type
,
2158 struct prefix_ipv4
*p
,
2159 ifindex_t ifindex
/*, struct in_addr nexthop */)
2161 struct ospf_lsa
*lsa
;
2163 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2164 zlog_debug("LSA: Flushing AS-external-LSA %s/%d",
2165 inet_ntoa(p
->prefix
), p
->prefixlen
);
2167 /* First lookup LSA from LSDB. */
2168 if (!(lsa
= ospf_external_info_find_lsa(ospf
, p
))) {
2169 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2171 "LSA: There is no such AS-external-LSA %s/%d in LSDB",
2172 inet_ntoa(p
->prefix
), p
->prefixlen
);
2176 /* If LSA is selforiginated, not a translated LSA, and there is
2177 * NSSA area, flush Type-7 LSA's at first.
2179 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2180 && !(CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2181 ospf_nssa_lsa_flush(ospf
, p
);
2183 /* Sweep LSA from Link State Retransmit List. */
2184 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2186 /* There must be no self-originated LSA in rtrs_external. */
2188 /* Remove External route from Zebra. */
2189 ospf_zebra_delete ((struct prefix_ipv4
*) p
, &nexthop
);
2192 if (!IS_LSA_MAXAGE(lsa
)) {
2193 /* Unregister LSA from Refresh queue. */
2194 ospf_refresher_unregister_lsa(ospf
, lsa
);
2196 /* Flush AS-external-LSA through AS. */
2197 ospf_lsa_flush_as(ospf
, lsa
);
2200 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2201 zlog_debug("ospf_external_lsa_flush(): stop");
2204 void ospf_external_lsa_refresh_default(struct ospf
*ospf
)
2206 struct prefix_ipv4 p
;
2207 struct external_info
*ei
;
2208 struct ospf_lsa
*lsa
;
2212 p
.prefix
.s_addr
= 0;
2214 ei
= ospf_default_external_info(ospf
);
2215 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
2219 if (IS_DEBUG_OSPF_EVENT
)
2221 "LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2223 ospf_external_lsa_refresh(ospf
, lsa
, ei
,
2226 if (IS_DEBUG_OSPF_EVENT
)
2228 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2229 ospf_external_lsa_originate(ospf
, ei
);
2233 if (IS_DEBUG_OSPF_EVENT
)
2235 "LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2236 ospf_refresher_unregister_lsa(ospf
, lsa
);
2237 ospf_lsa_flush_as(ospf
, lsa
);
2242 void ospf_external_lsa_refresh_type(struct ospf
*ospf
, u_char type
,
2243 u_short instance
, int force
)
2245 struct route_node
*rn
;
2246 struct external_info
*ei
;
2247 struct ospf_external
*ext
;
2249 if (type
== DEFAULT_ROUTE
)
2252 ext
= ospf_external_lookup(ospf
, type
, instance
);
2254 if (ext
&& EXTERNAL_INFO(ext
)) {
2255 /* Refresh each redistributed AS-external-LSAs. */
2256 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
2257 rn
= route_next(rn
)) {
2260 if (!is_prefix_default(&ei
->p
)) {
2261 struct ospf_lsa
*lsa
;
2263 lsa
= ospf_external_info_find_lsa(ospf
,
2266 ospf_external_lsa_refresh(ospf
,
2269 ospf_external_lsa_originate(ospf
2277 /* Refresh AS-external-LSA. */
2278 struct ospf_lsa
*ospf_external_lsa_refresh(struct ospf
*ospf
,
2279 struct ospf_lsa
*lsa
,
2280 struct external_info
*ei
, int force
)
2282 struct ospf_lsa
*new;
2285 /* Check the AS-external-LSA should be originated. */
2286 if (!ospf_redistribute_check(ospf
, ei
, &changed
)) {
2287 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2289 "LSA[Type%d:%s]: Could not be refreshed, "
2290 "redist check fail",
2291 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2292 ospf_external_lsa_flush(ospf
, ei
->type
, &ei
->p
,
2293 ei
->ifindex
/*, ei->nexthop */);
2297 if (!changed
&& !force
) {
2298 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2300 "LSA[Type%d:%s]: Not refreshed, not changed/forced",
2301 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2305 /* Delete LSA from neighbor retransmit-list. */
2306 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2308 /* Unregister AS-external-LSA from refresh-list. */
2309 ospf_refresher_unregister_lsa(ospf
, lsa
);
2311 new = ospf_external_lsa_new(ospf
, ei
, &lsa
->data
->id
);
2314 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2315 zlog_debug("LSA[Type%d:%s]: Could not be refreshed",
2316 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2320 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2322 ospf_lsa_install(ospf
, NULL
, new); /* As type-5. */
2324 /* Flood LSA through AS. */
2325 ospf_flood_through_as(ospf
, NULL
, new);
2327 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2328 if (ospf
->anyNSSA
&& !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2329 ospf_install_flood_nssa(ospf
, new,
2330 ei
); /* Install/Flood per new rules */
2332 /* Register self-originated LSA to refresh queue.
2333 * Translated LSAs should not be registered, but refreshed upon
2334 * refresh of the Type-7
2336 if (!CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2337 ospf_refresher_register_lsa(ospf
, new);
2339 /* Debug logging. */
2340 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2341 zlog_debug("LSA[Type%d:%s]: AS-external-LSA refresh",
2342 new->data
->type
, inet_ntoa(new->data
->id
));
2343 ospf_lsa_header_dump(new->data
);
2350 /* LSA installation functions. */
2352 /* Install router-LSA to an area. */
2353 static struct ospf_lsa
*
2354 ospf_router_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2356 struct ospf_area
*area
= new->area
;
2358 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2359 The entire routing table must be recalculated, starting with
2360 the shortest path calculations for each area (not just the
2361 area whose link-state database has changed).
2364 if (IS_LSA_SELF(new)) {
2366 /* Only install LSA if it is originated/refreshed by us.
2367 * If LSA was received by flooding, the RECEIVED flag is set so
2369 * not link the LSA */
2370 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2371 return new; /* ignore stale LSA */
2373 /* Set self-originated router-LSA. */
2374 ospf_lsa_unlock(&area
->router_lsa_self
);
2375 area
->router_lsa_self
= ospf_lsa_lock(new);
2377 ospf_refresher_register_lsa(ospf
, new);
2380 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2384 #define OSPF_INTERFACE_TIMER_ON(T, F, V) \
2386 (T) = thread_add_timer(master, (F), oi, (V))
2388 /* Install network-LSA to an area. */
2389 static struct ospf_lsa
*ospf_network_lsa_install(struct ospf
*ospf
,
2390 struct ospf_interface
*oi
,
2391 struct ospf_lsa
*new,
2395 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2396 The entire routing table must be recalculated, starting with
2397 the shortest path calculations for each area (not just the
2398 area whose link-state database has changed).
2400 if (IS_LSA_SELF(new)) {
2401 /* We supposed that when LSA is originated by us, we pass the
2403 for which it was originated. If LSA was received by flooding,
2404 the RECEIVED flag is set, so we do not link the LSA to the
2406 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2407 return new; /* ignore stale LSA */
2409 ospf_lsa_unlock(&oi
->network_lsa_self
);
2410 oi
->network_lsa_self
= ospf_lsa_lock(new);
2411 ospf_refresher_register_lsa(ospf
, new);
2414 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2419 /* Install summary-LSA to an area. */
2420 static struct ospf_lsa
*
2421 ospf_summary_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2423 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2424 /* RFC 2328 Section 13.2 Summary-LSAs
2425 The best route to the destination described by the summary-
2426 LSA must be recalculated (see Section 16.5). If this
2427 destination is an AS boundary router, it may also be
2428 necessary to re-examine all the AS-external-LSAs.
2432 /* This doesn't exist yet... */
2433 ospf_summary_incremental_update(new); */
2435 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2439 if (IS_LSA_SELF(new))
2440 ospf_refresher_register_lsa(ospf
, new);
2445 /* Install ASBR-summary-LSA to an area. */
2446 static struct ospf_lsa
*ospf_summary_asbr_lsa_install(struct ospf
*ospf
,
2447 struct ospf_lsa
*new,
2450 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2451 /* RFC 2328 Section 13.2 Summary-LSAs
2452 The best route to the destination described by the summary-
2453 LSA must be recalculated (see Section 16.5). If this
2454 destination is an AS boundary router, it may also be
2455 necessary to re-examine all the AS-external-LSAs.
2458 /* These don't exist yet... */
2459 ospf_summary_incremental_update(new);
2460 /* Isn't this done by the above call?
2461 - RFC 2328 Section 16.5 implies it should be */
2462 /* ospf_ase_calculate_schedule(); */
2464 ospf_spf_calculate_schedule(ospf
,
2465 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2469 /* register LSA to refresh-list. */
2470 if (IS_LSA_SELF(new))
2471 ospf_refresher_register_lsa(ospf
, new);
2476 /* Install AS-external-LSA. */
2477 static struct ospf_lsa
*ospf_external_lsa_install(struct ospf
*ospf
,
2478 struct ospf_lsa
*new,
2481 ospf_ase_register_external_lsa(new, ospf
);
2482 /* If LSA is not self-originated, calculate an external route. */
2484 /* RFC 2328 Section 13.2 AS-external-LSAs
2485 The best route to the destination described by the AS-
2486 external-LSA must be recalculated (see Section 16.6).
2489 if (!IS_LSA_SELF(new))
2490 ospf_ase_incremental_update(ospf
, new);
2493 if (new->data
->type
== OSPF_AS_NSSA_LSA
) {
2494 /* There is no point to register selforiginate Type-7 LSA for
2495 * refreshing. We rely on refreshing Type-5 LSA's
2497 if (IS_LSA_SELF(new))
2500 /* Try refresh type-5 translated LSA for this LSA, if
2502 * New translations will be taken care of by the
2505 ospf_translated_nssa_refresh(ospf
, new, NULL
);
2506 ospf_schedule_abr_task(ospf
);
2510 /* Register self-originated LSA to refresh queue.
2511 * Leave Translated LSAs alone if NSSA is enabled
2513 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2514 ospf_refresher_register_lsa(ospf
, new);
2519 void ospf_discard_from_db(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
2520 struct ospf_lsa
*lsa
)
2522 struct ospf_lsa
*old
;
2525 zlog_warn("%s: Called with NULL lsdb!", __func__
);
2527 zlog_warn("%s: and NULL LSA!", __func__
);
2529 zlog_warn("LSA[Type%d:%s]: not associated with LSDB!",
2530 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2534 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2539 if (old
->refresh_list
>= 0)
2540 ospf_refresher_unregister_lsa(ospf
, old
);
2542 switch (old
->data
->type
) {
2543 case OSPF_AS_EXTERNAL_LSA
:
2544 ospf_ase_unregister_external_lsa(old
, ospf
);
2545 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2547 case OSPF_OPAQUE_AS_LSA
:
2548 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2550 case OSPF_AS_NSSA_LSA
:
2551 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2552 ospf_ase_unregister_external_lsa(old
, ospf
);
2555 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2559 ospf_lsa_maxage_delete(ospf
, old
);
2560 ospf_lsa_discard(old
);
2563 struct ospf_lsa
*ospf_lsa_install(struct ospf
*ospf
, struct ospf_interface
*oi
,
2564 struct ospf_lsa
*lsa
)
2566 struct ospf_lsa
*new = NULL
;
2567 struct ospf_lsa
*old
= NULL
;
2568 struct ospf_lsdb
*lsdb
= NULL
;
2572 switch (lsa
->data
->type
) {
2574 case OSPF_AS_NSSA_LSA
:
2576 lsdb
= lsa
->area
->lsdb
;
2580 case OSPF_AS_EXTERNAL_LSA
:
2581 case OSPF_OPAQUE_AS_LSA
:
2585 lsdb
= lsa
->area
->lsdb
;
2591 /* RFC 2328 13.2. Installing LSAs in the database
2593 Installing a new LSA in the database, either as the result of
2594 flooding or a newly self-originated LSA, may cause the OSPF
2595 routing table structure to be recalculated. The contents of the
2596 new LSA should be compared to the old instance, if present. If
2597 there is no difference, there is no need to recalculate the
2598 routing table. When comparing an LSA to its previous instance,
2599 the following are all considered to be differences in contents:
2601 o The LSA's Options field has changed.
2603 o One of the LSA instances has LS age set to MaxAge, and
2606 o The length field in the LSA header has changed.
2608 o The body of the LSA (i.e., anything outside the 20-byte
2609 LSA header) has changed. Note that this excludes changes
2610 in LS Sequence Number and LS Checksum.
2613 /* Look up old LSA and determine if any SPF calculation or incremental
2615 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2617 /* Do comparision and record if recalc needed. */
2619 if (old
== NULL
|| ospf_lsa_different(old
, lsa
))
2623 Sequence number check (Section 14.1 of rfc 2328)
2624 "Premature aging is used when it is time for a self-originated
2625 LSA's sequence number field to wrap. At this point, the current
2626 LSA instance (having LS sequence number MaxSequenceNumber) must
2627 be prematurely aged and flushed from the routing domain before a
2628 new instance with sequence number equal to InitialSequenceNumber
2629 can be originated. "
2632 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
) {
2633 if (ospf_lsa_is_self_originated(ospf
, lsa
)) {
2634 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2636 if (!IS_LSA_MAXAGE(lsa
))
2637 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2638 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
2640 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
)) {
2642 "ospf_lsa_install() Premature Aging "
2643 "lsa 0x%p, seqnum 0x%x",
2645 ntohl(lsa
->data
->ls_seqnum
));
2646 ospf_lsa_header_dump(lsa
->data
);
2649 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2651 "ospf_lsa_install() got an lsa with seq 0x80000000 "
2652 "that was not self originated. Ignoring\n");
2653 ospf_lsa_header_dump(lsa
->data
);
2659 /* discard old LSA from LSDB */
2661 ospf_discard_from_db(ospf
, lsdb
, lsa
);
2663 /* Calculate Checksum if self-originated?. */
2664 if (IS_LSA_SELF(lsa
))
2665 ospf_lsa_checksum(lsa
->data
);
2667 /* Insert LSA to LSDB. */
2668 ospf_lsdb_add(lsdb
, lsa
);
2671 /* Do LSA specific installation process. */
2672 switch (lsa
->data
->type
) {
2673 case OSPF_ROUTER_LSA
:
2674 new = ospf_router_lsa_install(ospf
, lsa
, rt_recalc
);
2676 case OSPF_NETWORK_LSA
:
2678 new = ospf_network_lsa_install(ospf
, oi
, lsa
, rt_recalc
);
2680 case OSPF_SUMMARY_LSA
:
2681 new = ospf_summary_lsa_install(ospf
, lsa
, rt_recalc
);
2683 case OSPF_ASBR_SUMMARY_LSA
:
2684 new = ospf_summary_asbr_lsa_install(ospf
, lsa
, rt_recalc
);
2686 case OSPF_AS_EXTERNAL_LSA
:
2687 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2689 case OSPF_OPAQUE_LINK_LSA
:
2690 if (IS_LSA_SELF(lsa
))
2691 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for
2694 /* Incoming "oi" for this LSA has set at LSUpd
2698 case OSPF_OPAQUE_AREA_LSA
:
2699 case OSPF_OPAQUE_AS_LSA
:
2700 new = ospf_opaque_lsa_install(lsa
, rt_recalc
);
2702 case OSPF_AS_NSSA_LSA
:
2703 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2704 default: /* type-6,8,9....nothing special */
2709 return new; /* Installation failed, cannot proceed further --
2713 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
)) {
2714 char area_str
[INET_ADDRSTRLEN
];
2716 switch (lsa
->data
->type
) {
2717 case OSPF_AS_EXTERNAL_LSA
:
2718 case OSPF_OPAQUE_AS_LSA
:
2719 case OSPF_AS_NSSA_LSA
:
2720 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2721 lookup_msg(ospf_lsa_type_msg
,
2722 new->data
->type
, NULL
));
2725 strlcpy(area_str
, inet_ntoa(new->area
->area_id
),
2727 zlog_debug("LSA[%s]: Install %s to Area %s",
2729 lookup_msg(ospf_lsa_type_msg
,
2730 new->data
->type
, NULL
),
2737 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2738 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2740 if (IS_LSA_MAXAGE(new)) {
2741 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
))
2742 zlog_debug("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2743 new->data
->type
, inet_ntoa(new->data
->id
),
2745 ospf_lsa_maxage(ospf
, lsa
);
2752 int ospf_check_nbr_status(struct ospf
*ospf
)
2754 struct listnode
*node
, *nnode
;
2755 struct ospf_interface
*oi
;
2757 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
2758 struct route_node
*rn
;
2759 struct ospf_neighbor
*nbr
;
2761 if (ospf_if_is_enable(oi
))
2762 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
2763 if ((nbr
= rn
->info
) != NULL
)
2764 if (nbr
->state
== NSM_Exchange
2765 || nbr
->state
== NSM_Loading
) {
2766 route_unlock_node(rn
);
2775 static int ospf_maxage_lsa_remover(struct thread
*thread
)
2777 struct ospf
*ospf
= THREAD_ARG(thread
);
2778 struct ospf_lsa
*lsa
;
2779 struct route_node
*rn
;
2782 ospf
->t_maxage
= NULL
;
2784 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2785 zlog_debug("LSA[MaxAge]: remover Start");
2787 reschedule
= !ospf_check_nbr_status(ospf
);
2790 for (rn
= route_top(ospf
->maxage_lsa
); rn
;
2791 rn
= route_next(rn
)) {
2792 if ((lsa
= rn
->info
) == NULL
) {
2796 /* There is at least one neighbor from which we still
2798 * for that LSA, so we are not allowed to remove it from
2800 * as per RFC 2328 section 14 para 4 a) */
2801 if (lsa
->retransmit_counter
> 0) {
2806 /* TODO: maybe convert this function to a work-queue */
2807 if (thread_should_yield(thread
)) {
2808 OSPF_TIMER_ON(ospf
->t_maxage
,
2809 ospf_maxage_lsa_remover
, 0);
2811 rn
); /* route_top/route_next */
2815 /* Remove LSA from the LSDB */
2816 if (IS_LSA_SELF(lsa
))
2817 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2819 "LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2821 inet_ntoa(lsa
->data
->id
),
2824 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2826 "LSA[Type%d:%s]: MaxAge LSA removed from list",
2828 inet_ntoa(lsa
->data
->id
));
2830 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
)) {
2831 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2833 "originating new lsa for lsa 0x%p\n",
2835 ospf_lsa_refresh(ospf
, lsa
);
2838 /* Remove from lsdb. */
2840 ospf_discard_from_db(ospf
, lsa
->lsdb
, lsa
);
2841 ospf_lsdb_delete(lsa
->lsdb
, lsa
);
2844 "%s: LSA[Type%d:%s]: No associated LSDB!",
2845 __func__
, lsa
->data
->type
,
2846 inet_ntoa(lsa
->data
->id
));
2849 /* A MaxAge LSA must be removed immediately from the router's link
2850 state database as soon as both a) it is no longer contained on any
2851 neighbor Link state retransmission lists and b) none of the
2853 neighbors are in states Exchange or Loading. */
2855 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2856 ospf
->maxage_delay
);
2861 void ospf_lsa_maxage_delete(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2863 struct route_node
*rn
;
2864 struct prefix lsa_prefix
;
2866 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2867 lsa_prefix
.family
= 0;
2868 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2869 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2871 if ((rn
= route_node_lookup(ospf
->maxage_lsa
,
2872 (struct prefix
*)&lsa_prefix
))) {
2873 if (rn
->info
== lsa
) {
2874 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2875 ospf_lsa_unlock(&lsa
); /* maxage_lsa */
2878 rn
); /* unlock node because lsa is deleted */
2880 route_unlock_node(rn
); /* route_node_lookup */
2882 if (IS_DEBUG_OSPF_EVENT
)
2883 zlog_debug("%s: lsa %s is not found in maxage db.",
2884 __PRETTY_FUNCTION__
, dump_lsa_key(lsa
));
2888 /* Add LSA onto the MaxAge list, and schedule for removal.
2889 * This does *not* lead to the LSA being flooded, that must be taken
2890 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2893 void ospf_lsa_maxage(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2895 struct prefix lsa_prefix
;
2896 struct route_node
*rn
;
2898 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2899 and schedule the MaxAge LSA remover. */
2900 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
)) {
2901 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2903 "LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2904 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
2909 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2910 lsa_prefix
.family
= 0;
2911 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2912 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2914 if ((rn
= route_node_get(ospf
->maxage_lsa
,
2915 (struct prefix
*)&lsa_prefix
))
2917 if (rn
->info
!= NULL
) {
2918 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2920 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
2921 dump_lsa_key(lsa
), rn
->info
,
2922 (void *)lsa
, lsa_prefix
.prefixlen
);
2923 route_unlock_node(rn
);
2925 rn
->info
= ospf_lsa_lock(lsa
);
2926 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2929 zlog_err("Unable to allocate memory for maxage lsa %s\n",
2934 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2935 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
2938 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2939 ospf
->maxage_delay
);
2942 static int ospf_lsa_maxage_walker_remover(struct ospf
*ospf
,
2943 struct ospf_lsa
*lsa
)
2945 /* Stay away from any Local Translated Type-7 LSAs */
2946 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
2949 if (IS_LSA_MAXAGE(lsa
))
2950 /* Self-originated LSAs should NOT time-out instead,
2951 they're flushed and submitted to the max_age list explicitly.
2953 if (!ospf_lsa_is_self_originated(ospf
, lsa
)) {
2954 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2955 zlog_debug("LSA[%s]: is MaxAge",
2958 switch (lsa
->data
->type
) {
2959 case OSPF_OPAQUE_LINK_LSA
:
2960 case OSPF_OPAQUE_AREA_LSA
:
2961 case OSPF_OPAQUE_AS_LSA
:
2963 * As a general rule, whenever network topology
2965 * (due to an LSA removal in this case), routing
2967 * should be triggered. However, this is not
2969 * LSAs. Even if an opaque LSA instance is going
2971 * from the routing domain, it does not mean a
2973 * topology, and thus, routing recalculation is
2977 case OSPF_AS_EXTERNAL_LSA
:
2978 case OSPF_AS_NSSA_LSA
:
2979 ospf_ase_incremental_update(ospf
, lsa
);
2982 ospf_spf_calculate_schedule(ospf
,
2986 ospf_lsa_maxage(ospf
, lsa
);
2989 if (IS_LSA_MAXAGE(lsa
) && !ospf_lsa_is_self_originated(ospf
, lsa
))
2990 if (LS_AGE(lsa
) > OSPF_LSA_MAXAGE
+ 30)
2991 printf("Eek! Shouldn't happen!\n");
2996 /* Periodical check of MaxAge LSA. */
2997 int ospf_lsa_maxage_walker(struct thread
*thread
)
2999 struct ospf
*ospf
= THREAD_ARG(thread
);
3000 struct route_node
*rn
;
3001 struct ospf_lsa
*lsa
;
3002 struct ospf_area
*area
;
3003 struct listnode
*node
, *nnode
;
3005 ospf
->t_maxage_walker
= NULL
;
3007 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3008 LSDB_LOOP(ROUTER_LSDB(area
), rn
, lsa
)
3009 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3010 LSDB_LOOP(NETWORK_LSDB(area
), rn
, lsa
)
3011 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3012 LSDB_LOOP(SUMMARY_LSDB(area
), rn
, lsa
)
3013 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3014 LSDB_LOOP(ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3015 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3016 LSDB_LOOP(OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3017 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3018 LSDB_LOOP(OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3019 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3020 LSDB_LOOP(NSSA_LSDB(area
), rn
, lsa
)
3021 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3024 /* for AS-external-LSAs. */
3026 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
3027 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3028 LSDB_LOOP(OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3029 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3032 OSPF_TIMER_ON(ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3033 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3037 struct ospf_lsa
*ospf_lsa_lookup_by_prefix(struct ospf_lsdb
*lsdb
, u_char type
,
3038 struct prefix_ipv4
*p
,
3039 struct in_addr router_id
)
3041 struct ospf_lsa
*lsa
;
3042 struct in_addr mask
, id
;
3043 struct lsa_header_mask
{
3044 struct lsa_header header
;
3045 struct in_addr mask
;
3048 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, p
->prefix
, router_id
);
3052 masklen2ip(p
->prefixlen
, &mask
);
3054 hmask
= (struct lsa_header_mask
*)lsa
->data
;
3056 if (mask
.s_addr
!= hmask
->mask
.s_addr
) {
3057 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3058 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, router_id
);
3066 struct ospf_lsa
*ospf_lsa_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
3067 u_int32_t type
, struct in_addr id
,
3068 struct in_addr adv_router
)
3074 case OSPF_ROUTER_LSA
:
3075 case OSPF_NETWORK_LSA
:
3076 case OSPF_SUMMARY_LSA
:
3077 case OSPF_ASBR_SUMMARY_LSA
:
3078 case OSPF_AS_NSSA_LSA
:
3079 case OSPF_OPAQUE_LINK_LSA
:
3080 case OSPF_OPAQUE_AREA_LSA
:
3081 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, adv_router
);
3082 case OSPF_AS_EXTERNAL_LSA
:
3083 case OSPF_OPAQUE_AS_LSA
:
3084 return ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
, adv_router
);
3092 struct ospf_lsa
*ospf_lsa_lookup_by_id(struct ospf_area
*area
, u_int32_t type
,
3095 struct ospf_lsa
*lsa
;
3096 struct route_node
*rn
;
3099 case OSPF_ROUTER_LSA
:
3100 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3101 case OSPF_NETWORK_LSA
:
3102 for (rn
= route_top(NETWORK_LSDB(area
)); rn
;
3103 rn
= route_next(rn
))
3104 if ((lsa
= rn
->info
))
3105 if (IPV4_ADDR_SAME(&lsa
->data
->id
, &id
)) {
3106 route_unlock_node(rn
);
3110 case OSPF_SUMMARY_LSA
:
3111 case OSPF_ASBR_SUMMARY_LSA
:
3112 /* Currently not used. */
3114 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3115 case OSPF_AS_EXTERNAL_LSA
:
3116 case OSPF_AS_NSSA_LSA
:
3117 case OSPF_OPAQUE_LINK_LSA
:
3118 case OSPF_OPAQUE_AREA_LSA
:
3119 case OSPF_OPAQUE_AS_LSA
:
3120 /* Currently not used. */
3129 struct ospf_lsa
*ospf_lsa_lookup_by_header(struct ospf_area
*area
,
3130 struct lsa_header
*lsah
)
3132 struct ospf_lsa
*match
;
3135 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3136 * is redefined to have two subfields; opaque-type and opaque-id.
3137 * However, it is harmless to treat the two sub fields together, as if
3138 * they two were forming a unique LSA-ID.
3141 match
= ospf_lsa_lookup(area
->ospf
, area
, lsah
->type
, lsah
->id
,
3145 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3146 zlog_debug("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3147 lsah
->type
, inet_ntoa(lsah
->id
));
3152 /* return +n, l1 is more recent.
3153 return -n, l2 is more recent.
3154 return 0, l1 and l2 is identical. */
3155 int ospf_lsa_more_recent(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3160 if (l1
== NULL
&& l2
== NULL
)
3167 /* compare LS sequence number. */
3168 x
= (int)ntohl(l1
->data
->ls_seqnum
);
3169 y
= (int)ntohl(l2
->data
->ls_seqnum
);
3175 /* compare LS checksum. */
3176 r
= ntohs(l1
->data
->checksum
) - ntohs(l2
->data
->checksum
);
3180 /* compare LS age. */
3181 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3183 else if (!IS_LSA_MAXAGE(l1
) && IS_LSA_MAXAGE(l2
))
3186 /* compare LS age with MaxAgeDiff. */
3187 if (LS_AGE(l1
) - LS_AGE(l2
) > OSPF_LSA_MAXAGE_DIFF
)
3189 else if (LS_AGE(l2
) - LS_AGE(l1
) > OSPF_LSA_MAXAGE_DIFF
)
3192 /* LSAs are identical. */
3196 /* If two LSAs are different, return 1, otherwise return 0. */
3197 int ospf_lsa_different(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3205 if (l1
->data
->options
!= l2
->data
->options
)
3208 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3211 if (IS_LSA_MAXAGE(l2
) && !IS_LSA_MAXAGE(l1
))
3214 if (l1
->data
->length
!= l2
->data
->length
)
3217 if (l1
->data
->length
== 0)
3220 if (CHECK_FLAG((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3221 return 1; /* May be a stale LSA in the LSBD */
3223 assert(ntohs(l1
->data
->length
) > OSPF_LSA_HEADER_SIZE
);
3225 p1
= (char *)l1
->data
;
3226 p2
= (char *)l2
->data
;
3228 if (memcmp(p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3229 ntohs(l1
->data
->length
) - OSPF_LSA_HEADER_SIZE
)
3236 #ifdef ORIGINAL_CODING
3237 void ospf_lsa_flush_self_originated(struct ospf_neighbor
*nbr
,
3238 struct ospf_lsa
*self
, struct ospf_lsa
*new)
3242 /* Adjust LS Sequence Number. */
3243 seqnum
= ntohl(new->data
->ls_seqnum
) + 1;
3244 self
->data
->ls_seqnum
= htonl(seqnum
);
3246 /* Recalculate LSA checksum. */
3247 ospf_lsa_checksum(self
->data
);
3249 /* Reflooding LSA. */
3250 /* RFC2328 Section 13.3
3251 On non-broadcast networks, separate Link State Update
3252 packets must be sent, as unicasts, to each adjacent neighbor
3253 (i.e., those in state Exchange or greater). The destination
3254 IP addresses for these packets are the neighbors' IP
3256 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
) {
3257 struct route_node
*rn
;
3258 struct ospf_neighbor
*onbr
;
3260 for (rn
= route_top(nbr
->oi
->nbrs
); rn
; rn
= route_next(rn
))
3261 if ((onbr
= rn
->info
) != NULL
)
3262 if (onbr
!= nbr
->oi
->nbr_self
3263 && onbr
->status
>= NSM_Exchange
)
3264 ospf_ls_upd_send_lsa(
3266 OSPF_SEND_PACKET_DIRECT
);
3268 ospf_ls_upd_send_lsa(nbr
, self
, OSPF_SEND_PACKET_INDIRECT
);
3270 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3271 zlog_debug("LSA[Type%d:%s]: Flush self-originated LSA",
3272 self
->data
->type
, inet_ntoa(self
->data
->id
));
3274 #else /* ORIGINAL_CODING */
3275 int ospf_lsa_flush_schedule(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3277 if (lsa
== NULL
|| !IS_LSA_SELF(lsa
))
3280 if (IS_DEBUG_OSPF_EVENT
)
3282 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3283 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
3285 /* Force given lsa's age to MaxAge. */
3286 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
3288 switch (lsa
->data
->type
) {
3289 /* Opaque wants to be notified of flushes */
3290 case OSPF_OPAQUE_LINK_LSA
:
3291 case OSPF_OPAQUE_AREA_LSA
:
3292 case OSPF_OPAQUE_AS_LSA
:
3293 ospf_opaque_lsa_refresh(lsa
);
3296 ospf_refresher_unregister_lsa(ospf
, lsa
);
3297 ospf_lsa_flush(ospf
, lsa
);
3304 void ospf_flush_self_originated_lsas_now(struct ospf
*ospf
)
3306 struct listnode
*node
, *nnode
;
3307 struct listnode
*node2
, *nnode2
;
3308 struct ospf_area
*area
;
3309 struct ospf_interface
*oi
;
3310 struct ospf_lsa
*lsa
;
3311 struct route_node
*rn
;
3312 int need_to_flush_ase
= 0;
3314 ospf
->inst_shutdown
= 1;
3316 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3317 if ((lsa
= area
->router_lsa_self
) != NULL
) {
3318 if (IS_DEBUG_OSPF_EVENT
)
3320 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3322 inet_ntoa(lsa
->data
->id
));
3324 ospf_refresher_unregister_lsa(ospf
, lsa
);
3325 ospf_lsa_flush_area(lsa
, area
);
3326 ospf_lsa_unlock(&area
->router_lsa_self
);
3327 area
->router_lsa_self
= NULL
;
3330 for (ALL_LIST_ELEMENTS(area
->oiflist
, node2
, nnode2
, oi
)) {
3331 if ((lsa
= oi
->network_lsa_self
) != NULL
3332 && oi
->state
== ISM_DR
&& oi
->full_nbrs
> 0) {
3333 if (IS_DEBUG_OSPF_EVENT
)
3335 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3337 inet_ntoa(lsa
->data
->id
));
3339 ospf_refresher_unregister_lsa(
3340 ospf
, oi
->network_lsa_self
);
3341 ospf_lsa_flush_area(oi
->network_lsa_self
, area
);
3342 ospf_lsa_unlock(&oi
->network_lsa_self
);
3343 oi
->network_lsa_self
= NULL
;
3346 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3347 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3348 need_to_flush_ase
= 1;
3351 LSDB_LOOP(SUMMARY_LSDB(area
), rn
, lsa
)
3352 ospf_lsa_flush_schedule(ospf
, lsa
);
3353 LSDB_LOOP(ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3354 ospf_lsa_flush_schedule(ospf
, lsa
);
3355 LSDB_LOOP(OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3356 ospf_lsa_flush_schedule(ospf
, lsa
);
3357 LSDB_LOOP(OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3358 ospf_lsa_flush_schedule(ospf
, lsa
);
3361 if (need_to_flush_ase
) {
3362 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
3363 ospf_lsa_flush_schedule(ospf
, lsa
);
3364 LSDB_LOOP(OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3365 ospf_lsa_flush_schedule(ospf
, lsa
);
3369 * Make sure that the MaxAge LSA remover is executed immediately,
3370 * without conflicting to other threads.
3372 if (ospf
->t_maxage
!= NULL
) {
3373 OSPF_TIMER_OFF(ospf
->t_maxage
);
3374 thread_execute(master
, ospf_maxage_lsa_remover
, ospf
, 0);
3379 #endif /* ORIGINAL_CODING */
3381 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3382 /* An interface-independent version of ospf_lsa_is_self_originated */
3383 int ospf_lsa_is_self_originated(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3385 struct listnode
*node
;
3386 struct ospf_interface
*oi
;
3388 /* This LSA is already checked. */
3389 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3390 return IS_LSA_SELF(lsa
);
3392 /* Make sure LSA is self-checked. */
3393 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3395 /* AdvRouter and Router ID is the same. */
3396 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
, &ospf
->router_id
))
3397 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3399 /* LSA is router-LSA. */
3400 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
3401 && IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
))
3402 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3404 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3405 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3406 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
3407 /* Ignore virtual link. */
3408 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3409 if (oi
->address
->family
== AF_INET
)
3412 &oi
->address
->u
.prefix4
)) {
3413 /* to make it easier later */
3414 SET_FLAG(lsa
->flags
,
3416 return IS_LSA_SELF(lsa
);
3420 return IS_LSA_SELF(lsa
);
3423 /* Get unique Link State ID. */
3424 struct in_addr
ospf_lsa_unique_id(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
3425 u_char type
, struct prefix_ipv4
*p
)
3427 struct ospf_lsa
*lsa
;
3428 struct in_addr mask
, id
;
3432 /* Check existence of LSA instance. */
3433 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, ospf
->router_id
);
3435 struct as_external_lsa
*al
=
3436 (struct as_external_lsa
*)lsa
->data
;
3437 if (ip_masklen(al
->mask
) == p
->prefixlen
) {
3438 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3440 "ospf_lsa_unique_id(): "
3441 "Can't get Link State ID for %s/%d",
3442 inet_ntoa(p
->prefix
), p
->prefixlen
);
3443 /* id.s_addr = 0; */
3444 id
.s_addr
= 0xffffffff;
3447 /* Masklen differs, then apply wildcard mask to Link State ID.
3450 masklen2ip(p
->prefixlen
, &mask
);
3452 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3453 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
,
3456 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3458 "ospf_lsa_unique_id(): "
3459 "Can't get Link State ID for %s/%d",
3460 inet_ntoa(p
->prefix
),
3462 /* id.s_addr = 0; */
3463 id
.s_addr
= 0xffffffff;
3473 #define LSA_ACTION_FLOOD_AREA 1
3474 #define LSA_ACTION_FLUSH_AREA 2
3478 struct ospf_area
*area
;
3479 struct ospf_lsa
*lsa
;
3482 static int ospf_lsa_action(struct thread
*t
)
3484 struct lsa_action
*data
;
3486 data
= THREAD_ARG(t
);
3488 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3489 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3492 switch (data
->action
) {
3493 case LSA_ACTION_FLOOD_AREA
:
3494 ospf_flood_through_area(data
->area
, NULL
, data
->lsa
);
3496 case LSA_ACTION_FLUSH_AREA
:
3497 ospf_lsa_flush_area(data
->lsa
, data
->area
);
3501 ospf_lsa_unlock(&data
->lsa
); /* Message */
3502 XFREE(MTYPE_OSPF_MESSAGE
, data
);
3506 void ospf_schedule_lsa_flood_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3508 struct lsa_action
*data
;
3510 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3511 data
->action
= LSA_ACTION_FLOOD_AREA
;
3513 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flood area */
3515 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3518 void ospf_schedule_lsa_flush_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3520 struct lsa_action
*data
;
3522 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3523 data
->action
= LSA_ACTION_FLUSH_AREA
;
3525 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flush area */
3527 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3531 /* LSA Refreshment functions. */
3532 struct ospf_lsa
*ospf_lsa_refresh(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3534 struct external_info
*ei
;
3535 struct ospf_lsa
*new = NULL
;
3536 assert(CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF
));
3537 assert(IS_LSA_SELF(lsa
));
3538 assert(lsa
->lock
> 0);
3540 switch (lsa
->data
->type
) {
3541 /* Router and Network LSAs are processed differently. */
3542 case OSPF_ROUTER_LSA
:
3543 new = ospf_router_lsa_refresh(lsa
);
3545 case OSPF_NETWORK_LSA
:
3546 new = ospf_network_lsa_refresh(lsa
);
3548 case OSPF_SUMMARY_LSA
:
3549 new = ospf_summary_lsa_refresh(ospf
, lsa
);
3551 case OSPF_ASBR_SUMMARY_LSA
:
3552 new = ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
3554 case OSPF_AS_EXTERNAL_LSA
:
3555 /* Translated from NSSA Type-5s are refreshed when
3556 * from refresh of Type-7 - do not refresh these directly.
3558 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3560 ei
= ospf_external_info_check(ospf
, lsa
);
3562 new = ospf_external_lsa_refresh(ospf
, lsa
, ei
,
3565 ospf_lsa_flush_as(ospf
, lsa
);
3567 case OSPF_OPAQUE_LINK_LSA
:
3568 case OSPF_OPAQUE_AREA_LSA
:
3569 case OSPF_OPAQUE_AS_LSA
:
3570 new = ospf_opaque_lsa_refresh(lsa
);
3578 void ospf_refresher_register_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3580 u_int16_t index
, current_index
;
3582 assert(lsa
->lock
> 0);
3583 assert(IS_LSA_SELF(lsa
));
3585 if (lsa
->refresh_list
< 0) {
3588 OSPF_LS_REFRESH_TIME
- (2 * OSPF_LS_REFRESH_JITTER
);
3589 int max_delay
= OSPF_LS_REFRESH_TIME
- OSPF_LS_REFRESH_JITTER
;
3591 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3593 * 1800s. Use jitter so that we send the LSA sometime between
3597 delay
= (random() % (max_delay
- min_delay
)) + min_delay
;
3599 current_index
= ospf
->lsa_refresh_queue
.index
3600 + (monotime(NULL
) - ospf
->lsa_refresher_started
)
3601 / OSPF_LSA_REFRESHER_GRANULARITY
;
3603 index
= (current_index
+ delay
/ OSPF_LSA_REFRESHER_GRANULARITY
)
3604 % (OSPF_LSA_REFRESHER_SLOTS
);
3606 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3608 "LSA[Refresh:Type%d:%s]: age %d, added to index %d",
3609 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
3610 LS_AGE(lsa
), index
);
3612 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3613 ospf
->lsa_refresh_queue
.qs
[index
] = list_new();
3615 listnode_add(ospf
->lsa_refresh_queue
.qs
[index
],
3616 ospf_lsa_lock(lsa
)); /* lsa_refresh_queue */
3617 lsa
->refresh_list
= index
;
3619 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3621 "LSA[Refresh:Type%d:%s]: ospf_refresher_register_lsa(): "
3622 "setting refresh_list on lsa %p (slod %d)",
3623 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
3624 (void *)lsa
, index
);
3628 void ospf_refresher_unregister_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3630 assert(lsa
->lock
> 0);
3631 assert(IS_LSA_SELF(lsa
));
3632 if (lsa
->refresh_list
>= 0) {
3633 struct list
*refresh_list
=
3634 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3635 listnode_delete(refresh_list
, lsa
);
3636 if (!listcount(refresh_list
)) {
3637 list_delete_and_null(&refresh_list
);
3638 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3640 ospf_lsa_unlock(&lsa
); /* lsa_refresh_queue */
3641 lsa
->refresh_list
= -1;
3645 int ospf_lsa_refresh_walker(struct thread
*t
)
3647 struct list
*refresh_list
;
3648 struct listnode
*node
, *nnode
;
3649 struct ospf
*ospf
= THREAD_ARG(t
);
3650 struct ospf_lsa
*lsa
;
3652 struct list
*lsa_to_refresh
= list_new();
3654 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3655 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3658 i
= ospf
->lsa_refresh_queue
.index
;
3660 /* Note: if clock has jumped backwards, then time change could be
3662 so we are careful to cast the expression to unsigned before taking
3664 ospf
->lsa_refresh_queue
.index
=
3665 ((unsigned long)(ospf
->lsa_refresh_queue
.index
3667 - ospf
->lsa_refresher_started
)
3668 / OSPF_LSA_REFRESHER_GRANULARITY
))
3669 % OSPF_LSA_REFRESHER_SLOTS
;
3671 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3673 "LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3674 ospf
->lsa_refresh_queue
.index
);
3676 for (; i
!= ospf
->lsa_refresh_queue
.index
;
3677 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
) {
3678 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3680 "LSA[Refresh]: ospf_lsa_refresh_walker(): "
3684 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3688 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3691 for (ALL_LIST_ELEMENTS(refresh_list
, node
, nnode
,
3693 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3695 "LSA[Refresh:Type%d:%s]: ospf_lsa_refresh_walker(): "
3696 "refresh lsa %p (slot %d)",
3698 inet_ntoa(lsa
->data
->id
),
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_delete_and_null(&refresh_list
);
3710 ospf
->t_lsa_refresher
= NULL
;
3711 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
3712 ospf
->lsa_refresh_interval
, &ospf
->t_lsa_refresher
);
3713 ospf
->lsa_refresher_started
= monotime(NULL
);
3715 for (ALL_LIST_ELEMENTS(lsa_to_refresh
, node
, nnode
, lsa
)) {
3716 ospf_lsa_refresh(ospf
, lsa
);
3717 assert(lsa
->lock
> 0);
3719 &lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3722 list_delete_and_null(&lsa_to_refresh
);
3724 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3725 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");