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"
54 u_int32_t
get_metric(u_char
*metric
)
58 m
= (m
<< 8) + metric
[1];
59 m
= (m
<< 8) + metric
[2];
64 struct timeval
int2tv(int a
)
74 struct timeval
msec2tv(int a
)
78 ret
.tv_sec
= a
/ 1000;
79 ret
.tv_usec
= (a
% 1000) * 1000;
84 int ospf_lsa_refresh_delay(struct ospf_lsa
*lsa
)
89 if (monotime_since(&lsa
->tv_orig
, &delta
)
90 < OSPF_MIN_LS_INTERVAL
* 1000LL) {
91 struct timeval minv
= msec2tv(OSPF_MIN_LS_INTERVAL
);
92 timersub(&minv
, &delta
, &minv
);
94 /* TBD: remove padding to full sec, return timeval instead */
95 delay
= minv
.tv_sec
+ !!minv
.tv_usec
;
97 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
99 "LSA[Type%d:%s]: Refresh timer delay %d seconds",
100 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
110 int get_age(struct ospf_lsa
*lsa
)
114 monotime_since(&lsa
->tv_recv
, &rel
);
115 return ntohs(lsa
->data
->ls_age
) + rel
.tv_sec
;
119 /* Fletcher Checksum -- Refer to RFC1008. */
121 /* All the offsets are zero-based. The offsets in the RFC1008 are
123 u_int16_t
ospf_lsa_checksum(struct lsa_header
*lsa
)
125 u_char
*buffer
= (u_char
*)&lsa
->options
;
126 int options_offset
= buffer
- (u_char
*)&lsa
->ls_age
; /* should be 2 */
128 /* Skip the AGE field */
129 u_int16_t len
= ntohs(lsa
->length
) - options_offset
;
131 /* Checksum offset starts from "options" field, not the beginning of the
132 lsa_header struct. The offset is 14, rather than 16. */
133 int checksum_offset
= (u_char
*)&lsa
->checksum
- buffer
;
135 return fletcher_checksum(buffer
, len
, checksum_offset
);
138 int ospf_lsa_checksum_valid(struct lsa_header
*lsa
)
140 u_char
*buffer
= (u_char
*)&lsa
->options
;
141 int options_offset
= buffer
- (u_char
*)&lsa
->ls_age
; /* should be 2 */
143 /* Skip the AGE field */
144 u_int16_t len
= ntohs(lsa
->length
) - options_offset
;
146 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
151 /* Create OSPF LSA. */
152 struct ospf_lsa
*ospf_lsa_new()
154 struct ospf_lsa
*new;
156 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
160 new->retransmit_counter
= 0;
161 monotime(&new->tv_recv
);
162 new->tv_orig
= new->tv_recv
;
163 new->refresh_list
= -1;
168 /* Duplicate OSPF LSA. */
169 struct ospf_lsa
*ospf_lsa_dup(struct ospf_lsa
*lsa
)
171 struct ospf_lsa
*new;
176 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
178 memcpy(new, lsa
, sizeof(struct ospf_lsa
));
179 UNSET_FLAG(new->flags
, OSPF_LSA_DISCARD
);
181 new->retransmit_counter
= 0;
182 new->data
= ospf_lsa_data_dup(lsa
->data
);
184 /* kevinm: Clear the refresh_list, otherwise there are going
185 to be problems when we try to remove the LSA from the
186 queue (which it's not a member of.)
187 XXX: Should we add the LSA to the refresh_list queue? */
188 new->refresh_list
= -1;
190 if (IS_DEBUG_OSPF(lsa
, LSA
))
191 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa
,
198 void ospf_lsa_free(struct ospf_lsa
*lsa
)
200 assert(lsa
->lock
== 0);
202 if (IS_DEBUG_OSPF(lsa
, LSA
))
203 zlog_debug("LSA: freed %p", (void *)lsa
);
205 /* Delete LSA data. */
206 if (lsa
->data
!= NULL
)
207 ospf_lsa_data_free(lsa
->data
);
209 assert(lsa
->refresh_list
< 0);
211 memset(lsa
, 0, sizeof(struct ospf_lsa
));
212 XFREE(MTYPE_OSPF_LSA
, lsa
);
216 struct ospf_lsa
*ospf_lsa_lock(struct ospf_lsa
*lsa
)
223 void ospf_lsa_unlock(struct ospf_lsa
**lsa
)
225 /* This is sanity check. */
231 assert((*lsa
)->lock
>= 0);
233 if ((*lsa
)->lock
== 0) {
234 assert(CHECK_FLAG((*lsa
)->flags
, OSPF_LSA_DISCARD
));
240 /* Check discard flag. */
241 void ospf_lsa_discard(struct ospf_lsa
*lsa
)
243 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
244 SET_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
);
245 ospf_lsa_unlock(&lsa
);
249 /* Create LSA data. */
250 struct lsa_header
*ospf_lsa_data_new(size_t size
)
252 return XCALLOC(MTYPE_OSPF_LSA_DATA
, size
);
255 /* Duplicate LSA data. */
256 struct lsa_header
*ospf_lsa_data_dup(struct lsa_header
*lsah
)
258 struct lsa_header
*new;
260 new = ospf_lsa_data_new(ntohs(lsah
->length
));
261 memcpy(new, lsah
, ntohs(lsah
->length
));
267 void ospf_lsa_data_free(struct lsa_header
*lsah
)
269 if (IS_DEBUG_OSPF(lsa
, LSA
))
270 zlog_debug("LSA[Type%d:%s]: data freed %p", lsah
->type
,
271 inet_ntoa(lsah
->id
), (void *)lsah
);
273 XFREE(MTYPE_OSPF_LSA_DATA
, lsah
);
277 /* LSA general functions. */
279 const char *dump_lsa_key(struct ospf_lsa
*lsa
)
281 static char buf
[] = {"Type255,id(255.255.255.255),ar(255.255.255.255)"};
282 struct lsa_header
*lsah
;
284 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
) {
285 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
286 strcpy(id
, inet_ntoa(lsah
->id
));
287 strcpy(ar
, inet_ntoa(lsah
->adv_router
));
289 sprintf(buf
, "Type%d,id(%s),ar(%s)", lsah
->type
, id
, ar
);
296 u_int32_t
lsa_seqnum_increment(struct ospf_lsa
*lsa
)
300 seqnum
= ntohl(lsa
->data
->ls_seqnum
) + 1;
302 return htonl(seqnum
);
305 void lsa_header_set(struct stream
*s
, u_char options
, u_char type
,
306 struct in_addr id
, struct in_addr router_id
)
308 struct lsa_header
*lsah
;
310 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
312 lsah
->ls_age
= htons(OSPF_LSA_INITIAL_AGE
);
313 lsah
->options
= options
;
316 lsah
->adv_router
= router_id
;
317 lsah
->ls_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
319 stream_forward_endp(s
, OSPF_LSA_HEADER_SIZE
);
323 /* router-LSA related functions. */
324 /* Get router-LSA flags. */
325 static u_char
router_lsa_flags(struct ospf_area
*area
)
329 flags
= area
->ospf
->flags
;
331 /* Set virtual link flag. */
332 if (ospf_full_virtual_nbrs(area
))
333 SET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
335 /* Just sanity check */
336 UNSET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
338 /* Set Shortcut ABR behabiour flag. */
339 UNSET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
340 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
341 if (!OSPF_IS_AREA_BACKBONE(area
))
342 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
343 && area
->ospf
->backbone
== NULL
)
344 || area
->shortcut_configured
345 == OSPF_SHORTCUT_ENABLE
)
346 SET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
348 /* ASBR can't exit in stub area. */
349 if (area
->external_routing
== OSPF_AREA_STUB
)
350 UNSET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
351 /* If ASBR set External flag */
352 else if (IS_OSPF_ASBR(area
->ospf
))
353 SET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
355 /* Set ABR dependent flags */
356 if (IS_OSPF_ABR(area
->ospf
)) {
357 SET_FLAG(flags
, ROUTER_LSA_BORDER
);
358 /* If Area is NSSA and we are both ABR and unconditional
360 * set Nt bit to inform other routers.
362 if ((area
->external_routing
== OSPF_AREA_NSSA
)
363 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
364 SET_FLAG(flags
, ROUTER_LSA_NT
);
369 /* Lookup neighbor other than myself.
370 And check neighbor count,
371 Point-to-Point link must have only 1 neighbor. */
372 struct ospf_neighbor
*ospf_nbr_lookup_ptop(struct ospf_interface
*oi
)
374 struct ospf_neighbor
*nbr
= NULL
;
375 struct route_node
*rn
;
377 /* Search neighbor, there must be one of two nbrs. */
378 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
379 if ((nbr
= rn
->info
))
380 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
381 &oi
->ospf
->router_id
))
382 if (nbr
->state
== NSM_Full
) {
383 route_unlock_node(rn
);
387 /* PtoP link must have only 1 neighbor. */
388 if (ospf_nbr_count(oi
, 0) > 1)
389 zlog_warn("Point-to-Point link has more than 1 neighobrs.");
394 /* Determine cost of link, taking RFC3137 stub-router support into
397 static u_int16_t
ospf_link_cost(struct ospf_interface
*oi
)
399 /* RFC3137 stub router support */
400 if (!CHECK_FLAG(oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
401 return oi
->output_cost
;
403 return OSPF_OUTPUT_COST_INFINITE
;
406 /* Set a link information. */
407 static char link_info_set(struct stream
*s
, struct in_addr id
,
408 struct in_addr data
, u_char type
, u_char tos
,
411 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
412 * vast majority of cases. Some rare routers with lots of links need
414 * we try accomodate those here.
416 if (STREAM_WRITEABLE(s
) < OSPF_ROUTER_LSA_LINK_SIZE
) {
417 size_t ret
= OSPF_MAX_LSA_SIZE
;
419 /* Can we enlarge the stream still? */
420 if (STREAM_SIZE(s
) == OSPF_MAX_LSA_SIZE
) {
421 /* we futz the size here for simplicity, really we need
424 * IP Header - (sizeof (struct ip))
425 * OSPF Header - OSPF_HEADER_SIZE
426 * LSA Header - OSPF_LSA_HEADER_SIZE
427 * MD5 auth data, if MD5 is configured -
428 * OSPF_AUTH_MD5_SIZE.
430 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
433 s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
436 if (ret
== OSPF_MAX_LSA_SIZE
) {
438 "%s: Out of space in LSA stream, left %zd, size %zd",
439 __func__
, STREAM_REMAIN(s
), STREAM_SIZE(s
));
444 /* TOS based routing is not supported. */
445 stream_put_ipv4(s
, id
.s_addr
); /* Link ID. */
446 stream_put_ipv4(s
, data
.s_addr
); /* Link Data. */
447 stream_putc(s
, type
); /* Link Type. */
448 stream_putc(s
, tos
); /* TOS = 0. */
449 stream_putw(s
, cost
); /* Link Cost. */
454 /* Describe Point-to-Point link (Section 12.4.1.1). */
455 static int lsa_link_ptop_set(struct stream
*s
, struct ospf_interface
*oi
)
458 struct ospf_neighbor
*nbr
;
459 struct in_addr id
, mask
, data
;
460 u_int16_t cost
= ospf_link_cost(oi
);
462 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
463 zlog_debug("LSA[Type1]: Set link Point-to-Point");
465 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
466 if (nbr
->state
== NSM_Full
) {
467 if (CHECK_FLAG(oi
->connected
->flags
,
468 ZEBRA_IFA_UNNUMBERED
)) {
469 /* For unnumbered point-to-point networks, the
471 should specify the interface's MIB-II ifIndex
473 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
474 links
+= link_info_set(
475 s
, nbr
->router_id
, data
,
476 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
478 links
+= link_info_set(
480 oi
->address
->u
.prefix4
,
481 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
485 /* no need for a stub link for unnumbered interfaces */
486 if (!CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)) {
487 /* Regardless of the state of the neighboring router, we must
488 add a Type 3 link (stub network).
489 N.B. Options 1 & 2 share basically the same logic. */
490 masklen2ip(oi
->address
->prefixlen
, &mask
);
491 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
493 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
500 /* Describe Broadcast Link. */
501 static int lsa_link_broadcast_set(struct stream
*s
, struct ospf_interface
*oi
)
503 struct ospf_neighbor
*dr
;
504 struct in_addr id
, mask
;
505 u_int16_t cost
= ospf_link_cost(oi
);
507 /* Describe Type 3 Link. */
508 if (oi
->state
== ISM_Waiting
) {
509 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
511 "LSA[Type1]: Interface %s is in state Waiting. "
512 "Adding stub interface",
514 masklen2ip(oi
->address
->prefixlen
, &mask
);
515 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
516 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
520 dr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &DR(oi
));
521 /* Describe Type 2 link. */
522 if (dr
&& (dr
->state
== NSM_Full
523 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
)))
524 && ospf_nbr_count(oi
, NSM_Full
) > 0) {
525 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
527 "LSA[Type1]: Interface %s has a DR. "
528 "Adding transit interface",
530 return link_info_set(s
, DR(oi
), oi
->address
->u
.prefix4
,
531 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
533 /* Describe type 3 link. */
535 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
537 "LSA[Type1]: Interface %s has no DR. "
538 "Adding stub interface",
540 masklen2ip(oi
->address
->prefixlen
, &mask
);
541 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
542 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
547 static int lsa_link_loopback_set(struct stream
*s
, struct ospf_interface
*oi
)
549 struct in_addr id
, mask
;
551 /* Describe Type 3 Link. */
552 if (oi
->state
!= ISM_Loopback
)
555 mask
.s_addr
= 0xffffffff;
556 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
557 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
560 /* Describe Virtual Link. */
561 static int lsa_link_virtuallink_set(struct stream
*s
, struct ospf_interface
*oi
)
563 struct ospf_neighbor
*nbr
;
564 u_int16_t cost
= ospf_link_cost(oi
);
566 if (oi
->state
== ISM_PointToPoint
)
567 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
568 if (nbr
->state
== NSM_Full
) {
569 return link_info_set(s
, nbr
->router_id
,
570 oi
->address
->u
.prefix4
,
571 LSA_LINK_TYPE_VIRTUALLINK
,
578 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
580 /* this function add for support point-to-multipoint ,see rfc2328
582 /* from "edward rrr" <edward_rrr@hotmail.com>
583 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
584 static int lsa_link_ptomp_set(struct stream
*s
, struct ospf_interface
*oi
)
587 struct route_node
*rn
;
588 struct ospf_neighbor
*nbr
= NULL
;
589 struct in_addr id
, mask
;
590 u_int16_t cost
= ospf_link_cost(oi
);
592 mask
.s_addr
= 0xffffffff;
593 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
594 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
596 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
597 zlog_debug("PointToMultipoint: running ptomultip_set");
599 /* Search neighbor, */
600 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
601 if ((nbr
= rn
->info
) != NULL
)
603 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
604 &oi
->ospf
->router_id
))
605 if (nbr
->state
== NSM_Full
)
608 links
+= link_info_set(
610 oi
->address
->u
.prefix4
,
611 LSA_LINK_TYPE_POINTOPOINT
, 0,
613 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
615 "PointToMultipoint: set link to %s",
624 /* Set router-LSA link information. */
625 static int router_lsa_link_set(struct stream
*s
, struct ospf_area
*area
)
627 struct listnode
*node
;
628 struct ospf_interface
*oi
;
631 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
632 struct interface
*ifp
= oi
->ifp
;
634 /* Check interface is up, OSPF is enable. */
635 if (if_is_operative(ifp
)) {
636 if (oi
->state
!= ISM_Down
) {
637 oi
->lsa_pos_beg
= links
;
638 /* Describe each link. */
640 case OSPF_IFTYPE_POINTOPOINT
:
641 links
+= lsa_link_ptop_set(s
, oi
);
643 case OSPF_IFTYPE_BROADCAST
:
644 links
+= lsa_link_broadcast_set(s
, oi
);
646 case OSPF_IFTYPE_NBMA
:
647 links
+= lsa_link_nbma_set(s
, oi
);
649 case OSPF_IFTYPE_POINTOMULTIPOINT
:
650 links
+= lsa_link_ptomp_set(s
, oi
);
652 case OSPF_IFTYPE_VIRTUALLINK
:
654 lsa_link_virtuallink_set(s
, oi
);
656 case OSPF_IFTYPE_LOOPBACK
:
657 links
+= lsa_link_loopback_set(s
, oi
);
659 oi
->lsa_pos_end
= links
;
667 /* Set router-LSA body. */
668 static void ospf_router_lsa_body_set(struct stream
*s
, struct ospf_area
*area
)
674 stream_putc(s
, router_lsa_flags(area
));
676 /* Set Zero fields. */
679 /* Keep pointer to # links. */
680 putp
= stream_get_endp(s
);
685 /* Set all link information. */
686 cnt
= router_lsa_link_set(s
, area
);
688 /* Set # of links here. */
689 stream_putw_at(s
, putp
, cnt
);
692 static int ospf_stub_router_timer(struct thread
*t
)
694 struct ospf_area
*area
= THREAD_ARG(t
);
696 area
->t_stub_router
= NULL
;
698 SET_FLAG(area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
700 /* clear stub route state and generate router-lsa refresh, don't
701 * clobber an administratively set stub-router state though.
703 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
706 UNSET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
708 ospf_router_lsa_update_area(area
);
713 static void ospf_stub_router_check(struct ospf_area
*area
)
715 /* area must either be administratively configured to be stub
716 * or startup-time stub-router must be configured and we must in a
720 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
)) {
721 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
725 /* not admin-stubbed, check whether startup stubbing is configured and
726 * whether it's not been done yet
728 if (CHECK_FLAG(area
->stub_router_state
,
729 OSPF_AREA_WAS_START_STUB_ROUTED
))
732 if (area
->ospf
->stub_router_startup_time
733 == OSPF_STUB_ROUTER_UNCONFIGURED
) {
734 /* stub-router is hence done forever for this area, even if
736 * tries configure it (take effect next restart).
738 SET_FLAG(area
->stub_router_state
,
739 OSPF_AREA_WAS_START_STUB_ROUTED
);
743 /* startup stub-router configured and not yet done */
744 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
746 OSPF_AREA_TIMER_ON(area
->t_stub_router
, ospf_stub_router_timer
,
747 area
->ospf
->stub_router_startup_time
);
750 /* Create new router-LSA. */
751 static struct ospf_lsa
*ospf_router_lsa_new(struct ospf_area
*area
)
753 struct ospf
*ospf
= area
->ospf
;
755 struct lsa_header
*lsah
;
756 struct ospf_lsa
*new;
759 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
760 zlog_debug("LSA[Type1]: Create router-LSA instance");
762 /* check whether stub-router is desired, and if this is the first
765 ospf_stub_router_check(area
);
767 /* Create a stream for LSA. */
768 s
= stream_new(OSPF_MAX_LSA_SIZE
);
769 /* Set LSA common header fields. */
770 lsa_header_set(s
, LSA_OPTIONS_GET(area
) | LSA_OPTIONS_NSSA_GET(area
),
771 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
773 /* Set router-LSA body fields. */
774 ospf_router_lsa_body_set(s
, area
);
777 length
= stream_get_endp(s
);
778 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
779 lsah
->length
= htons(length
);
781 /* Now, create OSPF LSA instance. */
782 if ((new = ospf_lsa_new()) == NULL
) {
783 zlog_err("%s: Unable to create new lsa", __func__
);
788 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
790 /* Copy LSA data to store, discard stream. */
791 new->data
= ospf_lsa_data_new(length
);
792 memcpy(new->data
, lsah
, length
);
798 /* Originate Router-LSA. */
799 static struct ospf_lsa
*ospf_router_lsa_originate(struct ospf_area
*area
)
801 struct ospf_lsa
*new;
803 /* Create new router-LSA instance. */
804 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
805 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
810 if (new->data
->adv_router
.s_addr
== 0) {
811 if (IS_DEBUG_OSPF_EVENT
)
812 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
813 ospf_lsa_discard(new);
817 /* Install LSA to LSDB. */
818 new = ospf_lsa_install(area
->ospf
, NULL
, new);
820 /* Update LSA origination count. */
821 area
->ospf
->lsa_originate_count
++;
823 /* Flooding new LSA through area. */
824 ospf_flood_through_area(area
, NULL
, new);
826 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
827 zlog_debug("LSA[Type%d:%s]: Originate router-LSA %p",
828 new->data
->type
, inet_ntoa(new->data
->id
),
830 ospf_lsa_header_dump(new->data
);
836 /* Refresh router-LSA. */
837 static struct ospf_lsa
*ospf_router_lsa_refresh(struct ospf_lsa
*lsa
)
839 struct ospf_area
*area
= lsa
->area
;
840 struct ospf_lsa
*new;
845 /* Delete LSA from neighbor retransmit-list. */
846 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
848 /* Unregister LSA from refresh-list */
849 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
851 /* Create new router-LSA instance. */
852 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
853 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
857 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
859 ospf_lsa_install(area
->ospf
, NULL
, new);
861 /* Flood LSA through area. */
862 ospf_flood_through_area(area
, NULL
, new);
865 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
866 zlog_debug("LSA[Type%d:%s]: router-LSA refresh",
867 new->data
->type
, inet_ntoa(new->data
->id
));
868 ospf_lsa_header_dump(new->data
);
874 int ospf_router_lsa_update_area(struct ospf_area
*area
)
876 if (IS_DEBUG_OSPF_EVENT
)
877 zlog_debug("[router-LSA]: (router-LSA area update)");
879 /* Now refresh router-LSA. */
880 if (area
->router_lsa_self
)
881 ospf_lsa_refresh(area
->ospf
, area
->router_lsa_self
);
882 /* Newly originate router-LSA. */
884 ospf_router_lsa_originate(area
);
889 int ospf_router_lsa_update(struct ospf
*ospf
)
891 struct listnode
*node
, *nnode
;
892 struct ospf_area
*area
;
894 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
895 zlog_debug("Timer[router-LSA Update]: (timer expire)");
897 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
898 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
899 struct router_lsa
*rl
;
900 const char *area_str
;
902 /* Keep Area ID string. */
903 area_str
= AREA_NAME(area
);
905 /* If LSA not exist in this Area, originate new. */
907 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
909 "LSA[Type1]: Create router-LSA for Area %s",
912 ospf_router_lsa_originate(area
);
914 /* If router-ID is changed, Link ID must change.
915 First flush old LSA, then originate new. */
916 else if (!IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
)) {
917 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
919 "LSA[Type%d:%s]: Refresh router-LSA for Area %s",
921 inet_ntoa(lsa
->data
->id
), area_str
);
922 ospf_refresher_unregister_lsa(ospf
, lsa
);
923 ospf_lsa_flush_area(lsa
, area
);
924 ospf_lsa_unlock(&area
->router_lsa_self
);
925 area
->router_lsa_self
= NULL
;
927 /* Refresh router-LSA, (not install) and flood through
929 ospf_router_lsa_update_area(area
);
931 rl
= (struct router_lsa
*)lsa
->data
;
932 /* Refresh router-LSA, (not install) and flood through
934 if (rl
->flags
!= ospf
->flags
)
935 ospf_router_lsa_update_area(area
);
943 /* network-LSA related functions. */
944 /* Originate Network-LSA. */
945 static void ospf_network_lsa_body_set(struct stream
*s
,
946 struct ospf_interface
*oi
)
949 struct route_node
*rn
;
950 struct ospf_neighbor
*nbr
;
952 masklen2ip(oi
->address
->prefixlen
, &mask
);
953 stream_put_ipv4(s
, mask
.s_addr
);
955 /* The network-LSA lists those routers that are fully adjacent to
956 the Designated Router; each fully adjacent router is identified by
957 its OSPF Router ID. The Designated Router includes itself in this
958 list. RFC2328, Section 12.4.2 */
960 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
961 if ((nbr
= rn
->info
) != NULL
)
962 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
963 stream_put_ipv4(s
, nbr
->router_id
.s_addr
);
966 static struct ospf_lsa
*ospf_network_lsa_new(struct ospf_interface
*oi
)
969 struct ospf_lsa
*new;
970 struct lsa_header
*lsah
;
971 struct ospf_if_params
*oip
;
974 /* If there are no neighbours on this network (the net is stub),
975 the router does not originate network-LSA (see RFC 12.4.2) */
976 if (oi
->full_nbrs
== 0)
979 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
980 zlog_debug("LSA[Type2]: Create network-LSA instance");
982 /* Create new stream for LSA. */
983 s
= stream_new(OSPF_MAX_LSA_SIZE
);
984 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
986 lsa_header_set(s
, (OPTIONS(oi
) | LSA_OPTIONS_GET(oi
->area
)),
987 OSPF_NETWORK_LSA
, DR(oi
), oi
->ospf
->router_id
);
989 /* Set network-LSA body fields. */
990 ospf_network_lsa_body_set(s
, oi
);
993 length
= stream_get_endp(s
);
994 lsah
->length
= htons(length
);
996 /* Create OSPF LSA instance. */
997 if ((new = ospf_lsa_new()) == NULL
) {
998 zlog_err("%s: ospf_lsa_new returned NULL", __func__
);
1002 new->area
= oi
->area
;
1003 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1005 /* Copy LSA to store. */
1006 new->data
= ospf_lsa_data_new(length
);
1007 memcpy(new->data
, lsah
, length
);
1010 /* Remember prior network LSA sequence numbers, even if we stop
1011 * originating one for this oi, to try avoid re-originating LSAs with a
1012 * prior sequence number, and thus speed up adjency forming &
1015 if ((oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
))) {
1016 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1017 new->data
->ls_seqnum
= lsa_seqnum_increment(new);
1019 oip
= ospf_get_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1020 ospf_if_update_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1022 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1027 /* Originate network-LSA. */
1028 void ospf_network_lsa_update(struct ospf_interface
*oi
)
1030 struct ospf_lsa
*new;
1032 if (oi
->network_lsa_self
!= NULL
) {
1033 ospf_lsa_refresh(oi
->ospf
, oi
->network_lsa_self
);
1037 /* Create new network-LSA instance. */
1038 new = ospf_network_lsa_new(oi
);
1042 /* Install LSA to LSDB. */
1043 new = ospf_lsa_install(oi
->ospf
, oi
, new);
1045 /* Update LSA origination count. */
1046 oi
->ospf
->lsa_originate_count
++;
1048 /* Flooding new LSA through area. */
1049 ospf_flood_through_area(oi
->area
, NULL
, new);
1051 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1052 zlog_debug("LSA[Type%d:%s]: Originate network-LSA %p",
1053 new->data
->type
, inet_ntoa(new->data
->id
),
1055 ospf_lsa_header_dump(new->data
);
1061 static struct ospf_lsa
*ospf_network_lsa_refresh(struct ospf_lsa
*lsa
)
1063 struct ospf_area
*area
= lsa
->area
;
1064 struct ospf_lsa
*new, *new2
;
1065 struct ospf_if_params
*oip
;
1066 struct ospf_interface
*oi
;
1070 /* Retrieve the oi for the network LSA */
1071 oi
= ospf_if_lookup_by_local_addr(area
->ospf
, NULL
, lsa
->data
->id
);
1073 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1075 "LSA[Type%d:%s]: network-LSA refresh: "
1076 "no oi found, ick, ignoring.",
1077 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
1078 ospf_lsa_header_dump(lsa
->data
);
1082 /* Delete LSA from neighbor retransmit-list. */
1083 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
1085 /* Unregister LSA from refresh-list */
1086 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
1088 /* Create new network-LSA instance. */
1089 new = ospf_network_lsa_new(oi
);
1093 oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1094 assert(oip
!= NULL
);
1095 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
=
1096 lsa_seqnum_increment(lsa
);
1098 new2
= ospf_lsa_install(area
->ospf
, oi
, new);
1100 assert(new2
== new);
1102 /* Flood LSA through aera. */
1103 ospf_flood_through_area(area
, NULL
, new);
1105 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1106 zlog_debug("LSA[Type%d:%s]: network-LSA refresh",
1107 new->data
->type
, inet_ntoa(new->data
->id
));
1108 ospf_lsa_header_dump(new->data
);
1114 static void stream_put_ospf_metric(struct stream
*s
, u_int32_t metric_value
)
1119 /* Put 0 metric. TOS metric is not supported. */
1120 metric
= htonl(metric_value
);
1121 mp
= (char *)&metric
;
1123 stream_put(s
, mp
, 3);
1126 /* summary-LSA related functions. */
1127 static void ospf_summary_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1130 struct in_addr mask
;
1132 masklen2ip(p
->prefixlen
, &mask
);
1134 /* Put Network Mask. */
1135 stream_put_ipv4(s
, mask
.s_addr
);
1138 stream_putc(s
, (u_char
)0);
1141 stream_put_ospf_metric(s
, metric
);
1144 static struct ospf_lsa
*ospf_summary_lsa_new(struct ospf_area
*area
,
1145 struct prefix
*p
, u_int32_t metric
,
1149 struct ospf_lsa
*new;
1150 struct lsa_header
*lsah
;
1153 if (id
.s_addr
== 0xffffffff) {
1154 /* Maybe Link State ID not available. */
1155 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1157 "LSA[Type%d]: Link ID not available, can't originate",
1162 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1163 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1165 /* Create new stream for LSA. */
1166 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1167 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1169 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_SUMMARY_LSA
, id
,
1170 area
->ospf
->router_id
);
1172 /* Set summary-LSA body fields. */
1173 ospf_summary_lsa_body_set(s
, p
, metric
);
1176 length
= stream_get_endp(s
);
1177 lsah
->length
= htons(length
);
1179 /* Create OSPF LSA instance. */
1180 new = ospf_lsa_new();
1182 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1184 /* Copy LSA to store. */
1185 new->data
= ospf_lsa_data_new(length
);
1186 memcpy(new->data
, lsah
, length
);
1192 /* Originate Summary-LSA. */
1193 struct ospf_lsa
*ospf_summary_lsa_originate(struct prefix_ipv4
*p
,
1195 struct ospf_area
*area
)
1197 struct ospf_lsa
*new;
1200 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
);
1202 if (id
.s_addr
== 0xffffffff) {
1203 /* Maybe Link State ID not available. */
1204 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1206 "LSA[Type%d]: Link ID not available, can't originate",
1211 /* Create new summary-LSA instance. */
1212 if (!(new = ospf_summary_lsa_new(area
, (struct prefix
*)p
, metric
, id
)))
1215 /* Instlal LSA to LSDB. */
1216 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1218 /* Update LSA origination count. */
1219 area
->ospf
->lsa_originate_count
++;
1221 /* Flooding new LSA through area. */
1222 ospf_flood_through_area(area
, NULL
, new);
1224 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1225 zlog_debug("LSA[Type%d:%s]: Originate summary-LSA %p",
1226 new->data
->type
, inet_ntoa(new->data
->id
),
1228 ospf_lsa_header_dump(new->data
);
1234 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
1235 struct ospf_lsa
*lsa
)
1237 struct ospf_lsa
*new;
1238 struct summary_lsa
*sl
;
1244 sl
= (struct summary_lsa
*)lsa
->data
;
1245 p
.prefixlen
= ip_masklen(sl
->mask
);
1246 new = ospf_summary_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1252 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1254 ospf_lsa_install(ospf
, NULL
, new);
1256 /* Flood LSA through AS. */
1257 ospf_flood_through_area(new->area
, NULL
, new);
1259 /* Debug logging. */
1260 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1261 zlog_debug("LSA[Type%d:%s]: summary-LSA refresh",
1262 new->data
->type
, inet_ntoa(new->data
->id
));
1263 ospf_lsa_header_dump(new->data
);
1270 /* summary-ASBR-LSA related functions. */
1271 static void ospf_summary_asbr_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1274 /* Put Network Mask. */
1275 stream_put_ipv4(s
, (u_int32_t
)0);
1278 stream_putc(s
, (u_char
)0);
1281 stream_put_ospf_metric(s
, metric
);
1284 static struct ospf_lsa
*ospf_summary_asbr_lsa_new(struct ospf_area
*area
,
1290 struct ospf_lsa
*new;
1291 struct lsa_header
*lsah
;
1294 if (id
.s_addr
== 0xffffffff) {
1295 /* Maybe Link State ID not available. */
1296 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1298 "LSA[Type%d]: Link ID not available, can't originate",
1299 OSPF_ASBR_SUMMARY_LSA
);
1303 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1304 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1306 /* Create new stream for LSA. */
1307 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1308 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1310 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_ASBR_SUMMARY_LSA
, id
,
1311 area
->ospf
->router_id
);
1313 /* Set summary-LSA body fields. */
1314 ospf_summary_asbr_lsa_body_set(s
, p
, metric
);
1317 length
= stream_get_endp(s
);
1318 lsah
->length
= htons(length
);
1320 /* Create OSPF LSA instance. */
1321 new = ospf_lsa_new();
1323 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1325 /* Copy LSA to store. */
1326 new->data
= ospf_lsa_data_new(length
);
1327 memcpy(new->data
, lsah
, length
);
1333 /* Originate summary-ASBR-LSA. */
1334 struct ospf_lsa
*ospf_summary_asbr_lsa_originate(struct prefix_ipv4
*p
,
1336 struct ospf_area
*area
)
1338 struct ospf_lsa
*new;
1341 id
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_ASBR_SUMMARY_LSA
,
1344 if (id
.s_addr
== 0xffffffff) {
1345 /* Maybe Link State ID not available. */
1346 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1348 "LSA[Type%d]: Link ID not available, can't originate",
1349 OSPF_ASBR_SUMMARY_LSA
);
1353 /* Create new summary-LSA instance. */
1354 new = ospf_summary_asbr_lsa_new(area
, (struct prefix
*)p
, metric
, id
);
1358 /* Install LSA to LSDB. */
1359 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1361 /* Update LSA origination count. */
1362 area
->ospf
->lsa_originate_count
++;
1364 /* Flooding new LSA through area. */
1365 ospf_flood_through_area(area
, NULL
, new);
1367 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1368 zlog_debug("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1369 new->data
->type
, inet_ntoa(new->data
->id
),
1371 ospf_lsa_header_dump(new->data
);
1377 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
1378 struct ospf_lsa
*lsa
)
1380 struct ospf_lsa
*new;
1381 struct summary_lsa
*sl
;
1387 sl
= (struct summary_lsa
*)lsa
->data
;
1388 p
.prefixlen
= ip_masklen(sl
->mask
);
1389 new = ospf_summary_asbr_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1394 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1396 ospf_lsa_install(ospf
, NULL
, new);
1398 /* Flood LSA through area. */
1399 ospf_flood_through_area(new->area
, NULL
, new);
1401 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1402 zlog_debug("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1403 new->data
->type
, inet_ntoa(new->data
->id
));
1404 ospf_lsa_header_dump(new->data
);
1410 /* AS-external-LSA related functions. */
1412 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1413 is connected, else 0*/
1414 static struct in_addr
ospf_external_lsa_nexthop_get(struct ospf
*ospf
,
1415 struct in_addr nexthop
)
1419 struct listnode
*node
;
1420 struct ospf_interface
*oi
;
1424 if (!nexthop
.s_addr
)
1427 /* Check whether nexthop is covered by OSPF network. */
1428 nh
.family
= AF_INET
;
1429 nh
.u
.prefix4
= nexthop
;
1430 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1432 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1433 * better to make use of the per-ifp table of ois.
1435 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1436 if (if_is_operative(oi
->ifp
))
1437 if (oi
->address
->family
== AF_INET
)
1438 if (prefix_match(oi
->address
, &nh
))
1444 /* NSSA-external-LSA related functions. */
1446 /* Get 1st IP connection for Forward Addr */
1448 struct in_addr
ospf_get_ip_from_ifp(struct ospf_interface
*oi
)
1454 if (if_is_operative(oi
->ifp
))
1455 return oi
->address
->u
.prefix4
;
1460 /* Get 1st IP connection for Forward Addr */
1461 struct in_addr
ospf_get_nssa_ip(struct ospf_area
*area
)
1464 struct in_addr best_default
;
1465 struct listnode
*node
;
1466 struct ospf_interface
*oi
;
1469 best_default
.s_addr
= 0;
1471 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->oiflist
, node
, oi
)) {
1472 if (if_is_operative(oi
->ifp
))
1473 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1475 && oi
->address
->family
== AF_INET
) {
1476 if (best_default
.s_addr
== 0)
1478 oi
->address
->u
.prefix4
;
1479 if (oi
->area
== area
)
1480 return oi
->address
->u
.prefix4
;
1483 if (best_default
.s_addr
!= 0)
1484 return best_default
;
1486 if (best_default
.s_addr
!= 0)
1487 return best_default
;
1492 #define DEFAULT_DEFAULT_METRIC 20
1493 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1494 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1496 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1498 int metric_type(struct ospf
*ospf
, u_char src
, u_short instance
)
1500 struct ospf_redist
*red
;
1502 red
= ospf_redist_lookup(ospf
, src
, instance
);
1504 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
1505 : red
->dmetric
.type
);
1508 int metric_value(struct ospf
*ospf
, u_char src
, u_short instance
)
1510 struct ospf_redist
*red
;
1512 red
= ospf_redist_lookup(ospf
, src
, instance
);
1513 if (!red
|| red
->dmetric
.value
< 0) {
1514 if (src
== DEFAULT_ROUTE
) {
1515 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1516 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1518 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1519 } else if (ospf
->default_metric
< 0)
1520 return DEFAULT_DEFAULT_METRIC
;
1522 return ospf
->default_metric
;
1525 return red
->dmetric
.value
;
1528 /* Set AS-external-LSA body. */
1529 static void ospf_external_lsa_body_set(struct stream
*s
,
1530 struct external_info
*ei
,
1533 struct prefix_ipv4
*p
= &ei
->p
;
1534 struct in_addr mask
, fwd_addr
;
1540 /* Put Network Mask. */
1541 masklen2ip(p
->prefixlen
, &mask
);
1542 stream_put_ipv4(s
, mask
.s_addr
);
1544 /* If prefix is default, specify DEFAULT_ROUTE. */
1545 type
= is_prefix_default(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1546 instance
= is_prefix_default(&ei
->p
) ? 0 : ei
->instance
;
1548 mtype
= (ROUTEMAP_METRIC_TYPE(ei
) != -1)
1549 ? ROUTEMAP_METRIC_TYPE(ei
)
1550 : metric_type(ospf
, type
, instance
);
1552 mvalue
= (ROUTEMAP_METRIC(ei
) != -1)
1553 ? ROUTEMAP_METRIC(ei
)
1554 : metric_value(ospf
, type
, instance
);
1556 /* Put type of external metric. */
1557 stream_putc(s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1559 /* Put 0 metric. TOS metric is not supported. */
1560 stream_put_ospf_metric(s
, mvalue
);
1562 /* Get forwarding address to nexthop if on the Connection List, else 0.
1564 fwd_addr
= ospf_external_lsa_nexthop_get(ospf
, ei
->nexthop
);
1566 /* Put forwarding address. */
1567 stream_put_ipv4(s
, fwd_addr
.s_addr
);
1570 stream_putl(s
, ei
->tag
);
1573 /* Create new external-LSA. */
1574 static struct ospf_lsa
*ospf_external_lsa_new(struct ospf
*ospf
,
1575 struct external_info
*ei
,
1576 struct in_addr
*old_id
)
1579 struct lsa_header
*lsah
;
1580 struct ospf_lsa
*new;
1585 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1587 "LSA[Type5]: External info is NULL, can't originate");
1591 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1592 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1594 /* If old Link State ID is specified, refresh LSA with same ID. */
1597 /* Get Link State with unique ID. */
1599 id
= ospf_lsa_unique_id(ospf
, ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
,
1601 if (id
.s_addr
== 0xffffffff) {
1602 /* Maybe Link State ID not available. */
1603 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1605 "LSA[Type5]: Link ID not available, can't originate");
1610 /* Create new stream for LSA. */
1611 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1612 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1614 /* Set LSA common header fields. */
1615 lsa_header_set(s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
, id
,
1618 /* Set AS-external-LSA body fields. */
1619 ospf_external_lsa_body_set(s
, ei
, ospf
);
1622 length
= stream_get_endp(s
);
1623 lsah
->length
= htons(length
);
1625 /* Now, create OSPF LSA instance. */
1626 new = ospf_lsa_new();
1628 SET_FLAG(new->flags
,
1629 OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1631 /* Copy LSA data to store, discard stream. */
1632 new->data
= ospf_lsa_data_new(length
);
1633 memcpy(new->data
, lsah
, length
);
1640 static void ospf_install_flood_nssa(struct ospf
*ospf
, struct ospf_lsa
*lsa
,
1641 struct external_info
*ei
)
1643 struct ospf_lsa
*new;
1644 struct as_external_lsa
*extlsa
;
1645 struct ospf_area
*area
;
1646 struct listnode
*node
, *nnode
;
1648 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1649 * which originated from an NSSA area. In which case it should not be
1650 * flooded back to NSSA areas.
1652 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1655 /* NSSA Originate or Refresh (If anyNSSA)
1657 LSA is self-originated. And just installed as Type-5.
1658 Additionally, install as Type-7 LSDB for every attached NSSA.
1660 P-Bit controls which ABR performs translation to outside world; If
1661 we are an ABR....do not set the P-bit, because we send the Type-5,
1662 not as the ABR Translator, but as the ASBR owner within the AS!
1664 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1665 elected ABR Translator will see the P-bit, Translate, and re-flood.
1667 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1668 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1670 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
1671 /* Don't install Type-7 LSA's into nonNSSA area */
1672 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1675 /* make lsa duplicate, lock=1 */
1676 new = ospf_lsa_dup(lsa
);
1678 new->data
->type
= OSPF_AS_NSSA_LSA
;
1680 /* set P-bit if not ABR */
1681 if (!IS_OSPF_ABR(ospf
)) {
1682 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1684 /* set non-zero FWD ADDR
1686 draft-ietf-ospf-nssa-update-09.txt
1688 if the network between the NSSA AS boundary router and
1690 adjacent AS is advertised into OSPF as an internal OSPF
1692 the forwarding address should be the next op address as
1694 currently done with type-5 LSAs. If the intervening
1696 not adversited into OSPF as an internal OSPF route and
1698 type-7 LSA's P-bit is set a forwarding address should be
1699 selected from one of the router's active OSPF inteface
1701 which belong to the NSSA. If no such addresses exist,
1703 no type-7 LSA's with the P-bit set should originate from
1707 /* kevinm: not updating lsa anymore, just new */
1708 extlsa
= (struct as_external_lsa
*)(new->data
);
1710 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0)
1711 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(
1712 area
); /* this NSSA area in ifp */
1714 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0) {
1715 if (IS_DEBUG_OSPF_NSSA
)
1717 "LSA[Type-7]: Could not build FWD-ADDR");
1718 ospf_lsa_discard(new);
1723 /* install also as Type-7 */
1724 ospf_lsa_install(ospf
, NULL
,
1725 new); /* Remove Old, Lock New = 2 */
1727 /* will send each copy, lock=2+n */
1728 ospf_flood_through_as(
1729 ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1733 static struct ospf_lsa
*ospf_lsa_translated_nssa_new(struct ospf
*ospf
,
1734 struct ospf_lsa
*type7
)
1737 struct ospf_lsa
*new;
1738 struct as_external_lsa
*ext
, *extnew
;
1739 struct external_info ei
;
1741 ext
= (struct as_external_lsa
*)(type7
->data
);
1743 /* need external_info struct, fill in bare minimum */
1744 ei
.p
.family
= AF_INET
;
1745 ei
.p
.prefix
= type7
->data
->id
;
1746 ei
.p
.prefixlen
= ip_masklen(ext
->mask
);
1747 ei
.type
= ZEBRA_ROUTE_OSPF
;
1748 ei
.nexthop
= ext
->header
.adv_router
;
1749 ei
.route_map_set
.metric
= -1;
1750 ei
.route_map_set
.metric_type
= -1;
1753 if ((new = ospf_external_lsa_new(ospf
, &ei
, &type7
->data
->id
))
1755 if (IS_DEBUG_OSPF_NSSA
)
1757 "ospf_nssa_translate_originate(): Could not originate "
1758 "Translated Type-5 for %s",
1759 inet_ntoa(ei
.p
.prefix
));
1763 extnew
= (struct as_external_lsa
*)(new->data
);
1765 /* copy over Type-7 data to new */
1766 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1767 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1768 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1769 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1771 /* add translated flag, checksum and lock new lsa */
1772 SET_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1773 new = ospf_lsa_lock(new);
1778 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1779 struct ospf_lsa
*ospf_translated_nssa_originate(struct ospf
*ospf
,
1780 struct ospf_lsa
*type7
)
1782 struct ospf_lsa
*new;
1783 struct as_external_lsa
*extnew
;
1785 /* we cant use ospf_external_lsa_originate() as we need to set
1786 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1789 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1790 if (IS_DEBUG_OSPF_NSSA
)
1792 "ospf_translated_nssa_originate(): Could not translate "
1793 "Type-7, Id %s, to Type-5",
1794 inet_ntoa(type7
->data
->id
));
1798 extnew
= (struct as_external_lsa
*)new;
1800 if (IS_DEBUG_OSPF_NSSA
) {
1802 "ospf_translated_nssa_originate(): "
1803 "translated Type 7, installed:");
1804 ospf_lsa_header_dump(new->data
);
1805 zlog_debug(" Network mask: %d", ip_masklen(extnew
->mask
));
1806 zlog_debug(" Forward addr: %s",
1807 inet_ntoa(extnew
->e
[0].fwd_addr
));
1810 if ((new = ospf_lsa_install(ospf
, NULL
, new)) == NULL
) {
1811 if (IS_DEBUG_OSPF_NSSA
)
1813 "ospf_lsa_translated_nssa_originate(): "
1814 "Could not install LSA "
1816 inet_ntoa(type7
->data
->id
));
1820 ospf
->lsa_originate_count
++;
1821 ospf_flood_through_as(ospf
, NULL
, new);
1826 /* Refresh Translated from NSSA AS-external-LSA. */
1827 struct ospf_lsa
*ospf_translated_nssa_refresh(struct ospf
*ospf
,
1828 struct ospf_lsa
*type7
,
1829 struct ospf_lsa
*type5
)
1831 struct ospf_lsa
*new = NULL
;
1833 /* Sanity checks. */
1834 assert(type7
|| type5
);
1835 if (!(type7
|| type5
))
1838 assert(type7
->data
);
1840 assert(type5
->data
);
1841 assert(ospf
->anyNSSA
);
1843 /* get required data according to what has been given */
1844 if (type7
&& type5
== NULL
) {
1845 /* find the translated Type-5 for this Type-7 */
1846 struct as_external_lsa
*ext
=
1847 (struct as_external_lsa
*)(type7
->data
);
1848 struct prefix_ipv4 p
= {
1849 .prefix
= type7
->data
->id
,
1850 .prefixlen
= ip_masklen(ext
->mask
),
1854 type5
= ospf_external_info_find_lsa(ospf
, &p
);
1855 } else if (type5
&& type7
== NULL
) {
1856 /* find the type-7 from which supplied type-5 was translated,
1857 * ie find first type-7 with same LSA Id.
1859 struct listnode
*ln
, *lnn
;
1860 struct route_node
*rn
;
1861 struct ospf_lsa
*lsa
;
1862 struct ospf_area
*area
;
1864 for (ALL_LIST_ELEMENTS(ospf
->areas
, ln
, lnn
, area
)) {
1865 if (area
->external_routing
!= OSPF_AREA_NSSA
&& !type7
)
1868 LSDB_LOOP(NSSA_LSDB(area
), rn
, lsa
)
1870 if (lsa
->data
->id
.s_addr
1871 == type5
->data
->id
.s_addr
) {
1879 /* do we have type7? */
1881 if (IS_DEBUG_OSPF_NSSA
)
1883 "ospf_translated_nssa_refresh(): no Type-7 found for "
1885 inet_ntoa(type5
->data
->id
));
1889 /* do we have valid translated type5? */
1890 if (type5
== NULL
|| !CHECK_FLAG(type5
->flags
, OSPF_LSA_LOCAL_XLT
)) {
1891 if (IS_DEBUG_OSPF_NSSA
)
1893 "ospf_translated_nssa_refresh(): No translated Type-5 "
1894 "found for Type-7 with Id %s",
1895 inet_ntoa(type7
->data
->id
));
1899 /* Delete LSA from neighbor retransmit-list. */
1900 ospf_ls_retransmit_delete_nbr_as(ospf
, type5
);
1902 /* create new translated LSA */
1903 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
1904 if (IS_DEBUG_OSPF_NSSA
)
1906 "ospf_translated_nssa_refresh(): Could not translate "
1907 "Type-7 for %s to Type-5",
1908 inet_ntoa(type7
->data
->id
));
1912 if (!(new = ospf_lsa_install(ospf
, NULL
, new))) {
1913 if (IS_DEBUG_OSPF_NSSA
)
1915 "ospf_translated_nssa_refresh(): Could not install "
1916 "translated LSA, Id %s",
1917 inet_ntoa(type7
->data
->id
));
1921 /* Flood LSA through area. */
1922 ospf_flood_through_as(ospf
, NULL
, new);
1927 int is_prefix_default(struct prefix_ipv4
*p
)
1929 struct prefix_ipv4 q
;
1932 q
.prefix
.s_addr
= 0;
1935 return prefix_same((struct prefix
*)p
, (struct prefix
*)&q
);
1938 /* Originate an AS-external-LSA, install and flood. */
1939 struct ospf_lsa
*ospf_external_lsa_originate(struct ospf
*ospf
,
1940 struct external_info
*ei
)
1942 struct ospf_lsa
*new;
1944 /* Added for NSSA project....
1946 External LSAs are originated in ASBRs as usual, but for NSSA
1948 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1949 every area. The Type-7's are flooded to every IR and every ABR; We
1950 install the Type-5 LSDB so that the normal "refresh" code operates
1951 as usual, and flag them as not used during ASE calculations. The
1952 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
1953 Address of non-zero.
1955 If an ABR is the elected NSSA translator, following SPF and during
1956 the ABR task it will translate all the scanned Type-7's, with P-bit
1957 ON and not-self generated, and translate to Type-5's throughout the
1960 A difference in operation depends whether this ASBR is an ABR
1961 or not. If not an ABR, the P-bit is ON, to indicate that any
1962 elected NSSA-ABR can perform its translation.
1964 If an ABR, the P-bit is OFF; No ABR will perform translation and
1965 this ASBR will flood the Type-5 LSA as usual.
1967 For the case where this ASBR is not an ABR, the ASE calculations
1968 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
1969 demonstrate to the user that there are LSA's that belong to any
1972 Finally, it just so happens that when the ABR is translating every
1973 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
1974 approved Type-5 (translated from Type-7); at the end of translation
1975 if any Translated Type-5's remain unapproved, then they must be
1976 flushed from the AS.
1980 /* Check the AS-external-LSA should be originated. */
1981 if (!ospf_redistribute_check(ospf
, ei
, NULL
))
1984 /* Create new AS-external-LSA instance. */
1985 if ((new = ospf_external_lsa_new(ospf
, ei
, NULL
)) == NULL
) {
1986 if (IS_DEBUG_OSPF_EVENT
)
1988 "LSA[Type5:%s]: Could not originate AS-external-LSA",
1989 inet_ntoa(ei
->p
.prefix
));
1993 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
1994 ospf_lsa_install(ospf
, NULL
, new);
1996 /* Update LSA origination count. */
1997 ospf
->lsa_originate_count
++;
1999 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2000 ospf_flood_through_as(ospf
, NULL
, new);
2002 /* If there is any attached NSSA, do special handling */
2003 if (ospf
->anyNSSA
&&
2004 /* stay away from translated LSAs! */
2005 !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2006 ospf_install_flood_nssa(
2007 ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2009 /* Debug logging. */
2010 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2011 zlog_debug("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2012 new->data
->type
, inet_ntoa(new->data
->id
),
2014 ospf_lsa_header_dump(new->data
);
2020 /* Originate AS-external-LSA from external info with initial flag. */
2021 int ospf_external_lsa_originate_timer(struct thread
*thread
)
2023 struct ospf
*ospf
= THREAD_ARG(thread
);
2024 struct route_node
*rn
;
2025 struct external_info
*ei
;
2026 struct route_table
*rt
;
2027 int type
= THREAD_VAL(thread
);
2028 struct list
*ext_list
;
2029 struct listnode
*node
;
2030 struct ospf_external
*ext
;
2032 ospf
->t_external_lsa
= NULL
;
2034 ext_list
= om
->external
[type
];
2038 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
2039 /* Originate As-external-LSA from all type of distribute source.
2041 if ((rt
= ext
->external_info
))
2042 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
))
2043 if ((ei
= rn
->info
) != NULL
)
2044 if (!is_prefix_default(
2045 (struct prefix_ipv4
*)&ei
2047 if (!ospf_external_lsa_originate(
2050 "LSA: AS-external-LSA was not originated.");
2055 static struct external_info
*ospf_default_external_info(struct ospf
*ospf
)
2058 struct route_node
*rn
;
2059 struct prefix_ipv4 p
;
2062 p
.prefix
.s_addr
= 0;
2065 /* First, lookup redistributed default route. */
2066 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
2067 struct list
*ext_list
;
2068 struct listnode
*node
;
2069 struct ospf_external
*ext
;
2071 if (type
== ZEBRA_ROUTE_OSPF
)
2074 ext_list
= om
->external
[type
];
2078 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2079 rn
= route_node_lookup(ext
->external_info
,
2080 (struct prefix
*)&p
);
2082 route_unlock_node(rn
);
2084 if (ospf_redistribute_check(ospf
, rn
->info
,
2094 int ospf_default_originate_timer(struct thread
*thread
)
2096 struct prefix_ipv4 p
;
2097 struct in_addr nexthop
;
2098 struct external_info
*ei
;
2101 ospf
= THREAD_ARG(thread
);
2104 p
.prefix
.s_addr
= 0;
2107 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ALWAYS
) {
2108 /* If there is no default route via redistribute,
2109 then originate AS-external-LSA with nexthop 0 (self). */
2111 ospf_external_info_add(DEFAULT_ROUTE
, 0, p
, 0, nexthop
, 0);
2114 if ((ei
= ospf_default_external_info(ospf
)))
2115 ospf_external_lsa_originate(ospf
, ei
);
2120 /* Flush any NSSA LSAs for given prefix */
2121 void ospf_nssa_lsa_flush(struct ospf
*ospf
, struct prefix_ipv4
*p
)
2123 struct listnode
*node
, *nnode
;
2124 struct ospf_lsa
*lsa
;
2125 struct ospf_area
*area
;
2127 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2128 if (area
->external_routing
== OSPF_AREA_NSSA
) {
2129 if (!(lsa
= ospf_lsa_lookup(area
, OSPF_AS_NSSA_LSA
,
2131 ospf
->router_id
))) {
2132 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2134 "LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2135 inet_ntoa(p
->prefix
),
2139 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
2140 if (!IS_LSA_MAXAGE(lsa
)) {
2141 ospf_refresher_unregister_lsa(ospf
, lsa
);
2142 ospf_lsa_flush_area(lsa
, area
);
2148 /* Flush an AS-external-LSA from LSDB and routing domain. */
2149 void ospf_external_lsa_flush(struct ospf
*ospf
, u_char type
,
2150 struct prefix_ipv4
*p
,
2151 ifindex_t ifindex
/*, struct in_addr nexthop */)
2153 struct ospf_lsa
*lsa
;
2155 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2156 zlog_debug("LSA: Flushing AS-external-LSA %s/%d",
2157 inet_ntoa(p
->prefix
), p
->prefixlen
);
2159 /* First lookup LSA from LSDB. */
2160 if (!(lsa
= ospf_external_info_find_lsa(ospf
, p
))) {
2161 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2163 "LSA: There is no such AS-external-LSA %s/%d in LSDB",
2164 inet_ntoa(p
->prefix
), p
->prefixlen
);
2168 /* If LSA is selforiginated, not a translated LSA, and there is
2169 * NSSA area, flush Type-7 LSA's at first.
2171 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2172 && !(CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2173 ospf_nssa_lsa_flush(ospf
, p
);
2175 /* Sweep LSA from Link State Retransmit List. */
2176 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2178 /* There must be no self-originated LSA in rtrs_external. */
2180 /* Remove External route from Zebra. */
2181 ospf_zebra_delete ((struct prefix_ipv4
*) p
, &nexthop
);
2184 if (!IS_LSA_MAXAGE(lsa
)) {
2185 /* Unregister LSA from Refresh queue. */
2186 ospf_refresher_unregister_lsa(ospf
, lsa
);
2188 /* Flush AS-external-LSA through AS. */
2189 ospf_lsa_flush_as(ospf
, lsa
);
2192 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2193 zlog_debug("ospf_external_lsa_flush(): stop");
2196 void ospf_external_lsa_refresh_default(struct ospf
*ospf
)
2198 struct prefix_ipv4 p
;
2199 struct external_info
*ei
;
2200 struct ospf_lsa
*lsa
;
2204 p
.prefix
.s_addr
= 0;
2206 ei
= ospf_default_external_info(ospf
);
2207 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
2211 if (IS_DEBUG_OSPF_EVENT
)
2213 "LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2215 ospf_external_lsa_refresh(ospf
, lsa
, ei
,
2218 if (IS_DEBUG_OSPF_EVENT
)
2220 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2221 ospf_external_lsa_originate(ospf
, ei
);
2225 if (IS_DEBUG_OSPF_EVENT
)
2227 "LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2228 ospf_refresher_unregister_lsa(ospf
, lsa
);
2229 ospf_lsa_flush_as(ospf
, lsa
);
2234 void ospf_external_lsa_refresh_type(struct ospf
*ospf
, u_char type
,
2235 u_short instance
, int force
)
2237 struct route_node
*rn
;
2238 struct external_info
*ei
;
2239 struct ospf_external
*ext
;
2241 if (type
!= DEFAULT_ROUTE
)
2242 if ((ext
= ospf_external_lookup(type
, instance
))
2243 && EXTERNAL_INFO(ext
))
2244 /* Refresh each redistributed AS-external-LSAs. */
2245 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
2246 rn
= route_next(rn
))
2247 if ((ei
= rn
->info
))
2248 if (!is_prefix_default(&ei
->p
)) {
2249 struct ospf_lsa
*lsa
;
2251 if ((lsa
= ospf_external_info_find_lsa(
2253 ospf_external_lsa_refresh(
2257 ospf_external_lsa_originate(
2262 /* Refresh AS-external-LSA. */
2263 struct ospf_lsa
*ospf_external_lsa_refresh(struct ospf
*ospf
,
2264 struct ospf_lsa
*lsa
,
2265 struct external_info
*ei
, int force
)
2267 struct ospf_lsa
*new;
2270 /* Check the AS-external-LSA should be originated. */
2271 if (!ospf_redistribute_check(ospf
, ei
, &changed
)) {
2272 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2274 "LSA[Type%d:%s]: Could not be refreshed, "
2275 "redist check fail",
2276 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2277 ospf_external_lsa_flush(ospf
, ei
->type
, &ei
->p
,
2278 ei
->ifindex
/*, ei->nexthop */);
2282 if (!changed
&& !force
) {
2283 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2285 "LSA[Type%d:%s]: Not refreshed, not changed/forced",
2286 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2290 /* Delete LSA from neighbor retransmit-list. */
2291 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2293 /* Unregister AS-external-LSA from refresh-list. */
2294 ospf_refresher_unregister_lsa(ospf
, lsa
);
2296 new = ospf_external_lsa_new(ospf
, ei
, &lsa
->data
->id
);
2299 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2300 zlog_debug("LSA[Type%d:%s]: Could not be refreshed",
2301 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2305 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2307 ospf_lsa_install(ospf
, NULL
, new); /* As type-5. */
2309 /* Flood LSA through AS. */
2310 ospf_flood_through_as(ospf
, NULL
, new);
2312 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2313 if (ospf
->anyNSSA
&& !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2314 ospf_install_flood_nssa(ospf
, new,
2315 ei
); /* Install/Flood per new rules */
2317 /* Register self-originated LSA to refresh queue.
2318 * Translated LSAs should not be registered, but refreshed upon
2319 * refresh of the Type-7
2321 if (!CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2322 ospf_refresher_register_lsa(ospf
, new);
2324 /* Debug logging. */
2325 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2326 zlog_debug("LSA[Type%d:%s]: AS-external-LSA refresh",
2327 new->data
->type
, inet_ntoa(new->data
->id
));
2328 ospf_lsa_header_dump(new->data
);
2335 /* LSA installation functions. */
2337 /* Install router-LSA to an area. */
2338 static struct ospf_lsa
*
2339 ospf_router_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2341 struct ospf_area
*area
= new->area
;
2343 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2344 The entire routing table must be recalculated, starting with
2345 the shortest path calculations for each area (not just the
2346 area whose link-state database has changed).
2349 if (IS_LSA_SELF(new)) {
2351 /* Only install LSA if it is originated/refreshed by us.
2352 * If LSA was received by flooding, the RECEIVED flag is set so
2354 * not link the LSA */
2355 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2356 return new; /* ignore stale LSA */
2358 /* Set self-originated router-LSA. */
2359 ospf_lsa_unlock(&area
->router_lsa_self
);
2360 area
->router_lsa_self
= ospf_lsa_lock(new);
2362 ospf_refresher_register_lsa(ospf
, new);
2365 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2369 #define OSPF_INTERFACE_TIMER_ON(T, F, V) \
2371 (T) = thread_add_timer(master, (F), oi, (V))
2373 /* Install network-LSA to an area. */
2374 static struct ospf_lsa
*ospf_network_lsa_install(struct ospf
*ospf
,
2375 struct ospf_interface
*oi
,
2376 struct ospf_lsa
*new,
2380 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2381 The entire routing table must be recalculated, starting with
2382 the shortest path calculations for each area (not just the
2383 area whose link-state database has changed).
2385 if (IS_LSA_SELF(new)) {
2386 /* We supposed that when LSA is originated by us, we pass the
2388 for which it was originated. If LSA was received by flooding,
2389 the RECEIVED flag is set, so we do not link the LSA to the
2391 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2392 return new; /* ignore stale LSA */
2394 ospf_lsa_unlock(&oi
->network_lsa_self
);
2395 oi
->network_lsa_self
= ospf_lsa_lock(new);
2396 ospf_refresher_register_lsa(ospf
, new);
2399 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2404 /* Install summary-LSA to an area. */
2405 static struct ospf_lsa
*
2406 ospf_summary_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2408 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2409 /* RFC 2328 Section 13.2 Summary-LSAs
2410 The best route to the destination described by the summary-
2411 LSA must be recalculated (see Section 16.5). If this
2412 destination is an AS boundary router, it may also be
2413 necessary to re-examine all the AS-external-LSAs.
2417 /* This doesn't exist yet... */
2418 ospf_summary_incremental_update(new); */
2420 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2424 if (IS_LSA_SELF(new))
2425 ospf_refresher_register_lsa(ospf
, new);
2430 /* Install ASBR-summary-LSA to an area. */
2431 static struct ospf_lsa
*ospf_summary_asbr_lsa_install(struct ospf
*ospf
,
2432 struct ospf_lsa
*new,
2435 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2436 /* RFC 2328 Section 13.2 Summary-LSAs
2437 The best route to the destination described by the summary-
2438 LSA must be recalculated (see Section 16.5). If this
2439 destination is an AS boundary router, it may also be
2440 necessary to re-examine all the AS-external-LSAs.
2443 /* These don't exist yet... */
2444 ospf_summary_incremental_update(new);
2445 /* Isn't this done by the above call?
2446 - RFC 2328 Section 16.5 implies it should be */
2447 /* ospf_ase_calculate_schedule(); */
2449 ospf_spf_calculate_schedule(ospf
,
2450 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2454 /* register LSA to refresh-list. */
2455 if (IS_LSA_SELF(new))
2456 ospf_refresher_register_lsa(ospf
, new);
2461 /* Install AS-external-LSA. */
2462 static struct ospf_lsa
*ospf_external_lsa_install(struct ospf
*ospf
,
2463 struct ospf_lsa
*new,
2466 ospf_ase_register_external_lsa(new, ospf
);
2467 /* If LSA is not self-originated, calculate an external route. */
2469 /* RFC 2328 Section 13.2 AS-external-LSAs
2470 The best route to the destination described by the AS-
2471 external-LSA must be recalculated (see Section 16.6).
2474 if (!IS_LSA_SELF(new))
2475 ospf_ase_incremental_update(ospf
, new);
2478 if (new->data
->type
== OSPF_AS_NSSA_LSA
) {
2479 /* There is no point to register selforiginate Type-7 LSA for
2480 * refreshing. We rely on refreshing Type-5 LSA's
2482 if (IS_LSA_SELF(new))
2485 /* Try refresh type-5 translated LSA for this LSA, if
2487 * New translations will be taken care of by the
2490 ospf_translated_nssa_refresh(ospf
, new, NULL
);
2494 /* Register self-originated LSA to refresh queue.
2495 * Leave Translated LSAs alone if NSSA is enabled
2497 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2498 ospf_refresher_register_lsa(ospf
, new);
2503 void ospf_discard_from_db(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
2504 struct ospf_lsa
*lsa
)
2506 struct ospf_lsa
*old
;
2509 zlog_warn("%s: Called with NULL lsdb!", __func__
);
2511 zlog_warn("%s: and NULL LSA!", __func__
);
2513 zlog_warn("LSA[Type%d:%s]: not associated with LSDB!",
2514 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
2518 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2523 if (old
->refresh_list
>= 0)
2524 ospf_refresher_unregister_lsa(ospf
, old
);
2526 switch (old
->data
->type
) {
2527 case OSPF_AS_EXTERNAL_LSA
:
2528 ospf_ase_unregister_external_lsa(old
, ospf
);
2529 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2531 case OSPF_OPAQUE_AS_LSA
:
2532 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2534 case OSPF_AS_NSSA_LSA
:
2535 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2536 ospf_ase_unregister_external_lsa(old
, ospf
);
2539 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2543 ospf_lsa_maxage_delete(ospf
, old
);
2544 ospf_lsa_discard(old
);
2547 struct ospf_lsa
*ospf_lsa_install(struct ospf
*ospf
, struct ospf_interface
*oi
,
2548 struct ospf_lsa
*lsa
)
2550 struct ospf_lsa
*new = NULL
;
2551 struct ospf_lsa
*old
= NULL
;
2552 struct ospf_lsdb
*lsdb
= NULL
;
2556 switch (lsa
->data
->type
) {
2558 case OSPF_AS_NSSA_LSA
:
2560 lsdb
= lsa
->area
->lsdb
;
2564 case OSPF_AS_EXTERNAL_LSA
:
2565 case OSPF_OPAQUE_AS_LSA
:
2569 lsdb
= lsa
->area
->lsdb
;
2575 /* RFC 2328 13.2. Installing LSAs in the database
2577 Installing a new LSA in the database, either as the result of
2578 flooding or a newly self-originated LSA, may cause the OSPF
2579 routing table structure to be recalculated. The contents of the
2580 new LSA should be compared to the old instance, if present. If
2581 there is no difference, there is no need to recalculate the
2582 routing table. When comparing an LSA to its previous instance,
2583 the following are all considered to be differences in contents:
2585 o The LSA's Options field has changed.
2587 o One of the LSA instances has LS age set to MaxAge, and
2590 o The length field in the LSA header has changed.
2592 o The body of the LSA (i.e., anything outside the 20-byte
2593 LSA header) has changed. Note that this excludes changes
2594 in LS Sequence Number and LS Checksum.
2597 /* Look up old LSA and determine if any SPF calculation or incremental
2599 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2601 /* Do comparision and record if recalc needed. */
2603 if (old
== NULL
|| ospf_lsa_different(old
, lsa
))
2607 Sequence number check (Section 14.1 of rfc 2328)
2608 "Premature aging is used when it is time for a self-originated
2609 LSA's sequence number field to wrap. At this point, the current
2610 LSA instance (having LS sequence number MaxSequenceNumber) must
2611 be prematurely aged and flushed from the routing domain before a
2612 new instance with sequence number equal to InitialSequenceNumber
2613 can be originated. "
2616 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
) {
2617 if (ospf_lsa_is_self_originated(ospf
, lsa
)) {
2618 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2620 if (!IS_LSA_MAXAGE(lsa
))
2621 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2622 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
2624 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
)) {
2626 "ospf_lsa_install() Premature Aging "
2627 "lsa 0x%p, seqnum 0x%x",
2629 ntohl(lsa
->data
->ls_seqnum
));
2630 ospf_lsa_header_dump(lsa
->data
);
2633 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2635 "ospf_lsa_install() got an lsa with seq 0x80000000 "
2636 "that was not self originated. Ignoring\n");
2637 ospf_lsa_header_dump(lsa
->data
);
2643 /* discard old LSA from LSDB */
2645 ospf_discard_from_db(ospf
, lsdb
, lsa
);
2647 /* Calculate Checksum if self-originated?. */
2648 if (IS_LSA_SELF(lsa
))
2649 ospf_lsa_checksum(lsa
->data
);
2651 /* Insert LSA to LSDB. */
2652 ospf_lsdb_add(lsdb
, lsa
);
2655 /* Do LSA specific installation process. */
2656 switch (lsa
->data
->type
) {
2657 case OSPF_ROUTER_LSA
:
2658 new = ospf_router_lsa_install(ospf
, lsa
, rt_recalc
);
2660 case OSPF_NETWORK_LSA
:
2662 new = ospf_network_lsa_install(ospf
, oi
, lsa
, rt_recalc
);
2664 case OSPF_SUMMARY_LSA
:
2665 new = ospf_summary_lsa_install(ospf
, lsa
, rt_recalc
);
2667 case OSPF_ASBR_SUMMARY_LSA
:
2668 new = ospf_summary_asbr_lsa_install(ospf
, lsa
, rt_recalc
);
2670 case OSPF_AS_EXTERNAL_LSA
:
2671 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2673 case OSPF_OPAQUE_LINK_LSA
:
2674 if (IS_LSA_SELF(lsa
))
2675 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for
2678 /* Incoming "oi" for this LSA has set at LSUpd
2682 case OSPF_OPAQUE_AREA_LSA
:
2683 case OSPF_OPAQUE_AS_LSA
:
2684 new = ospf_opaque_lsa_install(lsa
, rt_recalc
);
2686 case OSPF_AS_NSSA_LSA
:
2687 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2688 default: /* type-6,8,9....nothing special */
2693 return new; /* Installation failed, cannot proceed further --
2697 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
)) {
2698 char area_str
[INET_ADDRSTRLEN
];
2700 switch (lsa
->data
->type
) {
2701 case OSPF_AS_EXTERNAL_LSA
:
2702 case OSPF_OPAQUE_AS_LSA
:
2703 case OSPF_AS_NSSA_LSA
:
2704 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2705 lookup_msg(ospf_lsa_type_msg
,
2706 new->data
->type
, NULL
));
2709 strcpy(area_str
, inet_ntoa(new->area
->area_id
));
2710 zlog_debug("LSA[%s]: Install %s to Area %s",
2712 lookup_msg(ospf_lsa_type_msg
,
2713 new->data
->type
, NULL
),
2720 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2721 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2723 if (IS_LSA_MAXAGE(new)) {
2724 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
))
2725 zlog_debug("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2726 new->data
->type
, inet_ntoa(new->data
->id
),
2728 ospf_lsa_maxage(ospf
, lsa
);
2735 int ospf_check_nbr_status(struct ospf
*ospf
)
2737 struct listnode
*node
, *nnode
;
2738 struct ospf_interface
*oi
;
2740 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
2741 struct route_node
*rn
;
2742 struct ospf_neighbor
*nbr
;
2744 if (ospf_if_is_enable(oi
))
2745 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
2746 if ((nbr
= rn
->info
) != NULL
)
2747 if (nbr
->state
== NSM_Exchange
2748 || nbr
->state
== NSM_Loading
) {
2749 route_unlock_node(rn
);
2758 static int ospf_maxage_lsa_remover(struct thread
*thread
)
2760 struct ospf
*ospf
= THREAD_ARG(thread
);
2761 struct ospf_lsa
*lsa
;
2762 struct route_node
*rn
;
2765 ospf
->t_maxage
= NULL
;
2767 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2768 zlog_debug("LSA[MaxAge]: remover Start");
2770 reschedule
= !ospf_check_nbr_status(ospf
);
2773 for (rn
= route_top(ospf
->maxage_lsa
); rn
;
2774 rn
= route_next(rn
)) {
2775 if ((lsa
= rn
->info
) == NULL
) {
2779 /* There is at least one neighbor from which we still
2781 * for that LSA, so we are not allowed to remove it from
2783 * as per RFC 2328 section 14 para 4 a) */
2784 if (lsa
->retransmit_counter
> 0) {
2789 /* TODO: maybe convert this function to a work-queue */
2790 if (thread_should_yield(thread
)) {
2791 OSPF_TIMER_ON(ospf
->t_maxage
,
2792 ospf_maxage_lsa_remover
, 0);
2794 rn
); /* route_top/route_next */
2798 /* Remove LSA from the LSDB */
2799 if (IS_LSA_SELF(lsa
))
2800 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2802 "LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2804 inet_ntoa(lsa
->data
->id
),
2807 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2809 "LSA[Type%d:%s]: MaxAge LSA removed from list",
2811 inet_ntoa(lsa
->data
->id
));
2813 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
)) {
2814 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2816 "originating new lsa for lsa 0x%p\n",
2818 ospf_lsa_refresh(ospf
, lsa
);
2821 /* Remove from lsdb. */
2823 ospf_discard_from_db(ospf
, lsa
->lsdb
, lsa
);
2824 ospf_lsdb_delete(lsa
->lsdb
, lsa
);
2827 "%s: LSA[Type%d:%s]: No associated LSDB!",
2828 __func__
, lsa
->data
->type
,
2829 inet_ntoa(lsa
->data
->id
));
2832 /* A MaxAge LSA must be removed immediately from the router's link
2833 state database as soon as both a) it is no longer contained on any
2834 neighbor Link state retransmission lists and b) none of the
2836 neighbors are in states Exchange or Loading. */
2838 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2839 ospf
->maxage_delay
);
2844 void ospf_lsa_maxage_delete(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2846 struct route_node
*rn
;
2847 struct prefix_ptr lsa_prefix
;
2849 lsa_prefix
.family
= 0;
2850 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.prefix
) * CHAR_BIT
;
2851 lsa_prefix
.prefix
= (uintptr_t)lsa
;
2853 if ((rn
= route_node_lookup(ospf
->maxage_lsa
,
2854 (struct prefix
*)&lsa_prefix
))) {
2855 if (rn
->info
== lsa
) {
2856 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2857 ospf_lsa_unlock(&lsa
); /* maxage_lsa */
2860 rn
); /* unlock node because lsa is deleted */
2862 route_unlock_node(rn
); /* route_node_lookup */
2866 /* Add LSA onto the MaxAge list, and schedule for removal.
2867 * This does *not* lead to the LSA being flooded, that must be taken
2868 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2871 void ospf_lsa_maxage(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2873 struct prefix_ptr lsa_prefix
;
2874 struct route_node
*rn
;
2876 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2877 and schedule the MaxAge LSA remover. */
2878 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
)) {
2879 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2881 "LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2882 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
2887 lsa_prefix
.family
= 0;
2888 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.prefix
) * CHAR_BIT
;
2889 lsa_prefix
.prefix
= (uintptr_t)lsa
;
2891 if ((rn
= route_node_get(ospf
->maxage_lsa
,
2892 (struct prefix
*)&lsa_prefix
))
2894 if (rn
->info
!= NULL
) {
2895 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2897 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
2898 dump_lsa_key(lsa
), rn
->info
,
2899 (void *)lsa
, lsa_prefix
.prefixlen
);
2900 route_unlock_node(rn
);
2902 rn
->info
= ospf_lsa_lock(lsa
);
2903 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2906 zlog_err("Unable to allocate memory for maxage lsa\n");
2910 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2911 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
2914 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2915 ospf
->maxage_delay
);
2918 static int ospf_lsa_maxage_walker_remover(struct ospf
*ospf
,
2919 struct ospf_lsa
*lsa
)
2921 /* Stay away from any Local Translated Type-7 LSAs */
2922 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
2925 if (IS_LSA_MAXAGE(lsa
))
2926 /* Self-originated LSAs should NOT time-out instead,
2927 they're flushed and submitted to the max_age list explicitly.
2929 if (!ospf_lsa_is_self_originated(ospf
, lsa
)) {
2930 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2931 zlog_debug("LSA[%s]: is MaxAge",
2934 switch (lsa
->data
->type
) {
2935 case OSPF_OPAQUE_LINK_LSA
:
2936 case OSPF_OPAQUE_AREA_LSA
:
2937 case OSPF_OPAQUE_AS_LSA
:
2939 * As a general rule, whenever network topology
2941 * (due to an LSA removal in this case), routing
2943 * should be triggered. However, this is not
2945 * LSAs. Even if an opaque LSA instance is going
2947 * from the routing domain, it does not mean a
2949 * topology, and thus, routing recalculation is
2953 case OSPF_AS_EXTERNAL_LSA
:
2954 case OSPF_AS_NSSA_LSA
:
2955 ospf_ase_incremental_update(ospf
, lsa
);
2958 ospf_spf_calculate_schedule(ospf
,
2962 ospf_lsa_maxage(ospf
, lsa
);
2965 if (IS_LSA_MAXAGE(lsa
) && !ospf_lsa_is_self_originated(ospf
, lsa
))
2966 if (LS_AGE(lsa
) > OSPF_LSA_MAXAGE
+ 30)
2967 printf("Eek! Shouldn't happen!\n");
2972 /* Periodical check of MaxAge LSA. */
2973 int ospf_lsa_maxage_walker(struct thread
*thread
)
2975 struct ospf
*ospf
= THREAD_ARG(thread
);
2976 struct route_node
*rn
;
2977 struct ospf_lsa
*lsa
;
2978 struct ospf_area
*area
;
2979 struct listnode
*node
, *nnode
;
2981 ospf
->t_maxage_walker
= NULL
;
2983 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2984 LSDB_LOOP(ROUTER_LSDB(area
), rn
, lsa
)
2985 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
2986 LSDB_LOOP(NETWORK_LSDB(area
), rn
, lsa
)
2987 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
2988 LSDB_LOOP(SUMMARY_LSDB(area
), rn
, lsa
)
2989 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
2990 LSDB_LOOP(ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
2991 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
2992 LSDB_LOOP(OPAQUE_AREA_LSDB(area
), rn
, lsa
)
2993 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
2994 LSDB_LOOP(OPAQUE_LINK_LSDB(area
), rn
, lsa
)
2995 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
2996 LSDB_LOOP(NSSA_LSDB(area
), rn
, lsa
)
2997 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3000 /* for AS-external-LSAs. */
3002 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
3003 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3004 LSDB_LOOP(OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3005 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3008 OSPF_TIMER_ON(ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3009 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3013 struct ospf_lsa
*ospf_lsa_lookup_by_prefix(struct ospf_lsdb
*lsdb
, u_char type
,
3014 struct prefix_ipv4
*p
,
3015 struct in_addr router_id
)
3017 struct ospf_lsa
*lsa
;
3018 struct in_addr mask
, id
;
3019 struct lsa_header_mask
{
3020 struct lsa_header header
;
3021 struct in_addr mask
;
3024 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, p
->prefix
, router_id
);
3028 masklen2ip(p
->prefixlen
, &mask
);
3030 hmask
= (struct lsa_header_mask
*)lsa
->data
;
3032 if (mask
.s_addr
!= hmask
->mask
.s_addr
) {
3033 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3034 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, router_id
);
3042 struct ospf_lsa
*ospf_lsa_lookup(struct ospf_area
*area
, u_int32_t type
,
3043 struct in_addr id
, struct in_addr adv_router
)
3045 struct ospf
*ospf
= ospf_lookup();
3049 case OSPF_ROUTER_LSA
:
3050 case OSPF_NETWORK_LSA
:
3051 case OSPF_SUMMARY_LSA
:
3052 case OSPF_ASBR_SUMMARY_LSA
:
3053 case OSPF_AS_NSSA_LSA
:
3054 case OSPF_OPAQUE_LINK_LSA
:
3055 case OSPF_OPAQUE_AREA_LSA
:
3056 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, adv_router
);
3057 case OSPF_AS_EXTERNAL_LSA
:
3058 case OSPF_OPAQUE_AS_LSA
:
3059 return ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
, adv_router
);
3067 struct ospf_lsa
*ospf_lsa_lookup_by_id(struct ospf_area
*area
, u_int32_t type
,
3070 struct ospf_lsa
*lsa
;
3071 struct route_node
*rn
;
3074 case OSPF_ROUTER_LSA
:
3075 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3076 case OSPF_NETWORK_LSA
:
3077 for (rn
= route_top(NETWORK_LSDB(area
)); rn
;
3078 rn
= route_next(rn
))
3079 if ((lsa
= rn
->info
))
3080 if (IPV4_ADDR_SAME(&lsa
->data
->id
, &id
)) {
3081 route_unlock_node(rn
);
3085 case OSPF_SUMMARY_LSA
:
3086 case OSPF_ASBR_SUMMARY_LSA
:
3087 /* Currently not used. */
3089 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3090 case OSPF_AS_EXTERNAL_LSA
:
3091 case OSPF_AS_NSSA_LSA
:
3092 case OSPF_OPAQUE_LINK_LSA
:
3093 case OSPF_OPAQUE_AREA_LSA
:
3094 case OSPF_OPAQUE_AS_LSA
:
3095 /* Currently not used. */
3104 struct ospf_lsa
*ospf_lsa_lookup_by_header(struct ospf_area
*area
,
3105 struct lsa_header
*lsah
)
3107 struct ospf_lsa
*match
;
3110 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3111 * is redefined to have two subfields; opaque-type and opaque-id.
3112 * However, it is harmless to treat the two sub fields together, as if
3113 * they two were forming a unique LSA-ID.
3116 match
= ospf_lsa_lookup(area
, lsah
->type
, lsah
->id
, lsah
->adv_router
);
3119 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3120 zlog_debug("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3121 lsah
->type
, inet_ntoa(lsah
->id
));
3126 /* return +n, l1 is more recent.
3127 return -n, l2 is more recent.
3128 return 0, l1 and l2 is identical. */
3129 int ospf_lsa_more_recent(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3134 if (l1
== NULL
&& l2
== NULL
)
3141 /* compare LS sequence number. */
3142 x
= (int)ntohl(l1
->data
->ls_seqnum
);
3143 y
= (int)ntohl(l2
->data
->ls_seqnum
);
3149 /* compare LS checksum. */
3150 r
= ntohs(l1
->data
->checksum
) - ntohs(l2
->data
->checksum
);
3154 /* compare LS age. */
3155 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3157 else if (!IS_LSA_MAXAGE(l1
) && IS_LSA_MAXAGE(l2
))
3160 /* compare LS age with MaxAgeDiff. */
3161 if (LS_AGE(l1
) - LS_AGE(l2
) > OSPF_LSA_MAXAGE_DIFF
)
3163 else if (LS_AGE(l2
) - LS_AGE(l1
) > OSPF_LSA_MAXAGE_DIFF
)
3166 /* LSAs are identical. */
3170 /* If two LSAs are different, return 1, otherwise return 0. */
3171 int ospf_lsa_different(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3179 if (l1
->data
->options
!= l2
->data
->options
)
3182 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3185 if (IS_LSA_MAXAGE(l2
) && !IS_LSA_MAXAGE(l1
))
3188 if (l1
->data
->length
!= l2
->data
->length
)
3191 if (l1
->data
->length
== 0)
3194 if (CHECK_FLAG((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3195 return 1; /* May be a stale LSA in the LSBD */
3197 assert(ntohs(l1
->data
->length
) > OSPF_LSA_HEADER_SIZE
);
3199 p1
= (char *)l1
->data
;
3200 p2
= (char *)l2
->data
;
3202 if (memcmp(p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3203 ntohs(l1
->data
->length
) - OSPF_LSA_HEADER_SIZE
)
3210 #ifdef ORIGINAL_CODING
3211 void ospf_lsa_flush_self_originated(struct ospf_neighbor
*nbr
,
3212 struct ospf_lsa
*self
, struct ospf_lsa
*new)
3216 /* Adjust LS Sequence Number. */
3217 seqnum
= ntohl(new->data
->ls_seqnum
) + 1;
3218 self
->data
->ls_seqnum
= htonl(seqnum
);
3220 /* Recalculate LSA checksum. */
3221 ospf_lsa_checksum(self
->data
);
3223 /* Reflooding LSA. */
3224 /* RFC2328 Section 13.3
3225 On non-broadcast networks, separate Link State Update
3226 packets must be sent, as unicasts, to each adjacent neighbor
3227 (i.e., those in state Exchange or greater). The destination
3228 IP addresses for these packets are the neighbors' IP
3230 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
) {
3231 struct route_node
*rn
;
3232 struct ospf_neighbor
*onbr
;
3234 for (rn
= route_top(nbr
->oi
->nbrs
); rn
; rn
= route_next(rn
))
3235 if ((onbr
= rn
->info
) != NULL
)
3236 if (onbr
!= nbr
->oi
->nbr_self
3237 && onbr
->status
>= NSM_Exchange
)
3238 ospf_ls_upd_send_lsa(
3240 OSPF_SEND_PACKET_DIRECT
);
3242 ospf_ls_upd_send_lsa(nbr
, self
, OSPF_SEND_PACKET_INDIRECT
);
3244 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3245 zlog_debug("LSA[Type%d:%s]: Flush self-originated LSA",
3246 self
->data
->type
, inet_ntoa(self
->data
->id
));
3248 #else /* ORIGINAL_CODING */
3249 int ospf_lsa_flush_schedule(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3251 if (lsa
== NULL
|| !IS_LSA_SELF(lsa
))
3254 if (IS_DEBUG_OSPF_EVENT
)
3256 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3257 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
));
3259 /* Force given lsa's age to MaxAge. */
3260 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
3262 switch (lsa
->data
->type
) {
3263 /* Opaque wants to be notified of flushes */
3264 case OSPF_OPAQUE_LINK_LSA
:
3265 case OSPF_OPAQUE_AREA_LSA
:
3266 case OSPF_OPAQUE_AS_LSA
:
3267 ospf_opaque_lsa_refresh(lsa
);
3270 ospf_refresher_unregister_lsa(ospf
, lsa
);
3271 ospf_lsa_flush(ospf
, lsa
);
3278 void ospf_flush_self_originated_lsas_now(struct ospf
*ospf
)
3280 struct listnode
*node
, *nnode
;
3281 struct listnode
*node2
, *nnode2
;
3282 struct ospf_area
*area
;
3283 struct ospf_interface
*oi
;
3284 struct ospf_lsa
*lsa
;
3285 struct route_node
*rn
;
3286 int need_to_flush_ase
= 0;
3288 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3289 if ((lsa
= area
->router_lsa_self
) != NULL
) {
3290 if (IS_DEBUG_OSPF_EVENT
)
3292 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3294 inet_ntoa(lsa
->data
->id
));
3296 ospf_refresher_unregister_lsa(ospf
, lsa
);
3297 ospf_lsa_flush_area(lsa
, area
);
3298 ospf_lsa_unlock(&area
->router_lsa_self
);
3299 area
->router_lsa_self
= NULL
;
3302 for (ALL_LIST_ELEMENTS(area
->oiflist
, node2
, nnode2
, oi
)) {
3303 if ((lsa
= oi
->network_lsa_self
) != NULL
3304 && oi
->state
== ISM_DR
&& oi
->full_nbrs
> 0) {
3305 if (IS_DEBUG_OSPF_EVENT
)
3307 "LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3309 inet_ntoa(lsa
->data
->id
));
3311 ospf_refresher_unregister_lsa(
3312 ospf
, oi
->network_lsa_self
);
3313 ospf_lsa_flush_area(oi
->network_lsa_self
, area
);
3314 ospf_lsa_unlock(&oi
->network_lsa_self
);
3315 oi
->network_lsa_self
= NULL
;
3318 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3319 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3320 need_to_flush_ase
= 1;
3323 LSDB_LOOP(SUMMARY_LSDB(area
), rn
, lsa
)
3324 ospf_lsa_flush_schedule(ospf
, lsa
);
3325 LSDB_LOOP(ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3326 ospf_lsa_flush_schedule(ospf
, lsa
);
3327 LSDB_LOOP(OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3328 ospf_lsa_flush_schedule(ospf
, lsa
);
3329 LSDB_LOOP(OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3330 ospf_lsa_flush_schedule(ospf
, lsa
);
3333 if (need_to_flush_ase
) {
3334 LSDB_LOOP(EXTERNAL_LSDB(ospf
), rn
, lsa
)
3335 ospf_lsa_flush_schedule(ospf
, lsa
);
3336 LSDB_LOOP(OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3337 ospf_lsa_flush_schedule(ospf
, lsa
);
3341 * Make sure that the MaxAge LSA remover is executed immediately,
3342 * without conflicting to other threads.
3344 if (ospf
->t_maxage
!= NULL
) {
3345 OSPF_TIMER_OFF(ospf
->t_maxage
);
3346 thread_execute(master
, ospf_maxage_lsa_remover
, ospf
, 0);
3351 #endif /* ORIGINAL_CODING */
3353 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3354 /* An interface-independent version of ospf_lsa_is_self_originated */
3355 int ospf_lsa_is_self_originated(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3357 struct listnode
*node
;
3358 struct ospf_interface
*oi
;
3360 /* This LSA is already checked. */
3361 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3362 return IS_LSA_SELF(lsa
);
3364 /* Make sure LSA is self-checked. */
3365 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3367 /* AdvRouter and Router ID is the same. */
3368 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
, &ospf
->router_id
))
3369 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3371 /* LSA is router-LSA. */
3372 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
3373 && IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
))
3374 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3376 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3377 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3378 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
3379 /* Ignore virtual link. */
3380 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3381 if (oi
->address
->family
== AF_INET
)
3384 &oi
->address
->u
.prefix4
)) {
3385 /* to make it easier later */
3386 SET_FLAG(lsa
->flags
,
3388 return IS_LSA_SELF(lsa
);
3392 return IS_LSA_SELF(lsa
);
3395 /* Get unique Link State ID. */
3396 struct in_addr
ospf_lsa_unique_id(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
3397 u_char type
, struct prefix_ipv4
*p
)
3399 struct ospf_lsa
*lsa
;
3400 struct in_addr mask
, id
;
3404 /* Check existence of LSA instance. */
3405 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, ospf
->router_id
);
3407 struct as_external_lsa
*al
=
3408 (struct as_external_lsa
*)lsa
->data
;
3409 if (ip_masklen(al
->mask
) == p
->prefixlen
) {
3410 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3412 "ospf_lsa_unique_id(): "
3413 "Can't get Link State ID for %s/%d",
3414 inet_ntoa(p
->prefix
), p
->prefixlen
);
3415 /* id.s_addr = 0; */
3416 id
.s_addr
= 0xffffffff;
3419 /* Masklen differs, then apply wildcard mask to Link State ID.
3422 masklen2ip(p
->prefixlen
, &mask
);
3424 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3425 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
,
3428 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3430 "ospf_lsa_unique_id(): "
3431 "Can't get Link State ID for %s/%d",
3432 inet_ntoa(p
->prefix
),
3434 /* id.s_addr = 0; */
3435 id
.s_addr
= 0xffffffff;
3445 #define LSA_ACTION_FLOOD_AREA 1
3446 #define LSA_ACTION_FLUSH_AREA 2
3450 struct ospf_area
*area
;
3451 struct ospf_lsa
*lsa
;
3454 static int ospf_lsa_action(struct thread
*t
)
3456 struct lsa_action
*data
;
3458 data
= THREAD_ARG(t
);
3460 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3461 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3464 switch (data
->action
) {
3465 case LSA_ACTION_FLOOD_AREA
:
3466 ospf_flood_through_area(data
->area
, NULL
, data
->lsa
);
3468 case LSA_ACTION_FLUSH_AREA
:
3469 ospf_lsa_flush_area(data
->lsa
, data
->area
);
3473 ospf_lsa_unlock(&data
->lsa
); /* Message */
3474 XFREE(MTYPE_OSPF_MESSAGE
, data
);
3478 void ospf_schedule_lsa_flood_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3480 struct lsa_action
*data
;
3482 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3483 data
->action
= LSA_ACTION_FLOOD_AREA
;
3485 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flood area */
3487 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3490 void ospf_schedule_lsa_flush_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3492 struct lsa_action
*data
;
3494 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3495 data
->action
= LSA_ACTION_FLUSH_AREA
;
3497 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flush area */
3499 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3503 /* LSA Refreshment functions. */
3504 struct ospf_lsa
*ospf_lsa_refresh(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3506 struct external_info
*ei
;
3507 struct ospf_lsa
*new = NULL
;
3508 assert(CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF
));
3509 assert(IS_LSA_SELF(lsa
));
3510 assert(lsa
->lock
> 0);
3512 switch (lsa
->data
->type
) {
3513 /* Router and Network LSAs are processed differently. */
3514 case OSPF_ROUTER_LSA
:
3515 new = ospf_router_lsa_refresh(lsa
);
3517 case OSPF_NETWORK_LSA
:
3518 new = ospf_network_lsa_refresh(lsa
);
3520 case OSPF_SUMMARY_LSA
:
3521 new = ospf_summary_lsa_refresh(ospf
, lsa
);
3523 case OSPF_ASBR_SUMMARY_LSA
:
3524 new = ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
3526 case OSPF_AS_EXTERNAL_LSA
:
3527 /* Translated from NSSA Type-5s are refreshed when
3528 * from refresh of Type-7 - do not refresh these directly.
3530 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3532 ei
= ospf_external_info_check(lsa
);
3534 new = ospf_external_lsa_refresh(ospf
, lsa
, ei
,
3537 ospf_lsa_flush_as(ospf
, lsa
);
3539 case OSPF_OPAQUE_LINK_LSA
:
3540 case OSPF_OPAQUE_AREA_LSA
:
3541 case OSPF_OPAQUE_AS_LSA
:
3542 new = ospf_opaque_lsa_refresh(lsa
);
3550 void ospf_refresher_register_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3552 u_int16_t index
, current_index
;
3554 assert(lsa
->lock
> 0);
3555 assert(IS_LSA_SELF(lsa
));
3557 if (lsa
->refresh_list
< 0) {
3560 OSPF_LS_REFRESH_TIME
- (2 * OSPF_LS_REFRESH_JITTER
);
3561 int max_delay
= OSPF_LS_REFRESH_TIME
- OSPF_LS_REFRESH_JITTER
;
3563 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3565 * 1800s. Use jitter so that we send the LSA sometime between
3569 delay
= (random() % (max_delay
- min_delay
)) + min_delay
;
3571 current_index
= ospf
->lsa_refresh_queue
.index
3572 + (monotime(NULL
) - ospf
->lsa_refresher_started
)
3573 / OSPF_LSA_REFRESHER_GRANULARITY
;
3575 index
= (current_index
+ delay
/ OSPF_LSA_REFRESHER_GRANULARITY
)
3576 % (OSPF_LSA_REFRESHER_SLOTS
);
3578 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3580 "LSA[Refresh:Type%d:%s]: age %d, added to index %d",
3581 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
3582 LS_AGE(lsa
), index
);
3584 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3585 ospf
->lsa_refresh_queue
.qs
[index
] = list_new();
3587 listnode_add(ospf
->lsa_refresh_queue
.qs
[index
],
3588 ospf_lsa_lock(lsa
)); /* lsa_refresh_queue */
3589 lsa
->refresh_list
= index
;
3591 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3593 "LSA[Refresh:Type%d:%s]: ospf_refresher_register_lsa(): "
3594 "setting refresh_list on lsa %p (slod %d)",
3595 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
3596 (void *)lsa
, index
);
3600 void ospf_refresher_unregister_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3602 assert(lsa
->lock
> 0);
3603 assert(IS_LSA_SELF(lsa
));
3604 if (lsa
->refresh_list
>= 0) {
3605 struct list
*refresh_list
=
3606 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3607 listnode_delete(refresh_list
, lsa
);
3608 if (!listcount(refresh_list
)) {
3609 list_free(refresh_list
);
3610 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3612 ospf_lsa_unlock(&lsa
); /* lsa_refresh_queue */
3613 lsa
->refresh_list
= -1;
3617 int ospf_lsa_refresh_walker(struct thread
*t
)
3619 struct list
*refresh_list
;
3620 struct listnode
*node
, *nnode
;
3621 struct ospf
*ospf
= THREAD_ARG(t
);
3622 struct ospf_lsa
*lsa
;
3624 struct list
*lsa_to_refresh
= list_new();
3626 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3627 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3630 i
= ospf
->lsa_refresh_queue
.index
;
3632 /* Note: if clock has jumped backwards, then time change could be
3634 so we are careful to cast the expression to unsigned before taking
3636 ospf
->lsa_refresh_queue
.index
=
3637 ((unsigned long)(ospf
->lsa_refresh_queue
.index
3639 - ospf
->lsa_refresher_started
)
3640 / OSPF_LSA_REFRESHER_GRANULARITY
))
3641 % OSPF_LSA_REFRESHER_SLOTS
;
3643 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3645 "LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3646 ospf
->lsa_refresh_queue
.index
);
3648 for (; i
!= ospf
->lsa_refresh_queue
.index
;
3649 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
) {
3650 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3652 "LSA[Refresh]: ospf_lsa_refresh_walker(): "
3656 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3660 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3663 for (ALL_LIST_ELEMENTS(refresh_list
, node
, nnode
,
3665 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3667 "LSA[Refresh:Type%d:%s]: ospf_lsa_refresh_walker(): "
3668 "refresh lsa %p (slot %d)",
3670 inet_ntoa(lsa
->data
->id
),
3673 assert(lsa
->lock
> 0);
3674 list_delete_node(refresh_list
, node
);
3675 lsa
->refresh_list
= -1;
3676 listnode_add(lsa_to_refresh
, lsa
);
3678 list_free(refresh_list
);
3682 ospf
->t_lsa_refresher
= NULL
;
3683 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
3684 ospf
->lsa_refresh_interval
, &ospf
->t_lsa_refresher
);
3685 ospf
->lsa_refresher_started
= monotime(NULL
);
3687 for (ALL_LIST_ELEMENTS(lsa_to_refresh
, node
, nnode
, lsa
)) {
3688 ospf_lsa_refresh(ospf
, lsa
);
3689 assert(lsa
->lock
> 0);
3691 &lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3694 list_delete(lsa_to_refresh
);
3696 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
3697 zlog_debug("LSA[Refresh]: ospf_lsa_refresh_walker(): end");