2 * OSPF Link State Advertisement
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h" /* for inet_aton() */
38 #include "ospfd/ospfd.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_ism.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_lsa.h"
43 #include "ospfd/ospf_lsdb.h"
44 #include "ospfd/ospf_neighbor.h"
45 #include "ospfd/ospf_nsm.h"
46 #include "ospfd/ospf_flood.h"
47 #include "ospfd/ospf_packet.h"
48 #include "ospfd/ospf_spf.h"
49 #include "ospfd/ospf_dump.h"
50 #include "ospfd/ospf_route.h"
51 #include "ospfd/ospf_ase.h"
52 #include "ospfd/ospf_zebra.h"
53 #include "ospfd/ospf_abr.h"
54 #include "ospfd/ospf_errors.h"
56 uint32_t get_metric(uint8_t *metric
)
60 m
= (m
<< 8) + metric
[1];
61 m
= (m
<< 8) + metric
[2];
66 struct timeval
int2tv(int a
)
76 struct timeval
msec2tv(int a
)
80 ret
.tv_sec
= a
/ 1000;
81 ret
.tv_usec
= (a
% 1000) * 1000;
86 int ospf_lsa_refresh_delay(struct ospf_lsa
*lsa
)
91 if (monotime_since(&lsa
->tv_orig
, &delta
)
92 < OSPF_MIN_LS_INTERVAL
* 1000LL) {
93 struct timeval minv
= msec2tv(OSPF_MIN_LS_INTERVAL
);
94 timersub(&minv
, &delta
, &minv
);
96 /* TBD: remove padding to full sec, return timeval instead */
97 delay
= minv
.tv_sec
+ !!minv
.tv_usec
;
99 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
101 "LSA[Type%d:%pI4]: Refresh timer delay %d seconds",
102 lsa
->data
->type
, &lsa
->data
->id
,
112 int get_age(struct ospf_lsa
*lsa
)
116 monotime_since(&lsa
->tv_recv
, &rel
);
117 return ntohs(lsa
->data
->ls_age
) + rel
.tv_sec
;
121 /* Fletcher Checksum -- Refer to RFC1008. */
123 /* All the offsets are zero-based. The offsets in the RFC1008 are
125 uint16_t ospf_lsa_checksum(struct lsa_header
*lsa
)
127 uint8_t *buffer
= &lsa
->options
;
128 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
130 /* Skip the AGE field */
131 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
133 /* Checksum offset starts from "options" field, not the beginning of the
134 lsa_header struct. The offset is 14, rather than 16. */
135 int checksum_offset
= (uint8_t *)&lsa
->checksum
- buffer
;
137 return fletcher_checksum(buffer
, len
, checksum_offset
);
140 int ospf_lsa_checksum_valid(struct lsa_header
*lsa
)
142 uint8_t *buffer
= &lsa
->options
;
143 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
145 /* Skip the AGE field */
146 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
148 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
153 /* Create OSPF LSA. */
154 struct ospf_lsa
*ospf_lsa_new(void)
156 struct ospf_lsa
*new;
158 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
162 new->retransmit_counter
= 0;
163 monotime(&new->tv_recv
);
164 new->tv_orig
= new->tv_recv
;
165 new->refresh_list
= -1;
166 new->vrf_id
= VRF_DEFAULT
;
167 new->to_be_acknowledged
= 0;
172 struct ospf_lsa
*ospf_lsa_new_and_data(size_t size
)
174 struct ospf_lsa
*new;
176 new = ospf_lsa_new();
177 new->data
= ospf_lsa_data_new(size
);
183 /* Duplicate OSPF LSA. */
184 struct ospf_lsa
*ospf_lsa_dup(struct ospf_lsa
*lsa
)
186 struct ospf_lsa
*new;
191 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
193 memcpy(new, lsa
, sizeof(struct ospf_lsa
));
194 UNSET_FLAG(new->flags
, OSPF_LSA_DISCARD
);
196 new->retransmit_counter
= 0;
197 new->data
= ospf_lsa_data_dup(lsa
->data
);
199 /* kevinm: Clear the refresh_list, otherwise there are going
200 to be problems when we try to remove the LSA from the
201 queue (which it's not a member of.)
202 XXX: Should we add the LSA to the refresh_list queue? */
203 new->refresh_list
= -1;
205 if (IS_DEBUG_OSPF(lsa
, LSA
))
206 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa
,
213 void ospf_lsa_free(struct ospf_lsa
*lsa
)
215 assert(lsa
->lock
== 0);
217 if (IS_DEBUG_OSPF(lsa
, LSA
))
218 zlog_debug("LSA: freed %p", (void *)lsa
);
220 /* Delete LSA data. */
221 if (lsa
->data
!= NULL
)
222 ospf_lsa_data_free(lsa
->data
);
224 assert(lsa
->refresh_list
< 0);
226 memset(lsa
, 0, sizeof(struct ospf_lsa
));
227 XFREE(MTYPE_OSPF_LSA
, lsa
);
231 struct ospf_lsa
*ospf_lsa_lock(struct ospf_lsa
*lsa
)
238 void ospf_lsa_unlock(struct ospf_lsa
**lsa
)
240 /* This is sanity check. */
246 assert((*lsa
)->lock
>= 0);
248 if ((*lsa
)->lock
== 0) {
249 assert(CHECK_FLAG((*lsa
)->flags
, OSPF_LSA_DISCARD
));
255 /* Check discard flag. */
256 void ospf_lsa_discard(struct ospf_lsa
*lsa
)
258 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
259 SET_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
);
260 ospf_lsa_unlock(&lsa
);
264 /* Create LSA data. */
265 struct lsa_header
*ospf_lsa_data_new(size_t size
)
267 return XCALLOC(MTYPE_OSPF_LSA_DATA
, size
);
270 /* Duplicate LSA data. */
271 struct lsa_header
*ospf_lsa_data_dup(struct lsa_header
*lsah
)
273 struct lsa_header
*new;
275 new = ospf_lsa_data_new(ntohs(lsah
->length
));
276 memcpy(new, lsah
, ntohs(lsah
->length
));
282 void ospf_lsa_data_free(struct lsa_header
*lsah
)
284 if (IS_DEBUG_OSPF(lsa
, LSA
))
285 zlog_debug("LSA[Type%d:%pI4]: data freed %p", lsah
->type
,
286 &lsah
->id
, (void *)lsah
);
288 XFREE(MTYPE_OSPF_LSA_DATA
, lsah
);
292 /* LSA general functions. */
294 const char *dump_lsa_key(struct ospf_lsa
*lsa
)
296 static char buf
[sizeof("Type255,id(255.255.255.255),ar(255.255.255.255)")+1];
297 struct lsa_header
*lsah
;
299 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
) {
300 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
301 inet_ntop(AF_INET
, &lsah
->id
, id
, sizeof(id
));
302 inet_ntop(AF_INET
, &lsah
->adv_router
, ar
, sizeof(ar
));
304 snprintf(buf
, sizeof(buf
), "Type%d,id(%s),ar(%s)", lsah
->type
,
307 strlcpy(buf
, "NULL", sizeof(buf
));
312 uint32_t lsa_seqnum_increment(struct ospf_lsa
*lsa
)
316 seqnum
= ntohl(lsa
->data
->ls_seqnum
) + 1;
318 return htonl(seqnum
);
321 void lsa_header_set(struct stream
*s
, uint8_t options
, uint8_t type
,
322 struct in_addr id
, struct in_addr router_id
)
324 struct lsa_header
*lsah
;
326 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
328 lsah
->ls_age
= htons(OSPF_LSA_INITIAL_AGE
);
329 lsah
->options
= options
;
332 lsah
->adv_router
= router_id
;
333 lsah
->ls_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
335 stream_forward_endp(s
, OSPF_LSA_HEADER_SIZE
);
339 /* router-LSA related functions. */
340 /* Get router-LSA flags. */
341 uint8_t router_lsa_flags(struct ospf_area
*area
)
345 flags
= area
->ospf
->flags
;
347 /* Set virtual link flag. */
348 if (ospf_full_virtual_nbrs(area
))
349 SET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
351 /* Just sanity check */
352 UNSET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
354 /* Set Shortcut ABR behabiour flag. */
355 UNSET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
356 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
357 if (!OSPF_IS_AREA_BACKBONE(area
))
358 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
359 && area
->ospf
->backbone
== NULL
)
360 || area
->shortcut_configured
361 == OSPF_SHORTCUT_ENABLE
)
362 SET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
364 /* ASBR can't exit in stub area. */
365 if (area
->external_routing
== OSPF_AREA_STUB
)
366 UNSET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
367 /* If ASBR set External flag */
368 else if (IS_OSPF_ASBR(area
->ospf
))
369 SET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
371 /* Set ABR dependent flags */
372 if (IS_OSPF_ABR(area
->ospf
)) {
373 SET_FLAG(flags
, ROUTER_LSA_BORDER
);
374 /* If Area is NSSA and we are both ABR and unconditional
376 * set Nt bit to inform other routers.
378 if ((area
->external_routing
== OSPF_AREA_NSSA
)
379 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
380 SET_FLAG(flags
, ROUTER_LSA_NT
);
385 /* Lookup neighbor other than myself.
386 And check neighbor count,
387 Point-to-Point link must have only 1 neighbor. */
388 struct ospf_neighbor
*ospf_nbr_lookup_ptop(struct ospf_interface
*oi
)
390 struct ospf_neighbor
*nbr
= NULL
;
391 struct route_node
*rn
;
393 /* Search neighbor, there must be one of two nbrs. */
394 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
395 if ((nbr
= rn
->info
))
396 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
397 &oi
->ospf
->router_id
))
398 if (nbr
->state
== NSM_Full
) {
399 route_unlock_node(rn
);
403 /* PtoP link must have only 1 neighbor. */
404 if (ospf_nbr_count(oi
, 0) > 1)
405 flog_warn(EC_OSPF_PTP_NEIGHBOR
,
406 "Point-to-Point link has more than 1 neighobrs.");
411 /* Determine cost of link, taking RFC3137 stub-router support into
414 static uint16_t ospf_link_cost(struct ospf_interface
*oi
)
416 /* RFC3137 stub router support */
417 if (!CHECK_FLAG(oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
418 return oi
->output_cost
;
420 return OSPF_OUTPUT_COST_INFINITE
;
423 /* Set a link information. */
424 char link_info_set(struct stream
**s
, struct in_addr id
, struct in_addr data
,
425 uint8_t type
, uint8_t tos
, uint16_t cost
)
427 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
428 * vast majority of cases. Some rare routers with lots of links need
430 * we try accomodate those here.
432 if (STREAM_WRITEABLE(*s
) < OSPF_ROUTER_LSA_LINK_SIZE
) {
433 size_t ret
= OSPF_MAX_LSA_SIZE
;
435 /* Can we enlarge the stream still? */
436 if (STREAM_SIZE(*s
) == OSPF_MAX_LSA_SIZE
) {
437 /* we futz the size here for simplicity, really we need
440 * IP Header - (sizeof(struct ip))
441 * OSPF Header - OSPF_HEADER_SIZE
442 * LSA Header - OSPF_LSA_HEADER_SIZE
443 * MD5 auth data, if MD5 is configured -
444 * OSPF_AUTH_MD5_SIZE.
446 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
448 ret
= stream_resize_inplace(
449 s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
452 if (ret
== OSPF_MAX_LSA_SIZE
) {
455 "%s: Out of space in LSA stream, left %zd, size %zd",
456 __func__
, STREAM_WRITEABLE(*s
),
462 /* TOS based routing is not supported. */
463 stream_put_ipv4(*s
, id
.s_addr
); /* Link ID. */
464 stream_put_ipv4(*s
, data
.s_addr
); /* Link Data. */
465 stream_putc(*s
, type
); /* Link Type. */
466 stream_putc(*s
, tos
); /* TOS = 0. */
467 stream_putw(*s
, cost
); /* Link Cost. */
472 /* Describe Point-to-Point link (Section 12.4.1.1). */
474 /* Note: If the interface is configured as point-to-point dmvpn then the other
475 * end of link is dmvpn hub with point-to-multipoint ospf network type. The
476 * hub then expects this router to populate the stub network and also Link Data
477 * Field set to IP Address and not MIB-II ifIndex
479 static int lsa_link_ptop_set(struct stream
**s
, struct ospf_interface
*oi
)
482 struct ospf_neighbor
*nbr
;
483 struct in_addr id
, mask
, data
;
484 uint16_t cost
= ospf_link_cost(oi
);
486 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
487 zlog_debug("LSA[Type1]: Set link Point-to-Point");
489 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
490 if (nbr
->state
== NSM_Full
) {
491 if (CHECK_FLAG(oi
->connected
->flags
,
492 ZEBRA_IFA_UNNUMBERED
)
494 /* For unnumbered point-to-point networks, the
496 should specify the interface's MIB-II ifIndex
498 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
499 links
+= link_info_set(
500 s
, nbr
->router_id
, data
,
501 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
503 links
+= link_info_set(
505 oi
->address
->u
.prefix4
,
506 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
510 /* no need for a stub link for unnumbered interfaces */
512 || !CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)) {
513 /* Regardless of the state of the neighboring router, we must
514 add a Type 3 link (stub network).
515 N.B. Options 1 & 2 share basically the same logic. */
516 masklen2ip(oi
->address
->prefixlen
, &mask
);
517 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
519 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
526 /* Describe Broadcast Link. */
527 static int lsa_link_broadcast_set(struct stream
**s
, struct ospf_interface
*oi
)
529 struct ospf_neighbor
*dr
;
530 struct in_addr id
, mask
;
531 uint16_t cost
= ospf_link_cost(oi
);
533 /* Describe Type 3 Link. */
534 if (oi
->state
== ISM_Waiting
) {
535 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
537 "LSA[Type1]: Interface %s is in state Waiting. Adding stub interface",
539 masklen2ip(oi
->address
->prefixlen
, &mask
);
540 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
541 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
545 dr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &DR(oi
));
546 /* Describe Type 2 link. */
547 if (dr
&& (dr
->state
== NSM_Full
548 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
)))
549 && ospf_nbr_count(oi
, NSM_Full
) > 0) {
550 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
552 "LSA[Type1]: Interface %s has a DR. Adding transit interface",
554 return link_info_set(s
, DR(oi
), oi
->address
->u
.prefix4
,
555 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
557 /* Describe type 3 link. */
559 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
561 "LSA[Type1]: Interface %s has no DR. Adding stub interface",
563 masklen2ip(oi
->address
->prefixlen
, &mask
);
564 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
565 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
570 static int lsa_link_loopback_set(struct stream
**s
, struct ospf_interface
*oi
)
572 struct in_addr id
, mask
;
574 /* Describe Type 3 Link. */
575 if (oi
->state
!= ISM_Loopback
)
578 mask
.s_addr
= 0xffffffff;
579 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
580 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
583 /* Describe Virtual Link. */
584 static int lsa_link_virtuallink_set(struct stream
**s
,
585 struct ospf_interface
*oi
)
587 struct ospf_neighbor
*nbr
;
588 uint16_t cost
= ospf_link_cost(oi
);
590 if (oi
->state
== ISM_PointToPoint
)
591 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
592 if (nbr
->state
== NSM_Full
) {
593 return link_info_set(s
, nbr
->router_id
,
594 oi
->address
->u
.prefix4
,
595 LSA_LINK_TYPE_VIRTUALLINK
,
602 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
604 /* this function add for support point-to-multipoint ,see rfc2328
606 /* from "edward rrr" <edward_rrr@hotmail.com>
607 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
608 static int lsa_link_ptomp_set(struct stream
**s
, struct ospf_interface
*oi
)
611 struct route_node
*rn
;
612 struct ospf_neighbor
*nbr
= NULL
;
613 struct in_addr id
, mask
;
614 uint16_t cost
= ospf_link_cost(oi
);
616 mask
.s_addr
= 0xffffffff;
617 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
618 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
620 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
621 zlog_debug("PointToMultipoint: running ptomultip_set");
623 /* Search neighbor, */
624 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
625 if ((nbr
= rn
->info
) != NULL
)
627 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
628 &oi
->ospf
->router_id
))
629 if (nbr
->state
== NSM_Full
)
632 links
+= link_info_set(
634 oi
->address
->u
.prefix4
,
635 LSA_LINK_TYPE_POINTOPOINT
, 0,
637 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
639 "PointToMultipoint: set link to %pI4",
640 &oi
->address
->u
.prefix4
);
646 /* Set router-LSA link information. */
647 static int router_lsa_link_set(struct stream
**s
, struct ospf_area
*area
)
649 struct listnode
*node
;
650 struct ospf_interface
*oi
;
653 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
654 struct interface
*ifp
= oi
->ifp
;
656 /* Check interface is up, OSPF is enable. */
657 if (if_is_operative(ifp
)) {
658 if (oi
->state
!= ISM_Down
) {
659 oi
->lsa_pos_beg
= links
;
660 /* Describe each link. */
662 case OSPF_IFTYPE_POINTOPOINT
:
663 links
+= lsa_link_ptop_set(s
, oi
);
665 case OSPF_IFTYPE_BROADCAST
:
666 links
+= lsa_link_broadcast_set(s
, oi
);
668 case OSPF_IFTYPE_NBMA
:
669 links
+= lsa_link_nbma_set(s
, oi
);
671 case OSPF_IFTYPE_POINTOMULTIPOINT
:
672 links
+= lsa_link_ptomp_set(s
, oi
);
674 case OSPF_IFTYPE_VIRTUALLINK
:
676 lsa_link_virtuallink_set(s
, oi
);
678 case OSPF_IFTYPE_LOOPBACK
:
679 links
+= lsa_link_loopback_set(s
, oi
);
681 oi
->lsa_pos_end
= links
;
689 /* Set router-LSA body. */
690 void ospf_router_lsa_body_set(struct stream
**s
, struct ospf_area
*area
)
696 stream_putc(*s
, router_lsa_flags(area
));
698 /* Set Zero fields. */
701 /* Keep pointer to # links. */
702 putp
= stream_get_endp(*s
);
707 /* Set all link information. */
708 cnt
= router_lsa_link_set(s
, area
);
710 /* Set # of links here. */
711 stream_putw_at(*s
, putp
, cnt
);
714 static int ospf_stub_router_timer(struct thread
*t
)
716 struct ospf_area
*area
= THREAD_ARG(t
);
718 area
->t_stub_router
= NULL
;
720 SET_FLAG(area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
722 /* clear stub route state and generate router-lsa refresh, don't
723 * clobber an administratively set stub-router state though.
725 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
728 UNSET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
730 ospf_router_lsa_update_area(area
);
735 static void ospf_stub_router_check(struct ospf_area
*area
)
737 /* area must either be administratively configured to be stub
738 * or startup-time stub-router must be configured and we must in a
742 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
)) {
743 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
747 /* not admin-stubbed, check whether startup stubbing is configured and
748 * whether it's not been done yet
750 if (CHECK_FLAG(area
->stub_router_state
,
751 OSPF_AREA_WAS_START_STUB_ROUTED
))
754 if (area
->ospf
->stub_router_startup_time
755 == OSPF_STUB_ROUTER_UNCONFIGURED
) {
756 /* stub-router is hence done forever for this area, even if
758 * tries configure it (take effect next restart).
760 SET_FLAG(area
->stub_router_state
,
761 OSPF_AREA_WAS_START_STUB_ROUTED
);
765 /* startup stub-router configured and not yet done */
766 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
768 OSPF_AREA_TIMER_ON(area
->t_stub_router
, ospf_stub_router_timer
,
769 area
->ospf
->stub_router_startup_time
);
772 /* Create new router-LSA. */
773 static struct ospf_lsa
*ospf_router_lsa_new(struct ospf_area
*area
)
775 struct ospf
*ospf
= area
->ospf
;
777 struct lsa_header
*lsah
;
778 struct ospf_lsa
*new;
781 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
782 zlog_debug("LSA[Type1]: Create router-LSA instance");
784 /* check whether stub-router is desired, and if this is the first
787 ospf_stub_router_check(area
);
789 /* Create a stream for LSA. */
790 s
= stream_new(OSPF_MAX_LSA_SIZE
);
791 /* Set LSA common header fields. */
792 lsa_header_set(s
, LSA_OPTIONS_GET(area
) | LSA_OPTIONS_NSSA_GET(area
),
793 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
795 /* Set router-LSA body fields. */
796 ospf_router_lsa_body_set(&s
, area
);
799 length
= stream_get_endp(s
);
800 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
801 lsah
->length
= htons(length
);
803 /* Now, create OSPF LSA instance. */
804 new = ospf_lsa_new_and_data(length
);
807 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
808 new->vrf_id
= area
->ospf
->vrf_id
;
810 /* Copy LSA data to store, discard stream. */
811 memcpy(new->data
, lsah
, length
);
817 /* Originate Router-LSA. */
818 static struct ospf_lsa
*ospf_router_lsa_originate(struct ospf_area
*area
)
820 struct ospf_lsa
*new;
822 if (area
->ospf
->gr_info
.restart_in_progress
) {
823 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
825 "LSA[Type%d]: Graceful Restart in progress, don't originate",
830 /* Create new router-LSA instance. */
831 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
832 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
837 if (new->data
->adv_router
.s_addr
== INADDR_ANY
) {
838 if (IS_DEBUG_OSPF_EVENT
)
839 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
840 ospf_lsa_discard(new);
844 /* Install LSA to LSDB. */
845 new = ospf_lsa_install(area
->ospf
, NULL
, new);
847 /* Update LSA origination count. */
848 area
->ospf
->lsa_originate_count
++;
850 /* Flooding new LSA through area. */
851 ospf_flood_through_area(area
, NULL
, new);
853 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
854 zlog_debug("LSA[Type%d:%pI4]: Originate router-LSA %p",
855 new->data
->type
, &new->data
->id
,
857 ospf_lsa_header_dump(new->data
);
863 /* Refresh router-LSA. */
864 static struct ospf_lsa
*ospf_router_lsa_refresh(struct ospf_lsa
*lsa
)
866 struct ospf_area
*area
= lsa
->area
;
867 struct ospf_lsa
*new;
872 /* Delete LSA from neighbor retransmit-list. */
873 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
875 /* Unregister LSA from refresh-list */
876 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
878 /* Create new router-LSA instance. */
879 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
880 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
884 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
886 ospf_lsa_install(area
->ospf
, NULL
, new);
888 /* Flood LSA through area. */
889 ospf_flood_through_area(area
, NULL
, new);
892 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
893 zlog_debug("LSA[Type%d:%pI4]: router-LSA refresh",
894 new->data
->type
, &new->data
->id
);
895 ospf_lsa_header_dump(new->data
);
901 int ospf_router_lsa_update_area(struct ospf_area
*area
)
903 if (IS_DEBUG_OSPF_EVENT
)
904 zlog_debug("[router-LSA]: (router-LSA area update)");
906 /* Now refresh router-LSA. */
907 if (area
->router_lsa_self
)
908 ospf_lsa_refresh(area
->ospf
, area
->router_lsa_self
);
909 /* Newly originate router-LSA. */
911 ospf_router_lsa_originate(area
);
916 int ospf_router_lsa_update(struct ospf
*ospf
)
918 struct listnode
*node
, *nnode
;
919 struct ospf_area
*area
;
921 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
922 zlog_debug("Timer[router-LSA Update]: (timer expire)");
924 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
925 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
926 struct router_lsa
*rl
;
927 const char *area_str
;
929 /* Keep Area ID string. */
930 area_str
= AREA_NAME(area
);
932 /* If LSA not exist in this Area, originate new. */
934 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
936 "LSA[Type1]: Create router-LSA for Area %s",
939 ospf_router_lsa_originate(area
);
941 /* If router-ID is changed, Link ID must change.
942 First flush old LSA, then originate new. */
943 else if (!IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
)) {
944 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
946 "LSA[Type%d:%pI4]: Refresh router-LSA for Area %s",
948 &lsa
->data
->id
, area_str
);
949 ospf_refresher_unregister_lsa(ospf
, lsa
);
950 ospf_lsa_flush_area(lsa
, area
);
951 ospf_lsa_unlock(&area
->router_lsa_self
);
952 area
->router_lsa_self
= NULL
;
954 /* Refresh router-LSA, (not install) and flood through
956 ospf_router_lsa_update_area(area
);
958 rl
= (struct router_lsa
*)lsa
->data
;
959 /* Refresh router-LSA, (not install) and flood through
961 if (rl
->flags
!= ospf
->flags
)
962 ospf_router_lsa_update_area(area
);
970 /* network-LSA related functions. */
971 /* Originate Network-LSA. */
972 static void ospf_network_lsa_body_set(struct stream
*s
,
973 struct ospf_interface
*oi
)
976 struct route_node
*rn
;
977 struct ospf_neighbor
*nbr
;
979 masklen2ip(oi
->address
->prefixlen
, &mask
);
980 stream_put_ipv4(s
, mask
.s_addr
);
982 /* The network-LSA lists those routers that are fully adjacent to
983 the Designated Router; each fully adjacent router is identified by
984 its OSPF Router ID. The Designated Router includes itself in this
985 list. RFC2328, Section 12.4.2 */
987 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
988 if ((nbr
= rn
->info
) != NULL
)
989 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
990 stream_put_ipv4(s
, nbr
->router_id
.s_addr
);
993 static struct ospf_lsa
*ospf_network_lsa_new(struct ospf_interface
*oi
)
996 struct ospf_lsa
*new;
997 struct lsa_header
*lsah
;
998 struct ospf_if_params
*oip
;
1001 /* If there are no neighbours on this network (the net is stub),
1002 the router does not originate network-LSA (see RFC 12.4.2) */
1003 if (oi
->full_nbrs
== 0)
1006 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1007 zlog_debug("LSA[Type2]: Create network-LSA instance");
1009 /* Create new stream for LSA. */
1010 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1011 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1013 lsa_header_set(s
, (OPTIONS(oi
) | LSA_OPTIONS_GET(oi
->area
)),
1014 OSPF_NETWORK_LSA
, DR(oi
), oi
->ospf
->router_id
);
1016 /* Set network-LSA body fields. */
1017 ospf_network_lsa_body_set(s
, oi
);
1020 length
= stream_get_endp(s
);
1021 lsah
->length
= htons(length
);
1023 /* Create OSPF LSA instance. */
1024 new = ospf_lsa_new_and_data(length
);
1026 new->area
= oi
->area
;
1027 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1028 new->vrf_id
= oi
->ospf
->vrf_id
;
1030 /* Copy LSA to store. */
1031 memcpy(new->data
, lsah
, length
);
1034 /* Remember prior network LSA sequence numbers, even if we stop
1035 * originating one for this oi, to try avoid re-originating LSAs with a
1036 * prior sequence number, and thus speed up adjency forming &
1039 if ((oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
))) {
1040 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1041 new->data
->ls_seqnum
= lsa_seqnum_increment(new);
1043 oip
= ospf_get_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1044 ospf_if_update_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1046 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1051 /* Originate network-LSA. */
1052 void ospf_network_lsa_update(struct ospf_interface
*oi
)
1054 struct ospf_lsa
*new;
1056 if (oi
->area
->ospf
->gr_info
.restart_in_progress
) {
1057 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1059 "LSA[Type%d]: Graceful Restart in progress, don't originate",
1064 if (oi
->network_lsa_self
!= NULL
) {
1065 ospf_lsa_refresh(oi
->ospf
, oi
->network_lsa_self
);
1069 /* Create new network-LSA instance. */
1070 new = ospf_network_lsa_new(oi
);
1074 /* Install LSA to LSDB. */
1075 new = ospf_lsa_install(oi
->ospf
, oi
, new);
1077 /* Update LSA origination count. */
1078 oi
->ospf
->lsa_originate_count
++;
1080 /* Flooding new LSA through area. */
1081 ospf_flood_through_area(oi
->area
, NULL
, new);
1083 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1084 zlog_debug("LSA[Type%d:%pI4]: Originate network-LSA %p",
1085 new->data
->type
, &new->data
->id
,
1087 ospf_lsa_header_dump(new->data
);
1093 static struct ospf_lsa
*ospf_network_lsa_refresh(struct ospf_lsa
*lsa
)
1095 struct ospf_area
*area
= lsa
->area
;
1096 struct ospf_lsa
*new, *new2
;
1097 struct ospf_if_params
*oip
;
1098 struct ospf_interface
*oi
;
1102 /* Retrieve the oi for the network LSA */
1103 oi
= ospf_if_lookup_by_local_addr(area
->ospf
, NULL
, lsa
->data
->id
);
1105 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1107 "LSA[Type%d:%pI4]: network-LSA refresh: no oi found, ick, ignoring.",
1108 lsa
->data
->type
, &lsa
->data
->id
);
1109 ospf_lsa_header_dump(lsa
->data
);
1113 /* Delete LSA from neighbor retransmit-list. */
1114 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
1116 /* Unregister LSA from refresh-list */
1117 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
1119 /* Create new network-LSA instance. */
1120 new = ospf_network_lsa_new(oi
);
1124 oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1125 assert(oip
!= NULL
);
1126 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
=
1127 lsa_seqnum_increment(lsa
);
1129 new2
= ospf_lsa_install(area
->ospf
, oi
, new);
1131 assert(new2
== new);
1133 /* Flood LSA through aera. */
1134 ospf_flood_through_area(area
, NULL
, new);
1136 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1137 zlog_debug("LSA[Type%d:%pI4]: network-LSA refresh",
1138 new->data
->type
, &new->data
->id
);
1139 ospf_lsa_header_dump(new->data
);
1145 static void stream_put_ospf_metric(struct stream
*s
, uint32_t metric_value
)
1150 /* Put 0 metric. TOS metric is not supported. */
1151 metric
= htonl(metric_value
);
1152 mp
= (char *)&metric
;
1154 stream_put(s
, mp
, 3);
1157 /* summary-LSA related functions. */
1158 static void ospf_summary_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1161 struct in_addr mask
;
1163 masklen2ip(p
->prefixlen
, &mask
);
1165 /* Put Network Mask. */
1166 stream_put_ipv4(s
, mask
.s_addr
);
1169 stream_putc(s
, (uint8_t)0);
1172 stream_put_ospf_metric(s
, metric
);
1175 static struct ospf_lsa
*ospf_summary_lsa_new(struct ospf_area
*area
,
1176 struct prefix
*p
, uint32_t metric
,
1180 struct ospf_lsa
*new;
1181 struct lsa_header
*lsah
;
1184 if (id
.s_addr
== 0xffffffff) {
1185 /* Maybe Link State ID not available. */
1186 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1188 "LSA[Type%d]: Link ID not available, can't originate",
1193 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1194 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1196 /* Create new stream for LSA. */
1197 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1198 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1200 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_SUMMARY_LSA
, id
,
1201 area
->ospf
->router_id
);
1203 /* Set summary-LSA body fields. */
1204 ospf_summary_lsa_body_set(s
, p
, metric
);
1207 length
= stream_get_endp(s
);
1208 lsah
->length
= htons(length
);
1210 /* Create OSPF LSA instance. */
1211 new = ospf_lsa_new_and_data(length
);
1213 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1214 new->vrf_id
= area
->ospf
->vrf_id
;
1216 /* Copy LSA to store. */
1217 memcpy(new->data
, lsah
, length
);
1223 /* Originate Summary-LSA. */
1224 struct ospf_lsa
*ospf_summary_lsa_originate(struct prefix_ipv4
*p
,
1226 struct ospf_area
*area
)
1228 struct ospf_lsa
*new;
1231 if (area
->ospf
->gr_info
.restart_in_progress
) {
1232 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1234 "LSA[Type%d]: Graceful Restart in progress, don't originate",
1239 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
);
1241 if (id
.s_addr
== 0xffffffff) {
1242 /* Maybe Link State ID not available. */
1243 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1245 "LSA[Type%d]: Link ID not available, can't originate",
1250 /* Create new summary-LSA instance. */
1251 if (!(new = ospf_summary_lsa_new(area
, (struct prefix
*)p
, metric
, id
)))
1254 /* Instlal LSA to LSDB. */
1255 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1257 /* Update LSA origination count. */
1258 area
->ospf
->lsa_originate_count
++;
1260 /* Flooding new LSA through area. */
1261 ospf_flood_through_area(area
, NULL
, new);
1263 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1264 zlog_debug("LSA[Type%d:%pI4]: Originate summary-LSA %p",
1265 new->data
->type
, &new->data
->id
,
1267 ospf_lsa_header_dump(new->data
);
1273 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
1274 struct ospf_lsa
*lsa
)
1276 struct ospf_lsa
*new;
1277 struct summary_lsa
*sl
;
1283 sl
= (struct summary_lsa
*)lsa
->data
;
1284 p
.prefixlen
= ip_masklen(sl
->mask
);
1285 new = ospf_summary_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1291 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1293 ospf_lsa_install(ospf
, NULL
, new);
1295 /* Flood LSA through AS. */
1296 ospf_flood_through_area(new->area
, NULL
, new);
1298 /* Debug logging. */
1299 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1300 zlog_debug("LSA[Type%d:%pI4]: summary-LSA refresh",
1301 new->data
->type
, &new->data
->id
);
1302 ospf_lsa_header_dump(new->data
);
1309 /* summary-ASBR-LSA related functions. */
1310 static void ospf_summary_asbr_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1313 /* Put Network Mask. */
1314 stream_put_ipv4(s
, (uint32_t)0);
1317 stream_putc(s
, (uint8_t)0);
1320 stream_put_ospf_metric(s
, metric
);
1323 static struct ospf_lsa
*ospf_summary_asbr_lsa_new(struct ospf_area
*area
,
1329 struct ospf_lsa
*new;
1330 struct lsa_header
*lsah
;
1333 if (id
.s_addr
== 0xffffffff) {
1334 /* Maybe Link State ID not available. */
1335 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1337 "LSA[Type%d]: Link ID not available, can't originate",
1338 OSPF_ASBR_SUMMARY_LSA
);
1342 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1343 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1345 /* Create new stream for LSA. */
1346 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1347 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1349 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_ASBR_SUMMARY_LSA
, id
,
1350 area
->ospf
->router_id
);
1352 /* Set summary-LSA body fields. */
1353 ospf_summary_asbr_lsa_body_set(s
, p
, metric
);
1356 length
= stream_get_endp(s
);
1357 lsah
->length
= htons(length
);
1359 /* Create OSPF LSA instance. */
1360 new = ospf_lsa_new_and_data(length
);
1362 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1363 new->vrf_id
= area
->ospf
->vrf_id
;
1365 /* Copy LSA to store. */
1366 memcpy(new->data
, lsah
, length
);
1372 /* Originate summary-ASBR-LSA. */
1373 struct ospf_lsa
*ospf_summary_asbr_lsa_originate(struct prefix_ipv4
*p
,
1375 struct ospf_area
*area
)
1377 struct ospf_lsa
*new;
1380 if (area
->ospf
->gr_info
.restart_in_progress
) {
1381 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1383 "LSA[Type%d]: Graceful Restart in progress, don't originate",
1384 OSPF_ASBR_SUMMARY_LSA
);
1388 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_ASBR_SUMMARY_LSA
,
1391 if (id
.s_addr
== 0xffffffff) {
1392 /* Maybe Link State ID not available. */
1393 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1395 "LSA[Type%d]: Link ID not available, can't originate",
1396 OSPF_ASBR_SUMMARY_LSA
);
1400 /* Create new summary-LSA instance. */
1401 new = ospf_summary_asbr_lsa_new(area
, (struct prefix
*)p
, metric
, id
);
1405 /* Install LSA to LSDB. */
1406 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1408 /* Update LSA origination count. */
1409 area
->ospf
->lsa_originate_count
++;
1411 /* Flooding new LSA through area. */
1412 ospf_flood_through_area(area
, NULL
, new);
1414 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1415 zlog_debug("LSA[Type%d:%pI4]: Originate summary-ASBR-LSA %p",
1416 new->data
->type
, &new->data
->id
,
1418 ospf_lsa_header_dump(new->data
);
1424 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
1425 struct ospf_lsa
*lsa
)
1427 struct ospf_lsa
*new;
1428 struct summary_lsa
*sl
;
1434 sl
= (struct summary_lsa
*)lsa
->data
;
1435 p
.prefixlen
= ip_masklen(sl
->mask
);
1436 new = ospf_summary_asbr_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1441 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1443 ospf_lsa_install(ospf
, NULL
, new);
1445 /* Flood LSA through area. */
1446 ospf_flood_through_area(new->area
, NULL
, new);
1448 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1449 zlog_debug("LSA[Type%d:%pI4]: summary-ASBR-LSA refresh",
1450 new->data
->type
, &new->data
->id
);
1451 ospf_lsa_header_dump(new->data
);
1457 /* AS-external-LSA related functions. */
1459 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1460 is connected, else 0*/
1461 static struct in_addr
ospf_external_lsa_nexthop_get(struct ospf
*ospf
,
1462 struct in_addr nexthop
)
1466 struct listnode
*node
;
1467 struct ospf_interface
*oi
;
1471 if (!nexthop
.s_addr
)
1474 /* Check whether nexthop is covered by OSPF network. */
1475 nh
.family
= AF_INET
;
1476 nh
.u
.prefix4
= nexthop
;
1477 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1479 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1480 * better to make use of the per-ifp table of ois.
1482 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1483 if (if_is_operative(oi
->ifp
))
1484 if (oi
->address
->family
== AF_INET
)
1485 if (prefix_match(oi
->address
, &nh
))
1491 /* NSSA-external-LSA related functions. */
1493 /* Get 1st IP connection for Forward Addr */
1495 struct in_addr
ospf_get_ip_from_ifp(struct ospf_interface
*oi
)
1499 fwd
.s_addr
= INADDR_ANY
;
1501 if (if_is_operative(oi
->ifp
))
1502 return oi
->address
->u
.prefix4
;
1507 /* Get 1st IP connection for Forward Addr */
1508 struct in_addr
ospf_get_nssa_ip(struct ospf_area
*area
)
1511 struct in_addr best_default
;
1512 struct listnode
*node
;
1513 struct ospf_interface
*oi
;
1516 best_default
.s_addr
= 0;
1518 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->oiflist
, node
, oi
)) {
1519 if (if_is_operative(oi
->ifp
))
1520 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1522 && oi
->address
->family
== AF_INET
) {
1523 if (best_default
.s_addr
== INADDR_ANY
)
1525 oi
->address
->u
.prefix4
;
1526 if (oi
->area
== area
)
1527 return oi
->address
->u
.prefix4
;
1530 if (best_default
.s_addr
!= INADDR_ANY
)
1531 return best_default
;
1533 if (best_default
.s_addr
!= INADDR_ANY
)
1534 return best_default
;
1539 int metric_type(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1541 struct ospf_redist
*red
;
1543 red
= ospf_redist_lookup(ospf
, src
, instance
);
1545 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
1546 : red
->dmetric
.type
);
1549 int metric_value(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1551 struct ospf_redist
*red
;
1553 red
= ospf_redist_lookup(ospf
, src
, instance
);
1554 if (!red
|| red
->dmetric
.value
< 0) {
1555 if (src
== DEFAULT_ROUTE
) {
1556 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1557 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1559 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1560 } else if (ospf
->default_metric
< 0)
1561 return DEFAULT_DEFAULT_METRIC
;
1563 return ospf
->default_metric
;
1566 return red
->dmetric
.value
;
1569 /* Set AS-external-LSA body. */
1570 static void ospf_external_lsa_body_set(struct stream
*s
,
1571 struct external_info
*ei
,
1574 struct prefix_ipv4
*p
= &ei
->p
;
1575 struct in_addr mask
, fwd_addr
;
1579 unsigned short instance
;
1581 /* Put Network Mask. */
1582 masklen2ip(p
->prefixlen
, &mask
);
1583 stream_put_ipv4(s
, mask
.s_addr
);
1585 /* If prefix is default, specify DEFAULT_ROUTE. */
1586 type
= is_default_prefix4(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1587 instance
= is_default_prefix4(&ei
->p
) ? 0 : ei
->instance
;
1589 mtype
= (ROUTEMAP_METRIC_TYPE(ei
) != -1)
1590 ? ROUTEMAP_METRIC_TYPE(ei
)
1591 : metric_type(ospf
, type
, instance
);
1593 mvalue
= (ROUTEMAP_METRIC(ei
) != -1)
1594 ? ROUTEMAP_METRIC(ei
)
1595 : metric_value(ospf
, type
, instance
);
1597 /* Put type of external metric. */
1598 stream_putc(s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1600 /* Put 0 metric. TOS metric is not supported. */
1601 stream_put_ospf_metric(s
, mvalue
);
1603 /* Get forwarding address to nexthop if on the Connection List, else 0.
1605 fwd_addr
= ospf_external_lsa_nexthop_get(ospf
, ei
->nexthop
);
1607 /* Put forwarding address. */
1608 stream_put_ipv4(s
, fwd_addr
.s_addr
);
1611 stream_putl(s
, ei
->tag
);
1614 /* Create new external-LSA. */
1615 static struct ospf_lsa
*ospf_external_lsa_new(struct ospf
*ospf
,
1616 struct external_info
*ei
,
1617 struct in_addr
*old_id
)
1620 struct lsa_header
*lsah
;
1621 struct ospf_lsa
*new;
1626 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1628 "LSA[Type5]: External info is NULL, can't originate");
1632 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1633 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1635 /* If old Link State ID is specified, refresh LSA with same ID. */
1638 /* Get Link State with unique ID. */
1640 id
= ospf_lsa_unique_id(ospf
, ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
,
1642 if (id
.s_addr
== 0xffffffff) {
1643 /* Maybe Link State ID not available. */
1644 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1646 "LSA[Type5]: Link ID not available, can't originate");
1651 /* Create new stream for LSA. */
1652 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1653 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1655 /* Set LSA common header fields. */
1656 lsa_header_set(s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
, id
,
1659 /* Set AS-external-LSA body fields. */
1660 ospf_external_lsa_body_set(s
, ei
, ospf
);
1663 length
= stream_get_endp(s
);
1664 lsah
->length
= htons(length
);
1666 /* Now, create OSPF LSA instance. */
1667 new = ospf_lsa_new_and_data(length
);
1669 SET_FLAG(new->flags
,
1670 OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1671 new->vrf_id
= ospf
->vrf_id
;
1673 /* Copy LSA data to store, discard stream. */
1674 memcpy(new->data
, lsah
, length
);
1681 static void ospf_install_flood_nssa(struct ospf
*ospf
, struct ospf_lsa
*lsa
,
1682 struct external_info
*ei
)
1684 struct ospf_lsa
*new;
1685 struct as_external_lsa
*extlsa
;
1686 struct ospf_area
*area
;
1687 struct listnode
*node
, *nnode
;
1689 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1690 * which originated from an NSSA area. In which case it should not be
1691 * flooded back to NSSA areas.
1693 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1696 /* NSSA Originate or Refresh (If anyNSSA)
1698 LSA is self-originated. And just installed as Type-5.
1699 Additionally, install as Type-7 LSDB for every attached NSSA.
1701 P-Bit controls which ABR performs translation to outside world; If
1702 we are an ABR....do not set the P-bit, because we send the Type-5,
1703 not as the ABR Translator, but as the ASBR owner within the AS!
1705 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1706 elected ABR Translator will see the P-bit, Translate, and re-flood.
1708 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1709 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1711 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
1712 /* Don't install Type-7 LSA's into nonNSSA area */
1713 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1716 /* make lsa duplicate, lock=1 */
1717 new = ospf_lsa_dup(lsa
);
1719 new->data
->type
= OSPF_AS_NSSA_LSA
;
1721 /* set P-bit if not ABR */
1722 if (!IS_OSPF_ABR(ospf
)) {
1723 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1725 /* set non-zero FWD ADDR
1727 draft-ietf-ospf-nssa-update-09.txt
1729 if the network between the NSSA AS boundary router and
1731 adjacent AS is advertised into OSPF as an internal OSPF
1733 the forwarding address should be the next op address as
1735 currently done with type-5 LSAs. If the intervening
1737 not adversited into OSPF as an internal OSPF route and
1739 type-7 LSA's P-bit is set a forwarding address should be
1740 selected from one of the router's active OSPF interface
1742 which belong to the NSSA. If no such addresses exist,
1744 no type-7 LSA's with the P-bit set should originate from
1748 /* kevinm: not updating lsa anymore, just new */
1749 extlsa
= (struct as_external_lsa
*)(new->data
);
1751 if (extlsa
->e
[0].fwd_addr
.s_addr
== INADDR_ANY
)
1752 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(
1753 area
); /* this NSSA area in ifp */
1755 if (extlsa
->e
[0].fwd_addr
.s_addr
== INADDR_ANY
) {
1756 if (IS_DEBUG_OSPF_NSSA
)
1758 "LSA[Type-7]: Could not build FWD-ADDR");
1759 ospf_lsa_discard(new);
1764 /* install also as Type-7 */
1765 ospf_lsa_install(ospf
, NULL
,
1766 new); /* Remove Old, Lock New = 2 */
1768 /* will send each copy, lock=2+n */
1769 ospf_flood_through_as(
1770 ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1774 static struct ospf_lsa
*ospf_lsa_translated_nssa_new(struct ospf
*ospf
,
1775 struct ospf_lsa
*type7
)
1778 struct ospf_lsa
*new;
1779 struct as_external_lsa
*ext
, *extnew
;
1780 struct external_info ei
;
1782 ext
= (struct as_external_lsa
*)(type7
->data
);
1784 /* need external_info struct, fill in bare minimum */
1785 ei
.p
.family
= AF_INET
;
1786 ei
.p
.prefix
= type7
->data
->id
;
1787 ei
.p
.prefixlen
= ip_masklen(ext
->mask
);
1788 ei
.type
= ZEBRA_ROUTE_OSPF
;
1789 ei
.nexthop
= ext
->header
.adv_router
;
1790 ei
.route_map_set
.metric
= -1;
1791 ei
.route_map_set
.metric_type
= -1;
1795 if ((new = ospf_external_lsa_new(ospf
, &ei
, &type7
->data
->id
))
1797 if (IS_DEBUG_OSPF_NSSA
)
1799 "%s: Could not originate Translated Type-5 for %pI4",
1800 __func__
, &ei
.p
.prefix
);
1804 extnew
= (struct as_external_lsa
*)(new->data
);
1806 /* copy over Type-7 data to new */
1807 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1808 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1809 if (type7
->area
->suppress_fa
) {
1810 extnew
->e
[0].fwd_addr
.s_addr
= 0;
1811 if (IS_DEBUG_OSPF_NSSA
)
1813 "ospf_lsa_translated_nssa_new(): Suppress forwarding address for %pI4",
1816 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1817 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1819 /* add translated flag, checksum and lock new lsa */
1820 SET_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1821 new = ospf_lsa_lock(new);
1826 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1827 struct ospf_lsa
*ospf_translated_nssa_originate(struct ospf
*ospf
,
1828 struct ospf_lsa
*type7
,
1829 struct ospf_lsa
*type5
)
1831 struct ospf_lsa
*new;
1832 struct as_external_lsa
*extnew
;
1834 if (ospf
->gr_info
.restart_in_progress
) {
1835 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1837 "LSA[Translated Type5]: Graceful Restart in progress, don't originate");
1841 /* we cant use ospf_external_lsa_originate() as we need to set
1842 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1845 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1846 if (IS_DEBUG_OSPF_NSSA
)
1848 "%s: Could not translate Type-7, Id %pI4, to Type-5",
1849 __func__
, &type7
->data
->id
);
1853 extnew
= (struct as_external_lsa
*)new->data
;
1855 /* Update LSA sequence number from translated Type-5 LSA */
1857 new->data
->ls_seqnum
= lsa_seqnum_increment(type5
);
1859 if ((new = ospf_lsa_install(ospf
, NULL
, new)) == NULL
) {
1860 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
1861 "%s: Could not install LSA id %pI4", __func__
,
1866 if (IS_DEBUG_OSPF_NSSA
) {
1867 zlog_debug("%s: translated Type 7, installed", __func__
);
1868 ospf_lsa_header_dump(new->data
);
1869 zlog_debug(" Network mask: %d", ip_masklen(extnew
->mask
));
1870 zlog_debug(" Forward addr: %pI4",
1871 &extnew
->e
[0].fwd_addr
);
1874 ospf
->lsa_originate_count
++;
1875 ospf_flood_through_as(ospf
, NULL
, new);
1880 /* Refresh Translated from NSSA AS-external-LSA. */
1881 struct ospf_lsa
*ospf_translated_nssa_refresh(struct ospf
*ospf
,
1882 struct ospf_lsa
*type7
,
1883 struct ospf_lsa
*type5
)
1885 struct ospf_lsa
*new = NULL
;
1886 struct as_external_lsa
*extold
= NULL
;
1887 uint32_t ls_seqnum
= 0;
1889 /* Sanity checks. */
1890 assert(type7
|| type5
);
1891 if (!(type7
|| type5
))
1894 assert(type7
->data
);
1896 assert(type5
->data
);
1897 assert(ospf
->anyNSSA
);
1899 /* get required data according to what has been given */
1900 if (type7
&& type5
== NULL
) {
1901 /* find the translated Type-5 for this Type-7 */
1902 struct as_external_lsa
*ext
=
1903 (struct as_external_lsa
*)(type7
->data
);
1904 struct prefix_ipv4 p
= {
1905 .prefix
= type7
->data
->id
,
1906 .prefixlen
= ip_masklen(ext
->mask
),
1910 type5
= ospf_external_info_find_lsa(ospf
, &p
);
1911 } else if (type5
&& type7
== NULL
) {
1912 /* find the type-7 from which supplied type-5 was translated,
1913 * ie find first type-7 with same LSA Id.
1915 struct listnode
*ln
, *lnn
;
1916 struct route_node
*rn
;
1917 struct ospf_lsa
*lsa
;
1918 struct ospf_area
*area
;
1920 for (ALL_LIST_ELEMENTS(ospf
->areas
, ln
, lnn
, area
)) {
1921 if (area
->external_routing
!= OSPF_AREA_NSSA
&& !type7
)
1924 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
) {
1925 if (lsa
->data
->id
.s_addr
1926 == type5
->data
->id
.s_addr
) {
1934 /* do we have type7? */
1936 if (IS_DEBUG_OSPF_NSSA
)
1938 "ospf_translated_nssa_refresh(): no Type-7 found for Type-5 LSA Id %pI4",
1943 /* do we have valid translated type5? */
1944 if (type5
== NULL
|| !CHECK_FLAG(type5
->flags
, OSPF_LSA_LOCAL_XLT
)) {
1945 if (IS_DEBUG_OSPF_NSSA
)
1947 "ospf_translated_nssa_refresh(): No translated Type-5 found for Type-7 with Id %pI4",
1952 extold
= (struct as_external_lsa
*)type5
->data
;
1953 if (type7
->area
->suppress_fa
== 1) {
1954 if (extold
->e
[0].fwd_addr
.s_addr
== 0)
1955 ls_seqnum
= ntohl(type5
->data
->ls_seqnum
);
1958 /* Delete LSA from neighbor retransmit-list. */
1959 ospf_ls_retransmit_delete_nbr_as(ospf
, type5
);
1961 /* create new translated LSA */
1962 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1963 if (IS_DEBUG_OSPF_NSSA
)
1965 "ospf_translated_nssa_refresh(): Could not translate Type-7 for %pI4 to Type-5",
1970 if (type7
->area
->suppress_fa
== 1) {
1971 if (extold
->e
[0].fwd_addr
.s_addr
== 0)
1972 new->data
->ls_seqnum
= htonl(ls_seqnum
+ 1);
1975 if (!(new = ospf_lsa_install(ospf
, NULL
, new))) {
1977 EC_OSPF_LSA_INSTALL_FAILURE
,
1978 "ospf_translated_nssa_refresh(): Could not install translated LSA, Id %pI4",
1983 /* Flood LSA through area. */
1984 ospf_flood_through_as(ospf
, NULL
, new);
1989 /* Originate an AS-external-LSA, install and flood. */
1990 struct ospf_lsa
*ospf_external_lsa_originate(struct ospf
*ospf
,
1991 struct external_info
*ei
)
1993 struct ospf_lsa
*new;
1995 if (ospf
->gr_info
.restart_in_progress
) {
1996 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1998 "LSA[Type5]: Graceful Restart in progress, don't originate");
2002 /* Added for NSSA project....
2004 External LSAs are originated in ASBRs as usual, but for NSSA
2006 there is the global Type-5 LSDB and a Type-7 LSDB installed for
2007 every area. The Type-7's are flooded to every IR and every ABR; We
2008 install the Type-5 LSDB so that the normal "refresh" code operates
2009 as usual, and flag them as not used during ASE calculations. The
2010 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2011 Address of non-zero.
2013 If an ABR is the elected NSSA translator, following SPF and during
2014 the ABR task it will translate all the scanned Type-7's, with P-bit
2015 ON and not-self generated, and translate to Type-5's throughout the
2018 A difference in operation depends whether this ASBR is an ABR
2019 or not. If not an ABR, the P-bit is ON, to indicate that any
2020 elected NSSA-ABR can perform its translation.
2022 If an ABR, the P-bit is OFF; No ABR will perform translation and
2023 this ASBR will flood the Type-5 LSA as usual.
2025 For the case where this ASBR is not an ABR, the ASE calculations
2026 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2027 demonstrate to the user that there are LSA's that belong to any
2030 Finally, it just so happens that when the ABR is translating every
2031 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2032 approved Type-5 (translated from Type-7); at the end of translation
2033 if any Translated Type-5's remain unapproved, then they must be
2034 flushed from the AS.
2038 if (ospf
->router_id
.s_addr
== INADDR_ANY
) {
2039 if (IS_DEBUG_OSPF_EVENT
)
2041 "LSA[Type5:%pI4]: deferring AS-external-LSA origination, router ID is zero",
2046 /* Create new AS-external-LSA instance. */
2047 if ((new = ospf_external_lsa_new(ospf
, ei
, NULL
)) == NULL
) {
2048 if (IS_DEBUG_OSPF_EVENT
)
2050 "LSA[Type5:%pI4]: Could not originate AS-external-LSA",
2055 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2056 ospf_lsa_install(ospf
, NULL
, new);
2058 /* Update LSA origination count. */
2059 ospf
->lsa_originate_count
++;
2061 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2062 ospf_flood_through_as(ospf
, NULL
, new);
2064 /* If there is any attached NSSA, do special handling */
2065 if (ospf
->anyNSSA
&&
2066 /* stay away from translated LSAs! */
2067 !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2068 ospf_install_flood_nssa(
2069 ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2071 /* Debug logging. */
2072 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2073 zlog_debug("LSA[Type%d:%pI4]: Originate AS-external-LSA %p",
2074 new->data
->type
, &new->data
->id
,
2076 ospf_lsa_header_dump(new->data
);
2082 static struct external_info
*ospf_default_external_info(struct ospf
*ospf
)
2085 struct prefix_ipv4 p
;
2086 struct external_info
*default_ei
;
2090 p
.prefix
.s_addr
= 0;
2093 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
,
2094 ospf
->instance
, &p
);
2098 /* First, lookup redistributed default route. */
2099 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
2100 struct list
*ext_list
;
2102 if (type
== ZEBRA_ROUTE_OSPF
)
2105 ext_list
= ospf
->external
[type
];
2109 ret
= ospf_external_default_routemap_apply_walk(ospf
, ext_list
,
2118 void ospf_external_lsa_rid_change(struct ospf
*ospf
)
2120 struct external_info
*ei
;
2121 struct ospf_external_aggr_rt
*aggr
;
2122 struct ospf_lsa
*lsa
= NULL
;
2126 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++) {
2127 struct route_node
*rn
;
2128 struct route_table
*rt
;
2129 struct list
*ext_list
;
2130 struct listnode
*node
;
2131 struct ospf_external
*ext
;
2133 ext_list
= ospf
->external
[type
];
2137 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2138 /* Originate As-external-LSA from all type of
2139 * distribute source.
2141 rt
= ext
->external_info
;
2145 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
2151 if (is_default_prefix4(&ei
->p
))
2154 lsa
= ospf_external_info_find_lsa(ospf
, &ei
->p
);
2156 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
2159 if (!ospf_redistribute_check(ospf
, ei
,
2163 if (IS_DEBUG_OSPF(lsa
, EXTNL_LSA_AGGR
))
2165 "Originate Summary LSA after reset/router-ID change");
2167 /* Here the LSA is originated as new */
2168 ospf_originate_summary_lsa(ospf
, aggr
,
2171 /* LSA needs to be refreshed even if
2172 * there is no change in the route
2173 * params if the LSA is in maxage.
2175 if (IS_LSA_MAXAGE(lsa
))
2176 force
= LSA_REFRESH_FORCE
;
2178 force
= LSA_REFRESH_IF_CHANGED
;
2180 ospf_external_lsa_refresh(ospf
, lsa
,
2183 if (!ospf_redistribute_check(ospf
, ei
,
2187 if (!ospf_external_lsa_originate(ospf
,
2190 EC_OSPF_LSA_INSTALL_FAILURE
,
2191 "LSA: AS-external-LSA was not originated.");
2197 ei
= ospf_default_external_info(ospf
);
2198 if (ei
&& !ospf_external_lsa_originate(ospf
, ei
)) {
2199 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2200 "LSA: AS-external-LSA for default route was not originated.");
2204 /* Flush any NSSA LSAs for given prefix */
2205 void ospf_nssa_lsa_flush(struct ospf
*ospf
, struct prefix_ipv4
*p
)
2207 struct listnode
*node
, *nnode
;
2208 struct ospf_lsa
*lsa
= NULL
;
2209 struct ospf_area
*area
;
2211 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2212 if (area
->external_routing
== OSPF_AREA_NSSA
) {
2213 lsa
= ospf_lsa_lookup(ospf
, area
, OSPF_AS_NSSA_LSA
,
2214 p
->prefix
, ospf
->router_id
);
2216 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2218 "LSA: There is no such AS-NSSA-LSA %pFX in LSDB",
2222 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
2223 if (!IS_LSA_MAXAGE(lsa
)) {
2224 ospf_refresher_unregister_lsa(ospf
, lsa
);
2225 ospf_lsa_flush_area(lsa
, area
);
2231 /* Flush an AS-external-LSA from LSDB and routing domain. */
2232 void ospf_external_lsa_flush(struct ospf
*ospf
, uint8_t type
,
2233 struct prefix_ipv4
*p
,
2234 ifindex_t ifindex
/*, struct in_addr nexthop */)
2236 struct ospf_lsa
*lsa
;
2238 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2239 zlog_debug("LSA: Flushing AS-external-LSA %pFX", p
);
2241 /* First lookup LSA from LSDB. */
2242 if (!(lsa
= ospf_external_info_find_lsa(ospf
, p
))) {
2243 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2245 "LSA: There is no such AS-external-LSA %pFX in LSDB",
2250 /* If LSA is selforiginated, not a translated LSA, and there is
2251 * NSSA area, flush Type-7 LSA's at first.
2253 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2254 && !(CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2255 ospf_nssa_lsa_flush(ospf
, p
);
2257 /* Sweep LSA from Link State Retransmit List. */
2258 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2260 if (!IS_LSA_MAXAGE(lsa
)) {
2261 /* Unregister LSA from Refresh queue. */
2262 ospf_refresher_unregister_lsa(ospf
, lsa
);
2264 /* Flush AS-external-LSA through AS. */
2265 ospf_lsa_flush_as(ospf
, lsa
);
2268 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2269 zlog_debug("ospf_external_lsa_flush(): stop");
2272 void ospf_external_lsa_refresh_default(struct ospf
*ospf
)
2274 struct prefix_ipv4 p
;
2275 struct external_info
*ei
;
2276 struct ospf_lsa
*lsa
;
2280 p
.prefix
.s_addr
= INADDR_ANY
;
2282 ei
= ospf_default_external_info(ospf
);
2283 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
2286 if (IS_DEBUG_OSPF_EVENT
)
2287 zlog_debug("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2289 ospf_external_lsa_refresh(ospf
, lsa
, ei
, LSA_REFRESH_FORCE
,
2291 } else if (ei
&& !lsa
) {
2292 if (IS_DEBUG_OSPF_EVENT
)
2294 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2295 ospf_external_lsa_originate(ospf
, ei
);
2297 if (IS_DEBUG_OSPF_EVENT
)
2298 zlog_debug("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2299 ospf_external_lsa_flush(ospf
, DEFAULT_ROUTE
, &p
, 0);
2303 void ospf_external_lsa_refresh_type(struct ospf
*ospf
, uint8_t type
,
2304 unsigned short instance
, int force
)
2306 struct route_node
*rn
;
2307 struct external_info
*ei
;
2308 struct ospf_external
*ext
;
2310 if (type
== DEFAULT_ROUTE
)
2313 ext
= ospf_external_lookup(ospf
, type
, instance
);
2315 if (ext
&& EXTERNAL_INFO(ext
)) {
2316 /* Refresh each redistributed AS-external-LSAs. */
2317 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
2318 rn
= route_next(rn
)) {
2321 if (!is_default_prefix4(&ei
->p
)) {
2322 struct ospf_lsa
*lsa
;
2323 struct ospf_external_aggr_rt
*aggr
;
2325 aggr
= ospf_external_aggr_match(ospf
,
2327 lsa
= ospf_external_info_find_lsa(
2330 /* Check the AS-external-LSA
2331 * should be originated.
2333 if (!ospf_redistribute_check(
2336 ospf_unlink_ei_from_aggr(
2345 "%s: Send Aggreate LSA (%pFX)",
2349 ospf_originate_summary_lsa(
2354 if (IS_LSA_MAXAGE(lsa
))
2355 force
= LSA_REFRESH_FORCE
;
2357 ospf_external_lsa_refresh(
2358 ospf
, lsa
, ei
, force
,
2361 if (!ospf_redistribute_check(
2364 ospf_external_lsa_originate(
2373 /* Refresh AS-external-LSA. */
2374 struct ospf_lsa
*ospf_external_lsa_refresh(struct ospf
*ospf
,
2375 struct ospf_lsa
*lsa
,
2376 struct external_info
*ei
, int force
,
2379 struct ospf_lsa
*new;
2382 /* Check the AS-external-LSA should be originated. */
2384 if (!ospf_redistribute_check(ospf
, ei
, &changed
)) {
2385 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2387 "LSA[Type%d:%pI4] Could not be refreshed, redist check fail",
2391 ospf_external_lsa_flush(ospf
, ei
->type
, &ei
->p
,
2392 ei
->ifindex
/*, ei->nexthop */);
2396 if (!changed
&& !force
) {
2397 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2399 "LSA[Type%d:%pI4]: Not refreshed, not changed/forced",
2400 lsa
->data
->type
, &lsa
->data
->id
);
2404 /* Delete LSA from neighbor retransmit-list. */
2405 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2407 /* Unregister AS-external-LSA from refresh-list. */
2408 ospf_refresher_unregister_lsa(ospf
, lsa
);
2410 new = ospf_external_lsa_new(ospf
, ei
, &lsa
->data
->id
);
2413 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2414 zlog_debug("LSA[Type%d:%pI4]: Could not be refreshed",
2415 lsa
->data
->type
, &lsa
->data
->id
);
2419 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2421 ospf_lsa_install(ospf
, NULL
, new); /* As type-5. */
2423 /* Flood LSA through AS. */
2424 ospf_flood_through_as(ospf
, NULL
, new);
2426 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2427 if (ospf
->anyNSSA
&& !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2428 ospf_install_flood_nssa(ospf
, new,
2429 ei
); /* Install/Flood per new rules */
2431 /* Register self-originated LSA to refresh queue.
2432 * Translated LSAs should not be registered, but refreshed upon
2433 * refresh of the Type-7
2435 if (!CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2436 ospf_refresher_register_lsa(ospf
, new);
2438 /* Debug logging. */
2439 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2440 zlog_debug("LSA[Type%d:%pI4]: AS-external-LSA refresh",
2441 new->data
->type
, &new->data
->id
);
2442 ospf_lsa_header_dump(new->data
);
2449 /* LSA installation functions. */
2451 /* Install router-LSA to an area. */
2452 static struct ospf_lsa
*
2453 ospf_router_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2455 struct ospf_area
*area
= new->area
;
2457 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2458 The entire routing table must be recalculated, starting with
2459 the shortest path calculations for each area (not just the
2460 area whose link-state database has changed).
2463 if (IS_LSA_SELF(new)) {
2465 /* Only install LSA if it is originated/refreshed by us.
2466 * If LSA was received by flooding, the RECEIVED flag is set so
2468 * not link the LSA */
2469 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2470 return new; /* ignore stale LSA */
2472 /* Set self-originated router-LSA. */
2473 ospf_lsa_unlock(&area
->router_lsa_self
);
2474 area
->router_lsa_self
= ospf_lsa_lock(new);
2476 ospf_refresher_register_lsa(ospf
, new);
2479 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2483 /* Install network-LSA to an area. */
2484 static struct ospf_lsa
*ospf_network_lsa_install(struct ospf
*ospf
,
2485 struct ospf_interface
*oi
,
2486 struct ospf_lsa
*new,
2490 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2491 The entire routing table must be recalculated, starting with
2492 the shortest path calculations for each area (not just the
2493 area whose link-state database has changed).
2495 if (IS_LSA_SELF(new)) {
2496 /* We supposed that when LSA is originated by us, we pass the
2498 for which it was originated. If LSA was received by flooding,
2499 the RECEIVED flag is set, so we do not link the LSA to the
2501 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2502 return new; /* ignore stale LSA */
2504 ospf_lsa_unlock(&oi
->network_lsa_self
);
2505 oi
->network_lsa_self
= ospf_lsa_lock(new);
2506 ospf_refresher_register_lsa(ospf
, new);
2509 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2514 /* Install summary-LSA to an area. */
2515 static struct ospf_lsa
*
2516 ospf_summary_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2518 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2519 /* RFC 2328 Section 13.2 Summary-LSAs
2520 The best route to the destination described by the summary-
2521 LSA must be recalculated (see Section 16.5). If this
2522 destination is an AS boundary router, it may also be
2523 necessary to re-examine all the AS-external-LSAs.
2526 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2529 if (IS_LSA_SELF(new))
2530 ospf_refresher_register_lsa(ospf
, new);
2535 /* Install ASBR-summary-LSA to an area. */
2536 static struct ospf_lsa
*ospf_summary_asbr_lsa_install(struct ospf
*ospf
,
2537 struct ospf_lsa
*new,
2540 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2541 /* RFC 2328 Section 13.2 Summary-LSAs
2542 The best route to the destination described by the summary-
2543 LSA must be recalculated (see Section 16.5). If this
2544 destination is an AS boundary router, it may also be
2545 necessary to re-examine all the AS-external-LSAs.
2547 ospf_spf_calculate_schedule(ospf
,
2548 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2551 /* register LSA to refresh-list. */
2552 if (IS_LSA_SELF(new))
2553 ospf_refresher_register_lsa(ospf
, new);
2558 /* Install AS-external-LSA. */
2559 static struct ospf_lsa
*ospf_external_lsa_install(struct ospf
*ospf
,
2560 struct ospf_lsa
*new,
2563 ospf_ase_register_external_lsa(new, ospf
);
2564 /* If LSA is not self-originated, calculate an external route. */
2566 /* RFC 2328 Section 13.2 AS-external-LSAs
2567 The best route to the destination described by the AS-
2568 external-LSA must be recalculated (see Section 16.6).
2571 if (!IS_LSA_SELF(new))
2572 ospf_ase_incremental_update(ospf
, new);
2575 if (new->data
->type
== OSPF_AS_NSSA_LSA
) {
2576 /* There is no point to register selforiginate Type-7 LSA for
2577 * refreshing. We rely on refreshing Type-5 LSA's
2579 if (IS_LSA_SELF(new))
2582 /* Try refresh type-5 translated LSA for this LSA, if
2584 * New translations will be taken care of by the
2587 ospf_translated_nssa_refresh(ospf
, new, NULL
);
2588 ospf_schedule_abr_task(ospf
);
2592 /* Register self-originated LSA to refresh queue.
2593 * Leave Translated LSAs alone if NSSA is enabled
2595 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2596 ospf_refresher_register_lsa(ospf
, new);
2601 void ospf_discard_from_db(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
2602 struct ospf_lsa
*lsa
)
2604 struct ospf_lsa
*old
;
2609 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2614 if (old
->refresh_list
>= 0)
2615 ospf_refresher_unregister_lsa(ospf
, old
);
2617 switch (old
->data
->type
) {
2618 case OSPF_AS_EXTERNAL_LSA
:
2619 ospf_ase_unregister_external_lsa(old
, ospf
);
2620 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2622 case OSPF_OPAQUE_AS_LSA
:
2623 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2625 case OSPF_AS_NSSA_LSA
:
2626 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2627 ospf_ase_unregister_external_lsa(old
, ospf
);
2630 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2634 ospf_lsa_maxage_delete(ospf
, old
);
2635 ospf_lsa_discard(old
);
2638 struct ospf_lsa
*ospf_lsa_install(struct ospf
*ospf
, struct ospf_interface
*oi
,
2639 struct ospf_lsa
*lsa
)
2641 struct ospf_lsa
*new = NULL
;
2642 struct ospf_lsa
*old
= NULL
;
2643 struct ospf_lsdb
*lsdb
= NULL
;
2647 switch (lsa
->data
->type
) {
2649 case OSPF_AS_NSSA_LSA
:
2651 lsdb
= lsa
->area
->lsdb
;
2655 case OSPF_AS_EXTERNAL_LSA
:
2656 case OSPF_OPAQUE_AS_LSA
:
2661 lsdb
= lsa
->area
->lsdb
;
2667 /* RFC 2328 13.2. Installing LSAs in the database
2669 Installing a new LSA in the database, either as the result of
2670 flooding or a newly self-originated LSA, may cause the OSPF
2671 routing table structure to be recalculated. The contents of the
2672 new LSA should be compared to the old instance, if present. If
2673 there is no difference, there is no need to recalculate the
2674 routing table. When comparing an LSA to its previous instance,
2675 the following are all considered to be differences in contents:
2677 o The LSA's Options field has changed.
2679 o One of the LSA instances has LS age set to MaxAge, and
2682 o The length field in the LSA header has changed.
2684 o The body of the LSA (i.e., anything outside the 20-byte
2685 LSA header) has changed. Note that this excludes changes
2686 in LS Sequence Number and LS Checksum.
2689 /* Look up old LSA and determine if any SPF calculation or incremental
2691 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2693 /* Do comparision and record if recalc needed. */
2695 if (old
== NULL
|| ospf_lsa_different(old
, lsa
)) {
2696 /* Ref rfc3623 section 3.2.3
2697 * Installing new lsa or change in the existing LSA
2698 * or flushing existing LSA leads to topo change
2699 * and trigger SPF caculation.
2700 * So, router should be aborted from HELPER role
2701 * if it is detected as TOPO change.
2703 if (CHECK_LSA_TYPE_1_TO_5_OR_7(lsa
->data
->type
))
2704 ospf_helper_handle_topo_chg(ospf
, lsa
);
2710 Sequence number check (Section 14.1 of rfc 2328)
2711 "Premature aging is used when it is time for a self-originated
2712 LSA's sequence number field to wrap. At this point, the current
2713 LSA instance (having LS sequence number MaxSequenceNumber) must
2714 be prematurely aged and flushed from the routing domain before a
2715 new instance with sequence number equal to InitialSequenceNumber
2716 can be originated. "
2719 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
) {
2720 if (ospf_lsa_is_self_originated(ospf
, lsa
)) {
2721 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2723 if (!IS_LSA_MAXAGE(lsa
))
2724 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2725 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
2727 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
)) {
2729 "%s() Premature Aging lsa %p, seqnum 0x%x",
2731 ntohl(lsa
->data
->ls_seqnum
));
2732 ospf_lsa_header_dump(lsa
->data
);
2735 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2737 "%s() got an lsa with seq 0x80000000 that was not self originated. Ignoring",
2739 ospf_lsa_header_dump(lsa
->data
);
2745 /* discard old LSA from LSDB */
2747 ospf_discard_from_db(ospf
, lsdb
, lsa
);
2749 /* Calculate Checksum if self-originated?. */
2750 if (IS_LSA_SELF(lsa
))
2751 ospf_lsa_checksum(lsa
->data
);
2753 /* Insert LSA to LSDB. */
2754 ospf_lsdb_add(lsdb
, lsa
);
2757 /* Do LSA specific installation process. */
2758 switch (lsa
->data
->type
) {
2759 case OSPF_ROUTER_LSA
:
2760 new = ospf_router_lsa_install(ospf
, lsa
, rt_recalc
);
2762 case OSPF_NETWORK_LSA
:
2764 new = ospf_network_lsa_install(ospf
, oi
, lsa
, rt_recalc
);
2766 case OSPF_SUMMARY_LSA
:
2767 new = ospf_summary_lsa_install(ospf
, lsa
, rt_recalc
);
2769 case OSPF_ASBR_SUMMARY_LSA
:
2770 new = ospf_summary_asbr_lsa_install(ospf
, lsa
, rt_recalc
);
2772 case OSPF_AS_EXTERNAL_LSA
:
2773 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2775 case OSPF_OPAQUE_LINK_LSA
:
2776 if (IS_LSA_SELF(lsa
))
2777 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for
2780 /* Incoming "oi" for this LSA has set at LSUpd
2784 case OSPF_OPAQUE_AREA_LSA
:
2785 case OSPF_OPAQUE_AS_LSA
:
2786 new = ospf_opaque_lsa_install(lsa
, rt_recalc
);
2788 case OSPF_AS_NSSA_LSA
:
2789 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2790 default: /* type-6,8,9....nothing special */
2795 return new; /* Installation failed, cannot proceed further --
2799 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
)) {
2800 switch (lsa
->data
->type
) {
2801 case OSPF_AS_EXTERNAL_LSA
:
2802 case OSPF_OPAQUE_AS_LSA
:
2803 case OSPF_AS_NSSA_LSA
:
2804 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2805 lookup_msg(ospf_lsa_type_msg
,
2806 new->data
->type
, NULL
));
2809 zlog_debug("LSA[%s]: Install %s to Area %pI4",
2811 lookup_msg(ospf_lsa_type_msg
,
2812 new->data
->type
, NULL
),
2813 &new->area
->area_id
);
2819 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2820 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2822 if (IS_LSA_MAXAGE(new)) {
2823 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
))
2824 zlog_debug("LSA[%s]: Install LSA %p, MaxAge",
2825 dump_lsa_key(new), lsa
);
2826 ospf_lsa_maxage(ospf
, lsa
);
2833 int ospf_check_nbr_status(struct ospf
*ospf
)
2835 struct listnode
*node
, *nnode
;
2836 struct ospf_interface
*oi
;
2838 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
2839 struct route_node
*rn
;
2840 struct ospf_neighbor
*nbr
;
2842 if (ospf_if_is_enable(oi
))
2843 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
2844 if ((nbr
= rn
->info
) != NULL
)
2845 if (nbr
->state
== NSM_Exchange
2846 || nbr
->state
== NSM_Loading
) {
2847 route_unlock_node(rn
);
2856 static int ospf_maxage_lsa_remover(struct thread
*thread
)
2858 struct ospf
*ospf
= THREAD_ARG(thread
);
2859 struct ospf_lsa
*lsa
, *old
;
2860 struct route_node
*rn
;
2863 ospf
->t_maxage
= NULL
;
2865 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2866 zlog_debug("LSA[MaxAge]: remover Start");
2868 reschedule
= !ospf_check_nbr_status(ospf
);
2871 for (rn
= route_top(ospf
->maxage_lsa
); rn
;
2872 rn
= route_next(rn
)) {
2873 if ((lsa
= rn
->info
) == NULL
) {
2877 /* There is at least one neighbor from which we still
2879 * for that LSA, so we are not allowed to remove it from
2881 * as per RFC 2328 section 14 para 4 a) */
2882 if (lsa
->retransmit_counter
> 0) {
2887 /* TODO: maybe convert this function to a work-queue */
2888 if (thread_should_yield(thread
)) {
2889 OSPF_TIMER_ON(ospf
->t_maxage
,
2890 ospf_maxage_lsa_remover
, 0);
2892 rn
); /* route_top/route_next */
2896 /* Remove LSA from the LSDB */
2897 if (IS_LSA_SELF(lsa
))
2898 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2900 "LSA[Type%d:%pI4]: LSA 0x%lx is self-originated: ",
2903 (unsigned long)lsa
);
2905 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2907 "LSA[%s]: MaxAge LSA removed from list",
2910 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
)) {
2911 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2913 "originating new lsa for lsa %p",
2915 ospf_lsa_refresh(ospf
, lsa
);
2918 /* Remove from lsdb. */
2920 old
= ospf_lsdb_lookup(lsa
->lsdb
, lsa
);
2921 /* The max age LSA here must be the same
2922 * as the LSA in LSDB
2925 flog_err(EC_OSPF_LSA_MISSING
,
2926 "%s: LSA[%s]: LSA not in LSDB",
2927 __func__
, dump_lsa_key(lsa
));
2931 ospf_discard_from_db(ospf
, lsa
->lsdb
, lsa
);
2932 ospf_lsdb_delete(lsa
->lsdb
, lsa
);
2934 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2936 "%s: LSA[%s]: No associated LSDB!",
2937 __func__
, dump_lsa_key(lsa
));
2941 /* A MaxAge LSA must be removed immediately from the router's link
2942 state database as soon as both a) it is no longer contained on any
2943 neighbor Link state retransmission lists and b) none of the
2945 neighbors are in states Exchange or Loading. */
2947 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2948 ospf
->maxage_delay
);
2953 /* This function checks whether an LSA with initial sequence number should be
2954 * originated after a wrap in sequence number
2956 void ospf_check_and_gen_init_seq_lsa(struct ospf_interface
*oi
,
2957 struct ospf_lsa
*recv_lsa
)
2959 struct ospf_lsa
*lsa
= NULL
;
2960 struct ospf
*ospf
= oi
->ospf
;
2962 lsa
= ospf_lsa_lookup_by_header(oi
->area
, recv_lsa
->data
);
2964 if ((lsa
== NULL
) || (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
))
2965 || (lsa
->retransmit_counter
!= 0)) {
2966 if (IS_DEBUG_OSPF(lsa
, LSA
))
2968 "Do not generate LSA with initial seqence number.");
2972 ospf_lsa_maxage_delete(ospf
, lsa
);
2974 lsa
->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2976 ospf_lsa_refresh(ospf
, lsa
);
2979 void ospf_lsa_maxage_delete(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2981 struct route_node
*rn
;
2982 struct prefix lsa_prefix
;
2984 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2985 lsa_prefix
.family
= 0;
2986 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2987 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2989 if ((rn
= route_node_lookup(ospf
->maxage_lsa
, &lsa_prefix
))) {
2990 if (rn
->info
== lsa
) {
2991 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2992 ospf_lsa_unlock(&lsa
); /* maxage_lsa */
2995 rn
); /* unlock node because lsa is deleted */
2997 route_unlock_node(rn
); /* route_node_lookup */
2999 if (IS_DEBUG_OSPF_EVENT
)
3000 zlog_debug("%s: lsa %s is not found in maxage db.",
3001 __func__
, dump_lsa_key(lsa
));
3005 /* Add LSA onto the MaxAge list, and schedule for removal.
3006 * This does *not* lead to the LSA being flooded, that must be taken
3007 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
3010 void ospf_lsa_maxage(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3012 struct prefix lsa_prefix
;
3013 struct route_node
*rn
;
3015 /* When we saw a MaxAge LSA flooded to us, we put it on the list
3016 and schedule the MaxAge LSA remover. */
3017 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
)) {
3018 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3020 "LSA[%s]: %p already exists on MaxAge LSA list",
3021 dump_lsa_key(lsa
), lsa
);
3025 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
3026 lsa_prefix
.family
= 0;
3027 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
3028 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
3030 rn
= route_node_get(ospf
->maxage_lsa
, &lsa_prefix
);
3031 if (rn
->info
!= NULL
) {
3032 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3034 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
3035 dump_lsa_key(lsa
), rn
->info
,
3036 (void *)lsa
, lsa_prefix
.prefixlen
);
3037 route_unlock_node(rn
);
3039 rn
->info
= ospf_lsa_lock(lsa
);
3040 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
3043 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3044 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
3047 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
3048 ospf
->maxage_delay
);
3051 static int ospf_lsa_maxage_walker_remover(struct ospf
*ospf
,
3052 struct ospf_lsa
*lsa
)
3054 /* Stay away from any Local Translated Type-7 LSAs */
3055 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3058 if (IS_LSA_MAXAGE(lsa
))
3059 /* Self-originated LSAs should NOT time-out instead,
3060 they're flushed and submitted to the max_age list explicitly.
3062 if (!ospf_lsa_is_self_originated(ospf
, lsa
)) {
3063 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3064 zlog_debug("LSA[%s]: is MaxAge",
3067 switch (lsa
->data
->type
) {
3068 case OSPF_OPAQUE_LINK_LSA
:
3069 case OSPF_OPAQUE_AREA_LSA
:
3070 case OSPF_OPAQUE_AS_LSA
:
3072 * As a general rule, whenever network topology
3074 * (due to an LSA removal in this case), routing
3076 * should be triggered. However, this is not
3078 * LSAs. Even if an opaque LSA instance is going
3080 * from the routing domain, it does not mean a
3082 * topology, and thus, routing recalculation is
3086 case OSPF_AS_EXTERNAL_LSA
:
3087 case OSPF_AS_NSSA_LSA
:
3088 ospf_ase_incremental_update(ospf
, lsa
);
3091 ospf_spf_calculate_schedule(ospf
,
3095 ospf_lsa_maxage(ospf
, lsa
);
3098 if (IS_LSA_MAXAGE(lsa
) && !ospf_lsa_is_self_originated(ospf
, lsa
))
3099 if (LS_AGE(lsa
) > OSPF_LSA_MAXAGE
+ 30)
3100 printf("Eek! Shouldn't happen!\n");
3105 /* Periodical check of MaxAge LSA. */
3106 int ospf_lsa_maxage_walker(struct thread
*thread
)
3108 struct ospf
*ospf
= THREAD_ARG(thread
);
3109 struct route_node
*rn
;
3110 struct ospf_lsa
*lsa
;
3111 struct ospf_area
*area
;
3112 struct listnode
*node
, *nnode
;
3114 ospf
->t_maxage_walker
= NULL
;
3116 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3117 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
3118 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3119 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
3120 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3121 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3122 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3123 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3124 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3125 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3126 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3127 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3128 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3129 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
3130 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3133 /* for AS-external-LSAs. */
3135 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3136 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3137 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3138 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3141 OSPF_TIMER_ON(ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3142 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3146 struct ospf_lsa
*ospf_lsa_lookup_by_prefix(struct ospf_lsdb
*lsdb
, uint8_t type
,
3147 struct prefix_ipv4
*p
,
3148 struct in_addr router_id
)
3150 struct ospf_lsa
*lsa
;
3151 struct in_addr mask
, id
;
3152 struct lsa_header_mask
{
3153 struct lsa_header header
;
3154 struct in_addr mask
;
3157 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, p
->prefix
, router_id
);
3161 masklen2ip(p
->prefixlen
, &mask
);
3163 hmask
= (struct lsa_header_mask
*)lsa
->data
;
3165 if (mask
.s_addr
!= hmask
->mask
.s_addr
) {
3166 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3167 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, router_id
);
3175 struct ospf_lsa
*ospf_lsa_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
3176 uint32_t type
, struct in_addr id
,
3177 struct in_addr adv_router
)
3183 case OSPF_ROUTER_LSA
:
3184 case OSPF_NETWORK_LSA
:
3185 case OSPF_SUMMARY_LSA
:
3186 case OSPF_ASBR_SUMMARY_LSA
:
3187 case OSPF_AS_NSSA_LSA
:
3188 case OSPF_OPAQUE_LINK_LSA
:
3189 case OSPF_OPAQUE_AREA_LSA
:
3190 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, adv_router
);
3191 case OSPF_AS_EXTERNAL_LSA
:
3192 case OSPF_OPAQUE_AS_LSA
:
3193 return ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
, adv_router
);
3201 struct ospf_lsa
*ospf_lsa_lookup_by_id(struct ospf_area
*area
, uint32_t type
,
3204 struct ospf_lsa
*lsa
;
3205 struct route_node
*rn
;
3208 case OSPF_ROUTER_LSA
:
3209 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3210 case OSPF_NETWORK_LSA
:
3211 for (rn
= route_top(NETWORK_LSDB(area
)); rn
;
3212 rn
= route_next(rn
))
3213 if ((lsa
= rn
->info
))
3214 if (IPV4_ADDR_SAME(&lsa
->data
->id
, &id
)) {
3215 route_unlock_node(rn
);
3219 case OSPF_SUMMARY_LSA
:
3220 case OSPF_ASBR_SUMMARY_LSA
:
3221 /* Currently not used. */
3223 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3224 case OSPF_AS_EXTERNAL_LSA
:
3225 case OSPF_AS_NSSA_LSA
:
3226 case OSPF_OPAQUE_LINK_LSA
:
3227 case OSPF_OPAQUE_AREA_LSA
:
3228 case OSPF_OPAQUE_AS_LSA
:
3229 /* Currently not used. */
3238 struct ospf_lsa
*ospf_lsa_lookup_by_header(struct ospf_area
*area
,
3239 struct lsa_header
*lsah
)
3241 struct ospf_lsa
*match
;
3244 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3245 * is redefined to have two subfields; opaque-type and opaque-id.
3246 * However, it is harmless to treat the two sub fields together, as if
3247 * they two were forming a unique LSA-ID.
3250 match
= ospf_lsa_lookup(area
->ospf
, area
, lsah
->type
, lsah
->id
,
3254 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3255 zlog_debug("LSA[Type%d:%pI4]: Lookup by header, NO MATCH",
3256 lsah
->type
, &lsah
->id
);
3261 /* return +n, l1 is more recent.
3262 return -n, l2 is more recent.
3263 return 0, l1 and l2 is identical. */
3264 int ospf_lsa_more_recent(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3269 if (l1
== NULL
&& l2
== NULL
)
3276 /* compare LS sequence number. */
3277 x
= (int)ntohl(l1
->data
->ls_seqnum
);
3278 y
= (int)ntohl(l2
->data
->ls_seqnum
);
3284 /* compare LS checksum. */
3285 r
= ntohs(l1
->data
->checksum
) - ntohs(l2
->data
->checksum
);
3289 /* compare LS age. */
3290 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3292 else if (!IS_LSA_MAXAGE(l1
) && IS_LSA_MAXAGE(l2
))
3295 /* compare LS age with MaxAgeDiff. */
3296 if (LS_AGE(l1
) - LS_AGE(l2
) > OSPF_LSA_MAXAGE_DIFF
)
3298 else if (LS_AGE(l2
) - LS_AGE(l1
) > OSPF_LSA_MAXAGE_DIFF
)
3301 /* LSAs are identical. */
3305 /* If two LSAs are different, return 1, otherwise return 0. */
3306 int ospf_lsa_different(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3314 if (l1
->data
->options
!= l2
->data
->options
)
3317 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3320 if (IS_LSA_MAXAGE(l2
) && !IS_LSA_MAXAGE(l1
))
3323 if (l1
->size
!= l2
->size
)
3329 if (CHECK_FLAG((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3330 return 1; /* May be a stale LSA in the LSBD */
3332 assert(l1
->size
> OSPF_LSA_HEADER_SIZE
);
3334 p1
= (char *)l1
->data
;
3335 p2
= (char *)l2
->data
;
3337 if (memcmp(p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3338 l1
->size
- OSPF_LSA_HEADER_SIZE
)
3345 int ospf_lsa_flush_schedule(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3347 if (lsa
== NULL
|| !IS_LSA_SELF(lsa
))
3350 if (IS_DEBUG_OSPF_EVENT
)
3352 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3353 lsa
->data
->type
, &lsa
->data
->id
);
3355 /* Force given lsa's age to MaxAge. */
3356 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
3358 switch (lsa
->data
->type
) {
3359 /* Opaque wants to be notified of flushes */
3360 case OSPF_OPAQUE_LINK_LSA
:
3361 case OSPF_OPAQUE_AREA_LSA
:
3362 case OSPF_OPAQUE_AS_LSA
:
3363 ospf_opaque_lsa_refresh(lsa
);
3366 ospf_refresher_unregister_lsa(ospf
, lsa
);
3367 ospf_lsa_flush(ospf
, lsa
);
3374 void ospf_flush_self_originated_lsas_now(struct ospf
*ospf
)
3376 struct listnode
*node
, *nnode
;
3377 struct listnode
*node2
, *nnode2
;
3378 struct ospf_area
*area
;
3379 struct ospf_interface
*oi
;
3380 struct ospf_lsa
*lsa
;
3381 struct route_node
*rn
;
3382 int need_to_flush_ase
= 0;
3384 ospf
->inst_shutdown
= 1;
3386 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3387 if ((lsa
= area
->router_lsa_self
) != NULL
) {
3388 if (IS_DEBUG_OSPF_EVENT
)
3390 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3394 ospf_refresher_unregister_lsa(ospf
, lsa
);
3395 ospf_lsa_flush_area(lsa
, area
);
3396 ospf_lsa_unlock(&area
->router_lsa_self
);
3397 area
->router_lsa_self
= NULL
;
3400 for (ALL_LIST_ELEMENTS(area
->oiflist
, node2
, nnode2
, oi
)) {
3401 if ((lsa
= oi
->network_lsa_self
) != NULL
3402 && oi
->state
== ISM_DR
&& oi
->full_nbrs
> 0) {
3403 if (IS_DEBUG_OSPF_EVENT
)
3405 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3409 ospf_refresher_unregister_lsa(
3410 ospf
, oi
->network_lsa_self
);
3411 ospf_lsa_flush_area(oi
->network_lsa_self
, area
);
3412 ospf_lsa_unlock(&oi
->network_lsa_self
);
3413 oi
->network_lsa_self
= NULL
;
3416 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3417 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3418 need_to_flush_ase
= 1;
3421 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3422 ospf_lsa_flush_schedule(ospf
, lsa
);
3423 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3424 ospf_lsa_flush_schedule(ospf
, lsa
);
3425 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3426 ospf_lsa_flush_schedule(ospf
, lsa
);
3427 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3428 ospf_lsa_flush_schedule(ospf
, lsa
);
3431 if (need_to_flush_ase
) {
3432 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3433 ospf_lsa_flush_schedule(ospf
, lsa
);
3434 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3435 ospf_lsa_flush_schedule(ospf
, lsa
);
3439 * Make sure that the MaxAge LSA remover is executed immediately,
3440 * without conflicting to other threads.
3442 if (ospf
->t_maxage
!= NULL
) {
3443 OSPF_TIMER_OFF(ospf
->t_maxage
);
3444 thread_execute(master
, ospf_maxage_lsa_remover
, ospf
, 0);
3450 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3451 /* An interface-independent version of ospf_lsa_is_self_originated */
3452 int ospf_lsa_is_self_originated(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3454 struct listnode
*node
;
3455 struct ospf_interface
*oi
;
3457 /* This LSA is already checked. */
3458 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3459 return IS_LSA_SELF(lsa
);
3461 /* Make sure LSA is self-checked. */
3462 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3464 /* AdvRouter and Router ID is the same. */
3465 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
, &ospf
->router_id
))
3466 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3468 /* LSA is router-LSA. */
3469 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
3470 && IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
))
3471 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3473 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3474 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3475 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
3476 /* Ignore virtual link. */
3477 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3478 if (oi
->address
->family
== AF_INET
)
3481 &oi
->address
->u
.prefix4
)) {
3482 /* to make it easier later */
3483 SET_FLAG(lsa
->flags
,
3485 return IS_LSA_SELF(lsa
);
3489 return IS_LSA_SELF(lsa
);
3492 /* Get unique Link State ID. */
3493 struct in_addr
ospf_lsa_unique_id(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
3494 uint8_t type
, struct prefix_ipv4
*p
)
3496 struct ospf_lsa
*lsa
;
3497 struct in_addr mask
, id
;
3501 /* Check existence of LSA instance. */
3502 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, ospf
->router_id
);
3504 struct as_external_lsa
*al
=
3505 (struct as_external_lsa
*)lsa
->data
;
3506 if (ip_masklen(al
->mask
) == p
->prefixlen
) {
3507 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3509 "ospf_lsa_unique_id(): Can't get Link State ID for %pFX",
3511 /* id.s_addr = 0; */
3512 id
.s_addr
= 0xffffffff;
3515 /* Masklen differs, then apply wildcard mask to Link State ID.
3518 masklen2ip(p
->prefixlen
, &mask
);
3520 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3521 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
,
3524 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3526 "ospf_lsa_unique_id(): Can't get Link State ID for %pFX",
3528 /* id.s_addr = 0; */
3529 id
.s_addr
= 0xffffffff;
3539 #define LSA_ACTION_FLOOD_AREA 1
3540 #define LSA_ACTION_FLUSH_AREA 2
3544 struct ospf_area
*area
;
3545 struct ospf_lsa
*lsa
;
3548 static int ospf_lsa_action(struct thread
*t
)
3550 struct lsa_action
*data
;
3552 data
= THREAD_ARG(t
);
3554 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3555 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3558 switch (data
->action
) {
3559 case LSA_ACTION_FLOOD_AREA
:
3560 ospf_flood_through_area(data
->area
, NULL
, data
->lsa
);
3562 case LSA_ACTION_FLUSH_AREA
:
3563 ospf_lsa_flush_area(data
->lsa
, data
->area
);
3567 ospf_lsa_unlock(&data
->lsa
); /* Message */
3568 XFREE(MTYPE_OSPF_MESSAGE
, data
);
3572 void ospf_schedule_lsa_flood_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3574 struct lsa_action
*data
;
3576 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3577 data
->action
= LSA_ACTION_FLOOD_AREA
;
3579 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flood area */
3581 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3584 void ospf_schedule_lsa_flush_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3586 struct lsa_action
*data
;
3588 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3589 data
->action
= LSA_ACTION_FLUSH_AREA
;
3591 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flush area */
3593 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3597 /* LSA Refreshment functions. */
3598 struct ospf_lsa
*ospf_lsa_refresh(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3600 struct external_info
*ei
;
3601 struct ospf_external_aggr_rt
*aggr
;
3602 struct ospf_lsa
*new = NULL
;
3603 struct as_external_lsa
*al
;
3604 struct prefix_ipv4 p
;
3606 assert(CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF
));
3607 assert(IS_LSA_SELF(lsa
));
3608 assert(lsa
->lock
> 0);
3610 switch (lsa
->data
->type
) {
3611 /* Router and Network LSAs are processed differently. */
3612 case OSPF_ROUTER_LSA
:
3613 new = ospf_router_lsa_refresh(lsa
);
3615 case OSPF_NETWORK_LSA
:
3616 new = ospf_network_lsa_refresh(lsa
);
3618 case OSPF_SUMMARY_LSA
:
3619 new = ospf_summary_lsa_refresh(ospf
, lsa
);
3621 case OSPF_ASBR_SUMMARY_LSA
:
3622 new = ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
3624 case OSPF_AS_EXTERNAL_LSA
:
3625 /* Translated from NSSA Type-5s are refreshed when
3626 * from refresh of Type-7 - do not refresh these directly.
3629 al
= (struct as_external_lsa
*)lsa
->data
;
3631 p
.prefixlen
= ip_masklen(al
->mask
);
3632 p
.prefix
= lsa
->data
->id
;
3634 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3636 ei
= ospf_external_info_check(ospf
, lsa
);
3638 new = ospf_external_lsa_refresh(
3639 ospf
, lsa
, ei
, LSA_REFRESH_FORCE
, false);
3641 aggr
= (struct ospf_external_aggr_rt
*)
3642 ospf_extrenal_aggregator_lookup(ospf
, &p
);
3644 struct external_info ei_aggr
;
3647 sizeof(struct external_info
));
3648 ei_aggr
.p
= aggr
->p
;
3649 ei_aggr
.tag
= aggr
->tag
;
3650 ei_aggr
.instance
= ospf
->instance
;
3651 ei_aggr
.route_map_set
.metric
= -1;
3652 ei_aggr
.route_map_set
.metric_type
= -1;
3654 ospf_external_lsa_refresh(ospf
, lsa
, &ei_aggr
,
3655 LSA_REFRESH_FORCE
, true);
3656 SET_FLAG(aggr
->flags
,
3657 OSPF_EXTERNAL_AGGRT_ORIGINATED
);
3659 ospf_lsa_flush_as(ospf
, lsa
);
3662 case OSPF_OPAQUE_LINK_LSA
:
3663 case OSPF_OPAQUE_AREA_LSA
:
3664 case OSPF_OPAQUE_AS_LSA
:
3665 new = ospf_opaque_lsa_refresh(lsa
);
3673 void ospf_refresher_register_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3675 uint16_t index
, current_index
;
3677 assert(lsa
->lock
> 0);
3678 assert(IS_LSA_SELF(lsa
));
3680 if (lsa
->refresh_list
< 0) {
3683 OSPF_LS_REFRESH_TIME
- (2 * OSPF_LS_REFRESH_JITTER
);
3684 int max_delay
= OSPF_LS_REFRESH_TIME
- OSPF_LS_REFRESH_JITTER
;
3686 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3688 * 1800s. Use jitter so that we send the LSA sometime between
3692 delay
= (frr_weak_random() % (max_delay
- min_delay
))
3695 current_index
= ospf
->lsa_refresh_queue
.index
3696 + (monotime(NULL
) - ospf
->lsa_refresher_started
)
3697 / OSPF_LSA_REFRESHER_GRANULARITY
;
3699 index
= (current_index
+ delay
/ OSPF_LSA_REFRESHER_GRANULARITY
)
3700 % (OSPF_LSA_REFRESHER_SLOTS
);
3702 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3704 "LSA[Refresh:Type%d:%pI4]: age %d, added to index %d",
3705 lsa
->data
->type
, &lsa
->data
->id
,
3706 LS_AGE(lsa
), index
);
3708 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3709 ospf
->lsa_refresh_queue
.qs
[index
] = list_new();
3711 listnode_add(ospf
->lsa_refresh_queue
.qs
[index
],
3712 ospf_lsa_lock(lsa
)); /* lsa_refresh_queue */
3713 lsa
->refresh_list
= index
;
3715 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3717 "LSA[Refresh:Type%d:%pI4]: ospf_refresher_register_lsa(): setting refresh_list on lsa %p (slod %d)",
3718 lsa
->data
->type
, &lsa
->data
->id
,
3719 (void *)lsa
, index
);
3723 void ospf_refresher_unregister_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3725 assert(lsa
->lock
> 0);
3726 assert(IS_LSA_SELF(lsa
));
3727 if (lsa
->refresh_list
>= 0) {
3728 struct list
*refresh_list
=
3729 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3730 listnode_delete(refresh_list
, lsa
);
3731 if (!listcount(refresh_list
)) {
3732 list_delete(&refresh_list
);
3733 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3735 lsa
->refresh_list
= -1;
3736 ospf_lsa_unlock(&lsa
); /* lsa_refresh_queue */
3740 int ospf_lsa_refresh_walker(struct thread
*t
)
3742 struct list
*refresh_list
;
3743 struct listnode
*node
, *nnode
;
3744 struct ospf
*ospf
= THREAD_ARG(t
);
3745 struct ospf_lsa
*lsa
;
3747 struct list
*lsa_to_refresh
= list_new();
3749 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3750 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3753 i
= ospf
->lsa_refresh_queue
.index
;
3755 /* Note: if clock has jumped backwards, then time change could be
3757 so we are careful to cast the expression to unsigned before taking
3759 ospf
->lsa_refresh_queue
.index
=
3760 ((unsigned long)(ospf
->lsa_refresh_queue
.index
3762 - ospf
->lsa_refresher_started
)
3763 / OSPF_LSA_REFRESHER_GRANULARITY
))
3764 % OSPF_LSA_REFRESHER_SLOTS
;
3766 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3768 "LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3769 ospf
->lsa_refresh_queue
.index
);
3771 for (; i
!= ospf
->lsa_refresh_queue
.index
;
3772 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
) {
3773 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3775 "LSA[Refresh]: ospf_lsa_refresh_walker(): refresh index %d",
3778 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3782 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3785 for (ALL_LIST_ELEMENTS(refresh_list
, node
, nnode
,
3787 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3789 "LSA[Refresh:Type%d:%pI4]: ospf_lsa_refresh_walker(): refresh lsa %p (slot %d)",
3794 assert(lsa
->lock
> 0);
3795 list_delete_node(refresh_list
, node
);
3796 lsa
->refresh_list
= -1;
3797 listnode_add(lsa_to_refresh
, lsa
);
3799 list_delete(&refresh_list
);
3803 ospf
->t_lsa_refresher
= NULL
;
3804 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
3805 ospf
->lsa_refresh_interval
, &ospf
->t_lsa_refresher
);
3806 ospf
->lsa_refresher_started
= monotime(NULL
);
3808 for (ALL_LIST_ELEMENTS(lsa_to_refresh
, node
, nnode
, lsa
)) {
3809 ospf_lsa_refresh(ospf
, lsa
);
3810 assert(lsa
->lock
> 0);
3812 &lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3815 list_delete(&lsa_to_refresh
);
3817 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3818 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3823 /* Flush the LSAs for the specific area */
3824 void ospf_flush_lsa_from_area(struct ospf
*ospf
, struct in_addr area_id
,
3827 struct ospf_area
*area
;
3828 struct route_node
*rn
;
3829 struct ospf_lsa
*lsa
;
3831 area
= ospf_area_get(ospf
, area_id
);
3834 case OSPF_AS_EXTERNAL_LSA
:
3835 if ((area
->external_routing
== OSPF_AREA_NSSA
) ||
3836 (area
->external_routing
== OSPF_AREA_STUB
)) {
3837 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3838 if (IS_LSA_SELF(lsa
) &&
3839 !(CHECK_FLAG(lsa
->flags
,
3840 OSPF_LSA_LOCAL_XLT
)))
3841 ospf_lsa_flush_area(lsa
, area
);
3844 case OSPF_AS_NSSA_LSA
:
3845 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
3846 if (IS_LSA_SELF(lsa
))
3847 ospf_lsa_flush_area(lsa
, area
);