[cleanup] Make command nodes static
[jleu-quagga.git] / ospfd / ospf_lsa.c
blob3c9c73a3b9d0c213e1e22ee14ec55dc184274c8e
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() */
35 #include "checksum.h"
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_interface.h"
39 #include "ospfd/ospf_ism.h"
40 #include "ospfd/ospf_asbr.h"
41 #include "ospfd/ospf_lsa.h"
42 #include "ospfd/ospf_lsdb.h"
43 #include "ospfd/ospf_neighbor.h"
44 #include "ospfd/ospf_nsm.h"
45 #include "ospfd/ospf_flood.h"
46 #include "ospfd/ospf_packet.h"
47 #include "ospfd/ospf_spf.h"
48 #include "ospfd/ospf_dump.h"
49 #include "ospfd/ospf_route.h"
50 #include "ospfd/ospf_ase.h"
51 #include "ospfd/ospf_zebra.h"
54 u_int32_t
55 get_metric (u_char *metric)
57 u_int32_t m;
58 m = metric[0];
59 m = (m << 8) + metric[1];
60 m = (m << 8) + metric[2];
61 return m;
65 struct timeval
66 tv_adjust (struct timeval a)
68 while (a.tv_usec >= 1000000)
70 a.tv_usec -= 1000000;
71 a.tv_sec++;
74 while (a.tv_usec < 0)
76 a.tv_usec += 1000000;
77 a.tv_sec--;
80 return a;
83 int
84 tv_ceil (struct timeval a)
86 a = tv_adjust (a);
88 return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec);
91 int
92 tv_floor (struct timeval a)
94 a = tv_adjust (a);
96 return a.tv_sec;
99 struct timeval
100 int2tv (int a)
102 struct timeval ret;
104 ret.tv_sec = a;
105 ret.tv_usec = 0;
107 return ret;
110 struct timeval
111 tv_add (struct timeval a, struct timeval b)
113 struct timeval ret;
115 ret.tv_sec = a.tv_sec + b.tv_sec;
116 ret.tv_usec = a.tv_usec + b.tv_usec;
118 return tv_adjust (ret);
121 struct timeval
122 tv_sub (struct timeval a, struct timeval b)
124 struct timeval ret;
126 ret.tv_sec = a.tv_sec - b.tv_sec;
127 ret.tv_usec = a.tv_usec - b.tv_usec;
129 return tv_adjust (ret);
133 tv_cmp (struct timeval a, struct timeval b)
135 return (a.tv_sec == b.tv_sec ?
136 a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
140 ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
142 struct timeval delta, now;
143 int delay = 0;
145 quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
146 delta = tv_sub (now, lsa->tv_orig);
148 if (tv_cmp (delta, int2tv (OSPF_MIN_LS_INTERVAL)) < 0)
150 delay = tv_ceil (tv_sub (int2tv (OSPF_MIN_LS_INTERVAL), delta));
152 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
153 zlog_debug ("LSA[Type%d:%s]: Refresh timer delay %d seconds",
154 lsa->data->type, inet_ntoa (lsa->data->id), delay);
156 assert (delay > 0);
159 return delay;
164 get_age (struct ospf_lsa *lsa)
166 int age;
168 age = ntohs (lsa->data->ls_age)
169 + tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
171 return age;
175 /* Fletcher Checksum -- Refer to RFC1008. */
177 /* All the offsets are zero-based. The offsets in the RFC1008 are
178 one-based. */
179 u_int16_t
180 ospf_lsa_checksum (struct lsa_header *lsa)
182 u_char *buffer = (u_char *) &lsa->options;
183 int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
185 /* Skip the AGE field */
186 u_int16_t len = ntohs(lsa->length) - options_offset;
188 /* Checksum offset starts from "options" field, not the beginning of the
189 lsa_header struct. The offset is 14, rather than 16. */
190 int checksum_offset = (u_char *) &lsa->checksum - buffer;
192 return fletcher_checksum(buffer, len, checksum_offset);
197 /* Create OSPF LSA. */
198 struct ospf_lsa *
199 ospf_lsa_new ()
201 struct ospf_lsa *new;
203 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
204 memset (new, 0, sizeof (struct ospf_lsa));
206 new->flags = 0;
207 new->lock = 1;
208 new->retransmit_counter = 0;
209 new->tv_recv = recent_relative_time ();
210 new->tv_orig = new->tv_recv;
211 new->refresh_list = -1;
213 return new;
216 /* Duplicate OSPF LSA. */
217 struct ospf_lsa *
218 ospf_lsa_dup (struct ospf_lsa *lsa)
220 struct ospf_lsa *new;
222 if (lsa == NULL)
223 return NULL;
225 new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
227 memcpy (new, lsa, sizeof (struct ospf_lsa));
228 UNSET_FLAG (new->flags, OSPF_LSA_DISCARD);
229 new->lock = 1;
230 new->retransmit_counter = 0;
231 new->data = ospf_lsa_data_dup (lsa->data);
233 /* kevinm: Clear the refresh_list, otherwise there are going
234 to be problems when we try to remove the LSA from the
235 queue (which it's not a member of.)
236 XXX: Should we add the LSA to the refresh_list queue? */
237 new->refresh_list = -1;
239 if (IS_DEBUG_OSPF (lsa, LSA))
240 zlog_debug ("LSA: duplicated %p (new: %p)", lsa, new);
242 return new;
245 /* Free OSPF LSA. */
246 void
247 ospf_lsa_free (struct ospf_lsa *lsa)
249 assert (lsa->lock == 0);
251 if (IS_DEBUG_OSPF (lsa, LSA))
252 zlog_debug ("LSA: freed %p", lsa);
254 /* Delete LSA data. */
255 if (lsa->data != NULL)
256 ospf_lsa_data_free (lsa->data);
258 assert (lsa->refresh_list < 0);
260 memset (lsa, 0, sizeof (struct ospf_lsa));
261 XFREE (MTYPE_OSPF_LSA, lsa);
264 /* Lock LSA. */
265 struct ospf_lsa *
266 ospf_lsa_lock (struct ospf_lsa *lsa)
268 lsa->lock++;
269 return lsa;
272 /* Unlock LSA. */
273 void
274 ospf_lsa_unlock (struct ospf_lsa **lsa)
276 /* This is sanity check. */
277 if (!lsa || !*lsa)
278 return;
280 (*lsa)->lock--;
282 assert ((*lsa)->lock >= 0);
284 if ((*lsa)->lock == 0)
286 assert (CHECK_FLAG ((*lsa)->flags, OSPF_LSA_DISCARD));
287 ospf_lsa_free (*lsa);
288 *lsa = NULL;
292 /* Check discard flag. */
293 void
294 ospf_lsa_discard (struct ospf_lsa *lsa)
296 if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
298 SET_FLAG (lsa->flags, OSPF_LSA_DISCARD);
299 ospf_lsa_unlock (&lsa);
303 /* Create LSA data. */
304 struct lsa_header *
305 ospf_lsa_data_new (size_t size)
307 return XCALLOC (MTYPE_OSPF_LSA_DATA, size);
310 /* Duplicate LSA data. */
311 struct lsa_header *
312 ospf_lsa_data_dup (struct lsa_header *lsah)
314 struct lsa_header *new;
316 new = ospf_lsa_data_new (ntohs (lsah->length));
317 memcpy (new, lsah, ntohs (lsah->length));
319 return new;
322 /* Free LSA data. */
323 void
324 ospf_lsa_data_free (struct lsa_header *lsah)
326 if (IS_DEBUG_OSPF (lsa, LSA))
327 zlog_debug ("LSA[Type%d:%s]: data freed %p",
328 lsah->type, inet_ntoa (lsah->id), lsah);
330 XFREE (MTYPE_OSPF_LSA_DATA, lsah);
334 /* LSA general functions. */
336 const char *
337 dump_lsa_key (struct ospf_lsa *lsa)
339 static char buf[] = {
340 "Type255,id(255.255.255.255),ar(255.255.255.255)"
342 struct lsa_header *lsah;
344 if (lsa != NULL && (lsah = lsa->data) != NULL)
346 char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
347 strcpy (id, inet_ntoa (lsah->id));
348 strcpy (ar, inet_ntoa (lsah->adv_router));
350 sprintf (buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar);
352 else
353 strcpy (buf, "NULL");
355 return buf;
358 u_int32_t
359 lsa_seqnum_increment (struct ospf_lsa *lsa)
361 u_int32_t seqnum;
363 seqnum = ntohl (lsa->data->ls_seqnum) + 1;
365 return htonl (seqnum);
368 void
369 lsa_header_set (struct stream *s, u_char options,
370 u_char type, struct in_addr id, struct in_addr router_id)
372 struct lsa_header *lsah;
374 lsah = (struct lsa_header *) STREAM_DATA (s);
376 lsah->ls_age = htons (0);
377 lsah->options = options;
378 lsah->type = type;
379 lsah->id = id;
380 lsah->adv_router = router_id;
381 lsah->ls_seqnum = htonl (OSPF_INITIAL_SEQUENCE_NUMBER);
383 stream_forward_endp (s, OSPF_LSA_HEADER_SIZE);
387 /* router-LSA related functions. */
388 /* Get router-LSA flags. */
389 static u_char
390 router_lsa_flags (struct ospf_area *area)
392 u_char flags;
394 flags = area->ospf->flags;
396 /* Set virtual link flag. */
397 if (ospf_full_virtual_nbrs (area))
398 SET_FLAG (flags, ROUTER_LSA_VIRTUAL);
399 else
400 /* Just sanity check */
401 UNSET_FLAG (flags, ROUTER_LSA_VIRTUAL);
403 /* Set Shortcut ABR behabiour flag. */
404 UNSET_FLAG (flags, ROUTER_LSA_SHORTCUT);
405 if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
406 if (!OSPF_IS_AREA_BACKBONE (area))
407 if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
408 area->ospf->backbone == NULL) ||
409 area->shortcut_configured == OSPF_SHORTCUT_ENABLE)
410 SET_FLAG (flags, ROUTER_LSA_SHORTCUT);
412 /* ASBR can't exit in stub area. */
413 if (area->external_routing == OSPF_AREA_STUB)
414 UNSET_FLAG (flags, ROUTER_LSA_EXTERNAL);
415 /* If ASBR set External flag */
416 else if (IS_OSPF_ASBR (area->ospf))
417 SET_FLAG (flags, ROUTER_LSA_EXTERNAL);
419 /* Set ABR dependent flags */
420 if (IS_OSPF_ABR (area->ospf))
422 SET_FLAG (flags, ROUTER_LSA_BORDER);
423 /* If Area is NSSA and we are both ABR and unconditional translator,
424 * set Nt bit to inform other routers.
426 if ( (area->external_routing == OSPF_AREA_NSSA)
427 && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
428 SET_FLAG (flags, ROUTER_LSA_NT);
430 return flags;
433 /* Lookup neighbor other than myself.
434 And check neighbor count,
435 Point-to-Point link must have only 1 neighbor. */
436 struct ospf_neighbor *
437 ospf_nbr_lookup_ptop (struct ospf_interface *oi)
439 struct ospf_neighbor *nbr = NULL;
440 struct route_node *rn;
442 /* Search neighbor, there must be one of two nbrs. */
443 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
444 if ((nbr = rn->info))
445 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
446 if (nbr->state == NSM_Full)
448 route_unlock_node (rn);
449 break;
452 /* PtoP link must have only 1 neighbor. */
453 if (ospf_nbr_count (oi, 0) > 1)
454 zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
456 return nbr;
459 /* Determine cost of link, taking RFC3137 stub-router support into
460 * consideration
462 static u_int16_t
463 ospf_link_cost (struct ospf_interface *oi)
465 /* RFC3137 stub router support */
466 if (!CHECK_FLAG (oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
467 return oi->output_cost;
468 else
469 return OSPF_OUTPUT_COST_INFINITE;
472 /* Set a link information. */
473 static char
474 link_info_set (struct stream *s, struct in_addr id,
475 struct in_addr data, u_char type, u_char tos, u_int16_t cost)
477 /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
478 * vast majority of cases. Some rare routers with lots of links need more.
479 * we try accomodate those here.
481 if (STREAM_WRITEABLE(s) < OSPF_ROUTER_LSA_LINK_SIZE)
483 size_t ret = OSPF_MAX_LSA_SIZE;
485 /* Can we enlarge the stream still? */
486 if (STREAM_SIZE(s) == OSPF_MAX_LSA_SIZE)
488 /* we futz the size here for simplicity, really we need to account
489 * for just:
490 * IP Header - (sizeof (struct ip))
491 * OSPF Header - OSPF_HEADER_SIZE
492 * LSA Header - OSPF_LSA_HEADER_SIZE
493 * MD5 auth data, if MD5 is configured - OSPF_AUTH_MD5_SIZE.
495 * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
497 ret = stream_resize (s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
500 if (ret == OSPF_MAX_LSA_SIZE)
502 zlog_warn ("%s: Out of space in LSA stream, left %ld, size %ld",
503 __func__, STREAM_REMAIN (s), STREAM_SIZE (s));
504 return 0;
508 /* TOS based routing is not supported. */
509 stream_put_ipv4 (s, id.s_addr); /* Link ID. */
510 stream_put_ipv4 (s, data.s_addr); /* Link Data. */
511 stream_putc (s, type); /* Link Type. */
512 stream_putc (s, tos); /* TOS = 0. */
513 stream_putw (s, cost); /* Link Cost. */
515 return 1;
518 /* Describe Point-to-Point link (Section 12.4.1.1). */
519 static int
520 lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi)
522 int links = 0;
523 struct ospf_neighbor *nbr;
524 struct in_addr id, mask;
525 u_int16_t cost = ospf_link_cost (oi);
527 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
528 zlog_debug ("LSA[Type1]: Set link Point-to-Point");
530 if ((nbr = ospf_nbr_lookup_ptop (oi)))
531 if (nbr->state == NSM_Full)
533 /* For unnumbered point-to-point networks, the Link Data field
534 should specify the interface's MIB-II ifIndex value. */
535 links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
536 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
539 /* Regardless of the state of the neighboring router, we must
540 add a Type 3 link (stub network).
541 N.B. Options 1 & 2 share basically the same logic. */
542 masklen2ip (oi->address->prefixlen, &mask);
543 id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr & mask.s_addr;
544 links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
545 oi->output_cost);
546 return links;
549 /* Describe Broadcast Link. */
550 static int
551 lsa_link_broadcast_set (struct stream *s, struct ospf_interface *oi)
553 struct ospf_neighbor *dr;
554 struct in_addr id, mask;
555 u_int16_t cost = ospf_link_cost (oi);
557 /* Describe Type 3 Link. */
558 if (oi->state == ISM_Waiting)
560 masklen2ip (oi->address->prefixlen, &mask);
561 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
562 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
563 oi->output_cost);
566 dr = ospf_nbr_lookup_by_addr (oi->nbrs, &DR (oi));
567 /* Describe Type 2 link. */
568 if (dr && (dr->state == NSM_Full ||
569 IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))) &&
570 ospf_nbr_count (oi, NSM_Full) > 0)
572 return link_info_set (s, DR (oi), oi->address->u.prefix4,
573 LSA_LINK_TYPE_TRANSIT, 0, cost);
575 /* Describe type 3 link. */
576 else
578 masklen2ip (oi->address->prefixlen, &mask);
579 id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
580 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
581 oi->output_cost);
585 static int
586 lsa_link_loopback_set (struct stream *s, struct ospf_interface *oi)
588 struct in_addr id, mask;
590 /* Describe Type 3 Link. */
591 if (oi->state != ISM_Loopback)
592 return 0;
594 mask.s_addr = 0xffffffff;
595 id.s_addr = oi->address->u.prefix4.s_addr;
596 return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
599 /* Describe Virtual Link. */
600 static int
601 lsa_link_virtuallink_set (struct stream *s, struct ospf_interface *oi)
603 struct ospf_neighbor *nbr;
604 u_int16_t cost = ospf_link_cost (oi);
606 if (oi->state == ISM_PointToPoint)
607 if ((nbr = ospf_nbr_lookup_ptop (oi)))
608 if (nbr->state == NSM_Full)
610 return link_info_set (s, nbr->router_id, oi->address->u.prefix4,
611 LSA_LINK_TYPE_VIRTUALLINK, 0, cost);
614 return 0;
617 #define lsa_link_nbma_set(S,O) lsa_link_broadcast_set (S, O)
619 /* this function add for support point-to-multipoint ,see rfc2328
620 12.4.1.4.*/
621 /* from "edward rrr" <edward_rrr@hotmail.com>
622 http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
623 static int
624 lsa_link_ptomp_set (struct stream *s, struct ospf_interface *oi)
626 int links = 0;
627 struct route_node *rn;
628 struct ospf_neighbor *nbr = NULL;
629 struct in_addr id, mask;
630 u_int16_t cost = ospf_link_cost (oi);
632 mask.s_addr = 0xffffffff;
633 id.s_addr = oi->address->u.prefix4.s_addr;
634 links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
636 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
637 zlog_debug ("PointToMultipoint: running ptomultip_set");
639 /* Search neighbor, */
640 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
641 if ((nbr = rn->info) != NULL)
642 /* Ignore myself. */
643 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
644 if (nbr->state == NSM_Full)
647 links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
648 LSA_LINK_TYPE_POINTOPOINT, 0, cost);
649 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
650 zlog_debug ("PointToMultipoint: set link to %s",
651 inet_ntoa(oi->address->u.prefix4));
654 return links;
657 /* Set router-LSA link information. */
658 static int
659 router_lsa_link_set (struct stream *s, struct ospf_area *area)
661 struct listnode *node;
662 struct ospf_interface *oi;
663 int links = 0;
665 for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
667 struct interface *ifp = oi->ifp;
669 /* Check interface is up, OSPF is enable. */
670 if (if_is_operative (ifp))
672 if (oi->state != ISM_Down)
674 /* Describe each link. */
675 switch (oi->type)
677 case OSPF_IFTYPE_POINTOPOINT:
678 links += lsa_link_ptop_set (s, oi);
679 break;
680 case OSPF_IFTYPE_BROADCAST:
681 links += lsa_link_broadcast_set (s, oi);
682 break;
683 case OSPF_IFTYPE_NBMA:
684 links += lsa_link_nbma_set (s, oi);
685 break;
686 case OSPF_IFTYPE_POINTOMULTIPOINT:
687 links += lsa_link_ptomp_set (s, oi);
688 break;
689 case OSPF_IFTYPE_VIRTUALLINK:
690 links += lsa_link_virtuallink_set (s, oi);
691 break;
692 case OSPF_IFTYPE_LOOPBACK:
693 links += lsa_link_loopback_set (s, oi);
699 return links;
702 /* Set router-LSA body. */
703 static void
704 ospf_router_lsa_body_set (struct stream *s, struct ospf_area *area)
706 unsigned long putp;
707 u_int16_t cnt;
709 /* Set flags. */
710 stream_putc (s, router_lsa_flags (area));
712 /* Set Zero fields. */
713 stream_putc (s, 0);
715 /* Keep pointer to # links. */
716 putp = stream_get_endp(s);
718 /* Forward word */
719 stream_putw(s, 0);
721 /* Set all link information. */
722 cnt = router_lsa_link_set (s, area);
724 /* Set # of links here. */
725 stream_putw_at (s, putp, cnt);
728 static int
729 ospf_stub_router_timer (struct thread *t)
731 struct ospf_area *area = THREAD_ARG (t);
733 area->t_stub_router = NULL;
735 SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
737 /* clear stub route state and generate router-lsa refresh, don't
738 * clobber an administratively set stub-router state though.
740 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
741 return 0;
743 UNSET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
745 ospf_router_lsa_timer_add (area);
747 return 0;
750 inline static void
751 ospf_stub_router_check (struct ospf_area *area)
753 /* area must either be administratively configured to be stub
754 * or startup-time stub-router must be configured and we must in a pre-stub
755 * state.
757 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
759 SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
760 return;
763 /* not admin-stubbed, check whether startup stubbing is configured and
764 * whether it's not been done yet
766 if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED))
767 return;
769 if (area->ospf->stub_router_startup_time == OSPF_STUB_ROUTER_UNCONFIGURED)
771 /* stub-router is hence done forever for this area, even if someone
772 * tries configure it (take effect next restart).
774 SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
775 return;
778 /* startup stub-router configured and not yet done */
779 SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
781 OSPF_AREA_TIMER_ON (area->t_stub_router, ospf_stub_router_timer,
782 area->ospf->stub_router_startup_time);
785 /* Create new router-LSA. */
786 static struct ospf_lsa *
787 ospf_router_lsa_new (struct ospf_area *area)
789 struct ospf *ospf = area->ospf;
790 struct stream *s;
791 struct lsa_header *lsah;
792 struct ospf_lsa *new;
793 int length;
795 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
796 zlog_debug ("LSA[Type1]: Create router-LSA instance");
798 /* check whether stub-router is desired, and if this is the first
799 * router LSA.
801 ospf_stub_router_check (area);
803 /* Create a stream for LSA. */
804 s = stream_new (OSPF_MAX_LSA_SIZE);
805 /* Set LSA common header fields. */
806 lsa_header_set (s, LSA_OPTIONS_GET (area) | LSA_OPTIONS_NSSA_GET (area),
807 OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
809 /* Set router-LSA body fields. */
810 ospf_router_lsa_body_set (s, area);
812 /* Set length. */
813 length = stream_get_endp (s);
814 lsah = (struct lsa_header *) STREAM_DATA (s);
815 lsah->length = htons (length);
817 /* Now, create OSPF LSA instance. */
818 if ( (new = ospf_lsa_new ()) == NULL)
820 zlog_err ("%s: Unable to create new lsa", __func__);
821 return NULL;
824 new->area = area;
825 SET_FLAG (new->flags, OSPF_LSA_SELF);
827 /* Copy LSA data to store, discard stream. */
828 new->data = ospf_lsa_data_new (length);
829 memcpy (new->data, lsah, length);
830 stream_free (s);
832 return new;
835 /* Originate Router-LSA. */
836 static struct ospf_lsa *
837 ospf_router_lsa_originate (struct ospf_area *area)
839 struct ospf_lsa *new;
841 /* Create new router-LSA instance. */
842 if ( (new = ospf_router_lsa_new (area)) == NULL)
844 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
845 return NULL;
848 /* Sanity check. */
849 if (new->data->adv_router.s_addr == 0)
851 if (IS_DEBUG_OSPF_EVENT)
852 zlog_debug ("LSA[Type1]: AdvRouter is 0, discard");
853 ospf_lsa_discard (new);
854 return NULL;
857 /* Install LSA to LSDB. */
858 new = ospf_lsa_install (area->ospf, NULL, new);
860 /* Update LSA origination count. */
861 area->ospf->lsa_originate_count++;
863 /* Flooding new LSA through area. */
864 ospf_flood_through_area (area, NULL, new);
866 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
868 zlog_debug ("LSA[Type%d:%s]: Originate router-LSA %p",
869 new->data->type, inet_ntoa (new->data->id), new);
870 ospf_lsa_header_dump (new->data);
873 return new;
876 /* Refresh router-LSA. */
877 static struct ospf_lsa *
878 ospf_router_lsa_refresh (struct ospf_lsa *lsa)
880 struct ospf_area *area = lsa->area;
881 struct ospf_lsa *new;
883 /* Sanity check. */
884 assert (lsa->data);
886 /* Delete LSA from neighbor retransmit-list. */
887 ospf_ls_retransmit_delete_nbr_area (area, lsa);
889 /* Create new router-LSA instance. */
890 if ( (new = ospf_router_lsa_new (area)) == NULL)
892 zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
893 return NULL;
896 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
898 ospf_lsa_install (area->ospf, NULL, new);
900 /* Flood LSA through area. */
901 ospf_flood_through_area (area, NULL, new);
903 /* Debug logging. */
904 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
906 zlog_debug ("LSA[Type%d:%s]: router-LSA refresh",
907 new->data->type, inet_ntoa (new->data->id));
908 ospf_lsa_header_dump (new->data);
911 return NULL;
914 static int
915 ospf_router_lsa_timer (struct thread *t)
917 struct ospf_area *area;
919 if (IS_DEBUG_OSPF_EVENT)
920 zlog_debug ("Timer[router-LSA]: (router-LSA Refresh expire)");
922 area = THREAD_ARG (t);
923 area->t_router_lsa_self = NULL;
925 /* Now refresh router-LSA. */
926 if (area->router_lsa_self)
927 ospf_router_lsa_refresh (area->router_lsa_self);
928 /* Newly originate router-LSA. */
929 else
930 ospf_router_lsa_originate (area);
932 return 0;
935 void
936 ospf_router_lsa_timer_add (struct ospf_area *area)
938 /* Keep area's self-originated router-LSA. */
939 struct ospf_lsa *lsa = area->router_lsa_self;
941 /* Cancel previously scheduled router-LSA timer. */
942 if (area->t_router_lsa_self)
943 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
944 zlog_debug ("LSA[Type1]: Cancel previous router-LSA timer");
946 OSPF_TIMER_OFF (area->t_router_lsa_self);
948 /* If router-LSA is originated previously, check the interval time. */
949 if (lsa)
951 int delay;
952 if ((delay = ospf_lsa_refresh_delay (lsa)) > 0)
954 OSPF_AREA_TIMER_ON (area->t_router_lsa_self,
955 ospf_router_lsa_timer, delay);
956 return;
960 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
961 zlog_debug ("LSA[Type1]: Scheduling router-LSA origination right away");
963 /* Immediately refresh router-LSA. */
964 OSPF_AREA_TIMER_ON (area->t_router_lsa_self, ospf_router_lsa_timer, 0);
968 ospf_router_lsa_update_timer (struct thread *thread)
970 struct ospf *ospf = THREAD_ARG (thread);
971 struct listnode *node, *nnode;
972 struct ospf_area *area;
974 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
975 zlog_debug ("Timer[router-LSA Update]: (timer expire)");
977 ospf->t_router_lsa_update = NULL;
979 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
981 struct ospf_lsa *lsa = area->router_lsa_self;
982 struct router_lsa *rl;
983 const char *area_str;
985 /* Keep Area ID string. */
986 area_str = AREA_NAME (area);
988 /* If LSA not exist in this Area, originate new. */
989 if (lsa == NULL)
991 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
992 zlog_debug("LSA[Type1]: Create router-LSA for Area %s", area_str);
994 ospf_router_lsa_originate (area);
996 /* If router-ID is changed, Link ID must change.
997 First flush old LSA, then originate new. */
998 else if (!IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
1000 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1001 zlog_debug("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
1002 lsa->data->type, inet_ntoa (lsa->data->id), area_str);
1003 ospf_lsa_flush_area (lsa, area);
1004 ospf_lsa_unlock (&area->router_lsa_self);
1005 area->router_lsa_self = NULL;
1007 /* Refresh router-LSA, (not install) and flood through area. */
1008 ospf_router_lsa_timer_add (area);
1010 else
1012 rl = (struct router_lsa *) lsa->data;
1013 /* Refresh router-LSA, (not install) and flood through area. */
1014 if (rl->flags != ospf->flags)
1015 ospf_router_lsa_timer_add (area);
1019 return 0;
1023 /* network-LSA related functions. */
1024 /* Originate Network-LSA. */
1025 static void
1026 ospf_network_lsa_body_set (struct stream *s, struct ospf_interface *oi)
1028 struct in_addr mask;
1029 struct route_node *rn;
1030 struct ospf_neighbor *nbr;
1032 masklen2ip (oi->address->prefixlen, &mask);
1033 stream_put_ipv4 (s, mask.s_addr);
1035 /* The network-LSA lists those routers that are fully adjacent to
1036 the Designated Router; each fully adjacent router is identified by
1037 its OSPF Router ID. The Designated Router includes itself in this
1038 list. RFC2328, Section 12.4.2 */
1040 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
1041 if ((nbr = rn->info) != NULL)
1042 if (nbr->state == NSM_Full || nbr == oi->nbr_self)
1043 stream_put_ipv4 (s, nbr->router_id.s_addr);
1046 static struct ospf_lsa *
1047 ospf_network_lsa_new (struct ospf_interface *oi)
1049 struct stream *s;
1050 struct ospf_lsa *new;
1051 struct lsa_header *lsah;
1052 int length;
1054 /* If there are no neighbours on this network (the net is stub),
1055 the router does not originate network-LSA (see RFC 12.4.2) */
1056 if (oi->full_nbrs == 0)
1057 return NULL;
1059 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1060 zlog_debug ("LSA[Type2]: Create network-LSA instance");
1062 /* Create new stream for LSA. */
1063 s = stream_new (OSPF_MAX_LSA_SIZE);
1064 lsah = (struct lsa_header *) STREAM_DATA (s);
1066 lsa_header_set (s, (OPTIONS (oi) | LSA_OPTIONS_GET (oi->area)),
1067 OSPF_NETWORK_LSA, DR (oi), oi->ospf->router_id);
1069 /* Set network-LSA body fields. */
1070 ospf_network_lsa_body_set (s, oi);
1072 /* Set length. */
1073 length = stream_get_endp (s);
1074 lsah->length = htons (length);
1076 /* Create OSPF LSA instance. */
1077 if ( (new = ospf_lsa_new ()) == NULL)
1079 zlog_err ("%s: ospf_lsa_new returned NULL", __func__);
1080 return NULL;
1083 new->area = oi->area;
1084 SET_FLAG (new->flags, OSPF_LSA_SELF);
1086 /* Copy LSA to store. */
1087 new->data = ospf_lsa_data_new (length);
1088 memcpy (new->data, lsah, length);
1089 stream_free (s);
1091 return new;
1094 /* Originate network-LSA. */
1095 static struct ospf_lsa *
1096 ospf_network_lsa_originate (struct ospf_interface *oi)
1098 struct ospf_lsa *new;
1100 /* Create new network-LSA instance. */
1101 new = ospf_network_lsa_new (oi);
1102 if (new == NULL)
1103 return NULL;
1105 /* Install LSA to LSDB. */
1106 new = ospf_lsa_install (oi->ospf, oi, new);
1108 /* Update LSA origination count. */
1109 oi->ospf->lsa_originate_count++;
1111 /* Flooding new LSA through area. */
1112 ospf_flood_through_area (oi->area, NULL, new);
1114 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1116 zlog_debug ("LSA[Type%d:%s]: Originate network-LSA %p",
1117 new->data->type, inet_ntoa (new->data->id), new);
1118 ospf_lsa_header_dump (new->data);
1121 return new;
1125 ospf_network_lsa_refresh (struct ospf_lsa *lsa, struct ospf_interface *oi)
1127 struct ospf_area *area = lsa->area;
1128 struct ospf_lsa *new;
1130 assert (lsa->data);
1132 /* Delete LSA from neighbor retransmit-list. */
1133 ospf_ls_retransmit_delete_nbr_area (area, lsa);
1135 /* Create new network-LSA instance. */
1136 new = ospf_network_lsa_new (oi);
1137 if (new == NULL)
1138 return -1;
1139 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1141 ospf_lsa_install (area->ospf, oi, new);
1143 /* Flood LSA through aera. */
1144 ospf_flood_through_area (area, NULL, new);
1146 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1148 zlog_debug ("LSA[Type%d:%s]: network-LSA refresh",
1149 new->data->type, inet_ntoa (new->data->id));
1150 ospf_lsa_header_dump (new->data);
1153 return 0;
1156 static int
1157 ospf_network_lsa_refresh_timer (struct thread *t)
1159 struct ospf_interface *oi;
1161 oi = THREAD_ARG (t);
1162 oi->t_network_lsa_self = NULL;
1164 if (oi->network_lsa_self)
1165 /* Now refresh network-LSA. */
1166 ospf_network_lsa_refresh (oi->network_lsa_self, oi);
1167 else
1168 /* Newly create network-LSA. */
1169 ospf_network_lsa_originate (oi);
1171 return 0;
1174 void
1175 ospf_network_lsa_timer_add (struct ospf_interface *oi)
1177 /* Keep interface's self-originated network-LSA. */
1178 struct ospf_lsa *lsa = oi->network_lsa_self;
1180 /* Cancel previously schedules network-LSA timer. */
1181 if (oi->t_network_lsa_self)
1182 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1183 zlog_debug ("LSA[Type2]: Cancel previous network-LSA timer");
1184 OSPF_TIMER_OFF (oi->t_network_lsa_self);
1186 /* If network-LSA is originated previously, check the interval time. */
1187 if (lsa)
1189 int delay;
1190 if ((delay = ospf_lsa_refresh_delay (lsa)) > 0)
1192 oi->t_network_lsa_self =
1193 thread_add_timer (master, ospf_network_lsa_refresh_timer,
1194 oi, delay);
1195 return;
1199 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1200 zlog_debug ("Scheduling network-LSA origination right away");
1202 /* Immediately refresh network-LSA. */
1203 oi->t_network_lsa_self =
1204 thread_add_event (master, ospf_network_lsa_refresh_timer, oi, 0);
1208 static void
1209 stream_put_ospf_metric (struct stream *s, u_int32_t metric_value)
1211 u_int32_t metric;
1212 char *mp;
1214 /* Put 0 metric. TOS metric is not supported. */
1215 metric = htonl (metric_value);
1216 mp = (char *) &metric;
1217 mp++;
1218 stream_put (s, mp, 3);
1221 /* summary-LSA related functions. */
1222 static void
1223 ospf_summary_lsa_body_set (struct stream *s, struct prefix *p,
1224 u_int32_t metric)
1226 struct in_addr mask;
1228 masklen2ip (p->prefixlen, &mask);
1230 /* Put Network Mask. */
1231 stream_put_ipv4 (s, mask.s_addr);
1233 /* Set # TOS. */
1234 stream_putc (s, (u_char) 0);
1236 /* Set metric. */
1237 stream_put_ospf_metric (s, metric);
1240 static struct ospf_lsa *
1241 ospf_summary_lsa_new (struct ospf_area *area, struct prefix *p,
1242 u_int32_t metric, struct in_addr id)
1244 struct stream *s;
1245 struct ospf_lsa *new;
1246 struct lsa_header *lsah;
1247 int length;
1249 if (id.s_addr == 0xffffffff)
1251 /* Maybe Link State ID not available. */
1252 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1253 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1254 OSPF_SUMMARY_LSA);
1255 return NULL;
1258 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1259 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1261 /* Create new stream for LSA. */
1262 s = stream_new (OSPF_MAX_LSA_SIZE);
1263 lsah = (struct lsa_header *) STREAM_DATA (s);
1265 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_SUMMARY_LSA,
1266 id, area->ospf->router_id);
1268 /* Set summary-LSA body fields. */
1269 ospf_summary_lsa_body_set (s, p, metric);
1271 /* Set length. */
1272 length = stream_get_endp (s);
1273 lsah->length = htons (length);
1275 /* Create OSPF LSA instance. */
1276 new = ospf_lsa_new ();
1277 new->area = area;
1278 SET_FLAG (new->flags, OSPF_LSA_SELF);
1280 /* Copy LSA to store. */
1281 new->data = ospf_lsa_data_new (length);
1282 memcpy (new->data, lsah, length);
1283 stream_free (s);
1285 return new;
1288 /* Originate Summary-LSA. */
1289 struct ospf_lsa *
1290 ospf_summary_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1291 struct ospf_area *area)
1293 struct ospf_lsa *new;
1294 struct in_addr id;
1296 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
1298 if (id.s_addr == 0xffffffff)
1300 /* Maybe Link State ID not available. */
1301 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1302 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1303 OSPF_SUMMARY_LSA);
1304 return NULL;
1307 /* Create new summary-LSA instance. */
1308 if ( !(new = ospf_summary_lsa_new (area, (struct prefix *) p, metric, id)))
1309 return NULL;
1311 /* Instlal LSA to LSDB. */
1312 new = ospf_lsa_install (area->ospf, NULL, new);
1314 /* Update LSA origination count. */
1315 area->ospf->lsa_originate_count++;
1317 /* Flooding new LSA through area. */
1318 ospf_flood_through_area (area, NULL, new);
1320 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1322 zlog_debug ("LSA[Type%d:%s]: Originate summary-LSA %p",
1323 new->data->type, inet_ntoa (new->data->id), new);
1324 ospf_lsa_header_dump (new->data);
1327 return new;
1330 struct ospf_lsa*
1331 ospf_summary_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1333 struct ospf_lsa *new;
1334 struct summary_lsa *sl;
1335 struct prefix p;
1337 /* Sanity check. */
1338 assert (lsa->data);
1340 sl = (struct summary_lsa *)lsa->data;
1341 p.prefixlen = ip_masklen (sl->mask);
1342 new = ospf_summary_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1343 sl->header.id);
1345 if (!new)
1346 return NULL;
1348 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1350 /* Re-calculate checksum. */
1351 ospf_lsa_checksum (new->data);
1353 ospf_lsa_install (ospf, NULL, new);
1355 /* Flood LSA through AS. */
1356 ospf_flood_through_area (new->area, NULL, new);
1358 /* Debug logging. */
1359 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1361 zlog_debug ("LSA[Type%d:%s]: summary-LSA refresh",
1362 new->data->type, inet_ntoa (new->data->id));
1363 ospf_lsa_header_dump (new->data);
1366 return new;
1370 /* summary-ASBR-LSA related functions. */
1371 static void
1372 ospf_summary_asbr_lsa_body_set (struct stream *s, struct prefix *p,
1373 u_int32_t metric)
1375 struct in_addr mask;
1377 masklen2ip (p->prefixlen, &mask);
1379 /* Put Network Mask. */
1380 stream_put_ipv4 (s, mask.s_addr);
1382 /* Set # TOS. */
1383 stream_putc (s, (u_char) 0);
1385 /* Set metric. */
1386 stream_put_ospf_metric (s, metric);
1389 static struct ospf_lsa *
1390 ospf_summary_asbr_lsa_new (struct ospf_area *area, struct prefix *p,
1391 u_int32_t metric, struct in_addr id)
1393 struct stream *s;
1394 struct ospf_lsa *new;
1395 struct lsa_header *lsah;
1396 int length;
1398 if (id.s_addr == 0xffffffff)
1400 /* Maybe Link State ID not available. */
1401 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1402 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1403 OSPF_ASBR_SUMMARY_LSA);
1404 return NULL;
1407 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1408 zlog_debug ("LSA[Type3]: Create summary-LSA instance");
1410 /* Create new stream for LSA. */
1411 s = stream_new (OSPF_MAX_LSA_SIZE);
1412 lsah = (struct lsa_header *) STREAM_DATA (s);
1414 lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_ASBR_SUMMARY_LSA,
1415 id, area->ospf->router_id);
1417 /* Set summary-LSA body fields. */
1418 ospf_summary_asbr_lsa_body_set (s, p, metric);
1420 /* Set length. */
1421 length = stream_get_endp (s);
1422 lsah->length = htons (length);
1424 /* Create OSPF LSA instance. */
1425 new = ospf_lsa_new ();
1426 new->area = area;
1427 SET_FLAG (new->flags, OSPF_LSA_SELF);
1429 /* Copy LSA to store. */
1430 new->data = ospf_lsa_data_new (length);
1431 memcpy (new->data, lsah, length);
1432 stream_free (s);
1434 return new;
1437 /* Originate summary-ASBR-LSA. */
1438 struct ospf_lsa *
1439 ospf_summary_asbr_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric,
1440 struct ospf_area *area)
1442 struct ospf_lsa *new;
1443 struct in_addr id;
1445 id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA, p);
1447 if (id.s_addr == 0xffffffff)
1449 /* Maybe Link State ID not available. */
1450 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1451 zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
1452 OSPF_ASBR_SUMMARY_LSA);
1453 return NULL;
1456 /* Create new summary-LSA instance. */
1457 new = ospf_summary_asbr_lsa_new (area, (struct prefix *) p, metric, id);
1458 if (!new)
1459 return NULL;
1461 /* Install LSA to LSDB. */
1462 new = ospf_lsa_install (area->ospf, NULL, new);
1464 /* Update LSA origination count. */
1465 area->ospf->lsa_originate_count++;
1467 /* Flooding new LSA through area. */
1468 ospf_flood_through_area (area, NULL, new);
1470 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1472 zlog_debug ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
1473 new->data->type, inet_ntoa (new->data->id), new);
1474 ospf_lsa_header_dump (new->data);
1477 return new;
1480 struct ospf_lsa*
1481 ospf_summary_asbr_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
1483 struct ospf_lsa *new;
1484 struct summary_lsa *sl;
1485 struct prefix p;
1487 /* Sanity check. */
1488 assert (lsa->data);
1490 sl = (struct summary_lsa *)lsa->data;
1491 p.prefixlen = ip_masklen (sl->mask);
1492 new = ospf_summary_asbr_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
1493 sl->header.id);
1494 if (!new)
1495 return NULL;
1497 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
1499 /* Re-calculate checksum. */
1500 ospf_lsa_checksum (new->data);
1502 ospf_lsa_install (ospf, NULL, new);
1504 /* Flood LSA through area. */
1505 ospf_flood_through_area (new->area, NULL, new);
1507 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1509 zlog_debug ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
1510 new->data->type, inet_ntoa (new->data->id));
1511 ospf_lsa_header_dump (new->data);
1514 return new;
1517 /* AS-external-LSA related functions. */
1519 /* Get nexthop for AS-external-LSAs. Return nexthop if its interface
1520 is connected, else 0*/
1521 static struct in_addr
1522 ospf_external_lsa_nexthop_get (struct ospf *ospf, struct in_addr nexthop)
1524 struct in_addr fwd;
1525 struct prefix nh;
1526 struct listnode *node;
1527 struct ospf_interface *oi;
1529 fwd.s_addr = 0;
1531 if (!nexthop.s_addr)
1532 return fwd;
1534 /* Check whether nexthop is covered by OSPF network. */
1535 nh.family = AF_INET;
1536 nh.u.prefix4 = nexthop;
1537 nh.prefixlen = IPV4_MAX_BITLEN;
1539 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
1540 if (if_is_operative (oi->ifp))
1541 if (oi->address->family == AF_INET)
1542 if (prefix_match (oi->address, &nh))
1543 return nexthop;
1545 return fwd;
1548 /* NSSA-external-LSA related functions. */
1550 /* Get 1st IP connection for Forward Addr */
1552 struct in_addr
1553 ospf_get_ip_from_ifp (struct ospf_interface *oi)
1555 struct in_addr fwd;
1557 fwd.s_addr = 0;
1559 if (if_is_operative (oi->ifp))
1560 return oi->address->u.prefix4;
1562 return fwd;
1565 /* Get 1st IP connection for Forward Addr */
1566 struct in_addr
1567 ospf_get_nssa_ip (struct ospf_area *area)
1569 struct in_addr fwd;
1570 struct in_addr best_default;
1571 struct listnode *node;
1572 struct ospf_interface *oi;
1574 fwd.s_addr = 0;
1575 best_default.s_addr = 0;
1577 for (ALL_LIST_ELEMENTS_RO (area->ospf->oiflist, node, oi))
1579 if (if_is_operative (oi->ifp))
1580 if (oi->area->external_routing == OSPF_AREA_NSSA)
1581 if (oi->address && oi->address->family == AF_INET)
1583 if (best_default.s_addr == 0)
1584 best_default = oi->address->u.prefix4;
1585 if (oi->area == area)
1586 return oi->address->u.prefix4;
1589 if (best_default.s_addr != 0)
1590 return best_default;
1592 if (best_default.s_addr != 0)
1593 return best_default;
1595 return fwd;
1598 #define DEFAULT_DEFAULT_METRIC 20
1599 #define DEFAULT_DEFAULT_ORIGINATE_METRIC 10
1600 #define DEFAULT_DEFAULT_ALWAYS_METRIC 1
1602 #define DEFAULT_METRIC_TYPE EXTERNAL_METRIC_TYPE_2
1605 metric_type (struct ospf *ospf, u_char src)
1607 return (ospf->dmetric[src].type < 0 ?
1608 DEFAULT_METRIC_TYPE : ospf->dmetric[src].type);
1612 metric_value (struct ospf *ospf, u_char src)
1614 if (ospf->dmetric[src].value < 0)
1616 if (src == DEFAULT_ROUTE)
1618 if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
1619 return DEFAULT_DEFAULT_ORIGINATE_METRIC;
1620 else
1621 return DEFAULT_DEFAULT_ALWAYS_METRIC;
1623 else if (ospf->default_metric < 0)
1624 return DEFAULT_DEFAULT_METRIC;
1625 else
1626 return ospf->default_metric;
1629 return ospf->dmetric[src].value;
1632 /* Set AS-external-LSA body. */
1633 static void
1634 ospf_external_lsa_body_set (struct stream *s, struct external_info *ei,
1635 struct ospf *ospf)
1637 struct prefix_ipv4 *p = &ei->p;
1638 struct in_addr mask, fwd_addr;
1639 u_int32_t mvalue;
1640 int mtype;
1641 int type;
1643 /* Put Network Mask. */
1644 masklen2ip (p->prefixlen, &mask);
1645 stream_put_ipv4 (s, mask.s_addr);
1647 /* If prefix is default, specify DEFAULT_ROUTE. */
1648 type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
1650 mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ?
1651 ROUTEMAP_METRIC_TYPE (ei) : metric_type (ospf, type);
1653 mvalue = (ROUTEMAP_METRIC (ei) != -1) ?
1654 ROUTEMAP_METRIC (ei) : metric_value (ospf, type);
1656 /* Put type of external metric. */
1657 stream_putc (s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
1659 /* Put 0 metric. TOS metric is not supported. */
1660 stream_put_ospf_metric (s, mvalue);
1662 /* Get forwarding address to nexthop if on the Connection List, else 0. */
1663 fwd_addr = ospf_external_lsa_nexthop_get (ospf, ei->nexthop);
1665 /* Put forwarding address. */
1666 stream_put_ipv4 (s, fwd_addr.s_addr);
1668 /* Put route tag -- This value should be introduced from configuration. */
1669 stream_putl (s, 0);
1672 /* Create new external-LSA. */
1673 static struct ospf_lsa *
1674 ospf_external_lsa_new (struct ospf *ospf,
1675 struct external_info *ei, struct in_addr *old_id)
1677 struct stream *s;
1678 struct lsa_header *lsah;
1679 struct ospf_lsa *new;
1680 struct in_addr id;
1681 int length;
1683 if (ei == NULL)
1685 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1686 zlog_debug ("LSA[Type5]: External info is NULL, could not originated");
1687 return NULL;
1690 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1691 zlog_debug ("LSA[Type5]: Originate AS-external-LSA instance");
1693 /* If old Link State ID is specified, refresh LSA with same ID. */
1694 if (old_id)
1695 id = *old_id;
1696 /* Get Link State with unique ID. */
1697 else
1699 id = ospf_lsa_unique_id (ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA, &ei->p);
1700 if (id.s_addr == 0xffffffff)
1702 /* Maybe Link State ID not available. */
1703 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
1704 zlog_debug ("LSA[Type5]: Link ID not available, can't originate");
1705 return NULL;
1709 /* Create new stream for LSA. */
1710 s = stream_new (OSPF_MAX_LSA_SIZE);
1711 lsah = (struct lsa_header *) STREAM_DATA (s);
1713 /* Set LSA common header fields. */
1714 lsa_header_set (s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA,
1715 id, ospf->router_id);
1717 /* Set AS-external-LSA body fields. */
1718 ospf_external_lsa_body_set (s, ei, ospf);
1720 /* Set length. */
1721 length = stream_get_endp (s);
1722 lsah->length = htons (length);
1724 /* Now, create OSPF LSA instance. */
1725 new = ospf_lsa_new ();
1726 new->area = NULL;
1727 SET_FLAG (new->flags, OSPF_LSA_SELF|OSPF_LSA_APPROVED);
1729 /* Copy LSA data to store, discard stream. */
1730 new->data = ospf_lsa_data_new (length);
1731 memcpy (new->data, lsah, length);
1732 stream_free (s);
1734 return new;
1737 /* As Type-7 */
1738 static void
1739 ospf_install_flood_nssa (struct ospf *ospf,
1740 struct ospf_lsa *lsa, struct external_info *ei)
1742 struct ospf_lsa *new;
1743 struct as_external_lsa *extlsa;
1744 struct ospf_area *area;
1745 struct listnode *node, *nnode;
1747 /* LSA may be a Type-5 originated via translation of a Type-7 LSA
1748 * which originated from an NSSA area. In which case it should not be
1749 * flooded back to NSSA areas.
1751 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
1752 return;
1754 /* NSSA Originate or Refresh (If anyNSSA)
1756 LSA is self-originated. And just installed as Type-5.
1757 Additionally, install as Type-7 LSDB for every attached NSSA.
1759 P-Bit controls which ABR performs translation to outside world; If
1760 we are an ABR....do not set the P-bit, because we send the Type-5,
1761 not as the ABR Translator, but as the ASBR owner within the AS!
1763 If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set. The
1764 elected ABR Translator will see the P-bit, Translate, and re-flood.
1766 Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
1767 Type-5's to non-NSSA Areas. (it will also attempt a re-install) */
1769 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
1771 /* Don't install Type-7 LSA's into nonNSSA area */
1772 if (area->external_routing != OSPF_AREA_NSSA)
1773 continue;
1775 /* make lsa duplicate, lock=1 */
1776 new = ospf_lsa_dup (lsa);
1777 new->area = area;
1778 new->data->type = OSPF_AS_NSSA_LSA;
1780 /* set P-bit if not ABR */
1781 if (! IS_OSPF_ABR (ospf))
1783 SET_FLAG(new->data->options, OSPF_OPTION_NP);
1785 /* set non-zero FWD ADDR
1787 draft-ietf-ospf-nssa-update-09.txt
1789 if the network between the NSSA AS boundary router and the
1790 adjacent AS is advertised into OSPF as an internal OSPF route,
1791 the forwarding address should be the next op address as is cu
1792 currently done with type-5 LSAs. If the intervening network is
1793 not adversited into OSPF as an internal OSPF route and the
1794 type-7 LSA's P-bit is set a forwarding address should be
1795 selected from one of the router's active OSPF inteface addresses
1796 which belong to the NSSA. If no such addresses exist, then
1797 no type-7 LSA's with the P-bit set should originate from this
1798 router. */
1800 /* kevinm: not updating lsa anymore, just new */
1801 extlsa = (struct as_external_lsa *)(new->data);
1803 if (extlsa->e[0].fwd_addr.s_addr == 0)
1804 extlsa->e[0].fwd_addr = ospf_get_nssa_ip(area); /* this NSSA area in ifp */
1806 if (extlsa->e[0].fwd_addr.s_addr == 0)
1808 if (IS_DEBUG_OSPF_NSSA)
1809 zlog_debug ("LSA[Type-7]: Could not build FWD-ADDR");
1810 ospf_lsa_discard (new);
1811 return;
1814 /* Re-calculate checksum. */
1815 ospf_lsa_checksum (new->data);
1817 /* install also as Type-7 */
1818 ospf_lsa_install (ospf, NULL, new); /* Remove Old, Lock New = 2 */
1820 /* will send each copy, lock=2+n */
1821 ospf_flood_through_as (ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
1825 static struct ospf_lsa *
1826 ospf_lsa_translated_nssa_new (struct ospf *ospf,
1827 struct ospf_lsa *type7)
1830 struct ospf_lsa *new;
1831 struct as_external_lsa *ext, *extnew;
1832 struct external_info ei;
1834 ext = (struct as_external_lsa *)(type7->data);
1836 /* need external_info struct, fill in bare minimum */
1837 ei.p.family = AF_INET;
1838 ei.p.prefix = type7->data->id;
1839 ei.p.prefixlen = ip_masklen (ext->mask);
1840 ei.type = ZEBRA_ROUTE_OSPF;
1841 ei.nexthop = ext->header.adv_router;
1842 ei.route_map_set.metric = -1;
1843 ei.route_map_set.metric_type = -1;
1844 ei.tag = 0;
1846 if ( (new = ospf_external_lsa_new (ospf, &ei, &type7->data->id)) == NULL)
1848 if (IS_DEBUG_OSPF_NSSA)
1849 zlog_debug ("ospf_nssa_translate_originate(): Could not originate "
1850 "Translated Type-5 for %s",
1851 inet_ntoa (ei.p.prefix));
1852 return NULL;
1855 extnew = (struct as_external_lsa *)(new->data);
1857 /* copy over Type-7 data to new */
1858 extnew->e[0].tos = ext->e[0].tos;
1859 extnew->e[0].route_tag = ext->e[0].route_tag;
1860 extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
1861 new->data->ls_seqnum = type7->data->ls_seqnum;
1863 /* add translated flag, checksum and lock new lsa */
1864 SET_FLAG (new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7 */
1865 ospf_lsa_checksum (new->data);
1866 new = ospf_lsa_lock (new);
1868 return new;
1871 /* compare type-5 to type-7
1872 * -1: err, 0: same, 1: different
1874 static int
1875 ospf_lsa_translated_nssa_compare (struct ospf_lsa *t7, struct ospf_lsa *t5)
1878 struct as_external_lsa *e5 = (struct as_external_lsa *)t5,
1879 *e7 = (struct as_external_lsa *)t7;
1882 /* sanity checks */
1883 if (! ((t5->data->type == OSPF_AS_EXTERNAL_LSA)
1884 && (t7->data->type == OSPF_AS_NSSA_LSA)))
1885 return -1;
1887 if (t5->data->id.s_addr != t7->data->id.s_addr)
1888 return -1;
1890 if (t5->data->ls_seqnum != t7->data->ls_seqnum)
1891 return LSA_REFRESH_FORCE;
1893 if (e5->mask.s_addr != e7->mask.s_addr)
1894 return LSA_REFRESH_FORCE;
1896 if (e5->e[0].fwd_addr.s_addr != e7->e[0].fwd_addr.s_addr)
1897 return LSA_REFRESH_FORCE;
1899 if (e5->e[0].route_tag != e7->e[0].route_tag)
1900 return LSA_REFRESH_FORCE;
1902 if (GET_METRIC (e5->e[0].metric) != GET_METRIC (e7->e[0].metric))
1903 return LSA_REFRESH_FORCE;
1905 return LSA_REFRESH_IF_CHANGED;
1908 /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
1909 struct ospf_lsa *
1910 ospf_translated_nssa_originate (struct ospf *ospf, struct ospf_lsa *type7)
1912 struct ospf_lsa *new;
1913 struct as_external_lsa *extnew;
1915 /* we cant use ospf_external_lsa_originate() as we need to set
1916 * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
1919 if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
1921 if (IS_DEBUG_OSPF_NSSA)
1922 zlog_debug ("ospf_translated_nssa_originate(): Could not translate "
1923 "Type-7, Id %s, to Type-5",
1924 inet_ntoa (type7->data->id));
1925 return NULL;
1928 extnew = (struct as_external_lsa *)new;
1930 if (IS_DEBUG_OSPF_NSSA)
1932 zlog_debug ("ospf_translated_nssa_originate(): "
1933 "translated Type 7, installed:");
1934 ospf_lsa_header_dump (new->data);
1935 zlog_debug (" Network mask: %d",ip_masklen (extnew->mask));
1936 zlog_debug (" Forward addr: %s", inet_ntoa (extnew->e[0].fwd_addr));
1939 if ( (new = ospf_lsa_install (ospf, NULL, new)) == NULL)
1941 if (IS_DEBUG_OSPF_NSSA);
1942 zlog_debug ("ospf_lsa_translated_nssa_originate(): "
1943 "Could not install LSA "
1944 "id %s", inet_ntoa (type7->data->id));
1945 return NULL;
1948 ospf->lsa_originate_count++;
1949 ospf_flood_through_as (ospf, NULL, new);
1951 return new;
1954 /* Refresh Translated from NSSA AS-external-LSA. */
1955 struct ospf_lsa *
1956 ospf_translated_nssa_refresh (struct ospf *ospf, struct ospf_lsa *type7,
1957 struct ospf_lsa *type5)
1959 struct ospf_lsa *new = NULL;
1961 /* Sanity checks. */
1962 assert (type7 || type5);
1963 if (!(type7 || type5))
1964 return NULL;
1965 if (type7)
1966 assert (type7->data);
1967 if (type5)
1968 assert (type5->data);
1969 assert (ospf->anyNSSA);
1971 /* get required data according to what has been given */
1972 if (type7 && type5 == NULL)
1974 /* find the translated Type-5 for this Type-7 */
1975 struct as_external_lsa *ext = (struct as_external_lsa *)(type7->data);
1976 struct prefix_ipv4 p =
1978 .prefix = type7->data->id,
1979 .prefixlen = ip_masklen (ext->mask),
1980 .family = AF_INET,
1983 type5 = ospf_external_info_find_lsa (ospf, &p);
1985 else if (type5 && type7 == NULL)
1987 /* find the type-7 from which supplied type-5 was translated,
1988 * ie find first type-7 with same LSA Id.
1990 struct listnode *ln, *lnn;
1991 struct route_node *rn;
1992 struct ospf_lsa *lsa;
1993 struct ospf_area *area;
1995 for (ALL_LIST_ELEMENTS (ospf->areas, ln, lnn, area))
1997 if (area->external_routing != OSPF_AREA_NSSA
1998 && !type7)
1999 continue;
2001 LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
2003 if (lsa->data->id.s_addr == type5->data->id.s_addr)
2005 type7 = lsa;
2006 break;
2012 /* do we have type7? */
2013 if (!type7)
2015 if (IS_DEBUG_OSPF_NSSA)
2016 zlog_debug ("ospf_translated_nssa_refresh(): no Type-7 found for "
2017 "Type-5 LSA Id %s",
2018 inet_ntoa (type5->data->id));
2019 return NULL;
2022 /* do we have valid translated type5? */
2023 if (type5 == NULL || !CHECK_FLAG (type5->flags, OSPF_LSA_LOCAL_XLT) )
2025 if (IS_DEBUG_OSPF_NSSA)
2026 zlog_debug ("ospf_translated_nssa_refresh(): No translated Type-5 "
2027 "found for Type-7 with Id %s",
2028 inet_ntoa (type7->data->id));
2029 return NULL;
2032 /* Delete LSA from neighbor retransmit-list. */
2033 ospf_ls_retransmit_delete_nbr_as (ospf, type5);
2035 /* create new translated LSA */
2036 if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
2038 if (IS_DEBUG_OSPF_NSSA)
2039 zlog_debug ("ospf_translated_nssa_refresh(): Could not translate "
2040 "Type-7 for %s to Type-5",
2041 inet_ntoa (type7->data->id));
2042 return NULL;
2045 if ( !(new = ospf_lsa_install (ospf, NULL, new)) )
2047 if (IS_DEBUG_OSPF_NSSA)
2048 zlog_debug ("ospf_translated_nssa_refresh(): Could not install "
2049 "translated LSA, Id %s",
2050 inet_ntoa (type7->data->id));
2051 return NULL;
2054 /* Flood LSA through area. */
2055 ospf_flood_through_as (ospf, NULL, new);
2057 return new;
2061 is_prefix_default (struct prefix_ipv4 *p)
2063 struct prefix_ipv4 q;
2065 q.family = AF_INET;
2066 q.prefix.s_addr = 0;
2067 q.prefixlen = 0;
2069 return prefix_same ((struct prefix *) p, (struct prefix *) &q);
2072 /* Originate an AS-external-LSA, install and flood. */
2073 struct ospf_lsa *
2074 ospf_external_lsa_originate (struct ospf *ospf, struct external_info *ei)
2076 struct ospf_lsa *new;
2078 /* Added for NSSA project....
2080 External LSAs are originated in ASBRs as usual, but for NSSA systems.
2081 there is the global Type-5 LSDB and a Type-7 LSDB installed for
2082 every area. The Type-7's are flooded to every IR and every ABR; We
2083 install the Type-5 LSDB so that the normal "refresh" code operates
2084 as usual, and flag them as not used during ASE calculations. The
2085 Type-7 LSDB is used for calculations. Each Type-7 has a Forwarding
2086 Address of non-zero.
2088 If an ABR is the elected NSSA translator, following SPF and during
2089 the ABR task it will translate all the scanned Type-7's, with P-bit
2090 ON and not-self generated, and translate to Type-5's throughout the
2091 non-NSSA/STUB AS.
2093 A difference in operation depends whether this ASBR is an ABR
2094 or not. If not an ABR, the P-bit is ON, to indicate that any
2095 elected NSSA-ABR can perform its translation.
2097 If an ABR, the P-bit is OFF; No ABR will perform translation and
2098 this ASBR will flood the Type-5 LSA as usual.
2100 For the case where this ASBR is not an ABR, the ASE calculations
2101 are based on the Type-5 LSDB; The Type-7 LSDB exists just to
2102 demonstrate to the user that there are LSA's that belong to any
2103 attached NSSA.
2105 Finally, it just so happens that when the ABR is translating every
2106 Type-7 into Type-5, it installs it into the Type-5 LSDB as an
2107 approved Type-5 (translated from Type-7); at the end of translation
2108 if any Translated Type-5's remain unapproved, then they must be
2109 flushed from the AS.
2113 /* Check the AS-external-LSA should be originated. */
2114 if (!ospf_redistribute_check (ospf, ei, NULL))
2115 return NULL;
2117 /* Create new AS-external-LSA instance. */
2118 if ((new = ospf_external_lsa_new (ospf, ei, NULL)) == NULL)
2120 if (IS_DEBUG_OSPF_EVENT)
2121 zlog_debug ("LSA[Type5:%s]: Could not originate AS-external-LSA",
2122 inet_ntoa (ei->p.prefix));
2123 return NULL;
2126 /* Install newly created LSA into Type-5 LSDB, lock = 1. */
2127 ospf_lsa_install (ospf, NULL, new);
2129 /* Update LSA origination count. */
2130 ospf->lsa_originate_count++;
2132 /* Flooding new LSA. only to AS (non-NSSA/STUB) */
2133 ospf_flood_through_as (ospf, NULL, new);
2135 /* If there is any attached NSSA, do special handling */
2136 if (ospf->anyNSSA &&
2137 /* stay away from translated LSAs! */
2138 !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
2139 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
2141 /* Debug logging. */
2142 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2144 zlog_debug ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
2145 new->data->type, inet_ntoa (new->data->id), new);
2146 ospf_lsa_header_dump (new->data);
2149 return new;
2152 /* Originate AS-external-LSA from external info with initial flag. */
2154 ospf_external_lsa_originate_timer (struct thread *thread)
2156 struct ospf *ospf = THREAD_ARG (thread);
2157 struct route_node *rn;
2158 struct external_info *ei;
2159 struct route_table *rt;
2160 int type = THREAD_VAL (thread);
2162 ospf->t_external_lsa = NULL;
2164 /* Originate As-external-LSA from all type of distribute source. */
2165 if ((rt = EXTERNAL_INFO (type)))
2166 for (rn = route_top (rt); rn; rn = route_next (rn))
2167 if ((ei = rn->info) != NULL)
2168 if (!is_prefix_default ((struct prefix_ipv4 *)&ei->p))
2169 if (!ospf_external_lsa_originate (ospf, ei))
2170 zlog_warn ("LSA: AS-external-LSA was not originated.");
2172 return 0;
2175 static struct external_info *
2176 ospf_default_external_info (struct ospf *ospf)
2178 int type;
2179 struct route_node *rn;
2180 struct prefix_ipv4 p;
2182 p.family = AF_INET;
2183 p.prefix.s_addr = 0;
2184 p.prefixlen = 0;
2186 /* First, lookup redistributed default route. */
2187 for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
2188 if (EXTERNAL_INFO (type) && type != ZEBRA_ROUTE_OSPF)
2190 rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
2191 if (rn != NULL)
2193 route_unlock_node (rn);
2194 assert (rn->info);
2195 if (ospf_redistribute_check (ospf, rn->info, NULL))
2196 return rn->info;
2200 return NULL;
2204 ospf_default_originate_timer (struct thread *thread)
2206 struct prefix_ipv4 p;
2207 struct in_addr nexthop;
2208 struct external_info *ei;
2209 struct ospf *ospf;
2211 ospf = THREAD_ARG (thread);
2213 p.family = AF_INET;
2214 p.prefix.s_addr = 0;
2215 p.prefixlen = 0;
2217 if (ospf->default_originate == DEFAULT_ORIGINATE_ALWAYS)
2219 /* If there is no default route via redistribute,
2220 then originate AS-external-LSA with nexthop 0 (self). */
2221 nexthop.s_addr = 0;
2222 ospf_external_info_add (DEFAULT_ROUTE, p, 0, nexthop);
2225 if ((ei = ospf_default_external_info (ospf)))
2226 ospf_external_lsa_originate (ospf, ei);
2228 return 0;
2231 /* Flush any NSSA LSAs for given prefix */
2232 void
2233 ospf_nssa_lsa_flush (struct ospf *ospf, struct prefix_ipv4 *p)
2235 struct listnode *node, *nnode;
2236 struct ospf_lsa *lsa;
2237 struct ospf_area *area;
2239 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
2241 if (area->external_routing == OSPF_AREA_NSSA)
2243 if (!(lsa = ospf_lsa_lookup (area, OSPF_AS_NSSA_LSA, p->prefix,
2244 ospf->router_id)))
2246 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2247 zlog_debug ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
2248 inet_ntoa (p->prefix), p->prefixlen);
2249 continue;
2251 ospf_ls_retransmit_delete_nbr_area (area, lsa);
2252 if (!IS_LSA_MAXAGE (lsa))
2254 ospf_refresher_unregister_lsa (ospf, lsa);
2255 ospf_lsa_flush_area (lsa, area);
2261 /* Flush an AS-external-LSA from LSDB and routing domain. */
2262 void
2263 ospf_external_lsa_flush (struct ospf *ospf,
2264 u_char type, struct prefix_ipv4 *p,
2265 unsigned int ifindex /*, struct in_addr nexthop */)
2267 struct ospf_lsa *lsa;
2269 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2270 zlog_debug ("LSA: Flushing AS-external-LSA %s/%d",
2271 inet_ntoa (p->prefix), p->prefixlen);
2273 /* First lookup LSA from LSDB. */
2274 if (!(lsa = ospf_external_info_find_lsa (ospf, p)))
2276 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2277 zlog_debug ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
2278 inet_ntoa (p->prefix), p->prefixlen);
2279 return;
2282 /* If LSA is selforiginated, not a translated LSA, and there is
2283 * NSSA area, flush Type-7 LSA's at first.
2285 if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
2286 && !(CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT)))
2287 ospf_nssa_lsa_flush (ospf, p);
2289 /* Sweep LSA from Link State Retransmit List. */
2290 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2292 /* There must be no self-originated LSA in rtrs_external. */
2293 #if 0
2294 /* Remove External route from Zebra. */
2295 ospf_zebra_delete ((struct prefix_ipv4 *) p, &nexthop);
2296 #endif
2298 if (!IS_LSA_MAXAGE (lsa))
2300 /* Unregister LSA from Refresh queue. */
2301 ospf_refresher_unregister_lsa (ospf, lsa);
2303 /* Flush AS-external-LSA through AS. */
2304 ospf_lsa_flush_as (ospf, lsa);
2307 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2308 zlog_debug ("ospf_external_lsa_flush(): stop");
2311 void
2312 ospf_external_lsa_refresh_default (struct ospf *ospf)
2314 struct prefix_ipv4 p;
2315 struct external_info *ei;
2316 struct ospf_lsa *lsa;
2318 p.family = AF_INET;
2319 p.prefixlen = 0;
2320 p.prefix.s_addr = 0;
2322 ei = ospf_default_external_info (ospf);
2323 lsa = ospf_external_info_find_lsa (ospf, &p);
2325 if (ei)
2327 if (lsa)
2329 if (IS_DEBUG_OSPF_EVENT)
2330 zlog_debug ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p", lsa);
2331 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
2333 else
2335 if (IS_DEBUG_OSPF_EVENT)
2336 zlog_debug ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
2337 ospf_external_lsa_originate (ospf, ei);
2340 else
2342 if (lsa)
2344 if (IS_DEBUG_OSPF_EVENT)
2345 zlog_debug ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
2346 ospf_lsa_flush_as (ospf, lsa);
2351 void
2352 ospf_external_lsa_refresh_type (struct ospf *ospf, u_char type, int force)
2354 struct route_node *rn;
2355 struct external_info *ei;
2357 if (type != DEFAULT_ROUTE)
2358 if (EXTERNAL_INFO(type))
2359 /* Refresh each redistributed AS-external-LSAs. */
2360 for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
2361 if ((ei = rn->info))
2362 if (!is_prefix_default (&ei->p))
2364 struct ospf_lsa *lsa;
2366 if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
2367 ospf_external_lsa_refresh (ospf, lsa, ei, force);
2368 else
2369 ospf_external_lsa_originate (ospf, ei);
2373 /* Refresh AS-external-LSA. */
2374 void
2375 ospf_external_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa,
2376 struct external_info *ei, int force)
2378 struct ospf_lsa *new;
2379 int changed;
2381 /* Check the AS-external-LSA should be originated. */
2382 if (!ospf_redistribute_check (ospf, ei, &changed))
2384 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2385 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed, "
2386 "redist check fail",
2387 lsa->data->type, inet_ntoa (lsa->data->id));
2388 ospf_external_lsa_flush (ospf, ei->type, &ei->p,
2389 ei->ifindex /*, ei->nexthop */);
2390 return;
2393 if (!changed && !force)
2395 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2396 zlog_debug ("LSA[Type%d:%s]: Not refreshed, not changed/forced",
2397 lsa->data->type, inet_ntoa (lsa->data->id));
2398 return;
2401 /* Delete LSA from neighbor retransmit-list. */
2402 ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
2404 /* Unregister AS-external-LSA from refresh-list. */
2405 ospf_refresher_unregister_lsa (ospf, lsa);
2407 new = ospf_external_lsa_new (ospf, ei, &lsa->data->id);
2409 if (new == NULL)
2411 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2412 zlog_debug ("LSA[Type%d:%s]: Could not be refreshed", lsa->data->type,
2413 inet_ntoa (lsa->data->id));
2414 return;
2417 new->data->ls_seqnum = lsa_seqnum_increment (lsa);
2419 /* Re-calculate checksum. */
2420 ospf_lsa_checksum (new->data);
2422 ospf_lsa_install (ospf, NULL, new); /* As type-5. */
2424 /* Flood LSA through AS. */
2425 ospf_flood_through_as (ospf, NULL, new);
2427 /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
2428 if (ospf->anyNSSA && !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
2429 ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood per new rules */
2431 /* Register self-originated LSA to refresh queue.
2432 * Translated LSAs should not be registered, but refreshed upon
2433 * refresh of the Type-7
2435 if ( !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT) )
2436 ospf_refresher_register_lsa (ospf, new);
2438 /* Debug logging. */
2439 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2441 zlog_debug ("LSA[Type%d:%s]: AS-external-LSA refresh",
2442 new->data->type, inet_ntoa (new->data->id));
2443 ospf_lsa_header_dump (new->data);
2446 return;
2450 /* LSA installation functions. */
2452 /* Install router-LSA to an area. */
2453 static struct ospf_lsa *
2454 ospf_router_lsa_install (struct ospf *ospf,
2455 struct ospf_lsa *new, int rt_recalc)
2457 struct ospf_area *area = new->area;
2459 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2460 The entire routing table must be recalculated, starting with
2461 the shortest path calculations for each area (not just the
2462 area whose link-state database has changed).
2464 if (rt_recalc)
2465 ospf_spf_calculate_schedule (ospf);
2467 if (IS_LSA_SELF (new))
2469 /* Set router-LSA refresh timer. */
2470 OSPF_TIMER_OFF (area->t_router_lsa_self);
2471 OSPF_AREA_TIMER_ON (area->t_router_lsa_self,
2472 ospf_router_lsa_timer, OSPF_LS_REFRESH_TIME);
2474 /* Set self-originated router-LSA. */
2475 ospf_lsa_unlock (&area->router_lsa_self);
2476 area->router_lsa_self = ospf_lsa_lock (new);
2478 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2479 zlog_debug("LSA[Type%d]: ID %s seq 0x%x is self-originated",
2480 new->data->type, inet_ntoa (new->data->id),
2481 ntohl(new->data->ls_seqnum));
2484 return new;
2487 #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
2488 if (!(T)) \
2489 (T) = thread_add_timer (master, (F), oi, (V))
2491 /* Install network-LSA to an area. */
2492 static struct ospf_lsa *
2493 ospf_network_lsa_install (struct ospf *ospf,
2494 struct ospf_interface *oi,
2495 struct ospf_lsa *new,
2496 int rt_recalc)
2499 /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
2500 The entire routing table must be recalculated, starting with
2501 the shortest path calculations for each area (not just the
2502 area whose link-state database has changed).
2504 if (rt_recalc)
2505 ospf_spf_calculate_schedule (ospf);
2507 /* We supposed that when LSA is originated by us, we pass the int
2508 for which it was originated. If LSA was received by flooding,
2509 the RECEIVED flag is set, so we do not link the LSA to the int. */
2510 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
2512 /* Set LSRefresh timer. */
2513 OSPF_TIMER_OFF (oi->t_network_lsa_self);
2515 OSPF_INTERFACE_TIMER_ON (oi->t_network_lsa_self,
2516 ospf_network_lsa_refresh_timer,
2517 OSPF_LS_REFRESH_TIME);
2519 ospf_lsa_unlock (&oi->network_lsa_self);
2520 oi->network_lsa_self = ospf_lsa_lock (new);
2523 return new;
2526 /* Install summary-LSA to an area. */
2527 static struct ospf_lsa *
2528 ospf_summary_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2529 int rt_recalc)
2531 if (rt_recalc && !IS_LSA_SELF (new))
2533 /* RFC 2328 Section 13.2 Summary-LSAs
2534 The best route to the destination described by the summary-
2535 LSA must be recalculated (see Section 16.5). If this
2536 destination is an AS boundary router, it may also be
2537 necessary to re-examine all the AS-external-LSAs.
2540 #if 0
2541 /* This doesn't exist yet... */
2542 ospf_summary_incremental_update(new); */
2543 #else /* #if 0 */
2544 ospf_spf_calculate_schedule (ospf);
2545 #endif /* #if 0 */
2547 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2548 zlog_debug ("ospf_summary_lsa_install(): SPF scheduled");
2551 if (IS_LSA_SELF (new))
2552 ospf_refresher_register_lsa (ospf, new);
2554 return new;
2557 /* Install ASBR-summary-LSA to an area. */
2558 static struct ospf_lsa *
2559 ospf_summary_asbr_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2560 int rt_recalc)
2562 if (rt_recalc && !IS_LSA_SELF (new))
2564 /* RFC 2328 Section 13.2 Summary-LSAs
2565 The best route to the destination described by the summary-
2566 LSA must be recalculated (see Section 16.5). If this
2567 destination is an AS boundary router, it may also be
2568 necessary to re-examine all the AS-external-LSAs.
2570 #if 0
2571 /* These don't exist yet... */
2572 ospf_summary_incremental_update(new);
2573 /* Isn't this done by the above call?
2574 - RFC 2328 Section 16.5 implies it should be */
2575 /* ospf_ase_calculate_schedule(); */
2576 #else /* #if 0 */
2577 ospf_spf_calculate_schedule (ospf);
2578 #endif /* #if 0 */
2581 /* register LSA to refresh-list. */
2582 if (IS_LSA_SELF (new))
2583 ospf_refresher_register_lsa (ospf, new);
2585 return new;
2588 /* Install AS-external-LSA. */
2589 static struct ospf_lsa *
2590 ospf_external_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
2591 int rt_recalc)
2593 ospf_ase_register_external_lsa (new, ospf);
2594 /* If LSA is not self-originated, calculate an external route. */
2595 if (rt_recalc)
2597 /* RFC 2328 Section 13.2 AS-external-LSAs
2598 The best route to the destination described by the AS-
2599 external-LSA must be recalculated (see Section 16.6).
2602 if (!IS_LSA_SELF (new))
2603 ospf_ase_incremental_update (ospf, new);
2606 if (new->data->type == OSPF_AS_NSSA_LSA)
2608 /* There is no point to register selforiginate Type-7 LSA for
2609 * refreshing. We rely on refreshing Type-5 LSA's
2611 if (IS_LSA_SELF (new))
2612 return new;
2613 else
2615 /* Try refresh type-5 translated LSA for this LSA, if one exists.
2616 * New translations will be taken care of by the abr_task.
2618 ospf_translated_nssa_refresh (ospf, new, NULL);
2622 /* Register self-originated LSA to refresh queue.
2623 * Leave Translated LSAs alone if NSSA is enabled
2625 if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT ) )
2626 ospf_refresher_register_lsa (ospf, new);
2628 return new;
2631 void
2632 ospf_discard_from_db (struct ospf *ospf,
2633 struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
2635 struct ospf_lsa *old;
2637 if (!lsdb)
2639 zlog_warn ("%s: Called with NULL lsdb!", __func__);
2640 if (!lsa)
2641 zlog_warn ("%s: and NULL LSA!", __func__);
2642 else
2643 zlog_warn ("LSA[Type%d:%s]: not associated with LSDB!",
2644 lsa->data->type, inet_ntoa (lsa->data->id));
2645 return;
2648 old = ospf_lsdb_lookup (lsdb, lsa);
2650 if (!old)
2651 return;
2653 if (old->refresh_list >= 0)
2654 ospf_refresher_unregister_lsa (ospf, old);
2656 switch (old->data->type)
2658 case OSPF_AS_EXTERNAL_LSA:
2659 ospf_ase_unregister_external_lsa (old, ospf);
2660 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2661 break;
2662 #ifdef HAVE_OPAQUE_LSA
2663 case OSPF_OPAQUE_AS_LSA:
2664 ospf_ls_retransmit_delete_nbr_as (ospf, old);
2665 break;
2666 #endif /* HAVE_OPAQUE_LSA */
2667 case OSPF_AS_NSSA_LSA:
2668 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2669 ospf_ase_unregister_external_lsa (old, ospf);
2670 break;
2671 default:
2672 ospf_ls_retransmit_delete_nbr_area (old->area, old);
2673 break;
2676 ospf_lsa_maxage_delete (ospf, old);
2677 ospf_lsa_discard (old);
2680 struct ospf_lsa *
2681 ospf_lsa_install (struct ospf *ospf, struct ospf_interface *oi,
2682 struct ospf_lsa *lsa)
2684 struct ospf_lsa *new = NULL;
2685 struct ospf_lsa *old = NULL;
2686 struct ospf_lsdb *lsdb = NULL;
2687 int rt_recalc;
2689 /* Set LSDB. */
2690 switch (lsa->data->type)
2692 /* kevinm */
2693 case OSPF_AS_NSSA_LSA:
2694 if (lsa->area)
2695 lsdb = lsa->area->lsdb;
2696 else
2697 lsdb = ospf->lsdb;
2698 break;
2699 case OSPF_AS_EXTERNAL_LSA:
2700 #ifdef HAVE_OPAQUE_LSA
2701 case OSPF_OPAQUE_AS_LSA:
2702 #endif /* HAVE_OPAQUE_LSA */
2703 lsdb = ospf->lsdb;
2704 break;
2705 default:
2706 lsdb = lsa->area->lsdb;
2707 break;
2710 assert (lsdb);
2712 /* RFC 2328 13.2. Installing LSAs in the database
2714 Installing a new LSA in the database, either as the result of
2715 flooding or a newly self-originated LSA, may cause the OSPF
2716 routing table structure to be recalculated. The contents of the
2717 new LSA should be compared to the old instance, if present. If
2718 there is no difference, there is no need to recalculate the
2719 routing table. When comparing an LSA to its previous instance,
2720 the following are all considered to be differences in contents:
2722 o The LSA's Options field has changed.
2724 o One of the LSA instances has LS age set to MaxAge, and
2725 the other does not.
2727 o The length field in the LSA header has changed.
2729 o The body of the LSA (i.e., anything outside the 20-byte
2730 LSA header) has changed. Note that this excludes changes
2731 in LS Sequence Number and LS Checksum.
2734 /* Look up old LSA and determine if any SPF calculation or incremental
2735 update is needed */
2736 old = ospf_lsdb_lookup (lsdb, lsa);
2738 /* Do comparision and record if recalc needed. */
2739 rt_recalc = 0;
2740 if ( old == NULL || ospf_lsa_different(old, lsa))
2741 rt_recalc = 1;
2744 Sequence number check (Section 14.1 of rfc 2328)
2745 "Premature aging is used when it is time for a self-originated
2746 LSA's sequence number field to wrap. At this point, the current
2747 LSA instance (having LS sequence number MaxSequenceNumber) must
2748 be prematurely aged and flushed from the routing domain before a
2749 new instance with sequence number equal to InitialSequenceNumber
2750 can be originated. "
2753 if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER)
2755 if (ospf_lsa_is_self_originated(ospf, lsa))
2757 lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
2759 if (!IS_LSA_MAXAGE(lsa))
2760 lsa->flags |= OSPF_LSA_PREMATURE_AGE;
2761 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
2763 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
2765 zlog_debug ("ospf_lsa_install() Premature Aging "
2766 "lsa 0x%lx", (u_long)lsa);
2767 ospf_lsa_header_dump (lsa->data);
2770 else
2772 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
2774 zlog_debug ("ospf_lsa_install() got an lsa with seq 0x80000000 "
2775 "that was not self originated. Ignoring\n");
2776 ospf_lsa_header_dump (lsa->data);
2778 return old;
2782 /* discard old LSA from LSDB */
2783 if (old != NULL)
2784 ospf_discard_from_db (ospf, lsdb, lsa);
2786 /* Calculate Checksum if self-originated?. */
2787 if (IS_LSA_SELF (lsa))
2788 ospf_lsa_checksum (lsa->data);
2790 /* Insert LSA to LSDB. */
2791 ospf_lsdb_add (lsdb, lsa);
2792 lsa->lsdb = lsdb;
2794 /* Do LSA specific installation process. */
2795 switch (lsa->data->type)
2797 case OSPF_ROUTER_LSA:
2798 new = ospf_router_lsa_install (ospf, lsa, rt_recalc);
2799 break;
2800 case OSPF_NETWORK_LSA:
2801 assert (oi);
2802 new = ospf_network_lsa_install (ospf, oi, lsa, rt_recalc);
2803 break;
2804 case OSPF_SUMMARY_LSA:
2805 new = ospf_summary_lsa_install (ospf, lsa, rt_recalc);
2806 break;
2807 case OSPF_ASBR_SUMMARY_LSA:
2808 new = ospf_summary_asbr_lsa_install (ospf, lsa, rt_recalc);
2809 break;
2810 case OSPF_AS_EXTERNAL_LSA:
2811 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2812 break;
2813 #ifdef HAVE_OPAQUE_LSA
2814 case OSPF_OPAQUE_LINK_LSA:
2815 if (IS_LSA_SELF (lsa))
2816 lsa->oi = oi; /* Specify outgoing ospf-interface for this LSA. */
2817 else
2818 ; /* Incoming "oi" for this LSA has set at LSUpd reception. */
2819 /* Fallthrough */
2820 case OSPF_OPAQUE_AREA_LSA:
2821 case OSPF_OPAQUE_AS_LSA:
2822 new = ospf_opaque_lsa_install (lsa, rt_recalc);
2823 break;
2824 #endif /* HAVE_OPAQUE_LSA */
2825 case OSPF_AS_NSSA_LSA:
2826 new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
2827 default: /* type-6,8,9....nothing special */
2828 break;
2831 if (new == NULL)
2832 return new; /* Installation failed, cannot proceed further -- endo. */
2834 /* Debug logs. */
2835 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2837 char area_str[INET_ADDRSTRLEN];
2839 switch (lsa->data->type)
2841 case OSPF_AS_EXTERNAL_LSA:
2842 #ifdef HAVE_OPAQUE_LSA
2843 case OSPF_OPAQUE_AS_LSA:
2844 #endif /* HAVE_OPAQUE_LSA */
2845 case OSPF_AS_NSSA_LSA:
2846 zlog_debug ("LSA[%s]: Install %s",
2847 dump_lsa_key (new),
2848 LOOKUP (ospf_lsa_type_msg, new->data->type));
2849 break;
2850 default:
2851 strcpy (area_str, inet_ntoa (new->area->area_id));
2852 zlog_debug ("LSA[%s]: Install %s to Area %s",
2853 dump_lsa_key (new),
2854 LOOKUP (ospf_lsa_type_msg, new->data->type), area_str);
2855 break;
2860 If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
2861 (it's getting flushed out of the area), set LSA on MaxAge LSA list.
2863 if ((lsa->flags & OSPF_LSA_PREMATURE_AGE) ||
2864 (IS_LSA_MAXAGE (new) && !IS_LSA_SELF (new)))
2866 if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
2867 zlog_debug ("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
2868 new->data->type,
2869 inet_ntoa (new->data->id),
2870 lsa);
2871 ospf_lsa_maxage (ospf, lsa);
2874 return new;
2878 static int
2879 ospf_check_nbr_status (struct ospf *ospf)
2881 struct listnode *node, *nnode;
2882 struct ospf_interface *oi;
2884 for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
2886 struct route_node *rn;
2887 struct ospf_neighbor *nbr;
2889 if (ospf_if_is_enable (oi))
2890 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
2891 if ((nbr = rn->info) != NULL)
2892 if (nbr->state == NSM_Exchange || nbr->state == NSM_Loading)
2894 route_unlock_node (rn);
2895 return 0;
2899 return 1;
2903 #ifdef ORIGINAL_CODING
2904 /* This function flood the maxaged LSA to DR. */
2905 void
2906 ospf_maxage_flood (struct ospf_lsa *lsa)
2908 switch (lsa->data->type)
2910 case OSPF_ROUTER_LSA:
2911 case OSPF_NETWORK_LSA:
2912 case OSPF_SUMMARY_LSA:
2913 case OSPF_ASBR_SUMMARY_LSA:
2914 case OSPF_AS_NSSA_LSA:
2915 #ifdef HAVE_OPAQUE_LSA
2916 case OSPF_OPAQUE_LINK_LSA:
2917 case OSPF_OPAQUE_AREA_LSA:
2918 #endif /* HAVE_OPAQUE_LSA */
2919 ospf_flood_through_area (lsa->area, NULL, lsa);
2920 break;
2921 case OSPF_AS_EXTERNAL_LSA:
2922 #ifdef HAVE_OPAQUE_LSA
2923 case OSPF_OPAQUE_AS_LSA:
2924 #endif /* HAVE_OPAQUE_LSA */
2925 ospf_flood_through_as (NULL, lsa);
2926 break;
2927 default:
2928 break;
2931 #endif /* ORIGINAL_CODING */
2933 static int
2934 ospf_maxage_lsa_remover (struct thread *thread)
2936 struct ospf *ospf = THREAD_ARG (thread);
2937 struct ospf_lsa *lsa;
2938 struct listnode *node, *nnode;
2939 int reschedule = 0;
2941 ospf->t_maxage = NULL;
2943 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2944 zlog_debug ("LSA[MaxAge]: remover Start");
2946 reschedule = !ospf_check_nbr_status (ospf);
2948 if (!reschedule)
2949 for (ALL_LIST_ELEMENTS (ospf->maxage_lsa, node, nnode, lsa))
2951 if (lsa->retransmit_counter > 0)
2953 reschedule = 1;
2954 continue;
2957 /* Remove LSA from the LSDB */
2958 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF))
2959 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2960 zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-oririnated: ",
2961 lsa->data->type, inet_ntoa (lsa->data->id), (u_long)lsa);
2963 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2964 zlog_debug ("LSA[Type%d:%s]: MaxAge LSA removed from list",
2965 lsa->data->type, inet_ntoa (lsa->data->id));
2967 /* Flood max age LSA. */
2968 #ifdef ORIGINAL_CODING
2969 ospf_maxage_flood (lsa);
2970 #else /* ORIGINAL_CODING */
2971 ospf_flood_through (ospf, NULL, lsa);
2972 #endif /* ORIGINAL_CODING */
2974 if (lsa->flags & OSPF_LSA_PREMATURE_AGE)
2976 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
2977 zlog_debug ("originating new router lsa for lsa 0x%lx \n",
2978 (u_long)lsa);
2979 ospf_router_lsa_originate(lsa->area);
2982 /* Remove from lsdb. */
2983 if (lsa->lsdb)
2985 ospf_discard_from_db (ospf, lsa->lsdb, lsa);
2986 ospf_lsdb_delete (lsa->lsdb, lsa);
2988 else
2989 zlog_warn ("%s: LSA[Type%d:%s]: No associated LSDB!", __func__,
2990 lsa->data->type, inet_ntoa (lsa->data->id));
2993 /* A MaxAge LSA must be removed immediately from the router's link
2994 state database as soon as both a) it is no longer contained on any
2995 neighbor Link state retransmission lists and b) none of the router's
2996 neighbors are in states Exchange or Loading. */
2997 if (reschedule)
2998 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 2);
3000 return 0;
3003 static int
3004 ospf_lsa_maxage_exist (struct ospf *ospf, struct ospf_lsa *new)
3006 struct listnode *node;
3007 struct ospf_lsa *lsa;
3009 for (ALL_LIST_ELEMENTS_RO (ospf->maxage_lsa, node, lsa))
3010 if (lsa == new)
3011 return 1;
3013 return 0;
3016 void
3017 ospf_lsa_maxage_delete (struct ospf *ospf, struct ospf_lsa *lsa)
3019 struct listnode *n;
3021 if ((n = listnode_lookup (ospf->maxage_lsa, lsa)))
3023 list_delete_node (ospf->maxage_lsa, n);
3024 ospf_lsa_unlock (&lsa); /* maxage_lsa */
3028 void
3029 ospf_lsa_maxage (struct ospf *ospf, struct ospf_lsa *lsa)
3031 /* When we saw a MaxAge LSA flooded to us, we put it on the list
3032 and schedule the MaxAge LSA remover. */
3033 if (ospf_lsa_maxage_exist (ospf, lsa))
3035 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3036 zlog_debug ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
3037 lsa->data->type, inet_ntoa (lsa->data->id), lsa);
3038 return;
3041 listnode_add (ospf->maxage_lsa, ospf_lsa_lock (lsa));
3043 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3044 zlog_debug ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa));
3046 OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 2);
3049 static int
3050 ospf_lsa_maxage_walker_remover (struct ospf *ospf, struct ospf_lsa *lsa)
3052 /* Stay away from any Local Translated Type-7 LSAs */
3053 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
3054 return 0;
3056 if (IS_LSA_MAXAGE (lsa))
3057 /* Self-originated LSAs should NOT time-out instead,
3058 they're flushed and submitted to the max_age list explicitly. */
3059 if (!ospf_lsa_is_self_originated (ospf, lsa))
3061 if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
3062 zlog_debug("LSA[%s]: is MaxAge", dump_lsa_key (lsa));
3064 switch (lsa->data->type)
3066 #ifdef HAVE_OPAQUE_LSA
3067 case OSPF_OPAQUE_LINK_LSA:
3068 case OSPF_OPAQUE_AREA_LSA:
3069 case OSPF_OPAQUE_AS_LSA:
3071 * As a general rule, whenever network topology has changed
3072 * (due to an LSA removal in this case), routing recalculation
3073 * should be triggered. However, this is not true for opaque
3074 * LSAs. Even if an opaque LSA instance is going to be removed
3075 * from the routing domain, it does not mean a change in network
3076 * topology, and thus, routing recalculation is not needed here.
3078 break;
3079 #endif /* HAVE_OPAQUE_LSA */
3080 case OSPF_AS_EXTERNAL_LSA:
3081 case OSPF_AS_NSSA_LSA:
3082 ospf_ase_incremental_update (ospf, lsa);
3083 break;
3084 default:
3085 ospf_spf_calculate_schedule (ospf);
3086 break;
3088 ospf_lsa_maxage (ospf, lsa);
3091 return 0;
3094 /* Periodical check of MaxAge LSA. */
3096 ospf_lsa_maxage_walker (struct thread *thread)
3098 struct ospf *ospf = THREAD_ARG (thread);
3099 struct route_node *rn;
3100 struct ospf_lsa *lsa;
3101 struct ospf_area *area;
3102 struct listnode *node, *nnode;
3104 ospf->t_maxage_walker = NULL;
3106 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
3108 LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
3109 ospf_lsa_maxage_walker_remover (ospf, lsa);
3110 LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
3111 ospf_lsa_maxage_walker_remover (ospf, lsa);
3112 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
3113 ospf_lsa_maxage_walker_remover (ospf, lsa);
3114 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
3115 ospf_lsa_maxage_walker_remover (ospf, lsa);
3116 #ifdef HAVE_OPAQUE_LSA
3117 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
3118 ospf_lsa_maxage_walker_remover (ospf, lsa);
3119 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
3120 ospf_lsa_maxage_walker_remover (ospf, lsa);
3121 #endif /* HAVE_OPAQUE_LSA */
3122 LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
3123 ospf_lsa_maxage_walker_remover (ospf, lsa);
3126 /* for AS-external-LSAs. */
3127 if (ospf->lsdb)
3129 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
3130 ospf_lsa_maxage_walker_remover (ospf, lsa);
3131 #ifdef HAVE_OPAQUE_LSA
3132 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
3133 ospf_lsa_maxage_walker_remover (ospf, lsa);
3134 #endif /* HAVE_OPAQUE_LSA */
3137 OSPF_TIMER_ON (ospf->t_maxage_walker, ospf_lsa_maxage_walker,
3138 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
3139 return 0;
3142 struct ospf_lsa *
3143 ospf_lsa_lookup_by_prefix (struct ospf_lsdb *lsdb, u_char type,
3144 struct prefix_ipv4 *p, struct in_addr router_id)
3146 struct ospf_lsa *lsa;
3147 struct in_addr mask, id;
3148 struct lsa_header_mask
3150 struct lsa_header header;
3151 struct in_addr mask;
3152 } *hmask;
3154 lsa = ospf_lsdb_lookup_by_id (lsdb, type, p->prefix, router_id);
3155 if (lsa == NULL)
3156 return NULL;
3158 masklen2ip (p->prefixlen, &mask);
3160 hmask = (struct lsa_header_mask *) lsa->data;
3162 if (mask.s_addr != hmask->mask.s_addr)
3164 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3165 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, router_id);
3166 if (!lsa)
3167 return NULL;
3170 return lsa;
3173 struct ospf_lsa *
3174 ospf_lsa_lookup (struct ospf_area *area, u_int32_t type,
3175 struct in_addr id, struct in_addr adv_router)
3177 struct ospf *ospf = ospf_lookup();
3178 assert(ospf);
3180 switch (type)
3182 case OSPF_ROUTER_LSA:
3183 case OSPF_NETWORK_LSA:
3184 case OSPF_SUMMARY_LSA:
3185 case OSPF_ASBR_SUMMARY_LSA:
3186 case OSPF_AS_NSSA_LSA:
3187 #ifdef HAVE_OPAQUE_LSA
3188 case OSPF_OPAQUE_LINK_LSA:
3189 case OSPF_OPAQUE_AREA_LSA:
3190 #endif /* HAVE_OPAQUE_LSA */
3191 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, adv_router);
3192 case OSPF_AS_EXTERNAL_LSA:
3193 #ifdef HAVE_OPAQUE_LSA
3194 case OSPF_OPAQUE_AS_LSA:
3195 #endif /* HAVE_OPAQUE_LSA */
3196 return ospf_lsdb_lookup_by_id (ospf->lsdb, type, id, adv_router);
3197 default:
3198 break;
3201 return NULL;
3204 struct ospf_lsa *
3205 ospf_lsa_lookup_by_id (struct ospf_area *area, u_int32_t type,
3206 struct in_addr id)
3208 struct ospf_lsa *lsa;
3209 struct route_node *rn;
3211 switch (type)
3213 case OSPF_ROUTER_LSA:
3214 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
3215 case OSPF_NETWORK_LSA:
3216 for (rn = route_top (NETWORK_LSDB (area)); rn; rn = route_next (rn))
3217 if ((lsa = rn->info))
3218 if (IPV4_ADDR_SAME (&lsa->data->id, &id))
3220 route_unlock_node (rn);
3221 return lsa;
3223 break;
3224 case OSPF_SUMMARY_LSA:
3225 case OSPF_ASBR_SUMMARY_LSA:
3226 /* Currently not used. */
3227 assert (1);
3228 return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
3229 case OSPF_AS_EXTERNAL_LSA:
3230 case OSPF_AS_NSSA_LSA:
3231 #ifdef HAVE_OPAQUE_LSA
3232 case OSPF_OPAQUE_LINK_LSA:
3233 case OSPF_OPAQUE_AREA_LSA:
3234 case OSPF_OPAQUE_AS_LSA:
3235 /* Currently not used. */
3236 break;
3237 #endif /* HAVE_OPAQUE_LSA */
3238 default:
3239 break;
3242 return NULL;
3245 struct ospf_lsa *
3246 ospf_lsa_lookup_by_header (struct ospf_area *area, struct lsa_header *lsah)
3248 struct ospf_lsa *match;
3250 #ifdef HAVE_OPAQUE_LSA
3252 * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
3253 * is redefined to have two subfields; opaque-type and opaque-id.
3254 * However, it is harmless to treat the two sub fields together, as if
3255 * they two were forming a unique LSA-ID.
3257 #endif /* HAVE_OPAQUE_LSA */
3259 match = ospf_lsa_lookup (area, lsah->type, lsah->id, lsah->adv_router);
3261 if (match == NULL)
3262 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3263 zlog_debug ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
3264 lsah->type, inet_ntoa (lsah->id));
3266 return match;
3269 /* return +n, l1 is more recent.
3270 return -n, l2 is more recent.
3271 return 0, l1 and l2 is identical. */
3273 ospf_lsa_more_recent (struct ospf_lsa *l1, struct ospf_lsa *l2)
3275 int r;
3276 int x, y;
3278 if (l1 == NULL && l2 == NULL)
3279 return 0;
3280 if (l1 == NULL)
3281 return -1;
3282 if (l2 == NULL)
3283 return 1;
3285 /* compare LS sequence number. */
3286 x = (int) ntohl (l1->data->ls_seqnum);
3287 y = (int) ntohl (l2->data->ls_seqnum);
3288 if (x > y)
3289 return 1;
3290 if (x < y)
3291 return -1;
3293 /* compare LS checksum. */
3294 r = ntohs (l1->data->checksum) - ntohs (l2->data->checksum);
3295 if (r)
3296 return r;
3298 /* compare LS age. */
3299 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
3300 return 1;
3301 else if (!IS_LSA_MAXAGE (l1) && IS_LSA_MAXAGE (l2))
3302 return -1;
3304 /* compare LS age with MaxAgeDiff. */
3305 if (LS_AGE (l1) - LS_AGE (l2) > OSPF_LSA_MAXAGE_DIFF)
3306 return -1;
3307 else if (LS_AGE (l2) - LS_AGE (l1) > OSPF_LSA_MAXAGE_DIFF)
3308 return 1;
3310 /* LSAs are identical. */
3311 return 0;
3314 /* If two LSAs are different, return 1, otherwise return 0. */
3316 ospf_lsa_different (struct ospf_lsa *l1, struct ospf_lsa *l2)
3318 char *p1, *p2;
3319 assert (l1);
3320 assert (l2);
3321 assert (l1->data);
3322 assert (l2->data);
3324 if (l1->data->options != l2->data->options)
3325 return 1;
3327 if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
3328 return 1;
3330 if (IS_LSA_MAXAGE (l2) && !IS_LSA_MAXAGE (l1))
3331 return 1;
3333 if (l1->data->length != l2->data->length)
3334 return 1;
3336 if (l1->data->length == 0)
3337 return 1;
3339 assert ( ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
3341 p1 = (char *) l1->data;
3342 p2 = (char *) l2->data;
3344 if (memcmp (p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
3345 ntohs( l1->data->length ) - OSPF_LSA_HEADER_SIZE) != 0)
3346 return 1;
3348 return 0;
3351 #ifdef ORIGINAL_CODING
3352 void
3353 ospf_lsa_flush_self_originated (struct ospf_neighbor *nbr,
3354 struct ospf_lsa *self,
3355 struct ospf_lsa *new)
3357 u_int32_t seqnum;
3359 /* Adjust LS Sequence Number. */
3360 seqnum = ntohl (new->data->ls_seqnum) + 1;
3361 self->data->ls_seqnum = htonl (seqnum);
3363 /* Recalculate LSA checksum. */
3364 ospf_lsa_checksum (self->data);
3366 /* Reflooding LSA. */
3367 /* RFC2328 Section 13.3
3368 On non-broadcast networks, separate Link State Update
3369 packets must be sent, as unicasts, to each adjacent neighbor
3370 (i.e., those in state Exchange or greater). The destination
3371 IP addresses for these packets are the neighbors' IP
3372 addresses. */
3373 if (nbr->oi->type == OSPF_IFTYPE_NBMA)
3375 struct route_node *rn;
3376 struct ospf_neighbor *onbr;
3378 for (rn = route_top (nbr->oi->nbrs); rn; rn = route_next (rn))
3379 if ((onbr = rn->info) != NULL)
3380 if (onbr != nbr->oi->nbr_self && onbr->status >= NSM_Exchange)
3381 ospf_ls_upd_send_lsa (onbr, self, OSPF_SEND_PACKET_DIRECT);
3383 else
3384 ospf_ls_upd_send_lsa (nbr, self, OSPF_SEND_PACKET_INDIRECT);
3386 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3387 zlog_debug ("LSA[Type%d:%s]: Flush self-originated LSA",
3388 self->data->type, inet_ntoa (self->data->id));
3390 #else /* ORIGINAL_CODING */
3391 static int
3392 ospf_lsa_flush_schedule (struct ospf *ospf, struct ospf_lsa *lsa)
3394 if (lsa == NULL || !IS_LSA_SELF (lsa))
3395 return 0;
3397 if (IS_DEBUG_OSPF_EVENT)
3398 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3400 /* Force given lsa's age to MaxAge. */
3401 lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
3403 switch (lsa->data->type)
3405 #ifdef HAVE_OPAQUE_LSA
3406 case OSPF_OPAQUE_LINK_LSA:
3407 case OSPF_OPAQUE_AREA_LSA:
3408 case OSPF_OPAQUE_AS_LSA:
3409 ospf_opaque_lsa_refresh (lsa);
3410 break;
3411 #endif /* HAVE_OPAQUE_LSA */
3412 default:
3413 ospf_lsa_maxage (ospf, lsa);
3414 break;
3417 return 0;
3420 void
3421 ospf_flush_self_originated_lsas_now (struct ospf *ospf)
3423 struct listnode *node, *nnode;
3424 struct listnode *node2, *nnode2;
3425 struct ospf_area *area;
3426 struct ospf_interface *oi;
3427 struct ospf_lsa *lsa;
3428 struct route_node *rn;
3429 int need_to_flush_ase = 0;
3431 for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
3433 if ((lsa = area->router_lsa_self) != NULL)
3435 if (IS_DEBUG_OSPF_EVENT)
3436 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3438 ospf_lsa_flush_area (lsa, area);
3439 ospf_lsa_unlock (&area->router_lsa_self);
3440 area->router_lsa_self = NULL;
3441 OSPF_TIMER_OFF (area->t_router_lsa_self);
3444 for (ALL_LIST_ELEMENTS (area->oiflist, node2, nnode2, oi))
3446 if ((lsa = oi->network_lsa_self) != NULL
3447 && oi->state == ISM_DR
3448 && oi->full_nbrs > 0)
3450 if (IS_DEBUG_OSPF_EVENT)
3451 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
3453 ospf_lsa_flush_area (oi->network_lsa_self, area);
3454 ospf_lsa_unlock (&oi->network_lsa_self);
3455 oi->network_lsa_self = NULL;
3456 OSPF_TIMER_OFF (oi->t_network_lsa_self);
3459 if (oi->type != OSPF_IFTYPE_VIRTUALLINK
3460 && area->external_routing == OSPF_AREA_DEFAULT)
3461 need_to_flush_ase = 1;
3464 LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
3465 ospf_lsa_flush_schedule (ospf, lsa);
3466 LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
3467 ospf_lsa_flush_schedule (ospf, lsa);
3468 #ifdef HAVE_OPAQUE_LSA
3469 LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
3470 ospf_lsa_flush_schedule (ospf, lsa);
3471 LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
3472 ospf_lsa_flush_schedule (ospf, lsa);
3473 #endif /* HAVE_OPAQUE_LSA */
3476 if (need_to_flush_ase)
3478 LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
3479 ospf_lsa_flush_schedule (ospf, lsa);
3480 #ifdef HAVE_OPAQUE_LSA
3481 LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
3482 ospf_lsa_flush_schedule (ospf, lsa);
3483 #endif /* HAVE_OPAQUE_LSA */
3487 * Make sure that the MaxAge LSA remover is executed immediately,
3488 * without conflicting to other threads.
3490 if (ospf->t_maxage != NULL)
3492 OSPF_TIMER_OFF (ospf->t_maxage);
3493 thread_execute (master, ospf_maxage_lsa_remover, ospf, 0);
3496 return;
3498 #endif /* ORIGINAL_CODING */
3500 /* If there is self-originated LSA, then return 1, otherwise return 0. */
3501 /* An interface-independent version of ospf_lsa_is_self_originated */
3502 int
3503 ospf_lsa_is_self_originated (struct ospf *ospf, struct ospf_lsa *lsa)
3505 struct listnode *node;
3506 struct ospf_interface *oi;
3508 /* This LSA is already checked. */
3509 if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED))
3510 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3512 /* Make sure LSA is self-checked. */
3513 SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
3515 /* AdvRouter and Router ID is the same. */
3516 if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf->router_id))
3517 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3519 /* LSA is router-LSA. */
3520 else if (lsa->data->type == OSPF_ROUTER_LSA &&
3521 IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
3522 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3524 /* LSA is network-LSA. Compare Link ID with all interfaces. */
3525 else if (lsa->data->type == OSPF_NETWORK_LSA)
3526 for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
3528 /* Ignore virtual link. */
3529 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
3530 if (oi->address->family == AF_INET)
3531 if (IPV4_ADDR_SAME (&lsa->data->id, &oi->address->u.prefix4))
3533 /* to make it easier later */
3534 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
3535 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3539 return CHECK_FLAG (lsa->flags, OSPF_LSA_SELF);
3542 /* Get unique Link State ID. */
3543 struct in_addr
3544 ospf_lsa_unique_id (struct ospf *ospf,
3545 struct ospf_lsdb *lsdb, u_char type, struct prefix_ipv4 *p)
3547 struct ospf_lsa *lsa;
3548 struct in_addr mask, id;
3550 id = p->prefix;
3552 /* Check existence of LSA instance. */
3553 lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, ospf->router_id);
3554 if (lsa)
3556 struct as_external_lsa *al = (struct as_external_lsa *) lsa->data;
3557 if (ip_masklen (al->mask) == p->prefixlen)
3559 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3560 zlog_debug ("ospf_lsa_unique_id(): "
3561 "Can't get Link State ID for %s/%d",
3562 inet_ntoa (p->prefix), p->prefixlen);
3563 /* id.s_addr = 0; */
3564 id.s_addr = 0xffffffff;
3565 return id;
3567 /* Masklen differs, then apply wildcard mask to Link State ID. */
3568 else
3570 masklen2ip (p->prefixlen, &mask);
3572 id.s_addr = p->prefix.s_addr | (~mask.s_addr);
3573 lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, type,
3574 id, ospf->router_id);
3575 if (lsa)
3577 if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
3578 zlog_debug ("ospf_lsa_unique_id(): "
3579 "Can't get Link State ID for %s/%d",
3580 inet_ntoa (p->prefix), p->prefixlen);
3581 /* id.s_addr = 0; */
3582 id.s_addr = 0xffffffff;
3583 return id;
3588 return id;
3592 #define LSA_ACTION_FLOOD_AREA 1
3593 #define LSA_ACTION_FLUSH_AREA 2
3595 struct lsa_action
3597 u_char action;
3598 struct ospf_area *area;
3599 struct ospf_lsa *lsa;
3602 static int
3603 ospf_lsa_action (struct thread *t)
3605 struct lsa_action *data;
3607 data = THREAD_ARG (t);
3609 if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
3610 zlog_debug ("LSA[Action]: Performing scheduled LSA action: %d",
3611 data->action);
3613 switch (data->action)
3615 case LSA_ACTION_FLOOD_AREA:
3616 ospf_flood_through_area (data->area, NULL, data->lsa);
3617 break;
3618 case LSA_ACTION_FLUSH_AREA:
3619 ospf_lsa_flush_area (data->lsa, data->area);
3620 break;
3623 ospf_lsa_unlock (&data->lsa); /* Message */
3624 XFREE (MTYPE_OSPF_MESSAGE, data);
3625 return 0;
3628 void
3629 ospf_schedule_lsa_flood_area (struct ospf_area *area, struct ospf_lsa *lsa)
3631 struct lsa_action *data;
3633 data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3634 data->action = LSA_ACTION_FLOOD_AREA;
3635 data->area = area;
3636 data->lsa = ospf_lsa_lock (lsa); /* Message / Flood area */
3638 thread_add_event (master, ospf_lsa_action, data, 0);
3641 void
3642 ospf_schedule_lsa_flush_area (struct ospf_area *area, struct ospf_lsa *lsa)
3644 struct lsa_action *data;
3646 data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
3647 data->action = LSA_ACTION_FLUSH_AREA;
3648 data->area = area;
3649 data->lsa = ospf_lsa_lock (lsa); /* Message / Flush area */
3651 thread_add_event (master, ospf_lsa_action, data, 0);
3655 /* LSA Refreshment functions. */
3656 static void
3657 ospf_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
3659 struct external_info *ei;
3660 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3662 switch (lsa->data->type)
3664 /* Router and Network LSAs are processed differently. */
3665 case OSPF_ROUTER_LSA:
3666 case OSPF_NETWORK_LSA:
3667 break;
3668 case OSPF_SUMMARY_LSA:
3669 ospf_summary_lsa_refresh (ospf, lsa);
3670 break;
3671 case OSPF_ASBR_SUMMARY_LSA:
3672 ospf_summary_asbr_lsa_refresh (ospf, lsa);
3673 break;
3674 case OSPF_AS_EXTERNAL_LSA:
3675 /* Translated from NSSA Type-5s are refreshed when
3676 * from refresh of Type-7 - do not refresh these directly.
3678 if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
3679 break;
3680 ei = ospf_external_info_check (lsa);
3681 if (ei)
3682 ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
3683 else
3684 ospf_lsa_flush_as (ospf, lsa);
3685 break;
3686 #ifdef HAVE_OPAQUE_LSA
3687 case OSPF_OPAQUE_LINK_LSA:
3688 case OSPF_OPAQUE_AREA_LSA:
3689 case OSPF_OPAQUE_AS_LSA:
3690 ospf_opaque_lsa_refresh (lsa);
3691 break;
3692 #endif /* HAVE_OPAQUE_LSA */
3693 default:
3694 break;
3698 void
3699 ospf_refresher_register_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3701 u_int16_t index, current_index;
3703 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3705 if (lsa->refresh_list < 0)
3707 int delay;
3709 if (LS_AGE (lsa) == 0 &&
3710 ntohl (lsa->data->ls_seqnum) == OSPF_INITIAL_SEQUENCE_NUMBER)
3711 /* Randomize first update by OSPF_LS_REFRESH_SHIFT factor */
3712 delay = OSPF_LS_REFRESH_SHIFT + (random () % OSPF_LS_REFRESH_TIME);
3713 else
3714 /* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */
3715 delay = OSPF_LS_REFRESH_TIME - LS_AGE (lsa) - OSPF_LS_REFRESH_JITTER
3716 + (random () % (2*OSPF_LS_REFRESH_JITTER));
3718 if (delay < 0)
3719 delay = 0;
3721 current_index = ospf->lsa_refresh_queue.index +
3722 (quagga_time (NULL) - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
3724 index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY)
3725 % (OSPF_LSA_REFRESHER_SLOTS);
3727 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3728 zlog_debug ("LSA[Refresh]: lsa %s with age %d added to index %d",
3729 inet_ntoa (lsa->data->id), LS_AGE (lsa), index);
3730 if (!ospf->lsa_refresh_queue.qs[index])
3731 ospf->lsa_refresh_queue.qs[index] = list_new ();
3732 listnode_add (ospf->lsa_refresh_queue.qs[index],
3733 ospf_lsa_lock (lsa)); /* lsa_refresh_queue */
3734 lsa->refresh_list = index;
3735 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3736 zlog_debug ("LSA[Refresh:%s]: ospf_refresher_register_lsa(): "
3737 "setting refresh_list on lsa %p (slod %d)",
3738 inet_ntoa (lsa->data->id), lsa, index);
3742 void
3743 ospf_refresher_unregister_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
3745 assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
3746 if (lsa->refresh_list >= 0)
3748 struct list *refresh_list = ospf->lsa_refresh_queue.qs[lsa->refresh_list];
3749 listnode_delete (refresh_list, lsa);
3750 if (!listcount (refresh_list))
3752 list_free (refresh_list);
3753 ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
3755 ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
3756 lsa->refresh_list = -1;
3761 ospf_lsa_refresh_walker (struct thread *t)
3763 struct list *refresh_list;
3764 struct listnode *node, *nnode;
3765 struct ospf *ospf = THREAD_ARG (t);
3766 struct ospf_lsa *lsa;
3767 int i;
3768 struct list *lsa_to_refresh = list_new ();
3770 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3771 zlog_debug ("LSA[Refresh]:ospf_lsa_refresh_walker(): start");
3774 i = ospf->lsa_refresh_queue.index;
3776 /* Note: if clock has jumped backwards, then time change could be negative,
3777 so we are careful to cast the expression to unsigned before taking
3778 modulus. */
3779 ospf->lsa_refresh_queue.index =
3780 ((unsigned long)(ospf->lsa_refresh_queue.index +
3781 (quagga_time (NULL) - ospf->lsa_refresher_started) /
3782 OSPF_LSA_REFRESHER_GRANULARITY)) % OSPF_LSA_REFRESHER_SLOTS;
3784 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3785 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
3786 ospf->lsa_refresh_queue.index);
3788 for (;i != ospf->lsa_refresh_queue.index;
3789 i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS)
3791 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3792 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): "
3793 "refresh index %d", i);
3795 refresh_list = ospf->lsa_refresh_queue.qs [i];
3797 ospf->lsa_refresh_queue.qs [i] = NULL;
3799 if (refresh_list)
3801 for (ALL_LIST_ELEMENTS (refresh_list, node, nnode, lsa))
3803 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3804 zlog_debug ("LSA[Refresh:%s]: ospf_lsa_refresh_walker(): "
3805 "refresh lsa %p (slot %d)",
3806 inet_ntoa (lsa->data->id), lsa, i);
3808 list_delete_node (refresh_list, node);
3809 ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
3810 lsa->refresh_list = -1;
3811 listnode_add (lsa_to_refresh, lsa);
3813 list_free (refresh_list);
3817 ospf->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
3818 ospf, ospf->lsa_refresh_interval);
3819 ospf->lsa_refresher_started = quagga_time (NULL);
3821 for (ALL_LIST_ELEMENTS (lsa_to_refresh, node, nnode, lsa))
3822 ospf_lsa_refresh (ospf, lsa);
3824 list_delete (lsa_to_refresh);
3826 if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
3827 zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
3829 return 0;