2 * OSPF Link State Advertisement
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "sockunion.h" /* for inet_aton() */
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_lsdb.h"
43 #include "ospfd/ospf_neighbor.h"
44 #include "ospfd/ospf_nsm.h"
45 #include "ospfd/ospf_flood.h"
46 #include "ospfd/ospf_packet.h"
47 #include "ospfd/ospf_spf.h"
48 #include "ospfd/ospf_dump.h"
49 #include "ospfd/ospf_route.h"
50 #include "ospfd/ospf_ase.h"
51 #include "ospfd/ospf_zebra.h"
55 get_metric (u_char
*metric
)
59 m
= (m
<< 8) + metric
[1];
60 m
= (m
<< 8) + metric
[2];
82 ret
.tv_usec
= (a
%1000) * 1000;
88 ospf_lsa_refresh_delay (struct ospf_lsa
*lsa
)
93 if (monotime_since (&lsa
->tv_orig
, &delta
) < OSPF_MIN_LS_INTERVAL
* 1000LL)
95 struct timeval minv
= msec2tv (OSPF_MIN_LS_INTERVAL
);
96 timersub (&minv
, &delta
, &minv
);
98 /* TBD: remove padding to full sec, return timeval instead */
99 delay
= minv
.tv_sec
+ !!minv
.tv_usec
;
101 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
102 zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
103 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), delay
);
113 get_age (struct ospf_lsa
*lsa
)
117 monotime_since (&lsa
->tv_recv
, &rel
);
118 return ntohs (lsa
->data
->ls_age
) + rel
.tv_sec
;
122 /* Fletcher Checksum -- Refer to RFC1008. */
124 /* All the offsets are zero-based. The offsets in the RFC1008 are
127 ospf_lsa_checksum (struct lsa_header
*lsa
)
129 u_char
*buffer
= (u_char
*) &lsa
->options
;
130 int options_offset
= buffer
- (u_char
*) &lsa
->ls_age
; /* should be 2 */
132 /* Skip the AGE field */
133 u_int16_t len
= ntohs(lsa
->length
) - options_offset
;
135 /* Checksum offset starts from "options" field, not the beginning of the
136 lsa_header struct. The offset is 14, rather than 16. */
137 int checksum_offset
= (u_char
*) &lsa
->checksum
- buffer
;
139 return fletcher_checksum(buffer
, len
, checksum_offset
);
143 ospf_lsa_checksum_valid (struct lsa_header
*lsa
)
145 u_char
*buffer
= (u_char
*) &lsa
->options
;
146 int options_offset
= buffer
- (u_char
*) &lsa
->ls_age
; /* should be 2 */
148 /* Skip the AGE field */
149 u_int16_t len
= ntohs(lsa
->length
) - options_offset
;
151 return(fletcher_checksum(buffer
, len
, FLETCHER_CHECKSUM_VALIDATE
) == 0);
156 /* Create OSPF LSA. */
160 struct ospf_lsa
*new;
162 new = XCALLOC (MTYPE_OSPF_LSA
, sizeof (struct ospf_lsa
));
166 new->retransmit_counter
= 0;
167 monotime(&new->tv_recv
);
168 new->tv_orig
= new->tv_recv
;
169 new->refresh_list
= -1;
174 /* Duplicate OSPF LSA. */
176 ospf_lsa_dup (struct ospf_lsa
*lsa
)
178 struct ospf_lsa
*new;
183 new = XCALLOC (MTYPE_OSPF_LSA
, sizeof (struct ospf_lsa
));
185 memcpy (new, lsa
, sizeof (struct ospf_lsa
));
186 UNSET_FLAG (new->flags
, OSPF_LSA_DISCARD
);
188 new->retransmit_counter
= 0;
189 new->data
= ospf_lsa_data_dup (lsa
->data
);
191 /* kevinm: Clear the refresh_list, otherwise there are going
192 to be problems when we try to remove the LSA from the
193 queue (which it's not a member of.)
194 XXX: Should we add the LSA to the refresh_list queue? */
195 new->refresh_list
= -1;
197 if (IS_DEBUG_OSPF (lsa
, LSA
))
198 zlog_debug ("LSA: duplicated %p (new: %p)", (void *)lsa
, (void *)new);
205 ospf_lsa_free (struct ospf_lsa
*lsa
)
207 assert (lsa
->lock
== 0);
209 if (IS_DEBUG_OSPF (lsa
, LSA
))
210 zlog_debug ("LSA: freed %p", (void *)lsa
);
212 /* Delete LSA data. */
213 if (lsa
->data
!= NULL
)
214 ospf_lsa_data_free (lsa
->data
);
216 assert (lsa
->refresh_list
< 0);
218 memset (lsa
, 0, sizeof (struct ospf_lsa
));
219 XFREE (MTYPE_OSPF_LSA
, lsa
);
224 ospf_lsa_lock (struct ospf_lsa
*lsa
)
232 ospf_lsa_unlock (struct ospf_lsa
**lsa
)
234 /* This is sanity check. */
240 assert ((*lsa
)->lock
>= 0);
242 if ((*lsa
)->lock
== 0)
244 assert (CHECK_FLAG ((*lsa
)->flags
, OSPF_LSA_DISCARD
));
245 ospf_lsa_free (*lsa
);
250 /* Check discard flag. */
252 ospf_lsa_discard (struct ospf_lsa
*lsa
)
254 if (!CHECK_FLAG (lsa
->flags
, OSPF_LSA_DISCARD
))
256 SET_FLAG (lsa
->flags
, OSPF_LSA_DISCARD
);
257 ospf_lsa_unlock (&lsa
);
261 /* Create LSA data. */
263 ospf_lsa_data_new (size_t size
)
265 return XCALLOC (MTYPE_OSPF_LSA_DATA
, size
);
268 /* Duplicate LSA data. */
270 ospf_lsa_data_dup (struct lsa_header
*lsah
)
272 struct lsa_header
*new;
274 new = ospf_lsa_data_new (ntohs (lsah
->length
));
275 memcpy (new, lsah
, ntohs (lsah
->length
));
282 ospf_lsa_data_free (struct lsa_header
*lsah
)
284 if (IS_DEBUG_OSPF (lsa
, LSA
))
285 zlog_debug ("LSA[Type%d:%s]: data freed %p",
286 lsah
->type
, inet_ntoa (lsah
->id
), (void *)lsah
);
288 XFREE (MTYPE_OSPF_LSA_DATA
, lsah
);
292 /* LSA general functions. */
295 dump_lsa_key (struct ospf_lsa
*lsa
)
297 static char buf
[] = {
298 "Type255,id(255.255.255.255),ar(255.255.255.255)"
300 struct lsa_header
*lsah
;
302 if (lsa
!= NULL
&& (lsah
= lsa
->data
) != NULL
)
304 char id
[INET_ADDRSTRLEN
], ar
[INET_ADDRSTRLEN
];
305 strcpy (id
, inet_ntoa (lsah
->id
));
306 strcpy (ar
, inet_ntoa (lsah
->adv_router
));
308 sprintf (buf
, "Type%d,id(%s),ar(%s)", lsah
->type
, id
, ar
);
311 strcpy (buf
, "NULL");
317 lsa_seqnum_increment (struct ospf_lsa
*lsa
)
321 seqnum
= ntohl (lsa
->data
->ls_seqnum
) + 1;
323 return htonl (seqnum
);
327 lsa_header_set (struct stream
*s
, u_char options
,
328 u_char type
, struct in_addr id
, struct in_addr router_id
)
330 struct lsa_header
*lsah
;
332 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
334 lsah
->ls_age
= htons (OSPF_LSA_INITIAL_AGE
);
335 lsah
->options
= options
;
338 lsah
->adv_router
= router_id
;
339 lsah
->ls_seqnum
= htonl (OSPF_INITIAL_SEQUENCE_NUMBER
);
341 stream_forward_endp (s
, OSPF_LSA_HEADER_SIZE
);
345 /* router-LSA related functions. */
346 /* Get router-LSA flags. */
348 router_lsa_flags (struct ospf_area
*area
)
352 flags
= area
->ospf
->flags
;
354 /* Set virtual link flag. */
355 if (ospf_full_virtual_nbrs (area
))
356 SET_FLAG (flags
, ROUTER_LSA_VIRTUAL
);
358 /* Just sanity check */
359 UNSET_FLAG (flags
, ROUTER_LSA_VIRTUAL
);
361 /* Set Shortcut ABR behabiour flag. */
362 UNSET_FLAG (flags
, ROUTER_LSA_SHORTCUT
);
363 if (area
->ospf
->abr_type
== OSPF_ABR_SHORTCUT
)
364 if (!OSPF_IS_AREA_BACKBONE (area
))
365 if ((area
->shortcut_configured
== OSPF_SHORTCUT_DEFAULT
&&
366 area
->ospf
->backbone
== NULL
) ||
367 area
->shortcut_configured
== OSPF_SHORTCUT_ENABLE
)
368 SET_FLAG (flags
, ROUTER_LSA_SHORTCUT
);
370 /* ASBR can't exit in stub area. */
371 if (area
->external_routing
== OSPF_AREA_STUB
)
372 UNSET_FLAG (flags
, ROUTER_LSA_EXTERNAL
);
373 /* If ASBR set External flag */
374 else if (IS_OSPF_ASBR (area
->ospf
))
375 SET_FLAG (flags
, ROUTER_LSA_EXTERNAL
);
377 /* Set ABR dependent flags */
378 if (IS_OSPF_ABR (area
->ospf
))
380 SET_FLAG (flags
, ROUTER_LSA_BORDER
);
381 /* If Area is NSSA and we are both ABR and unconditional translator,
382 * set Nt bit to inform other routers.
384 if ( (area
->external_routing
== OSPF_AREA_NSSA
)
385 && (area
->NSSATranslatorRole
== OSPF_NSSA_ROLE_ALWAYS
))
386 SET_FLAG (flags
, ROUTER_LSA_NT
);
391 /* Lookup neighbor other than myself.
392 And check neighbor count,
393 Point-to-Point link must have only 1 neighbor. */
394 struct ospf_neighbor
*
395 ospf_nbr_lookup_ptop (struct ospf_interface
*oi
)
397 struct ospf_neighbor
*nbr
= NULL
;
398 struct route_node
*rn
;
400 /* Search neighbor, there must be one of two nbrs. */
401 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
402 if ((nbr
= rn
->info
))
403 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
404 if (nbr
->state
== NSM_Full
)
406 route_unlock_node (rn
);
410 /* PtoP link must have only 1 neighbor. */
411 if (ospf_nbr_count (oi
, 0) > 1)
412 zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
417 /* Determine cost of link, taking RFC3137 stub-router support into
421 ospf_link_cost (struct ospf_interface
*oi
)
423 /* RFC3137 stub router support */
424 if (!CHECK_FLAG (oi
->area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
))
425 return oi
->output_cost
;
427 return OSPF_OUTPUT_COST_INFINITE
;
430 /* Set a link information. */
432 link_info_set (struct stream
*s
, struct in_addr id
,
433 struct in_addr data
, u_char type
, u_char tos
, u_int16_t cost
)
435 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
436 * vast majority of cases. Some rare routers with lots of links need more.
437 * we try accomodate those here.
439 if (STREAM_WRITEABLE(s
) < OSPF_ROUTER_LSA_LINK_SIZE
)
441 size_t ret
= OSPF_MAX_LSA_SIZE
;
443 /* Can we enlarge the stream still? */
444 if (STREAM_SIZE(s
) == OSPF_MAX_LSA_SIZE
)
446 /* we futz the size here for simplicity, really we need to account
448 * IP Header - (sizeof (struct ip))
449 * OSPF Header - OSPF_HEADER_SIZE
450 * LSA Header - OSPF_LSA_HEADER_SIZE
451 * MD5 auth data, if MD5 is configured - OSPF_AUTH_MD5_SIZE.
453 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
455 ret
= stream_resize (s
, OSPF_MAX_PACKET_SIZE
- OSPF_MAX_LSA_SIZE
);
458 if (ret
== OSPF_MAX_LSA_SIZE
)
460 zlog_warn ("%s: Out of space in LSA stream, left %zd, size %zd",
461 __func__
, STREAM_REMAIN (s
), STREAM_SIZE (s
));
466 /* TOS based routing is not supported. */
467 stream_put_ipv4 (s
, id
.s_addr
); /* Link ID. */
468 stream_put_ipv4 (s
, data
.s_addr
); /* Link Data. */
469 stream_putc (s
, type
); /* Link Type. */
470 stream_putc (s
, tos
); /* TOS = 0. */
471 stream_putw (s
, cost
); /* Link Cost. */
476 /* Describe Point-to-Point link (Section 12.4.1.1). */
478 lsa_link_ptop_set (struct stream
*s
, struct ospf_interface
*oi
)
481 struct ospf_neighbor
*nbr
;
482 struct in_addr id
, mask
, data
;
483 u_int16_t cost
= ospf_link_cost (oi
);
485 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
486 zlog_debug ("LSA[Type1]: Set link Point-to-Point");
488 if ((nbr
= ospf_nbr_lookup_ptop (oi
)))
489 if (nbr
->state
== NSM_Full
)
491 if (CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
493 /* For unnumbered point-to-point networks, the Link Data field
494 should specify the interface's MIB-II ifIndex value. */
495 data
.s_addr
= htonl(oi
->ifp
->ifindex
);
496 links
+= link_info_set (s
, nbr
->router_id
, data
,
497 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
501 links
+= link_info_set (s
, nbr
->router_id
,
502 oi
->address
->u
.prefix4
,
503 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
507 /* no need for a stub link for unnumbered interfaces */
508 if (!CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
))
510 /* Regardless of the state of the neighboring router, we must
511 add a Type 3 link (stub network).
512 N.B. Options 1 & 2 share basically the same logic. */
513 masklen2ip (oi
->address
->prefixlen
, &mask
);
514 id
.s_addr
= CONNECTED_PREFIX(oi
->connected
)->u
.prefix4
.s_addr
& mask
.s_addr
;
515 links
+= link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
522 /* Describe Broadcast Link. */
524 lsa_link_broadcast_set (struct stream
*s
, struct ospf_interface
*oi
)
526 struct ospf_neighbor
*dr
;
527 struct in_addr id
, mask
;
528 u_int16_t cost
= ospf_link_cost (oi
);
530 /* Describe Type 3 Link. */
531 if (oi
->state
== ISM_Waiting
)
533 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
534 zlog_debug ("LSA[Type1]: Interface %s is in state Waiting. "
535 "Adding stub interface", oi
->ifp
->name
);
536 masklen2ip (oi
->address
->prefixlen
, &mask
);
537 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
538 return link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
542 dr
= ospf_nbr_lookup_by_addr (oi
->nbrs
, &DR (oi
));
543 /* Describe Type 2 link. */
544 if (dr
&& (dr
->state
== NSM_Full
||
545 IPV4_ADDR_SAME (&oi
->address
->u
.prefix4
, &DR (oi
))) &&
546 ospf_nbr_count (oi
, NSM_Full
) > 0)
548 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
549 zlog_debug ("LSA[Type1]: Interface %s has a DR. "
550 "Adding transit interface", oi
->ifp
->name
);
551 return link_info_set (s
, DR (oi
), oi
->address
->u
.prefix4
,
552 LSA_LINK_TYPE_TRANSIT
, 0, cost
);
554 /* Describe type 3 link. */
557 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
558 zlog_debug ("LSA[Type1]: Interface %s has no DR. "
559 "Adding stub interface", oi
->ifp
->name
);
560 masklen2ip (oi
->address
->prefixlen
, &mask
);
561 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
562 return link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0,
568 lsa_link_loopback_set (struct stream
*s
, struct ospf_interface
*oi
)
570 struct in_addr id
, mask
;
572 /* Describe Type 3 Link. */
573 if (oi
->state
!= ISM_Loopback
)
576 mask
.s_addr
= 0xffffffff;
577 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
578 return link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
581 /* Describe Virtual Link. */
583 lsa_link_virtuallink_set (struct stream
*s
, struct ospf_interface
*oi
)
585 struct ospf_neighbor
*nbr
;
586 u_int16_t cost
= ospf_link_cost (oi
);
588 if (oi
->state
== ISM_PointToPoint
)
589 if ((nbr
= ospf_nbr_lookup_ptop (oi
)))
590 if (nbr
->state
== NSM_Full
)
592 return link_info_set (s
, nbr
->router_id
, oi
->address
->u
.prefix4
,
593 LSA_LINK_TYPE_VIRTUALLINK
, 0, cost
);
599 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
601 /* this function add for support point-to-multipoint ,see rfc2328
603 /* from "edward rrr" <edward_rrr@hotmail.com>
604 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
606 lsa_link_ptomp_set (struct stream
*s
, struct ospf_interface
*oi
)
609 struct route_node
*rn
;
610 struct ospf_neighbor
*nbr
= NULL
;
611 struct in_addr id
, mask
;
612 u_int16_t cost
= ospf_link_cost (oi
);
614 mask
.s_addr
= 0xffffffff;
615 id
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
616 links
+= link_info_set (s
, id
, mask
, LSA_LINK_TYPE_STUB
, 0, 0);
618 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
619 zlog_debug ("PointToMultipoint: running ptomultip_set");
621 /* Search neighbor, */
622 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
623 if ((nbr
= rn
->info
) != NULL
)
625 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
626 if (nbr
->state
== NSM_Full
)
629 links
+= link_info_set (s
, nbr
->router_id
, oi
->address
->u
.prefix4
,
630 LSA_LINK_TYPE_POINTOPOINT
, 0, cost
);
631 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
632 zlog_debug ("PointToMultipoint: set link to %s",
633 inet_ntoa(oi
->address
->u
.prefix4
));
639 /* Set router-LSA link information. */
641 router_lsa_link_set (struct stream
*s
, struct ospf_area
*area
)
643 struct listnode
*node
;
644 struct ospf_interface
*oi
;
647 for (ALL_LIST_ELEMENTS_RO (area
->oiflist
, node
, oi
))
649 struct interface
*ifp
= oi
->ifp
;
651 /* Check interface is up, OSPF is enable. */
652 if (if_is_operative (ifp
))
654 if (oi
->state
!= ISM_Down
)
656 oi
->lsa_pos_beg
= links
;
657 /* Describe each link. */
660 case OSPF_IFTYPE_POINTOPOINT
:
661 links
+= lsa_link_ptop_set (s
, oi
);
663 case OSPF_IFTYPE_BROADCAST
:
664 links
+= lsa_link_broadcast_set (s
, oi
);
666 case OSPF_IFTYPE_NBMA
:
667 links
+= lsa_link_nbma_set (s
, oi
);
669 case OSPF_IFTYPE_POINTOMULTIPOINT
:
670 links
+= lsa_link_ptomp_set (s
, oi
);
672 case OSPF_IFTYPE_VIRTUALLINK
:
673 links
+= lsa_link_virtuallink_set (s
, oi
);
675 case OSPF_IFTYPE_LOOPBACK
:
676 links
+= lsa_link_loopback_set (s
, oi
);
678 oi
->lsa_pos_end
= links
;
686 /* Set router-LSA body. */
688 ospf_router_lsa_body_set (struct stream
*s
, struct ospf_area
*area
)
694 stream_putc (s
, router_lsa_flags (area
));
696 /* Set Zero fields. */
699 /* Keep pointer to # links. */
700 putp
= stream_get_endp(s
);
705 /* Set all link information. */
706 cnt
= router_lsa_link_set (s
, area
);
708 /* Set # of links here. */
709 stream_putw_at (s
, putp
, cnt
);
713 ospf_stub_router_timer (struct thread
*t
)
715 struct ospf_area
*area
= THREAD_ARG (t
);
717 area
->t_stub_router
= NULL
;
719 SET_FLAG (area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
721 /* clear stub route state and generate router-lsa refresh, don't
722 * clobber an administratively set stub-router state though.
724 if (CHECK_FLAG (area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
727 UNSET_FLAG (area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
729 ospf_router_lsa_update_area (area
);
735 ospf_stub_router_check (struct ospf_area
*area
)
737 /* area must either be administratively configured to be stub
738 * or startup-time stub-router must be configured and we must in a pre-stub
741 if (CHECK_FLAG (area
->stub_router_state
, OSPF_AREA_ADMIN_STUB_ROUTED
))
743 SET_FLAG (area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
747 /* not admin-stubbed, check whether startup stubbing is configured and
748 * whether it's not been done yet
750 if (CHECK_FLAG (area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
))
753 if (area
->ospf
->stub_router_startup_time
== OSPF_STUB_ROUTER_UNCONFIGURED
)
755 /* stub-router is hence done forever for this area, even if someone
756 * tries configure it (take effect next restart).
758 SET_FLAG (area
->stub_router_state
, OSPF_AREA_WAS_START_STUB_ROUTED
);
762 /* startup stub-router configured and not yet done */
763 SET_FLAG (area
->stub_router_state
, OSPF_AREA_IS_STUB_ROUTED
);
765 OSPF_AREA_TIMER_ON (area
->t_stub_router
, ospf_stub_router_timer
,
766 area
->ospf
->stub_router_startup_time
);
769 /* Create new router-LSA. */
770 static struct ospf_lsa
*
771 ospf_router_lsa_new (struct ospf_area
*area
)
773 struct ospf
*ospf
= area
->ospf
;
775 struct lsa_header
*lsah
;
776 struct ospf_lsa
*new;
779 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
780 zlog_debug ("LSA[Type1]: Create router-LSA instance");
782 /* check whether stub-router is desired, and if this is the first
785 ospf_stub_router_check (area
);
787 /* Create a stream for LSA. */
788 s
= stream_new (OSPF_MAX_LSA_SIZE
);
789 /* Set LSA common header fields. */
790 lsa_header_set (s
, LSA_OPTIONS_GET (area
) | LSA_OPTIONS_NSSA_GET (area
),
791 OSPF_ROUTER_LSA
, ospf
->router_id
, ospf
->router_id
);
793 /* Set router-LSA body fields. */
794 ospf_router_lsa_body_set (s
, area
);
797 length
= stream_get_endp (s
);
798 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
799 lsah
->length
= htons (length
);
801 /* Now, create OSPF LSA instance. */
802 if ( (new = ospf_lsa_new ()) == NULL
)
804 zlog_err ("%s: Unable to create new lsa", __func__
);
809 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
811 /* Copy LSA data to store, discard stream. */
812 new->data
= ospf_lsa_data_new (length
);
813 memcpy (new->data
, lsah
, length
);
819 /* Originate Router-LSA. */
820 static struct ospf_lsa
*
821 ospf_router_lsa_originate (struct ospf_area
*area
)
823 struct ospf_lsa
*new;
825 /* Create new router-LSA instance. */
826 if ( (new = ospf_router_lsa_new (area
)) == NULL
)
828 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__
);
833 if (new->data
->adv_router
.s_addr
== 0)
835 if (IS_DEBUG_OSPF_EVENT
)
836 zlog_debug ("LSA[Type1]: AdvRouter is 0, discard");
837 ospf_lsa_discard (new);
841 /* Install LSA to LSDB. */
842 new = ospf_lsa_install (area
->ospf
, NULL
, new);
844 /* Update LSA origination count. */
845 area
->ospf
->lsa_originate_count
++;
847 /* Flooding new LSA through area. */
848 ospf_flood_through_area (area
, NULL
, new);
850 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
852 zlog_debug ("LSA[Type%d:%s]: Originate router-LSA %p",
853 new->data
->type
, inet_ntoa (new->data
->id
), (void *)new);
854 ospf_lsa_header_dump (new->data
);
860 /* Refresh router-LSA. */
861 static struct ospf_lsa
*
862 ospf_router_lsa_refresh (struct ospf_lsa
*lsa
)
864 struct ospf_area
*area
= lsa
->area
;
865 struct ospf_lsa
*new;
870 /* Delete LSA from neighbor retransmit-list. */
871 ospf_ls_retransmit_delete_nbr_area (area
, lsa
);
873 /* Unregister LSA from refresh-list */
874 ospf_refresher_unregister_lsa (area
->ospf
, lsa
);
876 /* Create new router-LSA instance. */
877 if ( (new = ospf_router_lsa_new (area
)) == NULL
)
879 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__
);
883 new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
885 ospf_lsa_install (area
->ospf
, NULL
, new);
887 /* Flood LSA through area. */
888 ospf_flood_through_area (area
, NULL
, new);
891 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
893 zlog_debug ("LSA[Type%d:%s]: router-LSA refresh",
894 new->data
->type
, inet_ntoa (new->data
->id
));
895 ospf_lsa_header_dump (new->data
);
902 ospf_router_lsa_update_area (struct ospf_area
*area
)
904 if (IS_DEBUG_OSPF_EVENT
)
905 zlog_debug ("[router-LSA]: (router-LSA area update)");
907 /* Now refresh router-LSA. */
908 if (area
->router_lsa_self
)
909 ospf_lsa_refresh (area
->ospf
, area
->router_lsa_self
);
910 /* Newly originate router-LSA. */
912 ospf_router_lsa_originate (area
);
918 ospf_router_lsa_update (struct ospf
*ospf
)
920 struct listnode
*node
, *nnode
;
921 struct ospf_area
*area
;
923 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
924 zlog_debug ("Timer[router-LSA Update]: (timer expire)");
926 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
928 struct ospf_lsa
*lsa
= area
->router_lsa_self
;
929 struct router_lsa
*rl
;
930 const char *area_str
;
932 /* Keep Area ID string. */
933 area_str
= AREA_NAME (area
);
935 /* If LSA not exist in this Area, originate new. */
938 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
939 zlog_debug("LSA[Type1]: Create router-LSA for Area %s", area_str
);
941 ospf_router_lsa_originate (area
);
943 /* If router-ID is changed, Link ID must change.
944 First flush old LSA, then originate new. */
945 else if (!IPV4_ADDR_SAME (&lsa
->data
->id
, &ospf
->router_id
))
947 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
948 zlog_debug("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
949 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), area_str
);
950 ospf_refresher_unregister_lsa (ospf
, lsa
);
951 ospf_lsa_flush_area (lsa
, area
);
952 ospf_lsa_unlock (&area
->router_lsa_self
);
953 area
->router_lsa_self
= NULL
;
955 /* Refresh router-LSA, (not install) and flood through area. */
956 ospf_router_lsa_update_area (area
);
960 rl
= (struct router_lsa
*) lsa
->data
;
961 /* Refresh router-LSA, (not install) and flood through area. */
962 if (rl
->flags
!= ospf
->flags
)
963 ospf_router_lsa_update_area (area
);
971 /* network-LSA related functions. */
972 /* Originate Network-LSA. */
974 ospf_network_lsa_body_set (struct stream
*s
, struct ospf_interface
*oi
)
977 struct route_node
*rn
;
978 struct ospf_neighbor
*nbr
;
980 masklen2ip (oi
->address
->prefixlen
, &mask
);
981 stream_put_ipv4 (s
, mask
.s_addr
);
983 /* The network-LSA lists those routers that are fully adjacent to
984 the Designated Router; each fully adjacent router is identified by
985 its OSPF Router ID. The Designated Router includes itself in this
986 list. RFC2328, Section 12.4.2 */
988 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
989 if ((nbr
= rn
->info
) != NULL
)
990 if (nbr
->state
== NSM_Full
|| nbr
== oi
->nbr_self
)
991 stream_put_ipv4 (s
, nbr
->router_id
.s_addr
);
994 static struct ospf_lsa
*
995 ospf_network_lsa_new (struct ospf_interface
*oi
)
998 struct ospf_lsa
*new;
999 struct lsa_header
*lsah
;
1000 struct ospf_if_params
*oip
;
1003 /* If there are no neighbours on this network (the net is stub),
1004 the router does not originate network-LSA (see RFC 12.4.2) */
1005 if (oi
->full_nbrs
== 0)
1008 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1009 zlog_debug ("LSA[Type2]: Create network-LSA instance");
1011 /* Create new stream for LSA. */
1012 s
= stream_new (OSPF_MAX_LSA_SIZE
);
1013 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
1015 lsa_header_set (s
, (OPTIONS (oi
) | LSA_OPTIONS_GET (oi
->area
)),
1016 OSPF_NETWORK_LSA
, DR (oi
), oi
->ospf
->router_id
);
1018 /* Set network-LSA body fields. */
1019 ospf_network_lsa_body_set (s
, oi
);
1022 length
= stream_get_endp (s
);
1023 lsah
->length
= htons (length
);
1025 /* Create OSPF LSA instance. */
1026 if ( (new = ospf_lsa_new ()) == NULL
)
1028 zlog_err ("%s: ospf_lsa_new returned NULL", __func__
);
1032 new->area
= oi
->area
;
1033 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1035 /* Copy LSA to store. */
1036 new->data
= ospf_lsa_data_new (length
);
1037 memcpy (new->data
, lsah
, length
);
1040 /* Remember prior network LSA sequence numbers, even if we stop
1041 * originating one for this oi, to try avoid re-originating LSAs with a
1042 * prior sequence number, and thus speed up adjency forming & convergence.
1044 if ((oip
= ospf_lookup_if_params (oi
->ifp
, oi
->address
->u
.prefix4
)))
1046 new->data
->ls_seqnum
= oip
->network_lsa_seqnum
;
1047 new->data
->ls_seqnum
= lsa_seqnum_increment (new);
1051 oip
= ospf_get_if_params (oi
->ifp
, oi
->address
->u
.prefix4
);
1052 ospf_if_update_params (oi
->ifp
, oi
->address
->u
.prefix4
);
1054 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
;
1059 /* Originate network-LSA. */
1061 ospf_network_lsa_update (struct ospf_interface
*oi
)
1063 struct ospf_lsa
*new;
1065 if (oi
->network_lsa_self
!= NULL
)
1067 ospf_lsa_refresh (oi
->ospf
, oi
->network_lsa_self
);
1071 /* Create new network-LSA instance. */
1072 new = ospf_network_lsa_new (oi
);
1076 /* Install LSA to LSDB. */
1077 new = ospf_lsa_install (oi
->ospf
, oi
, new);
1079 /* Update LSA origination count. */
1080 oi
->ospf
->lsa_originate_count
++;
1082 /* Flooding new LSA through area. */
1083 ospf_flood_through_area (oi
->area
, NULL
, new);
1085 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1087 zlog_debug ("LSA[Type%d:%s]: Originate network-LSA %p",
1088 new->data
->type
, inet_ntoa (new->data
->id
), (void *)new);
1089 ospf_lsa_header_dump (new->data
);
1095 static struct ospf_lsa
*
1096 ospf_network_lsa_refresh (struct ospf_lsa
*lsa
)
1098 struct ospf_area
*area
= lsa
->area
;
1099 struct ospf_lsa
*new, *new2
;
1100 struct ospf_if_params
*oip
;
1101 struct ospf_interface
*oi
;
1105 /* Retrieve the oi for the network LSA */
1106 oi
= ospf_if_lookup_by_local_addr (area
->ospf
, NULL
, lsa
->data
->id
);
1109 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1111 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh: "
1112 "no oi found, ick, ignoring.",
1113 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
1114 ospf_lsa_header_dump (lsa
->data
);
1118 /* Delete LSA from neighbor retransmit-list. */
1119 ospf_ls_retransmit_delete_nbr_area (area
, lsa
);
1121 /* Unregister LSA from refresh-list */
1122 ospf_refresher_unregister_lsa (area
->ospf
, lsa
);
1124 /* Create new network-LSA instance. */
1125 new = ospf_network_lsa_new (oi
);
1129 oip
= ospf_lookup_if_params (oi
->ifp
, oi
->address
->u
.prefix4
);
1130 assert (oip
!= NULL
);
1131 oip
->network_lsa_seqnum
= new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
1133 new2
= ospf_lsa_install (area
->ospf
, oi
, new);
1135 assert (new2
== new);
1137 /* Flood LSA through aera. */
1138 ospf_flood_through_area (area
, NULL
, new);
1140 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1142 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh",
1143 new->data
->type
, inet_ntoa (new->data
->id
));
1144 ospf_lsa_header_dump (new->data
);
1151 stream_put_ospf_metric (struct stream
*s
, u_int32_t metric_value
)
1156 /* Put 0 metric. TOS metric is not supported. */
1157 metric
= htonl (metric_value
);
1158 mp
= (char *) &metric
;
1160 stream_put (s
, mp
, 3);
1163 /* summary-LSA related functions. */
1165 ospf_summary_lsa_body_set (struct stream
*s
, struct prefix
*p
,
1168 struct in_addr mask
;
1170 masklen2ip (p
->prefixlen
, &mask
);
1172 /* Put Network Mask. */
1173 stream_put_ipv4 (s
, mask
.s_addr
);
1176 stream_putc (s
, (u_char
) 0);
1179 stream_put_ospf_metric (s
, metric
);
1182 static struct ospf_lsa
*
1183 ospf_summary_lsa_new (struct ospf_area
*area
, struct prefix
*p
,
1184 u_int32_t metric
, struct in_addr id
)
1187 struct ospf_lsa
*new;
1188 struct lsa_header
*lsah
;
1191 if (id
.s_addr
== 0xffffffff)
1193 /* Maybe Link State ID not available. */
1194 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1195 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1200 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1201 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1203 /* Create new stream for LSA. */
1204 s
= stream_new (OSPF_MAX_LSA_SIZE
);
1205 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
1207 lsa_header_set (s
, LSA_OPTIONS_GET (area
), OSPF_SUMMARY_LSA
,
1208 id
, area
->ospf
->router_id
);
1210 /* Set summary-LSA body fields. */
1211 ospf_summary_lsa_body_set (s
, p
, metric
);
1214 length
= stream_get_endp (s
);
1215 lsah
->length
= htons (length
);
1217 /* Create OSPF LSA instance. */
1218 new = ospf_lsa_new ();
1220 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1222 /* Copy LSA to store. */
1223 new->data
= ospf_lsa_data_new (length
);
1224 memcpy (new->data
, lsah
, length
);
1230 /* Originate Summary-LSA. */
1232 ospf_summary_lsa_originate (struct prefix_ipv4
*p
, u_int32_t metric
,
1233 struct ospf_area
*area
)
1235 struct ospf_lsa
*new;
1238 id
= ospf_lsa_unique_id (area
->ospf
, area
->lsdb
, OSPF_SUMMARY_LSA
, p
);
1240 if (id
.s_addr
== 0xffffffff)
1242 /* Maybe Link State ID not available. */
1243 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1244 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1249 /* Create new summary-LSA instance. */
1250 if ( !(new = ospf_summary_lsa_new (area
, (struct prefix
*) p
, metric
, id
)))
1253 /* Instlal LSA to LSDB. */
1254 new = ospf_lsa_install (area
->ospf
, NULL
, new);
1256 /* Update LSA origination count. */
1257 area
->ospf
->lsa_originate_count
++;
1259 /* Flooding new LSA through area. */
1260 ospf_flood_through_area (area
, NULL
, new);
1262 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1264 zlog_debug ("LSA[Type%d:%s]: Originate summary-LSA %p",
1265 new->data
->type
, inet_ntoa (new->data
->id
), (void *)new);
1266 ospf_lsa_header_dump (new->data
);
1272 static struct ospf_lsa
*
1273 ospf_summary_lsa_refresh (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
1275 struct ospf_lsa
*new;
1276 struct summary_lsa
*sl
;
1282 sl
= (struct summary_lsa
*)lsa
->data
;
1283 p
.prefixlen
= ip_masklen (sl
->mask
);
1284 new = ospf_summary_lsa_new (lsa
->area
, &p
, GET_METRIC (sl
->metric
),
1290 new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
1292 ospf_lsa_install (ospf
, NULL
, new);
1294 /* Flood LSA through AS. */
1295 ospf_flood_through_area (new->area
, NULL
, new);
1297 /* Debug logging. */
1298 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1300 zlog_debug ("LSA[Type%d:%s]: summary-LSA refresh",
1301 new->data
->type
, inet_ntoa (new->data
->id
));
1302 ospf_lsa_header_dump (new->data
);
1309 /* summary-ASBR-LSA related functions. */
1311 ospf_summary_asbr_lsa_body_set (struct stream
*s
, struct prefix
*p
,
1314 /* Put Network Mask. */
1315 stream_put_ipv4 (s
, (u_int32_t
) 0);
1318 stream_putc (s
, (u_char
) 0);
1321 stream_put_ospf_metric (s
, metric
);
1324 static struct ospf_lsa
*
1325 ospf_summary_asbr_lsa_new (struct ospf_area
*area
, struct prefix
*p
,
1326 u_int32_t metric
, struct in_addr id
)
1329 struct ospf_lsa
*new;
1330 struct lsa_header
*lsah
;
1333 if (id
.s_addr
== 0xffffffff)
1335 /* Maybe Link State ID not available. */
1336 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1337 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1338 OSPF_ASBR_SUMMARY_LSA
);
1342 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1343 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1345 /* Create new stream for LSA. */
1346 s
= stream_new (OSPF_MAX_LSA_SIZE
);
1347 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
1349 lsa_header_set (s
, LSA_OPTIONS_GET (area
), OSPF_ASBR_SUMMARY_LSA
,
1350 id
, area
->ospf
->router_id
);
1352 /* Set summary-LSA body fields. */
1353 ospf_summary_asbr_lsa_body_set (s
, p
, metric
);
1356 length
= stream_get_endp (s
);
1357 lsah
->length
= htons (length
);
1359 /* Create OSPF LSA instance. */
1360 new = ospf_lsa_new ();
1362 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_SELF_CHECKED
);
1364 /* Copy LSA to store. */
1365 new->data
= ospf_lsa_data_new (length
);
1366 memcpy (new->data
, lsah
, length
);
1372 /* Originate summary-ASBR-LSA. */
1374 ospf_summary_asbr_lsa_originate (struct prefix_ipv4
*p
, u_int32_t metric
,
1375 struct ospf_area
*area
)
1377 struct ospf_lsa
*new;
1380 id
= ospf_lsa_unique_id (area
->ospf
, area
->lsdb
, OSPF_ASBR_SUMMARY_LSA
, p
);
1382 if (id
.s_addr
== 0xffffffff)
1384 /* Maybe Link State ID not available. */
1385 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1386 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1387 OSPF_ASBR_SUMMARY_LSA
);
1391 /* Create new summary-LSA instance. */
1392 new = ospf_summary_asbr_lsa_new (area
, (struct prefix
*) p
, metric
, id
);
1396 /* Install LSA to LSDB. */
1397 new = ospf_lsa_install (area
->ospf
, NULL
, new);
1399 /* Update LSA origination count. */
1400 area
->ospf
->lsa_originate_count
++;
1402 /* Flooding new LSA through area. */
1403 ospf_flood_through_area (area
, NULL
, new);
1405 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1407 zlog_debug ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1408 new->data
->type
, inet_ntoa (new->data
->id
), (void *)new);
1409 ospf_lsa_header_dump (new->data
);
1415 static struct ospf_lsa
*
1416 ospf_summary_asbr_lsa_refresh (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
1418 struct ospf_lsa
*new;
1419 struct summary_lsa
*sl
;
1425 sl
= (struct summary_lsa
*)lsa
->data
;
1426 p
.prefixlen
= ip_masklen (sl
->mask
);
1427 new = ospf_summary_asbr_lsa_new (lsa
->area
, &p
, GET_METRIC (sl
->metric
),
1432 new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
1434 ospf_lsa_install (ospf
, NULL
, new);
1436 /* Flood LSA through area. */
1437 ospf_flood_through_area (new->area
, NULL
, new);
1439 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1441 zlog_debug ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1442 new->data
->type
, inet_ntoa (new->data
->id
));
1443 ospf_lsa_header_dump (new->data
);
1449 /* AS-external-LSA related functions. */
1451 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1452 is connected, else 0*/
1453 static struct in_addr
1454 ospf_external_lsa_nexthop_get (struct ospf
*ospf
, struct in_addr nexthop
)
1458 struct listnode
*node
;
1459 struct ospf_interface
*oi
;
1463 if (!nexthop
.s_addr
)
1466 /* Check whether nexthop is covered by OSPF network. */
1467 nh
.family
= AF_INET
;
1468 nh
.u
.prefix4
= nexthop
;
1469 nh
.prefixlen
= IPV4_MAX_BITLEN
;
1471 /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
1472 * better to make use of the per-ifp table of ois.
1474 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
1475 if (if_is_operative (oi
->ifp
))
1476 if (oi
->address
->family
== AF_INET
)
1477 if (prefix_match (oi
->address
, &nh
))
1483 /* NSSA-external-LSA related functions. */
1485 /* Get 1st IP connection for Forward Addr */
1488 ospf_get_ip_from_ifp (struct ospf_interface
*oi
)
1494 if (if_is_operative (oi
->ifp
))
1495 return oi
->address
->u
.prefix4
;
1500 /* Get 1st IP connection for Forward Addr */
1502 ospf_get_nssa_ip (struct ospf_area
*area
)
1505 struct in_addr best_default
;
1506 struct listnode
*node
;
1507 struct ospf_interface
*oi
;
1510 best_default
.s_addr
= 0;
1512 for (ALL_LIST_ELEMENTS_RO (area
->ospf
->oiflist
, node
, oi
))
1514 if (if_is_operative (oi
->ifp
))
1515 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1516 if (oi
->address
&& oi
->address
->family
== AF_INET
)
1518 if (best_default
.s_addr
== 0)
1519 best_default
= oi
->address
->u
.prefix4
;
1520 if (oi
->area
== area
)
1521 return oi
->address
->u
.prefix4
;
1524 if (best_default
.s_addr
!= 0)
1525 return best_default
;
1527 if (best_default
.s_addr
!= 0)
1528 return best_default
;
1533 #define DEFAULT_DEFAULT_METRIC 20
1534 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1535 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1537 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1540 metric_type (struct ospf
*ospf
, u_char src
, u_short instance
)
1542 struct ospf_redist
*red
;
1544 red
= ospf_redist_lookup(ospf
, src
, instance
);
1546 return ((!red
|| red
->dmetric
.type
< 0) ?
1547 DEFAULT_METRIC_TYPE
: red
->dmetric
.type
);
1551 metric_value (struct ospf
*ospf
, u_char src
, u_short instance
)
1553 struct ospf_redist
*red
;
1555 red
= ospf_redist_lookup(ospf
, src
, instance
);
1556 if (!red
|| red
->dmetric
.value
< 0)
1558 if (src
== DEFAULT_ROUTE
)
1560 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ZEBRA
)
1561 return DEFAULT_DEFAULT_ORIGINATE_METRIC
;
1563 return DEFAULT_DEFAULT_ALWAYS_METRIC
;
1565 else if (ospf
->default_metric
< 0)
1566 return DEFAULT_DEFAULT_METRIC
;
1568 return ospf
->default_metric
;
1571 return red
->dmetric
.value
;
1574 /* Set AS-external-LSA body. */
1576 ospf_external_lsa_body_set (struct stream
*s
, struct external_info
*ei
,
1579 struct prefix_ipv4
*p
= &ei
->p
;
1580 struct in_addr mask
, fwd_addr
;
1586 /* Put Network Mask. */
1587 masklen2ip (p
->prefixlen
, &mask
);
1588 stream_put_ipv4 (s
, mask
.s_addr
);
1590 /* If prefix is default, specify DEFAULT_ROUTE. */
1591 type
= is_prefix_default (&ei
->p
) ? DEFAULT_ROUTE
: ei
->type
;
1592 instance
= is_prefix_default (&ei
->p
) ? 0 : ei
->instance
;
1594 mtype
= (ROUTEMAP_METRIC_TYPE (ei
) != -1) ?
1595 ROUTEMAP_METRIC_TYPE (ei
) : metric_type (ospf
, type
, instance
);
1597 mvalue
= (ROUTEMAP_METRIC (ei
) != -1) ?
1598 ROUTEMAP_METRIC (ei
) : metric_value (ospf
, type
, instance
);
1600 /* Put type of external metric. */
1601 stream_putc (s
, (mtype
== EXTERNAL_METRIC_TYPE_2
? 0x80 : 0));
1603 /* Put 0 metric. TOS metric is not supported. */
1604 stream_put_ospf_metric (s
, mvalue
);
1606 /* Get forwarding address to nexthop if on the Connection List, else 0. */
1607 fwd_addr
= ospf_external_lsa_nexthop_get (ospf
, ei
->nexthop
);
1609 /* Put forwarding address. */
1610 stream_put_ipv4 (s
, fwd_addr
.s_addr
);
1613 stream_putl (s
, ei
->tag
);
1616 /* Create new external-LSA. */
1617 static struct ospf_lsa
*
1618 ospf_external_lsa_new (struct ospf
*ospf
,
1619 struct external_info
*ei
, struct in_addr
*old_id
)
1622 struct lsa_header
*lsah
;
1623 struct ospf_lsa
*new;
1629 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1630 zlog_debug ("LSA[Type5]: External info is NULL, can't originate");
1634 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1635 zlog_debug ("LSA[Type5]: Originate AS-external-LSA instance");
1637 /* If old Link State ID is specified, refresh LSA with same ID. */
1640 /* Get Link State with unique ID. */
1643 id
= ospf_lsa_unique_id (ospf
, ospf
->lsdb
, OSPF_AS_EXTERNAL_LSA
, &ei
->p
);
1644 if (id
.s_addr
== 0xffffffff)
1646 /* Maybe Link State ID not available. */
1647 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
1648 zlog_debug ("LSA[Type5]: Link ID not available, can't originate");
1653 /* Create new stream for LSA. */
1654 s
= stream_new (OSPF_MAX_LSA_SIZE
);
1655 lsah
= (struct lsa_header
*) STREAM_DATA (s
);
1657 /* Set LSA common header fields. */
1658 lsa_header_set (s
, OSPF_OPTION_E
, OSPF_AS_EXTERNAL_LSA
,
1659 id
, ospf
->router_id
);
1661 /* Set AS-external-LSA body fields. */
1662 ospf_external_lsa_body_set (s
, ei
, ospf
);
1665 length
= stream_get_endp (s
);
1666 lsah
->length
= htons (length
);
1668 /* Now, create OSPF LSA instance. */
1669 new = ospf_lsa_new ();
1671 SET_FLAG (new->flags
, OSPF_LSA_SELF
| OSPF_LSA_APPROVED
| OSPF_LSA_SELF_CHECKED
);
1673 /* Copy LSA data to store, discard stream. */
1674 new->data
= ospf_lsa_data_new (length
);
1675 memcpy (new->data
, lsah
, length
);
1683 ospf_install_flood_nssa (struct ospf
*ospf
,
1684 struct ospf_lsa
*lsa
, struct external_info
*ei
)
1686 struct ospf_lsa
*new;
1687 struct as_external_lsa
*extlsa
;
1688 struct ospf_area
*area
;
1689 struct listnode
*node
, *nnode
;
1691 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1692 * which originated from an NSSA area. In which case it should not be
1693 * flooded back to NSSA areas.
1695 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
1698 /* NSSA Originate or Refresh (If anyNSSA)
1700 LSA is self-originated. And just installed as Type-5.
1701 Additionally, install as Type-7 LSDB for every attached NSSA.
1703 P-Bit controls which ABR performs translation to outside world; If
1704 we are an ABR....do not set the P-bit, because we send the Type-5,
1705 not as the ABR Translator, but as the ASBR owner within the AS!
1707 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1708 elected ABR Translator will see the P-bit, Translate, and re-flood.
1710 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1711 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1713 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
1715 /* Don't install Type-7 LSA's into nonNSSA area */
1716 if (area
->external_routing
!= OSPF_AREA_NSSA
)
1719 /* make lsa duplicate, lock=1 */
1720 new = ospf_lsa_dup (lsa
);
1722 new->data
->type
= OSPF_AS_NSSA_LSA
;
1724 /* set P-bit if not ABR */
1725 if (! IS_OSPF_ABR (ospf
))
1727 SET_FLAG(new->data
->options
, OSPF_OPTION_NP
);
1729 /* set non-zero FWD ADDR
1731 draft-ietf-ospf-nssa-update-09.txt
1733 if the network between the NSSA AS boundary router and the
1734 adjacent AS is advertised into OSPF as an internal OSPF route,
1735 the forwarding address should be the next op address as is cu
1736 currently done with type-5 LSAs. If the intervening network is
1737 not adversited into OSPF as an internal OSPF route and the
1738 type-7 LSA's P-bit is set a forwarding address should be
1739 selected from one of the router's active OSPF inteface addresses
1740 which belong to the NSSA. If no such addresses exist, then
1741 no type-7 LSA's with the P-bit set should originate from this
1744 /* kevinm: not updating lsa anymore, just new */
1745 extlsa
= (struct as_external_lsa
*)(new->data
);
1747 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0)
1748 extlsa
->e
[0].fwd_addr
= ospf_get_nssa_ip(area
); /* this NSSA area in ifp */
1750 if (extlsa
->e
[0].fwd_addr
.s_addr
== 0)
1752 if (IS_DEBUG_OSPF_NSSA
)
1753 zlog_debug ("LSA[Type-7]: Could not build FWD-ADDR");
1754 ospf_lsa_discard (new);
1759 /* install also as Type-7 */
1760 ospf_lsa_install (ospf
, NULL
, new); /* Remove Old, Lock New = 2 */
1762 /* will send each copy, lock=2+n */
1763 ospf_flood_through_as (ospf
, NULL
, new); /* all attached NSSA's, no AS/STUBs */
1767 static struct ospf_lsa
*
1768 ospf_lsa_translated_nssa_new (struct ospf
*ospf
,
1769 struct ospf_lsa
*type7
)
1772 struct ospf_lsa
*new;
1773 struct as_external_lsa
*ext
, *extnew
;
1774 struct external_info ei
;
1776 ext
= (struct as_external_lsa
*)(type7
->data
);
1778 /* need external_info struct, fill in bare minimum */
1779 ei
.p
.family
= AF_INET
;
1780 ei
.p
.prefix
= type7
->data
->id
;
1781 ei
.p
.prefixlen
= ip_masklen (ext
->mask
);
1782 ei
.type
= ZEBRA_ROUTE_OSPF
;
1783 ei
.nexthop
= ext
->header
.adv_router
;
1784 ei
.route_map_set
.metric
= -1;
1785 ei
.route_map_set
.metric_type
= -1;
1788 if ( (new = ospf_external_lsa_new (ospf
, &ei
, &type7
->data
->id
)) == NULL
)
1790 if (IS_DEBUG_OSPF_NSSA
)
1791 zlog_debug ("ospf_nssa_translate_originate(): Could not originate "
1792 "Translated Type-5 for %s",
1793 inet_ntoa (ei
.p
.prefix
));
1797 extnew
= (struct as_external_lsa
*)(new->data
);
1799 /* copy over Type-7 data to new */
1800 extnew
->e
[0].tos
= ext
->e
[0].tos
;
1801 extnew
->e
[0].route_tag
= ext
->e
[0].route_tag
;
1802 extnew
->e
[0].fwd_addr
.s_addr
= ext
->e
[0].fwd_addr
.s_addr
;
1803 new->data
->ls_seqnum
= type7
->data
->ls_seqnum
;
1805 /* add translated flag, checksum and lock new lsa */
1806 SET_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
); /* Translated from 7 */
1807 new = ospf_lsa_lock (new);
1812 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1814 ospf_translated_nssa_originate (struct ospf
*ospf
, struct ospf_lsa
*type7
)
1816 struct ospf_lsa
*new;
1817 struct as_external_lsa
*extnew
;
1819 /* we cant use ospf_external_lsa_originate() as we need to set
1820 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1823 if ( (new = ospf_lsa_translated_nssa_new (ospf
, type7
)) == NULL
)
1825 if (IS_DEBUG_OSPF_NSSA
)
1826 zlog_debug ("ospf_translated_nssa_originate(): Could not translate "
1827 "Type-7, Id %s, to Type-5",
1828 inet_ntoa (type7
->data
->id
));
1832 extnew
= (struct as_external_lsa
*)new;
1834 if (IS_DEBUG_OSPF_NSSA
)
1836 zlog_debug ("ospf_translated_nssa_originate(): "
1837 "translated Type 7, installed:");
1838 ospf_lsa_header_dump (new->data
);
1839 zlog_debug (" Network mask: %d",ip_masklen (extnew
->mask
));
1840 zlog_debug (" Forward addr: %s", inet_ntoa (extnew
->e
[0].fwd_addr
));
1843 if ( (new = ospf_lsa_install (ospf
, NULL
, new)) == NULL
)
1845 if (IS_DEBUG_OSPF_NSSA
)
1846 zlog_debug ("ospf_lsa_translated_nssa_originate(): "
1847 "Could not install LSA "
1848 "id %s", inet_ntoa (type7
->data
->id
));
1852 ospf
->lsa_originate_count
++;
1853 ospf_flood_through_as (ospf
, NULL
, new);
1858 /* Refresh Translated from NSSA AS-external-LSA. */
1860 ospf_translated_nssa_refresh (struct ospf
*ospf
, struct ospf_lsa
*type7
,
1861 struct ospf_lsa
*type5
)
1863 struct ospf_lsa
*new = NULL
;
1865 /* Sanity checks. */
1866 assert (type7
|| type5
);
1867 if (!(type7
|| type5
))
1870 assert (type7
->data
);
1872 assert (type5
->data
);
1873 assert (ospf
->anyNSSA
);
1875 /* get required data according to what has been given */
1876 if (type7
&& type5
== NULL
)
1878 /* find the translated Type-5 for this Type-7 */
1879 struct as_external_lsa
*ext
= (struct as_external_lsa
*)(type7
->data
);
1880 struct prefix_ipv4 p
=
1882 .prefix
= type7
->data
->id
,
1883 .prefixlen
= ip_masklen (ext
->mask
),
1887 type5
= ospf_external_info_find_lsa (ospf
, &p
);
1889 else if (type5
&& type7
== NULL
)
1891 /* find the type-7 from which supplied type-5 was translated,
1892 * ie find first type-7 with same LSA Id.
1894 struct listnode
*ln
, *lnn
;
1895 struct route_node
*rn
;
1896 struct ospf_lsa
*lsa
;
1897 struct ospf_area
*area
;
1899 for (ALL_LIST_ELEMENTS (ospf
->areas
, ln
, lnn
, area
))
1901 if (area
->external_routing
!= OSPF_AREA_NSSA
1905 LSDB_LOOP (NSSA_LSDB(area
), rn
, lsa
)
1907 if (lsa
->data
->id
.s_addr
== type5
->data
->id
.s_addr
)
1916 /* do we have type7? */
1919 if (IS_DEBUG_OSPF_NSSA
)
1920 zlog_debug ("ospf_translated_nssa_refresh(): no Type-7 found for "
1922 inet_ntoa (type5
->data
->id
));
1926 /* do we have valid translated type5? */
1927 if (type5
== NULL
|| !CHECK_FLAG (type5
->flags
, OSPF_LSA_LOCAL_XLT
) )
1929 if (IS_DEBUG_OSPF_NSSA
)
1930 zlog_debug ("ospf_translated_nssa_refresh(): No translated Type-5 "
1931 "found for Type-7 with Id %s",
1932 inet_ntoa (type7
->data
->id
));
1936 /* Delete LSA from neighbor retransmit-list. */
1937 ospf_ls_retransmit_delete_nbr_as (ospf
, type5
);
1939 /* create new translated LSA */
1940 if ( (new = ospf_lsa_translated_nssa_new (ospf
, type7
)) == NULL
)
1942 if (IS_DEBUG_OSPF_NSSA
)
1943 zlog_debug ("ospf_translated_nssa_refresh(): Could not translate "
1944 "Type-7 for %s to Type-5",
1945 inet_ntoa (type7
->data
->id
));
1949 if ( !(new = ospf_lsa_install (ospf
, NULL
, new)) )
1951 if (IS_DEBUG_OSPF_NSSA
)
1952 zlog_debug ("ospf_translated_nssa_refresh(): Could not install "
1953 "translated LSA, Id %s",
1954 inet_ntoa (type7
->data
->id
));
1958 /* Flood LSA through area. */
1959 ospf_flood_through_as (ospf
, NULL
, new);
1965 is_prefix_default (struct prefix_ipv4
*p
)
1967 struct prefix_ipv4 q
;
1970 q
.prefix
.s_addr
= 0;
1973 return prefix_same ((struct prefix
*) p
, (struct prefix
*) &q
);
1976 /* Originate an AS-external-LSA, install and flood. */
1978 ospf_external_lsa_originate (struct ospf
*ospf
, struct external_info
*ei
)
1980 struct ospf_lsa
*new;
1982 /* Added for NSSA project....
1984 External LSAs are originated in ASBRs as usual, but for NSSA systems.
1985 there is the global Type-5 LSDB and a Type-7 LSDB installed for
1986 every area. The Type-7's are flooded to every IR and every ABR; We
1987 install the Type-5 LSDB so that the normal "refresh" code operates
1988 as usual, and flag them as not used during ASE calculations. The
1989 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
1990 Address of non-zero.
1992 If an ABR is the elected NSSA translator, following SPF and during
1993 the ABR task it will translate all the scanned Type-7's, with P-bit
1994 ON and not-self generated, and translate to Type-5's throughout the
1997 A difference in operation depends whether this ASBR is an ABR
1998 or not. If not an ABR, the P-bit is ON, to indicate that any
1999 elected NSSA-ABR can perform its translation.
2001 If an ABR, the P-bit is OFF; No ABR will perform translation and
2002 this ASBR will flood the Type-5 LSA as usual.
2004 For the case where this ASBR is not an ABR, the ASE calculations
2005 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2006 demonstrate to the user that there are LSA's that belong to any
2009 Finally, it just so happens that when the ABR is translating every
2010 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2011 approved Type-5 (translated from Type-7); at the end of translation
2012 if any Translated Type-5's remain unapproved, then they must be
2013 flushed from the AS.
2017 /* Check the AS-external-LSA should be originated. */
2018 if (!ospf_redistribute_check (ospf
, ei
, NULL
))
2021 /* Create new AS-external-LSA instance. */
2022 if ((new = ospf_external_lsa_new (ospf
, ei
, NULL
)) == NULL
)
2024 if (IS_DEBUG_OSPF_EVENT
)
2025 zlog_debug ("LSA[Type5:%s]: Could not originate AS-external-LSA",
2026 inet_ntoa (ei
->p
.prefix
));
2030 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2031 ospf_lsa_install (ospf
, NULL
, new);
2033 /* Update LSA origination count. */
2034 ospf
->lsa_originate_count
++;
2036 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2037 ospf_flood_through_as (ospf
, NULL
, new);
2039 /* If there is any attached NSSA, do special handling */
2040 if (ospf
->anyNSSA
&&
2041 /* stay away from translated LSAs! */
2042 !(CHECK_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
)))
2043 ospf_install_flood_nssa (ospf
, new, ei
); /* Install/Flood Type-7 to all NSSAs */
2045 /* Debug logging. */
2046 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2048 zlog_debug ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2049 new->data
->type
, inet_ntoa (new->data
->id
), (void *)new);
2050 ospf_lsa_header_dump (new->data
);
2056 /* Originate AS-external-LSA from external info with initial flag. */
2058 ospf_external_lsa_originate_timer (struct thread
*thread
)
2060 struct ospf
*ospf
= THREAD_ARG (thread
);
2061 struct route_node
*rn
;
2062 struct external_info
*ei
;
2063 struct route_table
*rt
;
2064 int type
= THREAD_VAL (thread
);
2065 struct list
*ext_list
;
2066 struct listnode
*node
;
2067 struct ospf_external
*ext
;
2069 ospf
->t_external_lsa
= NULL
;
2071 ext_list
= om
->external
[type
];
2075 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
2076 /* Originate As-external-LSA from all type of distribute source. */
2077 if ((rt
= ext
->external_info
))
2078 for (rn
= route_top (rt
); rn
; rn
= route_next (rn
))
2079 if ((ei
= rn
->info
) != NULL
)
2080 if (!is_prefix_default ((struct prefix_ipv4
*)&ei
->p
))
2081 if (!ospf_external_lsa_originate (ospf
, ei
))
2082 zlog_warn ("LSA: AS-external-LSA was not originated.");
2087 static struct external_info
*
2088 ospf_default_external_info (struct ospf
*ospf
)
2091 struct route_node
*rn
;
2092 struct prefix_ipv4 p
;
2095 p
.prefix
.s_addr
= 0;
2098 /* First, lookup redistributed default route. */
2099 for (type
= 0; type
<= ZEBRA_ROUTE_MAX
; type
++)
2101 struct list
*ext_list
;
2102 struct listnode
*node
;
2103 struct ospf_external
*ext
;
2105 if (type
== ZEBRA_ROUTE_OSPF
)
2108 ext_list
= om
->external
[type
];
2112 for (ALL_LIST_ELEMENTS_RO(ext_list
, node
, ext
))
2114 rn
= route_node_lookup (ext
->external_info
, (struct prefix
*) &p
);
2117 route_unlock_node (rn
);
2119 if (ospf_redistribute_check (ospf
, rn
->info
, NULL
))
2129 ospf_default_originate_timer (struct thread
*thread
)
2131 struct prefix_ipv4 p
;
2132 struct in_addr nexthop
;
2133 struct external_info
*ei
;
2136 ospf
= THREAD_ARG (thread
);
2139 p
.prefix
.s_addr
= 0;
2142 if (ospf
->default_originate
== DEFAULT_ORIGINATE_ALWAYS
)
2144 /* If there is no default route via redistribute,
2145 then originate AS-external-LSA with nexthop 0 (self). */
2147 ospf_external_info_add (DEFAULT_ROUTE
, 0, p
, 0, nexthop
, 0);
2150 if ((ei
= ospf_default_external_info (ospf
)))
2151 ospf_external_lsa_originate (ospf
, ei
);
2156 /* Flush any NSSA LSAs for given prefix */
2158 ospf_nssa_lsa_flush (struct ospf
*ospf
, struct prefix_ipv4
*p
)
2160 struct listnode
*node
, *nnode
;
2161 struct ospf_lsa
*lsa
;
2162 struct ospf_area
*area
;
2164 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
2166 if (area
->external_routing
== OSPF_AREA_NSSA
)
2168 if (!(lsa
= ospf_lsa_lookup (area
, OSPF_AS_NSSA_LSA
, p
->prefix
,
2171 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2172 zlog_debug ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2173 inet_ntoa (p
->prefix
), p
->prefixlen
);
2176 ospf_ls_retransmit_delete_nbr_area (area
, lsa
);
2177 if (!IS_LSA_MAXAGE (lsa
))
2179 ospf_refresher_unregister_lsa (ospf
, lsa
);
2180 ospf_lsa_flush_area (lsa
, area
);
2186 /* Flush an AS-external-LSA from LSDB and routing domain. */
2188 ospf_external_lsa_flush (struct ospf
*ospf
,
2189 u_char type
, struct prefix_ipv4
*p
,
2190 ifindex_t ifindex
/*, struct in_addr nexthop */)
2192 struct ospf_lsa
*lsa
;
2194 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2195 zlog_debug ("LSA: Flushing AS-external-LSA %s/%d",
2196 inet_ntoa (p
->prefix
), p
->prefixlen
);
2198 /* First lookup LSA from LSDB. */
2199 if (!(lsa
= ospf_external_info_find_lsa (ospf
, p
)))
2201 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2202 zlog_debug ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
2203 inet_ntoa (p
->prefix
), p
->prefixlen
);
2207 /* If LSA is selforiginated, not a translated LSA, and there is
2208 * NSSA area, flush Type-7 LSA's at first.
2210 if (IS_LSA_SELF(lsa
) && (ospf
->anyNSSA
)
2211 && !(CHECK_FLAG (lsa
->flags
, OSPF_LSA_LOCAL_XLT
)))
2212 ospf_nssa_lsa_flush (ospf
, p
);
2214 /* Sweep LSA from Link State Retransmit List. */
2215 ospf_ls_retransmit_delete_nbr_as (ospf
, lsa
);
2217 /* There must be no self-originated LSA in rtrs_external. */
2219 /* Remove External route from Zebra. */
2220 ospf_zebra_delete ((struct prefix_ipv4
*) p
, &nexthop
);
2223 if (!IS_LSA_MAXAGE (lsa
))
2225 /* Unregister LSA from Refresh queue. */
2226 ospf_refresher_unregister_lsa (ospf
, lsa
);
2228 /* Flush AS-external-LSA through AS. */
2229 ospf_lsa_flush_as (ospf
, lsa
);
2232 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2233 zlog_debug ("ospf_external_lsa_flush(): stop");
2237 ospf_external_lsa_refresh_default (struct ospf
*ospf
)
2239 struct prefix_ipv4 p
;
2240 struct external_info
*ei
;
2241 struct ospf_lsa
*lsa
;
2245 p
.prefix
.s_addr
= 0;
2247 ei
= ospf_default_external_info (ospf
);
2248 lsa
= ospf_external_info_find_lsa (ospf
, &p
);
2254 if (IS_DEBUG_OSPF_EVENT
)
2255 zlog_debug ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p",
2257 ospf_external_lsa_refresh (ospf
, lsa
, ei
, LSA_REFRESH_FORCE
);
2261 if (IS_DEBUG_OSPF_EVENT
)
2262 zlog_debug ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2263 ospf_external_lsa_originate (ospf
, ei
);
2270 if (IS_DEBUG_OSPF_EVENT
)
2271 zlog_debug ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2272 ospf_refresher_unregister_lsa (ospf
, lsa
);
2273 ospf_lsa_flush_as (ospf
, lsa
);
2279 ospf_external_lsa_refresh_type (struct ospf
*ospf
, u_char type
, u_short instance
,
2282 struct route_node
*rn
;
2283 struct external_info
*ei
;
2284 struct ospf_external
*ext
;
2286 if (type
!= DEFAULT_ROUTE
)
2287 if ((ext
= ospf_external_lookup(type
, instance
)) &&
2288 EXTERNAL_INFO (ext
))
2289 /* Refresh each redistributed AS-external-LSAs. */
2290 for (rn
= route_top (EXTERNAL_INFO (ext
)); rn
; rn
= route_next (rn
))
2291 if ((ei
= rn
->info
))
2292 if (!is_prefix_default (&ei
->p
))
2294 struct ospf_lsa
*lsa
;
2296 if ((lsa
= ospf_external_info_find_lsa (ospf
, &ei
->p
)))
2297 ospf_external_lsa_refresh (ospf
, lsa
, ei
, force
);
2299 ospf_external_lsa_originate (ospf
, ei
);
2303 /* Refresh AS-external-LSA. */
2305 ospf_external_lsa_refresh (struct ospf
*ospf
, struct ospf_lsa
*lsa
,
2306 struct external_info
*ei
, int force
)
2308 struct ospf_lsa
*new;
2311 /* Check the AS-external-LSA should be originated. */
2312 if (!ospf_redistribute_check (ospf
, ei
, &changed
))
2314 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2315 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed, "
2316 "redist check fail",
2317 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2318 ospf_external_lsa_flush (ospf
, ei
->type
, &ei
->p
,
2319 ei
->ifindex
/*, ei->nexthop */);
2323 if (!changed
&& !force
)
2325 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2326 zlog_debug ("LSA[Type%d:%s]: Not refreshed, not changed/forced",
2327 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2331 /* Delete LSA from neighbor retransmit-list. */
2332 ospf_ls_retransmit_delete_nbr_as (ospf
, lsa
);
2334 /* Unregister AS-external-LSA from refresh-list. */
2335 ospf_refresher_unregister_lsa (ospf
, lsa
);
2337 new = ospf_external_lsa_new (ospf
, ei
, &lsa
->data
->id
);
2341 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2342 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed", lsa
->data
->type
,
2343 inet_ntoa (lsa
->data
->id
));
2347 new->data
->ls_seqnum
= lsa_seqnum_increment (lsa
);
2349 ospf_lsa_install (ospf
, NULL
, new); /* As type-5. */
2351 /* Flood LSA through AS. */
2352 ospf_flood_through_as (ospf
, NULL
, new);
2354 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2355 if (ospf
->anyNSSA
&& !(CHECK_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
)))
2356 ospf_install_flood_nssa (ospf
, new, ei
); /* Install/Flood per new rules */
2358 /* Register self-originated LSA to refresh queue.
2359 * Translated LSAs should not be registered, but refreshed upon
2360 * refresh of the Type-7
2362 if ( !CHECK_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
) )
2363 ospf_refresher_register_lsa (ospf
, new);
2365 /* Debug logging. */
2366 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2368 zlog_debug ("LSA[Type%d:%s]: AS-external-LSA refresh",
2369 new->data
->type
, inet_ntoa (new->data
->id
));
2370 ospf_lsa_header_dump (new->data
);
2377 /* LSA installation functions. */
2379 /* Install router-LSA to an area. */
2380 static struct ospf_lsa
*
2381 ospf_router_lsa_install (struct ospf
*ospf
, struct ospf_lsa
*new,
2384 struct ospf_area
*area
= new->area
;
2386 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2387 The entire routing table must be recalculated, starting with
2388 the shortest path calculations for each area (not just the
2389 area whose link-state database has changed).
2392 if (IS_LSA_SELF (new))
2395 /* Only install LSA if it is originated/refreshed by us.
2396 * If LSA was received by flooding, the RECEIVED flag is set so do
2397 * not link the LSA */
2398 if (CHECK_FLAG (new->flags
, OSPF_LSA_RECEIVED
))
2399 return new; /* ignore stale LSA */
2401 /* Set self-originated router-LSA. */
2402 ospf_lsa_unlock (&area
->router_lsa_self
);
2403 area
->router_lsa_self
= ospf_lsa_lock (new);
2405 ospf_refresher_register_lsa (ospf
, new);
2408 ospf_spf_calculate_schedule (ospf
, SPF_FLAG_ROUTER_LSA_INSTALL
);
2412 #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
2414 (T) = thread_add_timer (master, (F), oi, (V))
2416 /* Install network-LSA to an area. */
2417 static struct ospf_lsa
*
2418 ospf_network_lsa_install (struct ospf
*ospf
,
2419 struct ospf_interface
*oi
,
2420 struct ospf_lsa
*new,
2424 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2425 The entire routing table must be recalculated, starting with
2426 the shortest path calculations for each area (not just the
2427 area whose link-state database has changed).
2429 if (IS_LSA_SELF (new))
2431 /* We supposed that when LSA is originated by us, we pass the int
2432 for which it was originated. If LSA was received by flooding,
2433 the RECEIVED flag is set, so we do not link the LSA to the int. */
2434 if (CHECK_FLAG (new->flags
, OSPF_LSA_RECEIVED
))
2435 return new; /* ignore stale LSA */
2437 ospf_lsa_unlock (&oi
->network_lsa_self
);
2438 oi
->network_lsa_self
= ospf_lsa_lock (new);
2439 ospf_refresher_register_lsa (ospf
, new);
2442 ospf_spf_calculate_schedule (ospf
, SPF_FLAG_NETWORK_LSA_INSTALL
);
2447 /* Install summary-LSA to an area. */
2448 static struct ospf_lsa
*
2449 ospf_summary_lsa_install (struct ospf
*ospf
, struct ospf_lsa
*new,
2452 if (rt_recalc
&& !IS_LSA_SELF (new))
2454 /* RFC 2328 Section 13.2 Summary-LSAs
2455 The best route to the destination described by the summary-
2456 LSA must be recalculated (see Section 16.5). If this
2457 destination is an AS boundary router, it may also be
2458 necessary to re-examine all the AS-external-LSAs.
2462 /* This doesn't exist yet... */
2463 ospf_summary_incremental_update(new); */
2465 ospf_spf_calculate_schedule (ospf
, SPF_FLAG_SUMMARY_LSA_INSTALL
);
2470 if (IS_LSA_SELF (new))
2471 ospf_refresher_register_lsa (ospf
, new);
2476 /* Install ASBR-summary-LSA to an area. */
2477 static struct ospf_lsa
*
2478 ospf_summary_asbr_lsa_install (struct ospf
*ospf
, struct ospf_lsa
*new,
2481 if (rt_recalc
&& !IS_LSA_SELF (new))
2483 /* RFC 2328 Section 13.2 Summary-LSAs
2484 The best route to the destination described by the summary-
2485 LSA must be recalculated (see Section 16.5). If this
2486 destination is an AS boundary router, it may also be
2487 necessary to re-examine all the AS-external-LSAs.
2490 /* These don't exist yet... */
2491 ospf_summary_incremental_update(new);
2492 /* Isn't this done by the above call?
2493 - RFC 2328 Section 16.5 implies it should be */
2494 /* ospf_ase_calculate_schedule(); */
2496 ospf_spf_calculate_schedule (ospf
, SPF_FLAG_ASBR_SUMMARY_LSA_INSTALL
);
2500 /* register LSA to refresh-list. */
2501 if (IS_LSA_SELF (new))
2502 ospf_refresher_register_lsa (ospf
, new);
2507 /* Install AS-external-LSA. */
2508 static struct ospf_lsa
*
2509 ospf_external_lsa_install (struct ospf
*ospf
, struct ospf_lsa
*new,
2512 ospf_ase_register_external_lsa (new, ospf
);
2513 /* If LSA is not self-originated, calculate an external route. */
2516 /* RFC 2328 Section 13.2 AS-external-LSAs
2517 The best route to the destination described by the AS-
2518 external-LSA must be recalculated (see Section 16.6).
2521 if (!IS_LSA_SELF (new))
2522 ospf_ase_incremental_update (ospf
, new);
2525 if (new->data
->type
== OSPF_AS_NSSA_LSA
)
2527 /* There is no point to register selforiginate Type-7 LSA for
2528 * refreshing. We rely on refreshing Type-5 LSA's
2530 if (IS_LSA_SELF (new))
2534 /* Try refresh type-5 translated LSA for this LSA, if one exists.
2535 * New translations will be taken care of by the abr_task.
2537 ospf_translated_nssa_refresh (ospf
, new, NULL
);
2541 /* Register self-originated LSA to refresh queue.
2542 * Leave Translated LSAs alone if NSSA is enabled
2544 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags
, OSPF_LSA_LOCAL_XLT
) )
2545 ospf_refresher_register_lsa (ospf
, new);
2551 ospf_discard_from_db (struct ospf
*ospf
,
2552 struct ospf_lsdb
*lsdb
, struct ospf_lsa
*lsa
)
2554 struct ospf_lsa
*old
;
2558 zlog_warn ("%s: Called with NULL lsdb!", __func__
);
2560 zlog_warn ("%s: and NULL LSA!", __func__
);
2562 zlog_warn ("LSA[Type%d:%s]: not associated with LSDB!",
2563 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2567 old
= ospf_lsdb_lookup (lsdb
, lsa
);
2572 if (old
->refresh_list
>= 0)
2573 ospf_refresher_unregister_lsa (ospf
, old
);
2575 switch (old
->data
->type
)
2577 case OSPF_AS_EXTERNAL_LSA
:
2578 ospf_ase_unregister_external_lsa (old
, ospf
);
2579 ospf_ls_retransmit_delete_nbr_as (ospf
, old
);
2581 case OSPF_OPAQUE_AS_LSA
:
2582 ospf_ls_retransmit_delete_nbr_as (ospf
, old
);
2584 case OSPF_AS_NSSA_LSA
:
2585 ospf_ls_retransmit_delete_nbr_area (old
->area
, old
);
2586 ospf_ase_unregister_external_lsa (old
, ospf
);
2589 ospf_ls_retransmit_delete_nbr_area (old
->area
, old
);
2593 ospf_lsa_maxage_delete (ospf
, old
);
2594 ospf_lsa_discard (old
);
2598 ospf_lsa_install (struct ospf
*ospf
, struct ospf_interface
*oi
,
2599 struct ospf_lsa
*lsa
)
2601 struct ospf_lsa
*new = NULL
;
2602 struct ospf_lsa
*old
= NULL
;
2603 struct ospf_lsdb
*lsdb
= NULL
;
2607 switch (lsa
->data
->type
)
2610 case OSPF_AS_NSSA_LSA
:
2612 lsdb
= lsa
->area
->lsdb
;
2616 case OSPF_AS_EXTERNAL_LSA
:
2617 case OSPF_OPAQUE_AS_LSA
:
2621 lsdb
= lsa
->area
->lsdb
;
2627 /* RFC 2328 13.2. Installing LSAs in the database
2629 Installing a new LSA in the database, either as the result of
2630 flooding or a newly self-originated LSA, may cause the OSPF
2631 routing table structure to be recalculated. The contents of the
2632 new LSA should be compared to the old instance, if present. If
2633 there is no difference, there is no need to recalculate the
2634 routing table. When comparing an LSA to its previous instance,
2635 the following are all considered to be differences in contents:
2637 o The LSA's Options field has changed.
2639 o One of the LSA instances has LS age set to MaxAge, and
2642 o The length field in the LSA header has changed.
2644 o The body of the LSA (i.e., anything outside the 20-byte
2645 LSA header) has changed. Note that this excludes changes
2646 in LS Sequence Number and LS Checksum.
2649 /* Look up old LSA and determine if any SPF calculation or incremental
2651 old
= ospf_lsdb_lookup (lsdb
, lsa
);
2653 /* Do comparision and record if recalc needed. */
2655 if ( old
== NULL
|| ospf_lsa_different(old
, lsa
))
2659 Sequence number check (Section 14.1 of rfc 2328)
2660 "Premature aging is used when it is time for a self-originated
2661 LSA's sequence number field to wrap. At this point, the current
2662 LSA instance (having LS sequence number MaxSequenceNumber) must
2663 be prematurely aged and flushed from the routing domain before a
2664 new instance with sequence number equal to InitialSequenceNumber
2665 can be originated. "
2668 if (ntohl(lsa
->data
->ls_seqnum
) - 1 == OSPF_MAX_SEQUENCE_NUMBER
)
2670 if (ospf_lsa_is_self_originated(ospf
, lsa
))
2672 lsa
->data
->ls_seqnum
= htonl(OSPF_MAX_SEQUENCE_NUMBER
);
2674 if (!IS_LSA_MAXAGE(lsa
))
2675 lsa
->flags
|= OSPF_LSA_PREMATURE_AGE
;
2676 lsa
->data
->ls_age
= htons (OSPF_LSA_MAXAGE
);
2678 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
2680 zlog_debug ("ospf_lsa_install() Premature Aging "
2681 "lsa 0x%p, seqnum 0x%x",
2682 (void *)lsa
, ntohl(lsa
->data
->ls_seqnum
));
2683 ospf_lsa_header_dump (lsa
->data
);
2688 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
2690 zlog_debug ("ospf_lsa_install() got an lsa with seq 0x80000000 "
2691 "that was not self originated. Ignoring\n");
2692 ospf_lsa_header_dump (lsa
->data
);
2698 /* discard old LSA from LSDB */
2700 ospf_discard_from_db (ospf
, lsdb
, lsa
);
2702 /* Calculate Checksum if self-originated?. */
2703 if (IS_LSA_SELF (lsa
))
2704 ospf_lsa_checksum (lsa
->data
);
2706 /* Insert LSA to LSDB. */
2707 ospf_lsdb_add (lsdb
, lsa
);
2710 /* Do LSA specific installation process. */
2711 switch (lsa
->data
->type
)
2713 case OSPF_ROUTER_LSA
:
2714 new = ospf_router_lsa_install (ospf
, lsa
, rt_recalc
);
2716 case OSPF_NETWORK_LSA
:
2718 new = ospf_network_lsa_install (ospf
, oi
, lsa
, rt_recalc
);
2720 case OSPF_SUMMARY_LSA
:
2721 new = ospf_summary_lsa_install (ospf
, lsa
, rt_recalc
);
2723 case OSPF_ASBR_SUMMARY_LSA
:
2724 new = ospf_summary_asbr_lsa_install (ospf
, lsa
, rt_recalc
);
2726 case OSPF_AS_EXTERNAL_LSA
:
2727 new = ospf_external_lsa_install (ospf
, lsa
, rt_recalc
);
2729 case OSPF_OPAQUE_LINK_LSA
:
2730 if (IS_LSA_SELF (lsa
))
2731 lsa
->oi
= oi
; /* Specify outgoing ospf-interface for this LSA. */
2734 /* Incoming "oi" for this LSA has set at LSUpd reception. */
2737 case OSPF_OPAQUE_AREA_LSA
:
2738 case OSPF_OPAQUE_AS_LSA
:
2739 new = ospf_opaque_lsa_install (lsa
, rt_recalc
);
2741 case OSPF_AS_NSSA_LSA
:
2742 new = ospf_external_lsa_install (ospf
, lsa
, rt_recalc
);
2743 default: /* type-6,8,9....nothing special */
2748 return new; /* Installation failed, cannot proceed further -- endo. */
2751 if (IS_DEBUG_OSPF (lsa
, LSA_INSTALL
))
2753 char area_str
[INET_ADDRSTRLEN
];
2755 switch (lsa
->data
->type
)
2757 case OSPF_AS_EXTERNAL_LSA
:
2758 case OSPF_OPAQUE_AS_LSA
:
2759 case OSPF_AS_NSSA_LSA
:
2760 zlog_debug ("LSA[%s]: Install %s",
2762 LOOKUP (ospf_lsa_type_msg
, new->data
->type
));
2765 strcpy (area_str
, inet_ntoa (new->area
->area_id
));
2766 zlog_debug ("LSA[%s]: Install %s to Area %s",
2768 LOOKUP (ospf_lsa_type_msg
, new->data
->type
), area_str
);
2774 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2775 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2777 if (IS_LSA_MAXAGE (new))
2779 if (IS_DEBUG_OSPF (lsa
, LSA_INSTALL
))
2780 zlog_debug ("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2782 inet_ntoa (new->data
->id
),
2784 ospf_lsa_maxage (ospf
, lsa
);
2792 ospf_check_nbr_status (struct ospf
*ospf
)
2794 struct listnode
*node
, *nnode
;
2795 struct ospf_interface
*oi
;
2797 for (ALL_LIST_ELEMENTS (ospf
->oiflist
, node
, nnode
, oi
))
2799 struct route_node
*rn
;
2800 struct ospf_neighbor
*nbr
;
2802 if (ospf_if_is_enable (oi
))
2803 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
2804 if ((nbr
= rn
->info
) != NULL
)
2805 if (nbr
->state
== NSM_Exchange
|| nbr
->state
== NSM_Loading
)
2807 route_unlock_node (rn
);
2818 ospf_maxage_lsa_remover (struct thread
*thread
)
2820 struct ospf
*ospf
= THREAD_ARG (thread
);
2821 struct ospf_lsa
*lsa
;
2822 struct route_node
*rn
;
2825 ospf
->t_maxage
= NULL
;
2827 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2828 zlog_debug ("LSA[MaxAge]: remover Start");
2830 reschedule
= !ospf_check_nbr_status (ospf
);
2833 for (rn
= route_top(ospf
->maxage_lsa
); rn
; rn
= route_next(rn
))
2835 if ((lsa
= rn
->info
) == NULL
)
2840 /* There is at least one neighbor from which we still await an ack
2841 * for that LSA, so we are not allowed to remove it from our lsdb yet
2842 * as per RFC 2328 section 14 para 4 a) */
2843 if (lsa
->retransmit_counter
> 0)
2849 /* TODO: maybe convert this function to a work-queue */
2850 if (thread_should_yield (thread
))
2852 OSPF_TIMER_ON (ospf
->t_maxage
, ospf_maxage_lsa_remover
, 0);
2853 route_unlock_node(rn
); /* route_top/route_next */
2857 /* Remove LSA from the LSDB */
2858 if (IS_LSA_SELF (lsa
))
2859 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2860 zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
2861 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), (u_long
)lsa
);
2863 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2864 zlog_debug ("LSA[Type%d:%s]: MaxAge LSA removed from list",
2865 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2867 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_PREMATURE_AGE
))
2869 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2870 zlog_debug ("originating new lsa for lsa 0x%p\n", (void *)lsa
);
2871 ospf_lsa_refresh (ospf
, lsa
);
2874 /* Remove from lsdb. */
2877 ospf_discard_from_db (ospf
, lsa
->lsdb
, lsa
);
2878 ospf_lsdb_delete (lsa
->lsdb
, lsa
);
2881 zlog_warn ("%s: LSA[Type%d:%s]: No associated LSDB!", __func__
,
2882 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
2885 /* A MaxAge LSA must be removed immediately from the router's link
2886 state database as soon as both a) it is no longer contained on any
2887 neighbor Link state retransmission lists and b) none of the router's
2888 neighbors are in states Exchange or Loading. */
2890 OSPF_TIMER_ON (ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2891 ospf
->maxage_delay
);
2897 ospf_lsa_maxage_delete (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2899 struct route_node
*rn
;
2900 struct prefix_ptr lsa_prefix
;
2902 lsa_prefix
.family
= 0;
2903 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.prefix
) * CHAR_BIT
;
2904 lsa_prefix
.prefix
= (uintptr_t) lsa
;
2906 if ((rn
= route_node_lookup(ospf
->maxage_lsa
,
2907 (struct prefix
*)&lsa_prefix
)))
2909 if (rn
->info
== lsa
)
2911 UNSET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2912 ospf_lsa_unlock (&lsa
); /* maxage_lsa */
2914 route_unlock_node (rn
); /* unlock node because lsa is deleted */
2916 route_unlock_node (rn
); /* route_node_lookup */
2920 /* Add LSA onto the MaxAge list, and schedule for removal.
2921 * This does *not* lead to the LSA being flooded, that must be taken
2922 * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
2926 ospf_lsa_maxage (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2928 struct prefix_ptr lsa_prefix
;
2929 struct route_node
*rn
;
2931 /* When we saw a MaxAge LSA flooded to us, we put it on the list
2932 and schedule the MaxAge LSA remover. */
2933 if (CHECK_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
))
2935 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2936 zlog_debug ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
2937 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), (void *)lsa
);
2941 lsa_prefix
.family
= 0;
2942 lsa_prefix
.prefixlen
= sizeof(lsa_prefix
.prefix
) * CHAR_BIT
;
2943 lsa_prefix
.prefix
= (uintptr_t) lsa
;
2945 if ((rn
= route_node_get (ospf
->maxage_lsa
,
2946 (struct prefix
*)&lsa_prefix
)) != NULL
)
2948 if (rn
->info
!= NULL
)
2950 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2951 zlog_debug ("LSA[%s]: found LSA (%p) in table for LSA %p %d",
2952 dump_lsa_key (lsa
), rn
->info
, (void *)lsa
,
2953 lsa_prefix
.prefixlen
);
2954 route_unlock_node (rn
);
2958 rn
->info
= ospf_lsa_lock(lsa
);
2959 SET_FLAG(lsa
->flags
, OSPF_LSA_IN_MAXAGE
);
2964 zlog_err("Unable to allocate memory for maxage lsa\n");
2968 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2969 zlog_debug ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa
));
2971 OSPF_TIMER_ON (ospf
->t_maxage
, ospf_maxage_lsa_remover
,
2972 ospf
->maxage_delay
);
2976 ospf_lsa_maxage_walker_remover (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
2978 /* Stay away from any Local Translated Type-7 LSAs */
2979 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
2982 if (IS_LSA_MAXAGE (lsa
))
2983 /* Self-originated LSAs should NOT time-out instead,
2984 they're flushed and submitted to the max_age list explicitly. */
2985 if (!ospf_lsa_is_self_originated (ospf
, lsa
))
2987 if (IS_DEBUG_OSPF (lsa
, LSA_FLOODING
))
2988 zlog_debug("LSA[%s]: is MaxAge", dump_lsa_key (lsa
));
2990 switch (lsa
->data
->type
)
2992 case OSPF_OPAQUE_LINK_LSA
:
2993 case OSPF_OPAQUE_AREA_LSA
:
2994 case OSPF_OPAQUE_AS_LSA
:
2996 * As a general rule, whenever network topology has changed
2997 * (due to an LSA removal in this case), routing recalculation
2998 * should be triggered. However, this is not true for opaque
2999 * LSAs. Even if an opaque LSA instance is going to be removed
3000 * from the routing domain, it does not mean a change in network
3001 * topology, and thus, routing recalculation is not needed here.
3004 case OSPF_AS_EXTERNAL_LSA
:
3005 case OSPF_AS_NSSA_LSA
:
3006 ospf_ase_incremental_update (ospf
, lsa
);
3009 ospf_spf_calculate_schedule (ospf
, SPF_FLAG_MAXAGE
);
3012 ospf_lsa_maxage (ospf
, lsa
);
3015 if (IS_LSA_MAXAGE (lsa
) && !ospf_lsa_is_self_originated (ospf
, lsa
))
3016 if (LS_AGE (lsa
) > OSPF_LSA_MAXAGE
+ 30)
3017 printf ("Eek! Shouldn't happen!\n");
3022 /* Periodical check of MaxAge LSA. */
3024 ospf_lsa_maxage_walker (struct thread
*thread
)
3026 struct ospf
*ospf
= THREAD_ARG (thread
);
3027 struct route_node
*rn
;
3028 struct ospf_lsa
*lsa
;
3029 struct ospf_area
*area
;
3030 struct listnode
*node
, *nnode
;
3032 ospf
->t_maxage_walker
= NULL
;
3034 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
3036 LSDB_LOOP (ROUTER_LSDB (area
), rn
, lsa
)
3037 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3038 LSDB_LOOP (NETWORK_LSDB (area
), rn
, lsa
)
3039 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3040 LSDB_LOOP (SUMMARY_LSDB (area
), rn
, lsa
)
3041 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3042 LSDB_LOOP (ASBR_SUMMARY_LSDB (area
), rn
, lsa
)
3043 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3044 LSDB_LOOP (OPAQUE_AREA_LSDB (area
), rn
, lsa
)
3045 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3046 LSDB_LOOP (OPAQUE_LINK_LSDB (area
), rn
, lsa
)
3047 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3048 LSDB_LOOP (NSSA_LSDB (area
), rn
, lsa
)
3049 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3052 /* for AS-external-LSAs. */
3055 LSDB_LOOP (EXTERNAL_LSDB (ospf
), rn
, lsa
)
3056 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3057 LSDB_LOOP (OPAQUE_AS_LSDB (ospf
), rn
, lsa
)
3058 ospf_lsa_maxage_walker_remover (ospf
, lsa
);
3061 OSPF_TIMER_ON (ospf
->t_maxage_walker
, ospf_lsa_maxage_walker
,
3062 OSPF_LSA_MAXAGE_CHECK_INTERVAL
);
3067 ospf_lsa_lookup_by_prefix (struct ospf_lsdb
*lsdb
, u_char type
,
3068 struct prefix_ipv4
*p
, struct in_addr router_id
)
3070 struct ospf_lsa
*lsa
;
3071 struct in_addr mask
, id
;
3072 struct lsa_header_mask
3074 struct lsa_header header
;
3075 struct in_addr mask
;
3078 lsa
= ospf_lsdb_lookup_by_id (lsdb
, type
, p
->prefix
, router_id
);
3082 masklen2ip (p
->prefixlen
, &mask
);
3084 hmask
= (struct lsa_header_mask
*) lsa
->data
;
3086 if (mask
.s_addr
!= hmask
->mask
.s_addr
)
3088 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3089 lsa
= ospf_lsdb_lookup_by_id (lsdb
, type
, id
, router_id
);
3098 ospf_lsa_lookup (struct ospf_area
*area
, u_int32_t type
,
3099 struct in_addr id
, struct in_addr adv_router
)
3101 struct ospf
*ospf
= ospf_lookup();
3106 case OSPF_ROUTER_LSA
:
3107 case OSPF_NETWORK_LSA
:
3108 case OSPF_SUMMARY_LSA
:
3109 case OSPF_ASBR_SUMMARY_LSA
:
3110 case OSPF_AS_NSSA_LSA
:
3111 case OSPF_OPAQUE_LINK_LSA
:
3112 case OSPF_OPAQUE_AREA_LSA
:
3113 return ospf_lsdb_lookup_by_id (area
->lsdb
, type
, id
, adv_router
);
3114 case OSPF_AS_EXTERNAL_LSA
:
3115 case OSPF_OPAQUE_AS_LSA
:
3116 return ospf_lsdb_lookup_by_id (ospf
->lsdb
, type
, id
, adv_router
);
3125 ospf_lsa_lookup_by_id (struct ospf_area
*area
, u_int32_t type
,
3128 struct ospf_lsa
*lsa
;
3129 struct route_node
*rn
;
3133 case OSPF_ROUTER_LSA
:
3134 return ospf_lsdb_lookup_by_id (area
->lsdb
, type
, id
, id
);
3135 case OSPF_NETWORK_LSA
:
3136 for (rn
= route_top (NETWORK_LSDB (area
)); rn
; rn
= route_next (rn
))
3137 if ((lsa
= rn
->info
))
3138 if (IPV4_ADDR_SAME (&lsa
->data
->id
, &id
))
3140 route_unlock_node (rn
);
3144 case OSPF_SUMMARY_LSA
:
3145 case OSPF_ASBR_SUMMARY_LSA
:
3146 /* Currently not used. */
3148 return ospf_lsdb_lookup_by_id (area
->lsdb
, type
, id
, id
);
3149 case OSPF_AS_EXTERNAL_LSA
:
3150 case OSPF_AS_NSSA_LSA
:
3151 case OSPF_OPAQUE_LINK_LSA
:
3152 case OSPF_OPAQUE_AREA_LSA
:
3153 case OSPF_OPAQUE_AS_LSA
:
3154 /* Currently not used. */
3164 ospf_lsa_lookup_by_header (struct ospf_area
*area
, struct lsa_header
*lsah
)
3166 struct ospf_lsa
*match
;
3169 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3170 * is redefined to have two subfields; opaque-type and opaque-id.
3171 * However, it is harmless to treat the two sub fields together, as if
3172 * they two were forming a unique LSA-ID.
3175 match
= ospf_lsa_lookup (area
, lsah
->type
, lsah
->id
, lsah
->adv_router
);
3178 if (IS_DEBUG_OSPF (lsa
, LSA
) == OSPF_DEBUG_LSA
)
3179 zlog_debug ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3180 lsah
->type
, inet_ntoa (lsah
->id
));
3185 /* return +n, l1 is more recent.
3186 return -n, l2 is more recent.
3187 return 0, l1 and l2 is identical. */
3189 ospf_lsa_more_recent (struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3194 if (l1
== NULL
&& l2
== NULL
)
3201 /* compare LS sequence number. */
3202 x
= (int) ntohl (l1
->data
->ls_seqnum
);
3203 y
= (int) ntohl (l2
->data
->ls_seqnum
);
3209 /* compare LS checksum. */
3210 r
= ntohs (l1
->data
->checksum
) - ntohs (l2
->data
->checksum
);
3214 /* compare LS age. */
3215 if (IS_LSA_MAXAGE (l1
) && !IS_LSA_MAXAGE (l2
))
3217 else if (!IS_LSA_MAXAGE (l1
) && IS_LSA_MAXAGE (l2
))
3220 /* compare LS age with MaxAgeDiff. */
3221 if (LS_AGE (l1
) - LS_AGE (l2
) > OSPF_LSA_MAXAGE_DIFF
)
3223 else if (LS_AGE (l2
) - LS_AGE (l1
) > OSPF_LSA_MAXAGE_DIFF
)
3226 /* LSAs are identical. */
3230 /* If two LSAs are different, return 1, otherwise return 0. */
3232 ospf_lsa_different (struct ospf_lsa
*l1
, struct ospf_lsa
*l2
)
3240 if (l1
->data
->options
!= l2
->data
->options
)
3243 if (IS_LSA_MAXAGE (l1
) && !IS_LSA_MAXAGE (l2
))
3246 if (IS_LSA_MAXAGE (l2
) && !IS_LSA_MAXAGE (l1
))
3249 if (l1
->data
->length
!= l2
->data
->length
)
3252 if (l1
->data
->length
== 0)
3255 if (CHECK_FLAG ((l1
->flags
^ l2
->flags
), OSPF_LSA_RECEIVED
))
3256 return 1; /* May be a stale LSA in the LSBD */
3258 assert ( ntohs(l1
->data
->length
) > OSPF_LSA_HEADER_SIZE
);
3260 p1
= (char *) l1
->data
;
3261 p2
= (char *) l2
->data
;
3263 if (memcmp (p1
+ OSPF_LSA_HEADER_SIZE
, p2
+ OSPF_LSA_HEADER_SIZE
,
3264 ntohs( l1
->data
->length
) - OSPF_LSA_HEADER_SIZE
) != 0)
3270 #ifdef ORIGINAL_CODING
3272 ospf_lsa_flush_self_originated (struct ospf_neighbor
*nbr
,
3273 struct ospf_lsa
*self
,
3274 struct ospf_lsa
*new)
3278 /* Adjust LS Sequence Number. */
3279 seqnum
= ntohl (new->data
->ls_seqnum
) + 1;
3280 self
->data
->ls_seqnum
= htonl (seqnum
);
3282 /* Recalculate LSA checksum. */
3283 ospf_lsa_checksum (self
->data
);
3285 /* Reflooding LSA. */
3286 /* RFC2328 Section 13.3
3287 On non-broadcast networks, separate Link State Update
3288 packets must be sent, as unicasts, to each adjacent neighbor
3289 (i.e., those in state Exchange or greater). The destination
3290 IP addresses for these packets are the neighbors' IP
3292 if (nbr
->oi
->type
== OSPF_IFTYPE_NBMA
)
3294 struct route_node
*rn
;
3295 struct ospf_neighbor
*onbr
;
3297 for (rn
= route_top (nbr
->oi
->nbrs
); rn
; rn
= route_next (rn
))
3298 if ((onbr
= rn
->info
) != NULL
)
3299 if (onbr
!= nbr
->oi
->nbr_self
&& onbr
->status
>= NSM_Exchange
)
3300 ospf_ls_upd_send_lsa (onbr
, self
, OSPF_SEND_PACKET_DIRECT
);
3303 ospf_ls_upd_send_lsa (nbr
, self
, OSPF_SEND_PACKET_INDIRECT
);
3305 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
3306 zlog_debug ("LSA[Type%d:%s]: Flush self-originated LSA",
3307 self
->data
->type
, inet_ntoa (self
->data
->id
));
3309 #else /* ORIGINAL_CODING */
3311 ospf_lsa_flush_schedule (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3313 if (lsa
== NULL
|| !IS_LSA_SELF (lsa
))
3316 if (IS_DEBUG_OSPF_EVENT
)
3317 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
3319 /* Force given lsa's age to MaxAge. */
3320 lsa
->data
->ls_age
= htons (OSPF_LSA_MAXAGE
);
3322 switch (lsa
->data
->type
)
3324 /* Opaque wants to be notified of flushes */
3325 case OSPF_OPAQUE_LINK_LSA
:
3326 case OSPF_OPAQUE_AREA_LSA
:
3327 case OSPF_OPAQUE_AS_LSA
:
3328 ospf_opaque_lsa_refresh (lsa
);
3331 ospf_refresher_unregister_lsa (ospf
, lsa
);
3332 ospf_lsa_flush (ospf
, lsa
);
3340 ospf_flush_self_originated_lsas_now (struct ospf
*ospf
)
3342 struct listnode
*node
, *nnode
;
3343 struct listnode
*node2
, *nnode2
;
3344 struct ospf_area
*area
;
3345 struct ospf_interface
*oi
;
3346 struct ospf_lsa
*lsa
;
3347 struct route_node
*rn
;
3348 int need_to_flush_ase
= 0;
3350 for (ALL_LIST_ELEMENTS (ospf
->areas
, node
, nnode
, area
))
3352 if ((lsa
= area
->router_lsa_self
) != NULL
)
3354 if (IS_DEBUG_OSPF_EVENT
)
3355 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3356 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
3358 ospf_refresher_unregister_lsa (ospf
, lsa
);
3359 ospf_lsa_flush_area (lsa
, area
);
3360 ospf_lsa_unlock (&area
->router_lsa_self
);
3361 area
->router_lsa_self
= NULL
;
3364 for (ALL_LIST_ELEMENTS (area
->oiflist
, node2
, nnode2
, oi
))
3366 if ((lsa
= oi
->network_lsa_self
) != NULL
3367 && oi
->state
== ISM_DR
3368 && oi
->full_nbrs
> 0)
3370 if (IS_DEBUG_OSPF_EVENT
)
3371 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
3372 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
));
3374 ospf_refresher_unregister_lsa (ospf
, oi
->network_lsa_self
);
3375 ospf_lsa_flush_area (oi
->network_lsa_self
, area
);
3376 ospf_lsa_unlock (&oi
->network_lsa_self
);
3377 oi
->network_lsa_self
= NULL
;
3380 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
3381 && area
->external_routing
== OSPF_AREA_DEFAULT
)
3382 need_to_flush_ase
= 1;
3385 LSDB_LOOP (SUMMARY_LSDB (area
), rn
, lsa
)
3386 ospf_lsa_flush_schedule (ospf
, lsa
);
3387 LSDB_LOOP (ASBR_SUMMARY_LSDB (area
), rn
, lsa
)
3388 ospf_lsa_flush_schedule (ospf
, lsa
);
3389 LSDB_LOOP (OPAQUE_LINK_LSDB (area
), rn
, lsa
)
3390 ospf_lsa_flush_schedule (ospf
, lsa
);
3391 LSDB_LOOP (OPAQUE_AREA_LSDB (area
), rn
, lsa
)
3392 ospf_lsa_flush_schedule (ospf
, lsa
);
3395 if (need_to_flush_ase
)
3397 LSDB_LOOP (EXTERNAL_LSDB (ospf
), rn
, lsa
)
3398 ospf_lsa_flush_schedule (ospf
, lsa
);
3399 LSDB_LOOP (OPAQUE_AS_LSDB (ospf
), rn
, lsa
)
3400 ospf_lsa_flush_schedule (ospf
, lsa
);
3404 * Make sure that the MaxAge LSA remover is executed immediately,
3405 * without conflicting to other threads.
3407 if (ospf
->t_maxage
!= NULL
)
3409 OSPF_TIMER_OFF (ospf
->t_maxage
);
3410 thread_execute (master
, ospf_maxage_lsa_remover
, ospf
, 0);
3415 #endif /* ORIGINAL_CODING */
3417 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3418 /* An interface-independent version of ospf_lsa_is_self_originated */
3420 ospf_lsa_is_self_originated (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3422 struct listnode
*node
;
3423 struct ospf_interface
*oi
;
3425 /* This LSA is already checked. */
3426 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF_CHECKED
))
3427 return IS_LSA_SELF (lsa
);
3429 /* Make sure LSA is self-checked. */
3430 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF_CHECKED
);
3432 /* AdvRouter and Router ID is the same. */
3433 if (IPV4_ADDR_SAME (&lsa
->data
->adv_router
, &ospf
->router_id
))
3434 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3436 /* LSA is router-LSA. */
3437 else if (lsa
->data
->type
== OSPF_ROUTER_LSA
&&
3438 IPV4_ADDR_SAME (&lsa
->data
->id
, &ospf
->router_id
))
3439 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3441 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3442 else if (lsa
->data
->type
== OSPF_NETWORK_LSA
)
3443 for (ALL_LIST_ELEMENTS_RO (ospf
->oiflist
, node
, oi
))
3445 /* Ignore virtual link. */
3446 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3447 if (oi
->address
->family
== AF_INET
)
3448 if (IPV4_ADDR_SAME (&lsa
->data
->id
, &oi
->address
->u
.prefix4
))
3450 /* to make it easier later */
3451 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
3452 return IS_LSA_SELF (lsa
);
3456 return IS_LSA_SELF (lsa
);
3459 /* Get unique Link State ID. */
3461 ospf_lsa_unique_id (struct ospf
*ospf
,
3462 struct ospf_lsdb
*lsdb
, u_char type
, struct prefix_ipv4
*p
)
3464 struct ospf_lsa
*lsa
;
3465 struct in_addr mask
, id
;
3469 /* Check existence of LSA instance. */
3470 lsa
= ospf_lsdb_lookup_by_id (lsdb
, type
, id
, ospf
->router_id
);
3473 struct as_external_lsa
*al
= (struct as_external_lsa
*) lsa
->data
;
3474 if (ip_masklen (al
->mask
) == p
->prefixlen
)
3476 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
3477 zlog_debug ("ospf_lsa_unique_id(): "
3478 "Can't get Link State ID for %s/%d",
3479 inet_ntoa (p
->prefix
), p
->prefixlen
);
3480 /* id.s_addr = 0; */
3481 id
.s_addr
= 0xffffffff;
3484 /* Masklen differs, then apply wildcard mask to Link State ID. */
3487 masklen2ip (p
->prefixlen
, &mask
);
3489 id
.s_addr
= p
->prefix
.s_addr
| (~mask
.s_addr
);
3490 lsa
= ospf_lsdb_lookup_by_id (ospf
->lsdb
, type
,
3491 id
, ospf
->router_id
);
3494 if (IS_DEBUG_OSPF (lsa
, LSA_GENERATE
))
3495 zlog_debug ("ospf_lsa_unique_id(): "
3496 "Can't get Link State ID for %s/%d",
3497 inet_ntoa (p
->prefix
), p
->prefixlen
);
3498 /* id.s_addr = 0; */
3499 id
.s_addr
= 0xffffffff;
3509 #define LSA_ACTION_FLOOD_AREA 1
3510 #define LSA_ACTION_FLUSH_AREA 2
3515 struct ospf_area
*area
;
3516 struct ospf_lsa
*lsa
;
3520 ospf_lsa_action (struct thread
*t
)
3522 struct lsa_action
*data
;
3524 data
= THREAD_ARG (t
);
3526 if (IS_DEBUG_OSPF (lsa
, LSA
) == OSPF_DEBUG_LSA
)
3527 zlog_debug ("LSA[Action]: Performing scheduled LSA action: %d",
3530 switch (data
->action
)
3532 case LSA_ACTION_FLOOD_AREA
:
3533 ospf_flood_through_area (data
->area
, NULL
, data
->lsa
);
3535 case LSA_ACTION_FLUSH_AREA
:
3536 ospf_lsa_flush_area (data
->lsa
, data
->area
);
3540 ospf_lsa_unlock (&data
->lsa
); /* Message */
3541 XFREE (MTYPE_OSPF_MESSAGE
, data
);
3546 ospf_schedule_lsa_flood_area (struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3548 struct lsa_action
*data
;
3550 data
= XCALLOC (MTYPE_OSPF_MESSAGE
, sizeof (struct lsa_action
));
3551 data
->action
= LSA_ACTION_FLOOD_AREA
;
3553 data
->lsa
= ospf_lsa_lock (lsa
); /* Message / Flood area */
3555 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3559 ospf_schedule_lsa_flush_area (struct ospf_area
*area
, struct ospf_lsa
*lsa
)
3561 struct lsa_action
*data
;
3563 data
= XCALLOC (MTYPE_OSPF_MESSAGE
, sizeof (struct lsa_action
));
3564 data
->action
= LSA_ACTION_FLUSH_AREA
;
3566 data
->lsa
= ospf_lsa_lock (lsa
); /* Message / Flush area */
3568 thread_add_event(master
, ospf_lsa_action
, data
, 0, NULL
);
3572 /* LSA Refreshment functions. */
3574 ospf_lsa_refresh (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3576 struct external_info
*ei
;
3577 struct ospf_lsa
*new = NULL
;
3578 assert (CHECK_FLAG (lsa
->flags
, OSPF_LSA_SELF
));
3579 assert (IS_LSA_SELF (lsa
));
3580 assert (lsa
->lock
> 0);
3582 switch (lsa
->data
->type
)
3584 /* Router and Network LSAs are processed differently. */
3585 case OSPF_ROUTER_LSA
:
3586 new = ospf_router_lsa_refresh (lsa
);
3588 case OSPF_NETWORK_LSA
:
3589 new = ospf_network_lsa_refresh (lsa
);
3591 case OSPF_SUMMARY_LSA
:
3592 new = ospf_summary_lsa_refresh (ospf
, lsa
);
3594 case OSPF_ASBR_SUMMARY_LSA
:
3595 new = ospf_summary_asbr_lsa_refresh (ospf
, lsa
);
3597 case OSPF_AS_EXTERNAL_LSA
:
3598 /* Translated from NSSA Type-5s are refreshed when
3599 * from refresh of Type-7 - do not refresh these directly.
3601 if (CHECK_FLAG (lsa
->flags
, OSPF_LSA_LOCAL_XLT
))
3603 ei
= ospf_external_info_check (lsa
);
3605 new = ospf_external_lsa_refresh (ospf
, lsa
, ei
, LSA_REFRESH_FORCE
);
3607 ospf_lsa_flush_as (ospf
, lsa
);
3609 case OSPF_OPAQUE_LINK_LSA
:
3610 case OSPF_OPAQUE_AREA_LSA
:
3611 case OSPF_OPAQUE_AS_LSA
:
3612 new = ospf_opaque_lsa_refresh (lsa
);
3621 ospf_refresher_register_lsa (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3623 u_int16_t index
, current_index
;
3625 assert (lsa
->lock
> 0);
3626 assert (IS_LSA_SELF (lsa
));
3628 if (lsa
->refresh_list
< 0)
3631 int min_delay
= OSPF_LS_REFRESH_TIME
- (2 * OSPF_LS_REFRESH_JITTER
);
3632 int max_delay
= OSPF_LS_REFRESH_TIME
- OSPF_LS_REFRESH_JITTER
;
3634 /* We want to refresh the LSA within OSPF_LS_REFRESH_TIME which is
3635 * 1800s. Use jitter so that we send the LSA sometime between 1680s
3638 delay
= (random() % (max_delay
- min_delay
)) + min_delay
;
3640 current_index
= ospf
->lsa_refresh_queue
.index
+ (monotime(NULL
)
3641 - ospf
->lsa_refresher_started
)/OSPF_LSA_REFRESHER_GRANULARITY
;
3643 index
= (current_index
+ delay
/OSPF_LSA_REFRESHER_GRANULARITY
)
3644 % (OSPF_LSA_REFRESHER_SLOTS
);
3646 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3647 zlog_debug ("LSA[Refresh:Type%d:%s]: age %d, added to index %d",
3648 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), LS_AGE (lsa
), index
);
3650 if (!ospf
->lsa_refresh_queue
.qs
[index
])
3651 ospf
->lsa_refresh_queue
.qs
[index
] = list_new ();
3653 listnode_add (ospf
->lsa_refresh_queue
.qs
[index
],
3654 ospf_lsa_lock (lsa
)); /* lsa_refresh_queue */
3655 lsa
->refresh_list
= index
;
3657 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3658 zlog_debug ("LSA[Refresh:Type%d:%s]: ospf_refresher_register_lsa(): "
3659 "setting refresh_list on lsa %p (slod %d)",
3660 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), (void *)lsa
, index
);
3665 ospf_refresher_unregister_lsa (struct ospf
*ospf
, struct ospf_lsa
*lsa
)
3667 assert (lsa
->lock
> 0);
3668 assert (IS_LSA_SELF (lsa
));
3669 if (lsa
->refresh_list
>= 0)
3671 struct list
*refresh_list
= ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
];
3672 listnode_delete (refresh_list
, lsa
);
3673 if (!listcount (refresh_list
))
3675 list_free (refresh_list
);
3676 ospf
->lsa_refresh_queue
.qs
[lsa
->refresh_list
] = NULL
;
3678 ospf_lsa_unlock (&lsa
); /* lsa_refresh_queue */
3679 lsa
->refresh_list
= -1;
3684 ospf_lsa_refresh_walker (struct thread
*t
)
3686 struct list
*refresh_list
;
3687 struct listnode
*node
, *nnode
;
3688 struct ospf
*ospf
= THREAD_ARG (t
);
3689 struct ospf_lsa
*lsa
;
3691 struct list
*lsa_to_refresh
= list_new ();
3693 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3694 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): start");
3697 i
= ospf
->lsa_refresh_queue
.index
;
3699 /* Note: if clock has jumped backwards, then time change could be negative,
3700 so we are careful to cast the expression to unsigned before taking
3702 ospf
->lsa_refresh_queue
.index
=
3703 ((unsigned long)(ospf
->lsa_refresh_queue
.index
+
3704 (monotime(NULL
) - ospf
->lsa_refresher_started
)
3705 / OSPF_LSA_REFRESHER_GRANULARITY
))
3706 % OSPF_LSA_REFRESHER_SLOTS
;
3708 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3709 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3710 ospf
->lsa_refresh_queue
.index
);
3712 for (;i
!= ospf
->lsa_refresh_queue
.index
;
3713 i
= (i
+ 1) % OSPF_LSA_REFRESHER_SLOTS
)
3715 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3716 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): "
3717 "refresh index %d", i
);
3719 refresh_list
= ospf
->lsa_refresh_queue
.qs
[i
];
3723 ospf
->lsa_refresh_queue
.qs
[i
] = NULL
;
3727 for (ALL_LIST_ELEMENTS (refresh_list
, node
, nnode
, lsa
))
3729 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3730 zlog_debug ("LSA[Refresh:Type%d:%s]: ospf_lsa_refresh_walker(): "
3731 "refresh lsa %p (slot %d)",
3732 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), (void *)lsa
, i
);
3734 assert (lsa
->lock
> 0);
3735 list_delete_node (refresh_list
, node
);
3736 lsa
->refresh_list
= -1;
3737 listnode_add (lsa_to_refresh
, lsa
);
3739 list_free (refresh_list
);
3743 ospf
->t_lsa_refresher
= NULL
;
3744 thread_add_timer(master
, ospf_lsa_refresh_walker
, ospf
, ospf
->lsa_refresh_interval
,
3745 &ospf
->t_lsa_refresher
);
3746 ospf
->lsa_refresher_started
= monotime(NULL
);
3748 for (ALL_LIST_ELEMENTS (lsa_to_refresh
, node
, nnode
, lsa
))
3750 ospf_lsa_refresh (ospf
, lsa
);
3751 assert (lsa
->lock
> 0);
3752 ospf_lsa_unlock (&lsa
); /* lsa_refresh_queue & temp for lsa_to_refresh*/
3755 list_delete (lsa_to_refresh
);
3757 if (IS_DEBUG_OSPF (lsa
, LSA_REFRESH
))
3758 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");