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"
53 #include "ospfd/ospf_errors.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 struct ospf_lsa
*ospf_lsa_new_and_data(size_t size
)
172 struct ospf_lsa
*new;
174 new = ospf_lsa_new();
175 new->data
= ospf_lsa_data_new(size
);
180 /* Duplicate OSPF LSA. */
181 struct ospf_lsa
*ospf_lsa_dup(struct ospf_lsa
*lsa
)
183 struct ospf_lsa
*new;
188 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
190 memcpy(new, lsa
, sizeof(struct ospf_lsa
));
191 UNSET_FLAG(new->flags
, OSPF_LSA_DISCARD
);
193 new->retransmit_counter
= 0;
194 new->data
= ospf_lsa_data_dup(lsa
->data
);
196 /* kevinm: Clear the refresh_list, otherwise there are going
197 to be problems when we try to remove the LSA from the
198 queue (which it's not a member of.)
199 XXX: Should we add the LSA to the refresh_list queue? */
200 new->refresh_list
= -1;
202 if (IS_DEBUG_OSPF(lsa
, LSA
))
203 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa
,
210 void ospf_lsa_free(struct ospf_lsa
*lsa
)
212 assert(lsa
->lock
== 0);
214 if (IS_DEBUG_OSPF(lsa
, LSA
))
215 zlog_debug("LSA: freed %p", (void *)lsa
);
217 /* Delete LSA data. */
218 if (lsa
->data
!= NULL
)
219 ospf_lsa_data_free(lsa
->data
);
221 assert(lsa
->refresh_list
< 0);
223 memset(lsa
, 0, sizeof(struct ospf_lsa
));
224 XFREE(MTYPE_OSPF_LSA
, lsa
);
228 struct ospf_lsa
*ospf_lsa_lock(struct ospf_lsa
*lsa
)
235 void ospf_lsa_unlock(struct ospf_lsa
**lsa
)
237 /* This is sanity check. */
243 assert((*lsa
)->lock
>= 0);
245 if ((*lsa
)->lock
== 0) {
246 assert(CHECK_FLAG((*lsa
)->flags
, OSPF_LSA_DISCARD
));
252 /* Check discard flag. */
253 void ospf_lsa_discard(struct ospf_lsa
*lsa
)
255 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
256 SET_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
);
257 ospf_lsa_unlock(&lsa
);
261 /* Create LSA data. */
262 struct lsa_header
*ospf_lsa_data_new(size_t size
)
264 return XCALLOC(MTYPE_OSPF_LSA_DATA
, size
);
267 /* Duplicate LSA data. */
268 struct lsa_header
*ospf_lsa_data_dup(struct lsa_header
*lsah
)
270 struct lsa_header
*new;
272 new = ospf_lsa_data_new(ntohs(lsah
->length
));
273 memcpy(new, lsah
, ntohs(lsah
->length
));
279 void ospf_lsa_data_free(struct lsa_header
*lsah
)
281 if (IS_DEBUG_OSPF(lsa
, LSA
))
282 zlog_debug("LSA[Type%d:%s]: data freed %p", lsah
->type
,
283 inet_ntoa(lsah
->id
), (void *)lsah
);
285 XFREE(MTYPE_OSPF_LSA_DATA
, lsah
);
289 /* LSA general functions. */
291 const char *dump_lsa_key(struct ospf_lsa
*lsa
)
293 static char buf
[] = {"Type255,id(255.255.255.255),ar(255.255.255.255)"};
294 struct lsa_header
*lsah
;
296 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
) {
297 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
298 strlcpy(id
, inet_ntoa(lsah
->id
), sizeof(id
));
299 strlcpy(ar
, inet_ntoa(lsah
->adv_router
), sizeof(ar
));
301 sprintf(buf
, "Type%d,id(%s),ar(%s)", lsah
->type
, id
, ar
);
303 strlcpy(buf
, "NULL", sizeof(buf
));
308 uint32_t lsa_seqnum_increment(struct ospf_lsa
*lsa
)
312 seqnum
= ntohl(lsa
->data
->ls_seqnum
) + 1;
314 return htonl(seqnum
);
317 void lsa_header_set(struct stream
*s
, uint8_t options
, uint8_t type
,
318 struct in_addr id
, struct in_addr router_id
)
320 struct lsa_header
*lsah
;
322 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
324 lsah
->ls_age
= htons(OSPF_LSA_INITIAL_AGE
);
325 lsah
->options
= options
;
328 lsah
->adv_router
= router_id
;
329 lsah
->ls_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
331 stream_forward_endp(s
, OSPF_LSA_HEADER_SIZE
);
335 /* router-LSA related functions. */
336 /* Get router-LSA flags. */
337 static uint8_t router_lsa_flags(struct ospf_area
*area
)
341 flags
= area
->ospf
->flags
;
343 /* Set virtual link flag. */
344 if (ospf_full_virtual_nbrs(area
))
345 SET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
347 /* Just sanity check */
348 UNSET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
350 /* Set Shortcut ABR behabiour flag. */
351 UNSET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
352 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
353 if (!OSPF_IS_AREA_BACKBONE(area
))
354 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
355 && area
->ospf
->backbone
== NULL
)
356 || area
->shortcut_configured
357 == OSPF_SHORTCUT_ENABLE
)
358 SET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
360 /* ASBR can't exit in stub area. */
361 if (area
->external_routing
== OSPF_AREA_STUB
)
362 UNSET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
363 /* If ASBR set External flag */
364 else if (IS_OSPF_ASBR(area
->ospf
))
365 SET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
367 /* Set ABR dependent flags */
368 if (IS_OSPF_ABR(area
->ospf
)) {
369 SET_FLAG(flags
, ROUTER_LSA_BORDER
);
370 /* If Area is NSSA and we are both ABR and unconditional
372 * set Nt bit to inform other routers.
374 if ((area
->external_routing
== OSPF_AREA_NSSA
)
375 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
376 SET_FLAG(flags
, ROUTER_LSA_NT
);
381 /* Lookup neighbor other than myself.
382 And check neighbor count,
383 Point-to-Point link must have only 1 neighbor. */
384 struct ospf_neighbor
*ospf_nbr_lookup_ptop(struct ospf_interface
*oi
)
386 struct ospf_neighbor
*nbr
= NULL
;
387 struct route_node
*rn
;
389 /* Search neighbor, there must be one of two nbrs. */
390 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
391 if ((nbr
= rn
->info
))
392 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
393 &oi
->ospf
->router_id
))
394 if (nbr
->state
== NSM_Full
) {
395 route_unlock_node(rn
);
399 /* PtoP link must have only 1 neighbor. */
400 if (ospf_nbr_count(oi
, 0) > 1)
401 flog_warn(EC_OSPF_PTP_NEIGHBOR
,
402 "Point-to-Point link has more than 1 neighobrs.");
407 /* Determine cost of link, taking RFC3137 stub-router support into
410 static uint16_t ospf_link_cost(struct ospf_interface
*oi
)
412 /* RFC3137 stub router support */
413 if (!CHECK_FLAG(oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
414 return oi
->output_cost
;
416 return OSPF_OUTPUT_COST_INFINITE
;
419 /* Set a link information. */
420 static char link_info_set(struct stream
**s
, struct in_addr id
,
421 struct in_addr data
, uint8_t type
, uint8_t tos
,
424 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
425 * vast majority of cases. Some rare routers with lots of links need
427 * we try accomodate those here.
429 if (STREAM_WRITEABLE(*s
) < OSPF_ROUTER_LSA_LINK_SIZE
) {
430 size_t ret
= OSPF_MAX_LSA_SIZE
;
432 /* Can we enlarge the stream still? */
433 if (STREAM_SIZE(*s
) == OSPF_MAX_LSA_SIZE
) {
434 /* we futz the size here for simplicity, really we need
437 * IP Header - (sizeof (struct ip))
438 * OSPF Header - OSPF_HEADER_SIZE
439 * LSA Header - OSPF_LSA_HEADER_SIZE
440 * MD5 auth data, if MD5 is configured -
441 * OSPF_AUTH_MD5_SIZE.
443 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
445 ret
= stream_resize_inplace(
446 s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
449 if (ret
== OSPF_MAX_LSA_SIZE
) {
452 "%s: Out of space in LSA stream, left %zd, size %zd",
453 __func__
, STREAM_WRITEABLE(*s
),
459 /* TOS based routing is not supported. */
460 stream_put_ipv4(*s
, id
.s_addr
); /* Link ID. */
461 stream_put_ipv4(*s
, data
.s_addr
); /* Link Data. */
462 stream_putc(*s
, type
); /* Link Type. */
463 stream_putc(*s
, tos
); /* TOS = 0. */
464 stream_putw(*s
, cost
); /* Link Cost. */
469 /* Describe Point-to-Point link (Section 12.4.1.1). */
470 static int lsa_link_ptop_set(struct stream
**s
, struct ospf_interface
*oi
)
473 struct ospf_neighbor
*nbr
;
474 struct in_addr id
, mask
, data
;
475 uint16_t cost
= ospf_link_cost(oi
);
477 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
478 zlog_debug("LSA[Type1]: Set link Point-to-Point");
480 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
481 if (nbr
->state
== NSM_Full
) {
482 if (CHECK_FLAG(oi
->connected
->flags
,
483 ZEBRA_IFA_UNNUMBERED
)) {
484 /* For unnumbered point-to-point networks, the
486 should specify the interface's MIB-II ifIndex
488 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
489 links
+= link_info_set(
490 s
, nbr
->router_id
, data
,
491 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
493 links
+= link_info_set(
495 oi
->address
->u
.prefix4
,
496 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
500 /* no need for a stub link for unnumbered interfaces */
501 if (!CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)) {
502 /* Regardless of the state of the neighboring router, we must
503 add a Type 3 link (stub network).
504 N.B. Options 1 & 2 share basically the same logic. */
505 masklen2ip(oi
->address
->prefixlen
, &mask
);
506 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
508 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
515 /* Describe Broadcast Link. */
516 static int lsa_link_broadcast_set(struct stream
**s
, struct ospf_interface
*oi
)
518 struct ospf_neighbor
*dr
;
519 struct in_addr id
, mask
;
520 uint16_t cost
= ospf_link_cost(oi
);
522 /* Describe Type 3 Link. */
523 if (oi
->state
== ISM_Waiting
) {
524 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
526 "LSA[Type1]: Interface %s is in state Waiting. "
527 "Adding stub interface",
529 masklen2ip(oi
->address
->prefixlen
, &mask
);
530 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
531 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
535 dr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &DR(oi
));
536 /* Describe Type 2 link. */
537 if (dr
&& (dr
->state
== NSM_Full
538 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
)))
539 && ospf_nbr_count(oi
, NSM_Full
) > 0) {
540 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
542 "LSA[Type1]: Interface %s has a DR. "
543 "Adding transit interface",
545 return link_info_set(s
, DR(oi
), oi
->address
->u
.prefix4
,
546 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
548 /* Describe type 3 link. */
550 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
552 "LSA[Type1]: Interface %s has no DR. "
553 "Adding stub interface",
555 masklen2ip(oi
->address
->prefixlen
, &mask
);
556 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
557 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
562 static int lsa_link_loopback_set(struct stream
**s
, struct ospf_interface
*oi
)
564 struct in_addr id
, mask
;
566 /* Describe Type 3 Link. */
567 if (oi
->state
!= ISM_Loopback
)
570 mask
.s_addr
= 0xffffffff;
571 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
572 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
575 /* Describe Virtual Link. */
576 static int lsa_link_virtuallink_set(struct stream
**s
,
577 struct ospf_interface
*oi
)
579 struct ospf_neighbor
*nbr
;
580 uint16_t cost
= ospf_link_cost(oi
);
582 if (oi
->state
== ISM_PointToPoint
)
583 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
584 if (nbr
->state
== NSM_Full
) {
585 return link_info_set(s
, nbr
->router_id
,
586 oi
->address
->u
.prefix4
,
587 LSA_LINK_TYPE_VIRTUALLINK
,
594 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
596 /* this function add for support point-to-multipoint ,see rfc2328
598 /* from "edward rrr" <edward_rrr@hotmail.com>
599 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
600 static int lsa_link_ptomp_set(struct stream
**s
, struct ospf_interface
*oi
)
603 struct route_node
*rn
;
604 struct ospf_neighbor
*nbr
= NULL
;
605 struct in_addr id
, mask
;
606 uint16_t cost
= ospf_link_cost(oi
);
608 mask
.s_addr
= 0xffffffff;
609 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
610 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
612 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
613 zlog_debug("PointToMultipoint: running ptomultip_set");
615 /* Search neighbor, */
616 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
617 if ((nbr
= rn
->info
) != NULL
)
619 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
620 &oi
->ospf
->router_id
))
621 if (nbr
->state
== NSM_Full
)
624 links
+= link_info_set(
626 oi
->address
->u
.prefix4
,
627 LSA_LINK_TYPE_POINTOPOINT
, 0,
629 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
631 "PointToMultipoint: set link to %s",
640 /* Set router-LSA link information. */
641 static int router_lsa_link_set(struct stream
**s
, struct ospf_area
*area
)
643 struct listnode
*node
;
644 struct ospf_interface
*oi
;
647 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
648 struct interface
*ifp
= oi
->ifp
;
650 /* Check interface is up, OSPF is enable. */
651 if (if_is_operative(ifp
)) {
652 if (oi
->state
!= ISM_Down
) {
653 oi
->lsa_pos_beg
= links
;
654 /* Describe each link. */
656 case OSPF_IFTYPE_POINTOPOINT
:
657 links
+= lsa_link_ptop_set(s
, oi
);
659 case OSPF_IFTYPE_BROADCAST
:
660 links
+= lsa_link_broadcast_set(s
, oi
);
662 case OSPF_IFTYPE_NBMA
:
663 links
+= lsa_link_nbma_set(s
, oi
);
665 case OSPF_IFTYPE_POINTOMULTIPOINT
:
666 links
+= lsa_link_ptomp_set(s
, oi
);
668 case OSPF_IFTYPE_VIRTUALLINK
:
670 lsa_link_virtuallink_set(s
, oi
);
672 case OSPF_IFTYPE_LOOPBACK
:
673 links
+= lsa_link_loopback_set(s
, oi
);
675 oi
->lsa_pos_end
= links
;
683 /* Set router-LSA body. */
684 static void ospf_router_lsa_body_set(struct stream
**s
, struct ospf_area
*area
)
690 stream_putc(*s
, router_lsa_flags(area
));
692 /* Set Zero fields. */
695 /* Keep pointer to # links. */
696 putp
= stream_get_endp(*s
);
701 /* Set all link information. */
702 cnt
= router_lsa_link_set(s
, area
);
704 /* Set # of links here. */
705 stream_putw_at(*s
, putp
, cnt
);
708 static int ospf_stub_router_timer(struct thread
*t
)
710 struct ospf_area
*area
= THREAD_ARG(t
);
712 area
->t_stub_router
= NULL
;
714 SET_FLAG(area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
716 /* clear stub route state and generate router-lsa refresh, don't
717 * clobber an administratively set stub-router state though.
719 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
722 UNSET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
724 ospf_router_lsa_update_area(area
);
729 static void ospf_stub_router_check(struct ospf_area
*area
)
731 /* area must either be administratively configured to be stub
732 * or startup-time stub-router must be configured and we must in a
736 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
)) {
737 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
741 /* not admin-stubbed, check whether startup stubbing is configured and
742 * whether it's not been done yet
744 if (CHECK_FLAG(area
->stub_router_state
,
745 OSPF_AREA_WAS_START_STUB_ROUTED
))
748 if (area
->ospf
->stub_router_startup_time
749 == OSPF_STUB_ROUTER_UNCONFIGURED
) {
750 /* stub-router is hence done forever for this area, even if
752 * tries configure it (take effect next restart).
754 SET_FLAG(area
->stub_router_state
,
755 OSPF_AREA_WAS_START_STUB_ROUTED
);
759 /* startup stub-router configured and not yet done */
760 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
762 OSPF_AREA_TIMER_ON(area
->t_stub_router
, ospf_stub_router_timer
,
763 area
->ospf
->stub_router_startup_time
);
766 /* Create new router-LSA. */
767 static struct ospf_lsa
*ospf_router_lsa_new(struct ospf_area
*area
)
769 struct ospf
*ospf
= area
->ospf
;
771 struct lsa_header
*lsah
;
772 struct ospf_lsa
*new;
775 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
776 zlog_debug("LSA[Type1]: Create router-LSA instance");
778 /* check whether stub-router is desired, and if this is the first
781 ospf_stub_router_check(area
);
783 /* Create a stream for LSA. */
784 s
= stream_new(OSPF_MAX_LSA_SIZE
);
785 /* Set LSA common header fields. */
786 lsa_header_set(s
, LSA_OPTIONS_GET(area
) | LSA_OPTIONS_NSSA_GET(area
),
787 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
789 /* Set router-LSA body fields. */
790 ospf_router_lsa_body_set(&s
, area
);
793 length
= stream_get_endp(s
);
794 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
795 lsah
->length
= htons(length
);
797 /* Now, create OSPF LSA instance. */
798 new = ospf_lsa_new_and_data(length
);
801 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
802 new->vrf_id
= area
->ospf
->vrf_id
;
804 /* Copy LSA data to store, discard stream. */
805 memcpy(new->data
, lsah
, length
);
811 /* Originate Router-LSA. */
812 static struct ospf_lsa
*ospf_router_lsa_originate(struct ospf_area
*area
)
814 struct ospf_lsa
*new;
816 /* Create new router-LSA instance. */
817 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
818 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
823 if (new->data
->adv_router
.s_addr
== 0) {
824 if (IS_DEBUG_OSPF_EVENT
)
825 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
826 ospf_lsa_discard(new);
830 /* Install LSA to LSDB. */
831 new = ospf_lsa_install(area
->ospf
, NULL
, new);
833 /* Update LSA origination count. */
834 area
->ospf
->lsa_originate_count
++;
836 /* Flooding new LSA through area. */
837 ospf_flood_through_area(area
, NULL
, new);
839 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
840 zlog_debug("LSA[Type%d:%s]: Originate router-LSA %p",
841 new->data
->type
, inet_ntoa(new->data
->id
),
843 ospf_lsa_header_dump(new->data
);
849 /* Refresh router-LSA. */
850 static struct ospf_lsa
*ospf_router_lsa_refresh(struct ospf_lsa
*lsa
)
852 struct ospf_area
*area
= lsa
->area
;
853 struct ospf_lsa
*new;
858 /* Delete LSA from neighbor retransmit-list. */
859 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
861 /* Unregister LSA from refresh-list */
862 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
864 /* Create new router-LSA instance. */
865 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
866 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
870 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
872 ospf_lsa_install(area
->ospf
, NULL
, new);
874 /* Flood LSA through area. */
875 ospf_flood_through_area(area
, NULL
, new);
878 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
879 zlog_debug("LSA[Type%d:%s]: router-LSA refresh",
880 new->data
->type
, inet_ntoa(new->data
->id
));
881 ospf_lsa_header_dump(new->data
);
887 int ospf_router_lsa_update_area(struct ospf_area
*area
)
889 if (IS_DEBUG_OSPF_EVENT
)
890 zlog_debug("[router-LSA]: (router-LSA area update)");
892 /* Now refresh router-LSA. */
893 if (area
->router_lsa_self
)
894 ospf_lsa_refresh(area
->ospf
, area
->router_lsa_self
);
895 /* Newly originate router-LSA. */
897 ospf_router_lsa_originate(area
);
902 int ospf_router_lsa_update(struct ospf
*ospf
)
904 struct listnode
*node
, *nnode
;
905 struct ospf_area
*area
;
907 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
908 zlog_debug("Timer[router-LSA Update]: (timer expire)");
910 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
911 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
912 struct router_lsa
*rl
;
913 const char *area_str
;
915 /* Keep Area ID string. */
916 area_str
= AREA_NAME(area
);
918 /* If LSA not exist in this Area, originate new. */
920 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
922 "LSA[Type1]: Create router-LSA for Area %s",
925 ospf_router_lsa_originate(area
);
927 /* If router-ID is changed, Link ID must change.
928 First flush old LSA, then originate new. */
929 else if (!IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
)) {
930 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
932 "LSA[Type%d:%s]: Refresh router-LSA for Area %s",
934 inet_ntoa(lsa
->data
->id
), area_str
);
935 ospf_refresher_unregister_lsa(ospf
, lsa
);
936 ospf_lsa_flush_area(lsa
, area
);
937 ospf_lsa_unlock(&area
->router_lsa_self
);
938 area
->router_lsa_self
= NULL
;
940 /* Refresh router-LSA, (not install) and flood through
942 ospf_router_lsa_update_area(area
);
944 rl
= (struct router_lsa
*)lsa
->data
;
945 /* Refresh router-LSA, (not install) and flood through
947 if (rl
->flags
!= ospf
->flags
)
948 ospf_router_lsa_update_area(area
);
956 /* network-LSA related functions. */
957 /* Originate Network-LSA. */
958 static void ospf_network_lsa_body_set(struct stream
*s
,
959 struct ospf_interface
*oi
)
962 struct route_node
*rn
;
963 struct ospf_neighbor
*nbr
;
965 masklen2ip(oi
->address
->prefixlen
, &mask
);
966 stream_put_ipv4(s
, mask
.s_addr
);
968 /* The network-LSA lists those routers that are fully adjacent to
969 the Designated Router; each fully adjacent router is identified by
970 its OSPF Router ID. The Designated Router includes itself in this
971 list. RFC2328, Section 12.4.2 */
973 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
974 if ((nbr
= rn
->info
) != NULL
)
975 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
976 stream_put_ipv4(s
, nbr
->router_id
.s_addr
);
979 static struct ospf_lsa
*ospf_network_lsa_new(struct ospf_interface
*oi
)
982 struct ospf_lsa
*new;
983 struct lsa_header
*lsah
;
984 struct ospf_if_params
*oip
;
987 /* If there are no neighbours on this network (the net is stub),
988 the router does not originate network-LSA (see RFC 12.4.2) */
989 if (oi
->full_nbrs
== 0)
992 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
993 zlog_debug("LSA[Type2]: Create network-LSA instance");
995 /* Create new stream for LSA. */
996 s
= stream_new(OSPF_MAX_LSA_SIZE
);
997 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
999 lsa_header_set(s
, (OPTIONS(oi
) | LSA_OPTIONS_GET(oi
->area
)),
1000 OSPF_NETWORK_LSA
, DR(oi
), oi
->ospf
->router_id
);
1002 /* Set network-LSA body fields. */
1003 ospf_network_lsa_body_set(s
, oi
);
1006 length
= stream_get_endp(s
);
1007 lsah
->length
= htons(length
);
1009 /* Create OSPF LSA instance. */
1010 new = ospf_lsa_new_and_data(length
);
1012 new->area
= oi
->area
;
1013 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1014 new->vrf_id
= oi
->ospf
->vrf_id
;
1016 /* Copy LSA to store. */
1017 memcpy(new->data
, lsah
, length
);
1020 /* Remember prior network LSA sequence numbers, even if we stop
1021 * originating one for this oi, to try avoid re-originating LSAs with a
1022 * prior sequence number, and thus speed up adjency forming &
1025 if ((oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
))) {
1026 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1027 new->data
->ls_seqnum
= lsa_seqnum_increment(new);
1029 oip
= ospf_get_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1030 ospf_if_update_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1032 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1037 /* Originate network-LSA. */
1038 void ospf_network_lsa_update(struct ospf_interface
*oi
)
1040 struct ospf_lsa
*new;
1042 if (oi
->network_lsa_self
!= NULL
) {
1043 ospf_lsa_refresh(oi
->ospf
, oi
->network_lsa_self
);
1047 /* Create new network-LSA instance. */
1048 new = ospf_network_lsa_new(oi
);
1052 /* Install LSA to LSDB. */
1053 new = ospf_lsa_install(oi
->ospf
, oi
, new);
1055 /* Update LSA origination count. */
1056 oi
->ospf
->lsa_originate_count
++;
1058 /* Flooding new LSA through area. */
1059 ospf_flood_through_area(oi
->area
, NULL
, new);
1061 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1062 zlog_debug("LSA[Type%d:%s]: Originate network-LSA %p",
1063 new->data
->type
, inet_ntoa(new->data
->id
),
1065 ospf_lsa_header_dump(new->data
);
1071 static struct ospf_lsa
*ospf_network_lsa_refresh(struct ospf_lsa
*lsa
)
1073 struct ospf_area
*area
= lsa
->area
;
1074 struct ospf_lsa
*new, *new2
;
1075 struct ospf_if_params
*oip
;
1076 struct ospf_interface
*oi
;
1080 /* Retrieve the oi for the network LSA */
1081 oi
= ospf_if_lookup_by_local_addr(area
->ospf
, NULL
, lsa
->data
->id
);
1083 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1085 "LSA[Type%d:%s]: network-LSA refresh: "
1086 "no oi found, ick, ignoring.",
1087 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
1088 ospf_lsa_header_dump(lsa
->data
);
1092 /* Delete LSA from neighbor retransmit-list. */
1093 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
1095 /* Unregister LSA from refresh-list */
1096 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
1098 /* Create new network-LSA instance. */
1099 new = ospf_network_lsa_new(oi
);
1103 oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1104 assert(oip
!= NULL
);
1105 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
=
1106 lsa_seqnum_increment(lsa
);
1108 new2
= ospf_lsa_install(area
->ospf
, oi
, new);
1110 assert(new2
== new);
1112 /* Flood LSA through aera. */
1113 ospf_flood_through_area(area
, NULL
, new);
1115 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1116 zlog_debug("LSA[Type%d:%s]: network-LSA refresh",
1117 new->data
->type
, inet_ntoa(new->data
->id
));
1118 ospf_lsa_header_dump(new->data
);
1124 static void stream_put_ospf_metric(struct stream
*s
, uint32_t metric_value
)
1129 /* Put 0 metric. TOS metric is not supported. */
1130 metric
= htonl(metric_value
);
1131 mp
= (char *)&metric
;
1133 stream_put(s
, mp
, 3);
1136 /* summary-LSA related functions. */
1137 static void ospf_summary_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1140 struct in_addr mask
;
1142 masklen2ip(p
->prefixlen
, &mask
);
1144 /* Put Network Mask. */
1145 stream_put_ipv4(s
, mask
.s_addr
);
1148 stream_putc(s
, (uint8_t)0);
1151 stream_put_ospf_metric(s
, metric
);
1154 static struct ospf_lsa
*ospf_summary_lsa_new(struct ospf_area
*area
,
1155 struct prefix
*p
, uint32_t metric
,
1159 struct ospf_lsa
*new;
1160 struct lsa_header
*lsah
;
1163 if (id
.s_addr
== 0xffffffff) {
1164 /* Maybe Link State ID not available. */
1165 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1167 "LSA[Type%d]: Link ID not available, can't originate",
1172 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1173 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1175 /* Create new stream for LSA. */
1176 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1177 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1179 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_SUMMARY_LSA
, id
,
1180 area
->ospf
->router_id
);
1182 /* Set summary-LSA body fields. */
1183 ospf_summary_lsa_body_set(s
, p
, metric
);
1186 length
= stream_get_endp(s
);
1187 lsah
->length
= htons(length
);
1189 /* Create OSPF LSA instance. */
1190 new = ospf_lsa_new_and_data(length
);
1192 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1193 new->vrf_id
= area
->ospf
->vrf_id
;
1195 /* Copy LSA to store. */
1196 memcpy(new->data
, lsah
, length
);
1202 /* Originate Summary-LSA. */
1203 struct ospf_lsa
*ospf_summary_lsa_originate(struct prefix_ipv4
*p
,
1205 struct ospf_area
*area
)
1207 struct ospf_lsa
*new;
1210 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
);
1212 if (id
.s_addr
== 0xffffffff) {
1213 /* Maybe Link State ID not available. */
1214 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1216 "LSA[Type%d]: Link ID not available, can't originate",
1221 /* Create new summary-LSA instance. */
1222 if (!(new = ospf_summary_lsa_new(area
, (struct prefix
*)p
, metric
, id
)))
1225 /* Instlal LSA to LSDB. */
1226 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1228 /* Update LSA origination count. */
1229 area
->ospf
->lsa_originate_count
++;
1231 /* Flooding new LSA through area. */
1232 ospf_flood_through_area(area
, NULL
, new);
1234 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1235 zlog_debug("LSA[Type%d:%s]: Originate summary-LSA %p",
1236 new->data
->type
, inet_ntoa(new->data
->id
),
1238 ospf_lsa_header_dump(new->data
);
1244 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
1245 struct ospf_lsa
*lsa
)
1247 struct ospf_lsa
*new;
1248 struct summary_lsa
*sl
;
1254 sl
= (struct summary_lsa
*)lsa
->data
;
1255 p
.prefixlen
= ip_masklen(sl
->mask
);
1256 new = ospf_summary_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1262 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1264 ospf_lsa_install(ospf
, NULL
, new);
1266 /* Flood LSA through AS. */
1267 ospf_flood_through_area(new->area
, NULL
, new);
1269 /* Debug logging. */
1270 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1271 zlog_debug("LSA[Type%d:%s]: summary-LSA refresh",
1272 new->data
->type
, inet_ntoa(new->data
->id
));
1273 ospf_lsa_header_dump(new->data
);
1280 /* summary-ASBR-LSA related functions. */
1281 static void ospf_summary_asbr_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1284 /* Put Network Mask. */
1285 stream_put_ipv4(s
, (uint32_t)0);
1288 stream_putc(s
, (uint8_t)0);
1291 stream_put_ospf_metric(s
, metric
);
1294 static struct ospf_lsa
*ospf_summary_asbr_lsa_new(struct ospf_area
*area
,
1300 struct ospf_lsa
*new;
1301 struct lsa_header
*lsah
;
1304 if (id
.s_addr
== 0xffffffff) {
1305 /* Maybe Link State ID not available. */
1306 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1308 "LSA[Type%d]: Link ID not available, can't originate",
1309 OSPF_ASBR_SUMMARY_LSA
);
1313 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1314 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1316 /* Create new stream for LSA. */
1317 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1318 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1320 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_ASBR_SUMMARY_LSA
, id
,
1321 area
->ospf
->router_id
);
1323 /* Set summary-LSA body fields. */
1324 ospf_summary_asbr_lsa_body_set(s
, p
, metric
);
1327 length
= stream_get_endp(s
);
1328 lsah
->length
= htons(length
);
1330 /* Create OSPF LSA instance. */
1331 new = ospf_lsa_new_and_data(length
);
1333 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1334 new->vrf_id
= area
->ospf
->vrf_id
;
1336 /* Copy LSA to store. */
1337 memcpy(new->data
, lsah
, length
);
1343 /* Originate summary-ASBR-LSA. */
1344 struct ospf_lsa
*ospf_summary_asbr_lsa_originate(struct prefix_ipv4
*p
,
1346 struct ospf_area
*area
)
1348 struct ospf_lsa
*new;
1351 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_ASBR_SUMMARY_LSA
,
1354 if (id
.s_addr
== 0xffffffff) {
1355 /* Maybe Link State ID not available. */
1356 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1358 "LSA[Type%d]: Link ID not available, can't originate",
1359 OSPF_ASBR_SUMMARY_LSA
);
1363 /* Create new summary-LSA instance. */
1364 new = ospf_summary_asbr_lsa_new(area
, (struct prefix
*)p
, metric
, id
);
1368 /* Install LSA to LSDB. */
1369 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1371 /* Update LSA origination count. */
1372 area
->ospf
->lsa_originate_count
++;
1374 /* Flooding new LSA through area. */
1375 ospf_flood_through_area(area
, NULL
, new);
1377 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1378 zlog_debug("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1379 new->data
->type
, inet_ntoa(new->data
->id
),
1381 ospf_lsa_header_dump(new->data
);
1387 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
1388 struct ospf_lsa
*lsa
)
1390 struct ospf_lsa
*new;
1391 struct summary_lsa
*sl
;
1397 sl
= (struct summary_lsa
*)lsa
->data
;
1398 p
.prefixlen
= ip_masklen(sl
->mask
);
1399 new = ospf_summary_asbr_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1404 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1406 ospf_lsa_install(ospf
, NULL
, new);
1408 /* Flood LSA through area. */
1409 ospf_flood_through_area(new->area
, NULL
, new);
1411 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1412 zlog_debug("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1413 new->data
->type
, inet_ntoa(new->data
->id
));
1414 ospf_lsa_header_dump(new->data
);
1420 /* AS-external-LSA related functions. */
1422 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1423 is connected, else 0*/
1424 static struct in_addr
ospf_external_lsa_nexthop_get(struct ospf
*ospf
,
1425 struct in_addr nexthop
)
1429 struct listnode
*node
;
1430 struct ospf_interface
*oi
;
1434 if (!nexthop
.s_addr
)
1437 /* Check whether nexthop is covered by OSPF network. */
1438 nh
.family
= AF_INET
;
1439 nh
.u
.prefix4
= nexthop
;
1440 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1442 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1443 * better to make use of the per-ifp table of ois.
1445 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1446 if (if_is_operative(oi
->ifp
))
1447 if (oi
->address
->family
== AF_INET
)
1448 if (prefix_match(oi
->address
, &nh
))
1454 /* NSSA-external-LSA related functions. */
1456 /* Get 1st IP connection for Forward Addr */
1458 struct in_addr
ospf_get_ip_from_ifp(struct ospf_interface
*oi
)
1464 if (if_is_operative(oi
->ifp
))
1465 return oi
->address
->u
.prefix4
;
1470 /* Get 1st IP connection for Forward Addr */
1471 struct in_addr
ospf_get_nssa_ip(struct ospf_area
*area
)
1474 struct in_addr best_default
;
1475 struct listnode
*node
;
1476 struct ospf_interface
*oi
;
1479 best_default
.s_addr
= 0;
1481 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->oiflist
, node
, oi
)) {
1482 if (if_is_operative(oi
->ifp
))
1483 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1485 && oi
->address
->family
== AF_INET
) {
1486 if (best_default
.s_addr
== 0)
1488 oi
->address
->u
.prefix4
;
1489 if (oi
->area
== area
)
1490 return oi
->address
->u
.prefix4
;
1493 if (best_default
.s_addr
!= 0)
1494 return best_default
;
1496 if (best_default
.s_addr
!= 0)
1497 return best_default
;
1502 #define DEFAULT_DEFAULT_METRIC 20
1503 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1504 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1506 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1508 int metric_type(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1510 struct ospf_redist
*red
;
1512 red
= ospf_redist_lookup(ospf
, src
, instance
);
1514 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
1515 : red
->dmetric
.type
);
1518 int metric_value(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1520 struct ospf_redist
*red
;
1522 red
= ospf_redist_lookup(ospf
, src
, instance
);
1523 if (!red
|| red
->dmetric
.value
< 0) {
1524 if (src
== DEFAULT_ROUTE
) {
1525 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1526 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1528 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1529 } else if (ospf
->default_metric
< 0)
1530 return DEFAULT_DEFAULT_METRIC
;
1532 return ospf
->default_metric
;
1535 return red
->dmetric
.value
;
1538 /* Set AS-external-LSA body. */
1539 static void ospf_external_lsa_body_set(struct stream
*s
,
1540 struct external_info
*ei
,
1543 struct prefix_ipv4
*p
= &ei
->p
;
1544 struct in_addr mask
, fwd_addr
;
1548 unsigned short instance
;
1550 /* Put Network Mask. */
1551 masklen2ip(p
->prefixlen
, &mask
);
1552 stream_put_ipv4(s
, mask
.s_addr
);
1554 /* If prefix is default, specify DEFAULT_ROUTE. */
1555 type
= is_prefix_default(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1556 instance
= is_prefix_default(&ei
->p
) ? 0 : ei
->instance
;
1558 mtype
= (ROUTEMAP_METRIC_TYPE(ei
) != -1)
1559 ? ROUTEMAP_METRIC_TYPE(ei
)
1560 : metric_type(ospf
, type
, instance
);
1562 mvalue
= (ROUTEMAP_METRIC(ei
) != -1)
1563 ? ROUTEMAP_METRIC(ei
)
1564 : metric_value(ospf
, type
, instance
);
1566 /* Put type of external metric. */
1567 stream_putc(s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1569 /* Put 0 metric. TOS metric is not supported. */
1570 stream_put_ospf_metric(s
, mvalue
);
1572 /* Get forwarding address to nexthop if on the Connection List, else 0.
1574 fwd_addr
= ospf_external_lsa_nexthop_get(ospf
, ei
->nexthop
);
1576 /* Put forwarding address. */
1577 stream_put_ipv4(s
, fwd_addr
.s_addr
);
1580 stream_putl(s
, ei
->tag
);
1583 /* Create new external-LSA. */
1584 static struct ospf_lsa
*ospf_external_lsa_new(struct ospf
*ospf
,
1585 struct external_info
*ei
,
1586 struct in_addr
*old_id
)
1589 struct lsa_header
*lsah
;
1590 struct ospf_lsa
*new;
1595 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1597 "LSA[Type5]: External info is NULL, can't originate");
1601 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1602 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1604 /* If old Link State ID is specified, refresh LSA with same ID. */
1607 /* Get Link State with unique ID. */
1609 id
= ospf_lsa_unique_id(ospf
, ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
,
1611 if (id
.s_addr
== 0xffffffff) {
1612 /* Maybe Link State ID not available. */
1613 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1615 "LSA[Type5]: Link ID not available, can't originate");
1620 /* Create new stream for LSA. */
1621 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1622 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1624 /* Set LSA common header fields. */
1625 lsa_header_set(s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
, id
,
1628 /* Set AS-external-LSA body fields. */
1629 ospf_external_lsa_body_set(s
, ei
, ospf
);
1632 length
= stream_get_endp(s
);
1633 lsah
->length
= htons(length
);
1635 /* Now, create OSPF LSA instance. */
1636 new = ospf_lsa_new_and_data(length
);
1638 SET_FLAG(new->flags
,
1639 OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1640 new->vrf_id
= ospf
->vrf_id
;
1642 /* Copy LSA data to store, discard stream. */
1643 memcpy(new->data
, lsah
, length
);
1650 static void ospf_install_flood_nssa(struct ospf
*ospf
, struct ospf_lsa
*lsa
,
1651 struct external_info
*ei
)
1653 struct ospf_lsa
*new;
1654 struct as_external_lsa
*extlsa
;
1655 struct ospf_area
*area
;
1656 struct listnode
*node
, *nnode
;
1658 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1659 * which originated from an NSSA area. In which case it should not be
1660 * flooded back to NSSA areas.
1662 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1665 /* NSSA Originate or Refresh (If anyNSSA)
1667 LSA is self-originated. And just installed as Type-5.
1668 Additionally, install as Type-7 LSDB for every attached NSSA.
1670 P-Bit controls which ABR performs translation to outside world; If
1671 we are an ABR....do not set the P-bit, because we send the Type-5,
1672 not as the ABR Translator, but as the ASBR owner within the AS!
1674 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1675 elected ABR Translator will see the P-bit, Translate, and re-flood.
1677 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1678 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1680 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
1681 /* Don't install Type-7 LSA's into nonNSSA area */
1682 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1685 /* make lsa duplicate, lock=1 */
1686 new = ospf_lsa_dup(lsa
);
1688 new->data
->type
= OSPF_AS_NSSA_LSA
;
1690 /* set P-bit if not ABR */
1691 if (!IS_OSPF_ABR(ospf
)) {
1692 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1694 /* set non-zero FWD ADDR
1696 draft-ietf-ospf-nssa-update-09.txt
1698 if the network between the NSSA AS boundary router and
1700 adjacent AS is advertised into OSPF as an internal OSPF
1702 the forwarding address should be the next op address as
1704 currently done with type-5 LSAs. If the intervening
1706 not adversited into OSPF as an internal OSPF route and
1708 type-7 LSA's P-bit is set a forwarding address should be
1709 selected from one of the router's active OSPF interface
1711 which belong to the NSSA. If no such addresses exist,
1713 no type-7 LSA's with the P-bit set should originate from
1717 /* kevinm: not updating lsa anymore, just new */
1718 extlsa
= (struct as_external_lsa
*)(new->data
);
1720 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0)
1721 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(
1722 area
); /* this NSSA area in ifp */
1724 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0) {
1725 if (IS_DEBUG_OSPF_NSSA
)
1727 "LSA[Type-7]: Could not build FWD-ADDR");
1728 ospf_lsa_discard(new);
1733 /* install also as Type-7 */
1734 ospf_lsa_install(ospf
, NULL
,
1735 new); /* Remove Old, Lock New = 2 */
1737 /* will send each copy, lock=2+n */
1738 ospf_flood_through_as(
1739 ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1743 static struct ospf_lsa
*ospf_lsa_translated_nssa_new(struct ospf
*ospf
,
1744 struct ospf_lsa
*type7
)
1747 struct ospf_lsa
*new;
1748 struct as_external_lsa
*ext
, *extnew
;
1749 struct external_info ei
;
1751 ext
= (struct as_external_lsa
*)(type7
->data
);
1753 /* need external_info struct, fill in bare minimum */
1754 ei
.p
.family
= AF_INET
;
1755 ei
.p
.prefix
= type7
->data
->id
;
1756 ei
.p
.prefixlen
= ip_masklen(ext
->mask
);
1757 ei
.type
= ZEBRA_ROUTE_OSPF
;
1758 ei
.nexthop
= ext
->header
.adv_router
;
1759 ei
.route_map_set
.metric
= -1;
1760 ei
.route_map_set
.metric_type
= -1;
1764 if ((new = ospf_external_lsa_new(ospf
, &ei
, &type7
->data
->id
))
1766 if (IS_DEBUG_OSPF_NSSA
)
1768 "ospf_nssa_translate_originate(): Could not originate "
1769 "Translated Type-5 for %s",
1770 inet_ntoa(ei
.p
.prefix
));
1774 extnew
= (struct as_external_lsa
*)(new->data
);
1776 /* copy over Type-7 data to new */
1777 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1778 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1779 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1780 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1782 /* add translated flag, checksum and lock new lsa */
1783 SET_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1784 new = ospf_lsa_lock(new);
1789 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1790 struct ospf_lsa
*ospf_translated_nssa_originate(struct ospf
*ospf
,
1791 struct ospf_lsa
*type7
)
1793 struct ospf_lsa
*new;
1794 struct as_external_lsa
*extnew
;
1796 /* we cant use ospf_external_lsa_originate() as we need to set
1797 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1800 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1801 if (IS_DEBUG_OSPF_NSSA
)
1803 "ospf_translated_nssa_originate(): Could not translate "
1804 "Type-7, Id %s, to Type-5",
1805 inet_ntoa(type7
->data
->id
));
1809 extnew
= (struct as_external_lsa
*)new;
1811 if (IS_DEBUG_OSPF_NSSA
) {
1813 "ospf_translated_nssa_originate(): "
1814 "translated Type 7, installed:");
1815 ospf_lsa_header_dump(new->data
);
1816 zlog_debug(" Network mask: %d", ip_masklen(extnew
->mask
));
1817 zlog_debug(" Forward addr: %s",
1818 inet_ntoa(extnew
->e
[0].fwd_addr
));
1821 if ((new = ospf_lsa_install(ospf
, NULL
, new)) == NULL
) {
1822 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
1823 "ospf_lsa_translated_nssa_originate(): "
1824 "Could not install LSA "
1826 inet_ntoa(type7
->data
->id
));
1830 ospf
->lsa_originate_count
++;
1831 ospf_flood_through_as(ospf
, NULL
, new);
1836 /* Refresh Translated from NSSA AS-external-LSA. */
1837 struct ospf_lsa
*ospf_translated_nssa_refresh(struct ospf
*ospf
,
1838 struct ospf_lsa
*type7
,
1839 struct ospf_lsa
*type5
)
1841 struct ospf_lsa
*new = NULL
;
1843 /* Sanity checks. */
1844 assert(type7
|| type5
);
1845 if (!(type7
|| type5
))
1848 assert(type7
->data
);
1850 assert(type5
->data
);
1851 assert(ospf
->anyNSSA
);
1853 /* get required data according to what has been given */
1854 if (type7
&& type5
== NULL
) {
1855 /* find the translated Type-5 for this Type-7 */
1856 struct as_external_lsa
*ext
=
1857 (struct as_external_lsa
*)(type7
->data
);
1858 struct prefix_ipv4 p
= {
1859 .prefix
= type7
->data
->id
,
1860 .prefixlen
= ip_masklen(ext
->mask
),
1864 type5
= ospf_external_info_find_lsa(ospf
, &p
);
1865 } else if (type5
&& type7
== NULL
) {
1866 /* find the type-7 from which supplied type-5 was translated,
1867 * ie find first type-7 with same LSA Id.
1869 struct listnode
*ln
, *lnn
;
1870 struct route_node
*rn
;
1871 struct ospf_lsa
*lsa
;
1872 struct ospf_area
*area
;
1874 for (ALL_LIST_ELEMENTS(ospf
->areas
, ln
, lnn
, area
)) {
1875 if (area
->external_routing
!= OSPF_AREA_NSSA
&& !type7
)
1878 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
) {
1879 if (lsa
->data
->id
.s_addr
1880 == type5
->data
->id
.s_addr
) {
1888 /* do we have type7? */
1890 if (IS_DEBUG_OSPF_NSSA
)
1892 "ospf_translated_nssa_refresh(): no Type-7 found for "
1894 inet_ntoa(type5
->data
->id
));
1898 /* do we have valid translated type5? */
1899 if (type5
== NULL
|| !CHECK_FLAG(type5
->flags
, OSPF_LSA_LOCAL_XLT
)) {
1900 if (IS_DEBUG_OSPF_NSSA
)
1902 "ospf_translated_nssa_refresh(): No translated Type-5 "
1903 "found for Type-7 with Id %s",
1904 inet_ntoa(type7
->data
->id
));
1908 /* Delete LSA from neighbor retransmit-list. */
1909 ospf_ls_retransmit_delete_nbr_as(ospf
, type5
);
1911 /* create new translated LSA */
1912 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1913 if (IS_DEBUG_OSPF_NSSA
)
1915 "ospf_translated_nssa_refresh(): Could not translate "
1916 "Type-7 for %s to Type-5",
1917 inet_ntoa(type7
->data
->id
));
1921 if (!(new = ospf_lsa_install(ospf
, NULL
, new))) {
1923 EC_OSPF_LSA_INSTALL_FAILURE
,
1924 "ospf_translated_nssa_refresh(): Could not install translated LSA, Id %s",
1925 inet_ntoa(type7
->data
->id
));
1929 /* Flood LSA through area. */
1930 ospf_flood_through_as(ospf
, NULL
, new);
1935 int is_prefix_default(struct prefix_ipv4
*p
)
1937 struct prefix_ipv4 q
;
1940 q
.prefix
.s_addr
= 0;
1943 return prefix_same((struct prefix
*)p
, (struct prefix
*)&q
);
1946 /* Originate an AS-external-LSA, install and flood. */
1947 struct ospf_lsa
*ospf_external_lsa_originate(struct ospf
*ospf
,
1948 struct external_info
*ei
)
1950 struct ospf_lsa
*new;
1952 /* Added for NSSA project....
1954 External LSAs are originated in ASBRs as usual, but for NSSA
1956 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1957 every area. The Type-7's are flooded to every IR and every ABR; We
1958 install the Type-5 LSDB so that the normal "refresh" code operates
1959 as usual, and flag them as not used during ASE calculations. The
1960 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
1961 Address of non-zero.
1963 If an ABR is the elected NSSA translator, following SPF and during
1964 the ABR task it will translate all the scanned Type-7's, with P-bit
1965 ON and not-self generated, and translate to Type-5's throughout the
1968 A difference in operation depends whether this ASBR is an ABR
1969 or not. If not an ABR, the P-bit is ON, to indicate that any
1970 elected NSSA-ABR can perform its translation.
1972 If an ABR, the P-bit is OFF; No ABR will perform translation and
1973 this ASBR will flood the Type-5 LSA as usual.
1975 For the case where this ASBR is not an ABR, the ASE calculations
1976 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
1977 demonstrate to the user that there are LSA's that belong to any
1980 Finally, it just so happens that when the ABR is translating every
1981 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
1982 approved Type-5 (translated from Type-7); at the end of translation
1983 if any Translated Type-5's remain unapproved, then they must be
1984 flushed from the AS.
1988 /* Check the AS-external-LSA should be originated. */
1989 if (!ospf_redistribute_check(ospf
, ei
, NULL
))
1992 /* Create new AS-external-LSA instance. */
1993 if ((new = ospf_external_lsa_new(ospf
, ei
, NULL
)) == NULL
) {
1994 if (IS_DEBUG_OSPF_EVENT
)
1996 "LSA[Type5:%s]: Could not originate AS-external-LSA",
1997 inet_ntoa(ei
->p
.prefix
));
2001 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2002 ospf_lsa_install(ospf
, NULL
, new);
2004 /* Update LSA origination count. */
2005 ospf
->lsa_originate_count
++;
2007 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2008 ospf_flood_through_as(ospf
, NULL
, new);
2010 /* If there is any attached NSSA, do special handling */
2011 if (ospf
->anyNSSA
&&
2012 /* stay away from translated LSAs! */
2013 !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2014 ospf_install_flood_nssa(
2015 ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2017 /* Debug logging. */
2018 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2019 zlog_debug("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2020 new->data
->type
, inet_ntoa(new->data
->id
),
2022 ospf_lsa_header_dump(new->data
);
2028 /* Originate AS-external-LSA from external info with initial flag. */
2029 int ospf_external_lsa_originate_timer(struct thread
*thread
)
2031 struct ospf
*ospf
= THREAD_ARG(thread
);
2032 struct route_node
*rn
;
2033 struct external_info
*ei
;
2034 struct route_table
*rt
;
2035 int type
= THREAD_VAL(thread
);
2036 struct list
*ext_list
;
2037 struct listnode
*node
;
2038 struct ospf_external
*ext
;
2040 ospf
->t_external_lsa
= NULL
;
2042 ext_list
= ospf
->external
[type
];
2046 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2047 /* Originate As-external-LSA from all type of distribute source.
2049 rt
= ext
->external_info
;
2053 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
2059 if (is_prefix_default((struct prefix_ipv4
*)&ei
->p
))
2062 if (!ospf_external_lsa_originate(ospf
, ei
))
2064 EC_OSPF_LSA_INSTALL_FAILURE
,
2065 "LSA: AS-external-LSA was not originated.");
2072 static struct external_info
*ospf_default_external_info(struct ospf
*ospf
)
2075 struct route_node
*rn
;
2076 struct prefix_ipv4 p
;
2079 p
.prefix
.s_addr
= 0;
2082 /* First, lookup redistributed default route. */
2083 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
2084 struct list
*ext_list
;
2085 struct listnode
*node
;
2086 struct ospf_external
*ext
;
2088 if (type
== ZEBRA_ROUTE_OSPF
)
2091 ext_list
= ospf
->external
[type
];
2095 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2096 rn
= route_node_lookup(ext
->external_info
,
2097 (struct prefix
*)&p
);
2099 route_unlock_node(rn
);
2101 if (ospf_redistribute_check(ospf
, rn
->info
,
2111 int ospf_default_originate_timer(struct thread
*thread
)
2113 struct prefix_ipv4 p
;
2114 struct in_addr nexthop
;
2115 struct external_info
*ei
;
2118 ospf
= THREAD_ARG(thread
);
2121 p
.prefix
.s_addr
= 0;
2124 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ALWAYS
) {
2125 /* If there is no default route via redistribute,
2126 then originate AS-external-LSA with nexthop 0 (self). */
2128 ospf_external_info_add(ospf
, DEFAULT_ROUTE
, 0, p
, 0, nexthop
,
2132 if ((ei
= ospf_default_external_info(ospf
)))
2133 ospf_external_lsa_originate(ospf
, ei
);
2138 /* Flush any NSSA LSAs for given prefix */
2139 void ospf_nssa_lsa_flush(struct ospf
*ospf
, struct prefix_ipv4
*p
)
2141 struct listnode
*node
, *nnode
;
2142 struct ospf_lsa
*lsa
= NULL
;
2143 struct ospf_area
*area
;
2145 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2146 if (area
->external_routing
== OSPF_AREA_NSSA
) {
2147 lsa
= ospf_lsa_lookup(ospf
, area
, OSPF_AS_NSSA_LSA
,
2148 p
->prefix
, ospf
->router_id
);
2150 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2152 "LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2153 inet_ntoa(p
->prefix
),
2157 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
2158 if (!IS_LSA_MAXAGE(lsa
)) {
2159 ospf_refresher_unregister_lsa(ospf
, lsa
);
2160 ospf_lsa_flush_area(lsa
, area
);
2166 /* Flush an AS-external-LSA from LSDB and routing domain. */
2167 void ospf_external_lsa_flush(struct ospf
*ospf
, uint8_t type
,
2168 struct prefix_ipv4
*p
,
2169 ifindex_t ifindex
/*, struct in_addr nexthop */)
2171 struct ospf_lsa
*lsa
;
2173 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2174 zlog_debug("LSA: Flushing AS-external-LSA %s/%d",
2175 inet_ntoa(p
->prefix
), p
->prefixlen
);
2177 /* First lookup LSA from LSDB. */
2178 if (!(lsa
= ospf_external_info_find_lsa(ospf
, p
))) {
2179 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2181 "LSA: There is no such AS-external-LSA %s/%d in LSDB",
2182 inet_ntoa(p
->prefix
), p
->prefixlen
);
2186 /* If LSA is selforiginated, not a translated LSA, and there is
2187 * NSSA area, flush Type-7 LSA's at first.
2189 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2190 && !(CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2191 ospf_nssa_lsa_flush(ospf
, p
);
2193 /* Sweep LSA from Link State Retransmit List. */
2194 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2196 /* There must be no self-originated LSA in rtrs_external. */
2198 /* Remove External route from Zebra. */
2199 ospf_zebra_delete ((struct prefix_ipv4
*) p
, &nexthop
);
2202 if (!IS_LSA_MAXAGE(lsa
)) {
2203 /* Unregister LSA from Refresh queue. */
2204 ospf_refresher_unregister_lsa(ospf
, lsa
);
2206 /* Flush AS-external-LSA through AS. */
2207 ospf_lsa_flush_as(ospf
, lsa
);
2210 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2211 zlog_debug("ospf_external_lsa_flush(): stop");
2214 void ospf_external_lsa_refresh_default(struct ospf
*ospf
)
2216 struct prefix_ipv4 p
;
2217 struct external_info
*ei
;
2218 struct ospf_lsa
*lsa
;
2222 p
.prefix
.s_addr
= 0;
2224 ei
= ospf_default_external_info(ospf
);
2225 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
2229 if (IS_DEBUG_OSPF_EVENT
)
2231 "LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2233 ospf_external_lsa_refresh(ospf
, lsa
, ei
,
2236 if (IS_DEBUG_OSPF_EVENT
)
2238 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2239 ospf_external_lsa_originate(ospf
, ei
);
2243 if (IS_DEBUG_OSPF_EVENT
)
2245 "LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2246 ospf_refresher_unregister_lsa(ospf
, lsa
);
2247 ospf_lsa_flush_as(ospf
, lsa
);
2252 void ospf_external_lsa_refresh_type(struct ospf
*ospf
, uint8_t type
,
2253 unsigned short instance
, int force
)
2255 struct route_node
*rn
;
2256 struct external_info
*ei
;
2257 struct ospf_external
*ext
;
2259 if (type
== DEFAULT_ROUTE
)
2262 ext
= ospf_external_lookup(ospf
, type
, instance
);
2264 if (ext
&& EXTERNAL_INFO(ext
)) {
2265 /* Refresh each redistributed AS-external-LSAs. */
2266 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
2267 rn
= route_next(rn
)) {
2270 if (!is_prefix_default(&ei
->p
)) {
2271 struct ospf_lsa
*lsa
;
2273 lsa
= ospf_external_info_find_lsa(
2276 ospf_external_lsa_refresh(
2277 ospf
, lsa
, ei
, force
);
2279 ospf_external_lsa_originate(
2287 /* Refresh AS-external-LSA. */
2288 struct ospf_lsa
*ospf_external_lsa_refresh(struct ospf
*ospf
,
2289 struct ospf_lsa
*lsa
,
2290 struct external_info
*ei
, int force
)
2292 struct ospf_lsa
*new;
2295 /* Check the AS-external-LSA should be originated. */
2296 if (!ospf_redistribute_check(ospf
, ei
, &changed
)) {
2297 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2299 "LSA[Type%d:%s]: Could not be refreshed, "
2300 "redist check fail",
2301 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2302 ospf_external_lsa_flush(ospf
, ei
->type
, &ei
->p
,
2303 ei
->ifindex
/*, ei->nexthop */);
2307 if (!changed
&& !force
) {
2308 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2310 "LSA[Type%d:%s]: Not refreshed, not changed/forced",
2311 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2315 /* Delete LSA from neighbor retransmit-list. */
2316 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2318 /* Unregister AS-external-LSA from refresh-list. */
2319 ospf_refresher_unregister_lsa(ospf
, lsa
);
2321 new = ospf_external_lsa_new(ospf
, ei
, &lsa
->data
->id
);
2324 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2325 zlog_debug("LSA[Type%d:%s]: Could not be refreshed",
2326 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2330 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2332 ospf_lsa_install(ospf
, NULL
, new); /* As type-5. */
2334 /* Flood LSA through AS. */
2335 ospf_flood_through_as(ospf
, NULL
, new);
2337 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2338 if (ospf
->anyNSSA
&& !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2339 ospf_install_flood_nssa(ospf
, new,
2340 ei
); /* Install/Flood per new rules */
2342 /* Register self-originated LSA to refresh queue.
2343 * Translated LSAs should not be registered, but refreshed upon
2344 * refresh of the Type-7
2346 if (!CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2347 ospf_refresher_register_lsa(ospf
, new);
2349 /* Debug logging. */
2350 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2351 zlog_debug("LSA[Type%d:%s]: AS-external-LSA refresh",
2352 new->data
->type
, inet_ntoa(new->data
->id
));
2353 ospf_lsa_header_dump(new->data
);
2360 /* LSA installation functions. */
2362 /* Install router-LSA to an area. */
2363 static struct ospf_lsa
*
2364 ospf_router_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2366 struct ospf_area
*area
= new->area
;
2368 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2369 The entire routing table must be recalculated, starting with
2370 the shortest path calculations for each area (not just the
2371 area whose link-state database has changed).
2374 if (IS_LSA_SELF(new)) {
2376 /* Only install LSA if it is originated/refreshed by us.
2377 * If LSA was received by flooding, the RECEIVED flag is set so
2379 * not link the LSA */
2380 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2381 return new; /* ignore stale LSA */
2383 /* Set self-originated router-LSA. */
2384 ospf_lsa_unlock(&area
->router_lsa_self
);
2385 area
->router_lsa_self
= ospf_lsa_lock(new);
2387 ospf_refresher_register_lsa(ospf
, new);
2390 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2394 #define OSPF_INTERFACE_TIMER_ON(T, F, V) \
2396 (T) = thread_add_timer(master, (F), oi, (V))
2398 /* Install network-LSA to an area. */
2399 static struct ospf_lsa
*ospf_network_lsa_install(struct ospf
*ospf
,
2400 struct ospf_interface
*oi
,
2401 struct ospf_lsa
*new,
2405 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2406 The entire routing table must be recalculated, starting with
2407 the shortest path calculations for each area (not just the
2408 area whose link-state database has changed).
2410 if (IS_LSA_SELF(new)) {
2411 /* We supposed that when LSA is originated by us, we pass the
2413 for which it was originated. If LSA was received by flooding,
2414 the RECEIVED flag is set, so we do not link the LSA to the
2416 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2417 return new; /* ignore stale LSA */
2419 ospf_lsa_unlock(&oi
->network_lsa_self
);
2420 oi
->network_lsa_self
= ospf_lsa_lock(new);
2421 ospf_refresher_register_lsa(ospf
, new);
2424 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2429 /* Install summary-LSA to an area. */
2430 static struct ospf_lsa
*
2431 ospf_summary_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2433 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2434 /* RFC 2328 Section 13.2 Summary-LSAs
2435 The best route to the destination described by the summary-
2436 LSA must be recalculated (see Section 16.5). If this
2437 destination is an AS boundary router, it may also be
2438 necessary to re-examine all the AS-external-LSAs.
2442 /* This doesn't exist yet... */
2443 ospf_summary_incremental_update(new); */
2445 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2449 if (IS_LSA_SELF(new))
2450 ospf_refresher_register_lsa(ospf
, new);
2455 /* Install ASBR-summary-LSA to an area. */
2456 static struct ospf_lsa
*ospf_summary_asbr_lsa_install(struct ospf
*ospf
,
2457 struct ospf_lsa
*new,
2460 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2461 /* RFC 2328 Section 13.2 Summary-LSAs
2462 The best route to the destination described by the summary-
2463 LSA must be recalculated (see Section 16.5). If this
2464 destination is an AS boundary router, it may also be
2465 necessary to re-examine all the AS-external-LSAs.
2468 /* These don't exist yet... */
2469 ospf_summary_incremental_update(new);
2470 /* Isn't this done by the above call?
2471 - RFC 2328 Section 16.5 implies it should be */
2472 /* ospf_ase_calculate_schedule(); */
2474 ospf_spf_calculate_schedule(ospf
,
2475 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2479 /* register LSA to refresh-list. */
2480 if (IS_LSA_SELF(new))
2481 ospf_refresher_register_lsa(ospf
, new);
2486 /* Install AS-external-LSA. */
2487 static struct ospf_lsa
*ospf_external_lsa_install(struct ospf
*ospf
,
2488 struct ospf_lsa
*new,
2491 ospf_ase_register_external_lsa(new, ospf
);
2492 /* If LSA is not self-originated, calculate an external route. */
2494 /* RFC 2328 Section 13.2 AS-external-LSAs
2495 The best route to the destination described by the AS-
2496 external-LSA must be recalculated (see Section 16.6).
2499 if (!IS_LSA_SELF(new))
2500 ospf_ase_incremental_update(ospf
, new);
2503 if (new->data
->type
== OSPF_AS_NSSA_LSA
) {
2504 /* There is no point to register selforiginate Type-7 LSA for
2505 * refreshing. We rely on refreshing Type-5 LSA's
2507 if (IS_LSA_SELF(new))
2510 /* Try refresh type-5 translated LSA for this LSA, if
2512 * New translations will be taken care of by the
2515 ospf_translated_nssa_refresh(ospf
, new, NULL
);
2516 ospf_schedule_abr_task(ospf
);
2520 /* Register self-originated LSA to refresh queue.
2521 * Leave Translated LSAs alone if NSSA is enabled
2523 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2524 ospf_refresher_register_lsa(ospf
, new);
2529 void ospf_discard_from_db(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
2530 struct ospf_lsa
*lsa
)
2532 struct ospf_lsa
*old
;
2537 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2542 if (old
->refresh_list
>= 0)
2543 ospf_refresher_unregister_lsa(ospf
, old
);
2545 switch (old
->data
->type
) {
2546 case OSPF_AS_EXTERNAL_LSA
:
2547 ospf_ase_unregister_external_lsa(old
, ospf
);
2548 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2550 case OSPF_OPAQUE_AS_LSA
:
2551 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2553 case OSPF_AS_NSSA_LSA
:
2554 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2555 ospf_ase_unregister_external_lsa(old
, ospf
);
2558 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2562 ospf_lsa_maxage_delete(ospf
, old
);
2563 ospf_lsa_discard(old
);
2566 struct ospf_lsa
*ospf_lsa_install(struct ospf
*ospf
, struct ospf_interface
*oi
,
2567 struct ospf_lsa
*lsa
)
2569 struct ospf_lsa
*new = NULL
;
2570 struct ospf_lsa
*old
= NULL
;
2571 struct ospf_lsdb
*lsdb
= NULL
;
2575 switch (lsa
->data
->type
) {
2577 case OSPF_AS_NSSA_LSA
:
2579 lsdb
= lsa
->area
->lsdb
;
2583 case OSPF_AS_EXTERNAL_LSA
:
2584 case OSPF_OPAQUE_AS_LSA
:
2589 lsdb
= lsa
->area
->lsdb
;
2595 /* RFC 2328 13.2. Installing LSAs in the database
2597 Installing a new LSA in the database, either as the result of
2598 flooding or a newly self-originated LSA, may cause the OSPF
2599 routing table structure to be recalculated. The contents of the
2600 new LSA should be compared to the old instance, if present. If
2601 there is no difference, there is no need to recalculate the
2602 routing table. When comparing an LSA to its previous instance,
2603 the following are all considered to be differences in contents:
2605 o The LSA's Options field has changed.
2607 o One of the LSA instances has LS age set to MaxAge, and
2610 o The length field in the LSA header has changed.
2612 o The body of the LSA (i.e., anything outside the 20-byte
2613 LSA header) has changed. Note that this excludes changes
2614 in LS Sequence Number and LS Checksum.
2617 /* Look up old LSA and determine if any SPF calculation or incremental
2619 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2621 /* Do comparision and record if recalc needed. */
2623 if (old
== NULL
|| ospf_lsa_different(old
, lsa
))
2627 Sequence number check (Section 14.1 of rfc 2328)
2628 "Premature aging is used when it is time for a self-originated
2629 LSA's sequence number field to wrap. At this point, the current
2630 LSA instance (having LS sequence number MaxSequenceNumber) must
2631 be prematurely aged and flushed from the routing domain before a
2632 new instance with sequence number equal to InitialSequenceNumber
2633 can be originated. "
2636 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
) {
2637 if (ospf_lsa_is_self_originated(ospf
, lsa
)) {
2638 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2640 if (!IS_LSA_MAXAGE(lsa
))
2641 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2642 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
2644 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
)) {
2646 "ospf_lsa_install() Premature Aging "
2647 "lsa 0x%p, seqnum 0x%x",
2649 ntohl(lsa
->data
->ls_seqnum
));
2650 ospf_lsa_header_dump(lsa
->data
);
2653 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2655 "ospf_lsa_install() got an lsa with seq 0x80000000 "
2656 "that was not self originated. Ignoring\n");
2657 ospf_lsa_header_dump(lsa
->data
);
2663 /* discard old LSA from LSDB */
2665 ospf_discard_from_db(ospf
, lsdb
, lsa
);
2667 /* Calculate Checksum if self-originated?. */
2668 if (IS_LSA_SELF(lsa
))
2669 ospf_lsa_checksum(lsa
->data
);
2671 /* Insert LSA to LSDB. */
2672 ospf_lsdb_add(lsdb
, lsa
);
2675 /* Do LSA specific installation process. */
2676 switch (lsa
->data
->type
) {
2677 case OSPF_ROUTER_LSA
:
2678 new = ospf_router_lsa_install(ospf
, lsa
, rt_recalc
);
2680 case OSPF_NETWORK_LSA
:
2682 new = ospf_network_lsa_install(ospf
, oi
, lsa
, rt_recalc
);
2684 case OSPF_SUMMARY_LSA
:
2685 new = ospf_summary_lsa_install(ospf
, lsa
, rt_recalc
);
2687 case OSPF_ASBR_SUMMARY_LSA
:
2688 new = ospf_summary_asbr_lsa_install(ospf
, lsa
, rt_recalc
);
2690 case OSPF_AS_EXTERNAL_LSA
:
2691 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2693 case OSPF_OPAQUE_LINK_LSA
:
2694 if (IS_LSA_SELF(lsa
))
2695 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for
2698 /* Incoming "oi" for this LSA has set at LSUpd
2702 case OSPF_OPAQUE_AREA_LSA
:
2703 case OSPF_OPAQUE_AS_LSA
:
2704 new = ospf_opaque_lsa_install(lsa
, rt_recalc
);
2706 case OSPF_AS_NSSA_LSA
:
2707 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2708 default: /* type-6,8,9....nothing special */
2713 return new; /* Installation failed, cannot proceed further --
2717 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
)) {
2718 char area_str
[INET_ADDRSTRLEN
];
2720 switch (lsa
->data
->type
) {
2721 case OSPF_AS_EXTERNAL_LSA
:
2722 case OSPF_OPAQUE_AS_LSA
:
2723 case OSPF_AS_NSSA_LSA
:
2724 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2725 lookup_msg(ospf_lsa_type_msg
,
2726 new->data
->type
, NULL
));
2729 strlcpy(area_str
, inet_ntoa(new->area
->area_id
),
2731 zlog_debug("LSA[%s]: Install %s to Area %s",
2733 lookup_msg(ospf_lsa_type_msg
,
2734 new->data
->type
, NULL
),
2741 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2742 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2744 if (IS_LSA_MAXAGE(new)) {
2745 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
))
2746 zlog_debug("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2747 new->data
->type
, inet_ntoa(new->data
->id
),
2749 ospf_lsa_maxage(ospf
, lsa
);
2756 int ospf_check_nbr_status(struct ospf
*ospf
)
2758 struct listnode
*node
, *nnode
;
2759 struct ospf_interface
*oi
;
2761 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
2762 struct route_node
*rn
;
2763 struct ospf_neighbor
*nbr
;
2765 if (ospf_if_is_enable(oi
))
2766 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
2767 if ((nbr
= rn
->info
) != NULL
)
2768 if (nbr
->state
== NSM_Exchange
2769 || nbr
->state
== NSM_Loading
) {
2770 route_unlock_node(rn
);
2779 static int ospf_maxage_lsa_remover(struct thread
*thread
)
2781 struct ospf
*ospf
= THREAD_ARG(thread
);
2782 struct ospf_lsa
*lsa
;
2783 struct route_node
*rn
;
2786 ospf
->t_maxage
= NULL
;
2788 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2789 zlog_debug("LSA[MaxAge]: remover Start");
2791 reschedule
= !ospf_check_nbr_status(ospf
);
2794 for (rn
= route_top(ospf
->maxage_lsa
); rn
;
2795 rn
= route_next(rn
)) {
2796 if ((lsa
= rn
->info
) == NULL
) {
2800 /* There is at least one neighbor from which we still
2802 * for that LSA, so we are not allowed to remove it from
2804 * as per RFC 2328 section 14 para 4 a) */
2805 if (lsa
->retransmit_counter
> 0) {
2810 /* TODO: maybe convert this function to a work-queue */
2811 if (thread_should_yield(thread
)) {
2812 OSPF_TIMER_ON(ospf
->t_maxage
,
2813 ospf_maxage_lsa_remover
, 0);
2815 rn
); /* route_top/route_next */
2819 /* Remove LSA from the LSDB */
2820 if (IS_LSA_SELF(lsa
))
2821 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2823 "LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2825 inet_ntoa(lsa
->data
->id
),
2826 (unsigned long)lsa
);
2828 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2830 "LSA[Type%d:%s]: MaxAge LSA removed from list",
2832 inet_ntoa(lsa
->data
->id
));
2834 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
)) {
2835 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2837 "originating new lsa for lsa 0x%p\n",
2839 ospf_lsa_refresh(ospf
, lsa
);
2842 /* Remove from lsdb. */
2844 ospf_discard_from_db(ospf
, lsa
->lsdb
, lsa
);
2845 ospf_lsdb_delete(lsa
->lsdb
, lsa
);
2847 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2849 "%s: LSA[Type%d:%s]: No associated LSDB!",
2850 __func__
, lsa
->data
->type
,
2851 inet_ntoa(lsa
->data
->id
));
2855 /* A MaxAge LSA must be removed immediately from the router's link
2856 state database as soon as both a) it is no longer contained on any
2857 neighbor Link state retransmission lists and b) none of the
2859 neighbors are in states Exchange or Loading. */
2861 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2862 ospf
->maxage_delay
);
2867 void ospf_lsa_maxage_delete(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2869 struct route_node
*rn
;
2870 struct prefix lsa_prefix
;
2872 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2873 lsa_prefix
.family
= 0;
2874 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2875 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2877 if ((rn
= route_node_lookup(ospf
->maxage_lsa
,
2878 (struct prefix
*)&lsa_prefix
))) {
2879 if (rn
->info
== lsa
) {
2880 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2881 ospf_lsa_unlock(&lsa
); /* maxage_lsa */
2884 rn
); /* unlock node because lsa is deleted */
2886 route_unlock_node(rn
); /* route_node_lookup */
2888 if (IS_DEBUG_OSPF_EVENT
)
2889 zlog_debug("%s: lsa %s is not found in maxage db.",
2890 __PRETTY_FUNCTION__
, dump_lsa_key(lsa
));
2894 /* Add LSA onto the MaxAge list, and schedule for removal.
2895 * This does *not* lead to the LSA being flooded, that must be taken
2896 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2899 void ospf_lsa_maxage(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2901 struct prefix lsa_prefix
;
2902 struct route_node
*rn
;
2904 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2905 and schedule the MaxAge LSA remover. */
2906 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
)) {
2907 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2909 "LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2910 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
2915 memset(&lsa_prefix
, 0, sizeof(struct prefix
));
2916 lsa_prefix
.family
= 0;
2917 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
2918 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
2920 rn
= route_node_get(ospf
->maxage_lsa
, (struct prefix
*)&lsa_prefix
);
2921 if (rn
->info
!= NULL
) {
2922 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2924 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
2925 dump_lsa_key(lsa
), rn
->info
,
2926 (void *)lsa
, lsa_prefix
.prefixlen
);
2927 route_unlock_node(rn
);
2929 rn
->info
= ospf_lsa_lock(lsa
);
2930 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2933 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2934 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
2937 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2938 ospf
->maxage_delay
);
2941 static int ospf_lsa_maxage_walker_remover(struct ospf
*ospf
,
2942 struct ospf_lsa
*lsa
)
2944 /* Stay away from any Local Translated Type-7 LSAs */
2945 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
2948 if (IS_LSA_MAXAGE(lsa
))
2949 /* Self-originated LSAs should NOT time-out instead,
2950 they're flushed and submitted to the max_age list explicitly.
2952 if (!ospf_lsa_is_self_originated(ospf
, lsa
)) {
2953 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2954 zlog_debug("LSA[%s]: is MaxAge",
2957 switch (lsa
->data
->type
) {
2958 case OSPF_OPAQUE_LINK_LSA
:
2959 case OSPF_OPAQUE_AREA_LSA
:
2960 case OSPF_OPAQUE_AS_LSA
:
2962 * As a general rule, whenever network topology
2964 * (due to an LSA removal in this case), routing
2966 * should be triggered. However, this is not
2968 * LSAs. Even if an opaque LSA instance is going
2970 * from the routing domain, it does not mean a
2972 * topology, and thus, routing recalculation is
2976 case OSPF_AS_EXTERNAL_LSA
:
2977 case OSPF_AS_NSSA_LSA
:
2978 ospf_ase_incremental_update(ospf
, lsa
);
2981 ospf_spf_calculate_schedule(ospf
,
2985 ospf_lsa_maxage(ospf
, lsa
);
2988 if (IS_LSA_MAXAGE(lsa
) && !ospf_lsa_is_self_originated(ospf
, lsa
))
2989 if (LS_AGE(lsa
) > OSPF_LSA_MAXAGE
+ 30)
2990 printf("Eek! Shouldn't happen!\n");
2995 /* Periodical check of MaxAge LSA. */
2996 int ospf_lsa_maxage_walker(struct thread
*thread
)
2998 struct ospf
*ospf
= THREAD_ARG(thread
);
2999 struct route_node
*rn
;
3000 struct ospf_lsa
*lsa
;
3001 struct ospf_area
*area
;
3002 struct listnode
*node
, *nnode
;
3004 ospf
->t_maxage_walker
= NULL
;
3006 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3007 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
3008 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3009 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
3010 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3011 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3012 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3013 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3014 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3015 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3016 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3017 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3018 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3019 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
3020 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3023 /* for AS-external-LSAs. */
3025 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3026 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3027 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3028 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3031 OSPF_TIMER_ON(ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3032 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3036 struct ospf_lsa
*ospf_lsa_lookup_by_prefix(struct ospf_lsdb
*lsdb
, uint8_t type
,
3037 struct prefix_ipv4
*p
,
3038 struct in_addr router_id
)
3040 struct ospf_lsa
*lsa
;
3041 struct in_addr mask
, id
;
3042 struct lsa_header_mask
{
3043 struct lsa_header header
;
3044 struct in_addr mask
;
3047 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, p
->prefix
, router_id
);
3051 masklen2ip(p
->prefixlen
, &mask
);
3053 hmask
= (struct lsa_header_mask
*)lsa
->data
;
3055 if (mask
.s_addr
!= hmask
->mask
.s_addr
) {
3056 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3057 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, router_id
);
3065 struct ospf_lsa
*ospf_lsa_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
3066 uint32_t type
, struct in_addr id
,
3067 struct in_addr adv_router
)
3073 case OSPF_ROUTER_LSA
:
3074 case OSPF_NETWORK_LSA
:
3075 case OSPF_SUMMARY_LSA
:
3076 case OSPF_ASBR_SUMMARY_LSA
:
3077 case OSPF_AS_NSSA_LSA
:
3078 case OSPF_OPAQUE_LINK_LSA
:
3079 case OSPF_OPAQUE_AREA_LSA
:
3080 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, adv_router
);
3081 case OSPF_AS_EXTERNAL_LSA
:
3082 case OSPF_OPAQUE_AS_LSA
:
3083 return ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
, adv_router
);
3091 struct ospf_lsa
*ospf_lsa_lookup_by_id(struct ospf_area
*area
, uint32_t type
,
3094 struct ospf_lsa
*lsa
;
3095 struct route_node
*rn
;
3098 case OSPF_ROUTER_LSA
:
3099 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3100 case OSPF_NETWORK_LSA
:
3101 for (rn
= route_top(NETWORK_LSDB(area
)); rn
;
3102 rn
= route_next(rn
))
3103 if ((lsa
= rn
->info
))
3104 if (IPV4_ADDR_SAME(&lsa
->data
->id
, &id
)) {
3105 route_unlock_node(rn
);
3109 case OSPF_SUMMARY_LSA
:
3110 case OSPF_ASBR_SUMMARY_LSA
:
3111 /* Currently not used. */
3113 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3114 case OSPF_AS_EXTERNAL_LSA
:
3115 case OSPF_AS_NSSA_LSA
:
3116 case OSPF_OPAQUE_LINK_LSA
:
3117 case OSPF_OPAQUE_AREA_LSA
:
3118 case OSPF_OPAQUE_AS_LSA
:
3119 /* Currently not used. */
3128 struct ospf_lsa
*ospf_lsa_lookup_by_header(struct ospf_area
*area
,
3129 struct lsa_header
*lsah
)
3131 struct ospf_lsa
*match
;
3134 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3135 * is redefined to have two subfields; opaque-type and opaque-id.
3136 * However, it is harmless to treat the two sub fields together, as if
3137 * they two were forming a unique LSA-ID.
3140 match
= ospf_lsa_lookup(area
->ospf
, area
, lsah
->type
, lsah
->id
,
3144 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3145 zlog_debug("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3146 lsah
->type
, inet_ntoa(lsah
->id
));
3151 /* return +n, l1 is more recent.
3152 return -n, l2 is more recent.
3153 return 0, l1 and l2 is identical. */
3154 int ospf_lsa_more_recent(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3159 if (l1
== NULL
&& l2
== NULL
)
3166 /* compare LS sequence number. */
3167 x
= (int)ntohl(l1
->data
->ls_seqnum
);
3168 y
= (int)ntohl(l2
->data
->ls_seqnum
);
3174 /* compare LS checksum. */
3175 r
= ntohs(l1
->data
->checksum
) - ntohs(l2
->data
->checksum
);
3179 /* compare LS age. */
3180 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3182 else if (!IS_LSA_MAXAGE(l1
) && IS_LSA_MAXAGE(l2
))
3185 /* compare LS age with MaxAgeDiff. */
3186 if (LS_AGE(l1
) - LS_AGE(l2
) > OSPF_LSA_MAXAGE_DIFF
)
3188 else if (LS_AGE(l2
) - LS_AGE(l1
) > OSPF_LSA_MAXAGE_DIFF
)
3191 /* LSAs are identical. */
3195 /* If two LSAs are different, return 1, otherwise return 0. */
3196 int ospf_lsa_different(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3204 if (l1
->data
->options
!= l2
->data
->options
)
3207 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3210 if (IS_LSA_MAXAGE(l2
) && !IS_LSA_MAXAGE(l1
))
3213 if (l1
->data
->length
!= l2
->data
->length
)
3216 if (l1
->data
->length
== 0)
3219 if (CHECK_FLAG((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3220 return 1; /* May be a stale LSA in the LSBD */
3222 assert(ntohs(l1
->data
->length
) > OSPF_LSA_HEADER_SIZE
);
3224 p1
= (char *)l1
->data
;
3225 p2
= (char *)l2
->data
;
3227 if (memcmp(p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3228 ntohs(l1
->data
->length
) - OSPF_LSA_HEADER_SIZE
)
3235 #ifdef ORIGINAL_CODING
3236 void ospf_lsa_flush_self_originated(struct ospf_neighbor
*nbr
,
3237 struct ospf_lsa
*self
, struct ospf_lsa
*new)
3241 /* Adjust LS Sequence Number. */
3242 seqnum
= ntohl(new->data
->ls_seqnum
) + 1;
3243 self
->data
->ls_seqnum
= htonl(seqnum
);
3245 /* Recalculate LSA checksum. */
3246 ospf_lsa_checksum(self
->data
);
3248 /* Reflooding LSA. */
3249 /* RFC2328 Section 13.3
3250 On non-broadcast networks, separate Link State Update
3251 packets must be sent, as unicasts, to each adjacent neighbor
3252 (i.e., those in state Exchange or greater). The destination
3253 IP addresses for these packets are the neighbors' IP
3255 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
) {
3256 struct route_node
*rn
;
3257 struct ospf_neighbor
*onbr
;
3259 for (rn
= route_top(nbr
->oi
->nbrs
); rn
; rn
= route_next(rn
))
3260 if ((onbr
= rn
->info
) != NULL
)
3261 if (onbr
!= nbr
->oi
->nbr_self
3262 && onbr
->status
>= NSM_Exchange
)
3263 ospf_ls_upd_send_lsa(
3265 OSPF_SEND_PACKET_DIRECT
);
3267 ospf_ls_upd_send_lsa(nbr
, self
, OSPF_SEND_PACKET_INDIRECT
);
3269 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3270 zlog_debug("LSA[Type%d:%s]: Flush self-originated LSA",
3271 self
->data
->type
, inet_ntoa(self
->data
->id
));
3273 #else /* ORIGINAL_CODING */
3274 int ospf_lsa_flush_schedule(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3276 if (lsa
== NULL
|| !IS_LSA_SELF(lsa
))
3279 if (IS_DEBUG_OSPF_EVENT
)
3281 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3282 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
3284 /* Force given lsa's age to MaxAge. */
3285 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
3287 switch (lsa
->data
->type
) {
3288 /* Opaque wants to be notified of flushes */
3289 case OSPF_OPAQUE_LINK_LSA
:
3290 case OSPF_OPAQUE_AREA_LSA
:
3291 case OSPF_OPAQUE_AS_LSA
:
3292 ospf_opaque_lsa_refresh(lsa
);
3295 ospf_refresher_unregister_lsa(ospf
, lsa
);
3296 ospf_lsa_flush(ospf
, lsa
);
3303 void ospf_flush_self_originated_lsas_now(struct ospf
*ospf
)
3305 struct listnode
*node
, *nnode
;
3306 struct listnode
*node2
, *nnode2
;
3307 struct ospf_area
*area
;
3308 struct ospf_interface
*oi
;
3309 struct ospf_lsa
*lsa
;
3310 struct route_node
*rn
;
3311 int need_to_flush_ase
= 0;
3313 ospf
->inst_shutdown
= 1;
3315 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3316 if ((lsa
= area
->router_lsa_self
) != NULL
) {
3317 if (IS_DEBUG_OSPF_EVENT
)
3319 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3321 inet_ntoa(lsa
->data
->id
));
3323 ospf_refresher_unregister_lsa(ospf
, lsa
);
3324 ospf_lsa_flush_area(lsa
, area
);
3325 ospf_lsa_unlock(&area
->router_lsa_self
);
3326 area
->router_lsa_self
= NULL
;
3329 for (ALL_LIST_ELEMENTS(area
->oiflist
, node2
, nnode2
, oi
)) {
3330 if ((lsa
= oi
->network_lsa_self
) != NULL
3331 && oi
->state
== ISM_DR
&& oi
->full_nbrs
> 0) {
3332 if (IS_DEBUG_OSPF_EVENT
)
3334 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3336 inet_ntoa(lsa
->data
->id
));
3338 ospf_refresher_unregister_lsa(
3339 ospf
, oi
->network_lsa_self
);
3340 ospf_lsa_flush_area(oi
->network_lsa_self
, area
);
3341 ospf_lsa_unlock(&oi
->network_lsa_self
);
3342 oi
->network_lsa_self
= NULL
;
3345 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3346 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3347 need_to_flush_ase
= 1;
3350 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3351 ospf_lsa_flush_schedule(ospf
, lsa
);
3352 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3353 ospf_lsa_flush_schedule(ospf
, lsa
);
3354 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3355 ospf_lsa_flush_schedule(ospf
, lsa
);
3356 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3357 ospf_lsa_flush_schedule(ospf
, lsa
);
3360 if (need_to_flush_ase
) {
3361 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3362 ospf_lsa_flush_schedule(ospf
, lsa
);
3363 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3364 ospf_lsa_flush_schedule(ospf
, lsa
);
3368 * Make sure that the MaxAge LSA remover is executed immediately,
3369 * without conflicting to other threads.
3371 if (ospf
->t_maxage
!= NULL
) {
3372 OSPF_TIMER_OFF(ospf
->t_maxage
);
3373 thread_execute(master
, ospf_maxage_lsa_remover
, ospf
, 0);
3378 #endif /* ORIGINAL_CODING */
3380 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3381 /* An interface-independent version of ospf_lsa_is_self_originated */
3382 int ospf_lsa_is_self_originated(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3384 struct listnode
*node
;
3385 struct ospf_interface
*oi
;
3387 /* This LSA is already checked. */
3388 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3389 return IS_LSA_SELF(lsa
);
3391 /* Make sure LSA is self-checked. */
3392 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3394 /* AdvRouter and Router ID is the same. */
3395 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
, &ospf
->router_id
))
3396 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3398 /* LSA is router-LSA. */
3399 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
3400 && IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
))
3401 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3403 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3404 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3405 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
3406 /* Ignore virtual link. */
3407 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3408 if (oi
->address
->family
== AF_INET
)
3411 &oi
->address
->u
.prefix4
)) {
3412 /* to make it easier later */
3413 SET_FLAG(lsa
->flags
,
3415 return IS_LSA_SELF(lsa
);
3419 return IS_LSA_SELF(lsa
);
3422 /* Get unique Link State ID. */
3423 struct in_addr
ospf_lsa_unique_id(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
3424 uint8_t type
, struct prefix_ipv4
*p
)
3426 struct ospf_lsa
*lsa
;
3427 struct in_addr mask
, id
;
3431 /* Check existence of LSA instance. */
3432 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, ospf
->router_id
);
3434 struct as_external_lsa
*al
=
3435 (struct as_external_lsa
*)lsa
->data
;
3436 if (ip_masklen(al
->mask
) == p
->prefixlen
) {
3437 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3439 "ospf_lsa_unique_id(): "
3440 "Can't get Link State ID for %s/%d",
3441 inet_ntoa(p
->prefix
), p
->prefixlen
);
3442 /* id.s_addr = 0; */
3443 id
.s_addr
= 0xffffffff;
3446 /* Masklen differs, then apply wildcard mask to Link State ID.
3449 masklen2ip(p
->prefixlen
, &mask
);
3451 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3452 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
,
3455 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3457 "ospf_lsa_unique_id(): "
3458 "Can't get Link State ID for %s/%d",
3459 inet_ntoa(p
->prefix
),
3461 /* id.s_addr = 0; */
3462 id
.s_addr
= 0xffffffff;
3472 #define LSA_ACTION_FLOOD_AREA 1
3473 #define LSA_ACTION_FLUSH_AREA 2
3477 struct ospf_area
*area
;
3478 struct ospf_lsa
*lsa
;
3481 static int ospf_lsa_action(struct thread
*t
)
3483 struct lsa_action
*data
;
3485 data
= THREAD_ARG(t
);
3487 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3488 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3491 switch (data
->action
) {
3492 case LSA_ACTION_FLOOD_AREA
:
3493 ospf_flood_through_area(data
->area
, NULL
, data
->lsa
);
3495 case LSA_ACTION_FLUSH_AREA
:
3496 ospf_lsa_flush_area(data
->lsa
, data
->area
);
3500 ospf_lsa_unlock(&data
->lsa
); /* Message */
3501 XFREE(MTYPE_OSPF_MESSAGE
, data
);
3505 void ospf_schedule_lsa_flood_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3507 struct lsa_action
*data
;
3509 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3510 data
->action
= LSA_ACTION_FLOOD_AREA
;
3512 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flood area */
3514 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3517 void ospf_schedule_lsa_flush_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3519 struct lsa_action
*data
;
3521 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3522 data
->action
= LSA_ACTION_FLUSH_AREA
;
3524 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flush area */
3526 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3530 /* LSA Refreshment functions. */
3531 struct ospf_lsa
*ospf_lsa_refresh(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3533 struct external_info
*ei
;
3534 struct ospf_lsa
*new = NULL
;
3535 assert(CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF
));
3536 assert(IS_LSA_SELF(lsa
));
3537 assert(lsa
->lock
> 0);
3539 switch (lsa
->data
->type
) {
3540 /* Router and Network LSAs are processed differently. */
3541 case OSPF_ROUTER_LSA
:
3542 new = ospf_router_lsa_refresh(lsa
);
3544 case OSPF_NETWORK_LSA
:
3545 new = ospf_network_lsa_refresh(lsa
);
3547 case OSPF_SUMMARY_LSA
:
3548 new = ospf_summary_lsa_refresh(ospf
, lsa
);
3550 case OSPF_ASBR_SUMMARY_LSA
:
3551 new = ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
3553 case OSPF_AS_EXTERNAL_LSA
:
3554 /* Translated from NSSA Type-5s are refreshed when
3555 * from refresh of Type-7 - do not refresh these directly.
3557 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3559 ei
= ospf_external_info_check(ospf
, lsa
);
3561 new = ospf_external_lsa_refresh(ospf
, lsa
, ei
,
3564 ospf_lsa_flush_as(ospf
, lsa
);
3566 case OSPF_OPAQUE_LINK_LSA
:
3567 case OSPF_OPAQUE_AREA_LSA
:
3568 case OSPF_OPAQUE_AS_LSA
:
3569 new = ospf_opaque_lsa_refresh(lsa
);
3577 void ospf_refresher_register_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3579 uint16_t index
, current_index
;
3581 assert(lsa
->lock
> 0);
3582 assert(IS_LSA_SELF(lsa
));
3584 if (lsa
->refresh_list
< 0) {
3587 OSPF_LS_REFRESH_TIME
- (2 * OSPF_LS_REFRESH_JITTER
);
3588 int max_delay
= OSPF_LS_REFRESH_TIME
- OSPF_LS_REFRESH_JITTER
;
3590 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3592 * 1800s. Use jitter so that we send the LSA sometime between
3596 delay
= (random() % (max_delay
- min_delay
)) + min_delay
;
3598 current_index
= ospf
->lsa_refresh_queue
.index
3599 + (monotime(NULL
) - ospf
->lsa_refresher_started
)
3600 / OSPF_LSA_REFRESHER_GRANULARITY
;
3602 index
= (current_index
+ delay
/ OSPF_LSA_REFRESHER_GRANULARITY
)
3603 % (OSPF_LSA_REFRESHER_SLOTS
);
3605 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3607 "LSA[Refresh:Type%d:%s]: age %d, added to index %d",
3608 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
3609 LS_AGE(lsa
), index
);
3611 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3612 ospf
->lsa_refresh_queue
.qs
[index
] = list_new();
3614 listnode_add(ospf
->lsa_refresh_queue
.qs
[index
],
3615 ospf_lsa_lock(lsa
)); /* lsa_refresh_queue */
3616 lsa
->refresh_list
= index
;
3618 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3620 "LSA[Refresh:Type%d:%s]: ospf_refresher_register_lsa(): "
3621 "setting refresh_list on lsa %p (slod %d)",
3622 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
3623 (void *)lsa
, index
);
3627 void ospf_refresher_unregister_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3629 assert(lsa
->lock
> 0);
3630 assert(IS_LSA_SELF(lsa
));
3631 if (lsa
->refresh_list
>= 0) {
3632 struct list
*refresh_list
=
3633 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3634 listnode_delete(refresh_list
, lsa
);
3635 if (!listcount(refresh_list
)) {
3636 list_delete_and_null(&refresh_list
);
3637 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3639 ospf_lsa_unlock(&lsa
); /* lsa_refresh_queue */
3640 lsa
->refresh_list
= -1;
3644 int ospf_lsa_refresh_walker(struct thread
*t
)
3646 struct list
*refresh_list
;
3647 struct listnode
*node
, *nnode
;
3648 struct ospf
*ospf
= THREAD_ARG(t
);
3649 struct ospf_lsa
*lsa
;
3651 struct list
*lsa_to_refresh
= list_new();
3653 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3654 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3657 i
= ospf
->lsa_refresh_queue
.index
;
3659 /* Note: if clock has jumped backwards, then time change could be
3661 so we are careful to cast the expression to unsigned before taking
3663 ospf
->lsa_refresh_queue
.index
=
3664 ((unsigned long)(ospf
->lsa_refresh_queue
.index
3666 - ospf
->lsa_refresher_started
)
3667 / OSPF_LSA_REFRESHER_GRANULARITY
))
3668 % OSPF_LSA_REFRESHER_SLOTS
;
3670 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3672 "LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3673 ospf
->lsa_refresh_queue
.index
);
3675 for (; i
!= ospf
->lsa_refresh_queue
.index
;
3676 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
) {
3677 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3679 "LSA[Refresh]: ospf_lsa_refresh_walker(): "
3683 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3687 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3690 for (ALL_LIST_ELEMENTS(refresh_list
, node
, nnode
,
3692 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3694 "LSA[Refresh:Type%d:%s]: ospf_lsa_refresh_walker(): "
3695 "refresh lsa %p (slot %d)",
3697 inet_ntoa(lsa
->data
->id
),
3700 assert(lsa
->lock
> 0);
3701 list_delete_node(refresh_list
, node
);
3702 lsa
->refresh_list
= -1;
3703 listnode_add(lsa_to_refresh
, lsa
);
3705 list_delete_and_null(&refresh_list
);
3709 ospf
->t_lsa_refresher
= NULL
;
3710 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
3711 ospf
->lsa_refresh_interval
, &ospf
->t_lsa_refresher
);
3712 ospf
->lsa_refresher_started
= monotime(NULL
);
3714 for (ALL_LIST_ELEMENTS(lsa_to_refresh
, node
, nnode
, lsa
)) {
3715 ospf_lsa_refresh(ospf
, lsa
);
3716 assert(lsa
->lock
> 0);
3718 &lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3721 list_delete_and_null(&lsa_to_refresh
);
3723 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3724 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");