2 * OSPF Link State Advertisement
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h" /* for inet_aton() */
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_lsdb.h"
43 #include "ospfd/ospf_neighbor.h"
44 #include "ospfd/ospf_nsm.h"
45 #include "ospfd/ospf_flood.h"
46 #include "ospfd/ospf_packet.h"
47 #include "ospfd/ospf_spf.h"
48 #include "ospfd/ospf_dump.h"
49 #include "ospfd/ospf_route.h"
50 #include "ospfd/ospf_ase.h"
51 #include "ospfd/ospf_zebra.h"
52 #include "ospfd/ospf_abr.h"
55 uint32_t get_metric(uint8_t *metric
)
59 m
= (m
<< 8) + metric
[1];
60 m
= (m
<< 8) + metric
[2];
65 struct timeval
int2tv(int a
)
75 struct timeval
msec2tv(int a
)
79 ret
.tv_sec
= a
/ 1000;
80 ret
.tv_usec
= (a
% 1000) * 1000;
85 int ospf_lsa_refresh_delay(struct ospf_lsa
*lsa
)
90 if (monotime_since(&lsa
->tv_orig
, &delta
)
91 < OSPF_MIN_LS_INTERVAL
* 1000LL) {
92 struct timeval minv
= msec2tv(OSPF_MIN_LS_INTERVAL
);
93 timersub(&minv
, &delta
, &minv
);
95 /* TBD: remove padding to full sec, return timeval instead */
96 delay
= minv
.tv_sec
+ !!minv
.tv_usec
;
98 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
100 "LSA[Type%d:%s]: Refresh timer delay %d seconds",
101 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
111 int get_age(struct ospf_lsa
*lsa
)
115 monotime_since(&lsa
->tv_recv
, &rel
);
116 return ntohs(lsa
->data
->ls_age
) + rel
.tv_sec
;
120 /* Fletcher Checksum -- Refer to RFC1008. */
122 /* All the offsets are zero-based. The offsets in the RFC1008 are
124 uint16_t ospf_lsa_checksum(struct lsa_header
*lsa
)
126 uint8_t *buffer
= (uint8_t *)&lsa
->options
;
127 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
129 /* Skip the AGE field */
130 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
132 /* Checksum offset starts from "options" field, not the beginning of the
133 lsa_header struct. The offset is 14, rather than 16. */
134 int checksum_offset
= (uint8_t *)&lsa
->checksum
- buffer
;
136 return fletcher_checksum(buffer
, len
, checksum_offset
);
139 int ospf_lsa_checksum_valid(struct lsa_header
*lsa
)
141 uint8_t *buffer
= (uint8_t *)&lsa
->options
;
142 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
144 /* Skip the AGE field */
145 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
147 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
152 /* Create OSPF LSA. */
153 struct ospf_lsa
*ospf_lsa_new()
155 struct ospf_lsa
*new;
157 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
161 new->retransmit_counter
= 0;
162 monotime(&new->tv_recv
);
163 new->tv_orig
= new->tv_recv
;
164 new->refresh_list
= -1;
165 new->vrf_id
= VRF_DEFAULT
;
170 /* Duplicate OSPF LSA. */
171 struct ospf_lsa
*ospf_lsa_dup(struct ospf_lsa
*lsa
)
173 struct ospf_lsa
*new;
178 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
180 memcpy(new, lsa
, sizeof(struct ospf_lsa
));
181 UNSET_FLAG(new->flags
, OSPF_LSA_DISCARD
);
183 new->retransmit_counter
= 0;
184 new->data
= ospf_lsa_data_dup(lsa
->data
);
186 /* kevinm: Clear the refresh_list, otherwise there are going
187 to be problems when we try to remove the LSA from the
188 queue (which it's not a member of.)
189 XXX: Should we add the LSA to the refresh_list queue? */
190 new->refresh_list
= -1;
192 if (IS_DEBUG_OSPF(lsa
, LSA
))
193 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa
,
200 void ospf_lsa_free(struct ospf_lsa
*lsa
)
202 assert(lsa
->lock
== 0);
204 if (IS_DEBUG_OSPF(lsa
, LSA
))
205 zlog_debug("LSA: freed %p", (void *)lsa
);
207 /* Delete LSA data. */
208 if (lsa
->data
!= NULL
)
209 ospf_lsa_data_free(lsa
->data
);
211 assert(lsa
->refresh_list
< 0);
213 memset(lsa
, 0, sizeof(struct ospf_lsa
));
214 XFREE(MTYPE_OSPF_LSA
, lsa
);
218 struct ospf_lsa
*ospf_lsa_lock(struct ospf_lsa
*lsa
)
225 void ospf_lsa_unlock(struct ospf_lsa
**lsa
)
227 /* This is sanity check. */
233 assert((*lsa
)->lock
>= 0);
235 if ((*lsa
)->lock
== 0) {
236 assert(CHECK_FLAG((*lsa
)->flags
, OSPF_LSA_DISCARD
));
242 /* Check discard flag. */
243 void ospf_lsa_discard(struct ospf_lsa
*lsa
)
245 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
246 SET_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
);
247 ospf_lsa_unlock(&lsa
);
251 /* Create LSA data. */
252 struct lsa_header
*ospf_lsa_data_new(size_t size
)
254 return XCALLOC(MTYPE_OSPF_LSA_DATA
, size
);
257 /* Duplicate LSA data. */
258 struct lsa_header
*ospf_lsa_data_dup(struct lsa_header
*lsah
)
260 struct lsa_header
*new;
262 new = ospf_lsa_data_new(ntohs(lsah
->length
));
263 memcpy(new, lsah
, ntohs(lsah
->length
));
269 void ospf_lsa_data_free(struct lsa_header
*lsah
)
271 if (IS_DEBUG_OSPF(lsa
, LSA
))
272 zlog_debug("LSA[Type%d:%s]: data freed %p", lsah
->type
,
273 inet_ntoa(lsah
->id
), (void *)lsah
);
275 XFREE(MTYPE_OSPF_LSA_DATA
, lsah
);
279 /* LSA general functions. */
281 const char *dump_lsa_key(struct ospf_lsa
*lsa
)
283 static char buf
[] = {"Type255,id(255.255.255.255),ar(255.255.255.255)"};
284 struct lsa_header
*lsah
;
286 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
) {
287 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
288 strlcpy(id
, inet_ntoa(lsah
->id
), sizeof(id
));
289 strlcpy(ar
, inet_ntoa(lsah
->adv_router
), sizeof(ar
));
291 sprintf(buf
, "Type%d,id(%s),ar(%s)", lsah
->type
, id
, ar
);
293 strlcpy(buf
, "NULL", sizeof(buf
));
298 uint32_t lsa_seqnum_increment(struct ospf_lsa
*lsa
)
302 seqnum
= ntohl(lsa
->data
->ls_seqnum
) + 1;
304 return htonl(seqnum
);
307 void lsa_header_set(struct stream
*s
, uint8_t options
, uint8_t type
,
308 struct in_addr id
, struct in_addr router_id
)
310 struct lsa_header
*lsah
;
312 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
314 lsah
->ls_age
= htons(OSPF_LSA_INITIAL_AGE
);
315 lsah
->options
= options
;
318 lsah
->adv_router
= router_id
;
319 lsah
->ls_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
321 stream_forward_endp(s
, OSPF_LSA_HEADER_SIZE
);
325 /* router-LSA related functions. */
326 /* Get router-LSA flags. */
327 static uint8_t router_lsa_flags(struct ospf_area
*area
)
331 flags
= area
->ospf
->flags
;
333 /* Set virtual link flag. */
334 if (ospf_full_virtual_nbrs(area
))
335 SET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
337 /* Just sanity check */
338 UNSET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
340 /* Set Shortcut ABR behabiour flag. */
341 UNSET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
342 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
343 if (!OSPF_IS_AREA_BACKBONE(area
))
344 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
345 && area
->ospf
->backbone
== NULL
)
346 || area
->shortcut_configured
347 == OSPF_SHORTCUT_ENABLE
)
348 SET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
350 /* ASBR can't exit in stub area. */
351 if (area
->external_routing
== OSPF_AREA_STUB
)
352 UNSET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
353 /* If ASBR set External flag */
354 else if (IS_OSPF_ASBR(area
->ospf
))
355 SET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
357 /* Set ABR dependent flags */
358 if (IS_OSPF_ABR(area
->ospf
)) {
359 SET_FLAG(flags
, ROUTER_LSA_BORDER
);
360 /* If Area is NSSA and we are both ABR and unconditional
362 * set Nt bit to inform other routers.
364 if ((area
->external_routing
== OSPF_AREA_NSSA
)
365 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
366 SET_FLAG(flags
, ROUTER_LSA_NT
);
371 /* Lookup neighbor other than myself.
372 And check neighbor count,
373 Point-to-Point link must have only 1 neighbor. */
374 struct ospf_neighbor
*ospf_nbr_lookup_ptop(struct ospf_interface
*oi
)
376 struct ospf_neighbor
*nbr
= NULL
;
377 struct route_node
*rn
;
379 /* Search neighbor, there must be one of two nbrs. */
380 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
381 if ((nbr
= rn
->info
))
382 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
383 &oi
->ospf
->router_id
))
384 if (nbr
->state
== NSM_Full
) {
385 route_unlock_node(rn
);
389 /* PtoP link must have only 1 neighbor. */
390 if (ospf_nbr_count(oi
, 0) > 1)
391 zlog_warn("Point-to-Point link has more than 1 neighobrs.");
396 /* Determine cost of link, taking RFC3137 stub-router support into
399 static uint16_t ospf_link_cost(struct ospf_interface
*oi
)
401 /* RFC3137 stub router support */
402 if (!CHECK_FLAG(oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
403 return oi
->output_cost
;
405 return OSPF_OUTPUT_COST_INFINITE
;
408 /* Set a link information. */
409 static char link_info_set(struct stream
*s
, struct in_addr id
,
410 struct in_addr data
, uint8_t type
, uint8_t tos
,
413 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
414 * vast majority of cases. Some rare routers with lots of links need
416 * we try accomodate those here.
418 if (STREAM_WRITEABLE(s
) < OSPF_ROUTER_LSA_LINK_SIZE
) {
419 size_t ret
= OSPF_MAX_LSA_SIZE
;
421 /* Can we enlarge the stream still? */
422 if (STREAM_SIZE(s
) == OSPF_MAX_LSA_SIZE
) {
423 /* we futz the size here for simplicity, really we need
426 * IP Header - (sizeof (struct ip))
427 * OSPF Header - OSPF_HEADER_SIZE
428 * LSA Header - OSPF_LSA_HEADER_SIZE
429 * MD5 auth data, if MD5 is configured -
430 * OSPF_AUTH_MD5_SIZE.
432 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
435 s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
438 if (ret
== OSPF_MAX_LSA_SIZE
) {
440 "%s: Out of space in LSA stream, left %zd, size %zd",
441 __func__
, STREAM_WRITEABLE(s
), STREAM_SIZE(s
));
446 /* TOS based routing is not supported. */
447 stream_put_ipv4(s
, id
.s_addr
); /* Link ID. */
448 stream_put_ipv4(s
, data
.s_addr
); /* Link Data. */
449 stream_putc(s
, type
); /* Link Type. */
450 stream_putc(s
, tos
); /* TOS = 0. */
451 stream_putw(s
, cost
); /* Link Cost. */
456 /* Describe Point-to-Point link (Section 12.4.1.1). */
457 static int lsa_link_ptop_set(struct stream
*s
, struct ospf_interface
*oi
)
460 struct ospf_neighbor
*nbr
;
461 struct in_addr id
, mask
, data
;
462 uint16_t cost
= ospf_link_cost(oi
);
464 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
465 zlog_debug("LSA[Type1]: Set link Point-to-Point");
467 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
468 if (nbr
->state
== NSM_Full
) {
469 if (CHECK_FLAG(oi
->connected
->flags
,
470 ZEBRA_IFA_UNNUMBERED
)) {
471 /* For unnumbered point-to-point networks, the
473 should specify the interface's MIB-II ifIndex
475 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
476 links
+= link_info_set(
477 s
, nbr
->router_id
, data
,
478 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
480 links
+= link_info_set(
482 oi
->address
->u
.prefix4
,
483 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
487 /* no need for a stub link for unnumbered interfaces */
488 if (!CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)) {
489 /* Regardless of the state of the neighboring router, we must
490 add a Type 3 link (stub network).
491 N.B. Options 1 & 2 share basically the same logic. */
492 masklen2ip(oi
->address
->prefixlen
, &mask
);
493 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
495 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
502 /* Describe Broadcast Link. */
503 static int lsa_link_broadcast_set(struct stream
*s
, struct ospf_interface
*oi
)
505 struct ospf_neighbor
*dr
;
506 struct in_addr id
, mask
;
507 uint16_t cost
= ospf_link_cost(oi
);
509 /* Describe Type 3 Link. */
510 if (oi
->state
== ISM_Waiting
) {
511 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
513 "LSA[Type1]: Interface %s is in state Waiting. "
514 "Adding stub interface",
516 masklen2ip(oi
->address
->prefixlen
, &mask
);
517 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
518 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
522 dr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &DR(oi
));
523 /* Describe Type 2 link. */
524 if (dr
&& (dr
->state
== NSM_Full
525 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
)))
526 && ospf_nbr_count(oi
, NSM_Full
) > 0) {
527 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
529 "LSA[Type1]: Interface %s has a DR. "
530 "Adding transit interface",
532 return link_info_set(s
, DR(oi
), oi
->address
->u
.prefix4
,
533 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
535 /* Describe type 3 link. */
537 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
539 "LSA[Type1]: Interface %s has no DR. "
540 "Adding stub interface",
542 masklen2ip(oi
->address
->prefixlen
, &mask
);
543 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
544 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
549 static int lsa_link_loopback_set(struct stream
*s
, struct ospf_interface
*oi
)
551 struct in_addr id
, mask
;
553 /* Describe Type 3 Link. */
554 if (oi
->state
!= ISM_Loopback
)
557 mask
.s_addr
= 0xffffffff;
558 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
559 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
562 /* Describe Virtual Link. */
563 static int lsa_link_virtuallink_set(struct stream
*s
, struct ospf_interface
*oi
)
565 struct ospf_neighbor
*nbr
;
566 uint16_t cost
= ospf_link_cost(oi
);
568 if (oi
->state
== ISM_PointToPoint
)
569 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
570 if (nbr
->state
== NSM_Full
) {
571 return link_info_set(s
, nbr
->router_id
,
572 oi
->address
->u
.prefix4
,
573 LSA_LINK_TYPE_VIRTUALLINK
,
580 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
582 /* this function add for support point-to-multipoint ,see rfc2328
584 /* from "edward rrr" <edward_rrr@hotmail.com>
585 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
586 static int lsa_link_ptomp_set(struct stream
*s
, struct ospf_interface
*oi
)
589 struct route_node
*rn
;
590 struct ospf_neighbor
*nbr
= NULL
;
591 struct in_addr id
, mask
;
592 uint16_t cost
= ospf_link_cost(oi
);
594 mask
.s_addr
= 0xffffffff;
595 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
596 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
598 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
599 zlog_debug("PointToMultipoint: running ptomultip_set");
601 /* Search neighbor, */
602 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
603 if ((nbr
= rn
->info
) != NULL
)
605 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
606 &oi
->ospf
->router_id
))
607 if (nbr
->state
== NSM_Full
)
610 links
+= link_info_set(
612 oi
->address
->u
.prefix4
,
613 LSA_LINK_TYPE_POINTOPOINT
, 0,
615 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
617 "PointToMultipoint: set link to %s",
626 /* Set router-LSA link information. */
627 static int router_lsa_link_set(struct stream
*s
, struct ospf_area
*area
)
629 struct listnode
*node
;
630 struct ospf_interface
*oi
;
633 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
634 struct interface
*ifp
= oi
->ifp
;
636 /* Check interface is up, OSPF is enable. */
637 if (if_is_operative(ifp
)) {
638 if (oi
->state
!= ISM_Down
) {
639 oi
->lsa_pos_beg
= links
;
640 /* Describe each link. */
642 case OSPF_IFTYPE_POINTOPOINT
:
643 links
+= lsa_link_ptop_set(s
, oi
);
645 case OSPF_IFTYPE_BROADCAST
:
646 links
+= lsa_link_broadcast_set(s
, oi
);
648 case OSPF_IFTYPE_NBMA
:
649 links
+= lsa_link_nbma_set(s
, oi
);
651 case OSPF_IFTYPE_POINTOMULTIPOINT
:
652 links
+= lsa_link_ptomp_set(s
, oi
);
654 case OSPF_IFTYPE_VIRTUALLINK
:
656 lsa_link_virtuallink_set(s
, oi
);
658 case OSPF_IFTYPE_LOOPBACK
:
659 links
+= lsa_link_loopback_set(s
, oi
);
661 oi
->lsa_pos_end
= links
;
669 /* Set router-LSA body. */
670 static void ospf_router_lsa_body_set(struct stream
*s
, struct ospf_area
*area
)
676 stream_putc(s
, router_lsa_flags(area
));
678 /* Set Zero fields. */
681 /* Keep pointer to # links. */
682 putp
= stream_get_endp(s
);
687 /* Set all link information. */
688 cnt
= router_lsa_link_set(s
, area
);
690 /* Set # of links here. */
691 stream_putw_at(s
, putp
, cnt
);
694 static int ospf_stub_router_timer(struct thread
*t
)
696 struct ospf_area
*area
= THREAD_ARG(t
);
698 area
->t_stub_router
= NULL
;
700 SET_FLAG(area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
702 /* clear stub route state and generate router-lsa refresh, don't
703 * clobber an administratively set stub-router state though.
705 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
708 UNSET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
710 ospf_router_lsa_update_area(area
);
715 static void ospf_stub_router_check(struct ospf_area
*area
)
717 /* area must either be administratively configured to be stub
718 * or startup-time stub-router must be configured and we must in a
722 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
)) {
723 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
727 /* not admin-stubbed, check whether startup stubbing is configured and
728 * whether it's not been done yet
730 if (CHECK_FLAG(area
->stub_router_state
,
731 OSPF_AREA_WAS_START_STUB_ROUTED
))
734 if (area
->ospf
->stub_router_startup_time
735 == OSPF_STUB_ROUTER_UNCONFIGURED
) {
736 /* stub-router is hence done forever for this area, even if
738 * tries configure it (take effect next restart).
740 SET_FLAG(area
->stub_router_state
,
741 OSPF_AREA_WAS_START_STUB_ROUTED
);
745 /* startup stub-router configured and not yet done */
746 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
748 OSPF_AREA_TIMER_ON(area
->t_stub_router
, ospf_stub_router_timer
,
749 area
->ospf
->stub_router_startup_time
);
752 /* Create new router-LSA. */
753 static struct ospf_lsa
*ospf_router_lsa_new(struct ospf_area
*area
)
755 struct ospf
*ospf
= area
->ospf
;
757 struct lsa_header
*lsah
;
758 struct ospf_lsa
*new;
761 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
762 zlog_debug("LSA[Type1]: Create router-LSA instance");
764 /* check whether stub-router is desired, and if this is the first
767 ospf_stub_router_check(area
);
769 /* Create a stream for LSA. */
770 s
= stream_new(OSPF_MAX_LSA_SIZE
);
771 /* Set LSA common header fields. */
772 lsa_header_set(s
, LSA_OPTIONS_GET(area
) | LSA_OPTIONS_NSSA_GET(area
),
773 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
775 /* Set router-LSA body fields. */
776 ospf_router_lsa_body_set(s
, area
);
779 length
= stream_get_endp(s
);
780 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
781 lsah
->length
= htons(length
);
783 /* Now, create OSPF LSA instance. */
784 if ((new = ospf_lsa_new()) == NULL
) {
785 zlog_err("%s: Unable to create new lsa", __func__
);
790 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
791 new->vrf_id
= area
->ospf
->vrf_id
;
793 /* Copy LSA data to store, discard stream. */
794 new->data
= ospf_lsa_data_new(length
);
795 memcpy(new->data
, lsah
, length
);
801 /* Originate Router-LSA. */
802 static struct ospf_lsa
*ospf_router_lsa_originate(struct ospf_area
*area
)
804 struct ospf_lsa
*new;
806 /* Create new router-LSA instance. */
807 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
808 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
813 if (new->data
->adv_router
.s_addr
== 0) {
814 if (IS_DEBUG_OSPF_EVENT
)
815 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
816 ospf_lsa_discard(new);
820 /* Install LSA to LSDB. */
821 new = ospf_lsa_install(area
->ospf
, NULL
, new);
823 /* Update LSA origination count. */
824 area
->ospf
->lsa_originate_count
++;
826 /* Flooding new LSA through area. */
827 ospf_flood_through_area(area
, NULL
, new);
829 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
830 zlog_debug("LSA[Type%d:%s]: Originate router-LSA %p",
831 new->data
->type
, inet_ntoa(new->data
->id
),
833 ospf_lsa_header_dump(new->data
);
839 /* Refresh router-LSA. */
840 static struct ospf_lsa
*ospf_router_lsa_refresh(struct ospf_lsa
*lsa
)
842 struct ospf_area
*area
= lsa
->area
;
843 struct ospf_lsa
*new;
848 /* Delete LSA from neighbor retransmit-list. */
849 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
851 /* Unregister LSA from refresh-list */
852 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
854 /* Create new router-LSA instance. */
855 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
856 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
860 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
862 ospf_lsa_install(area
->ospf
, NULL
, new);
864 /* Flood LSA through area. */
865 ospf_flood_through_area(area
, NULL
, new);
868 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
869 zlog_debug("LSA[Type%d:%s]: router-LSA refresh",
870 new->data
->type
, inet_ntoa(new->data
->id
));
871 ospf_lsa_header_dump(new->data
);
877 int ospf_router_lsa_update_area(struct ospf_area
*area
)
879 if (IS_DEBUG_OSPF_EVENT
)
880 zlog_debug("[router-LSA]: (router-LSA area update)");
882 /* Now refresh router-LSA. */
883 if (area
->router_lsa_self
)
884 ospf_lsa_refresh(area
->ospf
, area
->router_lsa_self
);
885 /* Newly originate router-LSA. */
887 ospf_router_lsa_originate(area
);
892 int ospf_router_lsa_update(struct ospf
*ospf
)
894 struct listnode
*node
, *nnode
;
895 struct ospf_area
*area
;
897 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
898 zlog_debug("Timer[router-LSA Update]: (timer expire)");
900 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
901 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
902 struct router_lsa
*rl
;
903 const char *area_str
;
905 /* Keep Area ID string. */
906 area_str
= AREA_NAME(area
);
908 /* If LSA not exist in this Area, originate new. */
910 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
912 "LSA[Type1]: Create router-LSA for Area %s",
915 ospf_router_lsa_originate(area
);
917 /* If router-ID is changed, Link ID must change.
918 First flush old LSA, then originate new. */
919 else if (!IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
)) {
920 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
922 "LSA[Type%d:%s]: Refresh router-LSA for Area %s",
924 inet_ntoa(lsa
->data
->id
), area_str
);
925 ospf_refresher_unregister_lsa(ospf
, lsa
);
926 ospf_lsa_flush_area(lsa
, area
);
927 ospf_lsa_unlock(&area
->router_lsa_self
);
928 area
->router_lsa_self
= NULL
;
930 /* Refresh router-LSA, (not install) and flood through
932 ospf_router_lsa_update_area(area
);
934 rl
= (struct router_lsa
*)lsa
->data
;
935 /* Refresh router-LSA, (not install) and flood through
937 if (rl
->flags
!= ospf
->flags
)
938 ospf_router_lsa_update_area(area
);
946 /* network-LSA related functions. */
947 /* Originate Network-LSA. */
948 static void ospf_network_lsa_body_set(struct stream
*s
,
949 struct ospf_interface
*oi
)
952 struct route_node
*rn
;
953 struct ospf_neighbor
*nbr
;
955 masklen2ip(oi
->address
->prefixlen
, &mask
);
956 stream_put_ipv4(s
, mask
.s_addr
);
958 /* The network-LSA lists those routers that are fully adjacent to
959 the Designated Router; each fully adjacent router is identified by
960 its OSPF Router ID. The Designated Router includes itself in this
961 list. RFC2328, Section 12.4.2 */
963 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
964 if ((nbr
= rn
->info
) != NULL
)
965 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
966 stream_put_ipv4(s
, nbr
->router_id
.s_addr
);
969 static struct ospf_lsa
*ospf_network_lsa_new(struct ospf_interface
*oi
)
972 struct ospf_lsa
*new;
973 struct lsa_header
*lsah
;
974 struct ospf_if_params
*oip
;
977 /* If there are no neighbours on this network (the net is stub),
978 the router does not originate network-LSA (see RFC 12.4.2) */
979 if (oi
->full_nbrs
== 0)
982 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
983 zlog_debug("LSA[Type2]: Create network-LSA instance");
985 /* Create new stream for LSA. */
986 s
= stream_new(OSPF_MAX_LSA_SIZE
);
987 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
989 lsa_header_set(s
, (OPTIONS(oi
) | LSA_OPTIONS_GET(oi
->area
)),
990 OSPF_NETWORK_LSA
, DR(oi
), oi
->ospf
->router_id
);
992 /* Set network-LSA body fields. */
993 ospf_network_lsa_body_set(s
, oi
);
996 length
= stream_get_endp(s
);
997 lsah
->length
= htons(length
);
999 /* Create OSPF LSA instance. */
1000 if ((new = ospf_lsa_new()) == NULL
) {
1001 zlog_err("%s: ospf_lsa_new returned NULL", __func__
);
1005 new->area
= oi
->area
;
1006 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1007 new->vrf_id
= oi
->ospf
->vrf_id
;
1009 /* Copy LSA to store. */
1010 new->data
= ospf_lsa_data_new(length
);
1011 memcpy(new->data
, lsah
, length
);
1014 /* Remember prior network LSA sequence numbers, even if we stop
1015 * originating one for this oi, to try avoid re-originating LSAs with a
1016 * prior sequence number, and thus speed up adjency forming &
1019 if ((oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
))) {
1020 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1021 new->data
->ls_seqnum
= lsa_seqnum_increment(new);
1023 oip
= ospf_get_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1024 ospf_if_update_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1026 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1031 /* Originate network-LSA. */
1032 void ospf_network_lsa_update(struct ospf_interface
*oi
)
1034 struct ospf_lsa
*new;
1036 if (oi
->network_lsa_self
!= NULL
) {
1037 ospf_lsa_refresh(oi
->ospf
, oi
->network_lsa_self
);
1041 /* Create new network-LSA instance. */
1042 new = ospf_network_lsa_new(oi
);
1046 /* Install LSA to LSDB. */
1047 new = ospf_lsa_install(oi
->ospf
, oi
, new);
1049 /* Update LSA origination count. */
1050 oi
->ospf
->lsa_originate_count
++;
1052 /* Flooding new LSA through area. */
1053 ospf_flood_through_area(oi
->area
, NULL
, new);
1055 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1056 zlog_debug("LSA[Type%d:%s]: Originate network-LSA %p",
1057 new->data
->type
, inet_ntoa(new->data
->id
),
1059 ospf_lsa_header_dump(new->data
);
1065 static struct ospf_lsa
*ospf_network_lsa_refresh(struct ospf_lsa
*lsa
)
1067 struct ospf_area
*area
= lsa
->area
;
1068 struct ospf_lsa
*new, *new2
;
1069 struct ospf_if_params
*oip
;
1070 struct ospf_interface
*oi
;
1074 /* Retrieve the oi for the network LSA */
1075 oi
= ospf_if_lookup_by_local_addr(area
->ospf
, NULL
, lsa
->data
->id
);
1077 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1079 "LSA[Type%d:%s]: network-LSA refresh: "
1080 "no oi found, ick, ignoring.",
1081 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
1082 ospf_lsa_header_dump(lsa
->data
);
1086 /* Delete LSA from neighbor retransmit-list. */
1087 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
1089 /* Unregister LSA from refresh-list */
1090 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
1092 /* Create new network-LSA instance. */
1093 new = ospf_network_lsa_new(oi
);
1097 oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1098 assert(oip
!= NULL
);
1099 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
=
1100 lsa_seqnum_increment(lsa
);
1102 new2
= ospf_lsa_install(area
->ospf
, oi
, new);
1104 assert(new2
== new);
1106 /* Flood LSA through aera. */
1107 ospf_flood_through_area(area
, NULL
, new);
1109 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1110 zlog_debug("LSA[Type%d:%s]: network-LSA refresh",
1111 new->data
->type
, inet_ntoa(new->data
->id
));
1112 ospf_lsa_header_dump(new->data
);
1118 static void stream_put_ospf_metric(struct stream
*s
, uint32_t metric_value
)
1123 /* Put 0 metric. TOS metric is not supported. */
1124 metric
= htonl(metric_value
);
1125 mp
= (char *)&metric
;
1127 stream_put(s
, mp
, 3);
1130 /* summary-LSA related functions. */
1131 static void ospf_summary_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1134 struct in_addr mask
;
1136 masklen2ip(p
->prefixlen
, &mask
);
1138 /* Put Network Mask. */
1139 stream_put_ipv4(s
, mask
.s_addr
);
1142 stream_putc(s
, (uint8_t)0);
1145 stream_put_ospf_metric(s
, metric
);
1148 static struct ospf_lsa
*ospf_summary_lsa_new(struct ospf_area
*area
,
1149 struct prefix
*p
, uint32_t metric
,
1153 struct ospf_lsa
*new;
1154 struct lsa_header
*lsah
;
1157 if (id
.s_addr
== 0xffffffff) {
1158 /* Maybe Link State ID not available. */
1159 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1161 "LSA[Type%d]: Link ID not available, can't originate",
1166 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1167 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1169 /* Create new stream for LSA. */
1170 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1171 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1173 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_SUMMARY_LSA
, id
,
1174 area
->ospf
->router_id
);
1176 /* Set summary-LSA body fields. */
1177 ospf_summary_lsa_body_set(s
, p
, metric
);
1180 length
= stream_get_endp(s
);
1181 lsah
->length
= htons(length
);
1183 /* Create OSPF LSA instance. */
1184 new = ospf_lsa_new();
1186 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1187 new->vrf_id
= area
->ospf
->vrf_id
;
1189 /* Copy LSA to store. */
1190 new->data
= ospf_lsa_data_new(length
);
1191 memcpy(new->data
, lsah
, length
);
1197 /* Originate Summary-LSA. */
1198 struct ospf_lsa
*ospf_summary_lsa_originate(struct prefix_ipv4
*p
,
1200 struct ospf_area
*area
)
1202 struct ospf_lsa
*new;
1205 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
);
1207 if (id
.s_addr
== 0xffffffff) {
1208 /* Maybe Link State ID not available. */
1209 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1211 "LSA[Type%d]: Link ID not available, can't originate",
1216 /* Create new summary-LSA instance. */
1217 if (!(new = ospf_summary_lsa_new(area
, (struct prefix
*)p
, metric
, id
)))
1220 /* Instlal LSA to LSDB. */
1221 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1223 /* Update LSA origination count. */
1224 area
->ospf
->lsa_originate_count
++;
1226 /* Flooding new LSA through area. */
1227 ospf_flood_through_area(area
, NULL
, new);
1229 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1230 zlog_debug("LSA[Type%d:%s]: Originate summary-LSA %p",
1231 new->data
->type
, inet_ntoa(new->data
->id
),
1233 ospf_lsa_header_dump(new->data
);
1239 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
1240 struct ospf_lsa
*lsa
)
1242 struct ospf_lsa
*new;
1243 struct summary_lsa
*sl
;
1249 sl
= (struct summary_lsa
*)lsa
->data
;
1250 p
.prefixlen
= ip_masklen(sl
->mask
);
1251 new = ospf_summary_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1257 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1259 ospf_lsa_install(ospf
, NULL
, new);
1261 /* Flood LSA through AS. */
1262 ospf_flood_through_area(new->area
, NULL
, new);
1264 /* Debug logging. */
1265 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1266 zlog_debug("LSA[Type%d:%s]: summary-LSA refresh",
1267 new->data
->type
, inet_ntoa(new->data
->id
));
1268 ospf_lsa_header_dump(new->data
);
1275 /* summary-ASBR-LSA related functions. */
1276 static void ospf_summary_asbr_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1279 /* Put Network Mask. */
1280 stream_put_ipv4(s
, (uint32_t)0);
1283 stream_putc(s
, (uint8_t)0);
1286 stream_put_ospf_metric(s
, metric
);
1289 static struct ospf_lsa
*ospf_summary_asbr_lsa_new(struct ospf_area
*area
,
1295 struct ospf_lsa
*new;
1296 struct lsa_header
*lsah
;
1299 if (id
.s_addr
== 0xffffffff) {
1300 /* Maybe Link State ID not available. */
1301 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1303 "LSA[Type%d]: Link ID not available, can't originate",
1304 OSPF_ASBR_SUMMARY_LSA
);
1308 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1309 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1311 /* Create new stream for LSA. */
1312 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1313 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1315 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_ASBR_SUMMARY_LSA
, id
,
1316 area
->ospf
->router_id
);
1318 /* Set summary-LSA body fields. */
1319 ospf_summary_asbr_lsa_body_set(s
, p
, metric
);
1322 length
= stream_get_endp(s
);
1323 lsah
->length
= htons(length
);
1325 /* Create OSPF LSA instance. */
1326 new = ospf_lsa_new();
1328 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1329 new->vrf_id
= area
->ospf
->vrf_id
;
1331 /* Copy LSA to store. */
1332 new->data
= ospf_lsa_data_new(length
);
1333 memcpy(new->data
, lsah
, length
);
1339 /* Originate summary-ASBR-LSA. */
1340 struct ospf_lsa
*ospf_summary_asbr_lsa_originate(struct prefix_ipv4
*p
,
1342 struct ospf_area
*area
)
1344 struct ospf_lsa
*new;
1347 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_ASBR_SUMMARY_LSA
,
1350 if (id
.s_addr
== 0xffffffff) {
1351 /* Maybe Link State ID not available. */
1352 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1354 "LSA[Type%d]: Link ID not available, can't originate",
1355 OSPF_ASBR_SUMMARY_LSA
);
1359 /* Create new summary-LSA instance. */
1360 new = ospf_summary_asbr_lsa_new(area
, (struct prefix
*)p
, metric
, id
);
1364 /* Install LSA to LSDB. */
1365 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1367 /* Update LSA origination count. */
1368 area
->ospf
->lsa_originate_count
++;
1370 /* Flooding new LSA through area. */
1371 ospf_flood_through_area(area
, NULL
, new);
1373 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1374 zlog_debug("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1375 new->data
->type
, inet_ntoa(new->data
->id
),
1377 ospf_lsa_header_dump(new->data
);
1383 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
1384 struct ospf_lsa
*lsa
)
1386 struct ospf_lsa
*new;
1387 struct summary_lsa
*sl
;
1393 sl
= (struct summary_lsa
*)lsa
->data
;
1394 p
.prefixlen
= ip_masklen(sl
->mask
);
1395 new = ospf_summary_asbr_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1400 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1402 ospf_lsa_install(ospf
, NULL
, new);
1404 /* Flood LSA through area. */
1405 ospf_flood_through_area(new->area
, NULL
, new);
1407 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1408 zlog_debug("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1409 new->data
->type
, inet_ntoa(new->data
->id
));
1410 ospf_lsa_header_dump(new->data
);
1416 /* AS-external-LSA related functions. */
1418 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1419 is connected, else 0*/
1420 static struct in_addr
ospf_external_lsa_nexthop_get(struct ospf
*ospf
,
1421 struct in_addr nexthop
)
1425 struct listnode
*node
;
1426 struct ospf_interface
*oi
;
1430 if (!nexthop
.s_addr
)
1433 /* Check whether nexthop is covered by OSPF network. */
1434 nh
.family
= AF_INET
;
1435 nh
.u
.prefix4
= nexthop
;
1436 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1438 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1439 * better to make use of the per-ifp table of ois.
1441 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1442 if (if_is_operative(oi
->ifp
))
1443 if (oi
->address
->family
== AF_INET
)
1444 if (prefix_match(oi
->address
, &nh
))
1450 /* NSSA-external-LSA related functions. */
1452 /* Get 1st IP connection for Forward Addr */
1454 struct in_addr
ospf_get_ip_from_ifp(struct ospf_interface
*oi
)
1460 if (if_is_operative(oi
->ifp
))
1461 return oi
->address
->u
.prefix4
;
1466 /* Get 1st IP connection for Forward Addr */
1467 struct in_addr
ospf_get_nssa_ip(struct ospf_area
*area
)
1470 struct in_addr best_default
;
1471 struct listnode
*node
;
1472 struct ospf_interface
*oi
;
1475 best_default
.s_addr
= 0;
1477 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->oiflist
, node
, oi
)) {
1478 if (if_is_operative(oi
->ifp
))
1479 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1481 && oi
->address
->family
== AF_INET
) {
1482 if (best_default
.s_addr
== 0)
1484 oi
->address
->u
.prefix4
;
1485 if (oi
->area
== area
)
1486 return oi
->address
->u
.prefix4
;
1489 if (best_default
.s_addr
!= 0)
1490 return best_default
;
1492 if (best_default
.s_addr
!= 0)
1493 return best_default
;
1498 #define DEFAULT_DEFAULT_METRIC 20
1499 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1500 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1502 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1504 int metric_type(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1506 struct ospf_redist
*red
;
1508 red
= ospf_redist_lookup(ospf
, src
, instance
);
1510 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
1511 : red
->dmetric
.type
);
1514 int metric_value(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1516 struct ospf_redist
*red
;
1518 red
= ospf_redist_lookup(ospf
, src
, instance
);
1519 if (!red
|| red
->dmetric
.value
< 0) {
1520 if (src
== DEFAULT_ROUTE
) {
1521 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1522 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1524 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1525 } else if (ospf
->default_metric
< 0)
1526 return DEFAULT_DEFAULT_METRIC
;
1528 return ospf
->default_metric
;
1531 return red
->dmetric
.value
;
1534 /* Set AS-external-LSA body. */
1535 static void ospf_external_lsa_body_set(struct stream
*s
,
1536 struct external_info
*ei
,
1539 struct prefix_ipv4
*p
= &ei
->p
;
1540 struct in_addr mask
, fwd_addr
;
1544 unsigned short instance
;
1546 /* Put Network Mask. */
1547 masklen2ip(p
->prefixlen
, &mask
);
1548 stream_put_ipv4(s
, mask
.s_addr
);
1550 /* If prefix is default, specify DEFAULT_ROUTE. */
1551 type
= is_prefix_default(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1552 instance
= is_prefix_default(&ei
->p
) ? 0 : ei
->instance
;
1554 mtype
= (ROUTEMAP_METRIC_TYPE(ei
) != -1)
1555 ? ROUTEMAP_METRIC_TYPE(ei
)
1556 : metric_type(ospf
, type
, instance
);
1558 mvalue
= (ROUTEMAP_METRIC(ei
) != -1)
1559 ? ROUTEMAP_METRIC(ei
)
1560 : metric_value(ospf
, type
, instance
);
1562 /* Put type of external metric. */
1563 stream_putc(s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1565 /* Put 0 metric. TOS metric is not supported. */
1566 stream_put_ospf_metric(s
, mvalue
);
1568 /* Get forwarding address to nexthop if on the Connection List, else 0.
1570 fwd_addr
= ospf_external_lsa_nexthop_get(ospf
, ei
->nexthop
);
1572 /* Put forwarding address. */
1573 stream_put_ipv4(s
, fwd_addr
.s_addr
);
1576 stream_putl(s
, ei
->tag
);
1579 /* Create new external-LSA. */
1580 static struct ospf_lsa
*ospf_external_lsa_new(struct ospf
*ospf
,
1581 struct external_info
*ei
,
1582 struct in_addr
*old_id
)
1585 struct lsa_header
*lsah
;
1586 struct ospf_lsa
*new;
1591 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1593 "LSA[Type5]: External info is NULL, can't originate");
1597 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1598 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1600 /* If old Link State ID is specified, refresh LSA with same ID. */
1603 /* Get Link State with unique ID. */
1605 id
= ospf_lsa_unique_id(ospf
, ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
,
1607 if (id
.s_addr
== 0xffffffff) {
1608 /* Maybe Link State ID not available. */
1609 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1611 "LSA[Type5]: Link ID not available, can't originate");
1616 /* Create new stream for LSA. */
1617 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1618 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1620 /* Set LSA common header fields. */
1621 lsa_header_set(s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
, id
,
1624 /* Set AS-external-LSA body fields. */
1625 ospf_external_lsa_body_set(s
, ei
, ospf
);
1628 length
= stream_get_endp(s
);
1629 lsah
->length
= htons(length
);
1631 /* Now, create OSPF LSA instance. */
1632 new = ospf_lsa_new();
1634 SET_FLAG(new->flags
,
1635 OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1636 new->vrf_id
= ospf
->vrf_id
;
1638 /* Copy LSA data to store, discard stream. */
1639 new->data
= ospf_lsa_data_new(length
);
1640 memcpy(new->data
, lsah
, length
);
1647 static void ospf_install_flood_nssa(struct ospf
*ospf
, struct ospf_lsa
*lsa
,
1648 struct external_info
*ei
)
1650 struct ospf_lsa
*new;
1651 struct as_external_lsa
*extlsa
;
1652 struct ospf_area
*area
;
1653 struct listnode
*node
, *nnode
;
1655 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1656 * which originated from an NSSA area. In which case it should not be
1657 * flooded back to NSSA areas.
1659 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1662 /* NSSA Originate or Refresh (If anyNSSA)
1664 LSA is self-originated. And just installed as Type-5.
1665 Additionally, install as Type-7 LSDB for every attached NSSA.
1667 P-Bit controls which ABR performs translation to outside world; If
1668 we are an ABR....do not set the P-bit, because we send the Type-5,
1669 not as the ABR Translator, but as the ASBR owner within the AS!
1671 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1672 elected ABR Translator will see the P-bit, Translate, and re-flood.
1674 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1675 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1677 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
1678 /* Don't install Type-7 LSA's into nonNSSA area */
1679 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1682 /* make lsa duplicate, lock=1 */
1683 new = ospf_lsa_dup(lsa
);
1685 new->data
->type
= OSPF_AS_NSSA_LSA
;
1687 /* set P-bit if not ABR */
1688 if (!IS_OSPF_ABR(ospf
)) {
1689 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1691 /* set non-zero FWD ADDR
1693 draft-ietf-ospf-nssa-update-09.txt
1695 if the network between the NSSA AS boundary router and
1697 adjacent AS is advertised into OSPF as an internal OSPF
1699 the forwarding address should be the next op address as
1701 currently done with type-5 LSAs. If the intervening
1703 not adversited into OSPF as an internal OSPF route and
1705 type-7 LSA's P-bit is set a forwarding address should be
1706 selected from one of the router's active OSPF interface
1708 which belong to the NSSA. If no such addresses exist,
1710 no type-7 LSA's with the P-bit set should originate from
1714 /* kevinm: not updating lsa anymore, just new */
1715 extlsa
= (struct as_external_lsa
*)(new->data
);
1717 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0)
1718 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(
1719 area
); /* this NSSA area in ifp */
1721 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0) {
1722 if (IS_DEBUG_OSPF_NSSA
)
1724 "LSA[Type-7]: Could not build FWD-ADDR");
1725 ospf_lsa_discard(new);
1730 /* install also as Type-7 */
1731 ospf_lsa_install(ospf
, NULL
,
1732 new); /* Remove Old, Lock New = 2 */
1734 /* will send each copy, lock=2+n */
1735 ospf_flood_through_as(
1736 ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1740 static struct ospf_lsa
*ospf_lsa_translated_nssa_new(struct ospf
*ospf
,
1741 struct ospf_lsa
*type7
)
1744 struct ospf_lsa
*new;
1745 struct as_external_lsa
*ext
, *extnew
;
1746 struct external_info ei
;
1748 ext
= (struct as_external_lsa
*)(type7
->data
);
1750 /* need external_info struct, fill in bare minimum */
1751 ei
.p
.family
= AF_INET
;
1752 ei
.p
.prefix
= type7
->data
->id
;
1753 ei
.p
.prefixlen
= ip_masklen(ext
->mask
);
1754 ei
.type
= ZEBRA_ROUTE_OSPF
;
1755 ei
.nexthop
= ext
->header
.adv_router
;
1756 ei
.route_map_set
.metric
= -1;
1757 ei
.route_map_set
.metric_type
= -1;
1761 if ((new = ospf_external_lsa_new(ospf
, &ei
, &type7
->data
->id
))
1763 if (IS_DEBUG_OSPF_NSSA
)
1765 "ospf_nssa_translate_originate(): Could not originate "
1766 "Translated Type-5 for %s",
1767 inet_ntoa(ei
.p
.prefix
));
1771 extnew
= (struct as_external_lsa
*)(new->data
);
1773 /* copy over Type-7 data to new */
1774 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1775 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1776 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1777 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1779 /* add translated flag, checksum and lock new lsa */
1780 SET_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1781 new = ospf_lsa_lock(new);
1786 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1787 struct ospf_lsa
*ospf_translated_nssa_originate(struct ospf
*ospf
,
1788 struct ospf_lsa
*type7
)
1790 struct ospf_lsa
*new;
1791 struct as_external_lsa
*extnew
;
1793 /* we cant use ospf_external_lsa_originate() as we need to set
1794 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1797 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1798 if (IS_DEBUG_OSPF_NSSA
)
1800 "ospf_translated_nssa_originate(): Could not translate "
1801 "Type-7, Id %s, to Type-5",
1802 inet_ntoa(type7
->data
->id
));
1806 extnew
= (struct as_external_lsa
*)new;
1808 if (IS_DEBUG_OSPF_NSSA
) {
1810 "ospf_translated_nssa_originate(): "
1811 "translated Type 7, installed:");
1812 ospf_lsa_header_dump(new->data
);
1813 zlog_debug(" Network mask: %d", ip_masklen(extnew
->mask
));
1814 zlog_debug(" Forward addr: %s",
1815 inet_ntoa(extnew
->e
[0].fwd_addr
));
1818 if ((new = ospf_lsa_install(ospf
, NULL
, new)) == NULL
) {
1819 if (IS_DEBUG_OSPF_NSSA
)
1821 "ospf_lsa_translated_nssa_originate(): "
1822 "Could not install LSA "
1824 inet_ntoa(type7
->data
->id
));
1828 ospf
->lsa_originate_count
++;
1829 ospf_flood_through_as(ospf
, NULL
, new);
1834 /* Refresh Translated from NSSA AS-external-LSA. */
1835 struct ospf_lsa
*ospf_translated_nssa_refresh(struct ospf
*ospf
,
1836 struct ospf_lsa
*type7
,
1837 struct ospf_lsa
*type5
)
1839 struct ospf_lsa
*new = NULL
;
1841 /* Sanity checks. */
1842 assert(type7
|| type5
);
1843 if (!(type7
|| type5
))
1846 assert(type7
->data
);
1848 assert(type5
->data
);
1849 assert(ospf
->anyNSSA
);
1851 /* get required data according to what has been given */
1852 if (type7
&& type5
== NULL
) {
1853 /* find the translated Type-5 for this Type-7 */
1854 struct as_external_lsa
*ext
=
1855 (struct as_external_lsa
*)(type7
->data
);
1856 struct prefix_ipv4 p
= {
1857 .prefix
= type7
->data
->id
,
1858 .prefixlen
= ip_masklen(ext
->mask
),
1862 type5
= ospf_external_info_find_lsa(ospf
, &p
);
1863 } else if (type5
&& type7
== NULL
) {
1864 /* find the type-7 from which supplied type-5 was translated,
1865 * ie find first type-7 with same LSA Id.
1867 struct listnode
*ln
, *lnn
;
1868 struct route_node
*rn
;
1869 struct ospf_lsa
*lsa
;
1870 struct ospf_area
*area
;
1872 for (ALL_LIST_ELEMENTS(ospf
->areas
, ln
, lnn
, area
)) {
1873 if (area
->external_routing
!= OSPF_AREA_NSSA
&& !type7
)
1876 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
) {
1877 if (lsa
->data
->id
.s_addr
1878 == type5
->data
->id
.s_addr
) {
1886 /* do we have type7? */
1888 if (IS_DEBUG_OSPF_NSSA
)
1890 "ospf_translated_nssa_refresh(): no Type-7 found for "
1892 inet_ntoa(type5
->data
->id
));
1896 /* do we have valid translated type5? */
1897 if (type5
== NULL
|| !CHECK_FLAG(type5
->flags
, OSPF_LSA_LOCAL_XLT
)) {
1898 if (IS_DEBUG_OSPF_NSSA
)
1900 "ospf_translated_nssa_refresh(): No translated Type-5 "
1901 "found for Type-7 with Id %s",
1902 inet_ntoa(type7
->data
->id
));
1906 /* Delete LSA from neighbor retransmit-list. */
1907 ospf_ls_retransmit_delete_nbr_as(ospf
, type5
);
1909 /* create new translated LSA */
1910 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1911 if (IS_DEBUG_OSPF_NSSA
)
1913 "ospf_translated_nssa_refresh(): Could not translate "
1914 "Type-7 for %s to Type-5",
1915 inet_ntoa(type7
->data
->id
));
1919 if (!(new = ospf_lsa_install(ospf
, NULL
, new))) {
1920 if (IS_DEBUG_OSPF_NSSA
)
1922 "ospf_translated_nssa_refresh(): Could not install "
1923 "translated LSA, Id %s",
1924 inet_ntoa(type7
->data
->id
));
1928 /* Flood LSA through area. */
1929 ospf_flood_through_as(ospf
, NULL
, new);
1934 int is_prefix_default(struct prefix_ipv4
*p
)
1936 struct prefix_ipv4 q
;
1939 q
.prefix
.s_addr
= 0;
1942 return prefix_same((struct prefix
*)p
, (struct prefix
*)&q
);
1945 /* Originate an AS-external-LSA, install and flood. */
1946 struct ospf_lsa
*ospf_external_lsa_originate(struct ospf
*ospf
,
1947 struct external_info
*ei
)
1949 struct ospf_lsa
*new;
1951 /* Added for NSSA project....
1953 External LSAs are originated in ASBRs as usual, but for NSSA
1955 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1956 every area. The Type-7's are flooded to every IR and every ABR; We
1957 install the Type-5 LSDB so that the normal "refresh" code operates
1958 as usual, and flag them as not used during ASE calculations. The
1959 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
1960 Address of non-zero.
1962 If an ABR is the elected NSSA translator, following SPF and during
1963 the ABR task it will translate all the scanned Type-7's, with P-bit
1964 ON and not-self generated, and translate to Type-5's throughout the
1967 A difference in operation depends whether this ASBR is an ABR
1968 or not. If not an ABR, the P-bit is ON, to indicate that any
1969 elected NSSA-ABR can perform its translation.
1971 If an ABR, the P-bit is OFF; No ABR will perform translation and
1972 this ASBR will flood the Type-5 LSA as usual.
1974 For the case where this ASBR is not an ABR, the ASE calculations
1975 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
1976 demonstrate to the user that there are LSA's that belong to any
1979 Finally, it just so happens that when the ABR is translating every
1980 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
1981 approved Type-5 (translated from Type-7); at the end of translation
1982 if any Translated Type-5's remain unapproved, then they must be
1983 flushed from the AS.
1987 /* Check the AS-external-LSA should be originated. */
1988 if (!ospf_redistribute_check(ospf
, ei
, NULL
))
1991 /* Create new AS-external-LSA instance. */
1992 if ((new = ospf_external_lsa_new(ospf
, ei
, NULL
)) == NULL
) {
1993 if (IS_DEBUG_OSPF_EVENT
)
1995 "LSA[Type5:%s]: Could not originate AS-external-LSA",
1996 inet_ntoa(ei
->p
.prefix
));
2000 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2001 ospf_lsa_install(ospf
, NULL
, new);
2003 /* Update LSA origination count. */
2004 ospf
->lsa_originate_count
++;
2006 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2007 ospf_flood_through_as(ospf
, NULL
, new);
2009 /* If there is any attached NSSA, do special handling */
2010 if (ospf
->anyNSSA
&&
2011 /* stay away from translated LSAs! */
2012 !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2013 ospf_install_flood_nssa(
2014 ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2016 /* Debug logging. */
2017 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2018 zlog_debug("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2019 new->data
->type
, inet_ntoa(new->data
->id
),
2021 ospf_lsa_header_dump(new->data
);
2027 /* Originate AS-external-LSA from external info with initial flag. */
2028 int ospf_external_lsa_originate_timer(struct thread
*thread
)
2030 struct ospf
*ospf
= THREAD_ARG(thread
);
2031 struct route_node
*rn
;
2032 struct external_info
*ei
;
2033 struct route_table
*rt
;
2034 int type
= THREAD_VAL(thread
);
2035 struct list
*ext_list
;
2036 struct listnode
*node
;
2037 struct ospf_external
*ext
;
2039 ospf
->t_external_lsa
= NULL
;
2041 ext_list
= ospf
->external
[type
];
2045 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
2046 /* Originate As-external-LSA from all type of distribute source.
2048 if ((rt
= ext
->external_info
))
2049 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
))
2050 if ((ei
= rn
->info
) != NULL
)
2051 if (!is_prefix_default(
2052 (struct prefix_ipv4
*)&ei
2054 if (!ospf_external_lsa_originate(
2057 "LSA: AS-external-LSA was not originated.");
2062 static struct external_info
*ospf_default_external_info(struct ospf
*ospf
)
2065 struct route_node
*rn
;
2066 struct prefix_ipv4 p
;
2069 p
.prefix
.s_addr
= 0;
2072 /* First, lookup redistributed default route. */
2073 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
2074 struct list
*ext_list
;
2075 struct listnode
*node
;
2076 struct ospf_external
*ext
;
2078 if (type
== ZEBRA_ROUTE_OSPF
)
2081 ext_list
= ospf
->external
[type
];
2085 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2086 rn
= route_node_lookup(ext
->external_info
,
2087 (struct prefix
*)&p
);
2089 route_unlock_node(rn
);
2091 if (ospf_redistribute_check(ospf
, rn
->info
,
2101 int ospf_default_originate_timer(struct thread
*thread
)
2103 struct prefix_ipv4 p
;
2104 struct in_addr nexthop
;
2105 struct external_info
*ei
;
2108 ospf
= THREAD_ARG(thread
);
2111 p
.prefix
.s_addr
= 0;
2114 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ALWAYS
) {
2115 /* If there is no default route via redistribute,
2116 then originate AS-external-LSA with nexthop 0 (self). */
2118 ospf_external_info_add(ospf
, DEFAULT_ROUTE
, 0, p
, 0, nexthop
,
2122 if ((ei
= ospf_default_external_info(ospf
)))
2123 ospf_external_lsa_originate(ospf
, ei
);
2128 /* Flush any NSSA LSAs for given prefix */
2129 void ospf_nssa_lsa_flush(struct ospf
*ospf
, struct prefix_ipv4
*p
)
2131 struct listnode
*node
, *nnode
;
2132 struct ospf_lsa
*lsa
= NULL
;
2133 struct ospf_area
*area
;
2135 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2136 if (area
->external_routing
== OSPF_AREA_NSSA
) {
2137 lsa
= ospf_lsa_lookup(ospf
, area
, OSPF_AS_NSSA_LSA
,
2138 p
->prefix
, ospf
->router_id
);
2140 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2142 "LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2143 inet_ntoa(p
->prefix
),
2147 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
2148 if (!IS_LSA_MAXAGE(lsa
)) {
2149 ospf_refresher_unregister_lsa(ospf
, lsa
);
2150 ospf_lsa_flush_area(lsa
, area
);
2156 /* Flush an AS-external-LSA from LSDB and routing domain. */
2157 void ospf_external_lsa_flush(struct ospf
*ospf
, uint8_t type
,
2158 struct prefix_ipv4
*p
,
2159 ifindex_t ifindex
/*, struct in_addr nexthop */)
2161 struct ospf_lsa
*lsa
;
2163 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2164 zlog_debug("LSA: Flushing AS-external-LSA %s/%d",
2165 inet_ntoa(p
->prefix
), p
->prefixlen
);
2167 /* First lookup LSA from LSDB. */
2168 if (!(lsa
= ospf_external_info_find_lsa(ospf
, p
))) {
2169 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2171 "LSA: There is no such AS-external-LSA %s/%d in LSDB",
2172 inet_ntoa(p
->prefix
), p
->prefixlen
);
2176 /* If LSA is selforiginated, not a translated LSA, and there is
2177 * NSSA area, flush Type-7 LSA's at first.
2179 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2180 && !(CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2181 ospf_nssa_lsa_flush(ospf
, p
);
2183 /* Sweep LSA from Link State Retransmit List. */
2184 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2186 /* There must be no self-originated LSA in rtrs_external. */
2188 /* Remove External route from Zebra. */
2189 ospf_zebra_delete ((struct prefix_ipv4
*) p
, &nexthop
);
2192 if (!IS_LSA_MAXAGE(lsa
)) {
2193 /* Unregister LSA from Refresh queue. */
2194 ospf_refresher_unregister_lsa(ospf
, lsa
);
2196 /* Flush AS-external-LSA through AS. */
2197 ospf_lsa_flush_as(ospf
, lsa
);
2200 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2201 zlog_debug("ospf_external_lsa_flush(): stop");
2204 void ospf_external_lsa_refresh_default(struct ospf
*ospf
)
2206 struct prefix_ipv4 p
;
2207 struct external_info
*ei
;
2208 struct ospf_lsa
*lsa
;
2212 p
.prefix
.s_addr
= 0;
2214 ei
= ospf_default_external_info(ospf
);
2215 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
2219 if (IS_DEBUG_OSPF_EVENT
)
2221 "LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2223 ospf_external_lsa_refresh(ospf
, lsa
, ei
,
2226 if (IS_DEBUG_OSPF_EVENT
)
2228 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2229 ospf_external_lsa_originate(ospf
, ei
);
2233 if (IS_DEBUG_OSPF_EVENT
)
2235 "LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2236 ospf_refresher_unregister_lsa(ospf
, lsa
);
2237 ospf_lsa_flush_as(ospf
, lsa
);
2242 void ospf_external_lsa_refresh_type(struct ospf
*ospf
, uint8_t type
,
2243 unsigned short instance
, int force
)
2245 struct route_node
*rn
;
2246 struct external_info
*ei
;
2247 struct ospf_external
*ext
;
2249 if (type
== DEFAULT_ROUTE
)
2252 ext
= ospf_external_lookup(ospf
, type
, instance
);
2254 if (ext
&& EXTERNAL_INFO(ext
)) {
2255 /* Refresh each redistributed AS-external-LSAs. */
2256 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
2257 rn
= route_next(rn
)) {
2260 if (!is_prefix_default(&ei
->p
)) {
2261 struct ospf_lsa
*lsa
;
2263 lsa
= ospf_external_info_find_lsa(
2266 ospf_external_lsa_refresh(
2267 ospf
, lsa
, ei
, force
);
2269 ospf_external_lsa_originate(
2277 /* Refresh AS-external-LSA. */
2278 struct ospf_lsa
*ospf_external_lsa_refresh(struct ospf
*ospf
,
2279 struct ospf_lsa
*lsa
,
2280 struct external_info
*ei
, int force
)
2282 struct ospf_lsa
*new;
2285 /* Check the AS-external-LSA should be originated. */
2286 if (!ospf_redistribute_check(ospf
, ei
, &changed
)) {
2287 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2289 "LSA[Type%d:%s]: Could not be refreshed, "
2290 "redist check fail",
2291 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2292 ospf_external_lsa_flush(ospf
, ei
->type
, &ei
->p
,
2293 ei
->ifindex
/*, ei->nexthop */);
2297 if (!changed
&& !force
) {
2298 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2300 "LSA[Type%d:%s]: Not refreshed, not changed/forced",
2301 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2305 /* Delete LSA from neighbor retransmit-list. */
2306 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2308 /* Unregister AS-external-LSA from refresh-list. */
2309 ospf_refresher_unregister_lsa(ospf
, lsa
);
2311 new = ospf_external_lsa_new(ospf
, ei
, &lsa
->data
->id
);
2314 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2315 zlog_debug("LSA[Type%d:%s]: Could not be refreshed",
2316 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2320 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2322 ospf_lsa_install(ospf
, NULL
, new); /* As type-5. */
2324 /* Flood LSA through AS. */
2325 ospf_flood_through_as(ospf
, NULL
, new);
2327 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2328 if (ospf
->anyNSSA
&& !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2329 ospf_install_flood_nssa(ospf
, new,
2330 ei
); /* Install/Flood per new rules */
2332 /* Register self-originated LSA to refresh queue.
2333 * Translated LSAs should not be registered, but refreshed upon
2334 * refresh of the Type-7
2336 if (!CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2337 ospf_refresher_register_lsa(ospf
, new);
2339 /* Debug logging. */
2340 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2341 zlog_debug("LSA[Type%d:%s]: AS-external-LSA refresh",
2342 new->data
->type
, inet_ntoa(new->data
->id
));
2343 ospf_lsa_header_dump(new->data
);
2350 /* LSA installation functions. */
2352 /* Install router-LSA to an area. */
2353 static struct ospf_lsa
*
2354 ospf_router_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2356 struct ospf_area
*area
= new->area
;
2358 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2359 The entire routing table must be recalculated, starting with
2360 the shortest path calculations for each area (not just the
2361 area whose link-state database has changed).
2364 if (IS_LSA_SELF(new)) {
2366 /* Only install LSA if it is originated/refreshed by us.
2367 * If LSA was received by flooding, the RECEIVED flag is set so
2369 * not link the LSA */
2370 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2371 return new; /* ignore stale LSA */
2373 /* Set self-originated router-LSA. */
2374 ospf_lsa_unlock(&area
->router_lsa_self
);
2375 area
->router_lsa_self
= ospf_lsa_lock(new);
2377 ospf_refresher_register_lsa(ospf
, new);
2380 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2384 #define OSPF_INTERFACE_TIMER_ON(T, F, V) \
2386 (T) = thread_add_timer(master, (F), oi, (V))
2388 /* Install network-LSA to an area. */
2389 static struct ospf_lsa
*ospf_network_lsa_install(struct ospf
*ospf
,
2390 struct ospf_interface
*oi
,
2391 struct ospf_lsa
*new,
2395 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2396 The entire routing table must be recalculated, starting with
2397 the shortest path calculations for each area (not just the
2398 area whose link-state database has changed).
2400 if (IS_LSA_SELF(new)) {
2401 /* We supposed that when LSA is originated by us, we pass the
2403 for which it was originated. If LSA was received by flooding,
2404 the RECEIVED flag is set, so we do not link the LSA to the
2406 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2407 return new; /* ignore stale LSA */
2409 ospf_lsa_unlock(&oi
->network_lsa_self
);
2410 oi
->network_lsa_self
= ospf_lsa_lock(new);
2411 ospf_refresher_register_lsa(ospf
, new);
2414 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2419 /* Install summary-LSA to an area. */
2420 static struct ospf_lsa
*
2421 ospf_summary_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2423 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2424 /* RFC 2328 Section 13.2 Summary-LSAs
2425 The best route to the destination described by the summary-
2426 LSA must be recalculated (see Section 16.5). If this
2427 destination is an AS boundary router, it may also be
2428 necessary to re-examine all the AS-external-LSAs.
2432 /* This doesn't exist yet... */
2433 ospf_summary_incremental_update(new); */
2435 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2439 if (IS_LSA_SELF(new))
2440 ospf_refresher_register_lsa(ospf
, new);
2445 /* Install ASBR-summary-LSA to an area. */
2446 static struct ospf_lsa
*ospf_summary_asbr_lsa_install(struct ospf
*ospf
,
2447 struct ospf_lsa
*new,
2450 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2451 /* RFC 2328 Section 13.2 Summary-LSAs
2452 The best route to the destination described by the summary-
2453 LSA must be recalculated (see Section 16.5). If this
2454 destination is an AS boundary router, it may also be
2455 necessary to re-examine all the AS-external-LSAs.
2458 /* These don't exist yet... */
2459 ospf_summary_incremental_update(new);
2460 /* Isn't this done by the above call?
2461 - RFC 2328 Section 16.5 implies it should be */
2462 /* ospf_ase_calculate_schedule(); */
2464 ospf_spf_calculate_schedule(ospf
,
2465 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2469 /* register LSA to refresh-list. */
2470 if (IS_LSA_SELF(new))
2471 ospf_refresher_register_lsa(ospf
, new);
2476 /* Install AS-external-LSA. */
2477 static struct ospf_lsa
*ospf_external_lsa_install(struct ospf
*ospf
,
2478 struct ospf_lsa
*new,
2481 ospf_ase_register_external_lsa(new, ospf
);
2482 /* If LSA is not self-originated, calculate an external route. */
2484 /* RFC 2328 Section 13.2 AS-external-LSAs
2485 The best route to the destination described by the AS-
2486 external-LSA must be recalculated (see Section 16.6).
2489 if (!IS_LSA_SELF(new))
2490 ospf_ase_incremental_update(ospf
, new);
2493 if (new->data
->type
== OSPF_AS_NSSA_LSA
) {
2494 /* There is no point to register selforiginate Type-7 LSA for
2495 * refreshing. We rely on refreshing Type-5 LSA's
2497 if (IS_LSA_SELF(new))
2500 /* Try refresh type-5 translated LSA for this LSA, if
2502 * New translations will be taken care of by the
2505 ospf_translated_nssa_refresh(ospf
, new, NULL
);
2506 ospf_schedule_abr_task(ospf
);
2510 /* Register self-originated LSA to refresh queue.
2511 * Leave Translated LSAs alone if NSSA is enabled
2513 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2514 ospf_refresher_register_lsa(ospf
, new);
2519 void ospf_discard_from_db(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
2520 struct ospf_lsa
*lsa
)
2522 struct ospf_lsa
*old
;
2525 zlog_warn("%s: Called with NULL lsdb!", __func__
);
2527 zlog_warn("%s: and NULL LSA!", __func__
);
2529 zlog_warn("LSA[Type%d:%s]: not associated with LSDB!",
2530 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2534 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2539 if (old
->refresh_list
>= 0)
2540 ospf_refresher_unregister_lsa(ospf
, old
);
2542 switch (old
->data
->type
) {
2543 case OSPF_AS_EXTERNAL_LSA
:
2544 ospf_ase_unregister_external_lsa(old
, ospf
);
2545 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2547 case OSPF_OPAQUE_AS_LSA
:
2548 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2550 case OSPF_AS_NSSA_LSA
:
2551 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2552 ospf_ase_unregister_external_lsa(old
, ospf
);
2555 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2559 ospf_lsa_maxage_delete(ospf
, old
);
2560 ospf_lsa_discard(old
);
2563 struct ospf_lsa
*ospf_lsa_install(struct ospf
*ospf
, struct ospf_interface
*oi
,
2564 struct ospf_lsa
*lsa
)
2566 struct ospf_lsa
*new = NULL
;
2567 struct ospf_lsa
*old
= NULL
;
2568 struct ospf_lsdb
*lsdb
= NULL
;
2572 switch (lsa
->data
->type
) {
2574 case OSPF_AS_NSSA_LSA
:
2576 lsdb
= lsa
->area
->lsdb
;
2580 case OSPF_AS_EXTERNAL_LSA
:
2581 case OSPF_OPAQUE_AS_LSA
:
2586 lsdb
= lsa
->area
->lsdb
;
2592 /* RFC 2328 13.2. Installing LSAs in the database
2594 Installing a new LSA in the database, either as the result of
2595 flooding or a newly self-originated LSA, may cause the OSPF
2596 routing table structure to be recalculated. The contents of the
2597 new LSA should be compared to the old instance, if present. If
2598 there is no difference, there is no need to recalculate the
2599 routing table. When comparing an LSA to its previous instance,
2600 the following are all considered to be differences in contents:
2602 o The LSA's Options field has changed.
2604 o One of the LSA instances has LS age set to MaxAge, and
2607 o The length field in the LSA header has changed.
2609 o The body of the LSA (i.e., anything outside the 20-byte
2610 LSA header) has changed. Note that this excludes changes
2611 in LS Sequence Number and LS Checksum.
2614 /* Look up old LSA and determine if any SPF calculation or incremental
2616 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2618 /* Do comparision and record if recalc needed. */
2620 if (old
== NULL
|| ospf_lsa_different(old
, lsa
))
2624 Sequence number check (Section 14.1 of rfc 2328)
2625 "Premature aging is used when it is time for a self-originated
2626 LSA's sequence number field to wrap. At this point, the current
2627 LSA instance (having LS sequence number MaxSequenceNumber) must
2628 be prematurely aged and flushed from the routing domain before a
2629 new instance with sequence number equal to InitialSequenceNumber
2630 can be originated. "
2633 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
) {
2634 if (ospf_lsa_is_self_originated(ospf
, lsa
)) {
2635 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2637 if (!IS_LSA_MAXAGE(lsa
))
2638 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2639 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
2641 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
)) {
2643 "ospf_lsa_install() Premature Aging "
2644 "lsa 0x%p, seqnum 0x%x",
2646 ntohl(lsa
->data
->ls_seqnum
));
2647 ospf_lsa_header_dump(lsa
->data
);
2650 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2652 "ospf_lsa_install() got an lsa with seq 0x80000000 "
2653 "that was not self originated. Ignoring\n");
2654 ospf_lsa_header_dump(lsa
->data
);
2660 /* discard old LSA from LSDB */
2662 ospf_discard_from_db(ospf
, lsdb
, lsa
);
2664 /* Calculate Checksum if self-originated?. */
2665 if (IS_LSA_SELF(lsa
))
2666 ospf_lsa_checksum(lsa
->data
);
2668 /* Insert LSA to LSDB. */
2669 ospf_lsdb_add(lsdb
, lsa
);
2672 /* Do LSA specific installation process. */
2673 switch (lsa
->data
->type
) {
2674 case OSPF_ROUTER_LSA
:
2675 new = ospf_router_lsa_install(ospf
, lsa
, rt_recalc
);
2677 case OSPF_NETWORK_LSA
:
2679 new = ospf_network_lsa_install(ospf
, oi
, lsa
, rt_recalc
);
2681 case OSPF_SUMMARY_LSA
:
2682 new = ospf_summary_lsa_install(ospf
, lsa
, rt_recalc
);
2684 case OSPF_ASBR_SUMMARY_LSA
:
2685 new = ospf_summary_asbr_lsa_install(ospf
, lsa
, rt_recalc
);
2687 case OSPF_AS_EXTERNAL_LSA
:
2688 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2690 case OSPF_OPAQUE_LINK_LSA
:
2691 if (IS_LSA_SELF(lsa
))
2692 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for
2695 /* Incoming "oi" for this LSA has set at LSUpd
2699 case OSPF_OPAQUE_AREA_LSA
:
2700 case OSPF_OPAQUE_AS_LSA
:
2701 new = ospf_opaque_lsa_install(lsa
, rt_recalc
);
2703 case OSPF_AS_NSSA_LSA
:
2704 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2705 default: /* type-6,8,9....nothing special */
2710 return new; /* Installation failed, cannot proceed further --
2714 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
)) {
2715 char area_str
[INET_ADDRSTRLEN
];
2717 switch (lsa
->data
->type
) {
2718 case OSPF_AS_EXTERNAL_LSA
:
2719 case OSPF_OPAQUE_AS_LSA
:
2720 case OSPF_AS_NSSA_LSA
:
2721 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2722 lookup_msg(ospf_lsa_type_msg
,
2723 new->data
->type
, NULL
));
2726 strlcpy(area_str
, inet_ntoa(new->area
->area_id
),
2728 zlog_debug("LSA[%s]: Install %s to Area %s",
2730 lookup_msg(ospf_lsa_type_msg
,
2731 new->data
->type
, NULL
),
2738 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2739 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2741 if (IS_LSA_MAXAGE(new)) {
2742 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
))
2743 zlog_debug("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2744 new->data
->type
, inet_ntoa(new->data
->id
),
2746 ospf_lsa_maxage(ospf
, lsa
);
2753 int ospf_check_nbr_status(struct ospf
*ospf
)
2755 struct listnode
*node
, *nnode
;
2756 struct ospf_interface
*oi
;
2758 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
2759 struct route_node
*rn
;
2760 struct ospf_neighbor
*nbr
;
2762 if (ospf_if_is_enable(oi
))
2763 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
2764 if ((nbr
= rn
->info
) != NULL
)
2765 if (nbr
->state
== NSM_Exchange
2766 || nbr
->state
== NSM_Loading
) {
2767 route_unlock_node(rn
);
2776 static int ospf_maxage_lsa_remover(struct thread
*thread
)
2778 struct ospf
*ospf
= THREAD_ARG(thread
);
2779 struct ospf_lsa
*lsa
;
2780 struct route_node
*rn
;
2783 ospf
->t_maxage
= NULL
;
2785 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2786 zlog_debug("LSA[MaxAge]: remover Start");
2788 reschedule
= !ospf_check_nbr_status(ospf
);
2791 for (rn
= route_top(ospf
->maxage_lsa
); rn
;
2792 rn
= route_next(rn
)) {
2793 if ((lsa
= rn
->info
) == NULL
) {
2797 /* There is at least one neighbor from which we still
2799 * for that LSA, so we are not allowed to remove it from
2801 * as per RFC 2328 section 14 para 4 a) */
2802 if (lsa
->retransmit_counter
> 0) {
2807 /* TODO: maybe convert this function to a work-queue */
2808 if (thread_should_yield(thread
)) {
2809 OSPF_TIMER_ON(ospf
->t_maxage
,
2810 ospf_maxage_lsa_remover
, 0);
2812 rn
); /* route_top/route_next */
2816 /* Remove LSA from the LSDB */
2817 if (IS_LSA_SELF(lsa
))
2818 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2820 "LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2822 inet_ntoa(lsa
->data
->id
),
2823 (unsigned long)lsa
);
2825 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2827 "LSA[Type%d:%s]: MaxAge LSA removed from list",
2829 inet_ntoa(lsa
->data
->id
));
2831 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
)) {
2832 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2834 "originating new lsa for lsa 0x%p\n",
2836 ospf_lsa_refresh(ospf
, lsa
);
2839 /* Remove from lsdb. */
2841 ospf_discard_from_db(ospf
, lsa
->lsdb
, lsa
);
2842 ospf_lsdb_delete(lsa
->lsdb
, lsa
);
2845 "%s: LSA[Type%d:%s]: No associated LSDB!",
2846 __func__
, lsa
->data
->type
,
2847 inet_ntoa(lsa
->data
->id
));
2850 /* A MaxAge LSA must be removed immediately from the router's link
2851 state database as soon as both a) it is no longer contained on any
2852 neighbor Link state retransmission lists and b) none of the
2854 neighbors are in states Exchange or Loading. */
2856 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2857 ospf
->maxage_delay
);
2862 void ospf_lsa_maxage_delete(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2864 struct route_node
*rn
;
2865 struct prefix lsa_prefix
;
2867 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2868 lsa_prefix
.family
= 0;
2869 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2870 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2872 if ((rn
= route_node_lookup(ospf
->maxage_lsa
,
2873 (struct prefix
*)&lsa_prefix
))) {
2874 if (rn
->info
== lsa
) {
2875 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2876 ospf_lsa_unlock(&lsa
); /* maxage_lsa */
2879 rn
); /* unlock node because lsa is deleted */
2881 route_unlock_node(rn
); /* route_node_lookup */
2883 if (IS_DEBUG_OSPF_EVENT
)
2884 zlog_debug("%s: lsa %s is not found in maxage db.",
2885 __PRETTY_FUNCTION__
, dump_lsa_key(lsa
));
2889 /* Add LSA onto the MaxAge list, and schedule for removal.
2890 * This does *not* lead to the LSA being flooded, that must be taken
2891 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2894 void ospf_lsa_maxage(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2896 struct prefix lsa_prefix
;
2897 struct route_node
*rn
;
2899 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2900 and schedule the MaxAge LSA remover. */
2901 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
)) {
2902 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2904 "LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2905 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
2910 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2911 lsa_prefix
.family
= 0;
2912 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2913 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2915 if ((rn
= route_node_get(ospf
->maxage_lsa
,
2916 (struct prefix
*)&lsa_prefix
))
2918 if (rn
->info
!= NULL
) {
2919 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2921 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
2922 dump_lsa_key(lsa
), rn
->info
,
2923 (void *)lsa
, lsa_prefix
.prefixlen
);
2924 route_unlock_node(rn
);
2926 rn
->info
= ospf_lsa_lock(lsa
);
2927 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2930 zlog_err("Unable to allocate memory for maxage lsa %s\n",
2935 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2936 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
2939 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2940 ospf
->maxage_delay
);
2943 static int ospf_lsa_maxage_walker_remover(struct ospf
*ospf
,
2944 struct ospf_lsa
*lsa
)
2946 /* Stay away from any Local Translated Type-7 LSAs */
2947 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
2950 if (IS_LSA_MAXAGE(lsa
))
2951 /* Self-originated LSAs should NOT time-out instead,
2952 they're flushed and submitted to the max_age list explicitly.
2954 if (!ospf_lsa_is_self_originated(ospf
, lsa
)) {
2955 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2956 zlog_debug("LSA[%s]: is MaxAge",
2959 switch (lsa
->data
->type
) {
2960 case OSPF_OPAQUE_LINK_LSA
:
2961 case OSPF_OPAQUE_AREA_LSA
:
2962 case OSPF_OPAQUE_AS_LSA
:
2964 * As a general rule, whenever network topology
2966 * (due to an LSA removal in this case), routing
2968 * should be triggered. However, this is not
2970 * LSAs. Even if an opaque LSA instance is going
2972 * from the routing domain, it does not mean a
2974 * topology, and thus, routing recalculation is
2978 case OSPF_AS_EXTERNAL_LSA
:
2979 case OSPF_AS_NSSA_LSA
:
2980 ospf_ase_incremental_update(ospf
, lsa
);
2983 ospf_spf_calculate_schedule(ospf
,
2987 ospf_lsa_maxage(ospf
, lsa
);
2990 if (IS_LSA_MAXAGE(lsa
) && !ospf_lsa_is_self_originated(ospf
, lsa
))
2991 if (LS_AGE(lsa
) > OSPF_LSA_MAXAGE
+ 30)
2992 printf("Eek! Shouldn't happen!\n");
2997 /* Periodical check of MaxAge LSA. */
2998 int ospf_lsa_maxage_walker(struct thread
*thread
)
3000 struct ospf
*ospf
= THREAD_ARG(thread
);
3001 struct route_node
*rn
;
3002 struct ospf_lsa
*lsa
;
3003 struct ospf_area
*area
;
3004 struct listnode
*node
, *nnode
;
3006 ospf
->t_maxage_walker
= NULL
;
3008 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3009 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
3010 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3011 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
3012 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3013 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3014 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3015 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3016 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3017 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3018 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3019 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3020 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3021 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
3022 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3025 /* for AS-external-LSAs. */
3027 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3028 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3029 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3030 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3033 OSPF_TIMER_ON(ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3034 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3038 struct ospf_lsa
*ospf_lsa_lookup_by_prefix(struct ospf_lsdb
*lsdb
, uint8_t type
,
3039 struct prefix_ipv4
*p
,
3040 struct in_addr router_id
)
3042 struct ospf_lsa
*lsa
;
3043 struct in_addr mask
, id
;
3044 struct lsa_header_mask
{
3045 struct lsa_header header
;
3046 struct in_addr mask
;
3049 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, p
->prefix
, router_id
);
3053 masklen2ip(p
->prefixlen
, &mask
);
3055 hmask
= (struct lsa_header_mask
*)lsa
->data
;
3057 if (mask
.s_addr
!= hmask
->mask
.s_addr
) {
3058 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3059 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, router_id
);
3067 struct ospf_lsa
*ospf_lsa_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
3068 uint32_t type
, struct in_addr id
,
3069 struct in_addr adv_router
)
3075 case OSPF_ROUTER_LSA
:
3076 case OSPF_NETWORK_LSA
:
3077 case OSPF_SUMMARY_LSA
:
3078 case OSPF_ASBR_SUMMARY_LSA
:
3079 case OSPF_AS_NSSA_LSA
:
3080 case OSPF_OPAQUE_LINK_LSA
:
3081 case OSPF_OPAQUE_AREA_LSA
:
3082 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, adv_router
);
3083 case OSPF_AS_EXTERNAL_LSA
:
3084 case OSPF_OPAQUE_AS_LSA
:
3085 return ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
, adv_router
);
3093 struct ospf_lsa
*ospf_lsa_lookup_by_id(struct ospf_area
*area
, uint32_t type
,
3096 struct ospf_lsa
*lsa
;
3097 struct route_node
*rn
;
3100 case OSPF_ROUTER_LSA
:
3101 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3102 case OSPF_NETWORK_LSA
:
3103 for (rn
= route_top(NETWORK_LSDB(area
)); rn
;
3104 rn
= route_next(rn
))
3105 if ((lsa
= rn
->info
))
3106 if (IPV4_ADDR_SAME(&lsa
->data
->id
, &id
)) {
3107 route_unlock_node(rn
);
3111 case OSPF_SUMMARY_LSA
:
3112 case OSPF_ASBR_SUMMARY_LSA
:
3113 /* Currently not used. */
3115 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3116 case OSPF_AS_EXTERNAL_LSA
:
3117 case OSPF_AS_NSSA_LSA
:
3118 case OSPF_OPAQUE_LINK_LSA
:
3119 case OSPF_OPAQUE_AREA_LSA
:
3120 case OSPF_OPAQUE_AS_LSA
:
3121 /* Currently not used. */
3130 struct ospf_lsa
*ospf_lsa_lookup_by_header(struct ospf_area
*area
,
3131 struct lsa_header
*lsah
)
3133 struct ospf_lsa
*match
;
3136 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3137 * is redefined to have two subfields; opaque-type and opaque-id.
3138 * However, it is harmless to treat the two sub fields together, as if
3139 * they two were forming a unique LSA-ID.
3142 match
= ospf_lsa_lookup(area
->ospf
, area
, lsah
->type
, lsah
->id
,
3146 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3147 zlog_debug("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3148 lsah
->type
, inet_ntoa(lsah
->id
));
3153 /* return +n, l1 is more recent.
3154 return -n, l2 is more recent.
3155 return 0, l1 and l2 is identical. */
3156 int ospf_lsa_more_recent(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3161 if (l1
== NULL
&& l2
== NULL
)
3168 /* compare LS sequence number. */
3169 x
= (int)ntohl(l1
->data
->ls_seqnum
);
3170 y
= (int)ntohl(l2
->data
->ls_seqnum
);
3176 /* compare LS checksum. */
3177 r
= ntohs(l1
->data
->checksum
) - ntohs(l2
->data
->checksum
);
3181 /* compare LS age. */
3182 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3184 else if (!IS_LSA_MAXAGE(l1
) && IS_LSA_MAXAGE(l2
))
3187 /* compare LS age with MaxAgeDiff. */
3188 if (LS_AGE(l1
) - LS_AGE(l2
) > OSPF_LSA_MAXAGE_DIFF
)
3190 else if (LS_AGE(l2
) - LS_AGE(l1
) > OSPF_LSA_MAXAGE_DIFF
)
3193 /* LSAs are identical. */
3197 /* If two LSAs are different, return 1, otherwise return 0. */
3198 int ospf_lsa_different(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3206 if (l1
->data
->options
!= l2
->data
->options
)
3209 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3212 if (IS_LSA_MAXAGE(l2
) && !IS_LSA_MAXAGE(l1
))
3215 if (l1
->data
->length
!= l2
->data
->length
)
3218 if (l1
->data
->length
== 0)
3221 if (CHECK_FLAG((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3222 return 1; /* May be a stale LSA in the LSBD */
3224 assert(ntohs(l1
->data
->length
) > OSPF_LSA_HEADER_SIZE
);
3226 p1
= (char *)l1
->data
;
3227 p2
= (char *)l2
->data
;
3229 if (memcmp(p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3230 ntohs(l1
->data
->length
) - OSPF_LSA_HEADER_SIZE
)
3237 #ifdef ORIGINAL_CODING
3238 void ospf_lsa_flush_self_originated(struct ospf_neighbor
*nbr
,
3239 struct ospf_lsa
*self
, struct ospf_lsa
*new)
3243 /* Adjust LS Sequence Number. */
3244 seqnum
= ntohl(new->data
->ls_seqnum
) + 1;
3245 self
->data
->ls_seqnum
= htonl(seqnum
);
3247 /* Recalculate LSA checksum. */
3248 ospf_lsa_checksum(self
->data
);
3250 /* Reflooding LSA. */
3251 /* RFC2328 Section 13.3
3252 On non-broadcast networks, separate Link State Update
3253 packets must be sent, as unicasts, to each adjacent neighbor
3254 (i.e., those in state Exchange or greater). The destination
3255 IP addresses for these packets are the neighbors' IP
3257 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
) {
3258 struct route_node
*rn
;
3259 struct ospf_neighbor
*onbr
;
3261 for (rn
= route_top(nbr
->oi
->nbrs
); rn
; rn
= route_next(rn
))
3262 if ((onbr
= rn
->info
) != NULL
)
3263 if (onbr
!= nbr
->oi
->nbr_self
3264 && onbr
->status
>= NSM_Exchange
)
3265 ospf_ls_upd_send_lsa(
3267 OSPF_SEND_PACKET_DIRECT
);
3269 ospf_ls_upd_send_lsa(nbr
, self
, OSPF_SEND_PACKET_INDIRECT
);
3271 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3272 zlog_debug("LSA[Type%d:%s]: Flush self-originated LSA",
3273 self
->data
->type
, inet_ntoa(self
->data
->id
));
3275 #else /* ORIGINAL_CODING */
3276 int ospf_lsa_flush_schedule(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3278 if (lsa
== NULL
|| !IS_LSA_SELF(lsa
))
3281 if (IS_DEBUG_OSPF_EVENT
)
3283 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3284 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
3286 /* Force given lsa's age to MaxAge. */
3287 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
3289 switch (lsa
->data
->type
) {
3290 /* Opaque wants to be notified of flushes */
3291 case OSPF_OPAQUE_LINK_LSA
:
3292 case OSPF_OPAQUE_AREA_LSA
:
3293 case OSPF_OPAQUE_AS_LSA
:
3294 ospf_opaque_lsa_refresh(lsa
);
3297 ospf_refresher_unregister_lsa(ospf
, lsa
);
3298 ospf_lsa_flush(ospf
, lsa
);
3305 void ospf_flush_self_originated_lsas_now(struct ospf
*ospf
)
3307 struct listnode
*node
, *nnode
;
3308 struct listnode
*node2
, *nnode2
;
3309 struct ospf_area
*area
;
3310 struct ospf_interface
*oi
;
3311 struct ospf_lsa
*lsa
;
3312 struct route_node
*rn
;
3313 int need_to_flush_ase
= 0;
3315 ospf
->inst_shutdown
= 1;
3317 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3318 if ((lsa
= area
->router_lsa_self
) != NULL
) {
3319 if (IS_DEBUG_OSPF_EVENT
)
3321 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3323 inet_ntoa(lsa
->data
->id
));
3325 ospf_refresher_unregister_lsa(ospf
, lsa
);
3326 ospf_lsa_flush_area(lsa
, area
);
3327 ospf_lsa_unlock(&area
->router_lsa_self
);
3328 area
->router_lsa_self
= NULL
;
3331 for (ALL_LIST_ELEMENTS(area
->oiflist
, node2
, nnode2
, oi
)) {
3332 if ((lsa
= oi
->network_lsa_self
) != NULL
3333 && oi
->state
== ISM_DR
&& oi
->full_nbrs
> 0) {
3334 if (IS_DEBUG_OSPF_EVENT
)
3336 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3338 inet_ntoa(lsa
->data
->id
));
3340 ospf_refresher_unregister_lsa(
3341 ospf
, oi
->network_lsa_self
);
3342 ospf_lsa_flush_area(oi
->network_lsa_self
, area
);
3343 ospf_lsa_unlock(&oi
->network_lsa_self
);
3344 oi
->network_lsa_self
= NULL
;
3347 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3348 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3349 need_to_flush_ase
= 1;
3352 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3353 ospf_lsa_flush_schedule(ospf
, lsa
);
3354 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3355 ospf_lsa_flush_schedule(ospf
, lsa
);
3356 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3357 ospf_lsa_flush_schedule(ospf
, lsa
);
3358 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3359 ospf_lsa_flush_schedule(ospf
, lsa
);
3362 if (need_to_flush_ase
) {
3363 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3364 ospf_lsa_flush_schedule(ospf
, lsa
);
3365 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3366 ospf_lsa_flush_schedule(ospf
, lsa
);
3370 * Make sure that the MaxAge LSA remover is executed immediately,
3371 * without conflicting to other threads.
3373 if (ospf
->t_maxage
!= NULL
) {
3374 OSPF_TIMER_OFF(ospf
->t_maxage
);
3375 thread_execute(master
, ospf_maxage_lsa_remover
, ospf
, 0);
3380 #endif /* ORIGINAL_CODING */
3382 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3383 /* An interface-independent version of ospf_lsa_is_self_originated */
3384 int ospf_lsa_is_self_originated(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3386 struct listnode
*node
;
3387 struct ospf_interface
*oi
;
3389 /* This LSA is already checked. */
3390 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3391 return IS_LSA_SELF(lsa
);
3393 /* Make sure LSA is self-checked. */
3394 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3396 /* AdvRouter and Router ID is the same. */
3397 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
, &ospf
->router_id
))
3398 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3400 /* LSA is router-LSA. */
3401 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
3402 && IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
))
3403 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3405 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3406 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3407 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
3408 /* Ignore virtual link. */
3409 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3410 if (oi
->address
->family
== AF_INET
)
3413 &oi
->address
->u
.prefix4
)) {
3414 /* to make it easier later */
3415 SET_FLAG(lsa
->flags
,
3417 return IS_LSA_SELF(lsa
);
3421 return IS_LSA_SELF(lsa
);
3424 /* Get unique Link State ID. */
3425 struct in_addr
ospf_lsa_unique_id(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
3426 uint8_t type
, struct prefix_ipv4
*p
)
3428 struct ospf_lsa
*lsa
;
3429 struct in_addr mask
, id
;
3433 /* Check existence of LSA instance. */
3434 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, ospf
->router_id
);
3436 struct as_external_lsa
*al
=
3437 (struct as_external_lsa
*)lsa
->data
;
3438 if (ip_masklen(al
->mask
) == p
->prefixlen
) {
3439 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3441 "ospf_lsa_unique_id(): "
3442 "Can't get Link State ID for %s/%d",
3443 inet_ntoa(p
->prefix
), p
->prefixlen
);
3444 /* id.s_addr = 0; */
3445 id
.s_addr
= 0xffffffff;
3448 /* Masklen differs, then apply wildcard mask to Link State ID.
3451 masklen2ip(p
->prefixlen
, &mask
);
3453 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3454 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
,
3457 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3459 "ospf_lsa_unique_id(): "
3460 "Can't get Link State ID for %s/%d",
3461 inet_ntoa(p
->prefix
),
3463 /* id.s_addr = 0; */
3464 id
.s_addr
= 0xffffffff;
3474 #define LSA_ACTION_FLOOD_AREA 1
3475 #define LSA_ACTION_FLUSH_AREA 2
3479 struct ospf_area
*area
;
3480 struct ospf_lsa
*lsa
;
3483 static int ospf_lsa_action(struct thread
*t
)
3485 struct lsa_action
*data
;
3487 data
= THREAD_ARG(t
);
3489 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3490 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3493 switch (data
->action
) {
3494 case LSA_ACTION_FLOOD_AREA
:
3495 ospf_flood_through_area(data
->area
, NULL
, data
->lsa
);
3497 case LSA_ACTION_FLUSH_AREA
:
3498 ospf_lsa_flush_area(data
->lsa
, data
->area
);
3502 ospf_lsa_unlock(&data
->lsa
); /* Message */
3503 XFREE(MTYPE_OSPF_MESSAGE
, data
);
3507 void ospf_schedule_lsa_flood_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3509 struct lsa_action
*data
;
3511 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3512 data
->action
= LSA_ACTION_FLOOD_AREA
;
3514 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flood area */
3516 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3519 void ospf_schedule_lsa_flush_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3521 struct lsa_action
*data
;
3523 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3524 data
->action
= LSA_ACTION_FLUSH_AREA
;
3526 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flush area */
3528 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3532 /* LSA Refreshment functions. */
3533 struct ospf_lsa
*ospf_lsa_refresh(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3535 struct external_info
*ei
;
3536 struct ospf_lsa
*new = NULL
;
3537 assert(CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF
));
3538 assert(IS_LSA_SELF(lsa
));
3539 assert(lsa
->lock
> 0);
3541 switch (lsa
->data
->type
) {
3542 /* Router and Network LSAs are processed differently. */
3543 case OSPF_ROUTER_LSA
:
3544 new = ospf_router_lsa_refresh(lsa
);
3546 case OSPF_NETWORK_LSA
:
3547 new = ospf_network_lsa_refresh(lsa
);
3549 case OSPF_SUMMARY_LSA
:
3550 new = ospf_summary_lsa_refresh(ospf
, lsa
);
3552 case OSPF_ASBR_SUMMARY_LSA
:
3553 new = ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
3555 case OSPF_AS_EXTERNAL_LSA
:
3556 /* Translated from NSSA Type-5s are refreshed when
3557 * from refresh of Type-7 - do not refresh these directly.
3559 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3561 ei
= ospf_external_info_check(ospf
, lsa
);
3563 new = ospf_external_lsa_refresh(ospf
, lsa
, ei
,
3566 ospf_lsa_flush_as(ospf
, lsa
);
3568 case OSPF_OPAQUE_LINK_LSA
:
3569 case OSPF_OPAQUE_AREA_LSA
:
3570 case OSPF_OPAQUE_AS_LSA
:
3571 new = ospf_opaque_lsa_refresh(lsa
);
3579 void ospf_refresher_register_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3581 uint16_t index
, current_index
;
3583 assert(lsa
->lock
> 0);
3584 assert(IS_LSA_SELF(lsa
));
3586 if (lsa
->refresh_list
< 0) {
3589 OSPF_LS_REFRESH_TIME
- (2 * OSPF_LS_REFRESH_JITTER
);
3590 int max_delay
= OSPF_LS_REFRESH_TIME
- OSPF_LS_REFRESH_JITTER
;
3592 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3594 * 1800s. Use jitter so that we send the LSA sometime between
3598 delay
= (random() % (max_delay
- min_delay
)) + min_delay
;
3600 current_index
= ospf
->lsa_refresh_queue
.index
3601 + (monotime(NULL
) - ospf
->lsa_refresher_started
)
3602 / OSPF_LSA_REFRESHER_GRANULARITY
;
3604 index
= (current_index
+ delay
/ OSPF_LSA_REFRESHER_GRANULARITY
)
3605 % (OSPF_LSA_REFRESHER_SLOTS
);
3607 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3609 "LSA[Refresh:Type%d:%s]: age %d, added to index %d",
3610 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
3611 LS_AGE(lsa
), index
);
3613 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3614 ospf
->lsa_refresh_queue
.qs
[index
] = list_new();
3616 listnode_add(ospf
->lsa_refresh_queue
.qs
[index
],
3617 ospf_lsa_lock(lsa
)); /* lsa_refresh_queue */
3618 lsa
->refresh_list
= index
;
3620 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3622 "LSA[Refresh:Type%d:%s]: ospf_refresher_register_lsa(): "
3623 "setting refresh_list on lsa %p (slod %d)",
3624 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
3625 (void *)lsa
, index
);
3629 void ospf_refresher_unregister_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3631 assert(lsa
->lock
> 0);
3632 assert(IS_LSA_SELF(lsa
));
3633 if (lsa
->refresh_list
>= 0) {
3634 struct list
*refresh_list
=
3635 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3636 listnode_delete(refresh_list
, lsa
);
3637 if (!listcount(refresh_list
)) {
3638 list_delete_and_null(&refresh_list
);
3639 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3641 ospf_lsa_unlock(&lsa
); /* lsa_refresh_queue */
3642 lsa
->refresh_list
= -1;
3646 int ospf_lsa_refresh_walker(struct thread
*t
)
3648 struct list
*refresh_list
;
3649 struct listnode
*node
, *nnode
;
3650 struct ospf
*ospf
= THREAD_ARG(t
);
3651 struct ospf_lsa
*lsa
;
3653 struct list
*lsa_to_refresh
= list_new();
3655 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3656 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3659 i
= ospf
->lsa_refresh_queue
.index
;
3661 /* Note: if clock has jumped backwards, then time change could be
3663 so we are careful to cast the expression to unsigned before taking
3665 ospf
->lsa_refresh_queue
.index
=
3666 ((unsigned long)(ospf
->lsa_refresh_queue
.index
3668 - ospf
->lsa_refresher_started
)
3669 / OSPF_LSA_REFRESHER_GRANULARITY
))
3670 % OSPF_LSA_REFRESHER_SLOTS
;
3672 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3674 "LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3675 ospf
->lsa_refresh_queue
.index
);
3677 for (; i
!= ospf
->lsa_refresh_queue
.index
;
3678 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
) {
3679 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3681 "LSA[Refresh]: ospf_lsa_refresh_walker(): "
3685 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3689 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3692 for (ALL_LIST_ELEMENTS(refresh_list
, node
, nnode
,
3694 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3696 "LSA[Refresh:Type%d:%s]: ospf_lsa_refresh_walker(): "
3697 "refresh lsa %p (slot %d)",
3699 inet_ntoa(lsa
->data
->id
),
3702 assert(lsa
->lock
> 0);
3703 list_delete_node(refresh_list
, node
);
3704 lsa
->refresh_list
= -1;
3705 listnode_add(lsa_to_refresh
, lsa
);
3707 list_delete_and_null(&refresh_list
);
3711 ospf
->t_lsa_refresher
= NULL
;
3712 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
3713 ospf
->lsa_refresh_interval
, &ospf
->t_lsa_refresher
);
3714 ospf
->lsa_refresher_started
= monotime(NULL
);
3716 for (ALL_LIST_ELEMENTS(lsa_to_refresh
, node
, nnode
, lsa
)) {
3717 ospf_lsa_refresh(ospf
, lsa
);
3718 assert(lsa
->lock
> 0);
3720 &lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3723 list_delete_and_null(&lsa_to_refresh
);
3725 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3726 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");