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() */
38 #include "ospfd/ospfd.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_ism.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_lsa.h"
43 #include "ospfd/ospf_lsdb.h"
44 #include "ospfd/ospf_neighbor.h"
45 #include "ospfd/ospf_nsm.h"
46 #include "ospfd/ospf_flood.h"
47 #include "ospfd/ospf_packet.h"
48 #include "ospfd/ospf_spf.h"
49 #include "ospfd/ospf_dump.h"
50 #include "ospfd/ospf_route.h"
51 #include "ospfd/ospf_ase.h"
52 #include "ospfd/ospf_zebra.h"
53 #include "ospfd/ospf_abr.h"
54 #include "ospfd/ospf_errors.h"
56 uint32_t get_metric(uint8_t *metric
)
60 m
= (m
<< 8) + metric
[1];
61 m
= (m
<< 8) + metric
[2];
66 struct timeval
int2tv(int a
)
76 struct timeval
msec2tv(int a
)
80 ret
.tv_sec
= a
/ 1000;
81 ret
.tv_usec
= (a
% 1000) * 1000;
86 int ospf_lsa_refresh_delay(struct ospf_lsa
*lsa
)
91 if (monotime_since(&lsa
->tv_orig
, &delta
)
92 < OSPF_MIN_LS_INTERVAL
* 1000LL) {
93 struct timeval minv
= msec2tv(OSPF_MIN_LS_INTERVAL
);
94 timersub(&minv
, &delta
, &minv
);
96 /* TBD: remove padding to full sec, return timeval instead */
97 delay
= minv
.tv_sec
+ !!minv
.tv_usec
;
99 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
101 "LSA[Type%d:%pI4]: Refresh timer delay %d seconds",
102 lsa
->data
->type
, &lsa
->data
->id
,
112 int get_age(struct ospf_lsa
*lsa
)
116 monotime_since(&lsa
->tv_recv
, &rel
);
117 return ntohs(lsa
->data
->ls_age
) + rel
.tv_sec
;
121 /* Fletcher Checksum -- Refer to RFC1008. */
123 /* All the offsets are zero-based. The offsets in the RFC1008 are
125 uint16_t ospf_lsa_checksum(struct lsa_header
*lsa
)
127 uint8_t *buffer
= &lsa
->options
;
128 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
130 /* Skip the AGE field */
131 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
133 /* Checksum offset starts from "options" field, not the beginning of the
134 lsa_header struct. The offset is 14, rather than 16. */
135 int checksum_offset
= (uint8_t *)&lsa
->checksum
- buffer
;
137 return fletcher_checksum(buffer
, len
, checksum_offset
);
140 int ospf_lsa_checksum_valid(struct lsa_header
*lsa
)
142 uint8_t *buffer
= &lsa
->options
;
143 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
145 /* Skip the AGE field */
146 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
148 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
153 /* Create OSPF LSA. */
154 struct ospf_lsa
*ospf_lsa_new(void)
156 struct ospf_lsa
*new;
158 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
162 new->retransmit_counter
= 0;
163 monotime(&new->tv_recv
);
164 new->tv_orig
= new->tv_recv
;
165 new->refresh_list
= -1;
166 new->vrf_id
= VRF_DEFAULT
;
167 new->to_be_acknowledged
= 0;
172 struct ospf_lsa
*ospf_lsa_new_and_data(size_t size
)
174 struct ospf_lsa
*new;
176 new = ospf_lsa_new();
177 new->data
= ospf_lsa_data_new(size
);
182 /* Duplicate OSPF LSA. */
183 struct ospf_lsa
*ospf_lsa_dup(struct ospf_lsa
*lsa
)
185 struct ospf_lsa
*new;
190 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
192 memcpy(new, lsa
, sizeof(struct ospf_lsa
));
193 UNSET_FLAG(new->flags
, OSPF_LSA_DISCARD
);
195 new->retransmit_counter
= 0;
196 new->data
= ospf_lsa_data_dup(lsa
->data
);
198 /* kevinm: Clear the refresh_list, otherwise there are going
199 to be problems when we try to remove the LSA from the
200 queue (which it's not a member of.)
201 XXX: Should we add the LSA to the refresh_list queue? */
202 new->refresh_list
= -1;
204 if (IS_DEBUG_OSPF(lsa
, LSA
))
205 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa
,
212 void ospf_lsa_free(struct ospf_lsa
*lsa
)
214 assert(lsa
->lock
== 0);
216 if (IS_DEBUG_OSPF(lsa
, LSA
))
217 zlog_debug("LSA: freed %p", (void *)lsa
);
219 /* Delete LSA data. */
220 if (lsa
->data
!= NULL
)
221 ospf_lsa_data_free(lsa
->data
);
223 assert(lsa
->refresh_list
< 0);
225 memset(lsa
, 0, sizeof(struct ospf_lsa
));
226 XFREE(MTYPE_OSPF_LSA
, lsa
);
230 struct ospf_lsa
*ospf_lsa_lock(struct ospf_lsa
*lsa
)
237 void ospf_lsa_unlock(struct ospf_lsa
**lsa
)
239 /* This is sanity check. */
245 assert((*lsa
)->lock
>= 0);
247 if ((*lsa
)->lock
== 0) {
248 assert(CHECK_FLAG((*lsa
)->flags
, OSPF_LSA_DISCARD
));
254 /* Check discard flag. */
255 void ospf_lsa_discard(struct ospf_lsa
*lsa
)
257 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
258 SET_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
);
259 ospf_lsa_unlock(&lsa
);
263 /* Create LSA data. */
264 struct lsa_header
*ospf_lsa_data_new(size_t size
)
266 return XCALLOC(MTYPE_OSPF_LSA_DATA
, size
);
269 /* Duplicate LSA data. */
270 struct lsa_header
*ospf_lsa_data_dup(struct lsa_header
*lsah
)
272 struct lsa_header
*new;
274 new = ospf_lsa_data_new(ntohs(lsah
->length
));
275 memcpy(new, lsah
, ntohs(lsah
->length
));
281 void ospf_lsa_data_free(struct lsa_header
*lsah
)
283 if (IS_DEBUG_OSPF(lsa
, LSA
))
284 zlog_debug("LSA[Type%d:%pI4]: data freed %p", lsah
->type
,
285 &lsah
->id
, (void *)lsah
);
287 XFREE(MTYPE_OSPF_LSA_DATA
, lsah
);
291 /* LSA general functions. */
293 const char *dump_lsa_key(struct ospf_lsa
*lsa
)
295 static char buf
[sizeof("Type255,id(255.255.255.255),ar(255.255.255.255)")+1];
296 struct lsa_header
*lsah
;
298 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
) {
299 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
300 inet_ntop(AF_INET
, &lsah
->id
, id
, sizeof(id
));
301 inet_ntop(AF_INET
, &lsah
->adv_router
, ar
, sizeof(ar
));
303 snprintf(buf
, sizeof(buf
), "Type%d,id(%s),ar(%s)", lsah
->type
,
306 strlcpy(buf
, "NULL", sizeof(buf
));
311 uint32_t lsa_seqnum_increment(struct ospf_lsa
*lsa
)
315 seqnum
= ntohl(lsa
->data
->ls_seqnum
) + 1;
317 return htonl(seqnum
);
320 void lsa_header_set(struct stream
*s
, uint8_t options
, uint8_t type
,
321 struct in_addr id
, struct in_addr router_id
)
323 struct lsa_header
*lsah
;
325 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
327 lsah
->ls_age
= htons(OSPF_LSA_INITIAL_AGE
);
328 lsah
->options
= options
;
331 lsah
->adv_router
= router_id
;
332 lsah
->ls_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
334 stream_forward_endp(s
, OSPF_LSA_HEADER_SIZE
);
338 /* router-LSA related functions. */
339 /* Get router-LSA flags. */
340 uint8_t router_lsa_flags(struct ospf_area
*area
)
344 flags
= area
->ospf
->flags
;
346 /* Set virtual link flag. */
347 if (ospf_full_virtual_nbrs(area
))
348 SET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
350 /* Just sanity check */
351 UNSET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
353 /* Set Shortcut ABR behabiour flag. */
354 UNSET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
355 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
356 if (!OSPF_IS_AREA_BACKBONE(area
))
357 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
358 && area
->ospf
->backbone
== NULL
)
359 || area
->shortcut_configured
360 == OSPF_SHORTCUT_ENABLE
)
361 SET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
363 /* ASBR can't exit in stub area. */
364 if (area
->external_routing
== OSPF_AREA_STUB
)
365 UNSET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
366 /* If ASBR set External flag */
367 else if (IS_OSPF_ASBR(area
->ospf
))
368 SET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
370 /* Set ABR dependent flags */
371 if (IS_OSPF_ABR(area
->ospf
)) {
372 SET_FLAG(flags
, ROUTER_LSA_BORDER
);
373 /* If Area is NSSA and we are both ABR and unconditional
375 * set Nt bit to inform other routers.
377 if ((area
->external_routing
== OSPF_AREA_NSSA
)
378 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
379 SET_FLAG(flags
, ROUTER_LSA_NT
);
384 /* Lookup neighbor other than myself.
385 And check neighbor count,
386 Point-to-Point link must have only 1 neighbor. */
387 struct ospf_neighbor
*ospf_nbr_lookup_ptop(struct ospf_interface
*oi
)
389 struct ospf_neighbor
*nbr
= NULL
;
390 struct route_node
*rn
;
392 /* Search neighbor, there must be one of two nbrs. */
393 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
394 if ((nbr
= rn
->info
))
395 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
396 &oi
->ospf
->router_id
))
397 if (nbr
->state
== NSM_Full
) {
398 route_unlock_node(rn
);
402 /* PtoP link must have only 1 neighbor. */
403 if (ospf_nbr_count(oi
, 0) > 1)
404 flog_warn(EC_OSPF_PTP_NEIGHBOR
,
405 "Point-to-Point link has more than 1 neighobrs.");
410 /* Determine cost of link, taking RFC3137 stub-router support into
413 static uint16_t ospf_link_cost(struct ospf_interface
*oi
)
415 /* RFC3137 stub router support */
416 if (!CHECK_FLAG(oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
417 return oi
->output_cost
;
419 return OSPF_OUTPUT_COST_INFINITE
;
422 /* Set a link information. */
423 char link_info_set(struct stream
**s
, struct in_addr id
, struct in_addr data
,
424 uint8_t type
, uint8_t tos
, uint16_t cost
)
426 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
427 * vast majority of cases. Some rare routers with lots of links need
429 * we try accomodate those here.
431 if (STREAM_WRITEABLE(*s
) < OSPF_ROUTER_LSA_LINK_SIZE
) {
432 size_t ret
= OSPF_MAX_LSA_SIZE
;
434 /* Can we enlarge the stream still? */
435 if (STREAM_SIZE(*s
) == OSPF_MAX_LSA_SIZE
) {
436 /* we futz the size here for simplicity, really we need
439 * IP Header - (sizeof(struct ip))
440 * OSPF Header - OSPF_HEADER_SIZE
441 * LSA Header - OSPF_LSA_HEADER_SIZE
442 * MD5 auth data, if MD5 is configured -
443 * OSPF_AUTH_MD5_SIZE.
445 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
447 ret
= stream_resize_inplace(
448 s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
451 if (ret
== OSPF_MAX_LSA_SIZE
) {
454 "%s: Out of space in LSA stream, left %zd, size %zd",
455 __func__
, STREAM_WRITEABLE(*s
),
461 /* TOS based routing is not supported. */
462 stream_put_ipv4(*s
, id
.s_addr
); /* Link ID. */
463 stream_put_ipv4(*s
, data
.s_addr
); /* Link Data. */
464 stream_putc(*s
, type
); /* Link Type. */
465 stream_putc(*s
, tos
); /* TOS = 0. */
466 stream_putw(*s
, cost
); /* Link Cost. */
471 /* Describe Point-to-Point link (Section 12.4.1.1). */
473 /* Note: If the interface is configured as point-to-point dmvpn then the other
474 * end of link is dmvpn hub with point-to-multipoint ospf network type. The
475 * hub then expects this router to populate the stub network and also Link Data
476 * Field set to IP Address and not MIB-II ifIndex
478 static int lsa_link_ptop_set(struct stream
**s
, struct ospf_interface
*oi
)
481 struct ospf_neighbor
*nbr
;
482 struct in_addr id
, mask
, data
;
483 uint16_t cost
= ospf_link_cost(oi
);
485 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
486 zlog_debug("LSA[Type1]: Set link Point-to-Point");
488 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
489 if (nbr
->state
== NSM_Full
) {
490 if (CHECK_FLAG(oi
->connected
->flags
,
491 ZEBRA_IFA_UNNUMBERED
)
493 /* For unnumbered point-to-point networks, the
495 should specify the interface's MIB-II ifIndex
497 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
498 links
+= link_info_set(
499 s
, nbr
->router_id
, data
,
500 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
502 links
+= link_info_set(
504 oi
->address
->u
.prefix4
,
505 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
509 /* no need for a stub link for unnumbered interfaces */
511 || !CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)) {
512 /* Regardless of the state of the neighboring router, we must
513 add a Type 3 link (stub network).
514 N.B. Options 1 & 2 share basically the same logic. */
515 masklen2ip(oi
->address
->prefixlen
, &mask
);
516 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
518 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
525 /* Describe Broadcast Link. */
526 static int lsa_link_broadcast_set(struct stream
**s
, struct ospf_interface
*oi
)
528 struct ospf_neighbor
*dr
;
529 struct in_addr id
, mask
;
530 uint16_t cost
= ospf_link_cost(oi
);
532 /* Describe Type 3 Link. */
533 if (oi
->state
== ISM_Waiting
) {
534 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
536 "LSA[Type1]: Interface %s is in state Waiting. Adding stub interface",
538 masklen2ip(oi
->address
->prefixlen
, &mask
);
539 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
540 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
544 dr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &DR(oi
));
545 /* Describe Type 2 link. */
546 if (dr
&& (dr
->state
== NSM_Full
547 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
)))
548 && ospf_nbr_count(oi
, NSM_Full
) > 0) {
549 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
551 "LSA[Type1]: Interface %s has a DR. Adding transit interface",
553 return link_info_set(s
, DR(oi
), oi
->address
->u
.prefix4
,
554 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
556 /* Describe type 3 link. */
558 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
560 "LSA[Type1]: Interface %s has no DR. Adding stub interface",
562 masklen2ip(oi
->address
->prefixlen
, &mask
);
563 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
564 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
569 static int lsa_link_loopback_set(struct stream
**s
, struct ospf_interface
*oi
)
571 struct in_addr id
, mask
;
573 /* Describe Type 3 Link. */
574 if (oi
->state
!= ISM_Loopback
)
577 mask
.s_addr
= 0xffffffff;
578 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
579 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
582 /* Describe Virtual Link. */
583 static int lsa_link_virtuallink_set(struct stream
**s
,
584 struct ospf_interface
*oi
)
586 struct ospf_neighbor
*nbr
;
587 uint16_t cost
= ospf_link_cost(oi
);
589 if (oi
->state
== ISM_PointToPoint
)
590 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
591 if (nbr
->state
== NSM_Full
) {
592 return link_info_set(s
, nbr
->router_id
,
593 oi
->address
->u
.prefix4
,
594 LSA_LINK_TYPE_VIRTUALLINK
,
601 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
603 /* this function add for support point-to-multipoint ,see rfc2328
605 /* from "edward rrr" <edward_rrr@hotmail.com>
606 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
607 static int lsa_link_ptomp_set(struct stream
**s
, struct ospf_interface
*oi
)
610 struct route_node
*rn
;
611 struct ospf_neighbor
*nbr
= NULL
;
612 struct in_addr id
, mask
;
613 uint16_t cost
= ospf_link_cost(oi
);
615 mask
.s_addr
= 0xffffffff;
616 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
617 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
619 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
620 zlog_debug("PointToMultipoint: running ptomultip_set");
622 /* Search neighbor, */
623 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
624 if ((nbr
= rn
->info
) != NULL
)
626 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
627 &oi
->ospf
->router_id
))
628 if (nbr
->state
== NSM_Full
)
631 links
+= link_info_set(
633 oi
->address
->u
.prefix4
,
634 LSA_LINK_TYPE_POINTOPOINT
, 0,
636 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
638 "PointToMultipoint: set link to %pI4",
639 &oi
->address
->u
.prefix4
);
645 /* Set router-LSA link information. */
646 static int router_lsa_link_set(struct stream
**s
, struct ospf_area
*area
)
648 struct listnode
*node
;
649 struct ospf_interface
*oi
;
652 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
653 struct interface
*ifp
= oi
->ifp
;
655 /* Check interface is up, OSPF is enable. */
656 if (if_is_operative(ifp
)) {
657 if (oi
->state
!= ISM_Down
) {
658 oi
->lsa_pos_beg
= links
;
659 /* Describe each link. */
661 case OSPF_IFTYPE_POINTOPOINT
:
662 links
+= lsa_link_ptop_set(s
, oi
);
664 case OSPF_IFTYPE_BROADCAST
:
665 links
+= lsa_link_broadcast_set(s
, oi
);
667 case OSPF_IFTYPE_NBMA
:
668 links
+= lsa_link_nbma_set(s
, oi
);
670 case OSPF_IFTYPE_POINTOMULTIPOINT
:
671 links
+= lsa_link_ptomp_set(s
, oi
);
673 case OSPF_IFTYPE_VIRTUALLINK
:
675 lsa_link_virtuallink_set(s
, oi
);
677 case OSPF_IFTYPE_LOOPBACK
:
678 links
+= lsa_link_loopback_set(s
, oi
);
680 oi
->lsa_pos_end
= links
;
688 /* Set router-LSA body. */
689 void ospf_router_lsa_body_set(struct stream
**s
, struct ospf_area
*area
)
695 stream_putc(*s
, router_lsa_flags(area
));
697 /* Set Zero fields. */
700 /* Keep pointer to # links. */
701 putp
= stream_get_endp(*s
);
706 /* Set all link information. */
707 cnt
= router_lsa_link_set(s
, area
);
709 /* Set # of links here. */
710 stream_putw_at(*s
, putp
, cnt
);
713 static int ospf_stub_router_timer(struct thread
*t
)
715 struct ospf_area
*area
= THREAD_ARG(t
);
717 area
->t_stub_router
= NULL
;
719 SET_FLAG(area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
721 /* clear stub route state and generate router-lsa refresh, don't
722 * clobber an administratively set stub-router state though.
724 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
727 UNSET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
729 ospf_router_lsa_update_area(area
);
734 static void ospf_stub_router_check(struct ospf_area
*area
)
736 /* area must either be administratively configured to be stub
737 * or startup-time stub-router must be configured and we must in a
741 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
)) {
742 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
746 /* not admin-stubbed, check whether startup stubbing is configured and
747 * whether it's not been done yet
749 if (CHECK_FLAG(area
->stub_router_state
,
750 OSPF_AREA_WAS_START_STUB_ROUTED
))
753 if (area
->ospf
->stub_router_startup_time
754 == OSPF_STUB_ROUTER_UNCONFIGURED
) {
755 /* stub-router is hence done forever for this area, even if
757 * tries configure it (take effect next restart).
759 SET_FLAG(area
->stub_router_state
,
760 OSPF_AREA_WAS_START_STUB_ROUTED
);
764 /* startup stub-router configured and not yet done */
765 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
767 OSPF_AREA_TIMER_ON(area
->t_stub_router
, ospf_stub_router_timer
,
768 area
->ospf
->stub_router_startup_time
);
771 /* Create new router-LSA. */
772 static struct ospf_lsa
*ospf_router_lsa_new(struct ospf_area
*area
)
774 struct ospf
*ospf
= area
->ospf
;
776 struct lsa_header
*lsah
;
777 struct ospf_lsa
*new;
780 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
781 zlog_debug("LSA[Type1]: Create router-LSA instance");
783 /* check whether stub-router is desired, and if this is the first
786 ospf_stub_router_check(area
);
788 /* Create a stream for LSA. */
789 s
= stream_new(OSPF_MAX_LSA_SIZE
);
790 /* Set LSA common header fields. */
791 lsa_header_set(s
, LSA_OPTIONS_GET(area
) | LSA_OPTIONS_NSSA_GET(area
),
792 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
794 /* Set router-LSA body fields. */
795 ospf_router_lsa_body_set(&s
, area
);
798 length
= stream_get_endp(s
);
799 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
800 lsah
->length
= htons(length
);
802 /* Now, create OSPF LSA instance. */
803 new = ospf_lsa_new_and_data(length
);
806 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
807 new->vrf_id
= area
->ospf
->vrf_id
;
809 /* Copy LSA data to store, discard stream. */
810 memcpy(new->data
, lsah
, length
);
816 /* Originate Router-LSA. */
817 static struct ospf_lsa
*ospf_router_lsa_originate(struct ospf_area
*area
)
819 struct ospf_lsa
*new;
821 /* Create new router-LSA instance. */
822 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
823 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
828 if (new->data
->adv_router
.s_addr
== INADDR_ANY
) {
829 if (IS_DEBUG_OSPF_EVENT
)
830 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
831 ospf_lsa_discard(new);
835 /* Install LSA to LSDB. */
836 new = ospf_lsa_install(area
->ospf
, NULL
, new);
838 /* Update LSA origination count. */
839 area
->ospf
->lsa_originate_count
++;
841 /* Flooding new LSA through area. */
842 ospf_flood_through_area(area
, NULL
, new);
844 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
845 zlog_debug("LSA[Type%d:%pI4]: Originate router-LSA %p",
846 new->data
->type
, &new->data
->id
,
848 ospf_lsa_header_dump(new->data
);
854 /* Refresh router-LSA. */
855 static struct ospf_lsa
*ospf_router_lsa_refresh(struct ospf_lsa
*lsa
)
857 struct ospf_area
*area
= lsa
->area
;
858 struct ospf_lsa
*new;
863 /* Delete LSA from neighbor retransmit-list. */
864 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
866 /* Unregister LSA from refresh-list */
867 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
869 /* Create new router-LSA instance. */
870 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
871 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
875 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
877 ospf_lsa_install(area
->ospf
, NULL
, new);
879 /* Flood LSA through area. */
880 ospf_flood_through_area(area
, NULL
, new);
883 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
884 zlog_debug("LSA[Type%d:%pI4]: router-LSA refresh",
885 new->data
->type
, &new->data
->id
);
886 ospf_lsa_header_dump(new->data
);
892 int ospf_router_lsa_update_area(struct ospf_area
*area
)
894 if (IS_DEBUG_OSPF_EVENT
)
895 zlog_debug("[router-LSA]: (router-LSA area update)");
897 /* Now refresh router-LSA. */
898 if (area
->router_lsa_self
)
899 ospf_lsa_refresh(area
->ospf
, area
->router_lsa_self
);
900 /* Newly originate router-LSA. */
902 ospf_router_lsa_originate(area
);
907 int ospf_router_lsa_update(struct ospf
*ospf
)
909 struct listnode
*node
, *nnode
;
910 struct ospf_area
*area
;
912 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
913 zlog_debug("Timer[router-LSA Update]: (timer expire)");
915 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
916 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
917 struct router_lsa
*rl
;
918 const char *area_str
;
920 /* Keep Area ID string. */
921 area_str
= AREA_NAME(area
);
923 /* If LSA not exist in this Area, originate new. */
925 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
927 "LSA[Type1]: Create router-LSA for Area %s",
930 ospf_router_lsa_originate(area
);
932 /* If router-ID is changed, Link ID must change.
933 First flush old LSA, then originate new. */
934 else if (!IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
)) {
935 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
937 "LSA[Type%d:%pI4]: Refresh router-LSA for Area %s",
939 &lsa
->data
->id
, area_str
);
940 ospf_refresher_unregister_lsa(ospf
, lsa
);
941 ospf_lsa_flush_area(lsa
, area
);
942 ospf_lsa_unlock(&area
->router_lsa_self
);
943 area
->router_lsa_self
= NULL
;
945 /* Refresh router-LSA, (not install) and flood through
947 ospf_router_lsa_update_area(area
);
949 rl
= (struct router_lsa
*)lsa
->data
;
950 /* Refresh router-LSA, (not install) and flood through
952 if (rl
->flags
!= ospf
->flags
)
953 ospf_router_lsa_update_area(area
);
961 /* network-LSA related functions. */
962 /* Originate Network-LSA. */
963 static void ospf_network_lsa_body_set(struct stream
*s
,
964 struct ospf_interface
*oi
)
967 struct route_node
*rn
;
968 struct ospf_neighbor
*nbr
;
970 masklen2ip(oi
->address
->prefixlen
, &mask
);
971 stream_put_ipv4(s
, mask
.s_addr
);
973 /* The network-LSA lists those routers that are fully adjacent to
974 the Designated Router; each fully adjacent router is identified by
975 its OSPF Router ID. The Designated Router includes itself in this
976 list. RFC2328, Section 12.4.2 */
978 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
979 if ((nbr
= rn
->info
) != NULL
)
980 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
981 stream_put_ipv4(s
, nbr
->router_id
.s_addr
);
984 static struct ospf_lsa
*ospf_network_lsa_new(struct ospf_interface
*oi
)
987 struct ospf_lsa
*new;
988 struct lsa_header
*lsah
;
989 struct ospf_if_params
*oip
;
992 /* If there are no neighbours on this network (the net is stub),
993 the router does not originate network-LSA (see RFC 12.4.2) */
994 if (oi
->full_nbrs
== 0)
997 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
998 zlog_debug("LSA[Type2]: Create network-LSA instance");
1000 /* Create new stream for LSA. */
1001 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1002 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1004 lsa_header_set(s
, (OPTIONS(oi
) | LSA_OPTIONS_GET(oi
->area
)),
1005 OSPF_NETWORK_LSA
, DR(oi
), oi
->ospf
->router_id
);
1007 /* Set network-LSA body fields. */
1008 ospf_network_lsa_body_set(s
, oi
);
1011 length
= stream_get_endp(s
);
1012 lsah
->length
= htons(length
);
1014 /* Create OSPF LSA instance. */
1015 new = ospf_lsa_new_and_data(length
);
1017 new->area
= oi
->area
;
1018 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1019 new->vrf_id
= oi
->ospf
->vrf_id
;
1021 /* Copy LSA to store. */
1022 memcpy(new->data
, lsah
, length
);
1025 /* Remember prior network LSA sequence numbers, even if we stop
1026 * originating one for this oi, to try avoid re-originating LSAs with a
1027 * prior sequence number, and thus speed up adjency forming &
1030 if ((oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
))) {
1031 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1032 new->data
->ls_seqnum
= lsa_seqnum_increment(new);
1034 oip
= ospf_get_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1035 ospf_if_update_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1037 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1042 /* Originate network-LSA. */
1043 void ospf_network_lsa_update(struct ospf_interface
*oi
)
1045 struct ospf_lsa
*new;
1047 if (oi
->network_lsa_self
!= NULL
) {
1048 ospf_lsa_refresh(oi
->ospf
, oi
->network_lsa_self
);
1052 /* Create new network-LSA instance. */
1053 new = ospf_network_lsa_new(oi
);
1057 /* Install LSA to LSDB. */
1058 new = ospf_lsa_install(oi
->ospf
, oi
, new);
1060 /* Update LSA origination count. */
1061 oi
->ospf
->lsa_originate_count
++;
1063 /* Flooding new LSA through area. */
1064 ospf_flood_through_area(oi
->area
, NULL
, new);
1066 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1067 zlog_debug("LSA[Type%d:%pI4]: Originate network-LSA %p",
1068 new->data
->type
, &new->data
->id
,
1070 ospf_lsa_header_dump(new->data
);
1076 static struct ospf_lsa
*ospf_network_lsa_refresh(struct ospf_lsa
*lsa
)
1078 struct ospf_area
*area
= lsa
->area
;
1079 struct ospf_lsa
*new, *new2
;
1080 struct ospf_if_params
*oip
;
1081 struct ospf_interface
*oi
;
1085 /* Retrieve the oi for the network LSA */
1086 oi
= ospf_if_lookup_by_local_addr(area
->ospf
, NULL
, lsa
->data
->id
);
1088 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1090 "LSA[Type%d:%pI4]: network-LSA refresh: no oi found, ick, ignoring.",
1091 lsa
->data
->type
, &lsa
->data
->id
);
1092 ospf_lsa_header_dump(lsa
->data
);
1096 /* Delete LSA from neighbor retransmit-list. */
1097 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
1099 /* Unregister LSA from refresh-list */
1100 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
1102 /* Create new network-LSA instance. */
1103 new = ospf_network_lsa_new(oi
);
1107 oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1108 assert(oip
!= NULL
);
1109 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
=
1110 lsa_seqnum_increment(lsa
);
1112 new2
= ospf_lsa_install(area
->ospf
, oi
, new);
1114 assert(new2
== new);
1116 /* Flood LSA through aera. */
1117 ospf_flood_through_area(area
, NULL
, new);
1119 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1120 zlog_debug("LSA[Type%d:%pI4]: network-LSA refresh",
1121 new->data
->type
, &new->data
->id
);
1122 ospf_lsa_header_dump(new->data
);
1128 static void stream_put_ospf_metric(struct stream
*s
, uint32_t metric_value
)
1133 /* Put 0 metric. TOS metric is not supported. */
1134 metric
= htonl(metric_value
);
1135 mp
= (char *)&metric
;
1137 stream_put(s
, mp
, 3);
1140 /* summary-LSA related functions. */
1141 static void ospf_summary_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1144 struct in_addr mask
;
1146 masklen2ip(p
->prefixlen
, &mask
);
1148 /* Put Network Mask. */
1149 stream_put_ipv4(s
, mask
.s_addr
);
1152 stream_putc(s
, (uint8_t)0);
1155 stream_put_ospf_metric(s
, metric
);
1158 static struct ospf_lsa
*ospf_summary_lsa_new(struct ospf_area
*area
,
1159 struct prefix
*p
, uint32_t metric
,
1163 struct ospf_lsa
*new;
1164 struct lsa_header
*lsah
;
1167 if (id
.s_addr
== 0xffffffff) {
1168 /* Maybe Link State ID not available. */
1169 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1171 "LSA[Type%d]: Link ID not available, can't originate",
1176 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1177 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1179 /* Create new stream for LSA. */
1180 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1181 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1183 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_SUMMARY_LSA
, id
,
1184 area
->ospf
->router_id
);
1186 /* Set summary-LSA body fields. */
1187 ospf_summary_lsa_body_set(s
, p
, metric
);
1190 length
= stream_get_endp(s
);
1191 lsah
->length
= htons(length
);
1193 /* Create OSPF LSA instance. */
1194 new = ospf_lsa_new_and_data(length
);
1196 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1197 new->vrf_id
= area
->ospf
->vrf_id
;
1199 /* Copy LSA to store. */
1200 memcpy(new->data
, lsah
, length
);
1206 /* Originate Summary-LSA. */
1207 struct ospf_lsa
*ospf_summary_lsa_originate(struct prefix_ipv4
*p
,
1209 struct ospf_area
*area
)
1211 struct ospf_lsa
*new;
1214 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
);
1216 if (id
.s_addr
== 0xffffffff) {
1217 /* Maybe Link State ID not available. */
1218 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1220 "LSA[Type%d]: Link ID not available, can't originate",
1225 /* Create new summary-LSA instance. */
1226 if (!(new = ospf_summary_lsa_new(area
, (struct prefix
*)p
, metric
, id
)))
1229 /* Instlal LSA to LSDB. */
1230 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1232 /* Update LSA origination count. */
1233 area
->ospf
->lsa_originate_count
++;
1235 /* Flooding new LSA through area. */
1236 ospf_flood_through_area(area
, NULL
, new);
1238 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1239 zlog_debug("LSA[Type%d:%pI4]: Originate summary-LSA %p",
1240 new->data
->type
, &new->data
->id
,
1242 ospf_lsa_header_dump(new->data
);
1248 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
1249 struct ospf_lsa
*lsa
)
1251 struct ospf_lsa
*new;
1252 struct summary_lsa
*sl
;
1258 sl
= (struct summary_lsa
*)lsa
->data
;
1259 p
.prefixlen
= ip_masklen(sl
->mask
);
1260 new = ospf_summary_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1266 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1268 ospf_lsa_install(ospf
, NULL
, new);
1270 /* Flood LSA through AS. */
1271 ospf_flood_through_area(new->area
, NULL
, new);
1273 /* Debug logging. */
1274 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1275 zlog_debug("LSA[Type%d:%pI4]: summary-LSA refresh",
1276 new->data
->type
, &new->data
->id
);
1277 ospf_lsa_header_dump(new->data
);
1284 /* summary-ASBR-LSA related functions. */
1285 static void ospf_summary_asbr_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1288 /* Put Network Mask. */
1289 stream_put_ipv4(s
, (uint32_t)0);
1292 stream_putc(s
, (uint8_t)0);
1295 stream_put_ospf_metric(s
, metric
);
1298 static struct ospf_lsa
*ospf_summary_asbr_lsa_new(struct ospf_area
*area
,
1304 struct ospf_lsa
*new;
1305 struct lsa_header
*lsah
;
1308 if (id
.s_addr
== 0xffffffff) {
1309 /* Maybe Link State ID not available. */
1310 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1312 "LSA[Type%d]: Link ID not available, can't originate",
1313 OSPF_ASBR_SUMMARY_LSA
);
1317 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1318 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1320 /* Create new stream for LSA. */
1321 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1322 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1324 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_ASBR_SUMMARY_LSA
, id
,
1325 area
->ospf
->router_id
);
1327 /* Set summary-LSA body fields. */
1328 ospf_summary_asbr_lsa_body_set(s
, p
, metric
);
1331 length
= stream_get_endp(s
);
1332 lsah
->length
= htons(length
);
1334 /* Create OSPF LSA instance. */
1335 new = ospf_lsa_new_and_data(length
);
1337 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1338 new->vrf_id
= area
->ospf
->vrf_id
;
1340 /* Copy LSA to store. */
1341 memcpy(new->data
, lsah
, length
);
1347 /* Originate summary-ASBR-LSA. */
1348 struct ospf_lsa
*ospf_summary_asbr_lsa_originate(struct prefix_ipv4
*p
,
1350 struct ospf_area
*area
)
1352 struct ospf_lsa
*new;
1355 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_ASBR_SUMMARY_LSA
,
1358 if (id
.s_addr
== 0xffffffff) {
1359 /* Maybe Link State ID not available. */
1360 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1362 "LSA[Type%d]: Link ID not available, can't originate",
1363 OSPF_ASBR_SUMMARY_LSA
);
1367 /* Create new summary-LSA instance. */
1368 new = ospf_summary_asbr_lsa_new(area
, (struct prefix
*)p
, metric
, id
);
1372 /* Install LSA to LSDB. */
1373 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1375 /* Update LSA origination count. */
1376 area
->ospf
->lsa_originate_count
++;
1378 /* Flooding new LSA through area. */
1379 ospf_flood_through_area(area
, NULL
, new);
1381 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1382 zlog_debug("LSA[Type%d:%pI4]: Originate summary-ASBR-LSA %p",
1383 new->data
->type
, &new->data
->id
,
1385 ospf_lsa_header_dump(new->data
);
1391 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
1392 struct ospf_lsa
*lsa
)
1394 struct ospf_lsa
*new;
1395 struct summary_lsa
*sl
;
1401 sl
= (struct summary_lsa
*)lsa
->data
;
1402 p
.prefixlen
= ip_masklen(sl
->mask
);
1403 new = ospf_summary_asbr_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1408 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1410 ospf_lsa_install(ospf
, NULL
, new);
1412 /* Flood LSA through area. */
1413 ospf_flood_through_area(new->area
, NULL
, new);
1415 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1416 zlog_debug("LSA[Type%d:%pI4]: summary-ASBR-LSA refresh",
1417 new->data
->type
, &new->data
->id
);
1418 ospf_lsa_header_dump(new->data
);
1424 /* AS-external-LSA related functions. */
1426 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1427 is connected, else 0*/
1428 static struct in_addr
ospf_external_lsa_nexthop_get(struct ospf
*ospf
,
1429 struct in_addr nexthop
)
1433 struct listnode
*node
;
1434 struct ospf_interface
*oi
;
1438 if (!nexthop
.s_addr
)
1441 /* Check whether nexthop is covered by OSPF network. */
1442 nh
.family
= AF_INET
;
1443 nh
.u
.prefix4
= nexthop
;
1444 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1446 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1447 * better to make use of the per-ifp table of ois.
1449 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1450 if (if_is_operative(oi
->ifp
))
1451 if (oi
->address
->family
== AF_INET
)
1452 if (prefix_match(oi
->address
, &nh
))
1458 /* NSSA-external-LSA related functions. */
1460 /* Get 1st IP connection for Forward Addr */
1462 struct in_addr
ospf_get_ip_from_ifp(struct ospf_interface
*oi
)
1466 fwd
.s_addr
= INADDR_ANY
;
1468 if (if_is_operative(oi
->ifp
))
1469 return oi
->address
->u
.prefix4
;
1474 /* Get 1st IP connection for Forward Addr */
1475 struct in_addr
ospf_get_nssa_ip(struct ospf_area
*area
)
1478 struct in_addr best_default
;
1479 struct listnode
*node
;
1480 struct ospf_interface
*oi
;
1483 best_default
.s_addr
= 0;
1485 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->oiflist
, node
, oi
)) {
1486 if (if_is_operative(oi
->ifp
))
1487 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1489 && oi
->address
->family
== AF_INET
) {
1490 if (best_default
.s_addr
== INADDR_ANY
)
1492 oi
->address
->u
.prefix4
;
1493 if (oi
->area
== area
)
1494 return oi
->address
->u
.prefix4
;
1497 if (best_default
.s_addr
!= INADDR_ANY
)
1498 return best_default
;
1500 if (best_default
.s_addr
!= INADDR_ANY
)
1501 return best_default
;
1506 int metric_type(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1508 struct ospf_redist
*red
;
1510 red
= ospf_redist_lookup(ospf
, src
, instance
);
1512 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
1513 : red
->dmetric
.type
);
1516 int metric_value(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1518 struct ospf_redist
*red
;
1520 red
= ospf_redist_lookup(ospf
, src
, instance
);
1521 if (!red
|| red
->dmetric
.value
< 0) {
1522 if (src
== DEFAULT_ROUTE
) {
1523 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1524 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1526 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1527 } else if (ospf
->default_metric
< 0)
1528 return DEFAULT_DEFAULT_METRIC
;
1530 return ospf
->default_metric
;
1533 return red
->dmetric
.value
;
1536 /* Set AS-external-LSA body. */
1537 static void ospf_external_lsa_body_set(struct stream
*s
,
1538 struct external_info
*ei
,
1541 struct prefix_ipv4
*p
= &ei
->p
;
1542 struct in_addr mask
, fwd_addr
;
1546 unsigned short instance
;
1548 /* Put Network Mask. */
1549 masklen2ip(p
->prefixlen
, &mask
);
1550 stream_put_ipv4(s
, mask
.s_addr
);
1552 /* If prefix is default, specify DEFAULT_ROUTE. */
1553 type
= is_prefix_default(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1554 instance
= is_prefix_default(&ei
->p
) ? 0 : ei
->instance
;
1556 mtype
= (ROUTEMAP_METRIC_TYPE(ei
) != -1)
1557 ? ROUTEMAP_METRIC_TYPE(ei
)
1558 : metric_type(ospf
, type
, instance
);
1560 mvalue
= (ROUTEMAP_METRIC(ei
) != -1)
1561 ? ROUTEMAP_METRIC(ei
)
1562 : metric_value(ospf
, type
, instance
);
1564 /* Put type of external metric. */
1565 stream_putc(s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1567 /* Put 0 metric. TOS metric is not supported. */
1568 stream_put_ospf_metric(s
, mvalue
);
1570 /* Get forwarding address to nexthop if on the Connection List, else 0.
1572 fwd_addr
= ospf_external_lsa_nexthop_get(ospf
, ei
->nexthop
);
1574 /* Put forwarding address. */
1575 stream_put_ipv4(s
, fwd_addr
.s_addr
);
1578 stream_putl(s
, ei
->tag
);
1581 /* Create new external-LSA. */
1582 static struct ospf_lsa
*ospf_external_lsa_new(struct ospf
*ospf
,
1583 struct external_info
*ei
,
1584 struct in_addr
*old_id
)
1587 struct lsa_header
*lsah
;
1588 struct ospf_lsa
*new;
1593 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1595 "LSA[Type5]: External info is NULL, can't originate");
1599 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1600 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1602 /* If old Link State ID is specified, refresh LSA with same ID. */
1605 /* Get Link State with unique ID. */
1607 id
= ospf_lsa_unique_id(ospf
, ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
,
1609 if (id
.s_addr
== 0xffffffff) {
1610 /* Maybe Link State ID not available. */
1611 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1613 "LSA[Type5]: Link ID not available, can't originate");
1618 /* Create new stream for LSA. */
1619 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1620 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1622 /* Set LSA common header fields. */
1623 lsa_header_set(s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
, id
,
1626 /* Set AS-external-LSA body fields. */
1627 ospf_external_lsa_body_set(s
, ei
, ospf
);
1630 length
= stream_get_endp(s
);
1631 lsah
->length
= htons(length
);
1633 /* Now, create OSPF LSA instance. */
1634 new = ospf_lsa_new_and_data(length
);
1636 SET_FLAG(new->flags
,
1637 OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1638 new->vrf_id
= ospf
->vrf_id
;
1640 /* Copy LSA data to store, discard stream. */
1641 memcpy(new->data
, lsah
, length
);
1648 static void ospf_install_flood_nssa(struct ospf
*ospf
, struct ospf_lsa
*lsa
,
1649 struct external_info
*ei
)
1651 struct ospf_lsa
*new;
1652 struct as_external_lsa
*extlsa
;
1653 struct ospf_area
*area
;
1654 struct listnode
*node
, *nnode
;
1656 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1657 * which originated from an NSSA area. In which case it should not be
1658 * flooded back to NSSA areas.
1660 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1663 /* NSSA Originate or Refresh (If anyNSSA)
1665 LSA is self-originated. And just installed as Type-5.
1666 Additionally, install as Type-7 LSDB for every attached NSSA.
1668 P-Bit controls which ABR performs translation to outside world; If
1669 we are an ABR....do not set the P-bit, because we send the Type-5,
1670 not as the ABR Translator, but as the ASBR owner within the AS!
1672 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1673 elected ABR Translator will see the P-bit, Translate, and re-flood.
1675 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1676 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1678 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
1679 /* Don't install Type-7 LSA's into nonNSSA area */
1680 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1683 /* make lsa duplicate, lock=1 */
1684 new = ospf_lsa_dup(lsa
);
1686 new->data
->type
= OSPF_AS_NSSA_LSA
;
1688 /* set P-bit if not ABR */
1689 if (!IS_OSPF_ABR(ospf
)) {
1690 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1692 /* set non-zero FWD ADDR
1694 draft-ietf-ospf-nssa-update-09.txt
1696 if the network between the NSSA AS boundary router and
1698 adjacent AS is advertised into OSPF as an internal OSPF
1700 the forwarding address should be the next op address as
1702 currently done with type-5 LSAs. If the intervening
1704 not adversited into OSPF as an internal OSPF route and
1706 type-7 LSA's P-bit is set a forwarding address should be
1707 selected from one of the router's active OSPF interface
1709 which belong to the NSSA. If no such addresses exist,
1711 no type-7 LSA's with the P-bit set should originate from
1715 /* kevinm: not updating lsa anymore, just new */
1716 extlsa
= (struct as_external_lsa
*)(new->data
);
1718 if (extlsa
->e
[0].fwd_addr
.s_addr
== INADDR_ANY
)
1719 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(
1720 area
); /* this NSSA area in ifp */
1722 if (extlsa
->e
[0].fwd_addr
.s_addr
== INADDR_ANY
) {
1723 if (IS_DEBUG_OSPF_NSSA
)
1725 "LSA[Type-7]: Could not build FWD-ADDR");
1726 ospf_lsa_discard(new);
1731 /* install also as Type-7 */
1732 ospf_lsa_install(ospf
, NULL
,
1733 new); /* Remove Old, Lock New = 2 */
1735 /* will send each copy, lock=2+n */
1736 ospf_flood_through_as(
1737 ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1741 static struct ospf_lsa
*ospf_lsa_translated_nssa_new(struct ospf
*ospf
,
1742 struct ospf_lsa
*type7
)
1745 struct ospf_lsa
*new;
1746 struct as_external_lsa
*ext
, *extnew
;
1747 struct external_info ei
;
1749 ext
= (struct as_external_lsa
*)(type7
->data
);
1751 /* need external_info struct, fill in bare minimum */
1752 ei
.p
.family
= AF_INET
;
1753 ei
.p
.prefix
= type7
->data
->id
;
1754 ei
.p
.prefixlen
= ip_masklen(ext
->mask
);
1755 ei
.type
= ZEBRA_ROUTE_OSPF
;
1756 ei
.nexthop
= ext
->header
.adv_router
;
1757 ei
.route_map_set
.metric
= -1;
1758 ei
.route_map_set
.metric_type
= -1;
1762 if ((new = ospf_external_lsa_new(ospf
, &ei
, &type7
->data
->id
))
1764 if (IS_DEBUG_OSPF_NSSA
)
1766 "%s: Could not originate Translated Type-5 for %pI4",
1767 __func__
, &ei
.p
.prefix
);
1771 extnew
= (struct as_external_lsa
*)(new->data
);
1773 /* copy over Type-7 data to new */
1774 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1775 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1776 if (type7
->area
->suppress_fa
) {
1777 extnew
->e
[0].fwd_addr
.s_addr
= 0;
1778 if (IS_DEBUG_OSPF_NSSA
)
1780 "ospf_lsa_translated_nssa_new(): Suppress forwarding address for %pI4",
1783 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1784 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1786 /* add translated flag, checksum and lock new lsa */
1787 SET_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1788 new = ospf_lsa_lock(new);
1793 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1794 struct ospf_lsa
*ospf_translated_nssa_originate(struct ospf
*ospf
,
1795 struct ospf_lsa
*type7
,
1796 struct ospf_lsa
*type5
)
1798 struct ospf_lsa
*new;
1799 struct as_external_lsa
*extnew
;
1801 /* we cant use ospf_external_lsa_originate() as we need to set
1802 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1805 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1806 if (IS_DEBUG_OSPF_NSSA
)
1808 "%s: Could not translate Type-7, Id %pI4, to Type-5",
1809 __func__
, &type7
->data
->id
);
1813 extnew
= (struct as_external_lsa
*)new->data
;
1815 /* Update LSA sequence number from translated Type-5 LSA */
1817 new->data
->ls_seqnum
= lsa_seqnum_increment(type5
);
1819 if ((new = ospf_lsa_install(ospf
, NULL
, new)) == NULL
) {
1820 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
1821 "%s: Could not install LSA id %pI4", __func__
,
1826 if (IS_DEBUG_OSPF_NSSA
) {
1827 zlog_debug("%s: translated Type 7, installed", __func__
);
1828 ospf_lsa_header_dump(new->data
);
1829 zlog_debug(" Network mask: %d", ip_masklen(extnew
->mask
));
1830 zlog_debug(" Forward addr: %pI4",
1831 &extnew
->e
[0].fwd_addr
);
1834 ospf
->lsa_originate_count
++;
1835 ospf_flood_through_as(ospf
, NULL
, new);
1840 /* Refresh Translated from NSSA AS-external-LSA. */
1841 struct ospf_lsa
*ospf_translated_nssa_refresh(struct ospf
*ospf
,
1842 struct ospf_lsa
*type7
,
1843 struct ospf_lsa
*type5
)
1845 struct ospf_lsa
*new = NULL
;
1846 struct as_external_lsa
*extold
= NULL
;
1847 uint32_t ls_seqnum
= 0;
1849 /* Sanity checks. */
1850 assert(type7
|| type5
);
1851 if (!(type7
|| type5
))
1854 assert(type7
->data
);
1856 assert(type5
->data
);
1857 assert(ospf
->anyNSSA
);
1859 /* get required data according to what has been given */
1860 if (type7
&& type5
== NULL
) {
1861 /* find the translated Type-5 for this Type-7 */
1862 struct as_external_lsa
*ext
=
1863 (struct as_external_lsa
*)(type7
->data
);
1864 struct prefix_ipv4 p
= {
1865 .prefix
= type7
->data
->id
,
1866 .prefixlen
= ip_masklen(ext
->mask
),
1870 type5
= ospf_external_info_find_lsa(ospf
, &p
);
1871 } else if (type5
&& type7
== NULL
) {
1872 /* find the type-7 from which supplied type-5 was translated,
1873 * ie find first type-7 with same LSA Id.
1875 struct listnode
*ln
, *lnn
;
1876 struct route_node
*rn
;
1877 struct ospf_lsa
*lsa
;
1878 struct ospf_area
*area
;
1880 for (ALL_LIST_ELEMENTS(ospf
->areas
, ln
, lnn
, area
)) {
1881 if (area
->external_routing
!= OSPF_AREA_NSSA
&& !type7
)
1884 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
) {
1885 if (lsa
->data
->id
.s_addr
1886 == type5
->data
->id
.s_addr
) {
1894 /* do we have type7? */
1896 if (IS_DEBUG_OSPF_NSSA
)
1898 "ospf_translated_nssa_refresh(): no Type-7 found for Type-5 LSA Id %pI4",
1903 /* do we have valid translated type5? */
1904 if (type5
== NULL
|| !CHECK_FLAG(type5
->flags
, OSPF_LSA_LOCAL_XLT
)) {
1905 if (IS_DEBUG_OSPF_NSSA
)
1907 "ospf_translated_nssa_refresh(): No translated Type-5 found for Type-7 with Id %pI4",
1912 extold
= (struct as_external_lsa
*)type5
->data
;
1913 if (type7
->area
->suppress_fa
== 1) {
1914 if (extold
->e
[0].fwd_addr
.s_addr
== 0)
1915 ls_seqnum
= ntohl(type5
->data
->ls_seqnum
);
1918 /* Delete LSA from neighbor retransmit-list. */
1919 ospf_ls_retransmit_delete_nbr_as(ospf
, type5
);
1921 /* create new translated LSA */
1922 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1923 if (IS_DEBUG_OSPF_NSSA
)
1925 "ospf_translated_nssa_refresh(): Could not translate Type-7 for %pI4 to Type-5",
1930 if (type7
->area
->suppress_fa
== 1) {
1931 if (extold
->e
[0].fwd_addr
.s_addr
== 0)
1932 new->data
->ls_seqnum
= htonl(ls_seqnum
+ 1);
1935 if (!(new = ospf_lsa_install(ospf
, NULL
, new))) {
1937 EC_OSPF_LSA_INSTALL_FAILURE
,
1938 "ospf_translated_nssa_refresh(): Could not install translated LSA, Id %pI4",
1943 /* Flood LSA through area. */
1944 ospf_flood_through_as(ospf
, NULL
, new);
1949 int is_prefix_default(struct prefix_ipv4
*p
)
1951 struct prefix_ipv4 q
;
1954 q
.prefix
.s_addr
= INADDR_ANY
;
1957 return prefix_same((struct prefix
*)p
, (struct prefix
*)&q
);
1960 /* Originate an AS-external-LSA, install and flood. */
1961 struct ospf_lsa
*ospf_external_lsa_originate(struct ospf
*ospf
,
1962 struct external_info
*ei
)
1964 struct ospf_lsa
*new;
1966 /* Added for NSSA project....
1968 External LSAs are originated in ASBRs as usual, but for NSSA
1970 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1971 every area. The Type-7's are flooded to every IR and every ABR; We
1972 install the Type-5 LSDB so that the normal "refresh" code operates
1973 as usual, and flag them as not used during ASE calculations. The
1974 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
1975 Address of non-zero.
1977 If an ABR is the elected NSSA translator, following SPF and during
1978 the ABR task it will translate all the scanned Type-7's, with P-bit
1979 ON and not-self generated, and translate to Type-5's throughout the
1982 A difference in operation depends whether this ASBR is an ABR
1983 or not. If not an ABR, the P-bit is ON, to indicate that any
1984 elected NSSA-ABR can perform its translation.
1986 If an ABR, the P-bit is OFF; No ABR will perform translation and
1987 this ASBR will flood the Type-5 LSA as usual.
1989 For the case where this ASBR is not an ABR, the ASE calculations
1990 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
1991 demonstrate to the user that there are LSA's that belong to any
1994 Finally, it just so happens that when the ABR is translating every
1995 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
1996 approved Type-5 (translated from Type-7); at the end of translation
1997 if any Translated Type-5's remain unapproved, then they must be
1998 flushed from the AS.
2002 if (ospf
->router_id
.s_addr
== INADDR_ANY
) {
2003 if (IS_DEBUG_OSPF_EVENT
)
2005 "LSA[Type5:%pI4]: deferring AS-external-LSA origination, router ID is zero",
2010 /* Create new AS-external-LSA instance. */
2011 if ((new = ospf_external_lsa_new(ospf
, ei
, NULL
)) == NULL
) {
2012 if (IS_DEBUG_OSPF_EVENT
)
2014 "LSA[Type5:%pI4]: Could not originate AS-external-LSA",
2019 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2020 ospf_lsa_install(ospf
, NULL
, new);
2022 /* Update LSA origination count. */
2023 ospf
->lsa_originate_count
++;
2025 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2026 ospf_flood_through_as(ospf
, NULL
, new);
2028 /* If there is any attached NSSA, do special handling */
2029 if (ospf
->anyNSSA
&&
2030 /* stay away from translated LSAs! */
2031 !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2032 ospf_install_flood_nssa(
2033 ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2035 /* Debug logging. */
2036 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2037 zlog_debug("LSA[Type%d:%pI4]: Originate AS-external-LSA %p",
2038 new->data
->type
, &new->data
->id
,
2040 ospf_lsa_header_dump(new->data
);
2046 static struct external_info
*ospf_default_external_info(struct ospf
*ospf
)
2049 struct prefix_ipv4 p
;
2050 struct external_info
*default_ei
;
2054 p
.prefix
.s_addr
= 0;
2057 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
,
2058 ospf
->instance
, &p
);
2062 /* First, lookup redistributed default route. */
2063 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
2064 struct list
*ext_list
;
2066 if (type
== ZEBRA_ROUTE_OSPF
)
2069 ext_list
= ospf
->external
[type
];
2073 ret
= ospf_external_default_routemap_apply_walk(ospf
, ext_list
,
2082 void ospf_external_lsa_rid_change(struct ospf
*ospf
)
2084 struct external_info
*ei
;
2085 struct ospf_external_aggr_rt
*aggr
;
2088 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++) {
2089 struct route_node
*rn
;
2090 struct route_table
*rt
;
2091 struct list
*ext_list
;
2092 struct listnode
*node
;
2093 struct ospf_external
*ext
;
2095 ext_list
= ospf
->external
[type
];
2099 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2100 /* Originate As-external-LSA from all type of
2101 * distribute source.
2103 rt
= ext
->external_info
;
2107 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
2113 if (is_prefix_default(
2114 (struct prefix_ipv4
*)&ei
->p
))
2117 if (!ospf_redistribute_check(ospf
, ei
, NULL
))
2120 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
2122 if (IS_DEBUG_OSPF(lsa
, EXTNL_LSA_AGGR
))
2124 "Originate Summary LSA after reset/router-ID change");
2125 /* Here the LSA is originated as new */
2126 ospf_originate_summary_lsa(ospf
, aggr
,
2128 } else if (!ospf_external_lsa_originate(ospf
,
2130 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2131 "LSA: AS-external-LSA was not originated.");
2136 ei
= ospf_default_external_info(ospf
);
2137 if (ei
&& !ospf_external_lsa_originate(ospf
, ei
)) {
2138 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2139 "LSA: AS-external-LSA for default route was not originated.");
2143 /* Flush any NSSA LSAs for given prefix */
2144 void ospf_nssa_lsa_flush(struct ospf
*ospf
, struct prefix_ipv4
*p
)
2146 struct listnode
*node
, *nnode
;
2147 struct ospf_lsa
*lsa
= NULL
;
2148 struct ospf_area
*area
;
2150 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2151 if (area
->external_routing
== OSPF_AREA_NSSA
) {
2152 lsa
= ospf_lsa_lookup(ospf
, area
, OSPF_AS_NSSA_LSA
,
2153 p
->prefix
, ospf
->router_id
);
2155 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2157 "LSA: There is no such AS-NSSA-LSA %pFX in LSDB",
2161 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
2162 if (!IS_LSA_MAXAGE(lsa
)) {
2163 ospf_refresher_unregister_lsa(ospf
, lsa
);
2164 ospf_lsa_flush_area(lsa
, area
);
2170 /* Flush an AS-external-LSA from LSDB and routing domain. */
2171 void ospf_external_lsa_flush(struct ospf
*ospf
, uint8_t type
,
2172 struct prefix_ipv4
*p
,
2173 ifindex_t ifindex
/*, struct in_addr nexthop */)
2175 struct ospf_lsa
*lsa
;
2177 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2178 zlog_debug("LSA: Flushing AS-external-LSA %pFX", p
);
2180 /* First lookup LSA from LSDB. */
2181 if (!(lsa
= ospf_external_info_find_lsa(ospf
, p
))) {
2182 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2184 "LSA: There is no such AS-external-LSA %pFX in LSDB",
2189 /* If LSA is selforiginated, not a translated LSA, and there is
2190 * NSSA area, flush Type-7 LSA's at first.
2192 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2193 && !(CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2194 ospf_nssa_lsa_flush(ospf
, p
);
2196 /* Sweep LSA from Link State Retransmit List. */
2197 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2199 if (!IS_LSA_MAXAGE(lsa
)) {
2200 /* Unregister LSA from Refresh queue. */
2201 ospf_refresher_unregister_lsa(ospf
, lsa
);
2203 /* Flush AS-external-LSA through AS. */
2204 ospf_lsa_flush_as(ospf
, lsa
);
2207 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2208 zlog_debug("ospf_external_lsa_flush(): stop");
2211 void ospf_external_lsa_refresh_default(struct ospf
*ospf
)
2213 struct prefix_ipv4 p
;
2214 struct external_info
*ei
;
2215 struct ospf_lsa
*lsa
;
2219 p
.prefix
.s_addr
= INADDR_ANY
;
2221 ei
= ospf_default_external_info(ospf
);
2222 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
2225 if (IS_DEBUG_OSPF_EVENT
)
2226 zlog_debug("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2228 ospf_external_lsa_refresh(ospf
, lsa
, ei
, LSA_REFRESH_FORCE
,
2230 } else if (ei
&& !lsa
) {
2231 if (IS_DEBUG_OSPF_EVENT
)
2233 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2234 ospf_external_lsa_originate(ospf
, ei
);
2236 if (IS_DEBUG_OSPF_EVENT
)
2237 zlog_debug("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2238 ospf_external_lsa_flush(ospf
, DEFAULT_ROUTE
, &p
, 0);
2242 void ospf_external_lsa_refresh_type(struct ospf
*ospf
, uint8_t type
,
2243 unsigned 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
;
2262 struct ospf_external_aggr_rt
*aggr
;
2264 aggr
= ospf_external_aggr_match(ospf
,
2266 lsa
= ospf_external_info_find_lsa(
2269 /* Check the AS-external-LSA
2270 * should be originated.
2272 if (!ospf_redistribute_check(
2275 ospf_unlink_ei_from_aggr(
2284 "%s: Send Aggreate LSA (%pFX)",
2288 ospf_originate_summary_lsa(
2293 if (IS_LSA_MAXAGE(lsa
))
2294 force
= LSA_REFRESH_FORCE
;
2296 ospf_external_lsa_refresh(
2297 ospf
, lsa
, ei
, force
,
2300 if (!ospf_redistribute_check(
2303 ospf_external_lsa_originate(
2312 /* Refresh AS-external-LSA. */
2313 struct ospf_lsa
*ospf_external_lsa_refresh(struct ospf
*ospf
,
2314 struct ospf_lsa
*lsa
,
2315 struct external_info
*ei
, int force
,
2318 struct ospf_lsa
*new;
2321 /* Check the AS-external-LSA should be originated. */
2323 if (!ospf_redistribute_check(ospf
, ei
, &changed
)) {
2324 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2326 "LSA[Type%d:%pI4] Could not be refreshed, redist check fail",
2330 ospf_external_lsa_flush(ospf
, ei
->type
, &ei
->p
,
2331 ei
->ifindex
/*, ei->nexthop */);
2335 if (!changed
&& !force
) {
2336 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2338 "LSA[Type%d:%pI4]: Not refreshed, not changed/forced",
2339 lsa
->data
->type
, &lsa
->data
->id
);
2343 /* Delete LSA from neighbor retransmit-list. */
2344 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2346 /* Unregister AS-external-LSA from refresh-list. */
2347 ospf_refresher_unregister_lsa(ospf
, lsa
);
2349 new = ospf_external_lsa_new(ospf
, ei
, &lsa
->data
->id
);
2352 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2353 zlog_debug("LSA[Type%d:%pI4]: Could not be refreshed",
2354 lsa
->data
->type
, &lsa
->data
->id
);
2358 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2360 ospf_lsa_install(ospf
, NULL
, new); /* As type-5. */
2362 /* Flood LSA through AS. */
2363 ospf_flood_through_as(ospf
, NULL
, new);
2365 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2366 if (ospf
->anyNSSA
&& !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2367 ospf_install_flood_nssa(ospf
, new,
2368 ei
); /* Install/Flood per new rules */
2370 /* Register self-originated LSA to refresh queue.
2371 * Translated LSAs should not be registered, but refreshed upon
2372 * refresh of the Type-7
2374 if (!CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2375 ospf_refresher_register_lsa(ospf
, new);
2377 /* Debug logging. */
2378 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2379 zlog_debug("LSA[Type%d:%pI4]: AS-external-LSA refresh",
2380 new->data
->type
, &new->data
->id
);
2381 ospf_lsa_header_dump(new->data
);
2388 /* LSA installation functions. */
2390 /* Install router-LSA to an area. */
2391 static struct ospf_lsa
*
2392 ospf_router_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2394 struct ospf_area
*area
= new->area
;
2396 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2397 The entire routing table must be recalculated, starting with
2398 the shortest path calculations for each area (not just the
2399 area whose link-state database has changed).
2402 if (IS_LSA_SELF(new)) {
2404 /* Only install LSA if it is originated/refreshed by us.
2405 * If LSA was received by flooding, the RECEIVED flag is set so
2407 * not link the LSA */
2408 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2409 return new; /* ignore stale LSA */
2411 /* Set self-originated router-LSA. */
2412 ospf_lsa_unlock(&area
->router_lsa_self
);
2413 area
->router_lsa_self
= ospf_lsa_lock(new);
2415 ospf_refresher_register_lsa(ospf
, new);
2418 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2422 #define OSPF_INTERFACE_TIMER_ON(T, F, V) \
2424 (T) = thread_add_timer(master, (F), oi, (V))
2426 /* Install network-LSA to an area. */
2427 static struct ospf_lsa
*ospf_network_lsa_install(struct ospf
*ospf
,
2428 struct ospf_interface
*oi
,
2429 struct ospf_lsa
*new,
2433 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2434 The entire routing table must be recalculated, starting with
2435 the shortest path calculations for each area (not just the
2436 area whose link-state database has changed).
2438 if (IS_LSA_SELF(new)) {
2439 /* We supposed that when LSA is originated by us, we pass the
2441 for which it was originated. If LSA was received by flooding,
2442 the RECEIVED flag is set, so we do not link the LSA to the
2444 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2445 return new; /* ignore stale LSA */
2447 ospf_lsa_unlock(&oi
->network_lsa_self
);
2448 oi
->network_lsa_self
= ospf_lsa_lock(new);
2449 ospf_refresher_register_lsa(ospf
, new);
2452 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2457 /* Install summary-LSA to an area. */
2458 static struct ospf_lsa
*
2459 ospf_summary_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2461 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2462 /* RFC 2328 Section 13.2 Summary-LSAs
2463 The best route to the destination described by the summary-
2464 LSA must be recalculated (see Section 16.5). If this
2465 destination is an AS boundary router, it may also be
2466 necessary to re-examine all the AS-external-LSAs.
2469 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2472 if (IS_LSA_SELF(new))
2473 ospf_refresher_register_lsa(ospf
, new);
2478 /* Install ASBR-summary-LSA to an area. */
2479 static struct ospf_lsa
*ospf_summary_asbr_lsa_install(struct ospf
*ospf
,
2480 struct ospf_lsa
*new,
2483 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2484 /* RFC 2328 Section 13.2 Summary-LSAs
2485 The best route to the destination described by the summary-
2486 LSA must be recalculated (see Section 16.5). If this
2487 destination is an AS boundary router, it may also be
2488 necessary to re-examine all the AS-external-LSAs.
2490 ospf_spf_calculate_schedule(ospf
,
2491 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2494 /* register LSA to refresh-list. */
2495 if (IS_LSA_SELF(new))
2496 ospf_refresher_register_lsa(ospf
, new);
2501 /* Install AS-external-LSA. */
2502 static struct ospf_lsa
*ospf_external_lsa_install(struct ospf
*ospf
,
2503 struct ospf_lsa
*new,
2506 ospf_ase_register_external_lsa(new, ospf
);
2507 /* If LSA is not self-originated, calculate an external route. */
2509 /* RFC 2328 Section 13.2 AS-external-LSAs
2510 The best route to the destination described by the AS-
2511 external-LSA must be recalculated (see Section 16.6).
2514 if (!IS_LSA_SELF(new))
2515 ospf_ase_incremental_update(ospf
, new);
2518 if (new->data
->type
== OSPF_AS_NSSA_LSA
) {
2519 /* There is no point to register selforiginate Type-7 LSA for
2520 * refreshing. We rely on refreshing Type-5 LSA's
2522 if (IS_LSA_SELF(new))
2525 /* Try refresh type-5 translated LSA for this LSA, if
2527 * New translations will be taken care of by the
2530 ospf_translated_nssa_refresh(ospf
, new, NULL
);
2531 ospf_schedule_abr_task(ospf
);
2535 /* Register self-originated LSA to refresh queue.
2536 * Leave Translated LSAs alone if NSSA is enabled
2538 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2539 ospf_refresher_register_lsa(ospf
, new);
2544 void ospf_discard_from_db(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
2545 struct ospf_lsa
*lsa
)
2547 struct ospf_lsa
*old
;
2552 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2557 if (old
->refresh_list
>= 0)
2558 ospf_refresher_unregister_lsa(ospf
, old
);
2560 switch (old
->data
->type
) {
2561 case OSPF_AS_EXTERNAL_LSA
:
2562 ospf_ase_unregister_external_lsa(old
, ospf
);
2563 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2565 case OSPF_OPAQUE_AS_LSA
:
2566 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2568 case OSPF_AS_NSSA_LSA
:
2569 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2570 ospf_ase_unregister_external_lsa(old
, ospf
);
2573 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2577 ospf_lsa_maxage_delete(ospf
, old
);
2578 ospf_lsa_discard(old
);
2581 struct ospf_lsa
*ospf_lsa_install(struct ospf
*ospf
, struct ospf_interface
*oi
,
2582 struct ospf_lsa
*lsa
)
2584 struct ospf_lsa
*new = NULL
;
2585 struct ospf_lsa
*old
= NULL
;
2586 struct ospf_lsdb
*lsdb
= NULL
;
2590 switch (lsa
->data
->type
) {
2592 case OSPF_AS_NSSA_LSA
:
2594 lsdb
= lsa
->area
->lsdb
;
2598 case OSPF_AS_EXTERNAL_LSA
:
2599 case OSPF_OPAQUE_AS_LSA
:
2604 lsdb
= lsa
->area
->lsdb
;
2610 /* RFC 2328 13.2. Installing LSAs in the database
2612 Installing a new LSA in the database, either as the result of
2613 flooding or a newly self-originated LSA, may cause the OSPF
2614 routing table structure to be recalculated. The contents of the
2615 new LSA should be compared to the old instance, if present. If
2616 there is no difference, there is no need to recalculate the
2617 routing table. When comparing an LSA to its previous instance,
2618 the following are all considered to be differences in contents:
2620 o The LSA's Options field has changed.
2622 o One of the LSA instances has LS age set to MaxAge, and
2625 o The length field in the LSA header has changed.
2627 o The body of the LSA (i.e., anything outside the 20-byte
2628 LSA header) has changed. Note that this excludes changes
2629 in LS Sequence Number and LS Checksum.
2632 /* Look up old LSA and determine if any SPF calculation or incremental
2634 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2636 /* Do comparision and record if recalc needed. */
2638 if (old
== NULL
|| ospf_lsa_different(old
, lsa
)) {
2639 /* Ref rfc3623 section 3.2.3
2640 * Installing new lsa or change in the existing LSA
2641 * or flushing existing LSA leads to topo change
2642 * and trigger SPF caculation.
2643 * So, router should be aborted from HELPER role
2644 * if it is detected as TOPO change.
2646 if (CHECK_LSA_TYPE_1_TO_5_OR_7(lsa
->data
->type
))
2647 ospf_helper_handle_topo_chg(ospf
, lsa
);
2653 Sequence number check (Section 14.1 of rfc 2328)
2654 "Premature aging is used when it is time for a self-originated
2655 LSA's sequence number field to wrap. At this point, the current
2656 LSA instance (having LS sequence number MaxSequenceNumber) must
2657 be prematurely aged and flushed from the routing domain before a
2658 new instance with sequence number equal to InitialSequenceNumber
2659 can be originated. "
2662 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
) {
2663 if (ospf_lsa_is_self_originated(ospf
, lsa
)) {
2664 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2666 if (!IS_LSA_MAXAGE(lsa
))
2667 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2668 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
2670 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
)) {
2672 "ospf_lsa_install() Premature Aging lsa 0x%p, seqnum 0x%x",
2674 ntohl(lsa
->data
->ls_seqnum
));
2675 ospf_lsa_header_dump(lsa
->data
);
2678 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2680 "ospf_lsa_install() got an lsa with seq 0x80000000 that was not self originated. Ignoring");
2681 ospf_lsa_header_dump(lsa
->data
);
2687 /* discard old LSA from LSDB */
2689 ospf_discard_from_db(ospf
, lsdb
, lsa
);
2691 /* Calculate Checksum if self-originated?. */
2692 if (IS_LSA_SELF(lsa
))
2693 ospf_lsa_checksum(lsa
->data
);
2695 /* Insert LSA to LSDB. */
2696 ospf_lsdb_add(lsdb
, lsa
);
2699 /* Do LSA specific installation process. */
2700 switch (lsa
->data
->type
) {
2701 case OSPF_ROUTER_LSA
:
2702 new = ospf_router_lsa_install(ospf
, lsa
, rt_recalc
);
2704 case OSPF_NETWORK_LSA
:
2706 new = ospf_network_lsa_install(ospf
, oi
, lsa
, rt_recalc
);
2708 case OSPF_SUMMARY_LSA
:
2709 new = ospf_summary_lsa_install(ospf
, lsa
, rt_recalc
);
2711 case OSPF_ASBR_SUMMARY_LSA
:
2712 new = ospf_summary_asbr_lsa_install(ospf
, lsa
, rt_recalc
);
2714 case OSPF_AS_EXTERNAL_LSA
:
2715 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2717 case OSPF_OPAQUE_LINK_LSA
:
2718 if (IS_LSA_SELF(lsa
))
2719 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for
2722 /* Incoming "oi" for this LSA has set at LSUpd
2726 case OSPF_OPAQUE_AREA_LSA
:
2727 case OSPF_OPAQUE_AS_LSA
:
2728 new = ospf_opaque_lsa_install(lsa
, rt_recalc
);
2730 case OSPF_AS_NSSA_LSA
:
2731 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2732 default: /* type-6,8,9....nothing special */
2737 return new; /* Installation failed, cannot proceed further --
2741 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
)) {
2742 switch (lsa
->data
->type
) {
2743 case OSPF_AS_EXTERNAL_LSA
:
2744 case OSPF_OPAQUE_AS_LSA
:
2745 case OSPF_AS_NSSA_LSA
:
2746 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2747 lookup_msg(ospf_lsa_type_msg
,
2748 new->data
->type
, NULL
));
2751 zlog_debug("LSA[%s]: Install %s to Area %pI4",
2753 lookup_msg(ospf_lsa_type_msg
,
2754 new->data
->type
, NULL
),
2755 &new->area
->area_id
);
2761 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2762 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2764 if (IS_LSA_MAXAGE(new)) {
2765 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
))
2766 zlog_debug("LSA[Type%d:%pI4]: Install LSA 0x%p, MaxAge",
2767 new->data
->type
, &new->data
->id
,
2769 ospf_lsa_maxage(ospf
, lsa
);
2776 int ospf_check_nbr_status(struct ospf
*ospf
)
2778 struct listnode
*node
, *nnode
;
2779 struct ospf_interface
*oi
;
2781 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
2782 struct route_node
*rn
;
2783 struct ospf_neighbor
*nbr
;
2785 if (ospf_if_is_enable(oi
))
2786 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
2787 if ((nbr
= rn
->info
) != NULL
)
2788 if (nbr
->state
== NSM_Exchange
2789 || nbr
->state
== NSM_Loading
) {
2790 route_unlock_node(rn
);
2799 static int ospf_maxage_lsa_remover(struct thread
*thread
)
2801 struct ospf
*ospf
= THREAD_ARG(thread
);
2802 struct ospf_lsa
*lsa
, *old
;
2803 struct route_node
*rn
;
2806 ospf
->t_maxage
= NULL
;
2808 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2809 zlog_debug("LSA[MaxAge]: remover Start");
2811 reschedule
= !ospf_check_nbr_status(ospf
);
2814 for (rn
= route_top(ospf
->maxage_lsa
); rn
;
2815 rn
= route_next(rn
)) {
2816 if ((lsa
= rn
->info
) == NULL
) {
2820 /* There is at least one neighbor from which we still
2822 * for that LSA, so we are not allowed to remove it from
2824 * as per RFC 2328 section 14 para 4 a) */
2825 if (lsa
->retransmit_counter
> 0) {
2830 /* TODO: maybe convert this function to a work-queue */
2831 if (thread_should_yield(thread
)) {
2832 OSPF_TIMER_ON(ospf
->t_maxage
,
2833 ospf_maxage_lsa_remover
, 0);
2835 rn
); /* route_top/route_next */
2839 /* Remove LSA from the LSDB */
2840 if (IS_LSA_SELF(lsa
))
2841 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2843 "LSA[Type%d:%pI4]: LSA 0x%lx is self-originated: ",
2846 (unsigned long)lsa
);
2848 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2850 "LSA[Type%d:%pI4]: MaxAge LSA removed from list",
2854 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
)) {
2855 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2857 "originating new lsa for lsa 0x%p",
2859 ospf_lsa_refresh(ospf
, lsa
);
2862 /* Remove from lsdb. */
2864 old
= ospf_lsdb_lookup(lsa
->lsdb
, lsa
);
2865 /* The max age LSA here must be the same
2866 * as the LSA in LSDB
2869 flog_err(EC_OSPF_LSA_MISSING
,
2870 "%s: LSA[Type%d:%pI4]: LSA not in LSDB",
2871 __func__
, lsa
->data
->type
,
2876 ospf_discard_from_db(ospf
, lsa
->lsdb
, lsa
);
2877 ospf_lsdb_delete(lsa
->lsdb
, lsa
);
2879 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2881 "%s: LSA[Type%d:%pI4]: No associated LSDB!",
2882 __func__
, lsa
->data
->type
,
2887 /* A MaxAge LSA must be removed immediately from the router's link
2888 state database as soon as both a) it is no longer contained on any
2889 neighbor Link state retransmission lists and b) none of the
2891 neighbors are in states Exchange or Loading. */
2893 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2894 ospf
->maxage_delay
);
2899 void ospf_lsa_maxage_delete(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2901 struct route_node
*rn
;
2902 struct prefix lsa_prefix
;
2904 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2905 lsa_prefix
.family
= 0;
2906 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2907 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2909 if ((rn
= route_node_lookup(ospf
->maxage_lsa
, &lsa_prefix
))) {
2910 if (rn
->info
== lsa
) {
2911 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2912 ospf_lsa_unlock(&lsa
); /* maxage_lsa */
2915 rn
); /* unlock node because lsa is deleted */
2917 route_unlock_node(rn
); /* route_node_lookup */
2919 if (IS_DEBUG_OSPF_EVENT
)
2920 zlog_debug("%s: lsa %s is not found in maxage db.",
2921 __func__
, dump_lsa_key(lsa
));
2925 /* Add LSA onto the MaxAge list, and schedule for removal.
2926 * This does *not* lead to the LSA being flooded, that must be taken
2927 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2930 void ospf_lsa_maxage(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2932 struct prefix lsa_prefix
;
2933 struct route_node
*rn
;
2935 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2936 and schedule the MaxAge LSA remover. */
2937 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
)) {
2938 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2940 "LSA[Type%d:%pI4]: %p already exists on MaxAge LSA list",
2941 lsa
->data
->type
, &lsa
->data
->id
,
2946 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2947 lsa_prefix
.family
= 0;
2948 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2949 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2951 rn
= route_node_get(ospf
->maxage_lsa
, &lsa_prefix
);
2952 if (rn
->info
!= NULL
) {
2953 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2955 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
2956 dump_lsa_key(lsa
), rn
->info
,
2957 (void *)lsa
, lsa_prefix
.prefixlen
);
2958 route_unlock_node(rn
);
2960 rn
->info
= ospf_lsa_lock(lsa
);
2961 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2964 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2965 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
2968 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2969 ospf
->maxage_delay
);
2972 static int ospf_lsa_maxage_walker_remover(struct ospf
*ospf
,
2973 struct ospf_lsa
*lsa
)
2975 /* Stay away from any Local Translated Type-7 LSAs */
2976 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
2979 if (IS_LSA_MAXAGE(lsa
))
2980 /* Self-originated LSAs should NOT time-out instead,
2981 they're flushed and submitted to the max_age list explicitly.
2983 if (!ospf_lsa_is_self_originated(ospf
, lsa
)) {
2984 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2985 zlog_debug("LSA[%s]: is MaxAge",
2988 switch (lsa
->data
->type
) {
2989 case OSPF_OPAQUE_LINK_LSA
:
2990 case OSPF_OPAQUE_AREA_LSA
:
2991 case OSPF_OPAQUE_AS_LSA
:
2993 * As a general rule, whenever network topology
2995 * (due to an LSA removal in this case), routing
2997 * should be triggered. However, this is not
2999 * LSAs. Even if an opaque LSA instance is going
3001 * from the routing domain, it does not mean a
3003 * topology, and thus, routing recalculation is
3007 case OSPF_AS_EXTERNAL_LSA
:
3008 case OSPF_AS_NSSA_LSA
:
3009 ospf_ase_incremental_update(ospf
, lsa
);
3012 ospf_spf_calculate_schedule(ospf
,
3016 ospf_lsa_maxage(ospf
, lsa
);
3019 if (IS_LSA_MAXAGE(lsa
) && !ospf_lsa_is_self_originated(ospf
, lsa
))
3020 if (LS_AGE(lsa
) > OSPF_LSA_MAXAGE
+ 30)
3021 printf("Eek! Shouldn't happen!\n");
3026 /* Periodical check of MaxAge LSA. */
3027 int ospf_lsa_maxage_walker(struct thread
*thread
)
3029 struct ospf
*ospf
= THREAD_ARG(thread
);
3030 struct route_node
*rn
;
3031 struct ospf_lsa
*lsa
;
3032 struct ospf_area
*area
;
3033 struct listnode
*node
, *nnode
;
3035 ospf
->t_maxage_walker
= NULL
;
3037 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3038 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
3039 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3040 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
3041 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3042 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3043 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3044 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3045 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3046 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3047 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3048 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3049 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3050 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
3051 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3054 /* for AS-external-LSAs. */
3056 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3057 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3058 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3059 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3062 OSPF_TIMER_ON(ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3063 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3067 struct ospf_lsa
*ospf_lsa_lookup_by_prefix(struct ospf_lsdb
*lsdb
, uint8_t type
,
3068 struct prefix_ipv4
*p
,
3069 struct in_addr router_id
)
3071 struct ospf_lsa
*lsa
;
3072 struct in_addr mask
, id
;
3073 struct lsa_header_mask
{
3074 struct lsa_header header
;
3075 struct in_addr mask
;
3078 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, p
->prefix
, router_id
);
3082 masklen2ip(p
->prefixlen
, &mask
);
3084 hmask
= (struct lsa_header_mask
*)lsa
->data
;
3086 if (mask
.s_addr
!= hmask
->mask
.s_addr
) {
3087 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3088 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, router_id
);
3096 struct ospf_lsa
*ospf_lsa_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
3097 uint32_t type
, struct in_addr id
,
3098 struct in_addr adv_router
)
3104 case OSPF_ROUTER_LSA
:
3105 case OSPF_NETWORK_LSA
:
3106 case OSPF_SUMMARY_LSA
:
3107 case OSPF_ASBR_SUMMARY_LSA
:
3108 case OSPF_AS_NSSA_LSA
:
3109 case OSPF_OPAQUE_LINK_LSA
:
3110 case OSPF_OPAQUE_AREA_LSA
:
3111 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, adv_router
);
3112 case OSPF_AS_EXTERNAL_LSA
:
3113 case OSPF_OPAQUE_AS_LSA
:
3114 return ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
, adv_router
);
3122 struct ospf_lsa
*ospf_lsa_lookup_by_id(struct ospf_area
*area
, uint32_t type
,
3125 struct ospf_lsa
*lsa
;
3126 struct route_node
*rn
;
3129 case OSPF_ROUTER_LSA
:
3130 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3131 case OSPF_NETWORK_LSA
:
3132 for (rn
= route_top(NETWORK_LSDB(area
)); rn
;
3133 rn
= route_next(rn
))
3134 if ((lsa
= rn
->info
))
3135 if (IPV4_ADDR_SAME(&lsa
->data
->id
, &id
)) {
3136 route_unlock_node(rn
);
3140 case OSPF_SUMMARY_LSA
:
3141 case OSPF_ASBR_SUMMARY_LSA
:
3142 /* Currently not used. */
3144 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3145 case OSPF_AS_EXTERNAL_LSA
:
3146 case OSPF_AS_NSSA_LSA
:
3147 case OSPF_OPAQUE_LINK_LSA
:
3148 case OSPF_OPAQUE_AREA_LSA
:
3149 case OSPF_OPAQUE_AS_LSA
:
3150 /* Currently not used. */
3159 struct ospf_lsa
*ospf_lsa_lookup_by_header(struct ospf_area
*area
,
3160 struct lsa_header
*lsah
)
3162 struct ospf_lsa
*match
;
3165 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3166 * is redefined to have two subfields; opaque-type and opaque-id.
3167 * However, it is harmless to treat the two sub fields together, as if
3168 * they two were forming a unique LSA-ID.
3171 match
= ospf_lsa_lookup(area
->ospf
, area
, lsah
->type
, lsah
->id
,
3175 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3176 zlog_debug("LSA[Type%d:%pI4]: Lookup by header, NO MATCH",
3177 lsah
->type
, &lsah
->id
);
3182 /* return +n, l1 is more recent.
3183 return -n, l2 is more recent.
3184 return 0, l1 and l2 is identical. */
3185 int ospf_lsa_more_recent(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3190 if (l1
== NULL
&& l2
== NULL
)
3197 /* compare LS sequence number. */
3198 x
= (int)ntohl(l1
->data
->ls_seqnum
);
3199 y
= (int)ntohl(l2
->data
->ls_seqnum
);
3205 /* compare LS checksum. */
3206 r
= ntohs(l1
->data
->checksum
) - ntohs(l2
->data
->checksum
);
3210 /* compare LS age. */
3211 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3213 else if (!IS_LSA_MAXAGE(l1
) && IS_LSA_MAXAGE(l2
))
3216 /* compare LS age with MaxAgeDiff. */
3217 if (LS_AGE(l1
) - LS_AGE(l2
) > OSPF_LSA_MAXAGE_DIFF
)
3219 else if (LS_AGE(l2
) - LS_AGE(l1
) > OSPF_LSA_MAXAGE_DIFF
)
3222 /* LSAs are identical. */
3226 /* If two LSAs are different, return 1, otherwise return 0. */
3227 int ospf_lsa_different(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3235 if (l1
->data
->options
!= l2
->data
->options
)
3238 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3241 if (IS_LSA_MAXAGE(l2
) && !IS_LSA_MAXAGE(l1
))
3244 if (l1
->data
->length
!= l2
->data
->length
)
3247 if (l1
->data
->length
== 0)
3250 if (CHECK_FLAG((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3251 return 1; /* May be a stale LSA in the LSBD */
3253 assert(ntohs(l1
->data
->length
) > OSPF_LSA_HEADER_SIZE
);
3255 p1
= (char *)l1
->data
;
3256 p2
= (char *)l2
->data
;
3258 if (memcmp(p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3259 ntohs(l1
->data
->length
) - OSPF_LSA_HEADER_SIZE
)
3266 int ospf_lsa_flush_schedule(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3268 if (lsa
== NULL
|| !IS_LSA_SELF(lsa
))
3271 if (IS_DEBUG_OSPF_EVENT
)
3273 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3274 lsa
->data
->type
, &lsa
->data
->id
);
3276 /* Force given lsa's age to MaxAge. */
3277 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
3279 switch (lsa
->data
->type
) {
3280 /* Opaque wants to be notified of flushes */
3281 case OSPF_OPAQUE_LINK_LSA
:
3282 case OSPF_OPAQUE_AREA_LSA
:
3283 case OSPF_OPAQUE_AS_LSA
:
3284 ospf_opaque_lsa_refresh(lsa
);
3287 ospf_refresher_unregister_lsa(ospf
, lsa
);
3288 ospf_lsa_flush(ospf
, lsa
);
3295 void ospf_flush_self_originated_lsas_now(struct ospf
*ospf
)
3297 struct listnode
*node
, *nnode
;
3298 struct listnode
*node2
, *nnode2
;
3299 struct ospf_area
*area
;
3300 struct ospf_interface
*oi
;
3301 struct ospf_lsa
*lsa
;
3302 struct route_node
*rn
;
3303 int need_to_flush_ase
= 0;
3305 ospf
->inst_shutdown
= 1;
3307 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3308 if ((lsa
= area
->router_lsa_self
) != NULL
) {
3309 if (IS_DEBUG_OSPF_EVENT
)
3311 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3315 ospf_refresher_unregister_lsa(ospf
, lsa
);
3316 ospf_lsa_flush_area(lsa
, area
);
3317 ospf_lsa_unlock(&area
->router_lsa_self
);
3318 area
->router_lsa_self
= NULL
;
3321 for (ALL_LIST_ELEMENTS(area
->oiflist
, node2
, nnode2
, oi
)) {
3322 if ((lsa
= oi
->network_lsa_self
) != NULL
3323 && oi
->state
== ISM_DR
&& oi
->full_nbrs
> 0) {
3324 if (IS_DEBUG_OSPF_EVENT
)
3326 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3330 ospf_refresher_unregister_lsa(
3331 ospf
, oi
->network_lsa_self
);
3332 ospf_lsa_flush_area(oi
->network_lsa_self
, area
);
3333 ospf_lsa_unlock(&oi
->network_lsa_self
);
3334 oi
->network_lsa_self
= NULL
;
3337 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3338 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3339 need_to_flush_ase
= 1;
3342 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3343 ospf_lsa_flush_schedule(ospf
, lsa
);
3344 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3345 ospf_lsa_flush_schedule(ospf
, lsa
);
3346 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3347 ospf_lsa_flush_schedule(ospf
, lsa
);
3348 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3349 ospf_lsa_flush_schedule(ospf
, lsa
);
3352 if (need_to_flush_ase
) {
3353 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3354 ospf_lsa_flush_schedule(ospf
, lsa
);
3355 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3356 ospf_lsa_flush_schedule(ospf
, lsa
);
3360 * Make sure that the MaxAge LSA remover is executed immediately,
3361 * without conflicting to other threads.
3363 if (ospf
->t_maxage
!= NULL
) {
3364 OSPF_TIMER_OFF(ospf
->t_maxage
);
3365 thread_execute(master
, ospf_maxage_lsa_remover
, ospf
, 0);
3371 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3372 /* An interface-independent version of ospf_lsa_is_self_originated */
3373 int ospf_lsa_is_self_originated(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3375 struct listnode
*node
;
3376 struct ospf_interface
*oi
;
3378 /* This LSA is already checked. */
3379 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3380 return IS_LSA_SELF(lsa
);
3382 /* Make sure LSA is self-checked. */
3383 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3385 /* AdvRouter and Router ID is the same. */
3386 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
, &ospf
->router_id
))
3387 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3389 /* LSA is router-LSA. */
3390 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
3391 && IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
))
3392 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3394 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3395 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3396 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
3397 /* Ignore virtual link. */
3398 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3399 if (oi
->address
->family
== AF_INET
)
3402 &oi
->address
->u
.prefix4
)) {
3403 /* to make it easier later */
3404 SET_FLAG(lsa
->flags
,
3406 return IS_LSA_SELF(lsa
);
3410 return IS_LSA_SELF(lsa
);
3413 /* Get unique Link State ID. */
3414 struct in_addr
ospf_lsa_unique_id(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
3415 uint8_t type
, struct prefix_ipv4
*p
)
3417 struct ospf_lsa
*lsa
;
3418 struct in_addr mask
, id
;
3422 /* Check existence of LSA instance. */
3423 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, ospf
->router_id
);
3425 struct as_external_lsa
*al
=
3426 (struct as_external_lsa
*)lsa
->data
;
3427 if (ip_masklen(al
->mask
) == p
->prefixlen
) {
3428 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3430 "ospf_lsa_unique_id(): Can't get Link State ID for %pFX",
3432 /* id.s_addr = 0; */
3433 id
.s_addr
= 0xffffffff;
3436 /* Masklen differs, then apply wildcard mask to Link State ID.
3439 masklen2ip(p
->prefixlen
, &mask
);
3441 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3442 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
,
3445 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3447 "ospf_lsa_unique_id(): Can't get Link State ID for %pFX",
3449 /* id.s_addr = 0; */
3450 id
.s_addr
= 0xffffffff;
3460 #define LSA_ACTION_FLOOD_AREA 1
3461 #define LSA_ACTION_FLUSH_AREA 2
3465 struct ospf_area
*area
;
3466 struct ospf_lsa
*lsa
;
3469 static int ospf_lsa_action(struct thread
*t
)
3471 struct lsa_action
*data
;
3473 data
= THREAD_ARG(t
);
3475 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3476 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3479 switch (data
->action
) {
3480 case LSA_ACTION_FLOOD_AREA
:
3481 ospf_flood_through_area(data
->area
, NULL
, data
->lsa
);
3483 case LSA_ACTION_FLUSH_AREA
:
3484 ospf_lsa_flush_area(data
->lsa
, data
->area
);
3488 ospf_lsa_unlock(&data
->lsa
); /* Message */
3489 XFREE(MTYPE_OSPF_MESSAGE
, data
);
3493 void ospf_schedule_lsa_flood_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3495 struct lsa_action
*data
;
3497 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3498 data
->action
= LSA_ACTION_FLOOD_AREA
;
3500 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flood area */
3502 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3505 void ospf_schedule_lsa_flush_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3507 struct lsa_action
*data
;
3509 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3510 data
->action
= LSA_ACTION_FLUSH_AREA
;
3512 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flush area */
3514 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3518 /* LSA Refreshment functions. */
3519 struct ospf_lsa
*ospf_lsa_refresh(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3521 struct external_info
*ei
;
3522 struct ospf_external_aggr_rt
*aggr
;
3523 struct ospf_lsa
*new = NULL
;
3524 struct as_external_lsa
*al
;
3525 struct prefix_ipv4 p
;
3527 assert(CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF
));
3528 assert(IS_LSA_SELF(lsa
));
3529 assert(lsa
->lock
> 0);
3531 switch (lsa
->data
->type
) {
3532 /* Router and Network LSAs are processed differently. */
3533 case OSPF_ROUTER_LSA
:
3534 new = ospf_router_lsa_refresh(lsa
);
3536 case OSPF_NETWORK_LSA
:
3537 new = ospf_network_lsa_refresh(lsa
);
3539 case OSPF_SUMMARY_LSA
:
3540 new = ospf_summary_lsa_refresh(ospf
, lsa
);
3542 case OSPF_ASBR_SUMMARY_LSA
:
3543 new = ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
3545 case OSPF_AS_EXTERNAL_LSA
:
3546 /* Translated from NSSA Type-5s are refreshed when
3547 * from refresh of Type-7 - do not refresh these directly.
3550 al
= (struct as_external_lsa
*)lsa
->data
;
3552 p
.prefixlen
= ip_masklen(al
->mask
);
3553 p
.prefix
= lsa
->data
->id
;
3555 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3557 ei
= ospf_external_info_check(ospf
, lsa
);
3559 new = ospf_external_lsa_refresh(
3560 ospf
, lsa
, ei
, LSA_REFRESH_FORCE
, false);
3562 aggr
= (struct ospf_external_aggr_rt
*)
3563 ospf_extrenal_aggregator_lookup(ospf
, &p
);
3565 struct external_info ei_aggr
;
3568 sizeof(struct external_info
));
3569 ei_aggr
.p
= aggr
->p
;
3570 ei_aggr
.tag
= aggr
->tag
;
3571 ei_aggr
.instance
= ospf
->instance
;
3572 ei_aggr
.route_map_set
.metric
= -1;
3573 ei_aggr
.route_map_set
.metric_type
= -1;
3575 ospf_external_lsa_refresh(ospf
, lsa
, &ei_aggr
,
3576 LSA_REFRESH_FORCE
, true);
3578 ospf_lsa_flush_as(ospf
, lsa
);
3581 case OSPF_OPAQUE_LINK_LSA
:
3582 case OSPF_OPAQUE_AREA_LSA
:
3583 case OSPF_OPAQUE_AS_LSA
:
3584 new = ospf_opaque_lsa_refresh(lsa
);
3592 void ospf_refresher_register_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3594 uint16_t index
, current_index
;
3596 assert(lsa
->lock
> 0);
3597 assert(IS_LSA_SELF(lsa
));
3599 if (lsa
->refresh_list
< 0) {
3602 OSPF_LS_REFRESH_TIME
- (2 * OSPF_LS_REFRESH_JITTER
);
3603 int max_delay
= OSPF_LS_REFRESH_TIME
- OSPF_LS_REFRESH_JITTER
;
3605 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3607 * 1800s. Use jitter so that we send the LSA sometime between
3611 delay
= (frr_weak_random() % (max_delay
- min_delay
))
3614 current_index
= ospf
->lsa_refresh_queue
.index
3615 + (monotime(NULL
) - ospf
->lsa_refresher_started
)
3616 / OSPF_LSA_REFRESHER_GRANULARITY
;
3618 index
= (current_index
+ delay
/ OSPF_LSA_REFRESHER_GRANULARITY
)
3619 % (OSPF_LSA_REFRESHER_SLOTS
);
3621 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3623 "LSA[Refresh:Type%d:%pI4]: age %d, added to index %d",
3624 lsa
->data
->type
, &lsa
->data
->id
,
3625 LS_AGE(lsa
), index
);
3627 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3628 ospf
->lsa_refresh_queue
.qs
[index
] = list_new();
3630 listnode_add(ospf
->lsa_refresh_queue
.qs
[index
],
3631 ospf_lsa_lock(lsa
)); /* lsa_refresh_queue */
3632 lsa
->refresh_list
= index
;
3634 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3636 "LSA[Refresh:Type%d:%pI4]: ospf_refresher_register_lsa(): setting refresh_list on lsa %p (slod %d)",
3637 lsa
->data
->type
, &lsa
->data
->id
,
3638 (void *)lsa
, index
);
3642 void ospf_refresher_unregister_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3644 assert(lsa
->lock
> 0);
3645 assert(IS_LSA_SELF(lsa
));
3646 if (lsa
->refresh_list
>= 0) {
3647 struct list
*refresh_list
=
3648 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3649 listnode_delete(refresh_list
, lsa
);
3650 if (!listcount(refresh_list
)) {
3651 list_delete(&refresh_list
);
3652 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3654 lsa
->refresh_list
= -1;
3655 ospf_lsa_unlock(&lsa
); /* lsa_refresh_queue */
3659 int ospf_lsa_refresh_walker(struct thread
*t
)
3661 struct list
*refresh_list
;
3662 struct listnode
*node
, *nnode
;
3663 struct ospf
*ospf
= THREAD_ARG(t
);
3664 struct ospf_lsa
*lsa
;
3666 struct list
*lsa_to_refresh
= list_new();
3668 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3669 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3672 i
= ospf
->lsa_refresh_queue
.index
;
3674 /* Note: if clock has jumped backwards, then time change could be
3676 so we are careful to cast the expression to unsigned before taking
3678 ospf
->lsa_refresh_queue
.index
=
3679 ((unsigned long)(ospf
->lsa_refresh_queue
.index
3681 - ospf
->lsa_refresher_started
)
3682 / OSPF_LSA_REFRESHER_GRANULARITY
))
3683 % OSPF_LSA_REFRESHER_SLOTS
;
3685 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3687 "LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3688 ospf
->lsa_refresh_queue
.index
);
3690 for (; i
!= ospf
->lsa_refresh_queue
.index
;
3691 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
) {
3692 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3694 "LSA[Refresh]: ospf_lsa_refresh_walker(): refresh index %d",
3697 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3701 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3704 for (ALL_LIST_ELEMENTS(refresh_list
, node
, nnode
,
3706 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3708 "LSA[Refresh:Type%d:%pI4]: ospf_lsa_refresh_walker(): refresh lsa %p (slot %d)",
3713 assert(lsa
->lock
> 0);
3714 list_delete_node(refresh_list
, node
);
3715 lsa
->refresh_list
= -1;
3716 listnode_add(lsa_to_refresh
, lsa
);
3718 list_delete(&refresh_list
);
3722 ospf
->t_lsa_refresher
= NULL
;
3723 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
3724 ospf
->lsa_refresh_interval
, &ospf
->t_lsa_refresher
);
3725 ospf
->lsa_refresher_started
= monotime(NULL
);
3727 for (ALL_LIST_ELEMENTS(lsa_to_refresh
, node
, nnode
, lsa
)) {
3728 ospf_lsa_refresh(ospf
, lsa
);
3729 assert(lsa
->lock
> 0);
3731 &lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3734 list_delete(&lsa_to_refresh
);
3736 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3737 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3742 /* Flush the LSAs for the specific area */
3743 void ospf_flush_lsa_from_area(struct ospf
*ospf
, struct in_addr area_id
,
3746 struct ospf_area
*area
;
3747 struct route_node
*rn
;
3748 struct ospf_lsa
*lsa
;
3750 area
= ospf_area_get(ospf
, area_id
);
3753 case OSPF_AS_EXTERNAL_LSA
:
3754 if ((area
->external_routing
== OSPF_AREA_NSSA
) ||
3755 (area
->external_routing
== OSPF_AREA_STUB
)) {
3756 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3757 if (IS_LSA_SELF(lsa
) &&
3758 !(CHECK_FLAG(lsa
->flags
,
3759 OSPF_LSA_LOCAL_XLT
)))
3760 ospf_lsa_flush_area(lsa
, area
);
3763 case OSPF_AS_NSSA_LSA
:
3764 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
3765 if (IS_LSA_SELF(lsa
))
3766 ospf_lsa_flush_area(lsa
, area
);