2 * OSPF Link State Advertisement
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h" /* for inet_aton() */
38 #include "ospfd/ospfd.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_ism.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_lsa.h"
43 #include "ospfd/ospf_lsdb.h"
44 #include "ospfd/ospf_neighbor.h"
45 #include "ospfd/ospf_nsm.h"
46 #include "ospfd/ospf_flood.h"
47 #include "ospfd/ospf_packet.h"
48 #include "ospfd/ospf_spf.h"
49 #include "ospfd/ospf_dump.h"
50 #include "ospfd/ospf_route.h"
51 #include "ospfd/ospf_ase.h"
52 #include "ospfd/ospf_zebra.h"
53 #include "ospfd/ospf_abr.h"
54 #include "ospfd/ospf_errors.h"
55 #include "ospfd/ospf_te.h"
56 #include "ospfd/ospf_orr.h"
58 static struct ospf_lsa
*ospf_handle_summarylsa_lsId_chg(struct ospf
*ospf
,
59 struct prefix_ipv4
*p
,
62 struct in_addr old_id
);
63 static struct ospf_lsa
*
64 ospf_summary_lsa_prepare_and_flood(struct prefix_ipv4
*p
, uint32_t metric
,
65 struct ospf_area
*area
, struct in_addr id
);
66 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
67 struct ospf_lsa
*lsa
);
68 static struct ospf_lsa
*
69 ospf_asbr_summary_lsa_prepare_and_flood(struct prefix_ipv4
*p
, uint32_t metric
,
70 struct ospf_area
*area
,
72 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
73 struct ospf_lsa
*lsa
);
74 static struct ospf_lsa
*ospf_handle_exnl_lsa_lsId_chg(struct ospf
*ospf
,
75 struct external_info
*ei
,
77 static struct ospf_lsa
*
78 ospf_exnl_lsa_prepare_and_flood(struct ospf
*ospf
, struct external_info
*ei
,
81 uint32_t get_metric(uint8_t *metric
)
85 m
= (m
<< 8) + metric
[1];
86 m
= (m
<< 8) + metric
[2];
91 struct timeval
int2tv(int a
)
101 struct timeval
msec2tv(int a
)
105 ret
.tv_sec
= a
/ 1000;
106 ret
.tv_usec
= (a
% 1000) * 1000;
111 int ospf_lsa_refresh_delay(struct ospf_lsa
*lsa
)
113 struct timeval delta
;
116 if (monotime_since(&lsa
->tv_orig
, &delta
)
117 < OSPF_MIN_LS_INTERVAL
* 1000LL) {
118 struct timeval minv
= msec2tv(OSPF_MIN_LS_INTERVAL
);
119 timersub(&minv
, &delta
, &minv
);
121 /* TBD: remove padding to full sec, return timeval instead */
122 delay
= minv
.tv_sec
+ !!minv
.tv_usec
;
124 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
126 "LSA[Type%d:%pI4]: Refresh timer delay %d seconds",
127 lsa
->data
->type
, &lsa
->data
->id
,
137 int get_age(struct ospf_lsa
*lsa
)
141 monotime_since(&lsa
->tv_recv
, &rel
);
142 return ntohs(lsa
->data
->ls_age
) + rel
.tv_sec
;
146 /* Fletcher Checksum -- Refer to RFC1008. */
148 /* All the offsets are zero-based. The offsets in the RFC1008 are
150 uint16_t ospf_lsa_checksum(struct lsa_header
*lsa
)
152 uint8_t *buffer
= &lsa
->options
;
153 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
155 /* Skip the AGE field */
156 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
158 /* Checksum offset starts from "options" field, not the beginning of the
159 lsa_header struct. The offset is 14, rather than 16. */
160 int checksum_offset
= (uint8_t *)&lsa
->checksum
- buffer
;
162 return fletcher_checksum(buffer
, len
, checksum_offset
);
165 int ospf_lsa_checksum_valid(struct lsa_header
*lsa
)
167 uint8_t *buffer
= &lsa
->options
;
168 int options_offset
= buffer
- (uint8_t *)&lsa
->ls_age
; /* should be 2 */
170 /* Skip the AGE field */
171 uint16_t len
= ntohs(lsa
->length
) - options_offset
;
173 return (fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
)
178 /* Create OSPF LSA. */
179 struct ospf_lsa
*ospf_lsa_new(void)
181 struct ospf_lsa
*new;
183 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
187 new->retransmit_counter
= 0;
188 monotime(&new->tv_recv
);
189 new->tv_orig
= new->tv_recv
;
190 new->refresh_list
= -1;
191 new->vrf_id
= VRF_DEFAULT
;
192 new->to_be_acknowledged
= 0;
193 new->opaque_zero_len_delete
= 0;
198 struct ospf_lsa
*ospf_lsa_new_and_data(size_t size
)
200 struct ospf_lsa
*new;
202 new = ospf_lsa_new();
203 new->data
= ospf_lsa_data_new(size
);
209 /* Duplicate OSPF LSA. */
210 struct ospf_lsa
*ospf_lsa_dup(struct ospf_lsa
*lsa
)
212 struct ospf_lsa
*new;
217 new = XCALLOC(MTYPE_OSPF_LSA
, sizeof(struct ospf_lsa
));
219 memcpy(new, lsa
, sizeof(struct ospf_lsa
));
220 UNSET_FLAG(new->flags
, OSPF_LSA_DISCARD
);
222 new->retransmit_counter
= 0;
223 new->data
= ospf_lsa_data_dup(lsa
->data
);
225 /* kevinm: Clear the refresh_list, otherwise there are going
226 to be problems when we try to remove the LSA from the
227 queue (which it's not a member of.)
228 XXX: Should we add the LSA to the refresh_list queue? */
229 new->refresh_list
= -1;
231 if (IS_DEBUG_OSPF(lsa
, LSA
))
232 zlog_debug("LSA: duplicated %p (new: %p)", (void *)lsa
,
239 void ospf_lsa_free(struct ospf_lsa
*lsa
)
241 assert(lsa
->lock
== 0);
243 if (IS_DEBUG_OSPF(lsa
, LSA
))
244 zlog_debug("LSA: freed %p", (void *)lsa
);
246 /* Delete LSA data. */
247 if (lsa
->data
!= NULL
)
248 ospf_lsa_data_free(lsa
->data
);
250 assert(lsa
->refresh_list
< 0);
252 memset(lsa
, 0, sizeof(struct ospf_lsa
));
253 XFREE(MTYPE_OSPF_LSA
, lsa
);
257 struct ospf_lsa
*ospf_lsa_lock(struct ospf_lsa
*lsa
)
264 void ospf_lsa_unlock(struct ospf_lsa
**lsa
)
266 /* This is sanity check. */
272 assert((*lsa
)->lock
>= 0);
274 if ((*lsa
)->lock
== 0) {
275 assert(CHECK_FLAG((*lsa
)->flags
, OSPF_LSA_DISCARD
));
281 /* Check discard flag. */
282 void ospf_lsa_discard(struct ospf_lsa
*lsa
)
284 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
285 SET_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
);
286 ospf_lsa_unlock(&lsa
);
290 /* Create LSA data. */
291 struct lsa_header
*ospf_lsa_data_new(size_t size
)
293 return XCALLOC(MTYPE_OSPF_LSA_DATA
, size
);
296 /* Duplicate LSA data. */
297 struct lsa_header
*ospf_lsa_data_dup(struct lsa_header
*lsah
)
299 struct lsa_header
*new;
301 new = ospf_lsa_data_new(ntohs(lsah
->length
));
302 memcpy(new, lsah
, ntohs(lsah
->length
));
308 void ospf_lsa_data_free(struct lsa_header
*lsah
)
310 if (IS_DEBUG_OSPF(lsa
, LSA
))
311 zlog_debug("LSA[Type%d:%pI4]: data freed %p", lsah
->type
,
312 &lsah
->id
, (void *)lsah
);
314 XFREE(MTYPE_OSPF_LSA_DATA
, lsah
);
318 /* LSA general functions. */
320 const char *dump_lsa_key(struct ospf_lsa
*lsa
)
322 static char buf
[sizeof("Type255,id(255.255.255.255),ar(255.255.255.255)")+1];
323 struct lsa_header
*lsah
;
325 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
) {
326 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
327 inet_ntop(AF_INET
, &lsah
->id
, id
, sizeof(id
));
328 inet_ntop(AF_INET
, &lsah
->adv_router
, ar
, sizeof(ar
));
330 snprintf(buf
, sizeof(buf
), "Type%d,id(%s),ar(%s)", lsah
->type
,
333 strlcpy(buf
, "NULL", sizeof(buf
));
338 uint32_t lsa_seqnum_increment(struct ospf_lsa
*lsa
)
342 seqnum
= ntohl(lsa
->data
->ls_seqnum
) + 1;
344 return htonl(seqnum
);
347 void lsa_header_set(struct stream
*s
, uint8_t options
, uint8_t type
,
348 struct in_addr id
, struct in_addr router_id
)
350 struct lsa_header
*lsah
;
352 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
354 lsah
->ls_age
= htons(OSPF_LSA_INITIAL_AGE
);
355 lsah
->options
= options
;
358 lsah
->adv_router
= router_id
;
359 lsah
->ls_seqnum
= htonl(OSPF_INITIAL_SEQUENCE_NUMBER
);
361 stream_forward_endp(s
, OSPF_LSA_HEADER_SIZE
);
365 /* router-LSA related functions. */
366 /* Get router-LSA flags. */
367 uint8_t router_lsa_flags(struct ospf_area
*area
)
371 flags
= area
->ospf
->flags
;
373 /* Set virtual link flag. */
374 if (ospf_full_virtual_nbrs(area
))
375 SET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
377 /* Just sanity check */
378 UNSET_FLAG(flags
, ROUTER_LSA_VIRTUAL
);
380 /* Set Shortcut ABR behabiour flag. */
381 UNSET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
382 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
383 if (!OSPF_IS_AREA_BACKBONE(area
))
384 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
385 && area
->ospf
->backbone
== NULL
)
386 || area
->shortcut_configured
387 == OSPF_SHORTCUT_ENABLE
)
388 SET_FLAG(flags
, ROUTER_LSA_SHORTCUT
);
390 /* ASBR can't exit in stub area. */
391 if (area
->external_routing
== OSPF_AREA_STUB
)
392 UNSET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
393 /* If ASBR set External flag */
394 else if (IS_OSPF_ASBR(area
->ospf
))
395 SET_FLAG(flags
, ROUTER_LSA_EXTERNAL
);
397 /* Set ABR dependent flags */
398 if (IS_OSPF_ABR(area
->ospf
)) {
399 SET_FLAG(flags
, ROUTER_LSA_BORDER
);
400 /* If Area is NSSA and we are both ABR and unconditional
402 * set Nt bit to inform other routers.
404 if ((area
->external_routing
== OSPF_AREA_NSSA
)
405 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
406 SET_FLAG(flags
, ROUTER_LSA_NT
);
411 /* Lookup neighbor other than myself.
412 And check neighbor count,
413 Point-to-Point link must have only 1 neighbor. */
414 struct ospf_neighbor
*ospf_nbr_lookup_ptop(struct ospf_interface
*oi
)
416 struct ospf_neighbor
*nbr
= NULL
;
417 struct route_node
*rn
;
419 /* Search neighbor, there must be one of two nbrs. */
420 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
421 if ((nbr
= rn
->info
))
422 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
423 &oi
->ospf
->router_id
))
424 if (nbr
->state
== NSM_Full
) {
425 route_unlock_node(rn
);
429 /* PtoP link must have only 1 neighbor. */
430 if (ospf_nbr_count(oi
, 0) > 1)
431 flog_warn(EC_OSPF_PTP_NEIGHBOR
,
432 "Point-to-Point link has more than 1 neighobrs.");
437 /* Determine cost of link, taking RFC3137 stub-router support into
440 static uint16_t ospf_link_cost(struct ospf_interface
*oi
)
442 /* RFC3137 stub router support */
443 if (!CHECK_FLAG(oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
444 return oi
->output_cost
;
446 return OSPF_OUTPUT_COST_INFINITE
;
449 /* Set a link information. */
450 char link_info_set(struct stream
**s
, struct in_addr id
, struct in_addr data
,
451 uint8_t type
, uint8_t tos
, uint16_t cost
)
453 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
454 * vast majority of cases. Some rare routers with lots of links need
456 * we try accommodate those here.
458 if (STREAM_WRITEABLE(*s
) < OSPF_ROUTER_LSA_LINK_SIZE
) {
459 size_t ret
= OSPF_MAX_LSA_SIZE
;
461 /* Can we enlarge the stream still? */
462 if (STREAM_SIZE(*s
) == OSPF_MAX_LSA_SIZE
) {
463 /* we futz the size here for simplicity, really we need
466 * IP Header - (sizeof(struct ip))
467 * OSPF Header - OSPF_HEADER_SIZE
468 * LSA Header - OSPF_LSA_HEADER_SIZE
469 * MD5 auth data, if MD5 is configured -
470 * OSPF_AUTH_MD5_SIZE.
472 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
474 ret
= stream_resize_inplace(
475 s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
478 if (ret
== OSPF_MAX_LSA_SIZE
) {
481 "%s: Out of space in LSA stream, left %zd, size %zd",
482 __func__
, STREAM_WRITEABLE(*s
),
488 /* TOS based routing is not supported. */
489 stream_put_ipv4(*s
, id
.s_addr
); /* Link ID. */
490 stream_put_ipv4(*s
, data
.s_addr
); /* Link Data. */
491 stream_putc(*s
, type
); /* Link Type. */
492 stream_putc(*s
, tos
); /* TOS = 0. */
493 stream_putw(*s
, cost
); /* Link Cost. */
498 /* Describe Point-to-Point link (Section 12.4.1.1). */
500 /* Note: If the interface is configured as point-to-point dmvpn then the other
501 * end of link is dmvpn hub with point-to-multipoint ospf network type. The
502 * hub then expects this router to populate the stub network and also Link Data
503 * Field set to IP Address and not MIB-II ifIndex
505 static int lsa_link_ptop_set(struct stream
**s
, struct ospf_interface
*oi
)
508 struct ospf_neighbor
*nbr
;
509 struct in_addr id
, mask
, data
;
510 uint16_t cost
= ospf_link_cost(oi
);
512 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
513 zlog_debug("LSA[Type1]: Set link Point-to-Point");
515 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
516 if (nbr
->state
== NSM_Full
) {
517 if (CHECK_FLAG(oi
->connected
->flags
,
518 ZEBRA_IFA_UNNUMBERED
)
520 /* For unnumbered point-to-point networks, the
522 should specify the interface's MIB-II ifIndex
524 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
525 links
+= link_info_set(
526 s
, nbr
->router_id
, data
,
527 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
529 links
+= link_info_set(
531 oi
->address
->u
.prefix4
,
532 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
536 /* no need for a stub link for unnumbered interfaces */
538 || !CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)) {
539 /* Regardless of the state of the neighboring router, we must
540 add a Type 3 link (stub network).
541 N.B. Options 1 & 2 share basically the same logic. */
542 masklen2ip(oi
->address
->prefixlen
, &mask
);
543 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
545 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
552 /* Describe Broadcast Link. */
553 static int lsa_link_broadcast_set(struct stream
**s
, struct ospf_interface
*oi
)
555 struct ospf_neighbor
*dr
;
556 struct in_addr id
, mask
;
557 uint16_t cost
= ospf_link_cost(oi
);
559 /* Describe Type 3 Link. */
560 if (oi
->state
== ISM_Waiting
) {
561 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
563 "LSA[Type1]: Interface %s is in state Waiting. Adding stub interface",
565 masklen2ip(oi
->address
->prefixlen
, &mask
);
566 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
567 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
571 dr
= ospf_nbr_lookup_by_addr(oi
->nbrs
, &DR(oi
));
572 /* Describe Type 2 link. */
573 if (dr
&& (dr
->state
== NSM_Full
574 || IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &DR(oi
)))
575 && ospf_nbr_count(oi
, NSM_Full
) > 0) {
576 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
578 "LSA[Type1]: Interface %s has a DR. Adding transit interface",
580 return link_info_set(s
, DR(oi
), oi
->address
->u
.prefix4
,
581 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
583 /* Describe type 3 link. */
585 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
587 "LSA[Type1]: Interface %s has no DR. Adding stub interface",
589 masklen2ip(oi
->address
->prefixlen
, &mask
);
590 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
591 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
596 static int lsa_link_loopback_set(struct stream
**s
, struct ospf_interface
*oi
)
598 struct in_addr id
, mask
;
600 /* Describe Type 3 Link. */
601 if (oi
->state
!= ISM_Loopback
)
604 mask
.s_addr
= 0xffffffff;
605 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
606 return link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
609 /* Describe Virtual Link. */
610 static int lsa_link_virtuallink_set(struct stream
**s
,
611 struct ospf_interface
*oi
)
613 struct ospf_neighbor
*nbr
;
614 uint16_t cost
= ospf_link_cost(oi
);
616 if (oi
->state
== ISM_PointToPoint
)
617 if ((nbr
= ospf_nbr_lookup_ptop(oi
)))
618 if (nbr
->state
== NSM_Full
) {
619 return link_info_set(s
, nbr
->router_id
,
620 oi
->address
->u
.prefix4
,
621 LSA_LINK_TYPE_VIRTUALLINK
,
628 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
630 /* this function add for support point-to-multipoint ,see rfc2328
632 /* from "edward rrr" <edward_rrr@hotmail.com>
633 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
634 static int lsa_link_ptomp_set(struct stream
**s
, struct ospf_interface
*oi
)
637 struct route_node
*rn
;
638 struct ospf_neighbor
*nbr
= NULL
;
639 struct in_addr id
, mask
;
640 uint16_t cost
= ospf_link_cost(oi
);
642 mask
.s_addr
= 0xffffffff;
643 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
644 links
+= link_info_set(s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
646 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
647 zlog_debug("PointToMultipoint: running ptomultip_set");
649 /* Search neighbor, */
650 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
651 if ((nbr
= rn
->info
) != NULL
)
653 if (!IPV4_ADDR_SAME(&nbr
->router_id
,
654 &oi
->ospf
->router_id
))
655 if (nbr
->state
== NSM_Full
)
658 links
+= link_info_set(
660 oi
->address
->u
.prefix4
,
661 LSA_LINK_TYPE_POINTOPOINT
, 0,
663 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
665 "PointToMultipoint: set link to %pI4",
666 &oi
->address
->u
.prefix4
);
672 /* Set router-LSA link information. */
673 static int router_lsa_link_set(struct stream
**s
, struct ospf_area
*area
)
675 struct listnode
*node
;
676 struct ospf_interface
*oi
;
679 for (ALL_LIST_ELEMENTS_RO(area
->oiflist
, node
, oi
)) {
680 struct interface
*ifp
= oi
->ifp
;
682 /* Check interface is up, OSPF is enable. */
683 if (if_is_operative(ifp
)) {
684 if (oi
->state
!= ISM_Down
) {
685 oi
->lsa_pos_beg
= links
;
686 /* Describe each link. */
688 case OSPF_IFTYPE_POINTOPOINT
:
689 links
+= lsa_link_ptop_set(s
, oi
);
691 case OSPF_IFTYPE_BROADCAST
:
692 links
+= lsa_link_broadcast_set(s
, oi
);
694 case OSPF_IFTYPE_NBMA
:
695 links
+= lsa_link_nbma_set(s
, oi
);
697 case OSPF_IFTYPE_POINTOMULTIPOINT
:
698 links
+= lsa_link_ptomp_set(s
, oi
);
700 case OSPF_IFTYPE_VIRTUALLINK
:
702 lsa_link_virtuallink_set(s
, oi
);
704 case OSPF_IFTYPE_LOOPBACK
:
705 links
+= lsa_link_loopback_set(s
, oi
);
707 oi
->lsa_pos_end
= links
;
715 /* Set router-LSA body. */
716 void ospf_router_lsa_body_set(struct stream
**s
, struct ospf_area
*area
)
722 stream_putc(*s
, router_lsa_flags(area
));
724 /* Set Zero fields. */
727 /* Keep pointer to # links. */
728 putp
= stream_get_endp(*s
);
733 /* Set all link information. */
734 cnt
= router_lsa_link_set(s
, area
);
736 /* Set # of links here. */
737 stream_putw_at(*s
, putp
, cnt
);
740 static void ospf_stub_router_timer(struct thread
*t
)
742 struct ospf_area
*area
= THREAD_ARG(t
);
744 area
->t_stub_router
= NULL
;
746 SET_FLAG(area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
748 /* clear stub route state and generate router-lsa refresh, don't
749 * clobber an administratively set stub-router state though.
751 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
754 UNSET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
756 ospf_router_lsa_update_area(area
);
759 static void ospf_stub_router_check(struct ospf_area
*area
)
761 /* area must either be administratively configured to be stub
762 * or startup-time stub-router must be configured and we must in a
766 if (CHECK_FLAG(area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
)) {
767 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
771 /* not admin-stubbed, check whether startup stubbing is configured and
772 * whether it's not been done yet
774 if (CHECK_FLAG(area
->stub_router_state
,
775 OSPF_AREA_WAS_START_STUB_ROUTED
))
778 if (area
->ospf
->stub_router_startup_time
779 == OSPF_STUB_ROUTER_UNCONFIGURED
) {
780 /* stub-router is hence done forever for this area, even if
782 * tries configure it (take effect next restart).
784 SET_FLAG(area
->stub_router_state
,
785 OSPF_AREA_WAS_START_STUB_ROUTED
);
789 /* startup stub-router configured and not yet done */
790 SET_FLAG(area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
792 OSPF_AREA_TIMER_ON(area
->t_stub_router
, ospf_stub_router_timer
,
793 area
->ospf
->stub_router_startup_time
);
796 /* Create new router-LSA. */
797 static struct ospf_lsa
*ospf_router_lsa_new(struct ospf_area
*area
)
799 struct ospf
*ospf
= area
->ospf
;
801 struct lsa_header
*lsah
;
802 struct ospf_lsa
*new;
805 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
806 zlog_debug("LSA[Type1]: Create router-LSA instance");
808 /* check whether stub-router is desired, and if this is the first
811 ospf_stub_router_check(area
);
813 /* Create a stream for LSA. */
814 s
= stream_new(OSPF_MAX_LSA_SIZE
);
815 /* Set LSA common header fields. */
816 lsa_header_set(s
, LSA_OPTIONS_GET(area
) | LSA_OPTIONS_NSSA_GET(area
),
817 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
819 /* Set router-LSA body fields. */
820 ospf_router_lsa_body_set(&s
, area
);
823 length
= stream_get_endp(s
);
824 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
825 lsah
->length
= htons(length
);
827 /* Now, create OSPF LSA instance. */
828 new = ospf_lsa_new_and_data(length
);
831 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
832 new->vrf_id
= area
->ospf
->vrf_id
;
834 /* Copy LSA data to store, discard stream. */
835 memcpy(new->data
, lsah
, length
);
841 /* Originate Router-LSA. */
842 static struct ospf_lsa
*ospf_router_lsa_originate(struct ospf_area
*area
)
844 struct ospf_lsa
*new;
846 if (area
->ospf
->gr_info
.restart_in_progress
) {
847 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
849 "LSA[Type%d]: Graceful Restart in progress, don't originate",
854 /* Create new router-LSA instance. */
855 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
856 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
861 if (new->data
->adv_router
.s_addr
== INADDR_ANY
) {
862 if (IS_DEBUG_OSPF_EVENT
)
863 zlog_debug("LSA[Type1]: AdvRouter is 0, discard");
864 ospf_lsa_discard(new);
868 /* Install LSA to LSDB. */
869 new = ospf_lsa_install(area
->ospf
, NULL
, new);
871 /* Update LSA origination count. */
872 area
->ospf
->lsa_originate_count
++;
874 /* Flooding new LSA through area. */
875 ospf_flood_through_area(area
, NULL
, new);
877 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
878 zlog_debug("LSA[Type%d:%pI4]: Originate router-LSA %p",
879 new->data
->type
, &new->data
->id
,
881 ospf_lsa_header_dump(new->data
);
887 /* Refresh router-LSA. */
888 static struct ospf_lsa
*ospf_router_lsa_refresh(struct ospf_lsa
*lsa
)
890 struct ospf_area
*area
= lsa
->area
;
891 struct ospf_lsa
*new;
896 /* Delete LSA from neighbor retransmit-list. */
897 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
899 /* Unregister LSA from refresh-list */
900 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
902 /* Create new router-LSA instance. */
903 if ((new = ospf_router_lsa_new(area
)) == NULL
) {
904 zlog_err("%s: ospf_router_lsa_new returned NULL", __func__
);
908 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
910 ospf_lsa_install(area
->ospf
, NULL
, new);
912 /* Flood LSA through area. */
913 ospf_flood_through_area(area
, NULL
, new);
916 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
917 zlog_debug("LSA[Type%d:%pI4]: router-LSA refresh",
918 new->data
->type
, &new->data
->id
);
919 ospf_lsa_header_dump(new->data
);
925 int ospf_router_lsa_update_area(struct ospf_area
*area
)
927 if (IS_DEBUG_OSPF_EVENT
)
928 zlog_debug("[router-LSA]: (router-LSA area update)");
930 /* Now refresh router-LSA. */
931 if (area
->router_lsa_self
)
932 ospf_lsa_refresh(area
->ospf
, area
->router_lsa_self
);
933 /* Newly originate router-LSA. */
935 ospf_router_lsa_originate(area
);
940 int ospf_router_lsa_update(struct ospf
*ospf
)
942 struct listnode
*node
, *nnode
;
943 struct ospf_area
*area
;
945 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
946 zlog_debug("Timer[router-LSA Update]: (timer expire)");
948 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
949 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
950 struct router_lsa
*rl
;
951 const char *area_str
;
953 /* Keep Area ID string. */
954 area_str
= AREA_NAME(area
);
956 /* If LSA not exist in this Area, originate new. */
958 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
960 "LSA[Type1]: Create router-LSA for Area %s",
963 ospf_router_lsa_originate(area
);
965 /* If router-ID is changed, Link ID must change.
966 First flush old LSA, then originate new. */
967 else if (!IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
)) {
968 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
970 "LSA[Type%d:%pI4]: Refresh router-LSA for Area %s",
972 &lsa
->data
->id
, area_str
);
973 ospf_refresher_unregister_lsa(ospf
, lsa
);
974 ospf_lsa_flush_area(lsa
, area
);
975 ospf_lsa_unlock(&area
->router_lsa_self
);
976 area
->router_lsa_self
= NULL
;
978 /* Refresh router-LSA, (not install) and flood through
980 ospf_router_lsa_update_area(area
);
982 rl
= (struct router_lsa
*)lsa
->data
;
983 /* Refresh router-LSA, (not install) and flood through
985 if (rl
->flags
!= ospf
->flags
)
986 ospf_router_lsa_update_area(area
);
994 /* network-LSA related functions. */
995 /* Originate Network-LSA. */
996 static void ospf_network_lsa_body_set(struct stream
*s
,
997 struct ospf_interface
*oi
)
1000 struct route_node
*rn
;
1001 struct ospf_neighbor
*nbr
;
1003 masklen2ip(oi
->address
->prefixlen
, &mask
);
1004 stream_put_ipv4(s
, mask
.s_addr
);
1006 /* The network-LSA lists those routers that are fully adjacent to
1007 the Designated Router; each fully adjacent router is identified by
1008 its OSPF Router ID. The Designated Router includes itself in this
1009 list. RFC2328, Section 12.4.2 */
1011 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
1012 if ((nbr
= rn
->info
) != NULL
)
1013 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
1014 stream_put_ipv4(s
, nbr
->router_id
.s_addr
);
1017 static struct ospf_lsa
*ospf_network_lsa_new(struct ospf_interface
*oi
)
1020 struct ospf_lsa
*new;
1021 struct lsa_header
*lsah
;
1022 struct ospf_if_params
*oip
;
1025 /* If there are no neighbours on this network (the net is stub),
1026 the router does not originate network-LSA (see RFC 12.4.2) */
1027 if (oi
->full_nbrs
== 0)
1030 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1031 zlog_debug("LSA[Type2]: Create network-LSA instance");
1033 /* Create new stream for LSA. */
1034 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1035 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1037 lsa_header_set(s
, (OPTIONS(oi
) | LSA_OPTIONS_GET(oi
->area
)),
1038 OSPF_NETWORK_LSA
, DR(oi
), oi
->ospf
->router_id
);
1040 /* Set network-LSA body fields. */
1041 ospf_network_lsa_body_set(s
, oi
);
1044 length
= stream_get_endp(s
);
1045 lsah
->length
= htons(length
);
1047 /* Create OSPF LSA instance. */
1048 new = ospf_lsa_new_and_data(length
);
1050 new->area
= oi
->area
;
1051 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1052 new->vrf_id
= oi
->ospf
->vrf_id
;
1054 /* Copy LSA to store. */
1055 memcpy(new->data
, lsah
, length
);
1058 /* Remember prior network LSA sequence numbers, even if we stop
1059 * originating one for this oi, to try avoid re-originating LSAs with a
1060 * prior sequence number, and thus speed up adjency forming &
1063 if ((oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
))) {
1064 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1065 new->data
->ls_seqnum
= lsa_seqnum_increment(new);
1067 oip
= ospf_get_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1068 ospf_if_update_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1070 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1075 /* Originate network-LSA. */
1076 void ospf_network_lsa_update(struct ospf_interface
*oi
)
1078 struct ospf_lsa
*new;
1080 if (oi
->area
->ospf
->gr_info
.restart_in_progress
) {
1081 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1083 "LSA[Type%d]: Graceful Restart in progress, don't originate",
1088 if (oi
->network_lsa_self
!= NULL
) {
1089 ospf_lsa_refresh(oi
->ospf
, oi
->network_lsa_self
);
1093 /* Create new network-LSA instance. */
1094 new = ospf_network_lsa_new(oi
);
1098 /* Install LSA to LSDB. */
1099 new = ospf_lsa_install(oi
->ospf
, oi
, new);
1101 /* Update LSA origination count. */
1102 oi
->ospf
->lsa_originate_count
++;
1104 /* Flooding new LSA through area. */
1105 ospf_flood_through_area(oi
->area
, NULL
, new);
1107 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1108 zlog_debug("LSA[Type%d:%pI4]: Originate network-LSA %p",
1109 new->data
->type
, &new->data
->id
,
1111 ospf_lsa_header_dump(new->data
);
1117 static struct ospf_lsa
*ospf_network_lsa_refresh(struct ospf_lsa
*lsa
)
1119 struct ospf_area
*area
= lsa
->area
;
1120 struct ospf_lsa
*new, *new2
;
1121 struct ospf_if_params
*oip
;
1122 struct ospf_interface
*oi
;
1126 /* Retrieve the oi for the network LSA */
1127 oi
= ospf_if_lookup_by_local_addr(area
->ospf
, NULL
, lsa
->data
->id
);
1129 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1131 "LSA[Type%d:%pI4]: network-LSA refresh: no oi found, ick, ignoring.",
1132 lsa
->data
->type
, &lsa
->data
->id
);
1133 ospf_lsa_header_dump(lsa
->data
);
1137 /* Delete LSA from neighbor retransmit-list. */
1138 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
1140 /* Unregister LSA from refresh-list */
1141 ospf_refresher_unregister_lsa(area
->ospf
, lsa
);
1143 /* Create new network-LSA instance. */
1144 new = ospf_network_lsa_new(oi
);
1148 oip
= ospf_lookup_if_params(oi
->ifp
, oi
->address
->u
.prefix4
);
1149 assert(oip
!= NULL
);
1150 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
=
1151 lsa_seqnum_increment(lsa
);
1153 new2
= ospf_lsa_install(area
->ospf
, oi
, new);
1155 assert(new2
== new);
1157 /* Flood LSA through aera. */
1158 ospf_flood_through_area(area
, NULL
, new);
1160 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1161 zlog_debug("LSA[Type%d:%pI4]: network-LSA refresh",
1162 new->data
->type
, &new->data
->id
);
1163 ospf_lsa_header_dump(new->data
);
1169 static void stream_put_ospf_metric(struct stream
*s
, uint32_t metric_value
)
1174 /* Put 0 metric. TOS metric is not supported. */
1175 metric
= htonl(metric_value
);
1176 mp
= (char *)&metric
;
1178 stream_put(s
, mp
, 3);
1181 /* summary-LSA related functions. */
1182 static void ospf_summary_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1185 struct in_addr mask
;
1187 masklen2ip(p
->prefixlen
, &mask
);
1189 /* Put Network Mask. */
1190 stream_put_ipv4(s
, mask
.s_addr
);
1193 stream_putc(s
, (uint8_t)0);
1196 stream_put_ospf_metric(s
, metric
);
1199 static struct ospf_lsa
*ospf_summary_lsa_new(struct ospf_area
*area
,
1200 struct prefix
*p
, uint32_t metric
,
1204 struct ospf_lsa
*new;
1205 struct lsa_header
*lsah
;
1208 if (id
.s_addr
== 0xffffffff) {
1209 /* Maybe Link State ID not available. */
1210 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1212 "LSA[Type%d]: Link ID not available, can't originate",
1217 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1218 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1220 /* Create new stream for LSA. */
1221 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1222 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1224 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_SUMMARY_LSA
, id
,
1225 area
->ospf
->router_id
);
1227 /* Set summary-LSA body fields. */
1228 ospf_summary_lsa_body_set(s
, p
, metric
);
1231 length
= stream_get_endp(s
);
1232 lsah
->length
= htons(length
);
1234 /* Create OSPF LSA instance. */
1235 new = ospf_lsa_new_and_data(length
);
1237 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1238 new->vrf_id
= area
->ospf
->vrf_id
;
1240 /* Copy LSA to store. */
1241 memcpy(new->data
, lsah
, length
);
1247 /* Originate Summary-LSA. */
1248 static struct ospf_lsa
*
1249 ospf_summary_lsa_prepare_and_flood(struct prefix_ipv4
*p
, uint32_t metric
,
1250 struct ospf_area
*area
, struct in_addr id
)
1252 struct ospf_lsa
*new;
1254 /* Create new summary-LSA instance. */
1255 if (!(new = ospf_summary_lsa_new(area
, (struct prefix
*)p
, metric
, id
)))
1258 /* Instlal LSA to LSDB. */
1259 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1261 /* Update LSA origination count. */
1262 area
->ospf
->lsa_originate_count
++;
1264 /* Flooding new LSA through area. */
1265 ospf_flood_through_area(area
, NULL
, new);
1267 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1268 zlog_debug("LSA[Type%d:%pI4]: Originate summary-LSA %p",
1269 new->data
->type
, &new->data
->id
,
1271 ospf_lsa_header_dump(new->data
);
1277 static struct ospf_lsa
*ospf_handle_summarylsa_lsId_chg(struct ospf
*ospf
,
1278 struct prefix_ipv4
*p
,
1281 struct in_addr old_id
)
1283 struct ospf_lsa
*lsa
= NULL
;
1284 struct ospf_lsa
*new = NULL
;
1285 struct summary_lsa
*sl
= NULL
;
1286 struct ospf_area
*old_area
= NULL
;
1287 struct prefix_ipv4 old_prefix
;
1288 uint32_t old_metric
;
1289 struct in_addr mask
;
1290 uint32_t metric_val
;
1293 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, p
->prefix
,
1297 flog_warn(EC_OSPF_LSA_NULL
, "(%s): LSA not found", __func__
);
1301 sl
= (struct summary_lsa
*)lsa
->data
;
1303 old_area
= lsa
->area
;
1304 old_metric
= GET_METRIC(sl
->metric
);
1305 old_prefix
.prefix
= sl
->header
.id
;
1306 old_prefix
.prefixlen
= ip_masklen(sl
->mask
);
1307 old_prefix
.family
= AF_INET
;
1310 /* change the mask */
1311 masklen2ip(p
->prefixlen
, &mask
);
1312 sl
->mask
.s_addr
= mask
.s_addr
;
1314 /* Copy the metric*/
1315 metric_val
= htonl(metric
);
1316 metric_buf
= (char *)&metric_val
;
1317 memcpy(sl
->metric
, metric_buf
, sizeof(metric_val
));
1319 if (type
== OSPF_SUMMARY_LSA
) {
1320 /*Refresh the LSA with new LSA*/
1321 ospf_summary_lsa_refresh(ospf
, lsa
);
1323 new = ospf_summary_lsa_prepare_and_flood(
1324 &old_prefix
, old_metric
, old_area
, old_id
);
1326 /*Refresh the LSA with new LSA*/
1327 ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
1329 new = ospf_asbr_summary_lsa_prepare_and_flood(
1330 &old_prefix
, old_metric
, old_area
, old_id
);
1336 /* Originate Summary-LSA. */
1337 struct ospf_lsa
*ospf_summary_lsa_originate(struct prefix_ipv4
*p
,
1339 struct ospf_area
*area
)
1342 enum lsid_status status
;
1343 struct ospf_lsa
*new = NULL
;
1345 status
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
,
1348 if (status
== LSID_CHANGE
) {
1349 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1350 zlog_debug("Link ID has to be changed.");
1352 new = ospf_handle_summarylsa_lsId_chg(
1353 area
->ospf
, p
, OSPF_SUMMARY_LSA
, metric
, id
);
1355 } else if (status
== LSID_NOT_AVAILABLE
) {
1356 /* Link State ID not available. */
1357 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1359 "LSA[Type5]: Link ID not available, can't originate");
1364 new = ospf_summary_lsa_prepare_and_flood(p
, metric
, area
, id
);
1368 static struct ospf_lsa
*ospf_summary_lsa_refresh(struct ospf
*ospf
,
1369 struct ospf_lsa
*lsa
)
1371 struct ospf_lsa
*new;
1372 struct summary_lsa
*sl
;
1378 sl
= (struct summary_lsa
*)lsa
->data
;
1379 p
.prefixlen
= ip_masklen(sl
->mask
);
1380 new = ospf_summary_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1386 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1388 ospf_lsa_install(ospf
, NULL
, new);
1390 /* Flood LSA through AS. */
1391 ospf_flood_through_area(new->area
, NULL
, new);
1393 /* Debug logging. */
1394 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1395 zlog_debug("LSA[Type%d:%pI4]: summary-LSA refresh",
1396 new->data
->type
, &new->data
->id
);
1397 ospf_lsa_header_dump(new->data
);
1404 /* summary-ASBR-LSA related functions. */
1405 static void ospf_summary_asbr_lsa_body_set(struct stream
*s
, struct prefix
*p
,
1408 /* Put Network Mask. */
1409 stream_put_ipv4(s
, (uint32_t)0);
1412 stream_putc(s
, (uint8_t)0);
1415 stream_put_ospf_metric(s
, metric
);
1418 static struct ospf_lsa
*ospf_summary_asbr_lsa_new(struct ospf_area
*area
,
1424 struct ospf_lsa
*new;
1425 struct lsa_header
*lsah
;
1428 if (id
.s_addr
== 0xffffffff) {
1429 /* Maybe Link State ID not available. */
1430 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1432 "LSA[Type%d]: Link ID not available, can't originate",
1433 OSPF_ASBR_SUMMARY_LSA
);
1437 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1438 zlog_debug("LSA[Type3]: Create summary-LSA instance");
1440 /* Create new stream for LSA. */
1441 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1442 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1444 lsa_header_set(s
, LSA_OPTIONS_GET(area
), OSPF_ASBR_SUMMARY_LSA
, id
,
1445 area
->ospf
->router_id
);
1447 /* Set summary-LSA body fields. */
1448 ospf_summary_asbr_lsa_body_set(s
, p
, metric
);
1451 length
= stream_get_endp(s
);
1452 lsah
->length
= htons(length
);
1454 /* Create OSPF LSA instance. */
1455 new = ospf_lsa_new_and_data(length
);
1457 SET_FLAG(new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1458 new->vrf_id
= area
->ospf
->vrf_id
;
1460 /* Copy LSA to store. */
1461 memcpy(new->data
, lsah
, length
);
1467 /* Originate summary-ASBR-LSA. */
1468 static struct ospf_lsa
*
1469 ospf_asbr_summary_lsa_prepare_and_flood(struct prefix_ipv4
*p
, uint32_t metric
,
1470 struct ospf_area
*area
,
1473 struct ospf_lsa
*new;
1475 /* Create new summary-LSA instance. */
1476 new = ospf_summary_asbr_lsa_new(area
, (struct prefix
*)p
, metric
, id
);
1480 /* Install LSA to LSDB. */
1481 new = ospf_lsa_install(area
->ospf
, NULL
, new);
1483 /* Update LSA origination count. */
1484 area
->ospf
->lsa_originate_count
++;
1486 /* Flooding new LSA through area. */
1487 ospf_flood_through_area(area
, NULL
, new);
1489 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1490 zlog_debug("LSA[Type%d:%pI4]: Originate summary-ASBR-LSA %p",
1491 new->data
->type
, &new->data
->id
,
1493 ospf_lsa_header_dump(new->data
);
1499 struct ospf_lsa
*ospf_summary_asbr_lsa_originate(struct prefix_ipv4
*p
,
1501 struct ospf_area
*area
)
1503 struct ospf_lsa
*new;
1505 enum lsid_status status
;
1507 status
= ospf_lsa_unique_id(area
->ospf
, area
->lsdb
,
1508 OSPF_ASBR_SUMMARY_LSA
, p
, &id
);
1510 if (status
== LSID_CHANGE
) {
1511 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1512 zlog_debug("Link ID has to be changed.");
1514 new = ospf_handle_summarylsa_lsId_chg(
1515 area
->ospf
, p
, OSPF_ASBR_SUMMARY_LSA
, metric
, id
);
1517 } else if (status
== LSID_NOT_AVAILABLE
) {
1518 /* Link State ID not available. */
1519 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1521 "LSA[Type5]: Link ID not available, can't originate");
1526 new = ospf_asbr_summary_lsa_prepare_and_flood(p
, metric
, area
, id
);
1530 static struct ospf_lsa
*ospf_summary_asbr_lsa_refresh(struct ospf
*ospf
,
1531 struct ospf_lsa
*lsa
)
1533 struct ospf_lsa
*new;
1534 struct summary_lsa
*sl
;
1540 sl
= (struct summary_lsa
*)lsa
->data
;
1541 p
.prefixlen
= ip_masklen(sl
->mask
);
1542 new = ospf_summary_asbr_lsa_new(lsa
->area
, &p
, GET_METRIC(sl
->metric
),
1547 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
1549 ospf_lsa_install(ospf
, NULL
, new);
1551 /* Flood LSA through area. */
1552 ospf_flood_through_area(new->area
, NULL
, new);
1554 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
1555 zlog_debug("LSA[Type%d:%pI4]: summary-ASBR-LSA refresh",
1556 new->data
->type
, &new->data
->id
);
1557 ospf_lsa_header_dump(new->data
);
1563 /* AS-external-LSA related functions. */
1565 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1566 is connected, else 0*/
1567 static struct in_addr
ospf_external_lsa_nexthop_get(struct ospf
*ospf
,
1568 struct in_addr nexthop
)
1572 struct listnode
*node
;
1573 struct ospf_interface
*oi
;
1577 if (!nexthop
.s_addr
)
1580 /* Check whether nexthop is covered by OSPF network. */
1581 nh
.family
= AF_INET
;
1582 nh
.u
.prefix4
= nexthop
;
1583 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1585 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1586 * better to make use of the per-ifp table of ois.
1588 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
))
1589 if (if_is_operative(oi
->ifp
))
1590 if (oi
->address
->family
== AF_INET
)
1591 if (prefix_match(oi
->address
, &nh
))
1597 /* NSSA-external-LSA related functions. */
1599 /* Get 1st IP connection for Forward Addr */
1601 struct in_addr
ospf_get_ip_from_ifp(struct ospf_interface
*oi
)
1605 fwd
.s_addr
= INADDR_ANY
;
1607 if (if_is_operative(oi
->ifp
))
1608 return oi
->address
->u
.prefix4
;
1613 /* Get 1st IP connection for Forward Addr */
1614 struct in_addr
ospf_get_nssa_ip(struct ospf_area
*area
)
1617 struct in_addr best_default
;
1618 struct listnode
*node
;
1619 struct ospf_interface
*oi
;
1622 best_default
.s_addr
= 0;
1624 for (ALL_LIST_ELEMENTS_RO(area
->ospf
->oiflist
, node
, oi
)) {
1625 if (if_is_operative(oi
->ifp
))
1626 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1628 && oi
->address
->family
== AF_INET
) {
1629 if (best_default
.s_addr
== INADDR_ANY
)
1631 oi
->address
->u
.prefix4
;
1632 if (oi
->area
== area
)
1633 return oi
->address
->u
.prefix4
;
1636 if (best_default
.s_addr
!= INADDR_ANY
)
1637 return best_default
;
1639 if (best_default
.s_addr
!= INADDR_ANY
)
1640 return best_default
;
1645 int metric_type(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1647 struct ospf_redist
*red
;
1649 red
= ospf_redist_lookup(ospf
, src
, instance
);
1651 return ((!red
|| red
->dmetric
.type
< 0) ? DEFAULT_METRIC_TYPE
1652 : red
->dmetric
.type
);
1655 int metric_value(struct ospf
*ospf
, uint8_t src
, unsigned short instance
)
1657 struct ospf_redist
*red
;
1659 red
= ospf_redist_lookup(ospf
, src
, instance
);
1660 if (!red
|| red
->dmetric
.value
< 0) {
1661 if (src
== DEFAULT_ROUTE
) {
1662 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1663 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1665 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1666 } else if (ospf
->default_metric
< 0)
1667 return DEFAULT_DEFAULT_METRIC
;
1669 return ospf
->default_metric
;
1672 return red
->dmetric
.value
;
1675 /* Set AS-external-LSA body. */
1676 static void ospf_external_lsa_body_set(struct stream
*s
,
1677 struct external_info
*ei
,
1680 struct prefix_ipv4
*p
= &ei
->p
;
1681 struct in_addr mask
, fwd_addr
;
1685 unsigned short instance
;
1687 /* Put Network Mask. */
1688 masklen2ip(p
->prefixlen
, &mask
);
1689 stream_put_ipv4(s
, mask
.s_addr
);
1691 /* If prefix is default, specify DEFAULT_ROUTE. */
1692 type
= is_default_prefix4(&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1693 instance
= is_default_prefix4(&ei
->p
) ? 0 : ei
->instance
;
1695 mtype
= (ROUTEMAP_METRIC_TYPE(ei
) != -1)
1696 ? ROUTEMAP_METRIC_TYPE(ei
)
1697 : metric_type(ospf
, type
, instance
);
1699 mvalue
= (ROUTEMAP_METRIC(ei
) != -1)
1700 ? ROUTEMAP_METRIC(ei
)
1701 : metric_value(ospf
, type
, instance
);
1703 /* Put type of external metric. */
1704 stream_putc(s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1706 /* Put 0 metric. TOS metric is not supported. */
1707 stream_put_ospf_metric(s
, mvalue
);
1709 /* Get forwarding address to nexthop if on the Connection List, else 0.
1711 fwd_addr
= ospf_external_lsa_nexthop_get(ospf
, ei
->nexthop
);
1713 /* Put forwarding address. */
1714 stream_put_ipv4(s
, fwd_addr
.s_addr
);
1717 stream_putl(s
, ei
->tag
);
1720 /* Create new external-LSA. */
1721 static struct ospf_lsa
*
1722 ospf_exnl_lsa_prepare_and_flood(struct ospf
*ospf
, struct external_info
*ei
,
1726 struct lsa_header
*lsah
;
1727 struct ospf_lsa
*new;
1730 /* Create new stream for LSA. */
1731 s
= stream_new(OSPF_MAX_LSA_SIZE
);
1732 lsah
= (struct lsa_header
*)STREAM_DATA(s
);
1734 /* Set LSA common header fields. */
1735 lsa_header_set(s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
, id
,
1738 /* Set AS-external-LSA body fields. */
1739 ospf_external_lsa_body_set(s
, ei
, ospf
);
1742 length
= stream_get_endp(s
);
1743 lsah
->length
= htons(length
);
1745 /* Now, create OSPF LSA instance. */
1746 new = ospf_lsa_new_and_data(length
);
1748 SET_FLAG(new->flags
,
1749 OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1750 new->vrf_id
= ospf
->vrf_id
;
1752 /* Copy LSA data to store, discard stream. */
1753 memcpy(new->data
, lsah
, length
);
1759 static struct ospf_lsa
*ospf_handle_exnl_lsa_lsId_chg(struct ospf
*ospf
,
1760 struct external_info
*ei
,
1763 struct ospf_lsa
*lsa
;
1764 struct as_external_lsa
*al
;
1765 struct in_addr mask
;
1766 struct ospf_lsa
*new;
1767 struct external_info ei_summary
= {};
1768 struct external_info
*ei_old
;
1770 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
,
1771 ei
->p
.prefix
, ospf
->router_id
);
1774 flog_warn(EC_OSPF_LSA_NULL
, "(%s): LSA not found", __func__
);
1778 ei_old
= ospf_external_info_check(ospf
, lsa
);
1780 al
= (struct as_external_lsa
*)lsa
->data
;
1783 /* eii_old pointer of LSA is NULL, this
1784 * must be external aggregate route.
1786 ei_summary
.p
.family
= AF_INET
;
1787 ei_summary
.p
.prefix
= al
->header
.id
;
1788 ei_summary
.p
.prefixlen
= ip_masklen(al
->mask
);
1789 ei_summary
.tag
= (unsigned long)ntohl(al
->e
[0].route_tag
);
1790 ei_old
= &ei_summary
;
1793 /* change the mask */
1794 masklen2ip(ei
->p
.prefixlen
, &mask
);
1795 al
->mask
.s_addr
= mask
.s_addr
;
1797 /*Refresh the LSA with new LSA*/
1798 ospf_external_lsa_refresh(ospf
, lsa
, ei
, LSA_REFRESH_FORCE
, 0);
1800 /*Originate the old LSA with changed LSID*/
1801 new = ospf_exnl_lsa_prepare_and_flood(ospf
, ei_old
, id
);
1806 static struct ospf_lsa
*ospf_external_lsa_new(struct ospf
*ospf
,
1807 struct external_info
*ei
,
1808 struct in_addr
*old_id
)
1810 struct ospf_lsa
*new;
1812 enum lsid_status status
;
1815 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1817 "LSA[Type5]: External info is NULL, can't originate");
1821 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1822 zlog_debug("LSA[Type5]: Originate AS-external-LSA instance");
1824 /* If old Link State ID is specified, refresh LSA with same ID. */
1827 /* Get Link State with unique ID. */
1829 status
= ospf_lsa_unique_id(ospf
, ospf
->lsdb
,
1830 OSPF_AS_EXTERNAL_LSA
, &ei
->p
, &id
);
1832 if (status
== LSID_CHANGE
) {
1833 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1834 zlog_debug("Link ID has to be changed.");
1836 new = ospf_handle_exnl_lsa_lsId_chg(ospf
, ei
, id
);
1838 } else if (status
== LSID_NOT_AVAILABLE
) {
1839 /* Link State ID not available. */
1840 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
1842 "LSA[Type5]: Link ID not available, can't originate");
1847 new = ospf_exnl_lsa_prepare_and_flood(ospf
, ei
, id
);
1853 static void ospf_install_flood_nssa(struct ospf
*ospf
, struct ospf_lsa
*lsa
,
1854 struct external_info
*ei
)
1856 struct ospf_lsa
*new;
1857 struct as_external_lsa
*extlsa
;
1858 struct ospf_area
*area
;
1859 struct listnode
*node
, *nnode
;
1861 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1862 * which originated from an NSSA area. In which case it should not be
1863 * flooded back to NSSA areas.
1865 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1868 /* NSSA Originate or Refresh (If anyNSSA)
1870 LSA is self-originated. And just installed as Type-5.
1871 Additionally, install as Type-7 LSDB for every attached NSSA.
1873 P-Bit controls which ABR performs translation to outside world; If
1874 we are an ABR....do not set the P-bit, because we send the Type-5,
1875 not as the ABR Translator, but as the ASBR owner within the AS!
1877 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1878 elected ABR Translator will see the P-bit, Translate, and re-flood.
1880 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1881 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1883 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
1884 /* Don't install Type-7 LSA's into nonNSSA area */
1885 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1888 /* make lsa duplicate, lock=1 */
1889 new = ospf_lsa_dup(lsa
);
1891 new->data
->type
= OSPF_AS_NSSA_LSA
;
1893 /* set P-bit if not ABR */
1894 if (!IS_OSPF_ABR(ospf
)) {
1895 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1897 /* set non-zero FWD ADDR
1899 draft-ietf-ospf-nssa-update-09.txt
1901 if the network between the NSSA AS boundary router and
1903 adjacent AS is advertised into OSPF as an internal OSPF
1905 the forwarding address should be the next op address as
1907 currently done with type-5 LSAs. If the intervening
1909 not adversited into OSPF as an internal OSPF route and
1911 type-7 LSA's P-bit is set a forwarding address should be
1912 selected from one of the router's active OSPF interface
1914 which belong to the NSSA. If no such addresses exist,
1916 no type-7 LSA's with the P-bit set should originate from
1920 /* kevinm: not updating lsa anymore, just new */
1921 extlsa
= (struct as_external_lsa
*)(new->data
);
1923 if (extlsa
->e
[0].fwd_addr
.s_addr
== INADDR_ANY
)
1924 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(
1925 area
); /* this NSSA area in ifp */
1927 if (extlsa
->e
[0].fwd_addr
.s_addr
== INADDR_ANY
) {
1928 if (IS_DEBUG_OSPF_NSSA
)
1930 "LSA[Type-7]: Could not build FWD-ADDR");
1931 ospf_lsa_discard(new);
1936 /* install also as Type-7 */
1937 ospf_lsa_install(ospf
, NULL
,
1938 new); /* Remove Old, Lock New = 2 */
1940 /* will send each copy, lock=2+n */
1941 ospf_flood_through_as(
1942 ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1946 static struct ospf_lsa
*ospf_lsa_translated_nssa_new(struct ospf
*ospf
,
1947 struct ospf_lsa
*type7
)
1950 struct ospf_lsa
*new;
1951 struct as_external_lsa
*ext
, *extnew
;
1952 struct external_info ei
;
1954 ext
= (struct as_external_lsa
*)(type7
->data
);
1956 /* need external_info struct, fill in bare minimum */
1957 ei
.p
.family
= AF_INET
;
1958 ei
.p
.prefix
= type7
->data
->id
;
1959 ei
.p
.prefixlen
= ip_masklen(ext
->mask
);
1960 ei
.type
= ZEBRA_ROUTE_OSPF
;
1961 ei
.nexthop
= ext
->header
.adv_router
;
1962 ei
.route_map_set
.metric
= -1;
1963 ei
.route_map_set
.metric_type
= -1;
1967 if ((new = ospf_external_lsa_new(ospf
, &ei
, &type7
->data
->id
))
1969 if (IS_DEBUG_OSPF_NSSA
)
1971 "%s: Could not originate Translated Type-5 for %pI4",
1972 __func__
, &ei
.p
.prefix
);
1976 extnew
= (struct as_external_lsa
*)(new->data
);
1978 /* copy over Type-7 data to new */
1979 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1980 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1981 if (type7
->area
->suppress_fa
) {
1982 extnew
->e
[0].fwd_addr
.s_addr
= 0;
1983 if (IS_DEBUG_OSPF_NSSA
)
1984 zlog_debug("%s: Suppress forwarding address for %pI4",
1985 __func__
, &ei
.p
.prefix
);
1987 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1988 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1990 /* add translated flag, checksum and lock new lsa */
1991 SET_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1992 new = ospf_lsa_lock(new);
1997 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1998 struct ospf_lsa
*ospf_translated_nssa_originate(struct ospf
*ospf
,
1999 struct ospf_lsa
*type7
,
2000 struct ospf_lsa
*type5
)
2002 struct ospf_lsa
*new;
2003 struct as_external_lsa
*extnew
;
2005 if (ospf
->gr_info
.restart_in_progress
) {
2006 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2008 "LSA[Translated Type5]: Graceful Restart in progress, don't originate");
2012 /* we cant use ospf_external_lsa_originate() as we need to set
2013 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
2016 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
2017 if (IS_DEBUG_OSPF_NSSA
)
2019 "%s: Could not translate Type-7, Id %pI4, to Type-5",
2020 __func__
, &type7
->data
->id
);
2024 extnew
= (struct as_external_lsa
*)new->data
;
2026 /* Update LSA sequence number from translated Type-5 LSA */
2028 new->data
->ls_seqnum
= lsa_seqnum_increment(type5
);
2030 if ((new = ospf_lsa_install(ospf
, NULL
, new)) == NULL
) {
2031 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2032 "%s: Could not install LSA id %pI4", __func__
,
2037 if (IS_DEBUG_OSPF_NSSA
) {
2038 zlog_debug("%s: translated Type 7, installed", __func__
);
2039 ospf_lsa_header_dump(new->data
);
2040 zlog_debug(" Network mask: %d", ip_masklen(extnew
->mask
));
2041 zlog_debug(" Forward addr: %pI4",
2042 &extnew
->e
[0].fwd_addr
);
2045 ospf
->lsa_originate_count
++;
2046 ospf_flood_through_as(ospf
, NULL
, new);
2051 /* Refresh Translated from NSSA AS-external-LSA. */
2052 struct ospf_lsa
*ospf_translated_nssa_refresh(struct ospf
*ospf
,
2053 struct ospf_lsa
*type7
,
2054 struct ospf_lsa
*type5
)
2056 struct ospf_lsa
*new = NULL
;
2057 struct as_external_lsa
*extold
= NULL
;
2058 uint32_t ls_seqnum
= 0;
2060 /* Sanity checks. */
2061 assert(type7
|| type5
);
2062 if (!(type7
|| type5
))
2065 assert(type7
->data
);
2067 assert(type5
->data
);
2068 assert(ospf
->anyNSSA
);
2070 /* get required data according to what has been given */
2071 if (type7
&& type5
== NULL
) {
2072 /* find the translated Type-5 for this Type-7 */
2073 struct as_external_lsa
*ext
=
2074 (struct as_external_lsa
*)(type7
->data
);
2075 struct prefix_ipv4 p
= {
2076 .prefix
= type7
->data
->id
,
2077 .prefixlen
= ip_masklen(ext
->mask
),
2081 type5
= ospf_external_info_find_lsa(ospf
, &p
);
2082 } else if (type5
&& type7
== NULL
) {
2083 /* find the type-7 from which supplied type-5 was translated,
2084 * ie find first type-7 with same LSA Id.
2086 struct listnode
*ln
, *lnn
;
2087 struct route_node
*rn
;
2088 struct ospf_lsa
*lsa
;
2089 struct ospf_area
*area
;
2091 for (ALL_LIST_ELEMENTS(ospf
->areas
, ln
, lnn
, area
)) {
2092 if (area
->external_routing
!= OSPF_AREA_NSSA
&& !type7
)
2095 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
) {
2096 if (lsa
->data
->id
.s_addr
2097 == type5
->data
->id
.s_addr
) {
2105 /* do we have type7? */
2107 if (IS_DEBUG_OSPF_NSSA
)
2108 zlog_debug("%s: no Type-7 found for Type-5 LSA Id %pI4",
2109 __func__
, &type5
->data
->id
);
2113 /* do we have valid translated type5? */
2114 if (type5
== NULL
|| !CHECK_FLAG(type5
->flags
, OSPF_LSA_LOCAL_XLT
)) {
2115 if (IS_DEBUG_OSPF_NSSA
)
2117 "%s: No translated Type-5 found for Type-7 with Id %pI4",
2118 __func__
, &type7
->data
->id
);
2122 extold
= (struct as_external_lsa
*)type5
->data
;
2123 if (type7
->area
->suppress_fa
== 1) {
2124 if (extold
->e
[0].fwd_addr
.s_addr
== 0)
2125 ls_seqnum
= ntohl(type5
->data
->ls_seqnum
);
2128 /* Delete LSA from neighbor retransmit-list. */
2129 ospf_ls_retransmit_delete_nbr_as(ospf
, type5
);
2131 /* create new translated LSA */
2132 if ((new = ospf_lsa_translated_nssa_new(ospf
, type7
)) == NULL
) {
2133 if (IS_DEBUG_OSPF_NSSA
)
2135 "%s: Could not translate Type-7 for %pI4 to Type-5",
2136 __func__
, &type7
->data
->id
);
2140 if (type7
->area
->suppress_fa
== 1) {
2141 if (extold
->e
[0].fwd_addr
.s_addr
== 0)
2142 new->data
->ls_seqnum
= htonl(ls_seqnum
+ 1);
2145 if (!(new = ospf_lsa_install(ospf
, NULL
, new))) {
2146 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2147 "%s: Could not install translated LSA, Id %pI4",
2148 __func__
, &type7
->data
->id
);
2152 /* Flood LSA through area. */
2153 ospf_flood_through_as(ospf
, NULL
, new);
2158 /* Originate an AS-external-LSA, install and flood. */
2159 struct ospf_lsa
*ospf_external_lsa_originate(struct ospf
*ospf
,
2160 struct external_info
*ei
)
2162 struct ospf_lsa
*new;
2164 if (ospf
->gr_info
.restart_in_progress
) {
2165 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2167 "LSA[Type5]: Graceful Restart in progress, don't originate");
2171 /* Added for NSSA project....
2173 External LSAs are originated in ASBRs as usual, but for NSSA
2175 there is the global Type-5 LSDB and a Type-7 LSDB installed for
2176 every area. The Type-7's are flooded to every IR and every ABR; We
2177 install the Type-5 LSDB so that the normal "refresh" code operates
2178 as usual, and flag them as not used during ASE calculations. The
2179 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2180 Address of non-zero.
2182 If an ABR is the elected NSSA translator, following SPF and during
2183 the ABR task it will translate all the scanned Type-7's, with P-bit
2184 ON and not-self generated, and translate to Type-5's throughout the
2187 A difference in operation depends whether this ASBR is an ABR
2188 or not. If not an ABR, the P-bit is ON, to indicate that any
2189 elected NSSA-ABR can perform its translation.
2191 If an ABR, the P-bit is OFF; No ABR will perform translation and
2192 this ASBR will flood the Type-5 LSA as usual.
2194 For the case where this ASBR is not an ABR, the ASE calculations
2195 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2196 demonstrate to the user that there are LSA's that belong to any
2199 Finally, it just so happens that when the ABR is translating every
2200 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2201 approved Type-5 (translated from Type-7); at the end of translation
2202 if any Translated Type-5's remain unapproved, then they must be
2203 flushed from the AS.
2207 if (ospf
->router_id
.s_addr
== INADDR_ANY
) {
2208 if (IS_DEBUG_OSPF_EVENT
)
2210 "LSA[Type5:%pI4]: deferring AS-external-LSA origination, router ID is zero",
2215 /* Create new AS-external-LSA instance. */
2216 if ((new = ospf_external_lsa_new(ospf
, ei
, NULL
)) == NULL
) {
2217 if (IS_DEBUG_OSPF_EVENT
)
2219 "LSA[Type5:%pI4]: Could not originate AS-external-LSA",
2224 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2225 ospf_lsa_install(ospf
, NULL
, new);
2227 /* Update LSA origination count. */
2228 ospf
->lsa_originate_count
++;
2230 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2231 ospf_flood_through_as(ospf
, NULL
, new);
2233 /* If there is any attached NSSA, do special handling */
2234 if (ospf
->anyNSSA
&&
2235 /* stay away from translated LSAs! */
2236 !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2237 ospf_install_flood_nssa(
2238 ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2240 /* Debug logging. */
2241 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2242 zlog_debug("LSA[Type%d:%pI4]: Originate AS-external-LSA %p",
2243 new->data
->type
, &new->data
->id
,
2245 ospf_lsa_header_dump(new->data
);
2251 static struct external_info
*ospf_default_external_info(struct ospf
*ospf
)
2254 struct prefix_ipv4 p
;
2255 struct external_info
*default_ei
;
2259 p
.prefix
.s_addr
= 0;
2262 default_ei
= ospf_external_info_lookup(ospf
, DEFAULT_ROUTE
, 0, &p
);
2266 /* First, lookup redistributed default route. */
2267 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++) {
2268 struct list
*ext_list
;
2270 if (type
== ZEBRA_ROUTE_OSPF
)
2273 ext_list
= ospf
->external
[type
];
2277 ret
= ospf_external_default_routemap_apply_walk(ospf
, ext_list
,
2286 void ospf_external_lsa_rid_change(struct ospf
*ospf
)
2288 struct external_info
*ei
;
2289 struct ospf_external_aggr_rt
*aggr
;
2290 struct ospf_lsa
*lsa
= NULL
;
2294 for (type
= 0; type
< ZEBRA_ROUTE_MAX
; type
++) {
2295 struct route_node
*rn
;
2296 struct route_table
*rt
;
2297 struct list
*ext_list
;
2298 struct listnode
*node
;
2299 struct ospf_external
*ext
;
2301 ext_list
= ospf
->external
[type
];
2305 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
)) {
2306 /* Originate As-external-LSA from all type of
2307 * distribute source.
2309 rt
= ext
->external_info
;
2313 for (rn
= route_top(rt
); rn
; rn
= route_next(rn
)) {
2319 if (is_default_prefix4(&ei
->p
))
2322 lsa
= ospf_external_info_find_lsa(ospf
, &ei
->p
);
2324 aggr
= ospf_external_aggr_match(ospf
, &ei
->p
);
2327 if (!ospf_redistribute_check(ospf
, ei
,
2331 if (IS_DEBUG_OSPF(lsa
, EXTNL_LSA_AGGR
))
2333 "Originate Summary LSA after reset/router-ID change");
2335 /* Here the LSA is originated as new */
2336 ospf_originate_summary_lsa(ospf
, aggr
,
2339 /* LSA needs to be refreshed even if
2340 * there is no change in the route
2341 * params if the LSA is in maxage.
2343 if (IS_LSA_MAXAGE(lsa
))
2344 force
= LSA_REFRESH_FORCE
;
2346 force
= LSA_REFRESH_IF_CHANGED
;
2348 ospf_external_lsa_refresh(ospf
, lsa
,
2351 if (!ospf_redistribute_check(ospf
, ei
,
2355 if (!ospf_external_lsa_originate(ospf
,
2358 EC_OSPF_LSA_INSTALL_FAILURE
,
2359 "LSA: AS-external-LSA was not originated.");
2365 ei
= ospf_default_external_info(ospf
);
2366 if (ei
&& !ospf_external_lsa_originate(ospf
, ei
)) {
2367 flog_warn(EC_OSPF_LSA_INSTALL_FAILURE
,
2368 "LSA: AS-external-LSA for default route was not originated.");
2372 /* Flush any NSSA LSAs for given prefix */
2373 void ospf_nssa_lsa_flush(struct ospf
*ospf
, struct prefix_ipv4
*p
)
2375 struct listnode
*node
, *nnode
;
2376 struct ospf_lsa
*lsa
= NULL
;
2377 struct ospf_area
*area
;
2379 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
2380 if (area
->external_routing
== OSPF_AREA_NSSA
) {
2381 lsa
= ospf_lsa_lookup(ospf
, area
, OSPF_AS_NSSA_LSA
,
2382 p
->prefix
, ospf
->router_id
);
2384 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2386 "LSA: There is no such AS-NSSA-LSA %pFX in LSDB",
2390 ospf_ls_retransmit_delete_nbr_area(area
, lsa
);
2391 if (!IS_LSA_MAXAGE(lsa
)) {
2392 ospf_refresher_unregister_lsa(ospf
, lsa
);
2393 ospf_lsa_flush_area(lsa
, area
);
2399 /* Flush an AS-external-LSA from LSDB and routing domain. */
2400 void ospf_external_lsa_flush(struct ospf
*ospf
, uint8_t type
,
2401 struct prefix_ipv4
*p
,
2402 ifindex_t ifindex
/*, struct in_addr nexthop */)
2404 struct ospf_lsa
*lsa
;
2406 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2407 zlog_debug("LSA: Flushing AS-external-LSA %pFX", p
);
2409 /* First lookup LSA from LSDB. */
2410 if (!(lsa
= ospf_external_info_find_lsa(ospf
, p
))) {
2411 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2413 "LSA: There is no such AS-external-LSA %pFX in LSDB",
2418 /* If LSA is selforiginated, not a translated LSA, and there is
2419 * NSSA area, flush Type-7 LSA's at first.
2421 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2422 && !(CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2423 ospf_nssa_lsa_flush(ospf
, p
);
2425 if (!IS_LSA_MAXAGE(lsa
)) {
2426 /* Sweep LSA from Link State Retransmit List. */
2427 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2429 /* Unregister LSA from Refresh queue. */
2430 ospf_refresher_unregister_lsa(ospf
, lsa
);
2432 /* Flush AS-external-LSA through AS. */
2433 ospf_lsa_flush_as(ospf
, lsa
);
2436 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
2437 zlog_debug("%s: stop", __func__
);
2440 void ospf_external_lsa_refresh_default(struct ospf
*ospf
)
2442 struct prefix_ipv4 p
;
2443 struct external_info
*ei
;
2444 struct ospf_lsa
*lsa
;
2448 p
.prefix
.s_addr
= INADDR_ANY
;
2450 ei
= ospf_default_external_info(ospf
);
2451 lsa
= ospf_external_info_find_lsa(ospf
, &p
);
2454 if (IS_DEBUG_OSPF_EVENT
)
2455 zlog_debug("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2457 ospf_external_lsa_refresh(ospf
, lsa
, ei
, LSA_REFRESH_FORCE
,
2459 } else if (ei
&& !lsa
) {
2460 if (IS_DEBUG_OSPF_EVENT
)
2462 "LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2463 ospf_external_lsa_originate(ospf
, ei
);
2465 if (IS_DEBUG_OSPF_EVENT
)
2466 zlog_debug("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2467 ospf_external_lsa_flush(ospf
, DEFAULT_ROUTE
, &p
, 0);
2471 void ospf_external_lsa_refresh_type(struct ospf
*ospf
, uint8_t type
,
2472 unsigned short instance
, int force
)
2474 struct route_node
*rn
;
2475 struct external_info
*ei
;
2476 struct ospf_external
*ext
;
2478 if (type
== DEFAULT_ROUTE
)
2481 ext
= ospf_external_lookup(ospf
, type
, instance
);
2483 if (ext
&& EXTERNAL_INFO(ext
)) {
2484 /* Refresh each redistributed AS-external-LSAs. */
2485 for (rn
= route_top(EXTERNAL_INFO(ext
)); rn
;
2486 rn
= route_next(rn
)) {
2489 if (!is_default_prefix4(&ei
->p
)) {
2490 struct ospf_lsa
*lsa
;
2491 struct ospf_external_aggr_rt
*aggr
;
2493 aggr
= ospf_external_aggr_match(ospf
,
2495 lsa
= ospf_external_info_find_lsa(
2498 /* Check the AS-external-LSA
2499 * should be originated.
2501 if (!ospf_redistribute_check(
2504 ospf_unlink_ei_from_aggr(
2513 "%s: Send Aggreate LSA (%pFX)",
2517 ospf_originate_summary_lsa(
2522 if (IS_LSA_MAXAGE(lsa
))
2523 force
= LSA_REFRESH_FORCE
;
2525 ospf_external_lsa_refresh(
2526 ospf
, lsa
, ei
, force
,
2529 if (!ospf_redistribute_check(
2532 ospf_external_lsa_originate(
2541 /* Refresh AS-external-LSA. */
2542 struct ospf_lsa
*ospf_external_lsa_refresh(struct ospf
*ospf
,
2543 struct ospf_lsa
*lsa
,
2544 struct external_info
*ei
, int force
,
2547 struct ospf_lsa
*new;
2550 /* Check the AS-external-LSA should be originated. */
2552 if (!ospf_redistribute_check(ospf
, ei
, &changed
)) {
2553 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2555 "LSA[Type%d:%pI4] Could not be refreshed, redist check fail",
2559 ospf_external_lsa_flush(ospf
, ei
->type
, &ei
->p
,
2560 ei
->ifindex
/*, ei->nexthop */);
2564 if (!changed
&& !force
) {
2565 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2567 "LSA[Type%d:%pI4]: Not refreshed, not changed/forced",
2568 lsa
->data
->type
, &lsa
->data
->id
);
2572 /* Delete LSA from neighbor retransmit-list. */
2573 ospf_ls_retransmit_delete_nbr_as(ospf
, lsa
);
2575 /* Unregister AS-external-LSA from refresh-list. */
2576 ospf_refresher_unregister_lsa(ospf
, lsa
);
2578 new = ospf_external_lsa_new(ospf
, ei
, &lsa
->data
->id
);
2581 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
2582 zlog_debug("LSA[Type%d:%pI4]: Could not be refreshed",
2583 lsa
->data
->type
, &lsa
->data
->id
);
2587 new->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
2589 ospf_lsa_install(ospf
, NULL
, new); /* As type-5. */
2591 /* Flood LSA through AS. */
2592 ospf_flood_through_as(ospf
, NULL
, new);
2594 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2595 if (ospf
->anyNSSA
&& !(CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
)))
2596 ospf_install_flood_nssa(ospf
, new,
2597 ei
); /* Install/Flood per new rules */
2599 /* Register self-originated LSA to refresh queue.
2600 * Translated LSAs should not be registered, but refreshed upon
2601 * refresh of the Type-7
2603 if (!CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2604 ospf_refresher_register_lsa(ospf
, new);
2606 /* Debug logging. */
2607 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2608 zlog_debug("LSA[Type%d:%pI4]: AS-external-LSA refresh",
2609 new->data
->type
, &new->data
->id
);
2610 ospf_lsa_header_dump(new->data
);
2617 /* LSA installation functions. */
2619 /* Install router-LSA to an area. */
2620 static struct ospf_lsa
*
2621 ospf_router_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2623 struct ospf_area
*area
= new->area
;
2625 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2626 The entire routing table must be recalculated, starting with
2627 the shortest path calculations for each area (not just the
2628 area whose link-state database has changed).
2631 if (IS_LSA_SELF(new)) {
2633 /* Only install LSA if it is originated/refreshed by us.
2634 * If LSA was received by flooding, the RECEIVED flag is set so
2636 * not link the LSA */
2637 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2638 return new; /* ignore stale LSA */
2640 /* Set self-originated router-LSA. */
2641 ospf_lsa_unlock(&area
->router_lsa_self
);
2642 area
->router_lsa_self
= ospf_lsa_lock(new);
2644 ospf_refresher_register_lsa(ospf
, new);
2646 /* For BGP ORR SPF should be calculated from specified root(s) */
2647 else if (ospf
->orr_spf_request
) {
2648 ospf_lsa_unlock(&area
->router_lsa_rcvd
);
2649 area
->router_lsa_rcvd
= ospf_lsa_lock(new);
2650 ospf_orr_root_update_rcvd_lsa(area
->router_lsa_rcvd
);
2654 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2658 /* Install network-LSA to an area. */
2659 static struct ospf_lsa
*ospf_network_lsa_install(struct ospf
*ospf
,
2660 struct ospf_interface
*oi
,
2661 struct ospf_lsa
*new,
2664 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2665 The entire routing table must be recalculated, starting with
2666 the shortest path calculations for each area (not just the
2667 area whose link-state database has changed).
2669 if (IS_LSA_SELF(new)) {
2670 /* We supposed that when LSA is originated by us, we pass the
2672 for which it was originated. If LSA was received by flooding,
2673 the RECEIVED flag is set, so we do not link the LSA to the
2675 if (CHECK_FLAG(new->flags
, OSPF_LSA_RECEIVED
))
2676 return new; /* ignore stale LSA */
2678 ospf_lsa_unlock(&oi
->network_lsa_self
);
2679 oi
->network_lsa_self
= ospf_lsa_lock(new);
2680 ospf_refresher_register_lsa(ospf
, new);
2683 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2688 /* Install summary-LSA to an area. */
2689 static struct ospf_lsa
*
2690 ospf_summary_lsa_install(struct ospf
*ospf
, struct ospf_lsa
*new, int rt_recalc
)
2692 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2693 /* RFC 2328 Section 13.2 Summary-LSAs
2694 The best route to the destination described by the summary-
2695 LSA must be recalculated (see Section 16.5). If this
2696 destination is an AS boundary router, it may also be
2697 necessary to re-examine all the AS-external-LSAs.
2700 ospf_spf_calculate_schedule(ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2703 if (IS_LSA_SELF(new))
2704 ospf_refresher_register_lsa(ospf
, new);
2709 /* Install ASBR-summary-LSA to an area. */
2710 static struct ospf_lsa
*ospf_summary_asbr_lsa_install(struct ospf
*ospf
,
2711 struct ospf_lsa
*new,
2714 if (rt_recalc
&& !IS_LSA_SELF(new)) {
2715 /* RFC 2328 Section 13.2 Summary-LSAs
2716 The best route to the destination described by the summary-
2717 LSA must be recalculated (see Section 16.5). If this
2718 destination is an AS boundary router, it may also be
2719 necessary to re-examine all the AS-external-LSAs.
2721 ospf_spf_calculate_schedule(ospf
,
2722 SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2725 /* register LSA to refresh-list. */
2726 if (IS_LSA_SELF(new))
2727 ospf_refresher_register_lsa(ospf
, new);
2732 /* Install AS-external-LSA. */
2733 static struct ospf_lsa
*ospf_external_lsa_install(struct ospf
*ospf
,
2734 struct ospf_lsa
*new,
2737 ospf_ase_register_external_lsa(new, ospf
);
2738 /* If LSA is not self-originated, calculate an external route. */
2740 /* RFC 2328 Section 13.2 AS-external-LSAs
2741 The best route to the destination described by the AS-
2742 external-LSA must be recalculated (see Section 16.6).
2745 if (!IS_LSA_SELF(new))
2746 ospf_ase_incremental_update(ospf
, new);
2749 if (new->data
->type
== OSPF_AS_NSSA_LSA
) {
2750 /* There is no point to register selforiginate Type-7 LSA for
2751 * refreshing. We rely on refreshing Type-5 LSA's
2753 if (IS_LSA_SELF(new))
2756 /* Try refresh type-5 translated LSA for this LSA, if
2758 * New translations will be taken care of by the
2761 ospf_translated_nssa_refresh(ospf
, new, NULL
);
2762 ospf_schedule_abr_task(ospf
);
2766 /* Register self-originated LSA to refresh queue.
2767 * Leave Translated LSAs alone if NSSA is enabled
2769 if (IS_LSA_SELF(new) && !CHECK_FLAG(new->flags
, OSPF_LSA_LOCAL_XLT
))
2770 ospf_refresher_register_lsa(ospf
, new);
2775 void ospf_discard_from_db(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
2776 struct ospf_lsa
*lsa
)
2778 struct ospf_lsa
*old
;
2783 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2788 if (old
->refresh_list
>= 0)
2789 ospf_refresher_unregister_lsa(ospf
, old
);
2791 switch (old
->data
->type
) {
2792 case OSPF_AS_EXTERNAL_LSA
:
2793 ospf_ase_unregister_external_lsa(old
, ospf
);
2794 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2796 case OSPF_OPAQUE_AS_LSA
:
2797 ospf_ls_retransmit_delete_nbr_as(ospf
, old
);
2799 case OSPF_AS_NSSA_LSA
:
2800 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2801 ospf_ase_unregister_external_lsa(old
, ospf
);
2804 ospf_ls_retransmit_delete_nbr_area(old
->area
, old
);
2808 ospf_lsa_maxage_delete(ospf
, old
);
2809 ospf_lsa_discard(old
);
2812 struct ospf_lsa
*ospf_lsa_install(struct ospf
*ospf
, struct ospf_interface
*oi
,
2813 struct ospf_lsa
*lsa
)
2815 struct ospf_lsa
*new = NULL
;
2816 struct ospf_lsa
*old
= NULL
;
2817 struct ospf_lsdb
*lsdb
= NULL
;
2821 switch (lsa
->data
->type
) {
2823 case OSPF_AS_NSSA_LSA
:
2825 lsdb
= lsa
->area
->lsdb
;
2829 case OSPF_AS_EXTERNAL_LSA
:
2830 case OSPF_OPAQUE_AS_LSA
:
2835 lsdb
= lsa
->area
->lsdb
;
2841 /* RFC 2328 13.2. Installing LSAs in the database
2843 Installing a new LSA in the database, either as the result of
2844 flooding or a newly self-originated LSA, may cause the OSPF
2845 routing table structure to be recalculated. The contents of the
2846 new LSA should be compared to the old instance, if present. If
2847 there is no difference, there is no need to recalculate the
2848 routing table. When comparing an LSA to its previous instance,
2849 the following are all considered to be differences in contents:
2851 o The LSA's Options field has changed.
2853 o One of the LSA instances has LS age set to MaxAge, and
2856 o The length field in the LSA header has changed.
2858 o The body of the LSA (i.e., anything outside the 20-byte
2859 LSA header) has changed. Note that this excludes changes
2860 in LS Sequence Number and LS Checksum.
2863 /* Look up old LSA and determine if any SPF calculation or incremental
2865 old
= ospf_lsdb_lookup(lsdb
, lsa
);
2867 /* Do comparison and record if recalc needed. */
2869 if (old
== NULL
|| ospf_lsa_different(old
, lsa
, false)) {
2870 /* Ref rfc3623 section 3.2.3
2871 * Installing new lsa or change in the existing LSA
2872 * or flushing existing LSA leads to topo change
2873 * and trigger SPF caculation.
2874 * So, router should be aborted from HELPER role
2875 * if it is detected as TOPO change.
2877 if (ospf
->active_restarter_cnt
&&
2878 CHECK_LSA_TYPE_1_TO_5_OR_7(lsa
->data
->type
)) {
2879 if (old
== NULL
|| ospf_lsa_different(old
, lsa
, true))
2880 ospf_helper_handle_topo_chg(ospf
, lsa
);
2887 Sequence number check (Section 14.1 of rfc 2328)
2888 "Premature aging is used when it is time for a self-originated
2889 LSA's sequence number field to wrap. At this point, the current
2890 LSA instance (having LS sequence number MaxSequenceNumber) must
2891 be prematurely aged and flushed from the routing domain before a
2892 new instance with sequence number equal to InitialSequenceNumber
2893 can be originated. "
2896 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
) {
2897 if (ospf_lsa_is_self_originated(ospf
, lsa
)) {
2898 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2900 if (!IS_LSA_MAXAGE(lsa
))
2901 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2902 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
2904 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
)) {
2906 "%s() Premature Aging lsa %p, seqnum 0x%x",
2908 ntohl(lsa
->data
->ls_seqnum
));
2909 ospf_lsa_header_dump(lsa
->data
);
2912 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
)) {
2914 "%s() got an lsa with seq 0x80000000 that was not self originated. Ignoring",
2916 ospf_lsa_header_dump(lsa
->data
);
2922 /* discard old LSA from LSDB */
2924 ospf_discard_from_db(ospf
, lsdb
, lsa
);
2926 /* Calculate Checksum if self-originated?. */
2927 if (IS_LSA_SELF(lsa
))
2928 ospf_lsa_checksum(lsa
->data
);
2930 /* Insert LSA to LSDB. */
2931 ospf_lsdb_add(lsdb
, lsa
);
2934 /* Do LSA specific installation process. */
2935 switch (lsa
->data
->type
) {
2936 case OSPF_ROUTER_LSA
:
2937 new = ospf_router_lsa_install(ospf
, lsa
, rt_recalc
);
2939 case OSPF_NETWORK_LSA
:
2941 new = ospf_network_lsa_install(ospf
, oi
, lsa
, rt_recalc
);
2943 case OSPF_SUMMARY_LSA
:
2944 new = ospf_summary_lsa_install(ospf
, lsa
, rt_recalc
);
2946 case OSPF_ASBR_SUMMARY_LSA
:
2947 new = ospf_summary_asbr_lsa_install(ospf
, lsa
, rt_recalc
);
2949 case OSPF_AS_EXTERNAL_LSA
:
2950 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2952 case OSPF_OPAQUE_LINK_LSA
:
2953 if (IS_LSA_SELF(lsa
))
2954 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for
2957 /* Incoming "oi" for this LSA has set at LSUpd
2961 case OSPF_OPAQUE_AREA_LSA
:
2962 case OSPF_OPAQUE_AS_LSA
:
2963 new = ospf_opaque_lsa_install(lsa
, rt_recalc
);
2965 case OSPF_AS_NSSA_LSA
:
2966 new = ospf_external_lsa_install(ospf
, lsa
, rt_recalc
);
2967 default: /* type-6,8,9....nothing special */
2972 return new; /* Installation failed, cannot proceed further --
2976 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
)) {
2977 switch (lsa
->data
->type
) {
2978 case OSPF_AS_EXTERNAL_LSA
:
2979 case OSPF_OPAQUE_AS_LSA
:
2980 case OSPF_AS_NSSA_LSA
:
2981 zlog_debug("LSA[%s]: Install %s", dump_lsa_key(new),
2982 lookup_msg(ospf_lsa_type_msg
,
2983 new->data
->type
, NULL
));
2986 zlog_debug("LSA[%s]: Install %s to Area %pI4",
2988 lookup_msg(ospf_lsa_type_msg
,
2989 new->data
->type
, NULL
),
2990 &new->area
->area_id
);
2996 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2997 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2999 if (IS_LSA_MAXAGE(new)) {
3000 if (IS_DEBUG_OSPF(lsa
, LSA_INSTALL
))
3001 zlog_debug("LSA[%s]: Install LSA %p, MaxAge",
3002 dump_lsa_key(new), lsa
);
3003 ospf_lsa_maxage(ospf
, lsa
);
3010 int ospf_check_nbr_status(struct ospf
*ospf
)
3012 struct listnode
*node
, *nnode
;
3013 struct ospf_interface
*oi
;
3015 for (ALL_LIST_ELEMENTS(ospf
->oiflist
, node
, nnode
, oi
)) {
3016 struct route_node
*rn
;
3017 struct ospf_neighbor
*nbr
;
3019 if (ospf_if_is_enable(oi
))
3020 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
3021 if ((nbr
= rn
->info
) != NULL
)
3022 if (nbr
->state
== NSM_Exchange
3023 || nbr
->state
== NSM_Loading
) {
3024 route_unlock_node(rn
);
3033 void ospf_maxage_lsa_remover(struct thread
*thread
)
3035 struct ospf
*ospf
= THREAD_ARG(thread
);
3036 struct ospf_lsa
*lsa
, *old
;
3037 struct route_node
*rn
;
3040 ospf
->t_maxage
= NULL
;
3042 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3043 zlog_debug("LSA[MaxAge]: remover Start");
3045 reschedule
= !ospf_check_nbr_status(ospf
);
3048 for (rn
= route_top(ospf
->maxage_lsa
); rn
;
3049 rn
= route_next(rn
)) {
3050 if ((lsa
= rn
->info
) == NULL
) {
3054 /* There is at least one neighbor from which we still
3056 * for that LSA, so we are not allowed to remove it from
3058 * as per RFC 2328 section 14 para 4 a) */
3059 if (lsa
->retransmit_counter
> 0) {
3064 /* TODO: maybe convert this function to a work-queue */
3065 if (thread_should_yield(thread
)) {
3066 OSPF_TIMER_ON(ospf
->t_maxage
,
3067 ospf_maxage_lsa_remover
, 0);
3069 rn
); /* route_top/route_next */
3073 /* Remove LSA from the LSDB */
3074 if (IS_LSA_SELF(lsa
))
3075 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3077 "LSA[Type%d:%pI4]: LSA 0x%lx is self-originated: ",
3080 (unsigned long)lsa
);
3082 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3084 "LSA[%s]: MaxAge LSA removed from list",
3087 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
)) {
3088 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3090 "originating new lsa for lsa %p",
3092 ospf_lsa_refresh(ospf
, lsa
);
3095 /* Remove from lsdb. */
3097 old
= ospf_lsdb_lookup(lsa
->lsdb
, lsa
);
3098 /* The max age LSA here must be the same
3099 * as the LSA in LSDB
3102 flog_err(EC_OSPF_LSA_MISSING
,
3103 "%s: LSA[%s]: LSA not in LSDB",
3104 __func__
, dump_lsa_key(lsa
));
3108 ospf_discard_from_db(ospf
, lsa
->lsdb
, lsa
);
3109 ospf_lsdb_delete(lsa
->lsdb
, lsa
);
3111 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3113 "%s: LSA[%s]: No associated LSDB!",
3114 __func__
, dump_lsa_key(lsa
));
3118 /* A MaxAge LSA must be removed immediately from the router's link
3119 state database as soon as both a) it is no longer contained on any
3120 neighbor Link state retransmission lists and b) none of the
3122 neighbors are in states Exchange or Loading. */
3124 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
3125 ospf
->maxage_delay
);
3128 /* This function checks whether an LSA with initial sequence number should be
3129 * originated after a wrap in sequence number
3131 void ospf_check_and_gen_init_seq_lsa(struct ospf_interface
*oi
,
3132 struct ospf_lsa
*recv_lsa
)
3134 struct ospf_lsa
*lsa
= NULL
;
3135 struct ospf
*ospf
= oi
->ospf
;
3137 lsa
= ospf_lsa_lookup_by_header(oi
->area
, recv_lsa
->data
);
3139 if ((lsa
== NULL
) || (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_PREMATURE_AGE
))
3140 || (lsa
->retransmit_counter
!= 0)) {
3141 if (IS_DEBUG_OSPF(lsa
, LSA
))
3143 "Do not generate LSA with initial seqence number.");
3147 ospf_lsa_maxage_delete(ospf
, lsa
);
3149 lsa
->data
->ls_seqnum
= lsa_seqnum_increment(lsa
);
3151 ospf_lsa_refresh(ospf
, lsa
);
3154 void ospf_lsa_maxage_delete(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3156 struct route_node
*rn
;
3157 struct prefix lsa_prefix
;
3159 memset(&lsa_prefix
, 0, sizeof(lsa_prefix
));
3160 lsa_prefix
.family
= AF_UNSPEC
;
3161 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
3162 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
3164 if ((rn
= route_node_lookup(ospf
->maxage_lsa
, &lsa_prefix
))) {
3165 if (rn
->info
== lsa
) {
3166 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
3167 ospf_lsa_unlock(&lsa
); /* maxage_lsa */
3170 rn
); /* unlock node because lsa is deleted */
3172 route_unlock_node(rn
); /* route_node_lookup */
3174 if (IS_DEBUG_OSPF_EVENT
)
3175 zlog_debug("%s: lsa %s is not found in maxage db.",
3176 __func__
, dump_lsa_key(lsa
));
3180 /* Add LSA onto the MaxAge list, and schedule for removal.
3181 * This does *not* lead to the LSA being flooded, that must be taken
3182 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
3185 void ospf_lsa_maxage(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3187 struct prefix lsa_prefix
;
3188 struct route_node
*rn
;
3190 /* When we saw a MaxAge LSA flooded to us, we put it on the list
3191 and schedule the MaxAge LSA remover. */
3192 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
)) {
3193 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3195 "LSA[%s]: %p already exists on MaxAge LSA list",
3196 dump_lsa_key(lsa
), lsa
);
3200 memset(&lsa_prefix
, 0, sizeof(lsa_prefix
));
3201 lsa_prefix
.family
= AF_UNSPEC
;
3202 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.u
.ptr
) * CHAR_BIT
;
3203 lsa_prefix
.u
.ptr
= (uintptr_t)lsa
;
3205 rn
= route_node_get(ospf
->maxage_lsa
, &lsa_prefix
);
3206 if (rn
->info
!= NULL
) {
3207 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3209 "LSA[%s]: found LSA (%p) in table for LSA %p %d",
3210 dump_lsa_key(lsa
), rn
->info
,
3211 (void *)lsa
, lsa_prefix
.prefixlen
);
3212 route_unlock_node(rn
);
3214 rn
->info
= ospf_lsa_lock(lsa
);
3215 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
3218 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3219 zlog_debug("LSA[%s]: MaxAge LSA remover scheduled.",
3222 OSPF_TIMER_ON(ospf
->t_maxage
, ospf_maxage_lsa_remover
,
3223 ospf
->maxage_delay
);
3226 static int ospf_lsa_maxage_walker_remover(struct ospf
*ospf
,
3227 struct ospf_lsa
*lsa
)
3229 /* Stay away from any Local Translated Type-7 LSAs */
3230 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3233 if (IS_LSA_MAXAGE(lsa
))
3234 /* Self-originated LSAs should NOT time-out instead,
3235 they're flushed and submitted to the max_age list explicitly.
3237 if (!ospf_lsa_is_self_originated(ospf
, lsa
)) {
3238 if (IS_DEBUG_OSPF(lsa
, LSA_FLOODING
))
3239 zlog_debug("LSA[%s]: is MaxAge",
3242 switch (lsa
->data
->type
) {
3243 case OSPF_OPAQUE_LINK_LSA
:
3244 case OSPF_OPAQUE_AREA_LSA
:
3245 case OSPF_OPAQUE_AS_LSA
:
3247 * As a general rule, whenever network topology
3249 * (due to an LSA removal in this case), routing
3251 * should be triggered. However, this is not
3253 * LSAs. Even if an opaque LSA instance is going
3255 * from the routing domain, it does not mean a
3257 * topology, and thus, routing recalculation is
3261 case OSPF_AS_EXTERNAL_LSA
:
3262 case OSPF_AS_NSSA_LSA
:
3263 ospf_ase_incremental_update(ospf
, lsa
);
3266 ospf_spf_calculate_schedule(ospf
,
3270 ospf_lsa_maxage(ospf
, lsa
);
3273 if (IS_LSA_MAXAGE(lsa
) && !ospf_lsa_is_self_originated(ospf
, lsa
))
3274 if (LS_AGE(lsa
) > OSPF_LSA_MAXAGE
+ 30)
3275 printf("Eek! Shouldn't happen!\n");
3280 /* Periodical check of MaxAge LSA. */
3281 void ospf_lsa_maxage_walker(struct thread
*thread
)
3283 struct ospf
*ospf
= THREAD_ARG(thread
);
3284 struct route_node
*rn
;
3285 struct ospf_lsa
*lsa
;
3286 struct ospf_area
*area
;
3287 struct listnode
*node
, *nnode
;
3289 ospf
->t_maxage_walker
= NULL
;
3291 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3292 LSDB_LOOP (ROUTER_LSDB(area
), rn
, lsa
)
3293 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3294 LSDB_LOOP (NETWORK_LSDB(area
), rn
, lsa
)
3295 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3296 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3297 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3298 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3299 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3300 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3301 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3302 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3303 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3304 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
3305 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3308 /* for AS-external-LSAs. */
3310 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3311 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3312 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3313 ospf_lsa_maxage_walker_remover(ospf
, lsa
);
3316 OSPF_TIMER_ON(ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3317 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3320 struct ospf_lsa
*ospf_lsa_lookup_by_prefix(struct ospf_lsdb
*lsdb
, uint8_t type
,
3321 struct prefix_ipv4
*p
,
3322 struct in_addr router_id
)
3324 struct ospf_lsa
*lsa
;
3325 struct in_addr mask
, id
;
3326 struct lsa_header_mask
{
3327 struct lsa_header header
;
3328 struct in_addr mask
;
3331 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, p
->prefix
, router_id
);
3335 masklen2ip(p
->prefixlen
, &mask
);
3337 hmask
= (struct lsa_header_mask
*)lsa
->data
;
3339 if (mask
.s_addr
!= hmask
->mask
.s_addr
) {
3340 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3341 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, id
, router_id
);
3349 struct ospf_lsa
*ospf_lsa_lookup(struct ospf
*ospf
, struct ospf_area
*area
,
3350 uint32_t type
, struct in_addr id
,
3351 struct in_addr adv_router
)
3357 case OSPF_ROUTER_LSA
:
3358 case OSPF_NETWORK_LSA
:
3359 case OSPF_SUMMARY_LSA
:
3360 case OSPF_ASBR_SUMMARY_LSA
:
3361 case OSPF_AS_NSSA_LSA
:
3362 case OSPF_OPAQUE_LINK_LSA
:
3363 case OSPF_OPAQUE_AREA_LSA
:
3364 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, adv_router
);
3365 case OSPF_AS_EXTERNAL_LSA
:
3366 case OSPF_OPAQUE_AS_LSA
:
3367 return ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, id
, adv_router
);
3375 struct ospf_lsa
*ospf_lsa_lookup_by_id(struct ospf_area
*area
, uint32_t type
,
3378 struct ospf_lsa
*lsa
;
3379 struct route_node
*rn
;
3382 case OSPF_ROUTER_LSA
:
3383 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3384 case OSPF_NETWORK_LSA
:
3385 for (rn
= route_top(NETWORK_LSDB(area
)); rn
;
3386 rn
= route_next(rn
))
3387 if ((lsa
= rn
->info
))
3388 if (IPV4_ADDR_SAME(&lsa
->data
->id
, &id
)) {
3389 route_unlock_node(rn
);
3393 case OSPF_SUMMARY_LSA
:
3394 case OSPF_ASBR_SUMMARY_LSA
:
3395 /* Currently not used. */
3397 return ospf_lsdb_lookup_by_id(area
->lsdb
, type
, id
, id
);
3398 case OSPF_AS_EXTERNAL_LSA
:
3399 case OSPF_AS_NSSA_LSA
:
3400 case OSPF_OPAQUE_LINK_LSA
:
3401 case OSPF_OPAQUE_AREA_LSA
:
3402 case OSPF_OPAQUE_AS_LSA
:
3403 /* Currently not used. */
3412 struct ospf_lsa
*ospf_lsa_lookup_by_adv_rid(struct ospf_area
*area
,
3413 uint32_t type
, struct in_addr id
)
3415 struct ospf_lsa
*lsa
= NULL
;
3416 struct route_node
*rn
= NULL
;
3419 case OSPF_ROUTER_LSA
:
3420 for (rn
= route_top(ROUTER_LSDB(area
)); rn
;
3421 rn
= route_next(rn
)) {
3424 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
,
3426 route_unlock_node(rn
);
3432 case OSPF_NETWORK_LSA
:
3433 case OSPF_SUMMARY_LSA
:
3434 case OSPF_ASBR_SUMMARY_LSA
:
3435 case OSPF_AS_EXTERNAL_LSA
:
3436 case OSPF_AS_NSSA_LSA
:
3437 case OSPF_OPAQUE_LINK_LSA
:
3438 case OSPF_OPAQUE_AREA_LSA
:
3439 case OSPF_OPAQUE_AS_LSA
:
3440 /* Currently not used. */
3449 struct ospf_lsa
*ospf_lsa_lookup_by_mpls_te_rid(struct ospf_area
*area
,
3453 struct ospf_lsa
*lsa
= NULL
;
3454 struct route_node
*rn
= NULL
;
3455 struct lsa_header
*lsah
= NULL
;
3457 uint8_t opaque_type
;
3458 struct tlv_header
*tlvh
= NULL
;
3459 struct te_tlv_router_addr
*router_addr
= NULL
;
3461 if (type
!= OSPF_OPAQUE_AREA_LSA
)
3464 for (rn
= route_top(OPAQUE_AREA_LSDB(area
)); rn
; rn
= route_next(rn
)) {
3468 lsid
= ntohl(lsah
->id
.s_addr
);
3469 opaque_type
= GET_OPAQUE_TYPE(lsid
);
3470 if (opaque_type
!= OPAQUE_TYPE_TRAFFIC_ENGINEERING_LSA
)
3473 tlvh
= TLV_HDR_TOP(lsah
);
3475 (ntohs(tlvh
->type
) != TE_TLV_ROUTER_ADDR
) ||
3476 (ntohs(tlvh
->length
) != TE_LINK_SUBTLV_DEF_SIZE
))
3478 router_addr
= (struct te_tlv_router_addr
*)tlvh
;
3479 if (IPV4_ADDR_SAME(&router_addr
->value
, &id
)) {
3480 route_unlock_node(rn
);
3488 struct ospf_lsa
*ospf_lsa_lookup_by_header(struct ospf_area
*area
,
3489 struct lsa_header
*lsah
)
3491 struct ospf_lsa
*match
;
3494 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3495 * is redefined to have two subfields; opaque-type and opaque-id.
3496 * However, it is harmless to treat the two sub fields together, as if
3497 * they two were forming a unique LSA-ID.
3500 match
= ospf_lsa_lookup(area
->ospf
, area
, lsah
->type
, lsah
->id
,
3504 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3505 zlog_debug("LSA[Type%d:%pI4]: Lookup by header, NO MATCH",
3506 lsah
->type
, &lsah
->id
);
3511 /* return +n, l1 is more recent.
3512 return -n, l2 is more recent.
3513 return 0, l1 and l2 is identical. */
3514 int ospf_lsa_more_recent(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3519 if (l1
== NULL
&& l2
== NULL
)
3526 /* compare LS sequence number. */
3527 x
= (int)ntohl(l1
->data
->ls_seqnum
);
3528 y
= (int)ntohl(l2
->data
->ls_seqnum
);
3534 /* compare LS checksum. */
3535 r
= ntohs(l1
->data
->checksum
) - ntohs(l2
->data
->checksum
);
3539 /* compare LS age. */
3540 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3542 else if (!IS_LSA_MAXAGE(l1
) && IS_LSA_MAXAGE(l2
))
3545 /* compare LS age with MaxAgeDiff. */
3546 if (LS_AGE(l1
) - LS_AGE(l2
) > OSPF_LSA_MAXAGE_DIFF
)
3548 else if (LS_AGE(l2
) - LS_AGE(l1
) > OSPF_LSA_MAXAGE_DIFF
)
3551 /* LSAs are identical. */
3556 * Check if two LSAs are different.
3559 * The first LSA to compare.
3562 * The second LSA to compare.
3565 * When set to true, ignore whether the LSAs were received from the network
3566 * or not. This parameter should be set to true when checking for topology
3567 * changes as part of the Graceful Restart helper neighbor procedures.
3570 * true if the LSAs are different, false otherwise.
3572 int ospf_lsa_different(struct ospf_lsa
*l1
, struct ospf_lsa
*l2
,
3573 bool ignore_rcvd_flag
)
3581 if (l1
->data
->options
!= l2
->data
->options
)
3584 if (IS_LSA_MAXAGE(l1
) && !IS_LSA_MAXAGE(l2
))
3587 if (IS_LSA_MAXAGE(l2
) && !IS_LSA_MAXAGE(l1
))
3590 if (l1
->size
!= l2
->size
)
3596 if (!ignore_rcvd_flag
3597 && CHECK_FLAG((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3598 return 1; /* May be a stale LSA in the LSBD */
3600 assert(l1
->size
> OSPF_LSA_HEADER_SIZE
);
3602 p1
= (char *)l1
->data
;
3603 p2
= (char *)l2
->data
;
3605 if (memcmp(p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3606 l1
->size
- OSPF_LSA_HEADER_SIZE
)
3613 int ospf_lsa_flush_schedule(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3615 if (lsa
== NULL
|| !IS_LSA_SELF(lsa
))
3618 if (IS_DEBUG_OSPF_EVENT
)
3620 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3621 lsa
->data
->type
, &lsa
->data
->id
);
3623 /* Force given lsa's age to MaxAge. */
3624 lsa
->data
->ls_age
= htons(OSPF_LSA_MAXAGE
);
3626 switch (lsa
->data
->type
) {
3627 /* Opaque wants to be notified of flushes */
3628 case OSPF_OPAQUE_LINK_LSA
:
3629 case OSPF_OPAQUE_AREA_LSA
:
3630 case OSPF_OPAQUE_AS_LSA
:
3631 ospf_opaque_lsa_refresh(lsa
);
3634 ospf_refresher_unregister_lsa(ospf
, lsa
);
3635 ospf_lsa_flush(ospf
, lsa
);
3642 void ospf_flush_self_originated_lsas_now(struct ospf
*ospf
)
3644 struct listnode
*node
, *nnode
;
3645 struct listnode
*node2
, *nnode2
;
3646 struct ospf_area
*area
;
3647 struct ospf_interface
*oi
;
3648 struct ospf_lsa
*lsa
;
3649 struct route_node
*rn
;
3650 int need_to_flush_ase
= 0;
3652 ospf
->inst_shutdown
= 1;
3654 for (ALL_LIST_ELEMENTS(ospf
->areas
, node
, nnode
, area
)) {
3655 if ((lsa
= area
->router_lsa_self
) != NULL
) {
3656 if (IS_DEBUG_OSPF_EVENT
)
3658 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3662 ospf_refresher_unregister_lsa(ospf
, lsa
);
3663 ospf_lsa_flush_area(lsa
, area
);
3664 ospf_lsa_unlock(&area
->router_lsa_self
);
3665 area
->router_lsa_self
= NULL
;
3668 for (ALL_LIST_ELEMENTS(area
->oiflist
, node2
, nnode2
, oi
)) {
3669 if ((lsa
= oi
->network_lsa_self
) != NULL
3670 && oi
->state
== ISM_DR
&& oi
->full_nbrs
> 0) {
3671 if (IS_DEBUG_OSPF_EVENT
)
3673 "LSA[Type%d:%pI4]: Schedule self-originated LSA to FLUSH",
3677 ospf_refresher_unregister_lsa(
3678 ospf
, oi
->network_lsa_self
);
3679 ospf_lsa_flush_area(oi
->network_lsa_self
, area
);
3680 ospf_lsa_unlock(&oi
->network_lsa_self
);
3681 oi
->network_lsa_self
= NULL
;
3684 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3685 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3686 need_to_flush_ase
= 1;
3689 LSDB_LOOP (SUMMARY_LSDB(area
), rn
, lsa
)
3690 ospf_lsa_flush_schedule(ospf
, lsa
);
3691 LSDB_LOOP (ASBR_SUMMARY_LSDB(area
), rn
, lsa
)
3692 ospf_lsa_flush_schedule(ospf
, lsa
);
3693 LSDB_LOOP (OPAQUE_LINK_LSDB(area
), rn
, lsa
)
3694 ospf_lsa_flush_schedule(ospf
, lsa
);
3695 LSDB_LOOP (OPAQUE_AREA_LSDB(area
), rn
, lsa
)
3696 ospf_lsa_flush_schedule(ospf
, lsa
);
3699 if (need_to_flush_ase
) {
3700 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
3701 ospf_lsa_flush_schedule(ospf
, lsa
);
3702 LSDB_LOOP (OPAQUE_AS_LSDB(ospf
), rn
, lsa
)
3703 ospf_lsa_flush_schedule(ospf
, lsa
);
3707 * Make sure that the MaxAge LSA remover is executed immediately,
3708 * without conflicting to other threads.
3710 if (ospf
->t_maxage
!= NULL
) {
3711 THREAD_OFF(ospf
->t_maxage
);
3712 thread_execute(master
, ospf_maxage_lsa_remover
, ospf
, 0);
3718 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3719 /* An interface-independent version of ospf_lsa_is_self_originated */
3720 int ospf_lsa_is_self_originated(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3722 struct listnode
*node
;
3723 struct ospf_interface
*oi
;
3725 /* This LSA is already checked. */
3726 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3727 return IS_LSA_SELF(lsa
);
3729 /* Make sure LSA is self-checked. */
3730 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3732 /* AdvRouter and Router ID is the same. */
3733 if (IPV4_ADDR_SAME(&lsa
->data
->adv_router
, &ospf
->router_id
))
3734 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3736 /* LSA is router-LSA. */
3737 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
3738 && IPV4_ADDR_SAME(&lsa
->data
->id
, &ospf
->router_id
))
3739 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
3741 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3742 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3743 for (ALL_LIST_ELEMENTS_RO(ospf
->oiflist
, node
, oi
)) {
3744 /* Ignore virtual link. */
3745 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3746 if (oi
->address
->family
== AF_INET
)
3749 &oi
->address
->u
.prefix4
)) {
3750 /* to make it easier later */
3751 SET_FLAG(lsa
->flags
,
3753 return IS_LSA_SELF(lsa
);
3757 return IS_LSA_SELF(lsa
);
3760 /* Get unique Link State ID. */
3761 enum lsid_status
ospf_lsa_unique_id(struct ospf
*ospf
, struct ospf_lsdb
*lsdb
,
3762 uint8_t type
, struct prefix_ipv4
*p
,
3765 struct ospf_lsa
*lsa
;
3766 struct in_addr mask
;
3770 /* Check existence of LSA instance. */
3771 lsa
= ospf_lsdb_lookup_by_id(lsdb
, type
, *id
, ospf
->router_id
);
3773 struct as_external_lsa
*al
=
3774 (struct as_external_lsa
*)lsa
->data
;
3775 /* Ref rfc2328,Appendex E.1
3776 * If router already originated the external lsa with lsid
3777 * as the current prefix, and the masklens are same then
3778 * terminate the LSID algorithem.
3780 if (ip_masklen(al
->mask
) == p
->prefixlen
) {
3781 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3783 "%s: Can't get Link State ID for %pFX",
3785 /* id.s_addr = 0; */
3786 id
->s_addr
= 0xffffffff;
3787 return LSID_NOT_AVAILABLE
;
3788 } else if (ip_masklen(al
->mask
) < p
->prefixlen
) {
3789 /* Ref rfc2328,Appendex E.2
3790 * the current prefix masklen is greater than the
3791 * existing LSA, then generate the Link state ID,
3792 * by setting all host bits in prefix addressa and
3795 * Eg: 1st Route : 10.0.0.0/16 - LSID:10.0.0.0
3796 * 2nd Route : 10.0.0.0/24 - LSID:10.0.0.255
3798 masklen2ip(p
->prefixlen
, &mask
);
3800 id
->s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3801 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, *id
,
3804 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3806 "%s: Can't get Link State ID for %pFX",
3808 id
->s_addr
= 0xffffffff;
3809 return LSID_NOT_AVAILABLE
;
3812 /* Ref rfc2328,Appendex E.3
3813 * the current prefix masklen is lesser than the
3814 * existing LSA,then the originated LSA has to be
3815 * refreshed by modifying masklen, cost and tag.
3816 * Originate the old route info with new LSID by
3817 * setting the host bits in prefix address.
3819 * Eg: 1st Route : 10.0.0.0/24 - LSID:10.0.0.0
3820 * 2nd Route : 10.0.0.0/16 - ?
3821 * Since 2nd route mask len is less than firstone
3822 * LSID has to be changed.
3823 * 1st route LSID:10.0.0.255
3824 * 2nd route LSID:10.0.0.0
3826 id
->s_addr
= lsa
->data
->id
.s_addr
| (~al
->mask
.s_addr
);
3827 lsa
= ospf_lsdb_lookup_by_id(ospf
->lsdb
, type
, *id
,
3829 if (lsa
&& (ip_masklen(al
->mask
) != IPV4_MAX_BITLEN
)) {
3830 if (IS_DEBUG_OSPF(lsa
, LSA_GENERATE
))
3832 "%s: Can't get Link State ID for %pFX",
3834 id
->s_addr
= 0xffffffff;
3835 return LSID_NOT_AVAILABLE
;
3841 return LSID_AVAILABLE
;
3845 #define LSA_ACTION_FLOOD_AREA 1
3846 #define LSA_ACTION_FLUSH_AREA 2
3850 struct ospf_area
*area
;
3851 struct ospf_lsa
*lsa
;
3854 static void ospf_lsa_action(struct thread
*t
)
3856 struct lsa_action
*data
;
3858 data
= THREAD_ARG(t
);
3860 if (IS_DEBUG_OSPF(lsa
, LSA
) == OSPF_DEBUG_LSA
)
3861 zlog_debug("LSA[Action]: Performing scheduled LSA action: %d",
3864 switch (data
->action
) {
3865 case LSA_ACTION_FLOOD_AREA
:
3866 ospf_flood_through_area(data
->area
, NULL
, data
->lsa
);
3868 case LSA_ACTION_FLUSH_AREA
:
3869 ospf_lsa_flush_area(data
->lsa
, data
->area
);
3873 ospf_lsa_unlock(&data
->lsa
); /* Message */
3874 XFREE(MTYPE_OSPF_MESSAGE
, data
);
3877 void ospf_schedule_lsa_flood_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3879 struct lsa_action
*data
;
3881 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3882 data
->action
= LSA_ACTION_FLOOD_AREA
;
3884 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flood area */
3886 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3889 void ospf_schedule_lsa_flush_area(struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3891 struct lsa_action
*data
;
3893 data
= XCALLOC(MTYPE_OSPF_MESSAGE
, sizeof(struct lsa_action
));
3894 data
->action
= LSA_ACTION_FLUSH_AREA
;
3896 data
->lsa
= ospf_lsa_lock(lsa
); /* Message / Flush area */
3898 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3902 /* LSA Refreshment functions. */
3903 struct ospf_lsa
*ospf_lsa_refresh(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3905 struct external_info
*ei
;
3906 struct ospf_external_aggr_rt
*aggr
;
3907 struct ospf_lsa
*new = NULL
;
3908 struct as_external_lsa
*al
;
3909 struct prefix_ipv4 p
;
3911 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_SELF
) && !IS_LSA_SELF(lsa
) &&
3914 assert(lsa
->lock
> 0);
3916 switch (lsa
->data
->type
) {
3917 /* Router and Network LSAs are processed differently. */
3918 case OSPF_ROUTER_LSA
:
3919 new = ospf_router_lsa_refresh(lsa
);
3921 case OSPF_NETWORK_LSA
:
3922 new = ospf_network_lsa_refresh(lsa
);
3924 case OSPF_SUMMARY_LSA
:
3925 new = ospf_summary_lsa_refresh(ospf
, lsa
);
3927 case OSPF_ASBR_SUMMARY_LSA
:
3928 new = ospf_summary_asbr_lsa_refresh(ospf
, lsa
);
3930 case OSPF_AS_EXTERNAL_LSA
:
3931 /* Translated from NSSA Type-5s are refreshed when
3932 * from refresh of Type-7 - do not refresh these directly.
3935 al
= (struct as_external_lsa
*)lsa
->data
;
3937 p
.prefixlen
= ip_masklen(al
->mask
);
3938 p
.prefix
= lsa
->data
->id
;
3940 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3942 ei
= ospf_external_info_check(ospf
, lsa
);
3944 new = ospf_external_lsa_refresh(
3945 ospf
, lsa
, ei
, LSA_REFRESH_FORCE
, false);
3947 aggr
= (struct ospf_external_aggr_rt
*)
3948 ospf_extrenal_aggregator_lookup(ospf
, &p
);
3950 struct external_info ei_aggr
;
3952 memset(&ei_aggr
, 0, sizeof(ei_aggr
));
3953 ei_aggr
.p
= aggr
->p
;
3954 ei_aggr
.tag
= aggr
->tag
;
3955 ei_aggr
.instance
= ospf
->instance
;
3956 ei_aggr
.route_map_set
.metric
= -1;
3957 ei_aggr
.route_map_set
.metric_type
= -1;
3959 ospf_external_lsa_refresh(ospf
, lsa
, &ei_aggr
,
3960 LSA_REFRESH_FORCE
, true);
3961 SET_FLAG(aggr
->flags
,
3962 OSPF_EXTERNAL_AGGRT_ORIGINATED
);
3964 ospf_lsa_flush_as(ospf
, lsa
);
3967 case OSPF_OPAQUE_LINK_LSA
:
3968 case OSPF_OPAQUE_AREA_LSA
:
3969 case OSPF_OPAQUE_AS_LSA
:
3970 new = ospf_opaque_lsa_refresh(lsa
);
3978 void ospf_refresher_register_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3980 uint16_t index
, current_index
;
3982 assert(lsa
->lock
> 0);
3983 if (!IS_LSA_SELF(lsa
) && !IS_LSA_ORR(lsa
))
3986 if (lsa
->refresh_list
< 0) {
3989 ospf
->lsa_refresh_timer
- (2 * OSPF_LS_REFRESH_JITTER
);
3991 ospf
->lsa_refresh_timer
- OSPF_LS_REFRESH_JITTER
;
3993 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which
3995 * 1800s. Use jitter so that we send the LSA sometime between
3999 delay
= (frr_weak_random() % (max_delay
- min_delay
))
4002 current_index
= ospf
->lsa_refresh_queue
.index
4003 + (monotime(NULL
) - ospf
->lsa_refresher_started
)
4004 / OSPF_LSA_REFRESHER_GRANULARITY
;
4006 index
= (current_index
+ delay
/ OSPF_LSA_REFRESHER_GRANULARITY
)
4007 % (OSPF_LSA_REFRESHER_SLOTS
);
4009 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4011 "LSA[Refresh:Type%d:%pI4]: age %d, added to index %d",
4012 lsa
->data
->type
, &lsa
->data
->id
,
4013 LS_AGE(lsa
), index
);
4015 if (!ospf
->lsa_refresh_queue
.qs
[index
])
4016 ospf
->lsa_refresh_queue
.qs
[index
] = list_new();
4018 listnode_add(ospf
->lsa_refresh_queue
.qs
[index
],
4019 ospf_lsa_lock(lsa
)); /* lsa_refresh_queue */
4020 lsa
->refresh_list
= index
;
4022 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4024 "LSA[Refresh:Type%d:%pI4]: %s: setting refresh_list on lsa %p (slot %d)",
4025 lsa
->data
->type
, &lsa
->data
->id
, __func__
,
4026 (void *)lsa
, index
);
4030 void ospf_refresher_unregister_lsa(struct ospf
*ospf
, struct ospf_lsa
*lsa
)
4032 assert(lsa
->lock
> 0);
4033 if (!IS_LSA_SELF(lsa
) || !IS_LSA_ORR(lsa
))
4035 if (lsa
->refresh_list
>= 0) {
4036 struct list
*refresh_list
=
4037 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
4038 listnode_delete(refresh_list
, lsa
);
4039 if (!listcount(refresh_list
)) {
4040 list_delete(&refresh_list
);
4041 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
4043 lsa
->refresh_list
= -1;
4044 ospf_lsa_unlock(&lsa
); /* lsa_refresh_queue */
4048 void ospf_lsa_refresh_walker(struct thread
*t
)
4050 struct list
*refresh_list
;
4051 struct listnode
*node
, *nnode
;
4052 struct ospf
*ospf
= THREAD_ARG(t
);
4053 struct ospf_lsa
*lsa
;
4055 struct list
*lsa_to_refresh
= list_new();
4057 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4058 zlog_debug("LSA[Refresh]: %s: start", __func__
);
4061 i
= ospf
->lsa_refresh_queue
.index
;
4063 /* Note: if clock has jumped backwards, then time change could be
4065 so we are careful to cast the expression to unsigned before taking
4067 ospf
->lsa_refresh_queue
.index
=
4068 ((unsigned long)(ospf
->lsa_refresh_queue
.index
4070 - ospf
->lsa_refresher_started
)
4071 / OSPF_LSA_REFRESHER_GRANULARITY
))
4072 % OSPF_LSA_REFRESHER_SLOTS
;
4074 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4075 zlog_debug("LSA[Refresh]: %s: next index %d", __func__
,
4076 ospf
->lsa_refresh_queue
.index
);
4078 for (; i
!= ospf
->lsa_refresh_queue
.index
;
4079 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
) {
4080 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4081 zlog_debug("LSA[Refresh]: %s: refresh index %d",
4084 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
4088 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
4091 for (ALL_LIST_ELEMENTS(refresh_list
, node
, nnode
,
4093 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4095 "LSA[Refresh:Type%d:%pI4]: %s: refresh lsa %p (slot %d)",
4096 lsa
->data
->type
, &lsa
->data
->id
,
4097 __func__
, (void *)lsa
, i
);
4099 assert(lsa
->lock
> 0);
4100 list_delete_node(refresh_list
, node
);
4101 lsa
->refresh_list
= -1;
4102 listnode_add(lsa_to_refresh
, lsa
);
4104 list_delete(&refresh_list
);
4108 ospf
->t_lsa_refresher
= NULL
;
4109 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
,
4110 ospf
->lsa_refresh_interval
, &ospf
->t_lsa_refresher
);
4111 ospf
->lsa_refresher_started
= monotime(NULL
);
4113 for (ALL_LIST_ELEMENTS(lsa_to_refresh
, node
, nnode
, lsa
)) {
4114 ospf_lsa_refresh(ospf
, lsa
);
4115 assert(lsa
->lock
> 0);
4117 &lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
4120 list_delete(&lsa_to_refresh
);
4122 if (IS_DEBUG_OSPF(lsa
, LSA_REFRESH
))
4123 zlog_debug("LSA[Refresh]: %s: end", __func__
);
4126 /* Flush the LSAs for the specific area */
4127 void ospf_flush_lsa_from_area(struct ospf
*ospf
, struct in_addr area_id
,
4130 struct ospf_area
*area
;
4131 struct route_node
*rn
;
4132 struct ospf_lsa
*lsa
;
4134 area
= ospf_area_get(ospf
, area_id
);
4137 case OSPF_AS_EXTERNAL_LSA
:
4138 if ((area
->external_routing
== OSPF_AREA_NSSA
) ||
4139 (area
->external_routing
== OSPF_AREA_STUB
)) {
4140 LSDB_LOOP (EXTERNAL_LSDB(ospf
), rn
, lsa
)
4141 if (IS_LSA_SELF(lsa
) &&
4142 !(CHECK_FLAG(lsa
->flags
,
4143 OSPF_LSA_LOCAL_XLT
)))
4144 ospf_lsa_flush_area(lsa
, area
);
4147 case OSPF_AS_NSSA_LSA
:
4148 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
4149 if (IS_LSA_SELF(lsa
))
4150 ospf_lsa_flush_area(lsa
, area
);