+ initial edition of meta-queue for RIB updates processing (bug #431)
[jleu-quagga.git] / ospfd / ospf_lsa.c
blob243928f4157db5afbf71dd07122a342f6d45e9b9
1 /*
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
10 * later version.
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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
23 #include <zebra.h>
25 #include "linklist.h"
26 #include "prefix.h"
27 #include "if.h"
28 #include "table.h"
29 #include "memory.h"
30 #include "stream.h"
31 #include "log.h"
32 #include "thread.h"
33 #include "hash.h"
34 #include "sockunion.h" /* for inet_aton() */
36 #include "ospfd/ospfd.h"
37 #include "ospfd/ospf_interface.h"
38 #include "ospfd/ospf_ism.h"
39 #include "ospfd/ospf_asbr.h"
40 #include "ospfd/ospf_lsa.h"
41 #include "ospfd/ospf_lsdb.h"
42 #include "ospfd/ospf_neighbor.h"
43 #include "ospfd/ospf_nsm.h"
44 #include "ospfd/ospf_flood.h"
45 #include "ospfd/ospf_packet.h"
46 #include "ospfd/ospf_spf.h"
47 #include "ospfd/ospf_dump.h"
48 #include "ospfd/ospf_route.h"
49 #include "ospfd/ospf_ase.h"
50 #include "ospfd/ospf_zebra.h"
53 u_int32_t
54 get_metric (u_char *metric)
56 u_int32_t m;
57 m = metric[0];
58 m = (m << 8) + metric[1];
59 m = (m << 8) + metric[2];
60 return m;
64 struct timeval
65 tv_adjust (struct timeval a)
67 while (a.tv_usec >= 1000000)
69 a.tv_usec -= 1000000;
70 a.tv_sec++;
73 while (a.tv_usec < 0)
75 a.tv_usec += 1000000;
76 a.tv_sec--;
79 return a;
82 int
83 tv_ceil (struct timeval a)
85 a = tv_adjust (a);
87 return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec);
90 int
91 tv_floor (struct timeval a)
93 a = tv_adjust (a);
95 return a.tv_sec;
98 struct timeval
99 int2tv (int a)
101 struct timeval ret;
103 ret.tv_sec = a;
104 ret.tv_usec = 0;
106 return ret;
109 struct timeval
110 tv_add (struct timeval a, struct timeval b)
112 struct timeval ret;
114 ret.tv_sec = a.tv_sec + b.tv_sec;
115 ret.tv_usec = a.tv_usec + b.tv_usec;
117 return tv_adjust (ret);
120 struct timeval
121 tv_sub (struct timeval a, struct timeval b)
123 struct timeval ret;
125 ret.tv_sec = a.tv_sec - b.tv_sec;
126 ret.tv_usec = a.tv_usec - b.tv_usec;
128 return tv_adjust (ret);
132 tv_cmp (struct timeval a, struct timeval b)
134 return (a.tv_sec == b.tv_sec ?
135 a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
139 ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
141 struct timeval delta, now;
142 int delay = 0;
144 quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
145 delta = tv_sub (now, lsa->tv_orig);
147 if (tv_cmp (delta, int2tv (OSPF_MIN_LS_INTERVAL)) < 0)
149 delay = tv_ceil (tv_sub (int2tv (OSPF_MIN_LS_INTERVAL), delta));
151 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
152 zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
153 lsa->data->type, inet_ntoa (lsa->data->id), delay);
155 assert (delay > 0);
158 return delay;
163 get_age (struct ospf_lsa *lsa)
165 int age;
167 age = ntohs (lsa->data->ls_age)
168 + tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
170 return age;
174 /* Fletcher Checksum -- Refer to RFC1008. */
175 #define MODX 4102
176 #define LSA_CHECKSUM_OFFSET 15
178 u_int16_t
179 ospf_lsa_checksum (struct lsa_header *lsa)
181 u_char *sp, *ep, *p, *q;
182 int c0 = 0, c1 = 0;
183 int x, y;
184 u_int16_t length;
186 lsa->checksum = 0;
187 length = ntohs (lsa->length) - 2;
188 sp = (u_char *) &lsa->options;
190 for (ep = sp + length; sp < ep; sp = q)
192 q = sp + MODX;
193 if (q > ep)
194 q = ep;
195 for (p = sp; p < q; p++)
197 c0 += *p;
198 c1 += c0;
200 c0 %= 255;
201 c1 %= 255;
204 x = (((int)length - LSA_CHECKSUM_OFFSET) * c0 - c1) % 255;
205 if (x <= 0)
206 x += 255;
207 y = 510 - c0 - x;
208 if (y > 255)
209 y -= 255;
211 /* take care endian issue. */
212 lsa->checksum = htons ((x << 8) + y);
214 return (lsa->checksum);
219 /* Create OSPF LSA. */
220 struct ospf_lsa *
221 ospf_lsa_new ()
223 struct ospf_lsa *new;
225 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
226 memset (new, 0, sizeof (struct ospf_lsa));
228 new->flags = 0;
229 new->lock = 1;
230 new->retransmit_counter = 0;
231 new->tv_recv = recent_relative_time ();
232 new->tv_orig = new->tv_recv;
233 new->refresh_list = -1;
235 return new;
238 /* Duplicate OSPF LSA. */
239 struct ospf_lsa *
240 ospf_lsa_dup (struct ospf_lsa *lsa)
242 struct ospf_lsa *new;
244 if (lsa == NULL)
245 return NULL;
247 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
249 memcpy (new, lsa, sizeof (struct ospf_lsa));
250 UNSET_FLAG (new->flags, OSPF_LSA_DISCARD);
251 new->lock = 1;
252 new->retransmit_counter = 0;
253 new->data = ospf_lsa_data_dup (lsa->data);
255 /* kevinm: Clear the refresh_list, otherwise there are going
256 to be problems when we try to remove the LSA from the
257 queue (which it's not a member of.)
258 XXX: Should we add the LSA to the refresh_list queue? */
259 new->refresh_list = -1;
261 if (IS_DEBUG_OSPF (lsa, LSA))
262 zlog_debug ("LSA: duplicated %p (new: %p)", lsa, new);
264 return new;
267 /* Free OSPF LSA. */
268 void
269 ospf_lsa_free (struct ospf_lsa *lsa)
271 assert (lsa->lock == 0);
273 if (IS_DEBUG_OSPF (lsa, LSA))
274 zlog_debug ("LSA: freed %p", lsa);
276 /* Delete LSA data. */
277 if (lsa->data != NULL)
278 ospf_lsa_data_free (lsa->data);
280 assert (lsa->refresh_list < 0);
282 memset (lsa, 0, sizeof (struct ospf_lsa));
283 XFREE (MTYPE_OSPF_LSA, lsa);
286 /* Lock LSA. */
287 struct ospf_lsa *
288 ospf_lsa_lock (struct ospf_lsa *lsa)
290 lsa->lock++;
291 return lsa;
294 /* Unlock LSA. */
295 void
296 ospf_lsa_unlock (struct ospf_lsa **lsa)
298 /* This is sanity check. */
299 if (!lsa || !*lsa)
300 return;
302 (*lsa)->lock--;
304 assert ((*lsa)->lock >= 0);
306 if ((*lsa)->lock == 0)
308 assert (CHECK_FLAG ((*lsa)->flags, OSPF_LSA_DISCARD));
309 ospf_lsa_free (*lsa);
310 *lsa = NULL;
314 /* Check discard flag. */
315 void
316 ospf_lsa_discard (struct ospf_lsa *lsa)
318 if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
320 SET_FLAG (lsa->flags, OSPF_LSA_DISCARD);
321 ospf_lsa_unlock (&lsa);
325 /* Create LSA data. */
326 struct lsa_header *
327 ospf_lsa_data_new (size_t size)
329 struct lsa_header *new;
331 new = (struct lsa_header *) XMALLOC (MTYPE_OSPF_LSA_DATA, size);
332 memset (new, 0, size);
334 return new;
337 /* Duplicate LSA data. */
338 struct lsa_header *
339 ospf_lsa_data_dup (struct lsa_header *lsah)
341 struct lsa_header *new;
343 new = ospf_lsa_data_new (ntohs (lsah->length));
344 memcpy (new, lsah, ntohs (lsah->length));
346 return new;
349 /* Free LSA data. */
350 void
351 ospf_lsa_data_free (struct lsa_header *lsah)
353 if (IS_DEBUG_OSPF (lsa, LSA))
354 zlog_debug ("LSA[Type%d:%s]: data freed %p",
355 lsah->type, inet_ntoa (lsah->id), lsah);
357 XFREE (MTYPE_OSPF_LSA_DATA, lsah);
361 /* LSA general functions. */
363 const char *
364 dump_lsa_key (struct ospf_lsa *lsa)
366 static char buf[] = {
367 "Type255,id(255.255.255.255),ar(255.255.255.255)"
369 struct lsa_header *lsah;
371 if (lsa != NULL && (lsah = lsa->data) != NULL)
373 char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
374 strcpy (id, inet_ntoa (lsah->id));
375 strcpy (ar, inet_ntoa (lsah->adv_router));
377 sprintf (buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar);
379 else
380 strcpy (buf, "NULL");
382 return buf;
385 u_int32_t
386 lsa_seqnum_increment (struct ospf_lsa *lsa)
388 u_int32_t seqnum;
390 seqnum = ntohl (lsa->data->ls_seqnum) + 1;
392 return htonl (seqnum);
395 void
396 lsa_header_set (struct stream *s, u_char options,
397 u_char type, struct in_addr id, struct in_addr router_id)
399 struct lsa_header *lsah;
401 lsah = (struct lsa_header *) STREAM_DATA (s);
403 lsah->ls_age = htons (0);
404 lsah->options = options;
405 lsah->type = type;
406 lsah->id = id;
407 lsah->adv_router = router_id;
408 lsah->ls_seqnum = htonl (OSPF_INITIAL_SEQUENCE_NUMBER);
410 stream_forward_endp (s, OSPF_LSA_HEADER_SIZE);
414 /* router-LSA related functions. */
415 /* Get router-LSA flags. */
416 static u_char
417 router_lsa_flags (struct ospf_area *area)
419 u_char flags;
421 flags = area->ospf->flags;
423 /* Set virtual link flag. */
424 if (ospf_full_virtual_nbrs (area))
425 SET_FLAG (flags, ROUTER_LSA_VIRTUAL);
426 else
427 /* Just sanity check */
428 UNSET_FLAG (flags, ROUTER_LSA_VIRTUAL);
430 /* Set Shortcut ABR behabiour flag. */
431 UNSET_FLAG (flags, ROUTER_LSA_SHORTCUT);
432 if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
433 if (!OSPF_IS_AREA_BACKBONE (area))
434 if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
435 area->ospf->backbone == NULL) ||
436 area->shortcut_configured == OSPF_SHORTCUT_ENABLE)
437 SET_FLAG (flags, ROUTER_LSA_SHORTCUT);
439 /* ASBR can't exit in stub area. */
440 if (area->external_routing == OSPF_AREA_STUB)
441 UNSET_FLAG (flags, ROUTER_LSA_EXTERNAL);
442 /* If ASBR set External flag */
443 else if (IS_OSPF_ASBR (area->ospf))
444 SET_FLAG (flags, ROUTER_LSA_EXTERNAL);
446 /* Set ABR dependent flags */
447 if (IS_OSPF_ABR (area->ospf))
449 SET_FLAG (flags, ROUTER_LSA_BORDER);
450 /* If Area is NSSA and we are both ABR and unconditional translator,
451 * set Nt bit to inform other routers.
453 if ( (area->external_routing == OSPF_AREA_NSSA)
454 && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
455 SET_FLAG (flags, ROUTER_LSA_NT);
457 return flags;
460 /* Lookup neighbor other than myself.
461 And check neighbor count,
462 Point-to-Point link must have only 1 neighbor. */
463 struct ospf_neighbor *
464 ospf_nbr_lookup_ptop (struct ospf_interface *oi)
466 struct ospf_neighbor *nbr = NULL;
467 struct route_node *rn;
469 /* Search neighbor, there must be one of two nbrs. */
470 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
471 if ((nbr = rn->info))
472 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
473 if (nbr->state == NSM_Full)
475 route_unlock_node (rn);
476 break;
479 /* PtoP link must have only 1 neighbor. */
480 if (ospf_nbr_count (oi, 0) > 1)
481 zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
483 return nbr;
486 /* Determine cost of link, taking RFC3137 stub-router support into
487 * consideration
489 static u_int16_t
490 ospf_link_cost (struct ospf_interface *oi)
492 /* RFC3137 stub router support */
493 if (!CHECK_FLAG (oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
494 return oi->output_cost;
495 else
496 return OSPF_OUTPUT_COST_INFINITE;
499 /* Set a link information. */
500 static char
501 link_info_set (struct stream *s, struct in_addr id,
502 struct in_addr data, u_char type, u_char tos, u_int16_t cost)
504 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
505 * vast majority of cases. Some rare routers with lots of links need more.
506 * we try accomodate those here.
508 if (STREAM_WRITEABLE(s) < OSPF_ROUTER_LSA_LINK_SIZE)
510 size_t ret = OSPF_MAX_LSA_SIZE;
512 /* Can we enlarge the stream still? */
513 if (STREAM_SIZE(s) == OSPF_MAX_LSA_SIZE)
515 /* we futz the size here for simplicity, really we need to account
516 * for just:
517 * IP Header - (sizeof (struct ip))
518 * OSPF Header - OSPF_HEADER_SIZE
519 * LSA Header - OSPF_LSA_HEADER_SIZE
520 * MD5 auth data, if MD5 is configured - OSPF_AUTH_MD5_SIZE.
522 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
524 ret = stream_resize (s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
527 if (ret == OSPF_MAX_LSA_SIZE)
529 zlog_warn ("%s: Out of space in LSA stream, left %ld, size %ld",
530 __func__, STREAM_REMAIN (s), STREAM_SIZE (s));
531 return 0;
535 /* TOS based routing is not supported. */
536 stream_put_ipv4 (s, id.s_addr); /* Link ID. */
537 stream_put_ipv4 (s, data.s_addr); /* Link Data. */
538 stream_putc (s, type); /* Link Type. */
539 stream_putc (s, tos); /* TOS = 0. */
540 stream_putw (s, cost); /* Link Cost. */
542 return 1;
545 /* Describe Point-to-Point link (Section 12.4.1.1). */
546 static int
547 lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi)
549 int links = 0;
550 struct ospf_neighbor *nbr;
551 struct in_addr id, mask;
552 u_int16_t cost = ospf_link_cost (oi);
554 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
555 zlog_debug ("LSA[Type1]: Set link Point-to-Point");
557 if ((nbr = ospf_nbr_lookup_ptop (oi)))
558 if (nbr->state == NSM_Full)
560 /* For unnumbered point-to-point networks, the Link Data field
561 should specify the interface's MIB-II ifIndex value. */
562 links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
563 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
566 /* Regardless of the state of the neighboring router, we must
567 add a Type 3 link (stub network).
568 N.B. Options 1 & 2 share basically the same logic. */
569 masklen2ip (oi->address->prefixlen, &mask);
570 id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr & mask.s_addr;
571 links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
572 oi->output_cost);
573 return links;
576 /* Describe Broadcast Link. */
577 static int
578 lsa_link_broadcast_set (struct stream *s, struct ospf_interface *oi)
580 struct ospf_neighbor *dr;
581 struct in_addr id, mask;
582 u_int16_t cost = ospf_link_cost (oi);
584 /* Describe Type 3 Link. */
585 if (oi->state == ISM_Waiting)
587 masklen2ip (oi->address->prefixlen, &mask);
588 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
589 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
590 oi->output_cost);
593 dr = ospf_nbr_lookup_by_addr (oi->nbrs, &DR (oi));
594 /* Describe Type 2 link. */
595 if (dr && (dr->state == NSM_Full ||
596 IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))) &&
597 ospf_nbr_count (oi, NSM_Full) > 0)
599 return link_info_set (s, DR (oi), oi->address->u.prefix4,
600 LSA_LINK_TYPE_TRANSIT, 0, cost);
602 /* Describe type 3 link. */
603 else
605 masklen2ip (oi->address->prefixlen, &mask);
606 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
607 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
608 oi->output_cost);
612 static int
613 lsa_link_loopback_set (struct stream *s, struct ospf_interface *oi)
615 struct in_addr id, mask;
617 /* Describe Type 3 Link. */
618 if (oi->state != ISM_Loopback)
619 return 0;
621 mask.s_addr = 0xffffffff;
622 id.s_addr = oi->address->u.prefix4.s_addr;
623 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
626 /* Describe Virtual Link. */
627 static int
628 lsa_link_virtuallink_set (struct stream *s, struct ospf_interface *oi)
630 struct ospf_neighbor *nbr;
631 u_int16_t cost = ospf_link_cost (oi);
633 if (oi->state == ISM_PointToPoint)
634 if ((nbr = ospf_nbr_lookup_ptop (oi)))
635 if (nbr->state == NSM_Full)
637 return link_info_set (s, nbr->router_id, oi->address->u.prefix4,
638 LSA_LINK_TYPE_VIRTUALLINK, 0, cost);
641 return 0;
644 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
646 /* this function add for support point-to-multipoint ,see rfc2328
647 12.4.1.4.*/
648 /* from "edward rrr" <edward_rrr@hotmail.com>
649 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
650 static int
651 lsa_link_ptomp_set (struct stream *s, struct ospf_interface *oi)
653 int links = 0;
654 struct route_node *rn;
655 struct ospf_neighbor *nbr = NULL;
656 struct in_addr id, mask;
657 u_int16_t cost = ospf_link_cost (oi);
659 mask.s_addr = 0xffffffff;
660 id.s_addr = oi->address->u.prefix4.s_addr;
661 links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
663 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
664 zlog_debug ("PointToMultipoint: running ptomultip_set");
666 /* Search neighbor, */
667 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
668 if ((nbr = rn->info) != NULL)
669 /* Ignore myself. */
670 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
671 if (nbr->state == NSM_Full)
674 links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
675 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
676 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
677 zlog_debug ("PointToMultipoint: set link to %s",
678 inet_ntoa(oi->address->u.prefix4));
681 return links;
684 /* Set router-LSA link information. */
685 static int
686 router_lsa_link_set (struct stream *s, struct ospf_area *area)
688 struct listnode *node;
689 struct ospf_interface *oi;
690 int links = 0;
692 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
694 struct interface *ifp = oi->ifp;
696 /* Check interface is up, OSPF is enable. */
697 if (if_is_operative (ifp))
699 if (oi->state != ISM_Down)
701 /* Describe each link. */
702 switch (oi->type)
704 case OSPF_IFTYPE_POINTOPOINT:
705 links += lsa_link_ptop_set (s, oi);
706 break;
707 case OSPF_IFTYPE_BROADCAST:
708 links += lsa_link_broadcast_set (s, oi);
709 break;
710 case OSPF_IFTYPE_NBMA:
711 links += lsa_link_nbma_set (s, oi);
712 break;
713 case OSPF_IFTYPE_POINTOMULTIPOINT:
714 links += lsa_link_ptomp_set (s, oi);
715 break;
716 case OSPF_IFTYPE_VIRTUALLINK:
717 links += lsa_link_virtuallink_set (s, oi);
718 break;
719 case OSPF_IFTYPE_LOOPBACK:
720 links += lsa_link_loopback_set (s, oi);
726 return links;
729 /* Set router-LSA body. */
730 static void
731 ospf_router_lsa_body_set (struct stream *s, struct ospf_area *area)
733 unsigned long putp;
734 u_int16_t cnt;
736 /* Set flags. */
737 stream_putc (s, router_lsa_flags (area));
739 /* Set Zero fields. */
740 stream_putc (s, 0);
742 /* Keep pointer to # links. */
743 putp = stream_get_endp(s);
745 /* Forward word */
746 stream_putw(s, 0);
748 /* Set all link information. */
749 cnt = router_lsa_link_set (s, area);
751 /* Set # of links here. */
752 stream_putw_at (s, putp, cnt);
755 static int
756 ospf_stub_router_timer (struct thread *t)
758 struct ospf_area *area = THREAD_ARG (t);
760 area->t_stub_router = NULL;
762 SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
764 /* clear stub route state and generate router-lsa refresh, don't
765 * clobber an administratively set stub-router state though.
767 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
768 return 0;
770 UNSET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
772 ospf_router_lsa_timer_add (area);
774 return 0;
777 inline static void
778 ospf_stub_router_check (struct ospf_area *area)
780 /* area must either be administratively configured to be stub
781 * or startup-time stub-router must be configured and we must in a pre-stub
782 * state.
784 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
786 SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
787 return;
790 /* not admin-stubbed, check whether startup stubbing is configured and
791 * whether it's not been done yet
793 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED))
794 return;
796 if (area->ospf->stub_router_startup_time == OSPF_STUB_ROUTER_UNCONFIGURED)
798 /* stub-router is hence done forever for this area, even if someone
799 * tries configure it (take effect next restart).
801 SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
802 return;
805 /* startup stub-router configured and not yet done */
806 SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
808 OSPF_AREA_TIMER_ON (area->t_stub_router, ospf_stub_router_timer,
809 area->ospf->stub_router_startup_time);
812 /* Create new router-LSA. */
813 static struct ospf_lsa *
814 ospf_router_lsa_new (struct ospf_area *area)
816 struct ospf *ospf = area->ospf;
817 struct stream *s;
818 struct lsa_header *lsah;
819 struct ospf_lsa *new;
820 int length;
822 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
823 zlog_debug ("LSA[Type1]: Create router-LSA instance");
825 /* check whether stub-router is desired, and if this is the first
826 * router LSA.
828 ospf_stub_router_check (area);
830 /* Create a stream for LSA. */
831 s = stream_new (OSPF_MAX_LSA_SIZE);
832 /* Set LSA common header fields. */
833 lsa_header_set (s, LSA_OPTIONS_GET (area) | LSA_OPTIONS_NSSA_GET (area),
834 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
836 /* Set router-LSA body fields. */
837 ospf_router_lsa_body_set (s, area);
839 /* Set length. */
840 length = stream_get_endp (s);
841 lsah = (struct lsa_header *) STREAM_DATA (s);
842 lsah->length = htons (length);
844 /* Now, create OSPF LSA instance. */
845 if ( (new = ospf_lsa_new ()) == NULL)
847 zlog_err ("%s: Unable to create new lsa", __func__);
848 return NULL;
851 new->area = area;
852 SET_FLAG (new->flags, OSPF_LSA_SELF);
854 /* Copy LSA data to store, discard stream. */
855 new->data = ospf_lsa_data_new (length);
856 memcpy (new->data, lsah, length);
857 stream_free (s);
859 return new;
862 /* Originate Router-LSA. */
863 static struct ospf_lsa *
864 ospf_router_lsa_originate (struct ospf_area *area)
866 struct ospf_lsa *new;
868 /* Create new router-LSA instance. */
869 if ( (new = ospf_router_lsa_new (area)) == NULL)
871 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
872 return NULL;
875 /* Sanity check. */
876 if (new->data->adv_router.s_addr == 0)
878 if (IS_DEBUG_OSPF_EVENT)
879 zlog_debug ("LSA[Type1]: AdvRouter is 0, discard");
880 ospf_lsa_discard (new);
881 return NULL;
884 /* Install LSA to LSDB. */
885 new = ospf_lsa_install (area->ospf, NULL, new);
887 /* Update LSA origination count. */
888 area->ospf->lsa_originate_count++;
890 /* Flooding new LSA through area. */
891 ospf_flood_through_area (area, NULL, new);
893 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
895 zlog_debug ("LSA[Type%d:%s]: Originate router-LSA %p",
896 new->data->type, inet_ntoa (new->data->id), new);
897 ospf_lsa_header_dump (new->data);
900 return new;
903 /* Refresh router-LSA. */
904 static struct ospf_lsa *
905 ospf_router_lsa_refresh (struct ospf_lsa *lsa)
907 struct ospf_area *area = lsa->area;
908 struct ospf_lsa *new;
910 /* Sanity check. */
911 assert (lsa->data);
913 /* Delete LSA from neighbor retransmit-list. */
914 ospf_ls_retransmit_delete_nbr_area (area, lsa);
916 /* Create new router-LSA instance. */
917 if ( (new = ospf_router_lsa_new (area)) == NULL)
919 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
920 return NULL;
923 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
925 ospf_lsa_install (area->ospf, NULL, new);
927 /* Flood LSA through area. */
928 ospf_flood_through_area (area, NULL, new);
930 /* Debug logging. */
931 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
933 zlog_debug ("LSA[Type%d:%s]: router-LSA refresh",
934 new->data->type, inet_ntoa (new->data->id));
935 ospf_lsa_header_dump (new->data);
938 return NULL;
941 static int
942 ospf_router_lsa_timer (struct thread *t)
944 struct ospf_area *area;
946 if (IS_DEBUG_OSPF_EVENT)
947 zlog_debug ("Timer[router-LSA]: (router-LSA Refresh expire)");
949 area = THREAD_ARG (t);
950 area->t_router_lsa_self = NULL;
952 /* Now refresh router-LSA. */
953 if (area->router_lsa_self)
954 ospf_router_lsa_refresh (area->router_lsa_self);
955 /* Newly originate router-LSA. */
956 else
957 ospf_router_lsa_originate (area);
959 return 0;
962 void
963 ospf_router_lsa_timer_add (struct ospf_area *area)
965 /* Keep area's self-originated router-LSA. */
966 struct ospf_lsa *lsa = area->router_lsa_self;
968 /* Cancel previously scheduled router-LSA timer. */
969 if (area->t_router_lsa_self)
970 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
971 zlog_debug ("LSA[Type1]: Cancel previous router-LSA timer");
973 OSPF_TIMER_OFF (area->t_router_lsa_self);
975 /* If router-LSA is originated previously, check the interval time. */
976 if (lsa)
978 int delay;
979 if ((delay = ospf_lsa_refresh_delay (lsa)) > 0)
981 OSPF_AREA_TIMER_ON (area->t_router_lsa_self,
982 ospf_router_lsa_timer, delay);
983 return;
987 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
988 zlog_debug ("LSA[Type1]: Scheduling router-LSA origination right away");
990 /* Immediately refresh router-LSA. */
991 OSPF_AREA_TIMER_ON (area->t_router_lsa_self, ospf_router_lsa_timer, 0);
995 ospf_router_lsa_update_timer (struct thread *thread)
997 struct ospf *ospf = THREAD_ARG (thread);
998 struct listnode *node, *nnode;
999 struct ospf_area *area;
1001 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1002 zlog_debug ("Timer[router-LSA Update]: (timer expire)");
1004 ospf->t_router_lsa_update = NULL;
1006 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
1008 struct ospf_lsa *lsa = area->router_lsa_self;
1009 struct router_lsa *rl;
1010 const char *area_str;
1012 /* Keep Area ID string. */
1013 area_str = AREA_NAME (area);
1015 /* If LSA not exist in this Area, originate new. */
1016 if (lsa == NULL)
1018 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1019 zlog_debug("LSA[Type1]: Create router-LSA for Area %s", area_str);
1021 ospf_router_lsa_originate (area);
1023 /* If router-ID is changed, Link ID must change.
1024 First flush old LSA, then originate new. */
1025 else if (!IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
1027 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1028 zlog_debug("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
1029 lsa->data->type, inet_ntoa (lsa->data->id), area_str);
1030 ospf_lsa_flush_area (lsa, area);
1031 ospf_lsa_unlock (&area->router_lsa_self);
1032 area->router_lsa_self = NULL;
1034 /* Refresh router-LSA, (not install) and flood through area. */
1035 ospf_router_lsa_timer_add (area);
1037 else
1039 rl = (struct router_lsa *) lsa->data;
1040 /* Refresh router-LSA, (not install) and flood through area. */
1041 if (rl->flags != ospf->flags)
1042 ospf_router_lsa_timer_add (area);
1046 return 0;
1050 /* network-LSA related functions. */
1051 /* Originate Network-LSA. */
1052 static void
1053 ospf_network_lsa_body_set (struct stream *s, struct ospf_interface *oi)
1055 struct in_addr mask;
1056 struct route_node *rn;
1057 struct ospf_neighbor *nbr;
1059 masklen2ip (oi->address->prefixlen, &mask);
1060 stream_put_ipv4 (s, mask.s_addr);
1062 /* The network-LSA lists those routers that are fully adjacent to
1063 the Designated Router; each fully adjacent router is identified by
1064 its OSPF Router ID. The Designated Router includes itself in this
1065 list. RFC2328, Section 12.4.2 */
1067 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
1068 if ((nbr = rn->info) != NULL)
1069 if (nbr->state == NSM_Full || nbr == oi->nbr_self)
1070 stream_put_ipv4 (s, nbr->router_id.s_addr);
1073 static struct ospf_lsa *
1074 ospf_network_lsa_new (struct ospf_interface *oi)
1076 struct stream *s;
1077 struct ospf_lsa *new;
1078 struct lsa_header *lsah;
1079 int length;
1081 /* If there are no neighbours on this network (the net is stub),
1082 the router does not originate network-LSA (see RFC 12.4.2) */
1083 if (oi->full_nbrs == 0)
1084 return NULL;
1086 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1087 zlog_debug ("LSA[Type2]: Create network-LSA instance");
1089 /* Create new stream for LSA. */
1090 s = stream_new (OSPF_MAX_LSA_SIZE);
1091 lsah = (struct lsa_header *) STREAM_DATA (s);
1093 lsa_header_set (s, (OPTIONS (oi) | LSA_OPTIONS_GET (oi->area)),
1094 OSPF_NETWORK_LSA, DR (oi), oi->ospf->router_id);
1096 /* Set network-LSA body fields. */
1097 ospf_network_lsa_body_set (s, oi);
1099 /* Set length. */
1100 length = stream_get_endp (s);
1101 lsah->length = htons (length);
1103 /* Create OSPF LSA instance. */
1104 if ( (new = ospf_lsa_new ()) == NULL)
1106 zlog_err ("%s: ospf_lsa_new returned NULL", __func__);
1107 return NULL;
1110 new->area = oi->area;
1111 SET_FLAG (new->flags, OSPF_LSA_SELF);
1113 /* Copy LSA to store. */
1114 new->data = ospf_lsa_data_new (length);
1115 memcpy (new->data, lsah, length);
1116 stream_free (s);
1118 return new;
1121 /* Originate network-LSA. */
1122 static struct ospf_lsa *
1123 ospf_network_lsa_originate (struct ospf_interface *oi)
1125 struct ospf_lsa *new;
1127 /* Create new network-LSA instance. */
1128 new = ospf_network_lsa_new (oi);
1129 if (new == NULL)
1130 return NULL;
1132 /* Install LSA to LSDB. */
1133 new = ospf_lsa_install (oi->ospf, oi, new);
1135 /* Update LSA origination count. */
1136 oi->ospf->lsa_originate_count++;
1138 /* Flooding new LSA through area. */
1139 ospf_flood_through_area (oi->area, NULL, new);
1141 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1143 zlog_debug ("LSA[Type%d:%s]: Originate network-LSA %p",
1144 new->data->type, inet_ntoa (new->data->id), new);
1145 ospf_lsa_header_dump (new->data);
1148 return new;
1152 ospf_network_lsa_refresh (struct ospf_lsa *lsa, struct ospf_interface *oi)
1154 struct ospf_area *area = lsa->area;
1155 struct ospf_lsa *new;
1157 assert (lsa->data);
1159 /* Delete LSA from neighbor retransmit-list. */
1160 ospf_ls_retransmit_delete_nbr_area (area, lsa);
1162 /* Create new network-LSA instance. */
1163 new = ospf_network_lsa_new (oi);
1164 if (new == NULL)
1165 return -1;
1166 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1168 ospf_lsa_install (area->ospf, oi, new);
1170 /* Flood LSA through aera. */
1171 ospf_flood_through_area (area, NULL, new);
1173 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1175 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh",
1176 new->data->type, inet_ntoa (new->data->id));
1177 ospf_lsa_header_dump (new->data);
1180 return 0;
1183 static int
1184 ospf_network_lsa_refresh_timer (struct thread *t)
1186 struct ospf_interface *oi;
1188 oi = THREAD_ARG (t);
1189 oi->t_network_lsa_self = NULL;
1191 if (oi->network_lsa_self)
1192 /* Now refresh network-LSA. */
1193 ospf_network_lsa_refresh (oi->network_lsa_self, oi);
1194 else
1195 /* Newly create network-LSA. */
1196 ospf_network_lsa_originate (oi);
1198 return 0;
1201 void
1202 ospf_network_lsa_timer_add (struct ospf_interface *oi)
1204 /* Keep interface's self-originated network-LSA. */
1205 struct ospf_lsa *lsa = oi->network_lsa_self;
1207 /* Cancel previously schedules network-LSA timer. */
1208 if (oi->t_network_lsa_self)
1209 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1210 zlog_debug ("LSA[Type2]: Cancel previous network-LSA timer");
1211 OSPF_TIMER_OFF (oi->t_network_lsa_self);
1213 /* If network-LSA is originated previously, check the interval time. */
1214 if (lsa)
1216 int delay;
1217 if ((delay = ospf_lsa_refresh_delay (lsa)) > 0)
1219 oi->t_network_lsa_self =
1220 thread_add_timer (master, ospf_network_lsa_refresh_timer,
1221 oi, delay);
1222 return;
1226 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1227 zlog_debug ("Scheduling network-LSA origination right away");
1229 /* Immediately refresh network-LSA. */
1230 oi->t_network_lsa_self =
1231 thread_add_event (master, ospf_network_lsa_refresh_timer, oi, 0);
1235 static void
1236 stream_put_ospf_metric (struct stream *s, u_int32_t metric_value)
1238 u_int32_t metric;
1239 char *mp;
1241 /* Put 0 metric. TOS metric is not supported. */
1242 metric = htonl (metric_value);
1243 mp = (char *) &metric;
1244 mp++;
1245 stream_put (s, mp, 3);
1248 /* summary-LSA related functions. */
1249 static void
1250 ospf_summary_lsa_body_set (struct stream *s, struct prefix *p,
1251 u_int32_t metric)
1253 struct in_addr mask;
1255 masklen2ip (p->prefixlen, &mask);
1257 /* Put Network Mask. */
1258 stream_put_ipv4 (s, mask.s_addr);
1260 /* Set # TOS. */
1261 stream_putc (s, (u_char) 0);
1263 /* Set metric. */
1264 stream_put_ospf_metric (s, metric);
1267 static struct ospf_lsa *
1268 ospf_summary_lsa_new (struct ospf_area *area, struct prefix *p,
1269 u_int32_t metric, struct in_addr id)
1271 struct stream *s;
1272 struct ospf_lsa *new;
1273 struct lsa_header *lsah;
1274 int length;
1276 if (id.s_addr == 0xffffffff)
1278 /* Maybe Link State ID not available. */
1279 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1280 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1281 OSPF_SUMMARY_LSA);
1282 return NULL;
1285 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1286 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1288 /* Create new stream for LSA. */
1289 s = stream_new (OSPF_MAX_LSA_SIZE);
1290 lsah = (struct lsa_header *) STREAM_DATA (s);
1292 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_SUMMARY_LSA,
1293 id, area->ospf->router_id);
1295 /* Set summary-LSA body fields. */
1296 ospf_summary_lsa_body_set (s, p, metric);
1298 /* Set length. */
1299 length = stream_get_endp (s);
1300 lsah->length = htons (length);
1302 /* Create OSPF LSA instance. */
1303 new = ospf_lsa_new ();
1304 new->area = area;
1305 SET_FLAG (new->flags, OSPF_LSA_SELF);
1307 /* Copy LSA to store. */
1308 new->data = ospf_lsa_data_new (length);
1309 memcpy (new->data, lsah, length);
1310 stream_free (s);
1312 return new;
1315 /* Originate Summary-LSA. */
1316 struct ospf_lsa *
1317 ospf_summary_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1318 struct ospf_area *area)
1320 struct ospf_lsa *new;
1321 struct in_addr id;
1323 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
1325 if (id.s_addr == 0xffffffff)
1327 /* Maybe Link State ID not available. */
1328 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1329 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1330 OSPF_SUMMARY_LSA);
1331 return NULL;
1334 /* Create new summary-LSA instance. */
1335 if ( !(new = ospf_summary_lsa_new (area, (struct prefix *) p, metric, id)))
1336 return NULL;
1338 /* Instlal LSA to LSDB. */
1339 new = ospf_lsa_install (area->ospf, NULL, new);
1341 /* Update LSA origination count. */
1342 area->ospf->lsa_originate_count++;
1344 /* Flooding new LSA through area. */
1345 ospf_flood_through_area (area, NULL, new);
1347 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1349 zlog_debug ("LSA[Type%d:%s]: Originate summary-LSA %p",
1350 new->data->type, inet_ntoa (new->data->id), new);
1351 ospf_lsa_header_dump (new->data);
1354 return new;
1357 struct ospf_lsa*
1358 ospf_summary_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1360 struct ospf_lsa *new;
1361 struct summary_lsa *sl;
1362 struct prefix p;
1364 /* Sanity check. */
1365 assert (lsa->data);
1367 sl = (struct summary_lsa *)lsa->data;
1368 p.prefixlen = ip_masklen (sl->mask);
1369 new = ospf_summary_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1370 sl->header.id);
1372 if (!new)
1373 return NULL;
1375 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1377 /* Re-calculate checksum. */
1378 ospf_lsa_checksum (new->data);
1380 ospf_lsa_install (ospf, NULL, new);
1382 /* Flood LSA through AS. */
1383 ospf_flood_through_area (new->area, NULL, new);
1385 /* Debug logging. */
1386 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1388 zlog_debug ("LSA[Type%d:%s]: summary-LSA refresh",
1389 new->data->type, inet_ntoa (new->data->id));
1390 ospf_lsa_header_dump (new->data);
1393 return new;
1397 /* summary-ASBR-LSA related functions. */
1398 static void
1399 ospf_summary_asbr_lsa_body_set (struct stream *s, struct prefix *p,
1400 u_int32_t metric)
1402 struct in_addr mask;
1404 masklen2ip (p->prefixlen, &mask);
1406 /* Put Network Mask. */
1407 stream_put_ipv4 (s, mask.s_addr);
1409 /* Set # TOS. */
1410 stream_putc (s, (u_char) 0);
1412 /* Set metric. */
1413 stream_put_ospf_metric (s, metric);
1416 static struct ospf_lsa *
1417 ospf_summary_asbr_lsa_new (struct ospf_area *area, struct prefix *p,
1418 u_int32_t metric, struct in_addr id)
1420 struct stream *s;
1421 struct ospf_lsa *new;
1422 struct lsa_header *lsah;
1423 int length;
1425 if (id.s_addr == 0xffffffff)
1427 /* Maybe Link State ID not available. */
1428 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1429 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1430 OSPF_ASBR_SUMMARY_LSA);
1431 return NULL;
1434 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1435 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1437 /* Create new stream for LSA. */
1438 s = stream_new (OSPF_MAX_LSA_SIZE);
1439 lsah = (struct lsa_header *) STREAM_DATA (s);
1441 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_ASBR_SUMMARY_LSA,
1442 id, area->ospf->router_id);
1444 /* Set summary-LSA body fields. */
1445 ospf_summary_asbr_lsa_body_set (s, p, metric);
1447 /* Set length. */
1448 length = stream_get_endp (s);
1449 lsah->length = htons (length);
1451 /* Create OSPF LSA instance. */
1452 new = ospf_lsa_new ();
1453 new->area = area;
1454 SET_FLAG (new->flags, OSPF_LSA_SELF);
1456 /* Copy LSA to store. */
1457 new->data = ospf_lsa_data_new (length);
1458 memcpy (new->data, lsah, length);
1459 stream_free (s);
1461 return new;
1464 /* Originate summary-ASBR-LSA. */
1465 struct ospf_lsa *
1466 ospf_summary_asbr_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1467 struct ospf_area *area)
1469 struct ospf_lsa *new;
1470 struct in_addr id;
1472 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA, p);
1474 if (id.s_addr == 0xffffffff)
1476 /* Maybe Link State ID not available. */
1477 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1478 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1479 OSPF_ASBR_SUMMARY_LSA);
1480 return NULL;
1483 /* Create new summary-LSA instance. */
1484 new = ospf_summary_asbr_lsa_new (area, (struct prefix *) p, metric, id);
1485 if (!new)
1486 return NULL;
1488 /* Install LSA to LSDB. */
1489 new = ospf_lsa_install (area->ospf, NULL, new);
1491 /* Update LSA origination count. */
1492 area->ospf->lsa_originate_count++;
1494 /* Flooding new LSA through area. */
1495 ospf_flood_through_area (area, NULL, new);
1497 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1499 zlog_debug ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1500 new->data->type, inet_ntoa (new->data->id), new);
1501 ospf_lsa_header_dump (new->data);
1504 return new;
1507 struct ospf_lsa*
1508 ospf_summary_asbr_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1510 struct ospf_lsa *new;
1511 struct summary_lsa *sl;
1512 struct prefix p;
1514 /* Sanity check. */
1515 assert (lsa->data);
1517 sl = (struct summary_lsa *)lsa->data;
1518 p.prefixlen = ip_masklen (sl->mask);
1519 new = ospf_summary_asbr_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1520 sl->header.id);
1521 if (!new)
1522 return NULL;
1524 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1526 /* Re-calculate checksum. */
1527 ospf_lsa_checksum (new->data);
1529 ospf_lsa_install (ospf, NULL, new);
1531 /* Flood LSA through area. */
1532 ospf_flood_through_area (new->area, NULL, new);
1534 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1536 zlog_debug ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1537 new->data->type, inet_ntoa (new->data->id));
1538 ospf_lsa_header_dump (new->data);
1541 return new;
1544 /* AS-external-LSA related functions. */
1546 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1547 is connected, else 0*/
1548 static struct in_addr
1549 ospf_external_lsa_nexthop_get (struct ospf *ospf, struct in_addr nexthop)
1551 struct in_addr fwd;
1552 struct prefix nh;
1553 struct listnode *node;
1554 struct ospf_interface *oi;
1556 fwd.s_addr = 0;
1558 if (!nexthop.s_addr)
1559 return fwd;
1561 /* Check whether nexthop is covered by OSPF network. */
1562 nh.family = AF_INET;
1563 nh.u.prefix4 = nexthop;
1564 nh.prefixlen = IPV4_MAX_BITLEN;
1566 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
1567 if (if_is_operative (oi->ifp))
1568 if (oi->address->family == AF_INET)
1569 if (prefix_match (oi->address, &nh))
1570 return nexthop;
1572 return fwd;
1575 /* NSSA-external-LSA related functions. */
1577 /* Get 1st IP connection for Forward Addr */
1579 struct in_addr
1580 ospf_get_ip_from_ifp (struct ospf_interface *oi)
1582 struct in_addr fwd;
1584 fwd.s_addr = 0;
1586 if (if_is_operative (oi->ifp))
1587 return oi->address->u.prefix4;
1589 return fwd;
1592 /* Get 1st IP connection for Forward Addr */
1593 struct in_addr
1594 ospf_get_nssa_ip (struct ospf_area *area)
1596 struct in_addr fwd;
1597 struct in_addr best_default;
1598 struct listnode *node;
1599 struct ospf_interface *oi;
1601 fwd.s_addr = 0;
1602 best_default.s_addr = 0;
1604 for (ALL_LIST_ELEMENTS_RO (area->ospf->oiflist, node, oi))
1606 if (if_is_operative (oi->ifp))
1607 if (oi->area->external_routing == OSPF_AREA_NSSA)
1608 if (oi->address && oi->address->family == AF_INET)
1610 if (best_default.s_addr == 0)
1611 best_default = oi->address->u.prefix4;
1612 if (oi->area == area)
1613 return oi->address->u.prefix4;
1616 if (best_default.s_addr != 0)
1617 return best_default;
1619 if (best_default.s_addr != 0)
1620 return best_default;
1622 return fwd;
1625 #define DEFAULT_DEFAULT_METRIC 20
1626 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1627 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1629 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1632 metric_type (struct ospf *ospf, u_char src)
1634 return (ospf->dmetric[src].type < 0 ?
1635 DEFAULT_METRIC_TYPE : ospf->dmetric[src].type);
1639 metric_value (struct ospf *ospf, u_char src)
1641 if (ospf->dmetric[src].value < 0)
1643 if (src == DEFAULT_ROUTE)
1645 if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1646 return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1647 else
1648 return DEFAULT_DEFAULT_ALWAYS_METRIC;
1650 else if (ospf->default_metric < 0)
1651 return DEFAULT_DEFAULT_METRIC;
1652 else
1653 return ospf->default_metric;
1656 return ospf->dmetric[src].value;
1659 /* Set AS-external-LSA body. */
1660 static void
1661 ospf_external_lsa_body_set (struct stream *s, struct external_info *ei,
1662 struct ospf *ospf)
1664 struct prefix_ipv4 *p = &ei->p;
1665 struct in_addr mask, fwd_addr;
1666 u_int32_t mvalue;
1667 int mtype;
1668 int type;
1670 /* Put Network Mask. */
1671 masklen2ip (p->prefixlen, &mask);
1672 stream_put_ipv4 (s, mask.s_addr);
1674 /* If prefix is default, specify DEFAULT_ROUTE. */
1675 type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
1677 mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ?
1678 ROUTEMAP_METRIC_TYPE (ei) : metric_type (ospf, type);
1680 mvalue = (ROUTEMAP_METRIC (ei) != -1) ?
1681 ROUTEMAP_METRIC (ei) : metric_value (ospf, type);
1683 /* Put type of external metric. */
1684 stream_putc (s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1686 /* Put 0 metric. TOS metric is not supported. */
1687 stream_put_ospf_metric (s, mvalue);
1689 /* Get forwarding address to nexthop if on the Connection List, else 0. */
1690 fwd_addr = ospf_external_lsa_nexthop_get (ospf, ei->nexthop);
1692 /* Put forwarding address. */
1693 stream_put_ipv4 (s, fwd_addr.s_addr);
1695 /* Put route tag -- This value should be introduced from configuration. */
1696 stream_putl (s, 0);
1699 /* Create new external-LSA. */
1700 static struct ospf_lsa *
1701 ospf_external_lsa_new (struct ospf *ospf,
1702 struct external_info *ei, struct in_addr *old_id)
1704 struct stream *s;
1705 struct lsa_header *lsah;
1706 struct ospf_lsa *new;
1707 struct in_addr id;
1708 int length;
1710 if (ei == NULL)
1712 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1713 zlog_debug ("LSA[Type5]: External info is NULL, could not originated");
1714 return NULL;
1717 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1718 zlog_debug ("LSA[Type5]: Originate AS-external-LSA instance");
1720 /* If old Link State ID is specified, refresh LSA with same ID. */
1721 if (old_id)
1722 id = *old_id;
1723 /* Get Link State with unique ID. */
1724 else
1726 id = ospf_lsa_unique_id (ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA, &ei->p);
1727 if (id.s_addr == 0xffffffff)
1729 /* Maybe Link State ID not available. */
1730 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1731 zlog_debug ("LSA[Type5]: Link ID not available, can't originate");
1732 return NULL;
1736 /* Create new stream for LSA. */
1737 s = stream_new (OSPF_MAX_LSA_SIZE);
1738 lsah = (struct lsa_header *) STREAM_DATA (s);
1740 /* Set LSA common header fields. */
1741 lsa_header_set (s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA,
1742 id, ospf->router_id);
1744 /* Set AS-external-LSA body fields. */
1745 ospf_external_lsa_body_set (s, ei, ospf);
1747 /* Set length. */
1748 length = stream_get_endp (s);
1749 lsah->length = htons (length);
1751 /* Now, create OSPF LSA instance. */
1752 new = ospf_lsa_new ();
1753 new->area = NULL;
1754 SET_FLAG (new->flags, OSPF_LSA_SELF|OSPF_LSA_APPROVED);
1756 /* Copy LSA data to store, discard stream. */
1757 new->data = ospf_lsa_data_new (length);
1758 memcpy (new->data, lsah, length);
1759 stream_free (s);
1761 return new;
1764 /* As Type-7 */
1765 static void
1766 ospf_install_flood_nssa (struct ospf *ospf,
1767 struct ospf_lsa *lsa, struct external_info *ei)
1769 struct ospf_lsa *new;
1770 struct as_external_lsa *extlsa;
1771 struct ospf_area *area;
1772 struct listnode *node, *nnode;
1774 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1775 * which originated from an NSSA area. In which case it should not be
1776 * flooded back to NSSA areas.
1778 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
1779 return;
1781 /* NSSA Originate or Refresh (If anyNSSA)
1783 LSA is self-originated. And just installed as Type-5.
1784 Additionally, install as Type-7 LSDB for every attached NSSA.
1786 P-Bit controls which ABR performs translation to outside world; If
1787 we are an ABR....do not set the P-bit, because we send the Type-5,
1788 not as the ABR Translator, but as the ASBR owner within the AS!
1790 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1791 elected ABR Translator will see the P-bit, Translate, and re-flood.
1793 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1794 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1796 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
1798 /* Don't install Type-7 LSA's into nonNSSA area */
1799 if (area->external_routing != OSPF_AREA_NSSA)
1800 continue;
1802 /* make lsa duplicate, lock=1 */
1803 new = ospf_lsa_dup (lsa);
1804 new->area = area;
1805 new->data->type = OSPF_AS_NSSA_LSA;
1807 /* set P-bit if not ABR */
1808 if (! IS_OSPF_ABR (ospf))
1810 SET_FLAG(new->data->options, OSPF_OPTION_NP);
1812 /* set non-zero FWD ADDR
1814 draft-ietf-ospf-nssa-update-09.txt
1816 if the network between the NSSA AS boundary router and the
1817 adjacent AS is advertised into OSPF as an internal OSPF route,
1818 the forwarding address should be the next op address as is cu
1819 currently done with type-5 LSAs. If the intervening network is
1820 not adversited into OSPF as an internal OSPF route and the
1821 type-7 LSA's P-bit is set a forwarding address should be
1822 selected from one of the router's active OSPF inteface addresses
1823 which belong to the NSSA. If no such addresses exist, then
1824 no type-7 LSA's with the P-bit set should originate from this
1825 router. */
1827 /* kevinm: not updating lsa anymore, just new */
1828 extlsa = (struct as_external_lsa *)(new->data);
1830 if (extlsa->e[0].fwd_addr.s_addr == 0)
1831 extlsa->e[0].fwd_addr = ospf_get_nssa_ip(area); /* this NSSA area in ifp */
1833 if (extlsa->e[0].fwd_addr.s_addr == 0)
1835 if (IS_DEBUG_OSPF_NSSA)
1836 zlog_debug ("LSA[Type-7]: Could not build FWD-ADDR");
1837 ospf_lsa_discard (new);
1838 return;
1841 /* Re-calculate checksum. */
1842 ospf_lsa_checksum (new->data);
1844 /* install also as Type-7 */
1845 ospf_lsa_install (ospf, NULL, new); /* Remove Old, Lock New = 2 */
1847 /* will send each copy, lock=2+n */
1848 ospf_flood_through_as (ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
1852 static struct ospf_lsa *
1853 ospf_lsa_translated_nssa_new (struct ospf *ospf,
1854 struct ospf_lsa *type7)
1857 struct ospf_lsa *new;
1858 struct as_external_lsa *ext, *extnew;
1859 struct external_info ei;
1861 ext = (struct as_external_lsa *)(type7->data);
1863 /* need external_info struct, fill in bare minimum */
1864 ei.p.family = AF_INET;
1865 ei.p.prefix = type7->data->id;
1866 ei.p.prefixlen = ip_masklen (ext->mask);
1867 ei.type = ZEBRA_ROUTE_OSPF;
1868 ei.nexthop = ext->header.adv_router;
1869 ei.route_map_set.metric = -1;
1870 ei.route_map_set.metric_type = -1;
1871 ei.tag = 0;
1873 if ( (new = ospf_external_lsa_new (ospf, &ei, &type7->data->id)) == NULL)
1875 if (IS_DEBUG_OSPF_NSSA)
1876 zlog_debug ("ospf_nssa_translate_originate(): Could not originate "
1877 "Translated Type-5 for %s",
1878 inet_ntoa (ei.p.prefix));
1879 return NULL;
1882 extnew = (struct as_external_lsa *)(new->data);
1884 /* copy over Type-7 data to new */
1885 extnew->e[0].tos = ext->e[0].tos;
1886 extnew->e[0].route_tag = ext->e[0].route_tag;
1887 extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
1888 new->data->ls_seqnum = type7->data->ls_seqnum;
1890 /* add translated flag, checksum and lock new lsa */
1891 SET_FLAG (new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7 */
1892 ospf_lsa_checksum (new->data);
1893 new = ospf_lsa_lock (new);
1895 return new;
1898 /* compare type-5 to type-7
1899 * -1: err, 0: same, 1: different
1901 static int
1902 ospf_lsa_translated_nssa_compare (struct ospf_lsa *t7, struct ospf_lsa *t5)
1905 struct as_external_lsa *e5 = (struct as_external_lsa *)t5,
1906 *e7 = (struct as_external_lsa *)t7;
1909 /* sanity checks */
1910 if (! ((t5->data->type == OSPF_AS_EXTERNAL_LSA)
1911 && (t7->data->type == OSPF_AS_NSSA_LSA)))
1912 return -1;
1914 if (t5->data->id.s_addr != t7->data->id.s_addr)
1915 return -1;
1917 if (t5->data->ls_seqnum != t7->data->ls_seqnum)
1918 return LSA_REFRESH_FORCE;
1920 if (e5->mask.s_addr != e7->mask.s_addr)
1921 return LSA_REFRESH_FORCE;
1923 if (e5->e[0].fwd_addr.s_addr != e7->e[0].fwd_addr.s_addr)
1924 return LSA_REFRESH_FORCE;
1926 if (e5->e[0].route_tag != e7->e[0].route_tag)
1927 return LSA_REFRESH_FORCE;
1929 if (GET_METRIC (e5->e[0].metric) != GET_METRIC (e7->e[0].metric))
1930 return LSA_REFRESH_FORCE;
1932 return LSA_REFRESH_IF_CHANGED;
1935 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1936 struct ospf_lsa *
1937 ospf_translated_nssa_originate (struct ospf *ospf, struct ospf_lsa *type7)
1939 struct ospf_lsa *new;
1940 struct as_external_lsa *extnew;
1942 /* we cant use ospf_external_lsa_originate() as we need to set
1943 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1946 if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
1948 if (IS_DEBUG_OSPF_NSSA)
1949 zlog_debug ("ospf_translated_nssa_originate(): Could not translate "
1950 "Type-7, Id %s, to Type-5",
1951 inet_ntoa (type7->data->id));
1952 return NULL;
1955 extnew = (struct as_external_lsa *)new;
1957 if (IS_DEBUG_OSPF_NSSA)
1959 zlog_debug ("ospf_translated_nssa_originate(): "
1960 "translated Type 7, installed:");
1961 ospf_lsa_header_dump (new->data);
1962 zlog_debug (" Network mask: %d",ip_masklen (extnew->mask));
1963 zlog_debug (" Forward addr: %s", inet_ntoa (extnew->e[0].fwd_addr));
1966 if ( (new = ospf_lsa_install (ospf, NULL, new)) == NULL)
1968 if (IS_DEBUG_OSPF_NSSA);
1969 zlog_debug ("ospf_lsa_translated_nssa_originate(): "
1970 "Could not install LSA "
1971 "id %s", inet_ntoa (type7->data->id));
1972 return NULL;
1975 ospf->lsa_originate_count++;
1976 ospf_flood_through_as (ospf, NULL, new);
1978 return new;
1981 /* Refresh Translated from NSSA AS-external-LSA. */
1982 struct ospf_lsa *
1983 ospf_translated_nssa_refresh (struct ospf *ospf, struct ospf_lsa *type7,
1984 struct ospf_lsa *type5)
1986 struct ospf_lsa *new = NULL;
1988 /* Sanity checks. */
1989 assert (type7 || type5);
1990 if (!(type7 || type5))
1991 return NULL;
1992 if (type7)
1993 assert (type7->data);
1994 if (type5)
1995 assert (type5->data);
1996 assert (ospf->anyNSSA);
1998 /* get required data according to what has been given */
1999 if (type7 && type5 == NULL)
2001 /* find the translated Type-5 for this Type-7 */
2002 struct as_external_lsa *ext = (struct as_external_lsa *)(type7->data);
2003 struct prefix_ipv4 p =
2005 .prefix = type7->data->id,
2006 .prefixlen = ip_masklen (ext->mask),
2007 .family = AF_INET,
2010 type5 = ospf_external_info_find_lsa (ospf, &p);
2012 else if (type5 && type7 == NULL)
2014 /* find the type-7 from which supplied type-5 was translated,
2015 * ie find first type-7 with same LSA Id.
2017 struct listnode *ln, *lnn;
2018 struct route_node *rn;
2019 struct ospf_lsa *lsa;
2020 struct ospf_area *area;
2022 for (ALL_LIST_ELEMENTS (ospf->areas, ln, lnn, area))
2024 if (area->external_routing != OSPF_AREA_NSSA
2025 && !type7)
2026 continue;
2028 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
2030 if (lsa->data->id.s_addr == type5->data->id.s_addr)
2032 type7 = lsa;
2033 break;
2039 /* do we have type7? */
2040 if (!type7)
2042 if (IS_DEBUG_OSPF_NSSA)
2043 zlog_debug ("ospf_translated_nssa_refresh(): no Type-7 found for "
2044 "Type-5 LSA Id %s",
2045 inet_ntoa (type5->data->id));
2046 return NULL;
2049 /* do we have valid translated type5? */
2050 if (type5 == NULL || !CHECK_FLAG (type5->flags, OSPF_LSA_LOCAL_XLT) )
2052 if (IS_DEBUG_OSPF_NSSA)
2053 zlog_debug ("ospf_translated_nssa_refresh(): No translated Type-5 "
2054 "found for Type-7 with Id %s",
2055 inet_ntoa (type7->data->id));
2056 return NULL;
2059 /* Delete LSA from neighbor retransmit-list. */
2060 ospf_ls_retransmit_delete_nbr_as (ospf, type5);
2062 /* create new translated LSA */
2063 if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
2065 if (IS_DEBUG_OSPF_NSSA)
2066 zlog_debug ("ospf_translated_nssa_refresh(): Could not translate "
2067 "Type-7 for %s to Type-5",
2068 inet_ntoa (type7->data->id));
2069 return NULL;
2072 if ( !(new = ospf_lsa_install (ospf, NULL, new)) )
2074 if (IS_DEBUG_OSPF_NSSA)
2075 zlog_debug ("ospf_translated_nssa_refresh(): Could not install "
2076 "translated LSA, Id %s",
2077 inet_ntoa (type7->data->id));
2078 return NULL;
2081 /* Flood LSA through area. */
2082 ospf_flood_through_as (ospf, NULL, new);
2084 return new;
2088 is_prefix_default (struct prefix_ipv4 *p)
2090 struct prefix_ipv4 q;
2092 q.family = AF_INET;
2093 q.prefix.s_addr = 0;
2094 q.prefixlen = 0;
2096 return prefix_same ((struct prefix *) p, (struct prefix *) &q);
2099 /* Originate an AS-external-LSA, install and flood. */
2100 struct ospf_lsa *
2101 ospf_external_lsa_originate (struct ospf *ospf, struct external_info *ei)
2103 struct ospf_lsa *new;
2105 /* Added for NSSA project....
2107 External LSAs are originated in ASBRs as usual, but for NSSA systems.
2108 there is the global Type-5 LSDB and a Type-7 LSDB installed for
2109 every area. The Type-7's are flooded to every IR and every ABR; We
2110 install the Type-5 LSDB so that the normal "refresh" code operates
2111 as usual, and flag them as not used during ASE calculations. The
2112 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2113 Address of non-zero.
2115 If an ABR is the elected NSSA translator, following SPF and during
2116 the ABR task it will translate all the scanned Type-7's, with P-bit
2117 ON and not-self generated, and translate to Type-5's throughout the
2118 non-NSSA/STUB AS.
2120 A difference in operation depends whether this ASBR is an ABR
2121 or not. If not an ABR, the P-bit is ON, to indicate that any
2122 elected NSSA-ABR can perform its translation.
2124 If an ABR, the P-bit is OFF; No ABR will perform translation and
2125 this ASBR will flood the Type-5 LSA as usual.
2127 For the case where this ASBR is not an ABR, the ASE calculations
2128 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2129 demonstrate to the user that there are LSA's that belong to any
2130 attached NSSA.
2132 Finally, it just so happens that when the ABR is translating every
2133 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2134 approved Type-5 (translated from Type-7); at the end of translation
2135 if any Translated Type-5's remain unapproved, then they must be
2136 flushed from the AS.
2140 /* Check the AS-external-LSA should be originated. */
2141 if (!ospf_redistribute_check (ospf, ei, NULL))
2142 return NULL;
2144 /* Create new AS-external-LSA instance. */
2145 if ((new = ospf_external_lsa_new (ospf, ei, NULL)) == NULL)
2147 if (IS_DEBUG_OSPF_EVENT)
2148 zlog_debug ("LSA[Type5:%s]: Could not originate AS-external-LSA",
2149 inet_ntoa (ei->p.prefix));
2150 return NULL;
2153 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2154 ospf_lsa_install (ospf, NULL, new);
2156 /* Update LSA origination count. */
2157 ospf->lsa_originate_count++;
2159 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2160 ospf_flood_through_as (ospf, NULL, new);
2162 /* If there is any attached NSSA, do special handling */
2163 if (ospf->anyNSSA &&
2164 /* stay away from translated LSAs! */
2165 !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
2166 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
2168 /* Debug logging. */
2169 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2171 zlog_debug ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2172 new->data->type, inet_ntoa (new->data->id), new);
2173 ospf_lsa_header_dump (new->data);
2176 return new;
2179 /* Originate AS-external-LSA from external info with initial flag. */
2181 ospf_external_lsa_originate_timer (struct thread *thread)
2183 struct ospf *ospf = THREAD_ARG (thread);
2184 struct route_node *rn;
2185 struct external_info *ei;
2186 struct route_table *rt;
2187 int type = THREAD_VAL (thread);
2189 ospf->t_external_lsa = NULL;
2191 /* Originate As-external-LSA from all type of distribute source. */
2192 if ((rt = EXTERNAL_INFO (type)))
2193 for (rn = route_top (rt); rn; rn = route_next (rn))
2194 if ((ei = rn->info) != NULL)
2195 if (!is_prefix_default ((struct prefix_ipv4 *)&ei->p))
2196 if (!ospf_external_lsa_originate (ospf, ei))
2197 zlog_warn ("LSA: AS-external-LSA was not originated.");
2199 return 0;
2202 static struct external_info *
2203 ospf_default_external_info (struct ospf *ospf)
2205 int type;
2206 struct route_node *rn;
2207 struct prefix_ipv4 p;
2209 p.family = AF_INET;
2210 p.prefix.s_addr = 0;
2211 p.prefixlen = 0;
2213 /* First, lookup redistributed default route. */
2214 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
2215 if (EXTERNAL_INFO (type) && type != ZEBRA_ROUTE_OSPF)
2217 rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
2218 if (rn != NULL)
2220 route_unlock_node (rn);
2221 assert (rn->info);
2222 if (ospf_redistribute_check (ospf, rn->info, NULL))
2223 return rn->info;
2227 return NULL;
2231 ospf_default_originate_timer (struct thread *thread)
2233 struct prefix_ipv4 p;
2234 struct in_addr nexthop;
2235 struct external_info *ei;
2236 struct ospf *ospf;
2238 ospf = THREAD_ARG (thread);
2240 p.family = AF_INET;
2241 p.prefix.s_addr = 0;
2242 p.prefixlen = 0;
2244 if (ospf->default_originate == DEFAULT_ORIGINATE_ALWAYS)
2246 /* If there is no default route via redistribute,
2247 then originate AS-external-LSA with nexthop 0 (self). */
2248 nexthop.s_addr = 0;
2249 ospf_external_info_add (DEFAULT_ROUTE, p, 0, nexthop);
2252 if ((ei = ospf_default_external_info (ospf)))
2253 ospf_external_lsa_originate (ospf, ei);
2255 return 0;
2258 /* Flush any NSSA LSAs for given prefix */
2259 void
2260 ospf_nssa_lsa_flush (struct ospf *ospf, struct prefix_ipv4 *p)
2262 struct listnode *node, *nnode;
2263 struct ospf_lsa *lsa;
2264 struct ospf_area *area;
2266 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
2268 if (area->external_routing == OSPF_AREA_NSSA)
2270 if (!(lsa = ospf_lsa_lookup (area, OSPF_AS_NSSA_LSA, p->prefix,
2271 ospf->router_id)))
2273 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2274 zlog_debug ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2275 inet_ntoa (p->prefix), p->prefixlen);
2276 continue;
2278 ospf_ls_retransmit_delete_nbr_area (area, lsa);
2279 if (!IS_LSA_MAXAGE (lsa))
2281 ospf_refresher_unregister_lsa (ospf, lsa);
2282 ospf_lsa_flush_area (lsa, area);
2288 /* Flush an AS-external-LSA from LSDB and routing domain. */
2289 void
2290 ospf_external_lsa_flush (struct ospf *ospf,
2291 u_char type, struct prefix_ipv4 *p,
2292 unsigned int ifindex /*, struct in_addr nexthop */)
2294 struct ospf_lsa *lsa;
2296 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2297 zlog_debug ("LSA: Flushing AS-external-LSA %s/%d",
2298 inet_ntoa (p->prefix), p->prefixlen);
2300 /* First lookup LSA from LSDB. */
2301 if (!(lsa = ospf_external_info_find_lsa (ospf, p)))
2303 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2304 zlog_debug ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
2305 inet_ntoa (p->prefix), p->prefixlen);
2306 return;
2309 /* If LSA is selforiginated, not a translated LSA, and there is
2310 * NSSA area, flush Type-7 LSA's at first.
2312 if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
2313 && !(CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT)))
2314 ospf_nssa_lsa_flush (ospf, p);
2316 /* Sweep LSA from Link State Retransmit List. */
2317 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2319 /* There must be no self-originated LSA in rtrs_external. */
2320 #if 0
2321 /* Remove External route from Zebra. */
2322 ospf_zebra_delete ((struct prefix_ipv4 *) p, &nexthop);
2323 #endif
2325 if (!IS_LSA_MAXAGE (lsa))
2327 /* Unregister LSA from Refresh queue. */
2328 ospf_refresher_unregister_lsa (ospf, lsa);
2330 /* Flush AS-external-LSA through AS. */
2331 ospf_lsa_flush_as (ospf, lsa);
2334 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2335 zlog_debug ("ospf_external_lsa_flush(): stop");
2338 void
2339 ospf_external_lsa_refresh_default (struct ospf *ospf)
2341 struct prefix_ipv4 p;
2342 struct external_info *ei;
2343 struct ospf_lsa *lsa;
2345 p.family = AF_INET;
2346 p.prefixlen = 0;
2347 p.prefix.s_addr = 0;
2349 ei = ospf_default_external_info (ospf);
2350 lsa = ospf_external_info_find_lsa (ospf, &p);
2352 if (ei)
2354 if (lsa)
2356 if (IS_DEBUG_OSPF_EVENT)
2357 zlog_debug ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p", lsa);
2358 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
2360 else
2362 if (IS_DEBUG_OSPF_EVENT)
2363 zlog_debug ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2364 ospf_external_lsa_originate (ospf, ei);
2367 else
2369 if (lsa)
2371 if (IS_DEBUG_OSPF_EVENT)
2372 zlog_debug ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2373 ospf_lsa_flush_as (ospf, lsa);
2378 void
2379 ospf_external_lsa_refresh_type (struct ospf *ospf, u_char type, int force)
2381 struct route_node *rn;
2382 struct external_info *ei;
2384 if (type != DEFAULT_ROUTE)
2385 if (EXTERNAL_INFO(type))
2386 /* Refresh each redistributed AS-external-LSAs. */
2387 for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
2388 if ((ei = rn->info))
2389 if (!is_prefix_default (&ei->p))
2391 struct ospf_lsa *lsa;
2393 if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
2394 ospf_external_lsa_refresh (ospf, lsa, ei, force);
2395 else
2396 ospf_external_lsa_originate (ospf, ei);
2400 /* Refresh AS-external-LSA. */
2401 void
2402 ospf_external_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa,
2403 struct external_info *ei, int force)
2405 struct ospf_lsa *new;
2406 int changed;
2408 /* Check the AS-external-LSA should be originated. */
2409 if (!ospf_redistribute_check (ospf, ei, &changed))
2411 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2412 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed, "
2413 "redist check fail",
2414 lsa->data->type, inet_ntoa (lsa->data->id));
2415 ospf_external_lsa_flush (ospf, ei->type, &ei->p,
2416 ei->ifindex /*, ei->nexthop */);
2417 return;
2420 if (!changed && !force)
2422 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2423 zlog_debug ("LSA[Type%d:%s]: Not refreshed, not changed/forced",
2424 lsa->data->type, inet_ntoa (lsa->data->id));
2425 return;
2428 /* Delete LSA from neighbor retransmit-list. */
2429 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2431 /* Unregister AS-external-LSA from refresh-list. */
2432 ospf_refresher_unregister_lsa (ospf, lsa);
2434 new = ospf_external_lsa_new (ospf, ei, &lsa->data->id);
2436 if (new == NULL)
2438 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2439 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed", lsa->data->type,
2440 inet_ntoa (lsa->data->id));
2441 return;
2444 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
2446 /* Re-calculate checksum. */
2447 ospf_lsa_checksum (new->data);
2449 ospf_lsa_install (ospf, NULL, new); /* As type-5. */
2451 /* Flood LSA through AS. */
2452 ospf_flood_through_as (ospf, NULL, new);
2454 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2455 if (ospf->anyNSSA && !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
2456 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood per new rules */
2458 /* Register self-originated LSA to refresh queue.
2459 * Translated LSAs should not be registered, but refreshed upon
2460 * refresh of the Type-7
2462 if ( !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT) )
2463 ospf_refresher_register_lsa (ospf, new);
2465 /* Debug logging. */
2466 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2468 zlog_debug ("LSA[Type%d:%s]: AS-external-LSA refresh",
2469 new->data->type, inet_ntoa (new->data->id));
2470 ospf_lsa_header_dump (new->data);
2473 return;
2477 /* LSA installation functions. */
2479 /* Install router-LSA to an area. */
2480 static struct ospf_lsa *
2481 ospf_router_lsa_install (struct ospf *ospf,
2482 struct ospf_lsa *new, int rt_recalc)
2484 struct ospf_area *area = new->area;
2486 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2487 The entire routing table must be recalculated, starting with
2488 the shortest path calculations for each area (not just the
2489 area whose link-state database has changed).
2491 if (rt_recalc)
2492 ospf_spf_calculate_schedule (ospf);
2494 if (IS_LSA_SELF (new))
2496 /* Set router-LSA refresh timer. */
2497 OSPF_TIMER_OFF (area->t_router_lsa_self);
2498 OSPF_AREA_TIMER_ON (area->t_router_lsa_self,
2499 ospf_router_lsa_timer, OSPF_LS_REFRESH_TIME);
2501 /* Set self-originated router-LSA. */
2502 ospf_lsa_unlock (&area->router_lsa_self);
2503 area->router_lsa_self = ospf_lsa_lock (new);
2505 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2506 zlog_debug("LSA[Type%d]: ID %s seq 0x%x is self-originated",
2507 new->data->type, inet_ntoa (new->data->id),
2508 ntohl(new->data->ls_seqnum));
2511 return new;
2514 #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
2515 if (!(T)) \
2516 (T) = thread_add_timer (master, (F), oi, (V))
2518 /* Install network-LSA to an area. */
2519 static struct ospf_lsa *
2520 ospf_network_lsa_install (struct ospf *ospf,
2521 struct ospf_interface *oi,
2522 struct ospf_lsa *new,
2523 int rt_recalc)
2526 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2527 The entire routing table must be recalculated, starting with
2528 the shortest path calculations for each area (not just the
2529 area whose link-state database has changed).
2531 if (rt_recalc)
2532 ospf_spf_calculate_schedule (ospf);
2534 /* We supposed that when LSA is originated by us, we pass the int
2535 for which it was originated. If LSA was received by flooding,
2536 the RECEIVED flag is set, so we do not link the LSA to the int. */
2537 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
2539 /* Set LSRefresh timer. */
2540 OSPF_TIMER_OFF (oi->t_network_lsa_self);
2542 OSPF_INTERFACE_TIMER_ON (oi->t_network_lsa_self,
2543 ospf_network_lsa_refresh_timer,
2544 OSPF_LS_REFRESH_TIME);
2546 ospf_lsa_unlock (&oi->network_lsa_self);
2547 oi->network_lsa_self = ospf_lsa_lock (new);
2550 return new;
2553 /* Install summary-LSA to an area. */
2554 static struct ospf_lsa *
2555 ospf_summary_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2556 int rt_recalc)
2558 if (rt_recalc && !IS_LSA_SELF (new))
2560 /* RFC 2328 Section 13.2 Summary-LSAs
2561 The best route to the destination described by the summary-
2562 LSA must be recalculated (see Section 16.5). If this
2563 destination is an AS boundary router, it may also be
2564 necessary to re-examine all the AS-external-LSAs.
2567 #if 0
2568 /* This doesn't exist yet... */
2569 ospf_summary_incremental_update(new); */
2570 #else /* #if 0 */
2571 ospf_spf_calculate_schedule (ospf);
2572 #endif /* #if 0 */
2574 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2575 zlog_debug ("ospf_summary_lsa_install(): SPF scheduled");
2578 if (IS_LSA_SELF (new))
2579 ospf_refresher_register_lsa (ospf, new);
2581 return new;
2584 /* Install ASBR-summary-LSA to an area. */
2585 static struct ospf_lsa *
2586 ospf_summary_asbr_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2587 int rt_recalc)
2589 if (rt_recalc && !IS_LSA_SELF (new))
2591 /* RFC 2328 Section 13.2 Summary-LSAs
2592 The best route to the destination described by the summary-
2593 LSA must be recalculated (see Section 16.5). If this
2594 destination is an AS boundary router, it may also be
2595 necessary to re-examine all the AS-external-LSAs.
2597 #if 0
2598 /* These don't exist yet... */
2599 ospf_summary_incremental_update(new);
2600 /* Isn't this done by the above call?
2601 - RFC 2328 Section 16.5 implies it should be */
2602 /* ospf_ase_calculate_schedule(); */
2603 #else /* #if 0 */
2604 ospf_spf_calculate_schedule (ospf);
2605 #endif /* #if 0 */
2608 /* register LSA to refresh-list. */
2609 if (IS_LSA_SELF (new))
2610 ospf_refresher_register_lsa (ospf, new);
2612 return new;
2615 /* Install AS-external-LSA. */
2616 static struct ospf_lsa *
2617 ospf_external_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2618 int rt_recalc)
2620 ospf_ase_register_external_lsa (new, ospf);
2621 /* If LSA is not self-originated, calculate an external route. */
2622 if (rt_recalc)
2624 /* RFC 2328 Section 13.2 AS-external-LSAs
2625 The best route to the destination described by the AS-
2626 external-LSA must be recalculated (see Section 16.6).
2629 if (!IS_LSA_SELF (new))
2630 ospf_ase_incremental_update (ospf, new);
2633 if (new->data->type == OSPF_AS_NSSA_LSA)
2635 /* There is no point to register selforiginate Type-7 LSA for
2636 * refreshing. We rely on refreshing Type-5 LSA's
2638 if (IS_LSA_SELF (new))
2639 return new;
2640 else
2642 /* Try refresh type-5 translated LSA for this LSA, if one exists.
2643 * New translations will be taken care of by the abr_task.
2645 ospf_translated_nssa_refresh (ospf, new, NULL);
2649 /* Register self-originated LSA to refresh queue.
2650 * Leave Translated LSAs alone if NSSA is enabled
2652 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT ) )
2653 ospf_refresher_register_lsa (ospf, new);
2655 return new;
2658 void
2659 ospf_discard_from_db (struct ospf *ospf,
2660 struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
2662 struct ospf_lsa *old;
2664 if (!lsdb)
2666 zlog_warn ("%s: Called with NULL lsdb!", __func__);
2667 if (!lsa)
2668 zlog_warn ("%s: and NULL LSA!", __func__);
2669 else
2670 zlog_warn ("LSA[Type%d:%s]: not associated with LSDB!",
2671 lsa->data->type, inet_ntoa (lsa->data->id));
2672 return;
2675 old = ospf_lsdb_lookup (lsdb, lsa);
2677 if (!old)
2678 return;
2680 if (old->refresh_list >= 0)
2681 ospf_refresher_unregister_lsa (ospf, old);
2683 switch (old->data->type)
2685 case OSPF_AS_EXTERNAL_LSA:
2686 ospf_ase_unregister_external_lsa (old, ospf);
2687 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2688 break;
2689 #ifdef HAVE_OPAQUE_LSA
2690 case OSPF_OPAQUE_AS_LSA:
2691 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2692 break;
2693 #endif /* HAVE_OPAQUE_LSA */
2694 case OSPF_AS_NSSA_LSA:
2695 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2696 ospf_ase_unregister_external_lsa (old, ospf);
2697 break;
2698 default:
2699 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2700 break;
2703 ospf_lsa_maxage_delete (ospf, old);
2704 ospf_lsa_discard (old);
2707 struct ospf_lsa *
2708 ospf_lsa_install (struct ospf *ospf, struct ospf_interface *oi,
2709 struct ospf_lsa *lsa)
2711 struct ospf_lsa *new = NULL;
2712 struct ospf_lsa *old = NULL;
2713 struct ospf_lsdb *lsdb = NULL;
2714 int rt_recalc;
2716 /* Set LSDB. */
2717 switch (lsa->data->type)
2719 /* kevinm */
2720 case OSPF_AS_NSSA_LSA:
2721 if (lsa->area)
2722 lsdb = lsa->area->lsdb;
2723 else
2724 lsdb = ospf->lsdb;
2725 break;
2726 case OSPF_AS_EXTERNAL_LSA:
2727 #ifdef HAVE_OPAQUE_LSA
2728 case OSPF_OPAQUE_AS_LSA:
2729 #endif /* HAVE_OPAQUE_LSA */
2730 lsdb = ospf->lsdb;
2731 break;
2732 default:
2733 lsdb = lsa->area->lsdb;
2734 break;
2737 assert (lsdb);
2739 /* RFC 2328 13.2. Installing LSAs in the database
2741 Installing a new LSA in the database, either as the result of
2742 flooding or a newly self-originated LSA, may cause the OSPF
2743 routing table structure to be recalculated. The contents of the
2744 new LSA should be compared to the old instance, if present. If
2745 there is no difference, there is no need to recalculate the
2746 routing table. When comparing an LSA to its previous instance,
2747 the following are all considered to be differences in contents:
2749 o The LSA's Options field has changed.
2751 o One of the LSA instances has LS age set to MaxAge, and
2752 the other does not.
2754 o The length field in the LSA header has changed.
2756 o The body of the LSA (i.e., anything outside the 20-byte
2757 LSA header) has changed. Note that this excludes changes
2758 in LS Sequence Number and LS Checksum.
2761 /* Look up old LSA and determine if any SPF calculation or incremental
2762 update is needed */
2763 old = ospf_lsdb_lookup (lsdb, lsa);
2765 /* Do comparision and record if recalc needed. */
2766 rt_recalc = 0;
2767 if ( old == NULL || ospf_lsa_different(old, lsa))
2768 rt_recalc = 1;
2771 Sequence number check (Section 14.1 of rfc 2328)
2772 "Premature aging is used when it is time for a self-originated
2773 LSA's sequence number field to wrap. At this point, the current
2774 LSA instance (having LS sequence number MaxSequenceNumber) must
2775 be prematurely aged and flushed from the routing domain before a
2776 new instance with sequence number equal to InitialSequenceNumber
2777 can be originated. "
2780 if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER)
2782 if (ospf_lsa_is_self_originated(ospf, lsa))
2784 lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
2786 if (!IS_LSA_MAXAGE(lsa))
2787 lsa->flags |= OSPF_LSA_PREMATURE_AGE;
2788 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
2790 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
2792 zlog_debug ("ospf_lsa_install() Premature Aging "
2793 "lsa 0x%lx", (u_long)lsa);
2794 ospf_lsa_header_dump (lsa->data);
2797 else
2799 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2801 zlog_debug ("ospf_lsa_install() got an lsa with seq 0x80000000 "
2802 "that was not self originated. Ignoring\n");
2803 ospf_lsa_header_dump (lsa->data);
2805 return old;
2809 /* discard old LSA from LSDB */
2810 if (old != NULL)
2811 ospf_discard_from_db (ospf, lsdb, lsa);
2813 /* Calculate Checksum if self-originated?. */
2814 if (IS_LSA_SELF (lsa))
2815 ospf_lsa_checksum (lsa->data);
2817 /* Insert LSA to LSDB. */
2818 ospf_lsdb_add (lsdb, lsa);
2819 lsa->lsdb = lsdb;
2821 /* Do LSA specific installation process. */
2822 switch (lsa->data->type)
2824 case OSPF_ROUTER_LSA:
2825 new = ospf_router_lsa_install (ospf, lsa, rt_recalc);
2826 break;
2827 case OSPF_NETWORK_LSA:
2828 assert (oi);
2829 new = ospf_network_lsa_install (ospf, oi, lsa, rt_recalc);
2830 break;
2831 case OSPF_SUMMARY_LSA:
2832 new = ospf_summary_lsa_install (ospf, lsa, rt_recalc);
2833 break;
2834 case OSPF_ASBR_SUMMARY_LSA:
2835 new = ospf_summary_asbr_lsa_install (ospf, lsa, rt_recalc);
2836 break;
2837 case OSPF_AS_EXTERNAL_LSA:
2838 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2839 break;
2840 #ifdef HAVE_OPAQUE_LSA
2841 case OSPF_OPAQUE_LINK_LSA:
2842 if (IS_LSA_SELF (lsa))
2843 lsa->oi = oi; /* Specify outgoing ospf-interface for this LSA. */
2844 else
2845 ; /* Incoming "oi" for this LSA has set at LSUpd reception. */
2846 /* Fallthrough */
2847 case OSPF_OPAQUE_AREA_LSA:
2848 case OSPF_OPAQUE_AS_LSA:
2849 new = ospf_opaque_lsa_install (lsa, rt_recalc);
2850 break;
2851 #endif /* HAVE_OPAQUE_LSA */
2852 case OSPF_AS_NSSA_LSA:
2853 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2854 default: /* type-6,8,9....nothing special */
2855 break;
2858 if (new == NULL)
2859 return new; /* Installation failed, cannot proceed further -- endo. */
2861 /* Debug logs. */
2862 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2864 char area_str[INET_ADDRSTRLEN];
2866 switch (lsa->data->type)
2868 case OSPF_AS_EXTERNAL_LSA:
2869 #ifdef HAVE_OPAQUE_LSA
2870 case OSPF_OPAQUE_AS_LSA:
2871 #endif /* HAVE_OPAQUE_LSA */
2872 case OSPF_AS_NSSA_LSA:
2873 zlog_debug ("LSA[%s]: Install %s",
2874 dump_lsa_key (new),
2875 LOOKUP (ospf_lsa_type_msg, new->data->type));
2876 break;
2877 default:
2878 strcpy (area_str, inet_ntoa (new->area->area_id));
2879 zlog_debug ("LSA[%s]: Install %s to Area %s",
2880 dump_lsa_key (new),
2881 LOOKUP (ospf_lsa_type_msg, new->data->type), area_str);
2882 break;
2887 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2888 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2890 if ((lsa->flags & OSPF_LSA_PREMATURE_AGE) ||
2891 (IS_LSA_MAXAGE (new) && !IS_LSA_SELF (new)))
2893 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2894 zlog_debug ("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2895 new->data->type,
2896 inet_ntoa (new->data->id),
2897 lsa);
2898 ospf_lsa_maxage (ospf, lsa);
2901 return new;
2905 static int
2906 ospf_check_nbr_status (struct ospf *ospf)
2908 struct listnode *node, *nnode;
2909 struct ospf_interface *oi;
2911 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
2913 struct route_node *rn;
2914 struct ospf_neighbor *nbr;
2916 if (ospf_if_is_enable (oi))
2917 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
2918 if ((nbr = rn->info) != NULL)
2919 if (nbr->state == NSM_Exchange || nbr->state == NSM_Loading)
2921 route_unlock_node (rn);
2922 return 0;
2926 return 1;
2930 #ifdef ORIGINAL_CODING
2931 /* This function flood the maxaged LSA to DR. */
2932 void
2933 ospf_maxage_flood (struct ospf_lsa *lsa)
2935 switch (lsa->data->type)
2937 case OSPF_ROUTER_LSA:
2938 case OSPF_NETWORK_LSA:
2939 case OSPF_SUMMARY_LSA:
2940 case OSPF_ASBR_SUMMARY_LSA:
2941 case OSPF_AS_NSSA_LSA:
2942 #ifdef HAVE_OPAQUE_LSA
2943 case OSPF_OPAQUE_LINK_LSA:
2944 case OSPF_OPAQUE_AREA_LSA:
2945 #endif /* HAVE_OPAQUE_LSA */
2946 ospf_flood_through_area (lsa->area, NULL, lsa);
2947 break;
2948 case OSPF_AS_EXTERNAL_LSA:
2949 #ifdef HAVE_OPAQUE_LSA
2950 case OSPF_OPAQUE_AS_LSA:
2951 #endif /* HAVE_OPAQUE_LSA */
2952 ospf_flood_through_as (NULL, lsa);
2953 break;
2954 default:
2955 break;
2958 #endif /* ORIGINAL_CODING */
2960 static int
2961 ospf_maxage_lsa_remover (struct thread *thread)
2963 struct ospf *ospf = THREAD_ARG (thread);
2964 struct ospf_lsa *lsa;
2965 struct listnode *node, *nnode;
2966 int reschedule = 0;
2968 ospf->t_maxage = NULL;
2970 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2971 zlog_debug ("LSA[MaxAge]: remover Start");
2973 reschedule = !ospf_check_nbr_status (ospf);
2975 if (!reschedule)
2976 for (ALL_LIST_ELEMENTS (ospf->maxage_lsa, node, nnode, lsa))
2978 if (lsa->retransmit_counter > 0)
2980 reschedule = 1;
2981 continue;
2984 /* Remove LSA from the LSDB */
2985 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF))
2986 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2987 zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-oririnated: ",
2988 lsa->data->type, inet_ntoa (lsa->data->id), (u_long)lsa);
2990 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2991 zlog_debug ("LSA[Type%d:%s]: MaxAge LSA removed from list",
2992 lsa->data->type, inet_ntoa (lsa->data->id));
2994 /* Flood max age LSA. */
2995 #ifdef ORIGINAL_CODING
2996 ospf_maxage_flood (lsa);
2997 #else /* ORIGINAL_CODING */
2998 ospf_flood_through (ospf, NULL, lsa);
2999 #endif /* ORIGINAL_CODING */
3001 if (lsa->flags & OSPF_LSA_PREMATURE_AGE)
3003 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3004 zlog_debug ("originating new router lsa for lsa 0x%lx \n",
3005 (u_long)lsa);
3006 ospf_router_lsa_originate(lsa->area);
3009 /* Remove from lsdb. */
3010 if (lsa->lsdb)
3012 ospf_discard_from_db (ospf, lsa->lsdb, lsa);
3013 ospf_lsdb_delete (lsa->lsdb, lsa);
3015 else
3016 zlog_warn ("%s: LSA[Type%d:%s]: No associated LSDB!", __func__,
3017 lsa->data->type, inet_ntoa (lsa->data->id));
3020 /* A MaxAge LSA must be removed immediately from the router's link
3021 state database as soon as both a) it is no longer contained on any
3022 neighbor Link state retransmission lists and b) none of the router's
3023 neighbors are in states Exchange or Loading. */
3024 if (reschedule)
3025 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 2);
3027 return 0;
3030 static int
3031 ospf_lsa_maxage_exist (struct ospf *ospf, struct ospf_lsa *new)
3033 struct listnode *node;
3034 struct ospf_lsa *lsa;
3036 for (ALL_LIST_ELEMENTS_RO (ospf->maxage_lsa, node, lsa))
3037 if (lsa == new)
3038 return 1;
3040 return 0;
3043 void
3044 ospf_lsa_maxage_delete (struct ospf *ospf, struct ospf_lsa *lsa)
3046 struct listnode *n;
3048 if ((n = listnode_lookup (ospf->maxage_lsa, lsa)))
3050 list_delete_node (ospf->maxage_lsa, n);
3051 ospf_lsa_unlock (&lsa); /* maxage_lsa */
3055 void
3056 ospf_lsa_maxage (struct ospf *ospf, struct ospf_lsa *lsa)
3058 /* When we saw a MaxAge LSA flooded to us, we put it on the list
3059 and schedule the MaxAge LSA remover. */
3060 if (ospf_lsa_maxage_exist (ospf, lsa))
3062 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3063 zlog_debug ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
3064 lsa->data->type, inet_ntoa (lsa->data->id), lsa);
3065 return;
3068 listnode_add (ospf->maxage_lsa, ospf_lsa_lock (lsa));
3070 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3071 zlog_debug ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa));
3073 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 2);
3076 static int
3077 ospf_lsa_maxage_walker_remover (struct ospf *ospf, struct ospf_lsa *lsa)
3079 /* Stay away from any Local Translated Type-7 LSAs */
3080 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
3081 return 0;
3083 if (IS_LSA_MAXAGE (lsa))
3084 /* Self-originated LSAs should NOT time-out instead,
3085 they're flushed and submitted to the max_age list explicitly. */
3086 if (!ospf_lsa_is_self_originated (ospf, lsa))
3088 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3089 zlog_debug("LSA[%s]: is MaxAge", dump_lsa_key (lsa));
3091 switch (lsa->data->type)
3093 #ifdef HAVE_OPAQUE_LSA
3094 case OSPF_OPAQUE_LINK_LSA:
3095 case OSPF_OPAQUE_AREA_LSA:
3096 case OSPF_OPAQUE_AS_LSA:
3098 * As a general rule, whenever network topology has changed
3099 * (due to an LSA removal in this case), routing recalculation
3100 * should be triggered. However, this is not true for opaque
3101 * LSAs. Even if an opaque LSA instance is going to be removed
3102 * from the routing domain, it does not mean a change in network
3103 * topology, and thus, routing recalculation is not needed here.
3105 break;
3106 #endif /* HAVE_OPAQUE_LSA */
3107 case OSPF_AS_EXTERNAL_LSA:
3108 case OSPF_AS_NSSA_LSA:
3109 ospf_ase_incremental_update (ospf, lsa);
3110 break;
3111 default:
3112 ospf_spf_calculate_schedule (ospf);
3113 break;
3115 ospf_lsa_maxage (ospf, lsa);
3118 return 0;
3121 /* Periodical check of MaxAge LSA. */
3123 ospf_lsa_maxage_walker (struct thread *thread)
3125 struct ospf *ospf = THREAD_ARG (thread);
3126 struct route_node *rn;
3127 struct ospf_lsa *lsa;
3128 struct ospf_area *area;
3129 struct listnode *node, *nnode;
3131 ospf->t_maxage_walker = NULL;
3133 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
3135 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
3136 ospf_lsa_maxage_walker_remover (ospf, lsa);
3137 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
3138 ospf_lsa_maxage_walker_remover (ospf, lsa);
3139 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
3140 ospf_lsa_maxage_walker_remover (ospf, lsa);
3141 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
3142 ospf_lsa_maxage_walker_remover (ospf, lsa);
3143 #ifdef HAVE_OPAQUE_LSA
3144 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
3145 ospf_lsa_maxage_walker_remover (ospf, lsa);
3146 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
3147 ospf_lsa_maxage_walker_remover (ospf, lsa);
3148 #endif /* HAVE_OPAQUE_LSA */
3149 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
3150 ospf_lsa_maxage_walker_remover (ospf, lsa);
3153 /* for AS-external-LSAs. */
3154 if (ospf->lsdb)
3156 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
3157 ospf_lsa_maxage_walker_remover (ospf, lsa);
3158 #ifdef HAVE_OPAQUE_LSA
3159 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
3160 ospf_lsa_maxage_walker_remover (ospf, lsa);
3161 #endif /* HAVE_OPAQUE_LSA */
3164 OSPF_TIMER_ON (ospf->t_maxage_walker, ospf_lsa_maxage_walker,
3165 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
3166 return 0;
3169 struct ospf_lsa *
3170 ospf_lsa_lookup_by_prefix (struct ospf_lsdb *lsdb, u_char type,
3171 struct prefix_ipv4 *p, struct in_addr router_id)
3173 struct ospf_lsa *lsa;
3174 struct in_addr mask, id;
3175 struct lsa_header_mask
3177 struct lsa_header header;
3178 struct in_addr mask;
3179 } *hmask;
3181 lsa = ospf_lsdb_lookup_by_id (lsdb, type, p->prefix, router_id);
3182 if (lsa == NULL)
3183 return NULL;
3185 masklen2ip (p->prefixlen, &mask);
3187 hmask = (struct lsa_header_mask *) lsa->data;
3189 if (mask.s_addr != hmask->mask.s_addr)
3191 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3192 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, router_id);
3193 if (!lsa)
3194 return NULL;
3197 return lsa;
3200 struct ospf_lsa *
3201 ospf_lsa_lookup (struct ospf_area *area, u_int32_t type,
3202 struct in_addr id, struct in_addr adv_router)
3204 struct ospf *ospf = ospf_lookup();
3205 assert(ospf);
3207 switch (type)
3209 case OSPF_ROUTER_LSA:
3210 case OSPF_NETWORK_LSA:
3211 case OSPF_SUMMARY_LSA:
3212 case OSPF_ASBR_SUMMARY_LSA:
3213 case OSPF_AS_NSSA_LSA:
3214 #ifdef HAVE_OPAQUE_LSA
3215 case OSPF_OPAQUE_LINK_LSA:
3216 case OSPF_OPAQUE_AREA_LSA:
3217 #endif /* HAVE_OPAQUE_LSA */
3218 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, adv_router);
3219 case OSPF_AS_EXTERNAL_LSA:
3220 #ifdef HAVE_OPAQUE_LSA
3221 case OSPF_OPAQUE_AS_LSA:
3222 #endif /* HAVE_OPAQUE_LSA */
3223 return ospf_lsdb_lookup_by_id (ospf->lsdb, type, id, adv_router);
3224 default:
3225 break;
3228 return NULL;
3231 struct ospf_lsa *
3232 ospf_lsa_lookup_by_id (struct ospf_area *area, u_int32_t type,
3233 struct in_addr id)
3235 struct ospf_lsa *lsa;
3236 struct route_node *rn;
3238 switch (type)
3240 case OSPF_ROUTER_LSA:
3241 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
3242 case OSPF_NETWORK_LSA:
3243 for (rn = route_top (NETWORK_LSDB (area)); rn; rn = route_next (rn))
3244 if ((lsa = rn->info))
3245 if (IPV4_ADDR_SAME (&lsa->data->id, &id))
3247 route_unlock_node (rn);
3248 return lsa;
3250 break;
3251 case OSPF_SUMMARY_LSA:
3252 case OSPF_ASBR_SUMMARY_LSA:
3253 /* Currently not used. */
3254 assert (1);
3255 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
3256 case OSPF_AS_EXTERNAL_LSA:
3257 case OSPF_AS_NSSA_LSA:
3258 #ifdef HAVE_OPAQUE_LSA
3259 case OSPF_OPAQUE_LINK_LSA:
3260 case OSPF_OPAQUE_AREA_LSA:
3261 case OSPF_OPAQUE_AS_LSA:
3262 /* Currently not used. */
3263 break;
3264 #endif /* HAVE_OPAQUE_LSA */
3265 default:
3266 break;
3269 return NULL;
3272 struct ospf_lsa *
3273 ospf_lsa_lookup_by_header (struct ospf_area *area, struct lsa_header *lsah)
3275 struct ospf_lsa *match;
3277 #ifdef HAVE_OPAQUE_LSA
3279 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3280 * is redefined to have two subfields; opaque-type and opaque-id.
3281 * However, it is harmless to treat the two sub fields together, as if
3282 * they two were forming a unique LSA-ID.
3284 #endif /* HAVE_OPAQUE_LSA */
3286 match = ospf_lsa_lookup (area, lsah->type, lsah->id, lsah->adv_router);
3288 if (match == NULL)
3289 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3290 zlog_debug ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3291 lsah->type, inet_ntoa (lsah->id));
3293 return match;
3296 /* return +n, l1 is more recent.
3297 return -n, l2 is more recent.
3298 return 0, l1 and l2 is identical. */
3300 ospf_lsa_more_recent (struct ospf_lsa *l1, struct ospf_lsa *l2)
3302 int r;
3303 int x, y;
3305 if (l1 == NULL && l2 == NULL)
3306 return 0;
3307 if (l1 == NULL)
3308 return -1;
3309 if (l2 == NULL)
3310 return 1;
3312 /* compare LS sequence number. */
3313 x = (int) ntohl (l1->data->ls_seqnum);
3314 y = (int) ntohl (l2->data->ls_seqnum);
3315 if (x > y)
3316 return 1;
3317 if (x < y)
3318 return -1;
3320 /* compare LS checksum. */
3321 r = ntohs (l1->data->checksum) - ntohs (l2->data->checksum);
3322 if (r)
3323 return r;
3325 /* compare LS age. */
3326 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
3327 return 1;
3328 else if (!IS_LSA_MAXAGE (l1) && IS_LSA_MAXAGE (l2))
3329 return -1;
3331 /* compare LS age with MaxAgeDiff. */
3332 if (LS_AGE (l1) - LS_AGE (l2) > OSPF_LSA_MAXAGE_DIFF)
3333 return -1;
3334 else if (LS_AGE (l2) - LS_AGE (l1) > OSPF_LSA_MAXAGE_DIFF)
3335 return 1;
3337 /* LSAs are identical. */
3338 return 0;
3341 /* If two LSAs are different, return 1, otherwise return 0. */
3343 ospf_lsa_different (struct ospf_lsa *l1, struct ospf_lsa *l2)
3345 char *p1, *p2;
3346 assert (l1);
3347 assert (l2);
3348 assert (l1->data);
3349 assert (l2->data);
3351 if (l1->data->options != l2->data->options)
3352 return 1;
3354 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
3355 return 1;
3357 if (IS_LSA_MAXAGE (l2) && !IS_LSA_MAXAGE (l1))
3358 return 1;
3360 if (l1->data->length != l2->data->length)
3361 return 1;
3363 if (l1->data->length == 0)
3364 return 1;
3366 assert ( ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
3368 p1 = (char *) l1->data;
3369 p2 = (char *) l2->data;
3371 if (memcmp (p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
3372 ntohs( l1->data->length ) - OSPF_LSA_HEADER_SIZE) != 0)
3373 return 1;
3375 return 0;
3378 #ifdef ORIGINAL_CODING
3379 void
3380 ospf_lsa_flush_self_originated (struct ospf_neighbor *nbr,
3381 struct ospf_lsa *self,
3382 struct ospf_lsa *new)
3384 u_int32_t seqnum;
3386 /* Adjust LS Sequence Number. */
3387 seqnum = ntohl (new->data->ls_seqnum) + 1;
3388 self->data->ls_seqnum = htonl (seqnum);
3390 /* Recalculate LSA checksum. */
3391 ospf_lsa_checksum (self->data);
3393 /* Reflooding LSA. */
3394 /* RFC2328 Section 13.3
3395 On non-broadcast networks, separate Link State Update
3396 packets must be sent, as unicasts, to each adjacent neighbor
3397 (i.e., those in state Exchange or greater). The destination
3398 IP addresses for these packets are the neighbors' IP
3399 addresses. */
3400 if (nbr->oi->type == OSPF_IFTYPE_NBMA)
3402 struct route_node *rn;
3403 struct ospf_neighbor *onbr;
3405 for (rn = route_top (nbr->oi->nbrs); rn; rn = route_next (rn))
3406 if ((onbr = rn->info) != NULL)
3407 if (onbr != nbr->oi->nbr_self && onbr->status >= NSM_Exchange)
3408 ospf_ls_upd_send_lsa (onbr, self, OSPF_SEND_PACKET_DIRECT);
3410 else
3411 ospf_ls_upd_send_lsa (nbr, self, OSPF_SEND_PACKET_INDIRECT);
3413 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3414 zlog_debug ("LSA[Type%d:%s]: Flush self-originated LSA",
3415 self->data->type, inet_ntoa (self->data->id));
3417 #else /* ORIGINAL_CODING */
3418 static int
3419 ospf_lsa_flush_schedule (struct ospf *ospf, struct ospf_lsa *lsa)
3421 if (lsa == NULL || !IS_LSA_SELF (lsa))
3422 return 0;
3424 if (IS_DEBUG_OSPF_EVENT)
3425 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3427 /* Force given lsa's age to MaxAge. */
3428 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
3430 switch (lsa->data->type)
3432 #ifdef HAVE_OPAQUE_LSA
3433 case OSPF_OPAQUE_LINK_LSA:
3434 case OSPF_OPAQUE_AREA_LSA:
3435 case OSPF_OPAQUE_AS_LSA:
3436 ospf_opaque_lsa_refresh (lsa);
3437 break;
3438 #endif /* HAVE_OPAQUE_LSA */
3439 default:
3440 ospf_lsa_maxage (ospf, lsa);
3441 break;
3444 return 0;
3447 void
3448 ospf_flush_self_originated_lsas_now (struct ospf *ospf)
3450 struct listnode *node, *nnode;
3451 struct listnode *node2, *nnode2;
3452 struct ospf_area *area;
3453 struct ospf_interface *oi;
3454 struct ospf_lsa *lsa;
3455 struct route_node *rn;
3456 int need_to_flush_ase = 0;
3458 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
3460 if ((lsa = area->router_lsa_self) != NULL)
3462 if (IS_DEBUG_OSPF_EVENT)
3463 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3465 ospf_lsa_flush_area (lsa, area);
3466 ospf_lsa_unlock (&area->router_lsa_self);
3467 area->router_lsa_self = NULL;
3468 OSPF_TIMER_OFF (area->t_router_lsa_self);
3471 for (ALL_LIST_ELEMENTS (area->oiflist, node2, nnode2, oi))
3473 if ((lsa = oi->network_lsa_self) != NULL
3474 && oi->state == ISM_DR
3475 && oi->full_nbrs > 0)
3477 if (IS_DEBUG_OSPF_EVENT)
3478 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3480 ospf_lsa_flush_area (oi->network_lsa_self, area);
3481 ospf_lsa_unlock (&oi->network_lsa_self);
3482 oi->network_lsa_self = NULL;
3483 OSPF_TIMER_OFF (oi->t_network_lsa_self);
3486 if (oi->type != OSPF_IFTYPE_VIRTUALLINK
3487 && area->external_routing == OSPF_AREA_DEFAULT)
3488 need_to_flush_ase = 1;
3491 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
3492 ospf_lsa_flush_schedule (ospf, lsa);
3493 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
3494 ospf_lsa_flush_schedule (ospf, lsa);
3495 #ifdef HAVE_OPAQUE_LSA
3496 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
3497 ospf_lsa_flush_schedule (ospf, lsa);
3498 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
3499 ospf_lsa_flush_schedule (ospf, lsa);
3500 #endif /* HAVE_OPAQUE_LSA */
3503 if (need_to_flush_ase)
3505 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
3506 ospf_lsa_flush_schedule (ospf, lsa);
3507 #ifdef HAVE_OPAQUE_LSA
3508 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
3509 ospf_lsa_flush_schedule (ospf, lsa);
3510 #endif /* HAVE_OPAQUE_LSA */
3514 * Make sure that the MaxAge LSA remover is executed immediately,
3515 * without conflicting to other threads.
3517 if (ospf->t_maxage != NULL)
3519 OSPF_TIMER_OFF (ospf->t_maxage);
3520 thread_execute (master, ospf_maxage_lsa_remover, ospf, 0);
3523 return;
3525 #endif /* ORIGINAL_CODING */
3527 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3528 /* An interface-independent version of ospf_lsa_is_self_originated */
3529 int
3530 ospf_lsa_is_self_originated (struct ospf *ospf, struct ospf_lsa *lsa)
3532 struct listnode *node;
3533 struct ospf_interface *oi;
3535 /* This LSA is already checked. */
3536 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED))
3537 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3539 /* Make sure LSA is self-checked. */
3540 SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
3542 /* AdvRouter and Router ID is the same. */
3543 if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf->router_id))
3544 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3546 /* LSA is router-LSA. */
3547 else if (lsa->data->type == OSPF_ROUTER_LSA &&
3548 IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
3549 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3551 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3552 else if (lsa->data->type == OSPF_NETWORK_LSA)
3553 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
3555 /* Ignore virtual link. */
3556 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
3557 if (oi->address->family == AF_INET)
3558 if (IPV4_ADDR_SAME (&lsa->data->id, &oi->address->u.prefix4))
3560 /* to make it easier later */
3561 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3562 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3566 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3569 /* Get unique Link State ID. */
3570 struct in_addr
3571 ospf_lsa_unique_id (struct ospf *ospf,
3572 struct ospf_lsdb *lsdb, u_char type, struct prefix_ipv4 *p)
3574 struct ospf_lsa *lsa;
3575 struct in_addr mask, id;
3577 id = p->prefix;
3579 /* Check existence of LSA instance. */
3580 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, ospf->router_id);
3581 if (lsa)
3583 struct as_external_lsa *al = (struct as_external_lsa *) lsa->data;
3584 if (ip_masklen (al->mask) == p->prefixlen)
3586 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3587 zlog_debug ("ospf_lsa_unique_id(): "
3588 "Can't get Link State ID for %s/%d",
3589 inet_ntoa (p->prefix), p->prefixlen);
3590 /* id.s_addr = 0; */
3591 id.s_addr = 0xffffffff;
3592 return id;
3594 /* Masklen differs, then apply wildcard mask to Link State ID. */
3595 else
3597 masklen2ip (p->prefixlen, &mask);
3599 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3600 lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, type,
3601 id, ospf->router_id);
3602 if (lsa)
3604 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3605 zlog_debug ("ospf_lsa_unique_id(): "
3606 "Can't get Link State ID for %s/%d",
3607 inet_ntoa (p->prefix), p->prefixlen);
3608 /* id.s_addr = 0; */
3609 id.s_addr = 0xffffffff;
3610 return id;
3615 return id;
3619 #define LSA_ACTION_FLOOD_AREA 1
3620 #define LSA_ACTION_FLUSH_AREA 2
3622 struct lsa_action
3624 u_char action;
3625 struct ospf_area *area;
3626 struct ospf_lsa *lsa;
3629 static int
3630 ospf_lsa_action (struct thread *t)
3632 struct lsa_action *data;
3634 data = THREAD_ARG (t);
3636 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3637 zlog_debug ("LSA[Action]: Performing scheduled LSA action: %d",
3638 data->action);
3640 switch (data->action)
3642 case LSA_ACTION_FLOOD_AREA:
3643 ospf_flood_through_area (data->area, NULL, data->lsa);
3644 break;
3645 case LSA_ACTION_FLUSH_AREA:
3646 ospf_lsa_flush_area (data->lsa, data->area);
3647 break;
3650 ospf_lsa_unlock (&data->lsa); /* Message */
3651 XFREE (MTYPE_OSPF_MESSAGE, data);
3652 return 0;
3655 void
3656 ospf_schedule_lsa_flood_area (struct ospf_area *area, struct ospf_lsa *lsa)
3658 struct lsa_action *data;
3660 data = XMALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3661 memset (data, 0, sizeof (struct lsa_action));
3663 data->action = LSA_ACTION_FLOOD_AREA;
3664 data->area = area;
3665 data->lsa = ospf_lsa_lock (lsa); /* Message / Flood area */
3667 thread_add_event (master, ospf_lsa_action, data, 0);
3670 void
3671 ospf_schedule_lsa_flush_area (struct ospf_area *area, struct ospf_lsa *lsa)
3673 struct lsa_action *data;
3675 data = XMALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3676 memset (data, 0, sizeof (struct lsa_action));
3678 data->action = LSA_ACTION_FLUSH_AREA;
3679 data->area = area;
3680 data->lsa = ospf_lsa_lock (lsa); /* Message / Flush area */
3682 thread_add_event (master, ospf_lsa_action, data, 0);
3686 /* LSA Refreshment functions. */
3687 static void
3688 ospf_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
3690 struct external_info *ei;
3691 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3693 switch (lsa->data->type)
3695 /* Router and Network LSAs are processed differently. */
3696 case OSPF_ROUTER_LSA:
3697 case OSPF_NETWORK_LSA:
3698 break;
3699 case OSPF_SUMMARY_LSA:
3700 ospf_summary_lsa_refresh (ospf, lsa);
3701 break;
3702 case OSPF_ASBR_SUMMARY_LSA:
3703 ospf_summary_asbr_lsa_refresh (ospf, lsa);
3704 break;
3705 case OSPF_AS_EXTERNAL_LSA:
3706 /* Translated from NSSA Type-5s are refreshed when
3707 * from refresh of Type-7 - do not refresh these directly.
3709 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
3710 break;
3711 ei = ospf_external_info_check (lsa);
3712 if (ei)
3713 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
3714 else
3715 ospf_lsa_flush_as (ospf, lsa);
3716 break;
3717 #ifdef HAVE_OPAQUE_LSA
3718 case OSPF_OPAQUE_LINK_LSA:
3719 case OSPF_OPAQUE_AREA_LSA:
3720 case OSPF_OPAQUE_AS_LSA:
3721 ospf_opaque_lsa_refresh (lsa);
3722 break;
3723 #endif /* HAVE_OPAQUE_LSA */
3724 default:
3725 break;
3729 void
3730 ospf_refresher_register_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3732 u_int16_t index, current_index;
3734 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3736 if (lsa->refresh_list < 0)
3738 int delay;
3740 if (LS_AGE (lsa) == 0 &&
3741 ntohl (lsa->data->ls_seqnum) == OSPF_INITIAL_SEQUENCE_NUMBER)
3742 /* Randomize first update by OSPF_LS_REFRESH_SHIFT factor */
3743 delay = OSPF_LS_REFRESH_SHIFT + (random () % OSPF_LS_REFRESH_TIME);
3744 else
3745 /* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */
3746 delay = OSPF_LS_REFRESH_TIME - LS_AGE (lsa) - OSPF_LS_REFRESH_JITTER
3747 + (random () % (2*OSPF_LS_REFRESH_JITTER));
3749 if (delay < 0)
3750 delay = 0;
3752 current_index = ospf->lsa_refresh_queue.index +
3753 (quagga_time (NULL) - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
3755 index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY)
3756 % (OSPF_LSA_REFRESHER_SLOTS);
3758 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3759 zlog_debug ("LSA[Refresh]: lsa %s with age %d added to index %d",
3760 inet_ntoa (lsa->data->id), LS_AGE (lsa), index);
3761 if (!ospf->lsa_refresh_queue.qs[index])
3762 ospf->lsa_refresh_queue.qs[index] = list_new ();
3763 listnode_add (ospf->lsa_refresh_queue.qs[index],
3764 ospf_lsa_lock (lsa)); /* lsa_refresh_queue */
3765 lsa->refresh_list = index;
3766 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3767 zlog_debug ("LSA[Refresh:%s]: ospf_refresher_register_lsa(): "
3768 "setting refresh_list on lsa %p (slod %d)",
3769 inet_ntoa (lsa->data->id), lsa, index);
3773 void
3774 ospf_refresher_unregister_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3776 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3777 if (lsa->refresh_list >= 0)
3779 struct list *refresh_list = ospf->lsa_refresh_queue.qs[lsa->refresh_list];
3780 listnode_delete (refresh_list, lsa);
3781 if (!listcount (refresh_list))
3783 list_free (refresh_list);
3784 ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
3786 ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
3787 lsa->refresh_list = -1;
3792 ospf_lsa_refresh_walker (struct thread *t)
3794 struct list *refresh_list;
3795 struct listnode *node, *nnode;
3796 struct ospf *ospf = THREAD_ARG (t);
3797 struct ospf_lsa *lsa;
3798 int i;
3799 struct list *lsa_to_refresh = list_new ();
3801 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3802 zlog_debug ("LSA[Refresh]:ospf_lsa_refresh_walker(): start");
3805 i = ospf->lsa_refresh_queue.index;
3807 /* Note: if clock has jumped backwards, then time change could be negative,
3808 so we are careful to cast the expression to unsigned before taking
3809 modulus. */
3810 ospf->lsa_refresh_queue.index =
3811 ((unsigned long)(ospf->lsa_refresh_queue.index +
3812 (quagga_time (NULL) - ospf->lsa_refresher_started) /
3813 OSPF_LSA_REFRESHER_GRANULARITY)) % OSPF_LSA_REFRESHER_SLOTS;
3815 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3816 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3817 ospf->lsa_refresh_queue.index);
3819 for (;i != ospf->lsa_refresh_queue.index;
3820 i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS)
3822 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3823 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): "
3824 "refresh index %d", i);
3826 refresh_list = ospf->lsa_refresh_queue.qs [i];
3828 ospf->lsa_refresh_queue.qs [i] = NULL;
3830 if (refresh_list)
3832 for (ALL_LIST_ELEMENTS (refresh_list, node, nnode, lsa))
3834 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3835 zlog_debug ("LSA[Refresh:%s]: ospf_lsa_refresh_walker(): "
3836 "refresh lsa %p (slot %d)",
3837 inet_ntoa (lsa->data->id), lsa, i);
3839 list_delete_node (refresh_list, node);
3840 ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
3841 lsa->refresh_list = -1;
3842 listnode_add (lsa_to_refresh, lsa);
3844 list_free (refresh_list);
3848 ospf->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
3849 ospf, ospf->lsa_refresh_interval);
3850 ospf->lsa_refresher_started = quagga_time (NULL);
3852 for (ALL_LIST_ELEMENTS (lsa_to_refresh, node, nnode, lsa))
3853 ospf_lsa_refresh (ospf, lsa);
3855 list_delete (lsa_to_refresh);
3857 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3858 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3860 return 0;