1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * OSPF Link State Advertisement
4 * Copyright (C) 1999, 2000 Toshiaki Takada
19 #include "sockunion.h" /* for inet_aton() */
23 #include "ospfd/ospfd.h"
24 #include "ospfd/ospf_interface.h"
25 #include "ospfd/ospf_ism.h"
26 #include "ospfd/ospf_asbr.h"
27 #include "ospfd/ospf_lsa.h"
28 #include "ospfd/ospf_lsdb.h"
29 #include "ospfd/ospf_neighbor.h"
30 #include "ospfd/ospf_nsm.h"
31 #include "ospfd/ospf_flood.h"
32 #include "ospfd/ospf_packet.h"
33 #include "ospfd/ospf_spf.h"
34 #include "ospfd/ospf_dump.h"
35 #include "ospfd/ospf_route.h"
36 #include "ospfd/ospf_ase.h"
37 #include "ospfd/ospf_zebra.h"
38 #include "ospfd/ospf_abr.h"
39 #include "ospfd/ospf_errors.h"
41 static struct ospf_lsa
*ospf_handle_summarylsa_lsId_chg(struct ospf
*ospf
,
42 struct prefix_ipv4
*p
,
45 struct in_addr old_id
);
46 static struct ospf_lsa
*
47 ospf_summary_lsa_prepare_and_flood(struct prefix_ipv4
*p
, uint32_t metric
,
48 struct ospf_area
*area
, struct in_addr id
);
49 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
50 struct ospf_lsa
*lsa
);
51 static struct ospf_lsa
*
52 ospf_asbr_summary_lsa_prepare_and_flood(struct prefix_ipv4
*p
, uint32_t metric
,
53 struct ospf_area
*area
,
55 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
56 struct ospf_lsa
*lsa
);
57 static struct ospf_lsa
*ospf_handle_exnl_lsa_lsId_chg(struct ospf
*ospf
,
58 struct external_info
*ei
,
60 static struct ospf_lsa
*
61 ospf_exnl_lsa_prepare_and_flood(struct ospf
*ospf
, struct external_info
*ei
,
64 uint32_t get_metric(uint8_t *metric
)
68 m
= (m
<< 8) + metric
[1];
69 m
= (m
<< 8) + metric
[2];
74 struct timeval
int2tv(int a
)
84 struct timeval
msec2tv(int a
)
88 ret
.tv_sec
= a
/ 1000;
89 ret
.tv_usec
= (a
% 1000) * 1000;
94 int ospf_lsa_refresh_delay(struct ospf_lsa
*lsa
)
99 if (monotime_since(&lsa
->tv_orig
, &delta
)
100 < OSPF_MIN_LS_INTERVAL
* 1000LL) {
101 struct timeval minv
= msec2tv(OSPF_MIN_LS_INTERVAL
);
102 timersub(&minv
, &delta
, &minv
);
104 /* TBD: remove padding to full sec, return timeval instead */
105 delay
= minv
.tv_sec
+ !!minv
.tv_usec
;
107 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
109 "LSA[Type%d:%pI4]: Refresh timer delay %d seconds",
110 lsa
->data
->type
, &lsa
->data
->id
,
120 int get_age(struct ospf_lsa
*lsa
)
124 monotime_since(&lsa
->tv_recv
, &rel
);
125 return ntohs(lsa
->data
->ls_age
) + rel
.tv_sec
;
129 /* Fletcher Checksum -- Refer to RFC1008. */
131 /* All the offsets are zero-based. The offsets in the RFC1008 are
133 uint16_t ospf_lsa_checksum(struct lsa_header
*lsa
)
135 uint8_t *buffer
= &lsa
->options
;
136 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
138 /* Skip the AGE field */
139 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
141 /* Checksum offset starts from "options" field, not the beginning of the
142 lsa_header struct. The offset is 14, rather than 16. */
143 int checksum_offset
= (uint8_t *)&lsa
->checksum
- buffer
;
145 return fletcher_checksum(buffer
, len
, checksum_offset
);
148 int ospf_lsa_checksum_valid(struct lsa_header
*lsa
)
150 uint8_t *buffer
= &lsa
->options
;
151 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
153 /* Skip the AGE field */
154 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
156 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
161 /* Create OSPF LSA. */
162 struct ospf_lsa
*ospf_lsa_new(void)
164 struct ospf_lsa
*new;
166 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
170 new->retransmit_counter
= 0;
171 monotime(&new->tv_recv
);
172 new->tv_orig
= new->tv_recv
;
173 new->refresh_list
= -1;
174 new->vrf_id
= VRF_DEFAULT
;
175 new->to_be_acknowledged
= 0;
176 new->opaque_zero_len_delete
= 0;
181 struct ospf_lsa
*ospf_lsa_new_and_data(size_t size
)
183 struct ospf_lsa
*new;
185 new = ospf_lsa_new();
186 new->data
= ospf_lsa_data_new(size
);
192 /* Duplicate OSPF LSA. */
193 struct ospf_lsa
*ospf_lsa_dup(struct ospf_lsa
*lsa
)
195 struct ospf_lsa
*new;
200 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
202 memcpy(new, lsa
, sizeof(struct ospf_lsa
));
203 UNSET_FLAG(new->flags
, OSPF_LSA_DISCARD
);
205 new->retransmit_counter
= 0;
206 new->data
= ospf_lsa_data_dup(lsa
->data
);
208 /* kevinm: Clear the refresh_list, otherwise there are going
209 to be problems when we try to remove the LSA from the
210 queue (which it's not a member of.)
211 XXX: Should we add the LSA to the refresh_list queue? */
212 new->refresh_list
= -1;
214 if (IS_DEBUG_OSPF(lsa
, LSA
))
215 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa
,
222 void ospf_lsa_free(struct ospf_lsa
*lsa
)
224 assert(lsa
->lock
== 0);
226 if (IS_DEBUG_OSPF(lsa
, LSA
))
227 zlog_debug("LSA: freed %p", (void *)lsa
);
229 /* Delete LSA data. */
230 if (lsa
->data
!= NULL
)
231 ospf_lsa_data_free(lsa
->data
);
233 assert(lsa
->refresh_list
< 0);
235 memset(lsa
, 0, sizeof(struct ospf_lsa
));
236 XFREE(MTYPE_OSPF_LSA
, lsa
);
240 struct ospf_lsa
*ospf_lsa_lock(struct ospf_lsa
*lsa
)
247 void ospf_lsa_unlock(struct ospf_lsa
**lsa
)
249 /* This is sanity check. */
255 assert((*lsa
)->lock
>= 0);
257 if ((*lsa
)->lock
== 0) {
258 assert(CHECK_FLAG((*lsa
)->flags
, OSPF_LSA_DISCARD
));
264 /* Check discard flag. */
265 void ospf_lsa_discard(struct ospf_lsa
*lsa
)
267 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
268 SET_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
);
269 ospf_lsa_unlock(&lsa
);
273 /* Create LSA data. */
274 struct lsa_header
*ospf_lsa_data_new(size_t size
)
276 return XCALLOC(MTYPE_OSPF_LSA_DATA
, size
);
279 /* Duplicate LSA data. */
280 struct lsa_header
*ospf_lsa_data_dup(struct lsa_header
*lsah
)
282 struct lsa_header
*new;
284 new = ospf_lsa_data_new(ntohs(lsah
->length
));
285 memcpy(new, lsah
, ntohs(lsah
->length
));
291 void ospf_lsa_data_free(struct lsa_header
*lsah
)
293 if (IS_DEBUG_OSPF(lsa
, LSA
))
294 zlog_debug("LSA[Type%d:%pI4]: data freed %p", lsah
->type
,
295 &lsah
->id
, (void *)lsah
);
297 XFREE(MTYPE_OSPF_LSA_DATA
, lsah
);
301 /* LSA general functions. */
303 const char *dump_lsa_key(struct ospf_lsa
*lsa
)
305 static char buf
[sizeof("Type255,id(255.255.255.255),ar(255.255.255.255)")+1];
306 struct lsa_header
*lsah
;
308 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
) {
309 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
310 inet_ntop(AF_INET
, &lsah
->id
, id
, sizeof(id
));
311 inet_ntop(AF_INET
, &lsah
->adv_router
, ar
, sizeof(ar
));
313 snprintf(buf
, sizeof(buf
), "Type%d,id(%s),ar(%s)", lsah
->type
,
316 strlcpy(buf
, "NULL", sizeof(buf
));
321 uint32_t lsa_seqnum_increment(struct ospf_lsa
*lsa
)
325 seqnum
= ntohl(lsa
->data
->ls_seqnum
) + 1;
327 return htonl(seqnum
);
330 void lsa_header_set(struct stream
*s
, uint8_t options
, uint8_t type
,
331 struct in_addr id
, struct in_addr router_id
)
333 struct lsa_header
*lsah
;
335 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
337 lsah
->ls_age
= htons(OSPF_LSA_INITIAL_AGE
);
338 lsah
->options
= options
;
341 lsah
->adv_router
= router_id
;
342 lsah
->ls_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
344 stream_forward_endp(s
, OSPF_LSA_HEADER_SIZE
);
348 /* router-LSA related functions. */
349 /* Get router-LSA flags. */
350 uint8_t router_lsa_flags(struct ospf_area
*area
)
354 flags
= area
->ospf
->flags
;
356 /* Set virtual link flag. */
357 if (ospf_full_virtual_nbrs(area
))
358 SET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
360 /* Just sanity check */
361 UNSET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
363 /* Set Shortcut ABR behabiour flag. */
364 UNSET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
365 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
366 if (!OSPF_IS_AREA_BACKBONE(area
))
367 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
368 && area
->ospf
->backbone
== NULL
)
369 || area
->shortcut_configured
370 == OSPF_SHORTCUT_ENABLE
)
371 SET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
373 /* ASBR can't exit in stub area. */
374 if (area
->external_routing
== OSPF_AREA_STUB
)
375 UNSET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
376 /* If ASBR set External flag */
377 else if (IS_OSPF_ASBR(area
->ospf
))
378 SET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
380 /* Set ABR dependent flags */
381 if (IS_OSPF_ABR(area
->ospf
)) {
382 SET_FLAG(flags
, ROUTER_LSA_BORDER
);
383 /* If Area is NSSA and we are both ABR and unconditional
385 * set Nt bit to inform other routers.
387 if ((area
->external_routing
== OSPF_AREA_NSSA
)
388 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
389 SET_FLAG(flags
, ROUTER_LSA_NT
);
394 /* Lookup neighbor other than myself.
395 And check neighbor count,
396 Point-to-Point link must have only 1 neighbor. */
397 struct ospf_neighbor
*ospf_nbr_lookup_ptop(struct ospf_interface
*oi
)
399 struct ospf_neighbor
*nbr
= NULL
;
400 struct route_node
*rn
;
402 /* Search neighbor, there must be one of two nbrs. */
403 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
404 if ((nbr
= rn
->info
))
405 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
406 &oi
->ospf
->router_id
))
407 if (nbr
->state
== NSM_Full
) {
408 route_unlock_node(rn
);
412 /* PtoP link must have only 1 neighbor. */
413 if (ospf_nbr_count(oi
, 0) > 1)
415 EC_OSPF_PTP_NEIGHBOR
,
416 "Point-to-Point link on interface %s has more than 1 neighbor.",
422 /* Determine cost of link, taking RFC3137 stub-router support into
425 static uint16_t ospf_link_cost(struct ospf_interface
*oi
)
427 /* RFC3137 stub router support */
428 if (!CHECK_FLAG(oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
429 return oi
->output_cost
;
431 return OSPF_OUTPUT_COST_INFINITE
;
434 /* Set a link information. */
435 char link_info_set(struct stream
**s
, struct in_addr id
, struct in_addr data
,
436 uint8_t type
, uint8_t tos
, uint16_t cost
)
438 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
439 * vast majority of cases. Some rare routers with lots of links need
441 * we try accommodate those here.
443 if (STREAM_WRITEABLE(*s
) < OSPF_ROUTER_LSA_LINK_SIZE
) {
444 size_t ret
= OSPF_MAX_LSA_SIZE
;
446 /* Can we enlarge the stream still? */
447 if (STREAM_SIZE(*s
) == OSPF_MAX_LSA_SIZE
) {
448 /* we futz the size here for simplicity, really we need
451 * IP Header - (sizeof(struct ip))
452 * OSPF Header - OSPF_HEADER_SIZE
453 * LSA Header - OSPF_LSA_HEADER_SIZE
454 * MD5 auth data, if MD5 is configured -
455 * OSPF_AUTH_MD5_SIZE.
457 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
459 ret
= stream_resize_inplace(
460 s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
463 if (ret
== OSPF_MAX_LSA_SIZE
) {
466 "%s: Out of space in LSA stream, left %zd, size %zd",
467 __func__
, STREAM_WRITEABLE(*s
),
473 /* TOS based routing is not supported. */
474 stream_put_ipv4(*s
, id
.s_addr
); /* Link ID. */
475 stream_put_ipv4(*s
, data
.s_addr
); /* Link Data. */
476 stream_putc(*s
, type
); /* Link Type. */
477 stream_putc(*s
, tos
); /* TOS = 0. */
478 stream_putw(*s
, cost
); /* Link Cost. */
483 /* Describe Point-to-Point link (Section 12.4.1.1). */
485 /* Note: If the interface is configured as point-to-point dmvpn then the other
486 * end of link is dmvpn hub with point-to-multipoint ospf network type. The
487 * hub then expects this router to populate the stub network and also Link Data
488 * Field set to IP Address and not MIB-II ifIndex
490 static int lsa_link_ptop_set(struct stream
**s
, struct ospf_interface
*oi
)
493 struct ospf_neighbor
*nbr
;
494 struct in_addr id
, mask
, data
;
495 uint16_t cost
= ospf_link_cost(oi
);
497 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
498 zlog_debug("LSA[Type1]: Set link Point-to-Point");
500 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
501 if (nbr
->state
== NSM_Full
) {
502 if (CHECK_FLAG(oi
->connected
->flags
,
503 ZEBRA_IFA_UNNUMBERED
)
505 /* For unnumbered point-to-point networks, the
507 should specify the interface's MIB-II ifIndex
509 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
510 links
+= link_info_set(
511 s
, nbr
->router_id
, data
,
512 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
514 links
+= link_info_set(
516 oi
->address
->u
.prefix4
,
517 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
521 /* no need for a stub link for unnumbered interfaces */
523 || !CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)) {
524 /* Regardless of the state of the neighboring router, we must
525 add a Type 3 link (stub network).
526 N.B. Options 1 & 2 share basically the same logic. */
527 masklen2ip(oi
->address
->prefixlen
, &mask
);
528 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
530 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
537 /* Describe Broadcast Link. */
538 static int lsa_link_broadcast_set(struct stream
**s
, struct ospf_interface
*oi
)
540 struct ospf_neighbor
*dr
;
541 struct in_addr id
, mask
;
542 uint16_t cost
= ospf_link_cost(oi
);
544 /* Describe Type 3 Link. */
545 if (oi
->state
== ISM_Waiting
) {
546 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
548 "LSA[Type1]: Interface %s is in state Waiting. Adding stub interface",
550 masklen2ip(oi
->address
->prefixlen
, &mask
);
551 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
552 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
556 dr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &DR(oi
));
557 /* Describe Type 2 link. */
558 if (dr
&& (dr
->state
== NSM_Full
559 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
)))
560 && ospf_nbr_count(oi
, NSM_Full
) > 0) {
561 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
563 "LSA[Type1]: Interface %s has a DR. Adding transit interface",
565 return link_info_set(s
, DR(oi
), oi
->address
->u
.prefix4
,
566 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
568 /* Describe type 3 link. */
570 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
572 "LSA[Type1]: Interface %s has no DR. Adding stub interface",
574 masklen2ip(oi
->address
->prefixlen
, &mask
);
575 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
576 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
581 static int lsa_link_loopback_set(struct stream
**s
, struct ospf_interface
*oi
)
583 struct in_addr id
, mask
;
585 /* Describe Type 3 Link. */
586 if (oi
->state
!= ISM_Loopback
)
589 mask
.s_addr
= 0xffffffff;
590 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
591 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
594 /* Describe Virtual Link. */
595 static int lsa_link_virtuallink_set(struct stream
**s
,
596 struct ospf_interface
*oi
)
598 struct ospf_neighbor
*nbr
;
599 uint16_t cost
= ospf_link_cost(oi
);
601 if (oi
->state
== ISM_PointToPoint
)
602 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
603 if (nbr
->state
== NSM_Full
) {
604 return link_info_set(s
, nbr
->router_id
,
605 oi
->address
->u
.prefix4
,
606 LSA_LINK_TYPE_VIRTUALLINK
,
613 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
615 /* this function add for support point-to-multipoint ,see rfc2328
617 /* from "edward rrr" <edward_rrr@hotmail.com>
618 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
619 static int lsa_link_ptomp_set(struct stream
**s
, struct ospf_interface
*oi
)
622 struct route_node
*rn
;
623 struct ospf_neighbor
*nbr
= NULL
;
624 struct in_addr id
, mask
;
625 uint16_t cost
= ospf_link_cost(oi
);
627 mask
.s_addr
= 0xffffffff;
628 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
629 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
631 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
632 zlog_debug("PointToMultipoint: running ptomultip_set");
634 /* Search neighbor, */
635 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
636 if ((nbr
= rn
->info
) != NULL
)
638 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
639 &oi
->ospf
->router_id
))
640 if (nbr
->state
== NSM_Full
)
643 links
+= link_info_set(
645 oi
->address
->u
.prefix4
,
646 LSA_LINK_TYPE_POINTOPOINT
, 0,
648 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
650 "PointToMultipoint: set link to %pI4",
651 &oi
->address
->u
.prefix4
);
657 /* Set router-LSA link information. */
658 static int router_lsa_link_set(struct stream
**s
, struct ospf_area
*area
)
660 struct listnode
*node
;
661 struct ospf_interface
*oi
;
664 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
665 struct interface
*ifp
= oi
->ifp
;
667 /* Check interface is up, OSPF is enable. */
668 if (if_is_operative(ifp
)) {
669 if (oi
->state
!= ISM_Down
) {
670 oi
->lsa_pos_beg
= links
;
671 /* Describe each link. */
673 case OSPF_IFTYPE_POINTOPOINT
:
674 links
+= lsa_link_ptop_set(s
, oi
);
676 case OSPF_IFTYPE_BROADCAST
:
677 links
+= lsa_link_broadcast_set(s
, oi
);
679 case OSPF_IFTYPE_NBMA
:
680 links
+= lsa_link_nbma_set(s
, oi
);
682 case OSPF_IFTYPE_POINTOMULTIPOINT
:
683 links
+= lsa_link_ptomp_set(s
, oi
);
685 case OSPF_IFTYPE_VIRTUALLINK
:
687 lsa_link_virtuallink_set(s
, oi
);
689 case OSPF_IFTYPE_LOOPBACK
:
690 links
+= lsa_link_loopback_set(s
, oi
);
692 oi
->lsa_pos_end
= links
;
700 /* Set router-LSA body. */
701 void ospf_router_lsa_body_set(struct stream
**s
, struct ospf_area
*area
)
707 stream_putc(*s
, router_lsa_flags(area
));
709 /* Set Zero fields. */
712 /* Keep pointer to # links. */
713 putp
= stream_get_endp(*s
);
718 /* Set all link information. */
719 cnt
= router_lsa_link_set(s
, area
);
721 /* Set # of links here. */
722 stream_putw_at(*s
, putp
, cnt
);
725 static void ospf_stub_router_timer(struct thread
*t
)
727 struct ospf_area
*area
= THREAD_ARG(t
);
729 area
->t_stub_router
= NULL
;
731 SET_FLAG(area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
733 /* clear stub route state and generate router-lsa refresh, don't
734 * clobber an administratively set stub-router state though.
736 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
739 UNSET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
741 ospf_router_lsa_update_area(area
);
744 static void ospf_stub_router_check(struct ospf_area
*area
)
746 /* area must either be administratively configured to be stub
747 * or startup-time stub-router must be configured and we must in a
751 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
)) {
752 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
756 /* not admin-stubbed, check whether startup stubbing is configured and
757 * whether it's not been done yet
759 if (CHECK_FLAG(area
->stub_router_state
,
760 OSPF_AREA_WAS_START_STUB_ROUTED
))
763 if (area
->ospf
->stub_router_startup_time
764 == OSPF_STUB_ROUTER_UNCONFIGURED
) {
765 /* stub-router is hence done forever for this area, even if
767 * tries configure it (take effect next restart).
769 SET_FLAG(area
->stub_router_state
,
770 OSPF_AREA_WAS_START_STUB_ROUTED
);
774 /* startup stub-router configured and not yet done */
775 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
777 OSPF_AREA_TIMER_ON(area
->t_stub_router
, ospf_stub_router_timer
,
778 area
->ospf
->stub_router_startup_time
);
781 /* Create new router-LSA. */
782 static struct ospf_lsa
*ospf_router_lsa_new(struct ospf_area
*area
)
784 struct ospf
*ospf
= area
->ospf
;
786 struct lsa_header
*lsah
;
787 struct ospf_lsa
*new;
790 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
791 zlog_debug("LSA[Type1]: Create router-LSA instance");
793 /* check whether stub-router is desired, and if this is the first
796 ospf_stub_router_check(area
);
798 /* Create a stream for LSA. */
799 s
= stream_new(OSPF_MAX_LSA_SIZE
);
800 /* Set LSA common header fields. */
801 lsa_header_set(s
, LSA_OPTIONS_GET(area
) | LSA_OPTIONS_NSSA_GET(area
),
802 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
804 /* Set router-LSA body fields. */
805 ospf_router_lsa_body_set(&s
, area
);
808 length
= stream_get_endp(s
);
809 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
810 lsah
->length
= htons(length
);
812 /* Now, create OSPF LSA instance. */
813 new = ospf_lsa_new_and_data(length
);
816 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
817 new->vrf_id
= area
->ospf
->vrf_id
;
819 /* Copy LSA data to store, discard stream. */
820 memcpy(new->data
, lsah
, length
);
826 /* Originate Router-LSA. */
827 static struct ospf_lsa
*ospf_router_lsa_originate(struct ospf_area
*area
)
829 struct ospf_lsa
*new;
831 if (area
->ospf
->gr_info
.restart_in_progress
) {
832 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
834 "LSA[Type%d]: Graceful Restart in progress, don't originate",
839 /* Create new router-LSA instance. */
840 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
841 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
846 if (new->data
->adv_router
.s_addr
== INADDR_ANY
) {
847 if (IS_DEBUG_OSPF_EVENT
)
848 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
849 ospf_lsa_discard(new);
853 /* Install LSA to LSDB. */
854 new = ospf_lsa_install(area
->ospf
, NULL
, new);
856 /* Update LSA origination count. */
857 area
->ospf
->lsa_originate_count
++;
859 /* Flooding new LSA through area. */
860 ospf_flood_through_area(area
, NULL
, new);
862 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
863 zlog_debug("LSA[Type%d:%pI4]: Originate router-LSA %p",
864 new->data
->type
, &new->data
->id
,
866 ospf_lsa_header_dump(new->data
);
872 /* Refresh router-LSA. */
873 static struct ospf_lsa
*ospf_router_lsa_refresh(struct ospf_lsa
*lsa
)
875 struct ospf_area
*area
= lsa
->area
;
876 struct ospf_lsa
*new;
881 /* Delete LSA from neighbor retransmit-list. */
882 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
884 /* Unregister LSA from refresh-list */
885 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
887 /* Create new router-LSA instance. */
888 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
889 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
893 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
895 ospf_lsa_install(area
->ospf
, NULL
, new);
897 /* Flood LSA through area. */
898 ospf_flood_through_area(area
, NULL
, new);
901 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
902 zlog_debug("LSA[Type%d:%pI4]: router-LSA refresh",
903 new->data
->type
, &new->data
->id
);
904 ospf_lsa_header_dump(new->data
);
910 int ospf_router_lsa_update_area(struct ospf_area
*area
)
912 if (IS_DEBUG_OSPF_EVENT
)
913 zlog_debug("[router-LSA]: (router-LSA area update)");
915 /* Now refresh router-LSA. */
916 if (area
->router_lsa_self
)
917 ospf_lsa_refresh(area
->ospf
, area
->router_lsa_self
);
918 /* Newly originate router-LSA. */
920 ospf_router_lsa_originate(area
);
925 int ospf_router_lsa_update(struct ospf
*ospf
)
927 struct listnode
*node
, *nnode
;
928 struct ospf_area
*area
;
930 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
931 zlog_debug("Timer[router-LSA Update]: (timer expire)");
933 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
934 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
935 struct router_lsa
*rl
;
936 const char *area_str
;
938 /* Keep Area ID string. */
939 area_str
= AREA_NAME(area
);
941 /* If LSA not exist in this Area, originate new. */
943 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
945 "LSA[Type1]: Create router-LSA for Area %s",
948 ospf_router_lsa_originate(area
);
950 /* If router-ID is changed, Link ID must change.
951 First flush old LSA, then originate new. */
952 else if (!IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
)) {
953 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
955 "LSA[Type%d:%pI4]: Refresh router-LSA for Area %s",
957 &lsa
->data
->id
, area_str
);
958 ospf_refresher_unregister_lsa(ospf
, lsa
);
959 ospf_lsa_flush_area(lsa
, area
);
960 ospf_lsa_unlock(&area
->router_lsa_self
);
961 area
->router_lsa_self
= NULL
;
963 /* Refresh router-LSA, (not install) and flood through
965 ospf_router_lsa_update_area(area
);
967 rl
= (struct router_lsa
*)lsa
->data
;
968 /* Refresh router-LSA, (not install) and flood through
970 if (rl
->flags
!= ospf
->flags
)
971 ospf_router_lsa_update_area(area
);
979 /* network-LSA related functions. */
980 /* Originate Network-LSA. */
981 static void ospf_network_lsa_body_set(struct stream
*s
,
982 struct ospf_interface
*oi
)
985 struct route_node
*rn
;
986 struct ospf_neighbor
*nbr
;
988 masklen2ip(oi
->address
->prefixlen
, &mask
);
989 stream_put_ipv4(s
, mask
.s_addr
);
991 /* The network-LSA lists those routers that are fully adjacent to
992 the Designated Router; each fully adjacent router is identified by
993 its OSPF Router ID. The Designated Router includes itself in this
994 list. RFC2328, Section 12.4.2 */
996 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
997 if ((nbr
= rn
->info
) != NULL
)
998 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
999 stream_put_ipv4(s
, nbr
->router_id
.s_addr
);
1002 static struct ospf_lsa
*ospf_network_lsa_new(struct ospf_interface
*oi
)
1005 struct ospf_lsa
*new;
1006 struct lsa_header
*lsah
;
1007 struct ospf_if_params
*oip
;
1010 /* If there are no neighbours on this network (the net is stub),
1011 the router does not originate network-LSA (see RFC 12.4.2) */
1012 if (oi
->full_nbrs
== 0)
1015 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1016 zlog_debug("LSA[Type2]: Create network-LSA instance");
1018 /* Create new stream for LSA. */
1019 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1020 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1022 lsa_header_set(s
, (OPTIONS(oi
) | LSA_OPTIONS_GET(oi
->area
)),
1023 OSPF_NETWORK_LSA
, DR(oi
), oi
->ospf
->router_id
);
1025 /* Set network-LSA body fields. */
1026 ospf_network_lsa_body_set(s
, oi
);
1029 length
= stream_get_endp(s
);
1030 lsah
->length
= htons(length
);
1032 /* Create OSPF LSA instance. */
1033 new = ospf_lsa_new_and_data(length
);
1035 new->area
= oi
->area
;
1036 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1037 new->vrf_id
= oi
->ospf
->vrf_id
;
1039 /* Copy LSA to store. */
1040 memcpy(new->data
, lsah
, length
);
1043 /* Remember prior network LSA sequence numbers, even if we stop
1044 * originating one for this oi, to try avoid re-originating LSAs with a
1045 * prior sequence number, and thus speed up adjency forming &
1048 if ((oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
))) {
1049 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1050 new->data
->ls_seqnum
= lsa_seqnum_increment(new);
1052 oip
= ospf_get_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1053 ospf_if_update_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1055 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1060 /* Originate network-LSA. */
1061 void ospf_network_lsa_update(struct ospf_interface
*oi
)
1063 struct ospf_lsa
*new;
1065 if (oi
->area
->ospf
->gr_info
.restart_in_progress
) {
1066 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1068 "LSA[Type%d]: Graceful Restart in progress, don't originate",
1073 if (oi
->network_lsa_self
!= NULL
) {
1074 ospf_lsa_refresh(oi
->ospf
, oi
->network_lsa_self
);
1078 /* Create new network-LSA instance. */
1079 new = ospf_network_lsa_new(oi
);
1083 /* Install LSA to LSDB. */
1084 new = ospf_lsa_install(oi
->ospf
, oi
, new);
1086 /* Update LSA origination count. */
1087 oi
->ospf
->lsa_originate_count
++;
1089 /* Flooding new LSA through area. */
1090 ospf_flood_through_area(oi
->area
, NULL
, new);
1092 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1093 zlog_debug("LSA[Type%d:%pI4]: Originate network-LSA %p",
1094 new->data
->type
, &new->data
->id
,
1096 ospf_lsa_header_dump(new->data
);
1102 static struct ospf_lsa
*ospf_network_lsa_refresh(struct ospf_lsa
*lsa
)
1104 struct ospf_area
*area
= lsa
->area
;
1105 struct ospf_lsa
*new, *new2
;
1106 struct ospf_if_params
*oip
;
1107 struct ospf_interface
*oi
;
1111 /* Retrieve the oi for the network LSA */
1112 oi
= ospf_if_lookup_by_local_addr(area
->ospf
, NULL
, lsa
->data
->id
);
1114 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1116 "LSA[Type%d:%pI4]: network-LSA refresh: no oi found, ick, ignoring.",
1117 lsa
->data
->type
, &lsa
->data
->id
);
1118 ospf_lsa_header_dump(lsa
->data
);
1122 /* Delete LSA from neighbor retransmit-list. */
1123 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
1125 /* Unregister LSA from refresh-list */
1126 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
1128 /* Create new network-LSA instance. */
1129 new = ospf_network_lsa_new(oi
);
1133 oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1134 assert(oip
!= NULL
);
1135 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
=
1136 lsa_seqnum_increment(lsa
);
1138 new2
= ospf_lsa_install(area
->ospf
, oi
, new);
1140 assert(new2
== new);
1142 /* Flood LSA through aera. */
1143 ospf_flood_through_area(area
, NULL
, new);
1145 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1146 zlog_debug("LSA[Type%d:%pI4]: network-LSA refresh",
1147 new->data
->type
, &new->data
->id
);
1148 ospf_lsa_header_dump(new->data
);
1154 static void stream_put_ospf_metric(struct stream
*s
, uint32_t metric_value
)
1159 /* Put 0 metric. TOS metric is not supported. */
1160 metric
= htonl(metric_value
);
1161 mp
= (char *)&metric
;
1163 stream_put(s
, mp
, 3);
1166 /* summary-LSA related functions. */
1167 static void ospf_summary_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1170 struct in_addr mask
;
1172 masklen2ip(p
->prefixlen
, &mask
);
1174 /* Put Network Mask. */
1175 stream_put_ipv4(s
, mask
.s_addr
);
1178 stream_putc(s
, (uint8_t)0);
1181 stream_put_ospf_metric(s
, metric
);
1184 static struct ospf_lsa
*ospf_summary_lsa_new(struct ospf_area
*area
,
1185 struct prefix
*p
, uint32_t metric
,
1189 struct ospf_lsa
*new;
1190 struct lsa_header
*lsah
;
1193 if (id
.s_addr
== 0xffffffff) {
1194 /* Maybe Link State ID not available. */
1195 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1197 "LSA[Type%d]: Link ID not available, can't originate",
1202 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1203 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1205 /* Create new stream for LSA. */
1206 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1207 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1209 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_SUMMARY_LSA
, id
,
1210 area
->ospf
->router_id
);
1212 /* Set summary-LSA body fields. */
1213 ospf_summary_lsa_body_set(s
, p
, metric
);
1216 length
= stream_get_endp(s
);
1217 lsah
->length
= htons(length
);
1219 /* Create OSPF LSA instance. */
1220 new = ospf_lsa_new_and_data(length
);
1222 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1223 new->vrf_id
= area
->ospf
->vrf_id
;
1225 /* Copy LSA to store. */
1226 memcpy(new->data
, lsah
, length
);
1232 /* Originate Summary-LSA. */
1233 static struct ospf_lsa
*
1234 ospf_summary_lsa_prepare_and_flood(struct prefix_ipv4
*p
, uint32_t metric
,
1235 struct ospf_area
*area
, struct in_addr id
)
1237 struct ospf_lsa
*new;
1239 /* Create new summary-LSA instance. */
1240 if (!(new = ospf_summary_lsa_new(area
, (struct prefix
*)p
, metric
, id
)))
1243 /* Instlal LSA to LSDB. */
1244 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1246 /* Update LSA origination count. */
1247 area
->ospf
->lsa_originate_count
++;
1249 /* Flooding new LSA through area. */
1250 ospf_flood_through_area(area
, NULL
, new);
1252 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1253 zlog_debug("LSA[Type%d:%pI4]: Originate summary-LSA %p",
1254 new->data
->type
, &new->data
->id
,
1256 ospf_lsa_header_dump(new->data
);
1262 static struct ospf_lsa
*ospf_handle_summarylsa_lsId_chg(struct ospf
*ospf
,
1263 struct prefix_ipv4
*p
,
1266 struct in_addr old_id
)
1268 struct ospf_lsa
*lsa
= NULL
;
1269 struct ospf_lsa
*new = NULL
;
1270 struct summary_lsa
*sl
= NULL
;
1271 struct ospf_area
*old_area
= NULL
;
1272 struct prefix_ipv4 old_prefix
;
1273 uint32_t old_metric
;
1274 struct in_addr mask
;
1275 uint32_t metric_val
;
1278 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, p
->prefix
,
1282 flog_warn(EC_OSPF_LSA_NULL
, "(%s): LSA not found", __func__
);
1286 sl
= (struct summary_lsa
*)lsa
->data
;
1288 old_area
= lsa
->area
;
1289 old_metric
= GET_METRIC(sl
->metric
);
1290 old_prefix
.prefix
= sl
->header
.id
;
1291 old_prefix
.prefixlen
= ip_masklen(sl
->mask
);
1292 old_prefix
.family
= AF_INET
;
1295 /* change the mask */
1296 masklen2ip(p
->prefixlen
, &mask
);
1297 sl
->mask
.s_addr
= mask
.s_addr
;
1299 /* Copy the metric*/
1300 metric_val
= htonl(metric
);
1301 metric_buf
= (char *)&metric_val
;
1302 memcpy(sl
->metric
, metric_buf
, sizeof(metric_val
));
1304 if (type
== OSPF_SUMMARY_LSA
) {
1305 /*Refresh the LSA with new LSA*/
1306 ospf_summary_lsa_refresh(ospf
, lsa
);
1308 new = ospf_summary_lsa_prepare_and_flood(
1309 &old_prefix
, old_metric
, old_area
, old_id
);
1311 /*Refresh the LSA with new LSA*/
1312 ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
1314 new = ospf_asbr_summary_lsa_prepare_and_flood(
1315 &old_prefix
, old_metric
, old_area
, old_id
);
1321 /* Originate Summary-LSA. */
1322 struct ospf_lsa
*ospf_summary_lsa_originate(struct prefix_ipv4
*p
,
1324 struct ospf_area
*area
)
1327 enum lsid_status status
;
1328 struct ospf_lsa
*new = NULL
;
1330 status
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
,
1333 if (status
== LSID_CHANGE
) {
1334 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1335 zlog_debug("Link ID has to be changed.");
1337 new = ospf_handle_summarylsa_lsId_chg(
1338 area
->ospf
, p
, OSPF_SUMMARY_LSA
, metric
, id
);
1340 } else if (status
== LSID_NOT_AVAILABLE
) {
1341 /* Link State ID not available. */
1342 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1344 "LSA[Type5]: Link ID not available, can't originate");
1349 new = ospf_summary_lsa_prepare_and_flood(p
, metric
, area
, id
);
1353 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
1354 struct ospf_lsa
*lsa
)
1356 struct ospf_lsa
*new;
1357 struct summary_lsa
*sl
;
1363 sl
= (struct summary_lsa
*)lsa
->data
;
1364 p
.prefixlen
= ip_masklen(sl
->mask
);
1365 new = ospf_summary_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1371 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1373 ospf_lsa_install(ospf
, NULL
, new);
1375 /* Flood LSA through AS. */
1376 ospf_flood_through_area(new->area
, NULL
, new);
1378 /* Debug logging. */
1379 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1380 zlog_debug("LSA[Type%d:%pI4]: summary-LSA refresh",
1381 new->data
->type
, &new->data
->id
);
1382 ospf_lsa_header_dump(new->data
);
1389 /* summary-ASBR-LSA related functions. */
1390 static void ospf_summary_asbr_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1393 /* Put Network Mask. */
1394 stream_put_ipv4(s
, (uint32_t)0);
1397 stream_putc(s
, (uint8_t)0);
1400 stream_put_ospf_metric(s
, metric
);
1403 static struct ospf_lsa
*ospf_summary_asbr_lsa_new(struct ospf_area
*area
,
1409 struct ospf_lsa
*new;
1410 struct lsa_header
*lsah
;
1413 if (id
.s_addr
== 0xffffffff) {
1414 /* Maybe Link State ID not available. */
1415 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1417 "LSA[Type%d]: Link ID not available, can't originate",
1418 OSPF_ASBR_SUMMARY_LSA
);
1422 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1423 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1425 /* Create new stream for LSA. */
1426 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1427 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1429 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_ASBR_SUMMARY_LSA
, id
,
1430 area
->ospf
->router_id
);
1432 /* Set summary-LSA body fields. */
1433 ospf_summary_asbr_lsa_body_set(s
, p
, metric
);
1436 length
= stream_get_endp(s
);
1437 lsah
->length
= htons(length
);
1439 /* Create OSPF LSA instance. */
1440 new = ospf_lsa_new_and_data(length
);
1442 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1443 new->vrf_id
= area
->ospf
->vrf_id
;
1445 /* Copy LSA to store. */
1446 memcpy(new->data
, lsah
, length
);
1452 /* Originate summary-ASBR-LSA. */
1453 static struct ospf_lsa
*
1454 ospf_asbr_summary_lsa_prepare_and_flood(struct prefix_ipv4
*p
, uint32_t metric
,
1455 struct ospf_area
*area
,
1458 struct ospf_lsa
*new;
1460 /* Create new summary-LSA instance. */
1461 new = ospf_summary_asbr_lsa_new(area
, (struct prefix
*)p
, metric
, id
);
1465 /* Install LSA to LSDB. */
1466 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1468 /* Update LSA origination count. */
1469 area
->ospf
->lsa_originate_count
++;
1471 /* Flooding new LSA through area. */
1472 ospf_flood_through_area(area
, NULL
, new);
1474 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1475 zlog_debug("LSA[Type%d:%pI4]: Originate summary-ASBR-LSA %p",
1476 new->data
->type
, &new->data
->id
,
1478 ospf_lsa_header_dump(new->data
);
1484 struct ospf_lsa
*ospf_summary_asbr_lsa_originate(struct prefix_ipv4
*p
,
1486 struct ospf_area
*area
)
1488 struct ospf_lsa
*new;
1490 enum lsid_status status
;
1492 status
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
,
1493 OSPF_ASBR_SUMMARY_LSA
, p
, &id
);
1495 if (status
== LSID_CHANGE
) {
1496 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1497 zlog_debug("Link ID has to be changed.");
1499 new = ospf_handle_summarylsa_lsId_chg(
1500 area
->ospf
, p
, OSPF_ASBR_SUMMARY_LSA
, metric
, id
);
1502 } else if (status
== LSID_NOT_AVAILABLE
) {
1503 /* Link State ID not available. */
1504 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1506 "LSA[Type5]: Link ID not available, can't originate");
1511 new = ospf_asbr_summary_lsa_prepare_and_flood(p
, metric
, area
, id
);
1515 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
1516 struct ospf_lsa
*lsa
)
1518 struct ospf_lsa
*new;
1519 struct summary_lsa
*sl
;
1525 sl
= (struct summary_lsa
*)lsa
->data
;
1526 p
.prefixlen
= ip_masklen(sl
->mask
);
1527 new = ospf_summary_asbr_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1532 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1534 ospf_lsa_install(ospf
, NULL
, new);
1536 /* Flood LSA through area. */
1537 ospf_flood_through_area(new->area
, NULL
, new);
1539 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1540 zlog_debug("LSA[Type%d:%pI4]: summary-ASBR-LSA refresh",
1541 new->data
->type
, &new->data
->id
);
1542 ospf_lsa_header_dump(new->data
);
1548 /* AS-external-LSA related functions. */
1550 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1551 is connected, else 0*/
1552 static struct in_addr
ospf_external_lsa_nexthop_get(struct ospf
*ospf
,
1553 struct in_addr nexthop
)
1557 struct listnode
*node
;
1558 struct ospf_interface
*oi
;
1562 if (!nexthop
.s_addr
)
1565 /* Check whether nexthop is covered by OSPF network. */
1566 nh
.family
= AF_INET
;
1567 nh
.u
.prefix4
= nexthop
;
1568 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1570 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1571 * better to make use of the per-ifp table of ois.
1573 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1574 if (if_is_operative(oi
->ifp
))
1575 if (oi
->address
->family
== AF_INET
)
1576 if (prefix_match(oi
->address
, &nh
))
1582 /* NSSA-external-LSA related functions. */
1584 /* Get 1st IP connection for Forward Addr */
1586 struct in_addr
ospf_get_ip_from_ifp(struct ospf_interface
*oi
)
1590 fwd
.s_addr
= INADDR_ANY
;
1592 if (if_is_operative(oi
->ifp
))
1593 return oi
->address
->u
.prefix4
;
1598 /* Get 1st IP connection for Forward Addr */
1599 struct in_addr
ospf_get_nssa_ip(struct ospf_area
*area
)
1602 struct in_addr best_default
;
1603 struct listnode
*node
;
1604 struct ospf_interface
*oi
;
1607 best_default
.s_addr
= 0;
1609 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->oiflist
, node
, oi
)) {
1610 if (if_is_operative(oi
->ifp
))
1611 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1613 && oi
->address
->family
== AF_INET
) {
1614 if (best_default
.s_addr
== INADDR_ANY
)
1616 oi
->address
->u
.prefix4
;
1617 if (oi
->area
== area
)
1618 return oi
->address
->u
.prefix4
;
1621 if (best_default
.s_addr
!= INADDR_ANY
)
1622 return best_default
;
1624 if (best_default
.s_addr
!= INADDR_ANY
)
1625 return best_default
;
1630 int metric_type(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1632 struct ospf_redist
*red
;
1634 red
= ospf_redist_lookup(ospf
, src
, instance
);
1636 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
1637 : red
->dmetric
.type
);
1640 int metric_value(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1642 struct ospf_redist
*red
;
1644 red
= ospf_redist_lookup(ospf
, src
, instance
);
1645 if (!red
|| red
->dmetric
.value
< 0) {
1646 if (src
== DEFAULT_ROUTE
) {
1647 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1648 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1650 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1651 } else if (ospf
->default_metric
< 0)
1652 return DEFAULT_DEFAULT_METRIC
;
1654 return ospf
->default_metric
;
1657 return red
->dmetric
.value
;
1660 /* Set AS-external-LSA body. */
1661 static void ospf_external_lsa_body_set(struct stream
*s
,
1662 struct external_info
*ei
,
1665 struct prefix_ipv4
*p
= &ei
->p
;
1666 struct in_addr mask
, fwd_addr
;
1670 unsigned short instance
;
1672 /* Put Network Mask. */
1673 masklen2ip(p
->prefixlen
, &mask
);
1674 stream_put_ipv4(s
, mask
.s_addr
);
1676 /* If prefix is default, specify DEFAULT_ROUTE. */
1677 type
= is_default_prefix4(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1678 instance
= is_default_prefix4(&ei
->p
) ? 0 : ei
->instance
;
1680 mtype
= (ROUTEMAP_METRIC_TYPE(ei
) != -1)
1681 ? ROUTEMAP_METRIC_TYPE(ei
)
1682 : metric_type(ospf
, type
, instance
);
1684 mvalue
= (ROUTEMAP_METRIC(ei
) != -1)
1685 ? ROUTEMAP_METRIC(ei
)
1686 : metric_value(ospf
, type
, instance
);
1688 /* Put type of external metric. */
1689 stream_putc(s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1691 /* Put 0 metric. TOS metric is not supported. */
1692 stream_put_ospf_metric(s
, mvalue
);
1694 /* Get forwarding address to nexthop if on the Connection List, else 0.
1696 fwd_addr
= ospf_external_lsa_nexthop_get(ospf
, ei
->nexthop
);
1698 /* Put forwarding address. */
1699 stream_put_ipv4(s
, fwd_addr
.s_addr
);
1702 stream_putl(s
, ei
->tag
);
1705 /* Create new external-LSA. */
1706 static struct ospf_lsa
*
1707 ospf_exnl_lsa_prepare_and_flood(struct ospf
*ospf
, struct external_info
*ei
,
1711 struct lsa_header
*lsah
;
1712 struct ospf_lsa
*new;
1715 /* Create new stream for LSA. */
1716 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1717 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1719 /* Set LSA common header fields. */
1720 lsa_header_set(s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
, id
,
1723 /* Set AS-external-LSA body fields. */
1724 ospf_external_lsa_body_set(s
, ei
, ospf
);
1727 length
= stream_get_endp(s
);
1728 lsah
->length
= htons(length
);
1730 /* Now, create OSPF LSA instance. */
1731 new = ospf_lsa_new_and_data(length
);
1733 SET_FLAG(new->flags
,
1734 OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1735 new->vrf_id
= ospf
->vrf_id
;
1737 /* Copy LSA data to store, discard stream. */
1738 memcpy(new->data
, lsah
, length
);
1744 static struct ospf_lsa
*ospf_handle_exnl_lsa_lsId_chg(struct ospf
*ospf
,
1745 struct external_info
*ei
,
1748 struct ospf_lsa
*lsa
;
1749 struct as_external_lsa
*al
;
1750 struct in_addr mask
;
1751 struct ospf_lsa
*new;
1752 struct external_info ei_summary
= {};
1753 struct external_info
*ei_old
;
1755 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
,
1756 ei
->p
.prefix
, ospf
->router_id
);
1759 flog_warn(EC_OSPF_LSA_NULL
, "(%s): LSA not found", __func__
);
1763 ei_old
= ospf_external_info_check(ospf
, lsa
);
1765 al
= (struct as_external_lsa
*)lsa
->data
;
1768 /* eii_old pointer of LSA is NULL, this
1769 * must be external aggregate route.
1771 ei_summary
.p
.family
= AF_INET
;
1772 ei_summary
.p
.prefix
= al
->header
.id
;
1773 ei_summary
.p
.prefixlen
= ip_masklen(al
->mask
);
1774 ei_summary
.tag
= (unsigned long)ntohl(al
->e
[0].route_tag
);
1775 ei_old
= &ei_summary
;
1778 /* change the mask */
1779 masklen2ip(ei
->p
.prefixlen
, &mask
);
1780 al
->mask
.s_addr
= mask
.s_addr
;
1782 /*Refresh the LSA with new LSA*/
1783 ospf_external_lsa_refresh(ospf
, lsa
, ei
, LSA_REFRESH_FORCE
, 0);
1785 /*Originate the old LSA with changed LSID*/
1786 new = ospf_exnl_lsa_prepare_and_flood(ospf
, ei_old
, id
);
1791 static struct ospf_lsa
*ospf_external_lsa_new(struct ospf
*ospf
,
1792 struct external_info
*ei
,
1793 struct in_addr
*old_id
)
1795 struct ospf_lsa
*new;
1797 enum lsid_status status
;
1800 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1802 "LSA[Type5]: External info is NULL, can't originate");
1806 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1807 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1809 /* If old Link State ID is specified, refresh LSA with same ID. */
1812 /* Get Link State with unique ID. */
1814 status
= ospf_lsa_unique_id(ospf
, ospf
->lsdb
,
1815 OSPF_AS_EXTERNAL_LSA
, &ei
->p
, &id
);
1817 if (status
== LSID_CHANGE
) {
1818 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1819 zlog_debug("Link ID has to be changed.");
1821 new = ospf_handle_exnl_lsa_lsId_chg(ospf
, ei
, id
);
1823 } else if (status
== LSID_NOT_AVAILABLE
) {
1824 /* Link State ID not available. */
1825 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1827 "LSA[Type5]: Link ID not available, can't originate");
1832 new = ospf_exnl_lsa_prepare_and_flood(ospf
, ei
, id
);
1838 static void ospf_install_flood_nssa(struct ospf
*ospf
, struct ospf_lsa
*lsa
,
1839 struct external_info
*ei
)
1841 struct ospf_lsa
*new;
1842 struct as_external_lsa
*extlsa
;
1843 struct ospf_area
*area
;
1844 struct listnode
*node
, *nnode
;
1846 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1847 * which originated from an NSSA area. In which case it should not be
1848 * flooded back to NSSA areas.
1850 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1853 /* NSSA Originate or Refresh (If anyNSSA)
1855 LSA is self-originated. And just installed as Type-5.
1856 Additionally, install as Type-7 LSDB for every attached NSSA.
1858 P-Bit controls which ABR performs translation to outside world; If
1859 we are an ABR....do not set the P-bit, because we send the Type-5,
1860 not as the ABR Translator, but as the ASBR owner within the AS!
1862 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1863 elected ABR Translator will see the P-bit, Translate, and re-flood.
1865 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1866 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1868 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
1869 /* Don't install Type-7 LSA's into nonNSSA area */
1870 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1873 /* make lsa duplicate, lock=1 */
1874 new = ospf_lsa_dup(lsa
);
1876 new->data
->type
= OSPF_AS_NSSA_LSA
;
1878 /* set P-bit if not ABR */
1879 if (!IS_OSPF_ABR(ospf
)) {
1880 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1882 /* set non-zero FWD ADDR
1884 draft-ietf-ospf-nssa-update-09.txt
1886 if the network between the NSSA AS boundary router and
1888 adjacent AS is advertised into OSPF as an internal OSPF
1890 the forwarding address should be the next op address as
1892 currently done with type-5 LSAs. If the intervening
1894 not adversited into OSPF as an internal OSPF route and
1896 type-7 LSA's P-bit is set a forwarding address should be
1897 selected from one of the router's active OSPF interface
1899 which belong to the NSSA. If no such addresses exist,
1901 no type-7 LSA's with the P-bit set should originate from
1905 /* kevinm: not updating lsa anymore, just new */
1906 extlsa
= (struct as_external_lsa
*)(new->data
);
1908 if (extlsa
->e
[0].fwd_addr
.s_addr
== INADDR_ANY
)
1909 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(
1910 area
); /* this NSSA area in ifp */
1912 if (extlsa
->e
[0].fwd_addr
.s_addr
== INADDR_ANY
) {
1913 if (IS_DEBUG_OSPF_NSSA
)
1915 "LSA[Type-7]: Could not build FWD-ADDR");
1916 ospf_lsa_discard(new);
1921 /* install also as Type-7 */
1922 ospf_lsa_install(ospf
, NULL
,
1923 new); /* Remove Old, Lock New = 2 */
1925 /* will send each copy, lock=2+n */
1926 ospf_flood_through_as(
1927 ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1931 static struct ospf_lsa
*ospf_lsa_translated_nssa_new(struct ospf
*ospf
,
1932 struct ospf_lsa
*type7
)
1935 struct ospf_lsa
*new;
1936 struct as_external_lsa
*ext
, *extnew
;
1937 struct external_info ei
;
1939 ext
= (struct as_external_lsa
*)(type7
->data
);
1941 /* need external_info struct, fill in bare minimum */
1942 ei
.p
.family
= AF_INET
;
1943 ei
.p
.prefix
= type7
->data
->id
;
1944 ei
.p
.prefixlen
= ip_masklen(ext
->mask
);
1945 ei
.type
= ZEBRA_ROUTE_OSPF
;
1946 ei
.nexthop
= ext
->header
.adv_router
;
1947 ei
.route_map_set
.metric
= -1;
1948 ei
.route_map_set
.metric_type
= -1;
1952 if ((new = ospf_external_lsa_new(ospf
, &ei
, &type7
->data
->id
))
1954 if (IS_DEBUG_OSPF_NSSA
)
1956 "%s: Could not originate Translated Type-5 for %pI4",
1957 __func__
, &ei
.p
.prefix
);
1961 extnew
= (struct as_external_lsa
*)(new->data
);
1963 /* copy over Type-7 data to new */
1964 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1965 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1966 if (type7
->area
->suppress_fa
) {
1967 extnew
->e
[0].fwd_addr
.s_addr
= 0;
1968 if (IS_DEBUG_OSPF_NSSA
)
1969 zlog_debug("%s: Suppress forwarding address for %pI4",
1970 __func__
, &ei
.p
.prefix
);
1972 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1973 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1975 /* add translated flag, checksum and lock new lsa */
1976 SET_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1977 new = ospf_lsa_lock(new);
1982 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1983 struct ospf_lsa
*ospf_translated_nssa_originate(struct ospf
*ospf
,
1984 struct ospf_lsa
*type7
,
1985 struct ospf_lsa
*type5
)
1987 struct ospf_lsa
*new;
1988 struct as_external_lsa
*extnew
;
1990 if (ospf
->gr_info
.restart_in_progress
) {
1991 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1993 "LSA[Translated Type5]: Graceful Restart in progress, don't originate");
1997 /* we cant use ospf_external_lsa_originate() as we need to set
1998 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
2001 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
2002 if (IS_DEBUG_OSPF_NSSA
)
2004 "%s: Could not translate Type-7, Id %pI4, to Type-5",
2005 __func__
, &type7
->data
->id
);
2009 extnew
= (struct as_external_lsa
*)new->data
;
2011 /* Update LSA sequence number from translated Type-5 LSA */
2013 new->data
->ls_seqnum
= lsa_seqnum_increment(type5
);
2015 if ((new = ospf_lsa_install(ospf
, NULL
, new)) == NULL
) {
2016 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2017 "%s: Could not install LSA id %pI4", __func__
,
2022 if (IS_DEBUG_OSPF_NSSA
) {
2023 zlog_debug("%s: translated Type 7, installed", __func__
);
2024 ospf_lsa_header_dump(new->data
);
2025 zlog_debug(" Network mask: %d", ip_masklen(extnew
->mask
));
2026 zlog_debug(" Forward addr: %pI4",
2027 &extnew
->e
[0].fwd_addr
);
2030 ospf
->lsa_originate_count
++;
2031 ospf_flood_through_as(ospf
, NULL
, new);
2036 /* Refresh Translated from NSSA AS-external-LSA. */
2037 struct ospf_lsa
*ospf_translated_nssa_refresh(struct ospf
*ospf
,
2038 struct ospf_lsa
*type7
,
2039 struct ospf_lsa
*type5
)
2041 struct ospf_lsa
*new = NULL
;
2042 struct as_external_lsa
*extold
= NULL
;
2043 uint32_t ls_seqnum
= 0;
2045 /* Sanity checks. */
2046 assert(type7
|| type5
);
2047 if (!(type7
|| type5
))
2050 assert(type7
->data
);
2052 assert(type5
->data
);
2053 assert(ospf
->anyNSSA
);
2055 /* get required data according to what has been given */
2056 if (type7
&& type5
== NULL
) {
2057 /* find the translated Type-5 for this Type-7 */
2058 struct as_external_lsa
*ext
=
2059 (struct as_external_lsa
*)(type7
->data
);
2060 struct prefix_ipv4 p
= {
2061 .prefix
= type7
->data
->id
,
2062 .prefixlen
= ip_masklen(ext
->mask
),
2066 type5
= ospf_external_info_find_lsa(ospf
, &p
);
2067 } else if (type5
&& type7
== NULL
) {
2068 /* find the type-7 from which supplied type-5 was translated,
2069 * ie find first type-7 with same LSA Id.
2071 struct listnode
*ln
, *lnn
;
2072 struct route_node
*rn
;
2073 struct ospf_lsa
*lsa
;
2074 struct ospf_area
*area
;
2076 for (ALL_LIST_ELEMENTS(ospf
->areas
, ln
, lnn
, area
)) {
2077 if (area
->external_routing
!= OSPF_AREA_NSSA
&& !type7
)
2080 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
) {
2081 if (lsa
->data
->id
.s_addr
2082 == type5
->data
->id
.s_addr
) {
2090 /* do we have type7? */
2092 if (IS_DEBUG_OSPF_NSSA
)
2093 zlog_debug("%s: no Type-7 found for Type-5 LSA Id %pI4",
2094 __func__
, &type5
->data
->id
);
2098 /* do we have valid translated type5? */
2099 if (type5
== NULL
|| !CHECK_FLAG(type5
->flags
, OSPF_LSA_LOCAL_XLT
)) {
2100 if (IS_DEBUG_OSPF_NSSA
)
2102 "%s: No translated Type-5 found for Type-7 with Id %pI4",
2103 __func__
, &type7
->data
->id
);
2107 extold
= (struct as_external_lsa
*)type5
->data
;
2108 if (type7
->area
->suppress_fa
== 1) {
2109 if (extold
->e
[0].fwd_addr
.s_addr
== 0)
2110 ls_seqnum
= ntohl(type5
->data
->ls_seqnum
);
2113 /* Delete LSA from neighbor retransmit-list. */
2114 ospf_ls_retransmit_delete_nbr_as(ospf
, type5
);
2116 /* create new translated LSA */
2117 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
2118 if (IS_DEBUG_OSPF_NSSA
)
2120 "%s: Could not translate Type-7 for %pI4 to Type-5",
2121 __func__
, &type7
->data
->id
);
2125 if (type7
->area
->suppress_fa
== 1) {
2126 if (extold
->e
[0].fwd_addr
.s_addr
== 0)
2127 new->data
->ls_seqnum
= htonl(ls_seqnum
+ 1);
2130 if (!(new = ospf_lsa_install(ospf
, NULL
, new))) {
2131 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2132 "%s: Could not install translated LSA, Id %pI4",
2133 __func__
, &type7
->data
->id
);
2137 /* Flood LSA through area. */
2138 ospf_flood_through_as(ospf
, NULL
, new);
2143 /* Originate an AS-external-LSA, install and flood. */
2144 struct ospf_lsa
*ospf_external_lsa_originate(struct ospf
*ospf
,
2145 struct external_info
*ei
)
2147 struct ospf_lsa
*new;
2149 if (ospf
->gr_info
.restart_in_progress
) {
2150 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2152 "LSA[Type5]: Graceful Restart in progress, don't originate");
2156 /* Added for NSSA project....
2158 External LSAs are originated in ASBRs as usual, but for NSSA
2160 there is the global Type-5 LSDB and a Type-7 LSDB installed for
2161 every area. The Type-7's are flooded to every IR and every ABR; We
2162 install the Type-5 LSDB so that the normal "refresh" code operates
2163 as usual, and flag them as not used during ASE calculations. The
2164 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2165 Address of non-zero.
2167 If an ABR is the elected NSSA translator, following SPF and during
2168 the ABR task it will translate all the scanned Type-7's, with P-bit
2169 ON and not-self generated, and translate to Type-5's throughout the
2172 A difference in operation depends whether this ASBR is an ABR
2173 or not. If not an ABR, the P-bit is ON, to indicate that any
2174 elected NSSA-ABR can perform its translation.
2176 If an ABR, the P-bit is OFF; No ABR will perform translation and
2177 this ASBR will flood the Type-5 LSA as usual.
2179 For the case where this ASBR is not an ABR, the ASE calculations
2180 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2181 demonstrate to the user that there are LSA's that belong to any
2184 Finally, it just so happens that when the ABR is translating every
2185 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2186 approved Type-5 (translated from Type-7); at the end of translation
2187 if any Translated Type-5's remain unapproved, then they must be
2188 flushed from the AS.
2192 if (ospf
->router_id
.s_addr
== INADDR_ANY
) {
2193 if (ei
&& IS_DEBUG_OSPF_EVENT
)
2195 "LSA[Type5:%pI4]: deferring AS-external-LSA origination, router ID is zero",
2200 /* Create new AS-external-LSA instance. */
2201 if ((new = ospf_external_lsa_new(ospf
, ei
, NULL
)) == NULL
) {
2202 if (ei
&& IS_DEBUG_OSPF_EVENT
)
2204 "LSA[Type5:%pI4]: Could not originate AS-external-LSA",
2209 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2210 ospf_lsa_install(ospf
, NULL
, new);
2212 /* Update LSA origination count. */
2213 ospf
->lsa_originate_count
++;
2215 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2216 ospf_flood_through_as(ospf
, NULL
, new);
2218 /* If there is any attached NSSA, do special handling */
2219 if (ospf
->anyNSSA
&&
2220 /* stay away from translated LSAs! */
2221 !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2222 ospf_install_flood_nssa(
2223 ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2225 /* Debug logging. */
2226 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2227 zlog_debug("LSA[Type%d:%pI4]: Originate AS-external-LSA %p",
2228 new->data
->type
, &new->data
->id
,
2230 ospf_lsa_header_dump(new->data
);
2236 static struct external_info
*ospf_default_external_info(struct ospf
*ospf
)
2239 struct prefix_ipv4 p
;
2240 struct external_info
*default_ei
;
2244 p
.prefix
.s_addr
= 0;
2247 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
, 0, &p
);
2251 /* First, lookup redistributed default route. */
2252 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
2253 struct list
*ext_list
;
2255 if (type
== ZEBRA_ROUTE_OSPF
)
2258 ext_list
= ospf
->external
[type
];
2262 ret
= ospf_external_default_routemap_apply_walk(ospf
, ext_list
,
2271 void ospf_external_lsa_rid_change(struct ospf
*ospf
)
2273 struct external_info
*ei
;
2274 struct ospf_external_aggr_rt
*aggr
;
2275 struct ospf_lsa
*lsa
= NULL
;
2279 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++) {
2280 struct route_node
*rn
;
2281 struct route_table
*rt
;
2282 struct list
*ext_list
;
2283 struct listnode
*node
;
2284 struct ospf_external
*ext
;
2286 ext_list
= ospf
->external
[type
];
2290 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2291 /* Originate As-external-LSA from all type of
2292 * distribute source.
2294 rt
= ext
->external_info
;
2298 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
2304 if (is_default_prefix4(&ei
->p
))
2307 lsa
= ospf_external_info_find_lsa(ospf
, &ei
->p
);
2309 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
2312 if (!ospf_redistribute_check(ospf
, ei
,
2316 if (IS_DEBUG_OSPF(lsa
, EXTNL_LSA_AGGR
))
2318 "Originate Summary LSA after reset/router-ID change");
2320 /* Here the LSA is originated as new */
2321 ospf_originate_summary_lsa(ospf
, aggr
,
2324 /* LSA needs to be refreshed even if
2325 * there is no change in the route
2326 * params if the LSA is in maxage.
2328 if (IS_LSA_MAXAGE(lsa
))
2329 force
= LSA_REFRESH_FORCE
;
2331 force
= LSA_REFRESH_IF_CHANGED
;
2333 ospf_external_lsa_refresh(ospf
, lsa
,
2336 if (!ospf_redistribute_check(ospf
, ei
,
2340 if (!ospf_external_lsa_originate(ospf
,
2343 EC_OSPF_LSA_INSTALL_FAILURE
,
2344 "LSA: AS-external-LSA was not originated.");
2350 ei
= ospf_default_external_info(ospf
);
2351 if (ei
&& !ospf_external_lsa_originate(ospf
, ei
)) {
2352 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2353 "LSA: AS-external-LSA for default route was not originated.");
2357 /* Flush any NSSA LSAs for given prefix */
2358 void ospf_nssa_lsa_flush(struct ospf
*ospf
, struct prefix_ipv4
*p
)
2360 struct listnode
*node
, *nnode
;
2361 struct ospf_lsa
*lsa
= NULL
;
2362 struct ospf_area
*area
;
2364 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2365 if (area
->external_routing
== OSPF_AREA_NSSA
) {
2366 lsa
= ospf_lsa_lookup(ospf
, area
, OSPF_AS_NSSA_LSA
,
2367 p
->prefix
, ospf
->router_id
);
2369 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2371 "LSA: There is no such AS-NSSA-LSA %pFX in LSDB",
2375 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
2376 if (!IS_LSA_MAXAGE(lsa
)) {
2377 ospf_refresher_unregister_lsa(ospf
, lsa
);
2378 ospf_lsa_flush_area(lsa
, area
);
2384 /* Flush an AS-external-LSA from LSDB and routing domain. */
2385 void ospf_external_lsa_flush(struct ospf
*ospf
, uint8_t type
,
2386 struct prefix_ipv4
*p
,
2387 ifindex_t ifindex
/*, struct in_addr nexthop */)
2389 struct ospf_lsa
*lsa
;
2391 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2392 zlog_debug("LSA: Flushing AS-external-LSA %pFX", p
);
2394 /* First lookup LSA from LSDB. */
2395 if (!(lsa
= ospf_external_info_find_lsa(ospf
, p
))) {
2396 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2398 "LSA: There is no such AS-external-LSA %pFX in LSDB",
2403 /* If LSA is selforiginated, not a translated LSA, and there is
2404 * NSSA area, flush Type-7 LSA's at first.
2406 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2407 && !(CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2408 ospf_nssa_lsa_flush(ospf
, p
);
2410 if (!IS_LSA_MAXAGE(lsa
)) {
2411 /* Sweep LSA from Link State Retransmit List. */
2412 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2414 /* Unregister LSA from Refresh queue. */
2415 ospf_refresher_unregister_lsa(ospf
, lsa
);
2417 /* Flush AS-external-LSA through AS. */
2418 ospf_lsa_flush_as(ospf
, lsa
);
2421 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2422 zlog_debug("%s: stop", __func__
);
2425 void ospf_external_lsa_refresh_default(struct ospf
*ospf
)
2427 struct prefix_ipv4 p
;
2428 struct external_info
*ei
;
2429 struct ospf_lsa
*lsa
;
2433 p
.prefix
.s_addr
= INADDR_ANY
;
2435 ei
= ospf_default_external_info(ospf
);
2436 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
2439 if (IS_DEBUG_OSPF_EVENT
)
2440 zlog_debug("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2442 ospf_external_lsa_refresh(ospf
, lsa
, ei
, LSA_REFRESH_FORCE
,
2444 } else if (ei
&& !lsa
) {
2445 if (IS_DEBUG_OSPF_EVENT
)
2447 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2448 ospf_external_lsa_originate(ospf
, ei
);
2450 if (IS_DEBUG_OSPF_EVENT
)
2451 zlog_debug("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2452 ospf_external_lsa_flush(ospf
, DEFAULT_ROUTE
, &p
, 0);
2456 void ospf_external_lsa_refresh_type(struct ospf
*ospf
, uint8_t type
,
2457 unsigned short instance
, int force
)
2459 struct route_node
*rn
;
2460 struct external_info
*ei
;
2461 struct ospf_external
*ext
;
2463 if (type
== DEFAULT_ROUTE
)
2466 ext
= ospf_external_lookup(ospf
, type
, instance
);
2468 if (ext
&& EXTERNAL_INFO(ext
)) {
2469 /* Refresh each redistributed AS-external-LSAs. */
2470 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
2471 rn
= route_next(rn
)) {
2474 if (!is_default_prefix4(&ei
->p
)) {
2475 struct ospf_lsa
*lsa
;
2476 struct ospf_external_aggr_rt
*aggr
;
2478 aggr
= ospf_external_aggr_match(ospf
,
2480 lsa
= ospf_external_info_find_lsa(
2483 /* Check the AS-external-LSA
2484 * should be originated.
2486 if (!ospf_redistribute_check(
2489 ospf_unlink_ei_from_aggr(
2498 "%s: Send Aggreate LSA (%pFX)",
2502 ospf_originate_summary_lsa(
2507 if (IS_LSA_MAXAGE(lsa
))
2508 force
= LSA_REFRESH_FORCE
;
2510 ospf_external_lsa_refresh(
2511 ospf
, lsa
, ei
, force
,
2514 if (!ospf_redistribute_check(
2517 ospf_external_lsa_originate(
2526 /* Refresh AS-external-LSA. */
2527 struct ospf_lsa
*ospf_external_lsa_refresh(struct ospf
*ospf
,
2528 struct ospf_lsa
*lsa
,
2529 struct external_info
*ei
, int force
,
2532 struct ospf_lsa
*new;
2535 /* Check the AS-external-LSA should be originated. */
2537 if (!ospf_redistribute_check(ospf
, ei
, &changed
)) {
2538 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2540 "LSA[Type%d:%pI4] Could not be refreshed, redist check fail",
2544 ospf_external_lsa_flush(ospf
, ei
->type
, &ei
->p
,
2545 ei
->ifindex
/*, ei->nexthop */);
2549 if (!changed
&& !force
) {
2550 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2552 "LSA[Type%d:%pI4]: Not refreshed, not changed/forced",
2553 lsa
->data
->type
, &lsa
->data
->id
);
2557 /* Delete LSA from neighbor retransmit-list. */
2558 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2560 /* Unregister AS-external-LSA from refresh-list. */
2561 ospf_refresher_unregister_lsa(ospf
, lsa
);
2563 new = ospf_external_lsa_new(ospf
, ei
, &lsa
->data
->id
);
2566 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2567 zlog_debug("LSA[Type%d:%pI4]: Could not be refreshed",
2568 lsa
->data
->type
, &lsa
->data
->id
);
2572 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2574 ospf_lsa_install(ospf
, NULL
, new); /* As type-5. */
2576 /* Flood LSA through AS. */
2577 ospf_flood_through_as(ospf
, NULL
, new);
2579 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2580 if (ospf
->anyNSSA
&& !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2581 ospf_install_flood_nssa(ospf
, new,
2582 ei
); /* Install/Flood per new rules */
2584 /* Register self-originated LSA to refresh queue.
2585 * Translated LSAs should not be registered, but refreshed upon
2586 * refresh of the Type-7
2588 if (!CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2589 ospf_refresher_register_lsa(ospf
, new);
2591 /* Debug logging. */
2592 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2593 zlog_debug("LSA[Type%d:%pI4]: AS-external-LSA refresh",
2594 new->data
->type
, &new->data
->id
);
2595 ospf_lsa_header_dump(new->data
);
2602 /* LSA installation functions. */
2604 /* Install router-LSA to an area. */
2605 static struct ospf_lsa
*
2606 ospf_router_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2608 struct ospf_area
*area
= new->area
;
2610 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2611 The entire routing table must be recalculated, starting with
2612 the shortest path calculations for each area (not just the
2613 area whose link-state database has changed).
2616 if (IS_LSA_SELF(new)) {
2618 /* Only install LSA if it is originated/refreshed by us.
2619 * If LSA was received by flooding, the RECEIVED flag is set so
2621 * not link the LSA */
2622 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2623 return new; /* ignore stale LSA */
2625 /* Set self-originated router-LSA. */
2626 ospf_lsa_unlock(&area
->router_lsa_self
);
2627 area
->router_lsa_self
= ospf_lsa_lock(new);
2629 ospf_refresher_register_lsa(ospf
, new);
2632 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2636 /* Install network-LSA to an area. */
2637 static struct ospf_lsa
*ospf_network_lsa_install(struct ospf
*ospf
,
2638 struct ospf_interface
*oi
,
2639 struct ospf_lsa
*new,
2643 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2644 The entire routing table must be recalculated, starting with
2645 the shortest path calculations for each area (not just the
2646 area whose link-state database has changed).
2648 if (IS_LSA_SELF(new)) {
2649 /* We supposed that when LSA is originated by us, we pass the
2651 for which it was originated. If LSA was received by flooding,
2652 the RECEIVED flag is set, so we do not link the LSA to the
2654 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2655 return new; /* ignore stale LSA */
2657 ospf_lsa_unlock(&oi
->network_lsa_self
);
2658 oi
->network_lsa_self
= ospf_lsa_lock(new);
2659 ospf_refresher_register_lsa(ospf
, new);
2662 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2667 /* Install summary-LSA to an area. */
2668 static struct ospf_lsa
*
2669 ospf_summary_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2671 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2672 /* RFC 2328 Section 13.2 Summary-LSAs
2673 The best route to the destination described by the summary-
2674 LSA must be recalculated (see Section 16.5). If this
2675 destination is an AS boundary router, it may also be
2676 necessary to re-examine all the AS-external-LSAs.
2679 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2682 if (IS_LSA_SELF(new))
2683 ospf_refresher_register_lsa(ospf
, new);
2688 /* Install ASBR-summary-LSA to an area. */
2689 static struct ospf_lsa
*ospf_summary_asbr_lsa_install(struct ospf
*ospf
,
2690 struct ospf_lsa
*new,
2693 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2694 /* RFC 2328 Section 13.2 Summary-LSAs
2695 The best route to the destination described by the summary-
2696 LSA must be recalculated (see Section 16.5). If this
2697 destination is an AS boundary router, it may also be
2698 necessary to re-examine all the AS-external-LSAs.
2700 ospf_spf_calculate_schedule(ospf
,
2701 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2704 /* register LSA to refresh-list. */
2705 if (IS_LSA_SELF(new))
2706 ospf_refresher_register_lsa(ospf
, new);
2711 /* Install AS-external-LSA. */
2712 static struct ospf_lsa
*ospf_external_lsa_install(struct ospf
*ospf
,
2713 struct ospf_lsa
*new,
2716 ospf_ase_register_external_lsa(new, ospf
);
2717 /* If LSA is not self-originated, calculate an external route. */
2719 /* RFC 2328 Section 13.2 AS-external-LSAs
2720 The best route to the destination described by the AS-
2721 external-LSA must be recalculated (see Section 16.6).
2724 if (!IS_LSA_SELF(new))
2725 ospf_ase_incremental_update(ospf
, new);
2728 if (new->data
->type
== OSPF_AS_NSSA_LSA
) {
2729 /* There is no point to register selforiginate Type-7 LSA for
2730 * refreshing. We rely on refreshing Type-5 LSA's
2732 if (IS_LSA_SELF(new))
2735 /* Try refresh type-5 translated LSA for this LSA, if
2737 * New translations will be taken care of by the
2740 ospf_translated_nssa_refresh(ospf
, new, NULL
);
2741 ospf_schedule_abr_task(ospf
);
2745 /* Register self-originated LSA to refresh queue.
2746 * Leave Translated LSAs alone if NSSA is enabled
2748 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2749 ospf_refresher_register_lsa(ospf
, new);
2754 void ospf_discard_from_db(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
2755 struct ospf_lsa
*lsa
)
2757 struct ospf_lsa
*old
;
2762 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2767 if (old
->refresh_list
>= 0)
2768 ospf_refresher_unregister_lsa(ospf
, old
);
2770 switch (old
->data
->type
) {
2771 case OSPF_AS_EXTERNAL_LSA
:
2772 ospf_ase_unregister_external_lsa(old
, ospf
);
2773 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2775 case OSPF_OPAQUE_AS_LSA
:
2776 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2778 case OSPF_AS_NSSA_LSA
:
2779 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2780 ospf_ase_unregister_external_lsa(old
, ospf
);
2783 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2787 ospf_lsa_maxage_delete(ospf
, old
);
2788 ospf_lsa_discard(old
);
2791 struct ospf_lsa
*ospf_lsa_install(struct ospf
*ospf
, struct ospf_interface
*oi
,
2792 struct ospf_lsa
*lsa
)
2794 struct ospf_lsa
*new = NULL
;
2795 struct ospf_lsa
*old
= NULL
;
2796 struct ospf_lsdb
*lsdb
= NULL
;
2800 switch (lsa
->data
->type
) {
2802 case OSPF_AS_NSSA_LSA
:
2804 lsdb
= lsa
->area
->lsdb
;
2808 case OSPF_AS_EXTERNAL_LSA
:
2809 case OSPF_OPAQUE_AS_LSA
:
2814 lsdb
= lsa
->area
->lsdb
;
2820 /* RFC 2328 13.2. Installing LSAs in the database
2822 Installing a new LSA in the database, either as the result of
2823 flooding or a newly self-originated LSA, may cause the OSPF
2824 routing table structure to be recalculated. The contents of the
2825 new LSA should be compared to the old instance, if present. If
2826 there is no difference, there is no need to recalculate the
2827 routing table. When comparing an LSA to its previous instance,
2828 the following are all considered to be differences in contents:
2830 o The LSA's Options field has changed.
2832 o One of the LSA instances has LS age set to MaxAge, and
2835 o The length field in the LSA header has changed.
2837 o The body of the LSA (i.e., anything outside the 20-byte
2838 LSA header) has changed. Note that this excludes changes
2839 in LS Sequence Number and LS Checksum.
2842 /* Look up old LSA and determine if any SPF calculation or incremental
2844 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2846 /* Do comparison and record if recalc needed. */
2848 if (old
== NULL
|| ospf_lsa_different(old
, lsa
, false)) {
2849 /* Ref rfc3623 section 3.2.3
2850 * Installing new lsa or change in the existing LSA
2851 * or flushing existing LSA leads to topo change
2852 * and trigger SPF caculation.
2853 * So, router should be aborted from HELPER role
2854 * if it is detected as TOPO change.
2856 if (ospf
->active_restarter_cnt
&&
2857 CHECK_LSA_TYPE_1_TO_5_OR_7(lsa
->data
->type
)) {
2858 if (old
== NULL
|| ospf_lsa_different(old
, lsa
, true))
2859 ospf_helper_handle_topo_chg(ospf
, lsa
);
2866 Sequence number check (Section 14.1 of rfc 2328)
2867 "Premature aging is used when it is time for a self-originated
2868 LSA's sequence number field to wrap. At this point, the current
2869 LSA instance (having LS sequence number MaxSequenceNumber) must
2870 be prematurely aged and flushed from the routing domain before a
2871 new instance with sequence number equal to InitialSequenceNumber
2872 can be originated. "
2875 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
) {
2876 if (ospf_lsa_is_self_originated(ospf
, lsa
)) {
2877 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2879 if (!IS_LSA_MAXAGE(lsa
))
2880 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2881 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
2883 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
)) {
2885 "%s() Premature Aging lsa %p, seqnum 0x%x",
2887 ntohl(lsa
->data
->ls_seqnum
));
2888 ospf_lsa_header_dump(lsa
->data
);
2891 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2893 "%s() got an lsa with seq 0x80000000 that was not self originated. Ignoring",
2895 ospf_lsa_header_dump(lsa
->data
);
2901 /* discard old LSA from LSDB */
2903 ospf_discard_from_db(ospf
, lsdb
, lsa
);
2905 /* Calculate Checksum if self-originated?. */
2906 if (IS_LSA_SELF(lsa
))
2907 ospf_lsa_checksum(lsa
->data
);
2909 /* Insert LSA to LSDB. */
2910 ospf_lsdb_add(lsdb
, lsa
);
2913 /* Do LSA specific installation process. */
2914 switch (lsa
->data
->type
) {
2915 case OSPF_ROUTER_LSA
:
2916 new = ospf_router_lsa_install(ospf
, lsa
, rt_recalc
);
2918 case OSPF_NETWORK_LSA
:
2920 new = ospf_network_lsa_install(ospf
, oi
, lsa
, rt_recalc
);
2922 case OSPF_SUMMARY_LSA
:
2923 new = ospf_summary_lsa_install(ospf
, lsa
, rt_recalc
);
2925 case OSPF_ASBR_SUMMARY_LSA
:
2926 new = ospf_summary_asbr_lsa_install(ospf
, lsa
, rt_recalc
);
2928 case OSPF_AS_EXTERNAL_LSA
:
2929 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2931 case OSPF_OPAQUE_LINK_LSA
:
2932 if (IS_LSA_SELF(lsa
))
2933 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for
2936 /* Incoming "oi" for this LSA has set at LSUpd
2940 case OSPF_OPAQUE_AREA_LSA
:
2941 case OSPF_OPAQUE_AS_LSA
:
2942 new = ospf_opaque_lsa_install(lsa
, rt_recalc
);
2944 case OSPF_AS_NSSA_LSA
:
2945 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2946 default: /* type-6,8,9....nothing special */
2951 return new; /* Installation failed, cannot proceed further --
2955 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
)) {
2956 switch (lsa
->data
->type
) {
2957 case OSPF_AS_EXTERNAL_LSA
:
2958 case OSPF_OPAQUE_AS_LSA
:
2959 case OSPF_AS_NSSA_LSA
:
2960 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2961 lookup_msg(ospf_lsa_type_msg
,
2962 new->data
->type
, NULL
));
2965 zlog_debug("LSA[%s]: Install %s to Area %pI4",
2967 lookup_msg(ospf_lsa_type_msg
,
2968 new->data
->type
, NULL
),
2969 &new->area
->area_id
);
2975 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2976 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2978 if (IS_LSA_MAXAGE(new)) {
2979 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
))
2980 zlog_debug("LSA[%s]: Install LSA %p, MaxAge",
2981 dump_lsa_key(new), lsa
);
2982 ospf_lsa_maxage(ospf
, lsa
);
2989 int ospf_check_nbr_status(struct ospf
*ospf
)
2991 struct listnode
*node
, *nnode
;
2992 struct ospf_interface
*oi
;
2994 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
2995 struct route_node
*rn
;
2996 struct ospf_neighbor
*nbr
;
2998 if (ospf_if_is_enable(oi
))
2999 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
3000 if ((nbr
= rn
->info
) != NULL
)
3001 if (nbr
->state
== NSM_Exchange
3002 || nbr
->state
== NSM_Loading
) {
3003 route_unlock_node(rn
);
3012 void ospf_maxage_lsa_remover(struct thread
*thread
)
3014 struct ospf
*ospf
= THREAD_ARG(thread
);
3015 struct ospf_lsa
*lsa
, *old
;
3016 struct route_node
*rn
;
3019 ospf
->t_maxage
= NULL
;
3021 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3022 zlog_debug("LSA[MaxAge]: remover Start");
3024 reschedule
= !ospf_check_nbr_status(ospf
);
3027 for (rn
= route_top(ospf
->maxage_lsa
); rn
;
3028 rn
= route_next(rn
)) {
3029 if ((lsa
= rn
->info
) == NULL
) {
3033 /* There is at least one neighbor from which we still
3035 * for that LSA, so we are not allowed to remove it from
3037 * as per RFC 2328 section 14 para 4 a) */
3038 if (lsa
->retransmit_counter
> 0) {
3043 /* TODO: maybe convert this function to a work-queue */
3044 if (thread_should_yield(thread
)) {
3045 OSPF_TIMER_ON(ospf
->t_maxage
,
3046 ospf_maxage_lsa_remover
, 0);
3048 rn
); /* route_top/route_next */
3052 /* Remove LSA from the LSDB */
3053 if (IS_LSA_SELF(lsa
))
3054 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3056 "LSA[Type%d:%pI4]: LSA 0x%lx is self-originated: ",
3059 (unsigned long)lsa
);
3061 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3063 "LSA[%s]: MaxAge LSA removed from list",
3066 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
)) {
3067 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3069 "originating new lsa for lsa %p",
3071 ospf_lsa_refresh(ospf
, lsa
);
3074 /* Remove from lsdb. */
3076 old
= ospf_lsdb_lookup(lsa
->lsdb
, lsa
);
3077 /* The max age LSA here must be the same
3078 * as the LSA in LSDB
3081 flog_err(EC_OSPF_LSA_MISSING
,
3082 "%s: LSA[%s]: LSA not in LSDB",
3083 __func__
, dump_lsa_key(lsa
));
3087 ospf_discard_from_db(ospf
, lsa
->lsdb
, lsa
);
3088 ospf_lsdb_delete(lsa
->lsdb
, lsa
);
3090 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3092 "%s: LSA[%s]: No associated LSDB!",
3093 __func__
, dump_lsa_key(lsa
));
3097 /* A MaxAge LSA must be removed immediately from the router's link
3098 state database as soon as both a) it is no longer contained on any
3099 neighbor Link state retransmission lists and b) none of the
3101 neighbors are in states Exchange or Loading. */
3103 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
3104 ospf
->maxage_delay
);
3107 /* This function checks whether an LSA with initial sequence number should be
3108 * originated after a wrap in sequence number
3110 void ospf_check_and_gen_init_seq_lsa(struct ospf_interface
*oi
,
3111 struct ospf_lsa
*recv_lsa
)
3113 struct ospf_lsa
*lsa
= NULL
;
3114 struct ospf
*ospf
= oi
->ospf
;
3116 lsa
= ospf_lsa_lookup_by_header(oi
->area
, recv_lsa
->data
);
3118 if ((lsa
== NULL
) || (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
))
3119 || (lsa
->retransmit_counter
!= 0)) {
3120 if (IS_DEBUG_OSPF(lsa
, LSA
))
3122 "Do not generate LSA with initial seqence number.");
3126 ospf_lsa_maxage_delete(ospf
, lsa
);
3128 lsa
->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
3130 ospf_lsa_refresh(ospf
, lsa
);
3133 void ospf_lsa_maxage_delete(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3135 struct route_node
*rn
;
3136 struct prefix lsa_prefix
;
3138 memset(&lsa_prefix
, 0, sizeof(lsa_prefix
));
3139 lsa_prefix
.family
= AF_UNSPEC
;
3140 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
3141 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
3143 if ((rn
= route_node_lookup(ospf
->maxage_lsa
, &lsa_prefix
))) {
3144 if (rn
->info
== lsa
) {
3145 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
3146 ospf_lsa_unlock(&lsa
); /* maxage_lsa */
3149 rn
); /* unlock node because lsa is deleted */
3151 route_unlock_node(rn
); /* route_node_lookup */
3153 if (IS_DEBUG_OSPF_EVENT
)
3154 zlog_debug("%s: lsa %s is not found in maxage db.",
3155 __func__
, dump_lsa_key(lsa
));
3159 /* Add LSA onto the MaxAge list, and schedule for removal.
3160 * This does *not* lead to the LSA being flooded, that must be taken
3161 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
3164 void ospf_lsa_maxage(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3166 struct prefix lsa_prefix
;
3167 struct route_node
*rn
;
3169 /* When we saw a MaxAge LSA flooded to us, we put it on the list
3170 and schedule the MaxAge LSA remover. */
3171 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
)) {
3172 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3174 "LSA[%s]: %p already exists on MaxAge LSA list",
3175 dump_lsa_key(lsa
), lsa
);
3179 memset(&lsa_prefix
, 0, sizeof(lsa_prefix
));
3180 lsa_prefix
.family
= AF_UNSPEC
;
3181 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
3182 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
3184 rn
= route_node_get(ospf
->maxage_lsa
, &lsa_prefix
);
3185 if (rn
->info
!= NULL
) {
3186 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3188 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
3189 dump_lsa_key(lsa
), rn
->info
,
3190 (void *)lsa
, lsa_prefix
.prefixlen
);
3191 route_unlock_node(rn
);
3193 rn
->info
= ospf_lsa_lock(lsa
);
3194 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
3197 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3198 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
3201 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
3202 ospf
->maxage_delay
);
3205 static int ospf_lsa_maxage_walker_remover(struct ospf
*ospf
,
3206 struct ospf_lsa
*lsa
)
3208 /* Stay away from any Local Translated Type-7 LSAs */
3209 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3212 if (IS_LSA_MAXAGE(lsa
))
3213 /* Self-originated LSAs should NOT time-out instead,
3214 they're flushed and submitted to the max_age list explicitly.
3216 if (!ospf_lsa_is_self_originated(ospf
, lsa
)) {
3217 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3218 zlog_debug("LSA[%s]: is MaxAge",
3221 switch (lsa
->data
->type
) {
3222 case OSPF_OPAQUE_LINK_LSA
:
3223 case OSPF_OPAQUE_AREA_LSA
:
3224 case OSPF_OPAQUE_AS_LSA
:
3226 * As a general rule, whenever network topology
3228 * (due to an LSA removal in this case), routing
3230 * should be triggered. However, this is not
3232 * LSAs. Even if an opaque LSA instance is going
3234 * from the routing domain, it does not mean a
3236 * topology, and thus, routing recalculation is
3240 case OSPF_AS_EXTERNAL_LSA
:
3241 case OSPF_AS_NSSA_LSA
:
3242 ospf_ase_incremental_update(ospf
, lsa
);
3245 ospf_spf_calculate_schedule(ospf
,
3249 ospf_lsa_maxage(ospf
, lsa
);
3252 if (IS_LSA_MAXAGE(lsa
) && !ospf_lsa_is_self_originated(ospf
, lsa
))
3253 if (LS_AGE(lsa
) > OSPF_LSA_MAXAGE
+ 30)
3254 printf("Eek! Shouldn't happen!\n");
3259 /* Periodical check of MaxAge LSA. */
3260 void ospf_lsa_maxage_walker(struct thread
*thread
)
3262 struct ospf
*ospf
= THREAD_ARG(thread
);
3263 struct route_node
*rn
;
3264 struct ospf_lsa
*lsa
;
3265 struct ospf_area
*area
;
3266 struct listnode
*node
, *nnode
;
3268 ospf
->t_maxage_walker
= NULL
;
3270 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3271 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
3272 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3273 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
3274 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3275 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3276 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3277 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3278 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3279 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3280 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3281 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3282 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3283 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
3284 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3287 /* for AS-external-LSAs. */
3289 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3290 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3291 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3292 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3295 OSPF_TIMER_ON(ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3296 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3299 struct ospf_lsa
*ospf_lsa_lookup_by_prefix(struct ospf_lsdb
*lsdb
, uint8_t type
,
3300 struct prefix_ipv4
*p
,
3301 struct in_addr router_id
)
3303 struct ospf_lsa
*lsa
;
3304 struct in_addr mask
, id
;
3305 struct lsa_header_mask
{
3306 struct lsa_header header
;
3307 struct in_addr mask
;
3310 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, p
->prefix
, router_id
);
3314 masklen2ip(p
->prefixlen
, &mask
);
3316 hmask
= (struct lsa_header_mask
*)lsa
->data
;
3318 if (mask
.s_addr
!= hmask
->mask
.s_addr
) {
3319 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3320 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, router_id
);
3328 struct ospf_lsa
*ospf_lsa_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
3329 uint32_t type
, struct in_addr id
,
3330 struct in_addr adv_router
)
3336 case OSPF_ROUTER_LSA
:
3337 case OSPF_NETWORK_LSA
:
3338 case OSPF_SUMMARY_LSA
:
3339 case OSPF_ASBR_SUMMARY_LSA
:
3340 case OSPF_AS_NSSA_LSA
:
3341 case OSPF_OPAQUE_LINK_LSA
:
3342 case OSPF_OPAQUE_AREA_LSA
:
3343 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, adv_router
);
3344 case OSPF_AS_EXTERNAL_LSA
:
3345 case OSPF_OPAQUE_AS_LSA
:
3346 return ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
, adv_router
);
3354 struct ospf_lsa
*ospf_lsa_lookup_by_id(struct ospf_area
*area
, uint32_t type
,
3357 struct ospf_lsa
*lsa
;
3358 struct route_node
*rn
;
3361 case OSPF_ROUTER_LSA
:
3362 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3363 case OSPF_NETWORK_LSA
:
3364 for (rn
= route_top(NETWORK_LSDB(area
)); rn
;
3365 rn
= route_next(rn
))
3366 if ((lsa
= rn
->info
))
3367 if (IPV4_ADDR_SAME(&lsa
->data
->id
, &id
)) {
3368 route_unlock_node(rn
);
3372 case OSPF_SUMMARY_LSA
:
3373 case OSPF_ASBR_SUMMARY_LSA
:
3374 /* Currently not used. */
3376 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3377 case OSPF_AS_EXTERNAL_LSA
:
3378 case OSPF_AS_NSSA_LSA
:
3379 case OSPF_OPAQUE_LINK_LSA
:
3380 case OSPF_OPAQUE_AREA_LSA
:
3381 case OSPF_OPAQUE_AS_LSA
:
3382 /* Currently not used. */
3391 struct ospf_lsa
*ospf_lsa_lookup_by_header(struct ospf_area
*area
,
3392 struct lsa_header
*lsah
)
3394 struct ospf_lsa
*match
;
3397 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3398 * is redefined to have two subfields; opaque-type and opaque-id.
3399 * However, it is harmless to treat the two sub fields together, as if
3400 * they two were forming a unique LSA-ID.
3403 match
= ospf_lsa_lookup(area
->ospf
, area
, lsah
->type
, lsah
->id
,
3407 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3408 zlog_debug("LSA[Type%d:%pI4]: Lookup by header, NO MATCH",
3409 lsah
->type
, &lsah
->id
);
3414 /* return +n, l1 is more recent.
3415 return -n, l2 is more recent.
3416 return 0, l1 and l2 is identical. */
3417 int ospf_lsa_more_recent(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3422 if (l1
== NULL
&& l2
== NULL
)
3429 /* compare LS sequence number. */
3430 x
= (int)ntohl(l1
->data
->ls_seqnum
);
3431 y
= (int)ntohl(l2
->data
->ls_seqnum
);
3437 /* compare LS checksum. */
3438 r
= ntohs(l1
->data
->checksum
) - ntohs(l2
->data
->checksum
);
3442 /* compare LS age. */
3443 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3445 else if (!IS_LSA_MAXAGE(l1
) && IS_LSA_MAXAGE(l2
))
3448 /* compare LS age with MaxAgeDiff. */
3449 if (LS_AGE(l1
) - LS_AGE(l2
) > OSPF_LSA_MAXAGE_DIFF
)
3451 else if (LS_AGE(l2
) - LS_AGE(l1
) > OSPF_LSA_MAXAGE_DIFF
)
3454 /* LSAs are identical. */
3459 * Check if two LSAs are different.
3462 * The first LSA to compare.
3465 * The second LSA to compare.
3468 * When set to true, ignore whether the LSAs were received from the network
3469 * or not. This parameter should be set to true when checking for topology
3470 * changes as part of the Graceful Restart helper neighbor procedures.
3473 * true if the LSAs are different, false otherwise.
3475 int ospf_lsa_different(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
,
3476 bool ignore_rcvd_flag
)
3484 if (l1
->data
->options
!= l2
->data
->options
)
3487 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3490 if (IS_LSA_MAXAGE(l2
) && !IS_LSA_MAXAGE(l1
))
3493 if (l1
->size
!= l2
->size
)
3499 if (!ignore_rcvd_flag
3500 && CHECK_FLAG((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3501 return 1; /* May be a stale LSA in the LSBD */
3503 if (l1
->size
== OSPF_LSA_HEADER_SIZE
)
3504 return 0; /* nothing to compare */
3506 p1
= (char *)l1
->data
;
3507 p2
= (char *)l2
->data
;
3509 if (memcmp(p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3510 l1
->size
- OSPF_LSA_HEADER_SIZE
)
3517 int ospf_lsa_flush_schedule(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3519 if (lsa
== NULL
|| !IS_LSA_SELF(lsa
))
3522 if (IS_DEBUG_OSPF_EVENT
)
3524 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3525 lsa
->data
->type
, &lsa
->data
->id
);
3527 /* Force given lsa's age to MaxAge. */
3528 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
3530 switch (lsa
->data
->type
) {
3531 /* Opaque wants to be notified of flushes */
3532 case OSPF_OPAQUE_LINK_LSA
:
3533 case OSPF_OPAQUE_AREA_LSA
:
3534 case OSPF_OPAQUE_AS_LSA
:
3535 ospf_opaque_lsa_refresh(lsa
);
3538 ospf_refresher_unregister_lsa(ospf
, lsa
);
3539 ospf_lsa_flush(ospf
, lsa
);
3546 void ospf_flush_self_originated_lsas_now(struct ospf
*ospf
)
3548 struct listnode
*node
, *nnode
;
3549 struct listnode
*node2
, *nnode2
;
3550 struct ospf_area
*area
;
3551 struct ospf_interface
*oi
;
3552 struct ospf_lsa
*lsa
;
3553 struct route_node
*rn
;
3554 struct ospf_if_params
*oip
;
3555 int need_to_flush_ase
= 0;
3557 ospf
->inst_shutdown
= 1;
3559 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3560 if ((lsa
= area
->router_lsa_self
) != NULL
) {
3561 if (IS_DEBUG_OSPF_EVENT
)
3563 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3567 ospf_refresher_unregister_lsa(ospf
, lsa
);
3568 ospf_lsa_flush_area(lsa
, area
);
3569 ospf_lsa_unlock(&area
->router_lsa_self
);
3570 area
->router_lsa_self
= NULL
;
3573 for (ALL_LIST_ELEMENTS(area
->oiflist
, node2
, nnode2
, oi
)) {
3574 if ((lsa
= oi
->network_lsa_self
) != NULL
3575 && oi
->state
== ISM_DR
&& oi
->full_nbrs
> 0) {
3576 if (IS_DEBUG_OSPF_EVENT
)
3578 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3582 ospf_refresher_unregister_lsa(
3583 ospf
, oi
->network_lsa_self
);
3584 ospf_lsa_flush_area(oi
->network_lsa_self
, area
);
3585 ospf_lsa_unlock(&oi
->network_lsa_self
);
3586 oi
->network_lsa_self
= NULL
;
3588 oip
= ospf_lookup_if_params(
3589 oi
->ifp
, oi
->address
->u
.prefix4
);
3591 oip
->network_lsa_seqnum
= htonl(
3592 OSPF_INVALID_SEQUENCE_NUMBER
);
3595 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3596 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3597 need_to_flush_ase
= 1;
3600 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3601 ospf_lsa_flush_schedule(ospf
, lsa
);
3602 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3603 ospf_lsa_flush_schedule(ospf
, lsa
);
3604 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3605 ospf_lsa_flush_schedule(ospf
, lsa
);
3606 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3607 ospf_lsa_flush_schedule(ospf
, lsa
);
3610 if (need_to_flush_ase
) {
3611 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3612 ospf_lsa_flush_schedule(ospf
, lsa
);
3613 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3614 ospf_lsa_flush_schedule(ospf
, lsa
);
3618 * Make sure that the MaxAge LSA remover is executed immediately,
3619 * without conflicting to other threads.
3621 if (ospf
->t_maxage
!= NULL
) {
3622 THREAD_OFF(ospf
->t_maxage
);
3623 thread_execute(master
, ospf_maxage_lsa_remover
, ospf
, 0);
3629 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3630 /* An interface-independent version of ospf_lsa_is_self_originated */
3631 int ospf_lsa_is_self_originated(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3633 struct listnode
*node
;
3634 struct ospf_interface
*oi
;
3636 /* This LSA is already checked. */
3637 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3638 return IS_LSA_SELF(lsa
);
3640 /* Make sure LSA is self-checked. */
3641 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3643 /* AdvRouter and Router ID is the same. */
3644 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
, &ospf
->router_id
))
3645 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3647 /* LSA is router-LSA. */
3648 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
3649 && IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
))
3650 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3652 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3653 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3654 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
3655 /* Ignore virtual link. */
3656 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3657 if (oi
->address
->family
== AF_INET
)
3660 &oi
->address
->u
.prefix4
)) {
3661 /* to make it easier later */
3662 SET_FLAG(lsa
->flags
,
3664 return IS_LSA_SELF(lsa
);
3668 return IS_LSA_SELF(lsa
);
3671 /* Get unique Link State ID. */
3672 enum lsid_status
ospf_lsa_unique_id(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
3673 uint8_t type
, struct prefix_ipv4
*p
,
3676 struct ospf_lsa
*lsa
;
3677 struct in_addr mask
;
3681 /* Check existence of LSA instance. */
3682 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, *id
, ospf
->router_id
);
3684 struct as_external_lsa
*al
=
3685 (struct as_external_lsa
*)lsa
->data
;
3686 /* Ref rfc2328,Appendex E.1
3687 * If router already originated the external lsa with lsid
3688 * as the current prefix, and the masklens are same then
3689 * terminate the LSID algorithem.
3691 if (ip_masklen(al
->mask
) == p
->prefixlen
) {
3692 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3694 "%s: Can't get Link State ID for %pFX",
3696 /* id.s_addr = 0; */
3697 id
->s_addr
= 0xffffffff;
3698 return LSID_NOT_AVAILABLE
;
3699 } else if (ip_masklen(al
->mask
) < p
->prefixlen
) {
3700 /* Ref rfc2328,Appendex E.2
3701 * the current prefix masklen is greater than the
3702 * existing LSA, then generate the Link state ID,
3703 * by setting all host bits in prefix addressa and
3706 * Eg: 1st Route : 10.0.0.0/16 - LSID:10.0.0.0
3707 * 2nd Route : 10.0.0.0/24 - LSID:10.0.0.255
3709 masklen2ip(p
->prefixlen
, &mask
);
3711 id
->s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3712 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, *id
,
3715 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3717 "%s: Can't get Link State ID for %pFX",
3719 id
->s_addr
= 0xffffffff;
3720 return LSID_NOT_AVAILABLE
;
3723 /* Ref rfc2328,Appendex E.3
3724 * the current prefix masklen is lesser than the
3725 * existing LSA,then the originated LSA has to be
3726 * refreshed by modifying masklen, cost and tag.
3727 * Originate the old route info with new LSID by
3728 * setting the host bits in prefix address.
3730 * Eg: 1st Route : 10.0.0.0/24 - LSID:10.0.0.0
3731 * 2nd Route : 10.0.0.0/16 - ?
3732 * Since 2nd route mask len is less than firstone
3733 * LSID has to be changed.
3734 * 1st route LSID:10.0.0.255
3735 * 2nd route LSID:10.0.0.0
3737 id
->s_addr
= lsa
->data
->id
.s_addr
| (~al
->mask
.s_addr
);
3738 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, *id
,
3740 if (lsa
&& (ip_masklen(al
->mask
) != IPV4_MAX_BITLEN
)) {
3741 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3743 "%s: Can't get Link State ID for %pFX",
3745 id
->s_addr
= 0xffffffff;
3746 return LSID_NOT_AVAILABLE
;
3752 return LSID_AVAILABLE
;
3756 #define LSA_ACTION_FLOOD_AREA 1
3757 #define LSA_ACTION_FLUSH_AREA 2
3761 struct ospf_area
*area
;
3762 struct ospf_lsa
*lsa
;
3765 static void ospf_lsa_action(struct thread
*t
)
3767 struct lsa_action
*data
;
3769 data
= THREAD_ARG(t
);
3771 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3772 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3775 switch (data
->action
) {
3776 case LSA_ACTION_FLOOD_AREA
:
3777 ospf_flood_through_area(data
->area
, NULL
, data
->lsa
);
3779 case LSA_ACTION_FLUSH_AREA
:
3780 ospf_lsa_flush_area(data
->lsa
, data
->area
);
3784 ospf_lsa_unlock(&data
->lsa
); /* Message */
3785 XFREE(MTYPE_OSPF_MESSAGE
, data
);
3788 void ospf_schedule_lsa_flood_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3790 struct lsa_action
*data
;
3792 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3793 data
->action
= LSA_ACTION_FLOOD_AREA
;
3795 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flood area */
3797 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3800 void ospf_schedule_lsa_flush_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3802 struct lsa_action
*data
;
3804 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3805 data
->action
= LSA_ACTION_FLUSH_AREA
;
3807 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flush area */
3809 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3813 /* LSA Refreshment functions. */
3814 struct ospf_lsa
*ospf_lsa_refresh(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3816 struct external_info
*ei
;
3817 struct ospf_external_aggr_rt
*aggr
;
3818 struct ospf_lsa
*new = NULL
;
3819 struct as_external_lsa
*al
;
3820 struct prefix_ipv4 p
;
3822 assert(CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF
));
3823 assert(IS_LSA_SELF(lsa
));
3824 assert(lsa
->lock
> 0);
3826 switch (lsa
->data
->type
) {
3827 /* Router and Network LSAs are processed differently. */
3828 case OSPF_ROUTER_LSA
:
3829 new = ospf_router_lsa_refresh(lsa
);
3831 case OSPF_NETWORK_LSA
:
3832 new = ospf_network_lsa_refresh(lsa
);
3834 case OSPF_SUMMARY_LSA
:
3835 new = ospf_summary_lsa_refresh(ospf
, lsa
);
3837 case OSPF_ASBR_SUMMARY_LSA
:
3838 new = ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
3840 case OSPF_AS_EXTERNAL_LSA
:
3841 /* Translated from NSSA Type-5s are refreshed when
3842 * from refresh of Type-7 - do not refresh these directly.
3845 al
= (struct as_external_lsa
*)lsa
->data
;
3847 p
.prefixlen
= ip_masklen(al
->mask
);
3848 p
.prefix
= lsa
->data
->id
;
3850 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3852 ei
= ospf_external_info_check(ospf
, lsa
);
3854 new = ospf_external_lsa_refresh(
3855 ospf
, lsa
, ei
, LSA_REFRESH_FORCE
, false);
3857 aggr
= (struct ospf_external_aggr_rt
*)
3858 ospf_extrenal_aggregator_lookup(ospf
, &p
);
3860 struct external_info ei_aggr
;
3862 memset(&ei_aggr
, 0, sizeof(ei_aggr
));
3863 ei_aggr
.p
= aggr
->p
;
3864 ei_aggr
.tag
= aggr
->tag
;
3865 ei_aggr
.instance
= ospf
->instance
;
3866 ei_aggr
.route_map_set
.metric
= -1;
3867 ei_aggr
.route_map_set
.metric_type
= -1;
3869 ospf_external_lsa_refresh(ospf
, lsa
, &ei_aggr
,
3870 LSA_REFRESH_FORCE
, true);
3871 SET_FLAG(aggr
->flags
,
3872 OSPF_EXTERNAL_AGGRT_ORIGINATED
);
3874 ospf_lsa_flush_as(ospf
, lsa
);
3877 case OSPF_OPAQUE_LINK_LSA
:
3878 case OSPF_OPAQUE_AREA_LSA
:
3879 case OSPF_OPAQUE_AS_LSA
:
3880 new = ospf_opaque_lsa_refresh(lsa
);
3888 void ospf_refresher_register_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3890 uint16_t index
, current_index
;
3892 assert(lsa
->lock
> 0);
3893 assert(IS_LSA_SELF(lsa
));
3895 if (lsa
->refresh_list
< 0) {
3898 ospf
->lsa_refresh_timer
- (2 * OSPF_LS_REFRESH_JITTER
);
3900 ospf
->lsa_refresh_timer
- OSPF_LS_REFRESH_JITTER
;
3902 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3904 * 1800s. Use jitter so that we send the LSA sometime between
3908 delay
= (frr_weak_random() % (max_delay
- min_delay
))
3911 current_index
= ospf
->lsa_refresh_queue
.index
3912 + (monotime(NULL
) - ospf
->lsa_refresher_started
)
3913 / OSPF_LSA_REFRESHER_GRANULARITY
;
3915 index
= (current_index
+ delay
/ OSPF_LSA_REFRESHER_GRANULARITY
)
3916 % (OSPF_LSA_REFRESHER_SLOTS
);
3918 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3920 "LSA[Refresh:Type%d:%pI4]: age %d, added to index %d",
3921 lsa
->data
->type
, &lsa
->data
->id
,
3922 LS_AGE(lsa
), index
);
3924 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3925 ospf
->lsa_refresh_queue
.qs
[index
] = list_new();
3927 listnode_add(ospf
->lsa_refresh_queue
.qs
[index
],
3928 ospf_lsa_lock(lsa
)); /* lsa_refresh_queue */
3929 lsa
->refresh_list
= index
;
3931 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3933 "LSA[Refresh:Type%d:%pI4]: %s: setting refresh_list on lsa %p (slot %d)",
3934 lsa
->data
->type
, &lsa
->data
->id
, __func__
,
3935 (void *)lsa
, index
);
3939 void ospf_refresher_unregister_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3941 assert(lsa
->lock
> 0);
3942 assert(IS_LSA_SELF(lsa
));
3943 if (lsa
->refresh_list
>= 0) {
3944 struct list
*refresh_list
=
3945 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3946 listnode_delete(refresh_list
, lsa
);
3947 if (!listcount(refresh_list
)) {
3948 list_delete(&refresh_list
);
3949 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3951 lsa
->refresh_list
= -1;
3952 ospf_lsa_unlock(&lsa
); /* lsa_refresh_queue */
3956 void ospf_lsa_refresh_walker(struct thread
*t
)
3958 struct list
*refresh_list
;
3959 struct listnode
*node
, *nnode
;
3960 struct ospf
*ospf
= THREAD_ARG(t
);
3961 struct ospf_lsa
*lsa
;
3963 struct list
*lsa_to_refresh
= list_new();
3965 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3966 zlog_debug("LSA[Refresh]: %s: start", __func__
);
3969 i
= ospf
->lsa_refresh_queue
.index
;
3971 /* Note: if clock has jumped backwards, then time change could be
3973 so we are careful to cast the expression to unsigned before taking
3975 ospf
->lsa_refresh_queue
.index
=
3976 ((unsigned long)(ospf
->lsa_refresh_queue
.index
3978 - ospf
->lsa_refresher_started
)
3979 / OSPF_LSA_REFRESHER_GRANULARITY
))
3980 % OSPF_LSA_REFRESHER_SLOTS
;
3982 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3983 zlog_debug("LSA[Refresh]: %s: next index %d", __func__
,
3984 ospf
->lsa_refresh_queue
.index
);
3986 for (; i
!= ospf
->lsa_refresh_queue
.index
;
3987 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
) {
3988 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3989 zlog_debug("LSA[Refresh]: %s: refresh index %d",
3992 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3996 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3999 for (ALL_LIST_ELEMENTS(refresh_list
, node
, nnode
,
4001 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4003 "LSA[Refresh:Type%d:%pI4]: %s: refresh lsa %p (slot %d)",
4004 lsa
->data
->type
, &lsa
->data
->id
,
4005 __func__
, (void *)lsa
, i
);
4007 assert(lsa
->lock
> 0);
4008 list_delete_node(refresh_list
, node
);
4009 lsa
->refresh_list
= -1;
4010 listnode_add(lsa_to_refresh
, lsa
);
4012 list_delete(&refresh_list
);
4016 ospf
->t_lsa_refresher
= NULL
;
4017 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
4018 ospf
->lsa_refresh_interval
, &ospf
->t_lsa_refresher
);
4019 ospf
->lsa_refresher_started
= monotime(NULL
);
4021 for (ALL_LIST_ELEMENTS(lsa_to_refresh
, node
, nnode
, lsa
)) {
4022 ospf_lsa_refresh(ospf
, lsa
);
4023 assert(lsa
->lock
> 0);
4025 &lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
4028 list_delete(&lsa_to_refresh
);
4030 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4031 zlog_debug("LSA[Refresh]: %s: end", __func__
);
4034 /* Flush the LSAs for the specific area */
4035 void ospf_flush_lsa_from_area(struct ospf
*ospf
, struct in_addr area_id
,
4038 struct ospf_area
*area
;
4039 struct route_node
*rn
;
4040 struct ospf_lsa
*lsa
;
4042 area
= ospf_area_get(ospf
, area_id
);
4045 case OSPF_AS_EXTERNAL_LSA
:
4046 if ((area
->external_routing
== OSPF_AREA_NSSA
) ||
4047 (area
->external_routing
== OSPF_AREA_STUB
)) {
4048 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
4049 if (IS_LSA_SELF(lsa
) &&
4050 !(CHECK_FLAG(lsa
->flags
,
4051 OSPF_LSA_LOCAL_XLT
)))
4052 ospf_lsa_flush_area(lsa
, area
);
4055 case OSPF_AS_NSSA_LSA
:
4056 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
4057 if (IS_LSA_SELF(lsa
))
4058 ospf_lsa_flush_area(lsa
, area
);