File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / quagga / ospfd / ospf_lsa.c
Revision 1.1.1.3.2.1: download - view: text, annotated - select for diffs - revision graph
Mon Jul 22 00:08:32 2013 UTC (10 years, 11 months ago) by misho
Branches: v0_99_22p0
Diff to: branchpoint 1.1.1.3: preferred, unified
patch0

    1: /*
    2:  * OSPF Link State Advertisement
    3:  * Copyright (C) 1999, 2000 Toshiaki Takada
    4:  *
    5:  * This file is part of GNU Zebra.
    6:  *
    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.
   11:  *
   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.
   16:  *
   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.
   21:  */
   22: 
   23: #include <zebra.h>
   24: 
   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"
   36: 
   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"
   52: 
   53: 
   54: u_int32_t
   55: get_metric (u_char *metric)
   56: {
   57:   u_int32_t m;
   58:   m = metric[0];
   59:   m = (m << 8) + metric[1];
   60:   m = (m << 8) + metric[2];
   61:   return m;
   62: }
   63: 
   64: 
   65: struct timeval
   66: tv_adjust (struct timeval a)
   67: {
   68:   while (a.tv_usec >= 1000000)
   69:     {
   70:       a.tv_usec -= 1000000;
   71:       a.tv_sec++;
   72:     }
   73: 
   74:   while (a.tv_usec < 0)
   75:     {
   76:       a.tv_usec += 1000000;
   77:       a.tv_sec--;
   78:     }
   79: 
   80:   return a;
   81: }
   82: 
   83: int
   84: tv_ceil (struct timeval a)
   85: {
   86:   a = tv_adjust (a);
   87: 
   88:   return (a.tv_usec ? a.tv_sec + 1 : a.tv_sec);
   89: }
   90: 
   91: int
   92: tv_floor (struct timeval a)
   93: {
   94:   a = tv_adjust (a);
   95: 
   96:   return a.tv_sec;
   97: }
   98: 
   99: struct timeval
  100: int2tv (int a)
  101: {
  102:   struct timeval ret;
  103: 
  104:   ret.tv_sec = a;
  105:   ret.tv_usec = 0;
  106: 
  107:   return ret;
  108: }
  109: 
  110: struct timeval
  111: tv_add (struct timeval a, struct timeval b)
  112: {
  113:   struct timeval ret;
  114: 
  115:   ret.tv_sec = a.tv_sec + b.tv_sec;
  116:   ret.tv_usec = a.tv_usec + b.tv_usec;
  117: 
  118:   return tv_adjust (ret);
  119: }
  120: 
  121: struct timeval
  122: tv_sub (struct timeval a, struct timeval b)
  123: {
  124:   struct timeval ret;
  125: 
  126:   ret.tv_sec = a.tv_sec - b.tv_sec;
  127:   ret.tv_usec = a.tv_usec - b.tv_usec;
  128: 
  129:   return tv_adjust (ret);
  130: }
  131: 
  132: int
  133: tv_cmp (struct timeval a, struct timeval b)
  134: {
  135:   return (a.tv_sec == b.tv_sec ?
  136: 	  a.tv_usec - b.tv_usec : a.tv_sec - b.tv_sec);
  137: }
  138: 
  139: int
  140: ospf_lsa_refresh_delay (struct ospf_lsa *lsa)
  141: {
  142:   struct timeval delta, now;
  143:   int delay = 0;
  144: 
  145:   quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
  146:   delta = tv_sub (now, lsa->tv_orig);
  147: 
  148:   if (tv_cmp (delta, int2tv (OSPF_MIN_LS_INTERVAL)) < 0)
  149:     {
  150:       delay = tv_ceil (tv_sub (int2tv (OSPF_MIN_LS_INTERVAL), delta));
  151: 
  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);
  155: 
  156:       assert (delay > 0);
  157:     }
  158: 
  159:   return delay;
  160: }
  161: 
  162: 
  163: int
  164: get_age (struct ospf_lsa *lsa)
  165: {
  166:   int age;
  167: 
  168:   age = ntohs (lsa->data->ls_age) 
  169:         + tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
  170: 
  171:   return age;
  172: }
  173: 
  174: 
  175: /* Fletcher Checksum -- Refer to RFC1008. */
  176: 
  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)
  181: {
  182:   u_char *buffer = (u_char *) &lsa->options;
  183:   int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
  184: 
  185:   /* Skip the AGE field */
  186:   u_int16_t len = ntohs(lsa->length) - options_offset; 
  187: 
  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;
  191: 
  192:   return fletcher_checksum(buffer, len, checksum_offset);
  193: }
  194: 
  195: int
  196: ospf_lsa_checksum_valid (struct lsa_header *lsa)
  197: {
  198:   u_char *buffer = (u_char *) &lsa->options;
  199:   int options_offset = buffer - (u_char *) &lsa->ls_age; /* should be 2 */
  200: 
  201:   /* Skip the AGE field */
  202:   u_int16_t len = ntohs(lsa->length) - options_offset;
  203: 
  204:   return(fletcher_checksum(buffer, len, FLETCHER_CHECKSUM_VALIDATE) == 0);
  205: }
  206: 
  207: 
  208: 
  209: /* Create OSPF LSA. */
  210: struct ospf_lsa *
  211: ospf_lsa_new ()
  212: {
  213:   struct ospf_lsa *new;
  214: 
  215:   new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
  216: 
  217:   new->flags = 0;
  218:   new->lock = 1;
  219:   new->retransmit_counter = 0;
  220:   new->tv_recv = recent_relative_time ();
  221:   new->tv_orig = new->tv_recv;
  222:   new->refresh_list = -1;
  223:   
  224:   return new;
  225: }
  226: 
  227: /* Duplicate OSPF LSA. */
  228: struct ospf_lsa *
  229: ospf_lsa_dup (struct ospf_lsa *lsa)
  230: {
  231:   struct ospf_lsa *new;
  232: 
  233:   if (lsa == NULL)
  234:     return NULL;
  235: 
  236:   new = XCALLOC (MTYPE_OSPF_LSA, sizeof (struct ospf_lsa));
  237: 
  238:   memcpy (new, lsa, sizeof (struct ospf_lsa));
  239:   UNSET_FLAG (new->flags, OSPF_LSA_DISCARD);
  240:   new->lock = 1;
  241:   new->retransmit_counter = 0;
  242:   new->data = ospf_lsa_data_dup (lsa->data);
  243: 
  244:   /* kevinm: Clear the refresh_list, otherwise there are going
  245:      to be problems when we try to remove the LSA from the
  246:      queue (which it's not a member of.)
  247:      XXX: Should we add the LSA to the refresh_list queue? */
  248:   new->refresh_list = -1;
  249: 
  250:   if (IS_DEBUG_OSPF (lsa, LSA))
  251:     zlog_debug ("LSA: duplicated %p (new: %p)", lsa, new);
  252: 
  253:   return new;
  254: }
  255: 
  256: /* Free OSPF LSA. */
  257: void
  258: ospf_lsa_free (struct ospf_lsa *lsa)
  259: {
  260:   assert (lsa->lock == 0);
  261:   
  262:   if (IS_DEBUG_OSPF (lsa, LSA))
  263:     zlog_debug ("LSA: freed %p", lsa);
  264: 
  265:   /* Delete LSA data. */
  266:   if (lsa->data != NULL)
  267:     ospf_lsa_data_free (lsa->data);
  268: 
  269:   assert (lsa->refresh_list < 0);
  270: 
  271:   memset (lsa, 0, sizeof (struct ospf_lsa)); 
  272:   XFREE (MTYPE_OSPF_LSA, lsa);
  273: }
  274: 
  275: /* Lock LSA. */
  276: struct ospf_lsa *
  277: ospf_lsa_lock (struct ospf_lsa *lsa)
  278: {
  279:   lsa->lock++;
  280:   return lsa;
  281: }
  282: 
  283: /* Unlock LSA. */
  284: void
  285: ospf_lsa_unlock (struct ospf_lsa **lsa)
  286: {
  287:   /* This is sanity check. */
  288:   if (!lsa || !*lsa)
  289:     return;
  290:   
  291:   (*lsa)->lock--;
  292: 
  293:   assert ((*lsa)->lock >= 0);
  294: 
  295:   if ((*lsa)->lock == 0)
  296:     {
  297:       assert (CHECK_FLAG ((*lsa)->flags, OSPF_LSA_DISCARD));
  298:       ospf_lsa_free (*lsa);
  299:       *lsa = NULL;
  300:     }
  301: }
  302: 
  303: /* Check discard flag. */
  304: void
  305: ospf_lsa_discard (struct ospf_lsa *lsa)
  306: {
  307:   if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
  308:     {
  309:       SET_FLAG (lsa->flags, OSPF_LSA_DISCARD);
  310:       ospf_lsa_unlock (&lsa);
  311:     }
  312: }
  313: 
  314: /* Create LSA data. */
  315: struct lsa_header *
  316: ospf_lsa_data_new (size_t size)
  317: {
  318:   return XCALLOC (MTYPE_OSPF_LSA_DATA, size);
  319: }
  320: 
  321: /* Duplicate LSA data. */
  322: struct lsa_header *
  323: ospf_lsa_data_dup (struct lsa_header *lsah)
  324: {
  325:   struct lsa_header *new;
  326: 
  327:   new = ospf_lsa_data_new (ntohs (lsah->length));
  328:   memcpy (new, lsah, ntohs (lsah->length));
  329: 
  330:   return new;
  331: }
  332: 
  333: /* Free LSA data. */
  334: void
  335: ospf_lsa_data_free (struct lsa_header *lsah)
  336: {
  337:   if (IS_DEBUG_OSPF (lsa, LSA))
  338:     zlog_debug ("LSA[Type%d:%s]: data freed %p",
  339: 	       lsah->type, inet_ntoa (lsah->id), lsah);
  340: 
  341:   XFREE (MTYPE_OSPF_LSA_DATA, lsah);
  342: }
  343: 
  344: 
  345: /* LSA general functions. */
  346: 
  347: const char *
  348: dump_lsa_key (struct ospf_lsa *lsa)
  349: {
  350:   static char buf[] = {
  351:     "Type255,id(255.255.255.255),ar(255.255.255.255)"
  352:   };
  353:   struct lsa_header *lsah;
  354: 
  355:   if (lsa != NULL && (lsah = lsa->data) != NULL)
  356:     {
  357:       char id[INET_ADDRSTRLEN], ar[INET_ADDRSTRLEN];
  358:       strcpy (id, inet_ntoa (lsah->id));
  359:       strcpy (ar, inet_ntoa (lsah->adv_router));
  360: 
  361:       sprintf (buf, "Type%d,id(%s),ar(%s)", lsah->type, id, ar);
  362:     }
  363:   else
  364:     strcpy (buf, "NULL");
  365: 
  366:   return buf;
  367: }
  368: 
  369: u_int32_t
  370: lsa_seqnum_increment (struct ospf_lsa *lsa)
  371: {
  372:   u_int32_t seqnum;
  373: 
  374:   seqnum = ntohl (lsa->data->ls_seqnum) + 1;
  375: 
  376:   return htonl (seqnum);
  377: }
  378: 
  379: void
  380: lsa_header_set (struct stream *s, u_char options,
  381: 		u_char type, struct in_addr id, struct in_addr router_id)
  382: {
  383:   struct lsa_header *lsah;
  384: 
  385:   lsah = (struct lsa_header *) STREAM_DATA (s);
  386: 
  387:   lsah->ls_age = htons (OSPF_LSA_INITIAL_AGE);
  388:   lsah->options = options;
  389:   lsah->type = type;
  390:   lsah->id = id;
  391:   lsah->adv_router = router_id;
  392:   lsah->ls_seqnum = htonl (OSPF_INITIAL_SEQUENCE_NUMBER);
  393: 
  394:   stream_forward_endp (s, OSPF_LSA_HEADER_SIZE);
  395: }
  396: 
  397: 
  398: /* router-LSA related functions. */
  399: /* Get router-LSA flags. */
  400: static u_char
  401: router_lsa_flags (struct ospf_area *area)
  402: {
  403:   u_char flags;
  404: 
  405:   flags = area->ospf->flags;
  406: 
  407:   /* Set virtual link flag. */
  408:   if (ospf_full_virtual_nbrs (area))
  409:     SET_FLAG (flags, ROUTER_LSA_VIRTUAL);
  410:   else
  411:     /* Just sanity check */
  412:     UNSET_FLAG (flags, ROUTER_LSA_VIRTUAL);
  413: 
  414:   /* Set Shortcut ABR behabiour flag. */
  415:   UNSET_FLAG (flags, ROUTER_LSA_SHORTCUT);
  416:   if (area->ospf->abr_type == OSPF_ABR_SHORTCUT)
  417:     if (!OSPF_IS_AREA_BACKBONE (area))
  418:       if ((area->shortcut_configured == OSPF_SHORTCUT_DEFAULT &&
  419: 	   area->ospf->backbone == NULL) ||
  420: 	  area->shortcut_configured == OSPF_SHORTCUT_ENABLE)
  421: 	SET_FLAG (flags, ROUTER_LSA_SHORTCUT);
  422: 
  423:   /* ASBR can't exit in stub area. */
  424:   if (area->external_routing == OSPF_AREA_STUB)
  425:     UNSET_FLAG (flags, ROUTER_LSA_EXTERNAL);
  426:   /* If ASBR set External flag */
  427:   else if (IS_OSPF_ASBR (area->ospf))
  428:     SET_FLAG (flags, ROUTER_LSA_EXTERNAL);
  429: 
  430:   /* Set ABR dependent flags */
  431:   if (IS_OSPF_ABR (area->ospf))
  432:     {
  433:       SET_FLAG (flags,  ROUTER_LSA_BORDER);
  434:       /* If Area is NSSA and we are both ABR and unconditional translator, 
  435:        * set Nt bit to inform other routers.
  436:        */
  437:       if ( (area->external_routing == OSPF_AREA_NSSA)
  438:            && (area->NSSATranslatorRole == OSPF_NSSA_ROLE_ALWAYS))
  439:         SET_FLAG (flags, ROUTER_LSA_NT);
  440:     }
  441:   return flags;
  442: }
  443: 
  444: /* Lookup neighbor other than myself.
  445:    And check neighbor count,
  446:    Point-to-Point link must have only 1 neighbor. */
  447: struct ospf_neighbor *
  448: ospf_nbr_lookup_ptop (struct ospf_interface *oi)
  449: {
  450:   struct ospf_neighbor *nbr = NULL;
  451:   struct route_node *rn;
  452: 
  453:   /* Search neighbor, there must be one of two nbrs. */
  454:   for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  455:     if ((nbr = rn->info))
  456:       if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  457: 	if (nbr->state == NSM_Full)
  458: 	  {
  459: 	    route_unlock_node (rn);
  460: 	    break;
  461: 	  }
  462: 
  463:   /* PtoP link must have only 1 neighbor. */
  464:   if (ospf_nbr_count (oi, 0) > 1)
  465:     zlog_warn ("Point-to-Point link has more than 1 neighobrs.");
  466: 
  467:   return nbr;
  468: }
  469: 
  470: /* Determine cost of link, taking RFC3137 stub-router support into
  471:  * consideration
  472:  */
  473: static u_int16_t
  474: ospf_link_cost (struct ospf_interface *oi)
  475: {
  476:   /* RFC3137 stub router support */
  477:   if (!CHECK_FLAG (oi->area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED))
  478:     return oi->output_cost;
  479:   else
  480:     return OSPF_OUTPUT_COST_INFINITE;
  481: }
  482: 
  483: /* Set a link information. */
  484: static char
  485: link_info_set (struct stream *s, struct in_addr id,
  486: 	       struct in_addr data, u_char type, u_char tos, u_int16_t cost)
  487: {
  488:   /* LSA stream is initially allocated to OSPF_MAX_LSA_SIZE, suits
  489:    * vast majority of cases. Some rare routers with lots of links need more.
  490:    * we try accomodate those here.
  491:    */
  492:   if (STREAM_WRITEABLE(s) < OSPF_ROUTER_LSA_LINK_SIZE)
  493:     {
  494:       size_t ret = OSPF_MAX_LSA_SIZE;
  495:       
  496:       /* Can we enlarge the stream still? */
  497:       if (STREAM_SIZE(s) == OSPF_MAX_LSA_SIZE)
  498:         {
  499:           /* we futz the size here for simplicity, really we need to account
  500:            * for just:
  501:            * IP Header - (sizeof (struct ip))
  502:            * OSPF Header - OSPF_HEADER_SIZE
  503:            * LSA Header - OSPF_LSA_HEADER_SIZE
  504:            * MD5 auth data, if MD5 is configured - OSPF_AUTH_MD5_SIZE.
  505:            *
  506:            * Simpler just to subtract OSPF_MAX_LSA_SIZE though.
  507:            */
  508:           ret = stream_resize (s, OSPF_MAX_PACKET_SIZE - OSPF_MAX_LSA_SIZE);
  509:         }
  510:       
  511:       if (ret == OSPF_MAX_LSA_SIZE)
  512:         {
  513:           zlog_warn ("%s: Out of space in LSA stream, left %zd, size %zd",
  514:                      __func__, STREAM_REMAIN (s), STREAM_SIZE (s));
  515:           return 0;
  516:         }
  517:     }
  518:   
  519:   /* TOS based routing is not supported. */
  520:   stream_put_ipv4 (s, id.s_addr);		/* Link ID. */
  521:   stream_put_ipv4 (s, data.s_addr);		/* Link Data. */
  522:   stream_putc (s, type);			/* Link Type. */
  523:   stream_putc (s, tos);				/* TOS = 0. */
  524:   stream_putw (s, cost);			/* Link Cost. */
  525:   
  526:   return 1;
  527: }
  528: 
  529: /* Describe Point-to-Point link (Section 12.4.1.1). */
  530: static int
  531: lsa_link_ptop_set (struct stream *s, struct ospf_interface *oi)
  532: {
  533:   int links = 0;
  534:   struct ospf_neighbor *nbr;
  535:   struct in_addr id, mask;
  536:   u_int16_t cost = ospf_link_cost (oi);
  537: 
  538:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  539:     zlog_debug ("LSA[Type1]: Set link Point-to-Point");
  540: 
  541:   if ((nbr = ospf_nbr_lookup_ptop (oi)))
  542:     if (nbr->state == NSM_Full)
  543:       {
  544: 	/* For unnumbered point-to-point networks, the Link Data field
  545: 	   should specify the interface's MIB-II ifIndex value. */
  546: 	links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
  547: 		                LSA_LINK_TYPE_POINTOPOINT, 0, cost);
  548:       }
  549: 
  550:   /* Regardless of the state of the neighboring router, we must
  551:      add a Type 3 link (stub network).
  552:      N.B. Options 1 & 2 share basically the same logic. */
  553:   masklen2ip (oi->address->prefixlen, &mask);
  554:   id.s_addr = CONNECTED_PREFIX(oi->connected)->u.prefix4.s_addr & mask.s_addr;
  555:   links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
  556: 			  oi->output_cost);
  557:   return links;
  558: }
  559: 
  560: /* Describe Broadcast Link. */
  561: static int
  562: lsa_link_broadcast_set (struct stream *s, struct ospf_interface *oi)
  563: {
  564:   struct ospf_neighbor *dr;
  565:   struct in_addr id, mask;
  566:   u_int16_t cost = ospf_link_cost (oi);
  567:   
  568:   /* Describe Type 3 Link. */
  569:   if (oi->state == ISM_Waiting)
  570:     {
  571:       masklen2ip (oi->address->prefixlen, &mask);
  572:       id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
  573:       return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
  574:                             oi->output_cost);
  575:     }
  576: 
  577:   dr = ospf_nbr_lookup_by_addr (oi->nbrs, &DR (oi));
  578:   /* Describe Type 2 link. */
  579:   if (dr && (dr->state == NSM_Full ||
  580: 	     IPV4_ADDR_SAME (&oi->address->u.prefix4, &DR (oi))) &&
  581:       ospf_nbr_count (oi, NSM_Full) > 0)
  582:     {
  583:       return link_info_set (s, DR (oi), oi->address->u.prefix4,
  584:                             LSA_LINK_TYPE_TRANSIT, 0, cost);
  585:     }
  586:   /* Describe type 3 link. */
  587:   else
  588:     {
  589:       masklen2ip (oi->address->prefixlen, &mask);
  590:       id.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
  591:       return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0,
  592:                             oi->output_cost);
  593:     }
  594: }
  595: 
  596: static int
  597: lsa_link_loopback_set (struct stream *s, struct ospf_interface *oi)
  598: {
  599:   struct in_addr id, mask;
  600:   
  601:   /* Describe Type 3 Link. */
  602:   if (oi->state != ISM_Loopback)
  603:     return 0;
  604: 
  605:   mask.s_addr = 0xffffffff;
  606:   id.s_addr = oi->address->u.prefix4.s_addr;
  607:   return link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, oi->output_cost);
  608: }
  609: 
  610: /* Describe Virtual Link. */
  611: static int
  612: lsa_link_virtuallink_set (struct stream *s, struct ospf_interface *oi)
  613: {
  614:   struct ospf_neighbor *nbr;
  615:   u_int16_t cost = ospf_link_cost (oi);
  616: 
  617:   if (oi->state == ISM_PointToPoint)
  618:     if ((nbr = ospf_nbr_lookup_ptop (oi)))
  619:       if (nbr->state == NSM_Full)
  620: 	{
  621: 	  return link_info_set (s, nbr->router_id, oi->address->u.prefix4,
  622: 			        LSA_LINK_TYPE_VIRTUALLINK, 0, cost);
  623: 	}
  624: 
  625:   return 0;
  626: }
  627: 
  628: #define lsa_link_nbma_set(S,O)  lsa_link_broadcast_set (S, O)
  629: 
  630: /* this function add for support point-to-multipoint ,see rfc2328 
  631: 12.4.1.4.*/
  632: /* from "edward rrr" <edward_rrr@hotmail.com>
  633:    http://marc.theaimsgroup.com/?l=zebra&m=100739222210507&w=2 */
  634: static int
  635: lsa_link_ptomp_set (struct stream *s, struct ospf_interface *oi)
  636: {
  637:   int links = 0;
  638:   struct route_node *rn;
  639:   struct ospf_neighbor *nbr = NULL;
  640:   struct in_addr id, mask;
  641:   u_int16_t cost = ospf_link_cost (oi);
  642: 
  643:   mask.s_addr = 0xffffffff;
  644:   id.s_addr = oi->address->u.prefix4.s_addr;
  645:   links += link_info_set (s, id, mask, LSA_LINK_TYPE_STUB, 0, 0);
  646: 
  647:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  648:     zlog_debug ("PointToMultipoint: running ptomultip_set");
  649: 
  650:   /* Search neighbor, */
  651:   for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  652:     if ((nbr = rn->info) != NULL)
  653:       /* Ignore myself. */
  654:       if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  655: 	if (nbr->state == NSM_Full)
  656: 
  657: 	  {
  658: 	    links += link_info_set (s, nbr->router_id, oi->address->u.prefix4,
  659: 			            LSA_LINK_TYPE_POINTOPOINT, 0, cost);
  660:             if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  661:  	      zlog_debug ("PointToMultipoint: set link to %s",
  662: 		         inet_ntoa(oi->address->u.prefix4));
  663: 	  }
  664:   
  665:   return links;
  666: }
  667: 
  668: /* Set router-LSA link information. */
  669: static int
  670: router_lsa_link_set (struct stream *s, struct ospf_area *area)
  671: {
  672:   struct listnode *node;
  673:   struct ospf_interface *oi;
  674:   int links = 0;
  675: 
  676:   for (ALL_LIST_ELEMENTS_RO (area->oiflist, node, oi))
  677:     {
  678:       struct interface *ifp = oi->ifp;
  679: 
  680:       /* Check interface is up, OSPF is enable. */
  681:       if (if_is_operative (ifp))
  682: 	{
  683: 	  if (oi->state != ISM_Down)
  684: 	    {
  685: 	      oi->lsa_pos_beg = links;
  686: 	      /* Describe each link. */
  687: 	      switch (oi->type)
  688: 		{
  689: 		case OSPF_IFTYPE_POINTOPOINT:
  690: 		  links += lsa_link_ptop_set (s, oi);
  691: 		  break;
  692: 		case OSPF_IFTYPE_BROADCAST:
  693: 		  links += lsa_link_broadcast_set (s, oi);
  694: 		  break;
  695: 		case OSPF_IFTYPE_NBMA:
  696: 		  links += lsa_link_nbma_set (s, oi);
  697: 		  break;
  698: 		case OSPF_IFTYPE_POINTOMULTIPOINT:
  699: 		  links += lsa_link_ptomp_set (s, oi);
  700: 		  break;
  701: 		case OSPF_IFTYPE_VIRTUALLINK:
  702: 		  links += lsa_link_virtuallink_set (s, oi);
  703: 		  break;
  704: 		case OSPF_IFTYPE_LOOPBACK:
  705: 		  links += lsa_link_loopback_set (s, oi); 
  706: 		}
  707: 	      oi->lsa_pos_end = links;
  708: 	    }
  709: 	}
  710:     }
  711: 
  712:   return links;
  713: }
  714: 
  715: /* Set router-LSA body. */
  716: static void
  717: ospf_router_lsa_body_set (struct stream *s, struct ospf_area *area)
  718: {
  719:   unsigned long putp;
  720:   u_int16_t cnt;
  721: 
  722:   /* Set flags. */
  723:   stream_putc (s, router_lsa_flags (area));
  724: 
  725:   /* Set Zero fields. */
  726:   stream_putc (s, 0);
  727: 
  728:   /* Keep pointer to # links. */
  729:   putp = stream_get_endp(s);
  730: 
  731:   /* Forward word */
  732:   stream_putw(s, 0);
  733: 
  734:   /* Set all link information. */
  735:   cnt = router_lsa_link_set (s, area);
  736: 
  737:   /* Set # of links here. */
  738:   stream_putw_at (s, putp, cnt);
  739: }
  740: 
  741: static int
  742: ospf_stub_router_timer (struct thread *t)
  743: {
  744:   struct ospf_area *area = THREAD_ARG (t);
  745:   
  746:   area->t_stub_router = NULL;
  747:   
  748:   SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
  749:   
  750:   /* clear stub route state and generate router-lsa refresh, don't
  751:    * clobber an administratively set stub-router state though.
  752:    */
  753:   if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
  754:     return 0;
  755:   
  756:   UNSET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
  757:   
  758:   ospf_router_lsa_update_area (area);
  759:   
  760:   return 0;
  761: }
  762: 
  763: static void
  764: ospf_stub_router_check (struct ospf_area *area)
  765: {
  766:   /* area must either be administratively configured to be stub
  767:    * or startup-time stub-router must be configured and we must in a pre-stub
  768:    * state.
  769:    */
  770:   if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_ADMIN_STUB_ROUTED))
  771:     {
  772:       SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
  773:       return;
  774:     }
  775:   
  776:   /* not admin-stubbed, check whether startup stubbing is configured and
  777:    * whether it's not been done yet
  778:    */
  779:   if (CHECK_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED))
  780:     return;
  781:   
  782:   if (area->ospf->stub_router_startup_time == OSPF_STUB_ROUTER_UNCONFIGURED)
  783:     {
  784:       /* stub-router is hence done forever for this area, even if someone
  785:        * tries configure it (take effect next restart).
  786:        */
  787:       SET_FLAG (area->stub_router_state, OSPF_AREA_WAS_START_STUB_ROUTED);
  788:       return;
  789:     }
  790:   
  791:   /* startup stub-router configured and not yet done */
  792:   SET_FLAG (area->stub_router_state, OSPF_AREA_IS_STUB_ROUTED);
  793:   
  794:   OSPF_AREA_TIMER_ON (area->t_stub_router, ospf_stub_router_timer,
  795:                       area->ospf->stub_router_startup_time);
  796: }
  797:  
  798: /* Create new router-LSA. */
  799: static struct ospf_lsa *
  800: ospf_router_lsa_new (struct ospf_area *area)
  801: {
  802:   struct ospf *ospf = area->ospf;
  803:   struct stream *s;
  804:   struct lsa_header *lsah;
  805:   struct ospf_lsa *new;
  806:   int length;
  807: 
  808:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  809:     zlog_debug ("LSA[Type1]: Create router-LSA instance");
  810: 
  811:   /* check whether stub-router is desired, and if this is the first 
  812:    * router LSA.
  813:    */
  814:   ospf_stub_router_check (area);
  815:   
  816:   /* Create a stream for LSA. */
  817:   s = stream_new (OSPF_MAX_LSA_SIZE);
  818:   /* Set LSA common header fields. */
  819:   lsa_header_set (s, LSA_OPTIONS_GET (area) | LSA_OPTIONS_NSSA_GET (area),
  820: 		  OSPF_ROUTER_LSA, ospf->router_id, ospf->router_id);
  821: 
  822:   /* Set router-LSA body fields. */
  823:   ospf_router_lsa_body_set (s, area);
  824: 
  825:   /* Set length. */
  826:   length = stream_get_endp (s);
  827:   lsah = (struct lsa_header *) STREAM_DATA (s);
  828:   lsah->length = htons (length);
  829: 
  830:   /* Now, create OSPF LSA instance. */
  831:   if ( (new = ospf_lsa_new ()) == NULL)
  832:     {
  833:       zlog_err ("%s: Unable to create new lsa", __func__);
  834:       return NULL;
  835:     }
  836:   
  837:   new->area = area;
  838:   SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
  839: 
  840:   /* Copy LSA data to store, discard stream. */
  841:   new->data = ospf_lsa_data_new (length);
  842:   memcpy (new->data, lsah, length);
  843:   stream_free (s);
  844: 
  845:   return new;
  846: }
  847: 
  848: /* Originate Router-LSA. */
  849: static struct ospf_lsa *
  850: ospf_router_lsa_originate (struct ospf_area *area)
  851: {
  852:   struct ospf_lsa *new;
  853:   
  854:   /* Create new router-LSA instance. */
  855:   if ( (new = ospf_router_lsa_new (area)) == NULL)
  856:     {
  857:       zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
  858:       return NULL;
  859:     }
  860: 
  861:   /* Sanity check. */
  862:   if (new->data->adv_router.s_addr == 0)
  863:     {
  864:       if (IS_DEBUG_OSPF_EVENT)
  865: 	zlog_debug ("LSA[Type1]: AdvRouter is 0, discard");
  866:       ospf_lsa_discard (new);
  867:       return NULL;
  868:     }
  869: 
  870:   /* Install LSA to LSDB. */
  871:   new = ospf_lsa_install (area->ospf, NULL, new);
  872: 
  873:   /* Update LSA origination count. */
  874:   area->ospf->lsa_originate_count++;
  875: 
  876:   /* Flooding new LSA through area. */
  877:   ospf_flood_through_area (area, NULL, new);
  878: 
  879:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  880:     {
  881:       zlog_debug ("LSA[Type%d:%s]: Originate router-LSA %p",
  882: 		 new->data->type, inet_ntoa (new->data->id), new);
  883:       ospf_lsa_header_dump (new->data);
  884:     }
  885: 
  886:   return new;
  887: }
  888: 
  889: /* Refresh router-LSA. */
  890: static struct ospf_lsa *
  891: ospf_router_lsa_refresh (struct ospf_lsa *lsa)
  892: {
  893:   struct ospf_area *area = lsa->area;
  894:   struct ospf_lsa *new;
  895: 
  896:   /* Sanity check. */
  897:   assert (lsa->data);
  898: 
  899:   /* Delete LSA from neighbor retransmit-list. */
  900:   ospf_ls_retransmit_delete_nbr_area (area, lsa);
  901: 
  902:   /* Unregister LSA from refresh-list */
  903:   ospf_refresher_unregister_lsa (area->ospf, lsa);
  904:   
  905:   /* Create new router-LSA instance. */
  906:   if ( (new = ospf_router_lsa_new (area)) == NULL)
  907:     {
  908:       zlog_err ("%s: ospf_router_lsa_new returned NULL", __func__);
  909:       return NULL;
  910:     }
  911:   
  912:   new->data->ls_seqnum = lsa_seqnum_increment (lsa);
  913: 
  914:   ospf_lsa_install (area->ospf, NULL, new);
  915: 
  916:   /* Flood LSA through area. */
  917:   ospf_flood_through_area (area, NULL, new);
  918: 
  919:   /* Debug logging. */
  920:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  921:     {
  922:       zlog_debug ("LSA[Type%d:%s]: router-LSA refresh",
  923: 		 new->data->type, inet_ntoa (new->data->id));
  924:       ospf_lsa_header_dump (new->data);
  925:     }
  926: 
  927:   return NULL;
  928: }
  929: 
  930: int
  931: ospf_router_lsa_update_area (struct ospf_area *area)
  932: {
  933:   if (IS_DEBUG_OSPF_EVENT)
  934:     zlog_debug ("[router-LSA]: (router-LSA area update)");
  935: 
  936:   /* Now refresh router-LSA. */
  937:   if (area->router_lsa_self)
  938:     ospf_lsa_refresh (area->ospf, area->router_lsa_self);
  939:   /* Newly originate router-LSA. */
  940:   else
  941:     ospf_router_lsa_originate (area);
  942: 
  943:   return 0;
  944: }
  945: 
  946: int
  947: ospf_router_lsa_update (struct ospf *ospf)
  948: {
  949:   struct listnode *node, *nnode;
  950:   struct ospf_area *area;
  951: 
  952:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  953:     zlog_debug ("Timer[router-LSA Update]: (timer expire)");
  954: 
  955:   for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
  956:     {
  957:       struct ospf_lsa *lsa = area->router_lsa_self;
  958:       struct router_lsa *rl;
  959:       const char *area_str;
  960: 
  961:       /* Keep Area ID string. */
  962:       area_str = AREA_NAME (area);
  963: 
  964:       /* If LSA not exist in this Area, originate new. */
  965:       if (lsa == NULL)
  966:         {
  967: 	  if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  968: 	    zlog_debug("LSA[Type1]: Create router-LSA for Area %s", area_str);
  969: 
  970: 	  ospf_router_lsa_originate (area);
  971:         }
  972:       /* If router-ID is changed, Link ID must change.
  973: 	 First flush old LSA, then originate new. */
  974:       else if (!IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
  975: 	{
  976: 	  if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
  977: 	    zlog_debug("LSA[Type%d:%s]: Refresh router-LSA for Area %s",
  978: 		      lsa->data->type, inet_ntoa (lsa->data->id), area_str);
  979:           ospf_refresher_unregister_lsa (ospf, lsa);
  980: 	  ospf_lsa_flush_area (lsa, area);
  981: 	  ospf_lsa_unlock (&area->router_lsa_self);
  982: 	  area->router_lsa_self = NULL;
  983: 
  984: 	  /* Refresh router-LSA, (not install) and flood through area. */
  985: 	  ospf_router_lsa_update_area (area);
  986: 	}
  987:       else
  988: 	{
  989: 	  rl = (struct router_lsa *) lsa->data;
  990: 	  /* Refresh router-LSA, (not install) and flood through area. */
  991: 	  if (rl->flags != ospf->flags)
  992: 	    ospf_router_lsa_update_area (area);
  993: 	}
  994:     }
  995: 
  996:   return 0;
  997: }
  998: 
  999: 
 1000: /* network-LSA related functions. */
 1001: /* Originate Network-LSA. */
 1002: static void
 1003: ospf_network_lsa_body_set (struct stream *s, struct ospf_interface *oi)
 1004: {
 1005:   struct in_addr mask;
 1006:   struct route_node *rn;
 1007:   struct ospf_neighbor *nbr;
 1008: 
 1009:   masklen2ip (oi->address->prefixlen, &mask);
 1010:   stream_put_ipv4 (s, mask.s_addr);
 1011: 
 1012:   /* The network-LSA lists those routers that are fully adjacent to
 1013:     the Designated Router; each fully adjacent router is identified by
 1014:     its OSPF Router ID.  The Designated Router includes itself in this
 1015:     list. RFC2328, Section 12.4.2 */
 1016: 
 1017:   for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
 1018:     if ((nbr = rn->info) != NULL)
 1019:       if (nbr->state == NSM_Full || nbr == oi->nbr_self)
 1020: 	stream_put_ipv4 (s, nbr->router_id.s_addr);
 1021: }
 1022: 
 1023: static struct ospf_lsa *
 1024: ospf_network_lsa_new (struct ospf_interface *oi)
 1025: {
 1026:   struct stream *s;
 1027:   struct ospf_lsa *new;
 1028:   struct lsa_header *lsah;
 1029:   struct ospf_if_params *oip;
 1030:   int length;
 1031: 
 1032:   /* If there are no neighbours on this network (the net is stub),
 1033:      the router does not originate network-LSA (see RFC 12.4.2) */
 1034:   if (oi->full_nbrs == 0)
 1035:     return NULL;
 1036:   
 1037:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1038:     zlog_debug ("LSA[Type2]: Create network-LSA instance");
 1039: 
 1040:   /* Create new stream for LSA. */
 1041:   s = stream_new (OSPF_MAX_LSA_SIZE);
 1042:   lsah = (struct lsa_header *) STREAM_DATA (s);
 1043: 
 1044:   lsa_header_set (s, (OPTIONS (oi) | LSA_OPTIONS_GET (oi->area)),
 1045: 		  OSPF_NETWORK_LSA, DR (oi), oi->ospf->router_id);
 1046: 
 1047:   /* Set network-LSA body fields. */
 1048:   ospf_network_lsa_body_set (s, oi);
 1049: 
 1050:   /* Set length. */
 1051:   length = stream_get_endp (s);
 1052:   lsah->length = htons (length);
 1053: 
 1054:   /* Create OSPF LSA instance. */
 1055:   if ( (new = ospf_lsa_new ()) == NULL)
 1056:     {
 1057:       zlog_err ("%s: ospf_lsa_new returned NULL", __func__);
 1058:       return NULL;
 1059:     }
 1060:   
 1061:   new->area = oi->area;
 1062:   SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
 1063: 
 1064:   /* Copy LSA to store. */
 1065:   new->data = ospf_lsa_data_new (length);
 1066:   memcpy (new->data, lsah, length);
 1067:   stream_free (s);
 1068:   
 1069:   /* Remember prior network LSA sequence numbers, even if we stop
 1070:    * originating one for this oi, to try avoid re-originating LSAs with a
 1071:    * prior sequence number, and thus speed up adjency forming & convergence.
 1072:    */
 1073:   if ((oip = ospf_lookup_if_params (oi->ifp, oi->address->u.prefix4)))
 1074:     {
 1075:       new->data->ls_seqnum = oip->network_lsa_seqnum;
 1076:       new->data->ls_seqnum = lsa_seqnum_increment (new);
 1077:     }
 1078:   else
 1079:     {
 1080:       oip = ospf_get_if_params (oi->ifp, oi->address->u.prefix4);
 1081:       ospf_if_update_params (oi->ifp, oi->address->u.prefix4);
 1082:     }
 1083:   oip->network_lsa_seqnum = new->data->ls_seqnum;
 1084:   
 1085:   return new;
 1086: }
 1087: 
 1088: /* Originate network-LSA. */
 1089: void
 1090: ospf_network_lsa_update (struct ospf_interface *oi)
 1091: {
 1092:   struct ospf_lsa *new;
 1093:   
 1094:   if (oi->network_lsa_self != NULL)
 1095:     {
 1096:       ospf_lsa_refresh (oi->ospf, oi->network_lsa_self);
 1097:       return;
 1098:     }
 1099:   
 1100:   /* Create new network-LSA instance. */
 1101:   new = ospf_network_lsa_new (oi);
 1102:   if (new == NULL)
 1103:     return;
 1104: 
 1105:   /* Install LSA to LSDB. */
 1106:   new = ospf_lsa_install (oi->ospf, oi, new);
 1107: 
 1108:   /* Update LSA origination count. */
 1109:   oi->ospf->lsa_originate_count++;
 1110: 
 1111:   /* Flooding new LSA through area. */
 1112:   ospf_flood_through_area (oi->area, NULL, new);
 1113: 
 1114:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1115:     {
 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);
 1119:     }
 1120: 
 1121:   return;
 1122: }
 1123: 
 1124: static struct ospf_lsa *
 1125: ospf_network_lsa_refresh (struct ospf_lsa *lsa)
 1126: {
 1127:   struct ospf_area *area = lsa->area;
 1128:   struct ospf_lsa *new, *new2;
 1129:   struct ospf_if_params *oip;
 1130:   struct ospf_interface *oi;
 1131:   
 1132:   assert (lsa->data);
 1133:   
 1134:   /* Retrieve the oi for the network LSA */
 1135:   oi = ospf_if_lookup_by_local_addr (area->ospf, NULL, lsa->data->id);
 1136:   if (oi == NULL)
 1137:     {
 1138:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1139:         {
 1140:           zlog_debug ("LSA[Type%d:%s]: network-LSA refresh: "
 1141:                       "no oi found, ick, ignoring.",
 1142: 		      lsa->data->type, inet_ntoa (lsa->data->id));
 1143:           ospf_lsa_header_dump (lsa->data);
 1144:         }
 1145:       return NULL;
 1146:     }
 1147:   /* Delete LSA from neighbor retransmit-list. */
 1148:   ospf_ls_retransmit_delete_nbr_area (area, lsa);
 1149: 
 1150:   /* Unregister LSA from refresh-list */
 1151:   ospf_refresher_unregister_lsa (area->ospf, lsa);
 1152:   
 1153:   /* Create new network-LSA instance. */
 1154:   new = ospf_network_lsa_new (oi);
 1155:   if (new == NULL)
 1156:     return NULL;
 1157:   
 1158:   oip = ospf_lookup_if_params (oi->ifp, oi->address->u.prefix4);
 1159:   assert (oip != NULL);
 1160:   oip->network_lsa_seqnum = new->data->ls_seqnum = lsa_seqnum_increment (lsa);
 1161: 
 1162:   new2 = ospf_lsa_install (area->ospf, oi, new);
 1163:   
 1164:   assert (new2 == new);
 1165:   
 1166:   /* Flood LSA through aera. */
 1167:   ospf_flood_through_area (area, NULL, new);
 1168: 
 1169:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1170:     {
 1171:       zlog_debug ("LSA[Type%d:%s]: network-LSA refresh",
 1172: 		 new->data->type, inet_ntoa (new->data->id));
 1173:       ospf_lsa_header_dump (new->data);
 1174:     }
 1175: 
 1176:   return new;
 1177: }
 1178: 
 1179: static void
 1180: stream_put_ospf_metric (struct stream *s, u_int32_t metric_value)
 1181: {
 1182:   u_int32_t metric;
 1183:   char *mp;
 1184: 
 1185:   /* Put 0 metric. TOS metric is not supported. */
 1186:   metric = htonl (metric_value);
 1187:   mp = (char *) &metric;
 1188:   mp++;
 1189:   stream_put (s, mp, 3);
 1190: }
 1191: 
 1192: /* summary-LSA related functions. */
 1193: static void
 1194: ospf_summary_lsa_body_set (struct stream *s, struct prefix *p,
 1195: 			   u_int32_t metric)
 1196: {
 1197:   struct in_addr mask;
 1198: 
 1199:   masklen2ip (p->prefixlen, &mask);
 1200: 
 1201:   /* Put Network Mask. */
 1202:   stream_put_ipv4 (s, mask.s_addr);
 1203: 
 1204:   /* Set # TOS. */
 1205:   stream_putc (s, (u_char) 0);
 1206: 
 1207:   /* Set metric. */
 1208:   stream_put_ospf_metric (s, metric);
 1209: }
 1210: 
 1211: static struct ospf_lsa *
 1212: ospf_summary_lsa_new (struct ospf_area *area, struct prefix *p,
 1213: 		      u_int32_t metric, struct in_addr id)
 1214: {
 1215:   struct stream *s;
 1216:   struct ospf_lsa *new;
 1217:   struct lsa_header *lsah;
 1218:   int length;
 1219: 
 1220:   if (id.s_addr == 0xffffffff)
 1221:     {
 1222:       /* Maybe Link State ID not available. */
 1223:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1224:         zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
 1225:                     OSPF_SUMMARY_LSA);
 1226:       return NULL;
 1227:     }
 1228: 
 1229:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1230:     zlog_debug ("LSA[Type3]: Create summary-LSA instance");
 1231: 
 1232:   /* Create new stream for LSA. */
 1233:   s = stream_new (OSPF_MAX_LSA_SIZE);
 1234:   lsah = (struct lsa_header *) STREAM_DATA (s);
 1235: 
 1236:   lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_SUMMARY_LSA,
 1237: 		  id, area->ospf->router_id);
 1238: 
 1239:   /* Set summary-LSA body fields. */
 1240:   ospf_summary_lsa_body_set (s, p, metric);
 1241: 
 1242:   /* Set length. */
 1243:   length = stream_get_endp (s);
 1244:   lsah->length = htons (length);
 1245: 
 1246:   /* Create OSPF LSA instance. */
 1247:   new = ospf_lsa_new ();
 1248:   new->area = area;
 1249:   SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
 1250: 
 1251:   /* Copy LSA to store. */
 1252:   new->data = ospf_lsa_data_new (length);
 1253:   memcpy (new->data, lsah, length);
 1254:   stream_free (s);
 1255: 
 1256:   return new;
 1257: }
 1258: 
 1259: /* Originate Summary-LSA. */
 1260: struct ospf_lsa *
 1261: ospf_summary_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric, 
 1262: 			    struct ospf_area *area)
 1263: {
 1264:   struct ospf_lsa *new;
 1265:   struct in_addr id;
 1266:   
 1267:   id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_SUMMARY_LSA, p);
 1268: 
 1269:   if (id.s_addr == 0xffffffff)
 1270:     {
 1271:       /* Maybe Link State ID not available. */
 1272:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1273:         zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
 1274:                     OSPF_SUMMARY_LSA);
 1275:       return NULL;
 1276:     }
 1277:   
 1278:   /* Create new summary-LSA instance. */
 1279:   if ( !(new = ospf_summary_lsa_new (area, (struct prefix *) p, metric, id)))
 1280:     return NULL;
 1281: 
 1282:   /* Instlal LSA to LSDB. */
 1283:   new = ospf_lsa_install (area->ospf, NULL, new);
 1284: 
 1285:   /* Update LSA origination count. */
 1286:   area->ospf->lsa_originate_count++;
 1287: 
 1288:   /* Flooding new LSA through area. */
 1289:   ospf_flood_through_area (area, NULL, new);
 1290: 
 1291:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1292:     {
 1293:       zlog_debug ("LSA[Type%d:%s]: Originate summary-LSA %p",
 1294: 		 new->data->type, inet_ntoa (new->data->id), new);
 1295:       ospf_lsa_header_dump (new->data);
 1296:     }
 1297: 
 1298:   return new;
 1299: }
 1300: 
 1301: static struct ospf_lsa*
 1302: ospf_summary_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
 1303: {
 1304:   struct ospf_lsa *new;
 1305:   struct summary_lsa *sl;
 1306:   struct prefix p;
 1307:   
 1308:   /* Sanity check. */
 1309:   assert (lsa->data);
 1310: 
 1311:   sl = (struct summary_lsa *)lsa->data;
 1312:   p.prefixlen = ip_masklen (sl->mask);
 1313:   new = ospf_summary_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
 1314: 			      sl->header.id);
 1315:   
 1316:   if (!new)
 1317:     return NULL;
 1318:   
 1319:   new->data->ls_seqnum = lsa_seqnum_increment (lsa);
 1320: 
 1321:   ospf_lsa_install (ospf, NULL, new);
 1322:   
 1323:   /* Flood LSA through AS. */
 1324:   ospf_flood_through_area (new->area, NULL, new);
 1325: 
 1326:   /* Debug logging. */
 1327:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1328:     {
 1329:       zlog_debug ("LSA[Type%d:%s]: summary-LSA refresh",
 1330: 		 new->data->type, inet_ntoa (new->data->id));
 1331:       ospf_lsa_header_dump (new->data);
 1332:     }
 1333:   
 1334:   return new;
 1335: }
 1336: 
 1337: 
 1338: /* summary-ASBR-LSA related functions. */
 1339: static void
 1340: ospf_summary_asbr_lsa_body_set (struct stream *s, struct prefix *p,
 1341: 				u_int32_t metric)
 1342: {
 1343:   /* Put Network Mask. */
 1344:   stream_put_ipv4 (s, (u_int32_t) 0);
 1345: 
 1346:   /* Set # TOS. */
 1347:   stream_putc (s, (u_char) 0);
 1348: 
 1349:   /* Set metric. */
 1350:   stream_put_ospf_metric (s, metric);
 1351: }
 1352: 
 1353: static struct ospf_lsa *
 1354: ospf_summary_asbr_lsa_new (struct ospf_area *area, struct prefix *p,
 1355: 			   u_int32_t metric, struct in_addr id)
 1356: {
 1357:   struct stream *s;
 1358:   struct ospf_lsa *new;
 1359:   struct lsa_header *lsah;
 1360:   int length;
 1361: 
 1362:   if (id.s_addr == 0xffffffff)
 1363:     {
 1364:       /* Maybe Link State ID not available. */
 1365:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1366:         zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
 1367:                     OSPF_ASBR_SUMMARY_LSA);
 1368:       return NULL;
 1369:     }
 1370: 
 1371:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1372:     zlog_debug ("LSA[Type3]: Create summary-LSA instance");
 1373: 
 1374:   /* Create new stream for LSA. */
 1375:   s = stream_new (OSPF_MAX_LSA_SIZE);
 1376:   lsah = (struct lsa_header *) STREAM_DATA (s);
 1377: 
 1378:   lsa_header_set (s, LSA_OPTIONS_GET (area), OSPF_ASBR_SUMMARY_LSA,
 1379: 		  id, area->ospf->router_id);
 1380: 
 1381:   /* Set summary-LSA body fields. */
 1382:   ospf_summary_asbr_lsa_body_set (s, p, metric);
 1383: 
 1384:   /* Set length. */
 1385:   length = stream_get_endp (s);
 1386:   lsah->length = htons (length);
 1387: 
 1388:   /* Create OSPF LSA instance. */
 1389:   new = ospf_lsa_new ();
 1390:   new->area = area;
 1391:   SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_SELF_CHECKED);
 1392: 
 1393:   /* Copy LSA to store. */
 1394:   new->data = ospf_lsa_data_new (length);
 1395:   memcpy (new->data, lsah, length);
 1396:   stream_free (s);
 1397: 
 1398:   return new;
 1399: }
 1400: 
 1401: /* Originate summary-ASBR-LSA. */
 1402: struct ospf_lsa *
 1403: ospf_summary_asbr_lsa_originate (struct prefix_ipv4 *p, u_int32_t metric, 
 1404: 				 struct ospf_area *area)
 1405: {
 1406:   struct ospf_lsa *new;
 1407:   struct in_addr id;
 1408:   
 1409:   id = ospf_lsa_unique_id (area->ospf, area->lsdb, OSPF_ASBR_SUMMARY_LSA, p);
 1410: 
 1411:   if (id.s_addr == 0xffffffff)
 1412:     {
 1413:       /* Maybe Link State ID not available. */
 1414:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1415:         zlog_debug ("LSA[Type%d]: Link ID not available, can't originate",
 1416:                     OSPF_ASBR_SUMMARY_LSA);
 1417:       return NULL;
 1418:     }
 1419:   
 1420:   /* Create new summary-LSA instance. */
 1421:   new = ospf_summary_asbr_lsa_new (area, (struct prefix *) p, metric, id);
 1422:   if (!new)
 1423:     return NULL;
 1424: 
 1425:   /* Install LSA to LSDB. */
 1426:   new = ospf_lsa_install (area->ospf, NULL, new);
 1427:   
 1428:   /* Update LSA origination count. */
 1429:   area->ospf->lsa_originate_count++;
 1430: 
 1431:   /* Flooding new LSA through area. */
 1432:   ospf_flood_through_area (area, NULL, new);
 1433: 
 1434:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1435:     {
 1436:       zlog_debug ("LSA[Type%d:%s]: Originate summary-ASBR-LSA %p",
 1437: 		 new->data->type, inet_ntoa (new->data->id), new);
 1438:       ospf_lsa_header_dump (new->data);
 1439:     }
 1440: 
 1441:   return new;
 1442: }
 1443: 
 1444: static struct ospf_lsa*
 1445: ospf_summary_asbr_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
 1446: {
 1447:   struct ospf_lsa *new;
 1448:   struct summary_lsa *sl;
 1449:   struct prefix p;
 1450: 
 1451:   /* Sanity check. */
 1452:   assert (lsa->data);
 1453: 
 1454:   sl = (struct summary_lsa *)lsa->data;
 1455:   p.prefixlen = ip_masklen (sl->mask);
 1456:   new = ospf_summary_asbr_lsa_new (lsa->area, &p, GET_METRIC (sl->metric),
 1457: 				   sl->header.id);
 1458:   if (!new)
 1459:     return NULL;
 1460:   
 1461:   new->data->ls_seqnum = lsa_seqnum_increment (lsa);
 1462: 
 1463:   ospf_lsa_install (ospf, NULL, new);
 1464:   
 1465:   /* Flood LSA through area. */
 1466:   ospf_flood_through_area (new->area, NULL, new);
 1467: 
 1468:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1469:     {
 1470:       zlog_debug ("LSA[Type%d:%s]: summary-ASBR-LSA refresh",
 1471: 		 new->data->type, inet_ntoa (new->data->id));
 1472:       ospf_lsa_header_dump (new->data);
 1473:     }
 1474: 
 1475:   return new;
 1476: }
 1477: 
 1478: /* AS-external-LSA related functions. */
 1479: 
 1480: /* Get nexthop for AS-external-LSAs.  Return nexthop if its interface
 1481:    is connected, else 0*/
 1482: static struct in_addr
 1483: ospf_external_lsa_nexthop_get (struct ospf *ospf, struct in_addr nexthop)
 1484: {
 1485:   struct in_addr fwd;
 1486:   struct prefix nh;
 1487:   struct listnode *node;
 1488:   struct ospf_interface *oi;
 1489: 
 1490:   fwd.s_addr = 0;
 1491: 
 1492:   if (!nexthop.s_addr)
 1493:     return fwd;
 1494: 
 1495:   /* Check whether nexthop is covered by OSPF network. */
 1496:   nh.family = AF_INET;
 1497:   nh.u.prefix4 = nexthop;
 1498:   nh.prefixlen = IPV4_MAX_BITLEN;
 1499:   
 1500:   /* XXX/SCALE: If there were a lot of oi's on an ifp, then it'd be
 1501:    * better to make use of the per-ifp table of ois.
 1502:    */
 1503:   for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
 1504:     if (if_is_operative (oi->ifp))
 1505:       if (oi->address->family == AF_INET)
 1506:         if (prefix_match (oi->address, &nh))
 1507:           return nexthop;
 1508: 
 1509:   return fwd;
 1510: }
 1511: 
 1512: /* NSSA-external-LSA related functions. */
 1513: 
 1514: /* Get 1st IP connection for Forward Addr */
 1515: 
 1516: struct in_addr
 1517: ospf_get_ip_from_ifp (struct ospf_interface *oi)
 1518: {
 1519:   struct in_addr fwd;
 1520: 
 1521:   fwd.s_addr = 0;
 1522: 
 1523:   if (if_is_operative (oi->ifp))
 1524:     return oi->address->u.prefix4;
 1525:   
 1526:   return fwd;
 1527: }
 1528: 
 1529: /* Get 1st IP connection for Forward Addr */
 1530: struct in_addr
 1531: ospf_get_nssa_ip (struct ospf_area *area)
 1532: {
 1533:   struct in_addr fwd;
 1534:   struct in_addr best_default;
 1535:   struct listnode *node;
 1536:   struct ospf_interface *oi;
 1537: 
 1538:   fwd.s_addr = 0;
 1539:   best_default.s_addr = 0;
 1540: 
 1541:   for (ALL_LIST_ELEMENTS_RO (area->ospf->oiflist, node, oi))
 1542:     {
 1543:       if (if_is_operative (oi->ifp))
 1544: 	if (oi->area->external_routing == OSPF_AREA_NSSA)
 1545: 	  if (oi->address && oi->address->family == AF_INET)
 1546: 	    {
 1547: 	      if (best_default.s_addr == 0)
 1548: 		best_default = oi->address->u.prefix4;
 1549: 	      if (oi->area == area)
 1550: 		return oi->address->u.prefix4;
 1551: 	    }
 1552:     }
 1553:   if (best_default.s_addr != 0)
 1554:     return best_default;
 1555: 
 1556:   if (best_default.s_addr != 0)
 1557:     return best_default;
 1558: 
 1559:   return fwd;
 1560: }
 1561: 
 1562: #define DEFAULT_DEFAULT_METRIC	             20
 1563: #define DEFAULT_DEFAULT_ORIGINATE_METRIC     10
 1564: #define DEFAULT_DEFAULT_ALWAYS_METRIC	      1
 1565: 
 1566: #define DEFAULT_METRIC_TYPE		     EXTERNAL_METRIC_TYPE_2
 1567: 
 1568: int
 1569: metric_type (struct ospf *ospf, u_char src)
 1570: {
 1571:   return (ospf->dmetric[src].type < 0 ?
 1572: 	  DEFAULT_METRIC_TYPE : ospf->dmetric[src].type);
 1573: }
 1574: 
 1575: int
 1576: metric_value (struct ospf *ospf, u_char src)
 1577: {
 1578:   if (ospf->dmetric[src].value < 0)
 1579:     {
 1580:       if (src == DEFAULT_ROUTE)
 1581: 	{
 1582: 	  if (ospf->default_originate == DEFAULT_ORIGINATE_ZEBRA)
 1583: 	    return DEFAULT_DEFAULT_ORIGINATE_METRIC;
 1584: 	  else
 1585: 	    return DEFAULT_DEFAULT_ALWAYS_METRIC;
 1586: 	}
 1587:       else if (ospf->default_metric < 0)
 1588: 	return DEFAULT_DEFAULT_METRIC;
 1589:       else
 1590: 	return ospf->default_metric;
 1591:     }
 1592: 
 1593:   return ospf->dmetric[src].value;
 1594: }
 1595: 
 1596: /* Set AS-external-LSA body. */
 1597: static void
 1598: ospf_external_lsa_body_set (struct stream *s, struct external_info *ei,
 1599: 			    struct ospf *ospf)
 1600: {
 1601:   struct prefix_ipv4 *p = &ei->p;
 1602:   struct in_addr mask, fwd_addr;
 1603:   u_int32_t mvalue;
 1604:   int mtype;
 1605:   int type;
 1606: 
 1607:   /* Put Network Mask. */
 1608:   masklen2ip (p->prefixlen, &mask);
 1609:   stream_put_ipv4 (s, mask.s_addr);
 1610: 
 1611:   /* If prefix is default, specify DEFAULT_ROUTE. */
 1612:   type = is_prefix_default (&ei->p) ? DEFAULT_ROUTE : ei->type;
 1613:   
 1614:   mtype = (ROUTEMAP_METRIC_TYPE (ei) != -1) ?
 1615:     ROUTEMAP_METRIC_TYPE (ei) : metric_type (ospf, type);
 1616: 
 1617:   mvalue = (ROUTEMAP_METRIC (ei) != -1) ?
 1618:     ROUTEMAP_METRIC (ei) : metric_value (ospf, type);
 1619: 
 1620:   /* Put type of external metric. */
 1621:   stream_putc (s, (mtype == EXTERNAL_METRIC_TYPE_2 ? 0x80 : 0));
 1622: 
 1623:   /* Put 0 metric. TOS metric is not supported. */
 1624:   stream_put_ospf_metric (s, mvalue);
 1625:   
 1626:   /* Get forwarding address to nexthop if on the Connection List, else 0. */
 1627:   fwd_addr = (ei->route_map_set.nexthop.s_addr != -1) ?
 1628:     ROUTEMAP_NEXTHOP (ei) : ospf_external_lsa_nexthop_get (ospf, ei->nexthop);
 1629: 
 1630:   /* Put forwarding address. */
 1631:   stream_put_ipv4 (s, fwd_addr.s_addr);
 1632:   
 1633:   /* Put route tag -- This value should be introduced from configuration. */
 1634:   stream_putl (s, 0);
 1635: }
 1636: 
 1637: /* Create new external-LSA. */
 1638: static struct ospf_lsa *
 1639: ospf_external_lsa_new (struct ospf *ospf,
 1640: 		       struct external_info *ei, struct in_addr *old_id)
 1641: {
 1642:   struct stream *s;
 1643:   struct lsa_header *lsah;
 1644:   struct ospf_lsa *new;
 1645:   struct in_addr id;
 1646:   int length;
 1647: 
 1648:   if (ei == NULL)
 1649:     {
 1650:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1651: 	zlog_debug ("LSA[Type5]: External info is NULL, can't originate");
 1652:       return NULL;
 1653:     }
 1654: 
 1655:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1656:     zlog_debug ("LSA[Type5]: Originate AS-external-LSA instance");
 1657: 
 1658:   /* If old Link State ID is specified, refresh LSA with same ID. */
 1659:   if (old_id)
 1660:     id = *old_id;
 1661:   /* Get Link State with unique ID. */
 1662:   else
 1663:     {
 1664:       id = ospf_lsa_unique_id (ospf, ospf->lsdb, OSPF_AS_EXTERNAL_LSA, &ei->p);
 1665:       if (id.s_addr == 0xffffffff)
 1666: 	{
 1667: 	  /* Maybe Link State ID not available. */
 1668: 	  if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 1669: 	    zlog_debug ("LSA[Type5]: Link ID not available, can't originate");
 1670: 	  return NULL;
 1671: 	}
 1672:     }
 1673: 
 1674:   /* Create new stream for LSA. */
 1675:   s = stream_new (OSPF_MAX_LSA_SIZE);
 1676:   lsah = (struct lsa_header *) STREAM_DATA (s);
 1677: 
 1678:   /* Set LSA common header fields. */
 1679:   lsa_header_set (s, OSPF_OPTION_E, OSPF_AS_EXTERNAL_LSA,
 1680: 		  id, ospf->router_id);
 1681: 
 1682:   /* Set AS-external-LSA body fields. */
 1683:   ospf_external_lsa_body_set (s, ei, ospf);
 1684: 
 1685:   /* Set length. */
 1686:   length = stream_get_endp (s);
 1687:   lsah->length = htons (length);
 1688: 
 1689:   /* Now, create OSPF LSA instance. */
 1690:   new = ospf_lsa_new ();
 1691:   new->area = NULL;
 1692:   SET_FLAG (new->flags, OSPF_LSA_SELF | OSPF_LSA_APPROVED | OSPF_LSA_SELF_CHECKED);
 1693: 
 1694:   /* Copy LSA data to store, discard stream. */
 1695:   new->data = ospf_lsa_data_new (length);
 1696:   memcpy (new->data, lsah, length);
 1697:   stream_free (s);
 1698: 
 1699:   return new;
 1700: }
 1701: 
 1702: /* As Type-7 */
 1703: static void
 1704: ospf_install_flood_nssa (struct ospf *ospf, 
 1705: 			 struct ospf_lsa *lsa, struct external_info *ei)
 1706: {
 1707:   struct ospf_lsa *new;
 1708:   struct as_external_lsa *extlsa;
 1709:   struct ospf_area *area;
 1710:   struct listnode *node, *nnode;
 1711: 
 1712:   /* LSA may be a Type-5 originated via translation of a Type-7 LSA
 1713:    * which originated from an NSSA area. In which case it should not be 
 1714:    * flooded back to NSSA areas.
 1715:    */
 1716:   if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
 1717:     return;
 1718:     
 1719:   /* NSSA Originate or Refresh (If anyNSSA)
 1720: 
 1721:   LSA is self-originated. And just installed as Type-5.
 1722:   Additionally, install as Type-7 LSDB for every attached NSSA.
 1723: 
 1724:   P-Bit controls which ABR performs translation to outside world; If
 1725:   we are an ABR....do not set the P-bit, because we send the Type-5,
 1726:   not as the ABR Translator, but as the ASBR owner within the AS!
 1727: 
 1728:   If we are NOT ABR, Flood through NSSA as Type-7 w/P-bit set.  The
 1729:   elected ABR Translator will see the P-bit, Translate, and re-flood.
 1730: 
 1731:   Later, ABR_TASK and P-bit will scan Type-7 LSDB and translate to
 1732:   Type-5's to non-NSSA Areas.  (it will also attempt a re-install) */
 1733: 
 1734:   for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
 1735:     {
 1736:       /* Don't install Type-7 LSA's into nonNSSA area */
 1737:       if (area->external_routing != OSPF_AREA_NSSA)
 1738:         continue;
 1739: 
 1740:       /* make lsa duplicate, lock=1 */
 1741:       new = ospf_lsa_dup (lsa);
 1742:       new->area = area;
 1743:       new->data->type = OSPF_AS_NSSA_LSA;
 1744: 
 1745:       /* set P-bit if not ABR */
 1746:       if (! IS_OSPF_ABR (ospf))
 1747:         {
 1748: 	  SET_FLAG(new->data->options, OSPF_OPTION_NP);
 1749:        
 1750: 	  /* set non-zero FWD ADDR
 1751:        
 1752: 	  draft-ietf-ospf-nssa-update-09.txt
 1753:        
 1754: 	  if the network between the NSSA AS boundary router and the
 1755: 	  adjacent AS is advertised into OSPF as an internal OSPF route,
 1756: 	  the forwarding address should be the next op address as is cu
 1757: 	  currently done with type-5 LSAs.  If the intervening network is
 1758: 	  not adversited into OSPF as an internal OSPF route and the
 1759: 	  type-7 LSA's P-bit is set a forwarding address should be
 1760: 	  selected from one of the router's active OSPF inteface addresses
 1761: 	  which belong to the NSSA.  If no such addresses exist, then
 1762: 	  no type-7 LSA's with the P-bit set should originate from this
 1763: 	  router.   */
 1764:        
 1765: 	  /* kevinm: not updating lsa anymore, just new */
 1766: 	  extlsa = (struct as_external_lsa *)(new->data);
 1767:        
 1768: 	  if (extlsa->e[0].fwd_addr.s_addr == 0)
 1769: 	    extlsa->e[0].fwd_addr = ospf_get_nssa_ip(area); /* this NSSA area in ifp */
 1770: 
 1771: 	  if (extlsa->e[0].fwd_addr.s_addr == 0) 
 1772: 	  {
 1773: 	    if (IS_DEBUG_OSPF_NSSA)
 1774: 	      zlog_debug ("LSA[Type-7]: Could not build FWD-ADDR");
 1775: 	    ospf_lsa_discard (new);
 1776: 	    return;
 1777: 	  }
 1778: 	}
 1779: 
 1780:       /* install also as Type-7 */
 1781:       ospf_lsa_install (ospf, NULL, new);   /* Remove Old, Lock New = 2 */
 1782: 
 1783:       /* will send each copy, lock=2+n */
 1784:       ospf_flood_through_as (ospf, NULL, new); /* all attached NSSA's, no AS/STUBs */
 1785:     }
 1786: }
 1787: 
 1788: static struct ospf_lsa *
 1789: ospf_lsa_translated_nssa_new (struct ospf *ospf, 
 1790:                              struct ospf_lsa *type7)
 1791: {
 1792: 
 1793:   struct ospf_lsa *new;
 1794:   struct as_external_lsa *ext, *extnew;
 1795:   struct external_info ei;
 1796:   
 1797:   ext = (struct as_external_lsa *)(type7->data);
 1798: 
 1799:   /* need external_info struct, fill in bare minimum */  
 1800:   ei.p.family = AF_INET;
 1801:   ei.p.prefix = type7->data->id;
 1802:   ei.p.prefixlen = ip_masklen (ext->mask);
 1803:   ei.type = ZEBRA_ROUTE_OSPF;
 1804:   ei.nexthop = ext->header.adv_router;
 1805:   ei.route_map_set.metric = -1;
 1806:   ei.route_map_set.metric_type = -1;
 1807:   ei.tag = 0;
 1808:   
 1809:   if ( (new = ospf_external_lsa_new (ospf, &ei, &type7->data->id)) == NULL)
 1810:   {
 1811:     if (IS_DEBUG_OSPF_NSSA)
 1812:       zlog_debug ("ospf_nssa_translate_originate(): Could not originate "
 1813:                  "Translated Type-5 for %s", 
 1814:                  inet_ntoa (ei.p.prefix));
 1815:     return NULL;
 1816:   }
 1817: 
 1818:   extnew = (struct as_external_lsa *)(new->data);
 1819:    
 1820:   /* copy over Type-7 data to new */
 1821:   extnew->e[0].tos = ext->e[0].tos;
 1822:   extnew->e[0].route_tag = ext->e[0].route_tag;
 1823:   extnew->e[0].fwd_addr.s_addr = ext->e[0].fwd_addr.s_addr;
 1824:   new->data->ls_seqnum = type7->data->ls_seqnum;
 1825: 
 1826:   /* add translated flag, checksum and lock new lsa */
 1827:   SET_FLAG (new->flags, OSPF_LSA_LOCAL_XLT); /* Translated from 7  */   
 1828:   new = ospf_lsa_lock (new);
 1829:   
 1830:   return new; 
 1831: }
 1832: 
 1833: /* Originate Translated Type-5 for supplied Type-7 NSSA LSA */
 1834: struct ospf_lsa *
 1835: ospf_translated_nssa_originate (struct ospf *ospf, struct ospf_lsa *type7)
 1836: {
 1837:   struct ospf_lsa *new;
 1838:   struct as_external_lsa *extnew;
 1839:   
 1840:   /* we cant use ospf_external_lsa_originate() as we need to set
 1841:    * the OSPF_LSA_LOCAL_XLT flag, must originate by hand
 1842:    */
 1843:   
 1844:   if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
 1845:     {
 1846:       if (IS_DEBUG_OSPF_NSSA)
 1847:         zlog_debug ("ospf_translated_nssa_originate(): Could not translate "
 1848:                  "Type-7, Id %s, to Type-5",
 1849:                  inet_ntoa (type7->data->id));
 1850:       return NULL;
 1851:     }
 1852:     
 1853:   extnew = (struct as_external_lsa *)new;
 1854:   
 1855:   if (IS_DEBUG_OSPF_NSSA)
 1856:     {
 1857:       zlog_debug ("ospf_translated_nssa_originate(): "
 1858:                  "translated Type 7, installed:");
 1859:       ospf_lsa_header_dump (new->data);
 1860:       zlog_debug ("   Network mask: %d",ip_masklen (extnew->mask));
 1861:       zlog_debug ("   Forward addr: %s", inet_ntoa (extnew->e[0].fwd_addr));
 1862:     }
 1863:   
 1864:   if ( (new = ospf_lsa_install (ospf, NULL, new)) == NULL)
 1865:     {
 1866:       if (IS_DEBUG_OSPF_NSSA)
 1867:         zlog_debug ("ospf_lsa_translated_nssa_originate(): "
 1868:                    "Could not install LSA "
 1869:                    "id %s", inet_ntoa (type7->data->id));
 1870:       return NULL;
 1871:     }
 1872:     
 1873:   ospf->lsa_originate_count++;
 1874:   ospf_flood_through_as (ospf, NULL, new);
 1875: 
 1876:   return new;
 1877: }
 1878: 
 1879: /* Refresh Translated from NSSA AS-external-LSA. */
 1880: struct ospf_lsa *
 1881: ospf_translated_nssa_refresh (struct ospf *ospf, struct ospf_lsa *type7, 
 1882:                               struct ospf_lsa *type5)
 1883: {
 1884:   struct ospf_lsa *new = NULL;
 1885:   
 1886:   /* Sanity checks. */
 1887:   assert (type7 || type5);
 1888:   if (!(type7 || type5))
 1889:     return NULL;
 1890:   if (type7)
 1891:     assert (type7->data);
 1892:   if (type5)
 1893:     assert (type5->data);
 1894:   assert (ospf->anyNSSA);
 1895: 
 1896:   /* get required data according to what has been given */
 1897:   if (type7 && type5 == NULL)
 1898:     {
 1899:       /* find the translated Type-5 for this Type-7 */
 1900:       struct as_external_lsa *ext = (struct as_external_lsa *)(type7->data);
 1901:       struct prefix_ipv4 p = 
 1902:         { 
 1903:           .prefix = type7->data->id,
 1904:           .prefixlen = ip_masklen (ext->mask),
 1905:           .family = AF_INET,
 1906:         };
 1907: 
 1908:       type5 = ospf_external_info_find_lsa (ospf, &p);
 1909:     }
 1910:   else if (type5 && type7 == NULL)
 1911:     {
 1912:       /* find the type-7 from which supplied type-5 was translated,
 1913:        * ie find first type-7 with same LSA Id.
 1914:        */
 1915:       struct listnode *ln, *lnn;
 1916:       struct route_node *rn;
 1917:       struct ospf_lsa *lsa;
 1918:       struct ospf_area *area;
 1919:           
 1920:       for (ALL_LIST_ELEMENTS (ospf->areas, ln, lnn, area))
 1921:         {
 1922:           if (area->external_routing != OSPF_AREA_NSSA 
 1923:               && !type7)
 1924:             continue;
 1925:             
 1926:           LSDB_LOOP (NSSA_LSDB(area), rn, lsa)
 1927:             {
 1928:               if (lsa->data->id.s_addr == type5->data->id.s_addr)
 1929:                 {
 1930:                   type7 = lsa;
 1931:                   break;
 1932:                 }
 1933:             }
 1934:         }
 1935:     }
 1936: 
 1937:   /* do we have type7? */
 1938:   if (!type7)
 1939:     {
 1940:       if (IS_DEBUG_OSPF_NSSA)
 1941:         zlog_debug ("ospf_translated_nssa_refresh(): no Type-7 found for "
 1942:                    "Type-5 LSA Id %s",
 1943:                    inet_ntoa (type5->data->id));
 1944:       return NULL;
 1945:     }
 1946: 
 1947:   /* do we have valid translated type5? */
 1948:   if (type5 == NULL || !CHECK_FLAG (type5->flags, OSPF_LSA_LOCAL_XLT) )
 1949:     {
 1950:       if (IS_DEBUG_OSPF_NSSA)
 1951:         zlog_debug ("ospf_translated_nssa_refresh(): No translated Type-5 "
 1952:                    "found for Type-7 with Id %s",
 1953:                    inet_ntoa (type7->data->id));
 1954:       return NULL;
 1955:     }
 1956: 
 1957:   /* Delete LSA from neighbor retransmit-list. */
 1958:   ospf_ls_retransmit_delete_nbr_as (ospf, type5);
 1959:   
 1960:   /* create new translated LSA */
 1961:   if ( (new = ospf_lsa_translated_nssa_new (ospf, type7)) == NULL)
 1962:     {
 1963:       if (IS_DEBUG_OSPF_NSSA)
 1964:         zlog_debug ("ospf_translated_nssa_refresh(): Could not translate "
 1965:                    "Type-7 for %s to Type-5",
 1966:                    inet_ntoa (type7->data->id));
 1967:       return NULL;
 1968:     }
 1969: 
 1970:   if ( !(new = ospf_lsa_install (ospf, NULL, new)) )
 1971:     {
 1972:       if (IS_DEBUG_OSPF_NSSA)
 1973:         zlog_debug ("ospf_translated_nssa_refresh(): Could not install "
 1974:                    "translated LSA, Id %s",
 1975:                    inet_ntoa (type7->data->id));
 1976:       return NULL;
 1977:     }
 1978:   
 1979:   /* Flood LSA through area. */
 1980:   ospf_flood_through_as (ospf, NULL, new);
 1981: 
 1982:   return new;
 1983: }
 1984: 
 1985: int
 1986: is_prefix_default (struct prefix_ipv4 *p)
 1987: {
 1988:   struct prefix_ipv4 q;
 1989: 
 1990:   q.family = AF_INET;
 1991:   q.prefix.s_addr = 0;
 1992:   q.prefixlen = 0;
 1993: 
 1994:   return prefix_same ((struct prefix *) p, (struct prefix *) &q);
 1995: }
 1996: 
 1997: /* Originate an AS-external-LSA, install and flood. */
 1998: struct ospf_lsa *
 1999: ospf_external_lsa_originate (struct ospf *ospf, struct external_info *ei)
 2000: {
 2001:   struct ospf_lsa *new;
 2002: 
 2003:   /* Added for NSSA project....
 2004: 
 2005:        External LSAs are originated in ASBRs as usual, but for NSSA systems.
 2006:      there is the global Type-5 LSDB and a Type-7 LSDB installed for
 2007:      every area.  The Type-7's are flooded to every IR and every ABR; We
 2008:      install the Type-5 LSDB so that the normal "refresh" code operates
 2009:      as usual, and flag them as not used during ASE calculations.  The
 2010:      Type-7 LSDB is used for calculations.  Each Type-7 has a Forwarding
 2011:      Address of non-zero.
 2012: 
 2013:      If an ABR is the elected NSSA translator, following SPF and during
 2014:      the ABR task it will translate all the scanned Type-7's, with P-bit
 2015:      ON and not-self generated, and translate to Type-5's throughout the
 2016:      non-NSSA/STUB AS.
 2017: 
 2018:      A difference in operation depends whether this ASBR is an ABR
 2019:      or not.  If not an ABR, the P-bit is ON, to indicate that any
 2020:      elected NSSA-ABR can perform its translation.
 2021: 
 2022:      If an ABR, the P-bit is OFF;  No ABR will perform translation and
 2023:      this ASBR will flood the Type-5 LSA as usual.
 2024: 
 2025:      For the case where this ASBR is not an ABR, the ASE calculations
 2026:      are based on the Type-5 LSDB;  The Type-7 LSDB exists just to
 2027:      demonstrate to the user that there are LSA's that belong to any
 2028:      attached NSSA.
 2029: 
 2030:      Finally, it just so happens that when the ABR is translating every
 2031:      Type-7 into Type-5, it installs it into the Type-5 LSDB as an
 2032:      approved Type-5 (translated from Type-7);  at the end of translation
 2033:      if any Translated Type-5's remain unapproved, then they must be
 2034:      flushed from the AS.
 2035: 
 2036:      */
 2037:   
 2038:   /* Check the AS-external-LSA should be originated. */
 2039:   if (!ospf_redistribute_check (ospf, ei, NULL))
 2040:     return NULL;
 2041:   
 2042:   /* Create new AS-external-LSA instance. */
 2043:   if ((new = ospf_external_lsa_new (ospf, ei, NULL)) == NULL)
 2044:     {
 2045:       if (IS_DEBUG_OSPF_EVENT)
 2046: 	zlog_debug ("LSA[Type5:%s]: Could not originate AS-external-LSA",
 2047: 		   inet_ntoa (ei->p.prefix));
 2048:       return NULL;
 2049:     }
 2050: 
 2051:   /* Install newly created LSA into Type-5 LSDB, lock = 1. */
 2052:   ospf_lsa_install (ospf, NULL, new);
 2053: 
 2054:   /* Update LSA origination count. */
 2055:   ospf->lsa_originate_count++;
 2056: 
 2057:   /* Flooding new LSA. only to AS (non-NSSA/STUB) */
 2058:   ospf_flood_through_as (ospf, NULL, new);
 2059: 
 2060:   /* If there is any attached NSSA, do special handling */
 2061:   if (ospf->anyNSSA &&
 2062:       /* stay away from translated LSAs! */
 2063:       !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
 2064:     ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood Type-7 to all NSSAs */
 2065: 
 2066:   /* Debug logging. */
 2067:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 2068:     {
 2069:       zlog_debug ("LSA[Type%d:%s]: Originate AS-external-LSA %p",
 2070: 		 new->data->type, inet_ntoa (new->data->id), new);
 2071:       ospf_lsa_header_dump (new->data);
 2072:     }
 2073: 
 2074:   return new;
 2075: }
 2076: 
 2077: /* Originate AS-external-LSA from external info with initial flag. */
 2078: int
 2079: ospf_external_lsa_originate_timer (struct thread *thread)
 2080: {
 2081:   struct ospf *ospf = THREAD_ARG (thread);
 2082:   struct route_node *rn;
 2083:   struct external_info *ei;
 2084:   struct route_table *rt;
 2085:   int type = THREAD_VAL (thread);
 2086: 
 2087:   ospf->t_external_lsa = NULL;
 2088: 
 2089:   /* Originate As-external-LSA from all type of distribute source. */
 2090:   if ((rt = EXTERNAL_INFO (type)))
 2091:     for (rn = route_top (rt); rn; rn = route_next (rn))
 2092:       if ((ei = rn->info) != NULL)
 2093: 	if (!is_prefix_default ((struct prefix_ipv4 *)&ei->p))
 2094: 	  if (!ospf_external_lsa_originate (ospf, ei))
 2095: 	    zlog_warn ("LSA: AS-external-LSA was not originated.");
 2096:   
 2097:   return 0;
 2098: }
 2099: 
 2100: static struct external_info *
 2101: ospf_default_external_info (struct ospf *ospf)
 2102: {
 2103:   int type;
 2104:   struct route_node *rn;
 2105:   struct prefix_ipv4 p;
 2106:   
 2107:   p.family = AF_INET;
 2108:   p.prefix.s_addr = 0;
 2109:   p.prefixlen = 0;
 2110: 
 2111:   /* First, lookup redistributed default route. */
 2112:   for (type = 0; type <= ZEBRA_ROUTE_MAX; type++)
 2113:     if (EXTERNAL_INFO (type) && type != ZEBRA_ROUTE_OSPF)
 2114:       {
 2115: 	rn = route_node_lookup (EXTERNAL_INFO (type), (struct prefix *) &p);
 2116: 	if (rn != NULL)
 2117: 	  {
 2118: 	    route_unlock_node (rn);
 2119: 	    assert (rn->info);
 2120: 	    if (ospf_redistribute_check (ospf, rn->info, NULL))
 2121: 	      return rn->info;
 2122: 	  }
 2123:       }
 2124: 
 2125:   return NULL;
 2126: }
 2127: 
 2128: int
 2129: ospf_default_originate_timer (struct thread *thread)
 2130: {
 2131:   struct prefix_ipv4 p;
 2132:   struct in_addr nexthop;
 2133:   struct external_info *ei;
 2134:   struct ospf *ospf;
 2135:   
 2136:   ospf = THREAD_ARG (thread);
 2137: 
 2138:   p.family = AF_INET;
 2139:   p.prefix.s_addr = 0;
 2140:   p.prefixlen = 0;
 2141: 
 2142:   if (ospf->default_originate == DEFAULT_ORIGINATE_ALWAYS)
 2143:     {
 2144:       /* If there is no default route via redistribute,
 2145: 	 then originate AS-external-LSA with nexthop 0 (self). */
 2146:       nexthop.s_addr = 0;
 2147:       ospf_external_info_add (DEFAULT_ROUTE, p, 0, nexthop);
 2148:     }
 2149: 
 2150:   if ((ei = ospf_default_external_info (ospf)))
 2151:     ospf_external_lsa_originate (ospf, ei);
 2152:   
 2153:   return 0;
 2154: }
 2155: 
 2156: /* Flush any NSSA LSAs for given prefix */
 2157: void
 2158: ospf_nssa_lsa_flush (struct ospf *ospf, struct prefix_ipv4 *p)
 2159: {
 2160:   struct listnode *node, *nnode;
 2161:   struct ospf_lsa *lsa;
 2162:   struct ospf_area *area;
 2163: 
 2164:   for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
 2165:   {
 2166:     if (area->external_routing == OSPF_AREA_NSSA)
 2167:     {
 2168:       if (!(lsa = ospf_lsa_lookup (area, OSPF_AS_NSSA_LSA, p->prefix,
 2169:                                 ospf->router_id))) 
 2170:       {
 2171:         if (IS_DEBUG_OSPF (lsa, LSA_FLOODING)) 
 2172:           zlog_debug ("LSA: There is no such AS-NSSA-LSA %s/%d in LSDB",
 2173:                     inet_ntoa (p->prefix), p->prefixlen);
 2174:         continue;
 2175:       }
 2176:       ospf_ls_retransmit_delete_nbr_area (area, lsa);
 2177:       if (!IS_LSA_MAXAGE (lsa)) 
 2178:       {
 2179:         ospf_refresher_unregister_lsa (ospf, lsa);
 2180:         ospf_lsa_flush_area (lsa, area);
 2181:       }
 2182:     }
 2183:   }
 2184: }
 2185: 
 2186: /* Flush an AS-external-LSA from LSDB and routing domain. */
 2187: void
 2188: ospf_external_lsa_flush (struct ospf *ospf,
 2189: 			 u_char type, struct prefix_ipv4 *p,
 2190: 			 unsigned int ifindex /*, struct in_addr nexthop */)
 2191: {
 2192:   struct ospf_lsa *lsa;
 2193: 
 2194:   if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2195:     zlog_debug ("LSA: Flushing AS-external-LSA %s/%d",
 2196: 	       inet_ntoa (p->prefix), p->prefixlen);
 2197: 
 2198:   /* First lookup LSA from LSDB. */
 2199:   if (!(lsa = ospf_external_info_find_lsa (ospf, p)))
 2200:     {
 2201:       if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2202: 	zlog_debug ("LSA: There is no such AS-external-LSA %s/%d in LSDB",
 2203: 		   inet_ntoa (p->prefix), p->prefixlen);
 2204:       return;
 2205:     }
 2206: 
 2207:   /* If LSA is selforiginated, not a translated LSA, and there is 
 2208:    * NSSA area, flush Type-7 LSA's at first. 
 2209:    */
 2210:   if (IS_LSA_SELF(lsa) && (ospf->anyNSSA)
 2211:       && !(CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT)))
 2212:     ospf_nssa_lsa_flush (ospf, p);
 2213: 
 2214:   /* Sweep LSA from Link State Retransmit List. */
 2215:   ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
 2216: 
 2217:   /* There must be no self-originated LSA in rtrs_external. */
 2218: #if 0
 2219:   /* Remove External route from Zebra. */
 2220:   ospf_zebra_delete ((struct prefix_ipv4 *) p, &nexthop);
 2221: #endif
 2222: 
 2223:   if (!IS_LSA_MAXAGE (lsa))
 2224:     {
 2225:       /* Unregister LSA from Refresh queue. */
 2226:       ospf_refresher_unregister_lsa (ospf, lsa);
 2227: 
 2228:       /* Flush AS-external-LSA through AS. */
 2229:       ospf_lsa_flush_as (ospf, lsa);
 2230:     }
 2231: 
 2232:   if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2233:     zlog_debug ("ospf_external_lsa_flush(): stop");
 2234: }
 2235: 
 2236: void
 2237: ospf_external_lsa_refresh_default (struct ospf *ospf)
 2238: {
 2239:   struct prefix_ipv4 p;
 2240:   struct external_info *ei;
 2241:   struct ospf_lsa *lsa;
 2242: 
 2243:   p.family = AF_INET;
 2244:   p.prefixlen = 0;
 2245:   p.prefix.s_addr = 0;
 2246: 
 2247:   ei = ospf_default_external_info (ospf);
 2248:   lsa = ospf_external_info_find_lsa (ospf, &p);
 2249: 
 2250:   if (ei)
 2251:     {
 2252:       if (lsa)
 2253: 	{
 2254: 	  if (IS_DEBUG_OSPF_EVENT)
 2255: 	    zlog_debug ("LSA[Type5:0.0.0.0]: Refresh AS-external-LSA %p", lsa);
 2256: 	  ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
 2257: 	}
 2258:       else
 2259: 	{
 2260: 	  if (IS_DEBUG_OSPF_EVENT)
 2261: 	    zlog_debug ("LSA[Type5:0.0.0.0]: Originate AS-external-LSA");
 2262: 	  ospf_external_lsa_originate (ospf, ei);
 2263: 	}
 2264:     }
 2265:   else
 2266:     {
 2267:       if (lsa)
 2268: 	{
 2269: 	  if (IS_DEBUG_OSPF_EVENT)
 2270: 	    zlog_debug ("LSA[Type5:0.0.0.0]: Flush AS-external-LSA");
 2271:           ospf_refresher_unregister_lsa (ospf, lsa);
 2272: 	  ospf_lsa_flush_as (ospf, lsa);
 2273: 	}
 2274:     }
 2275: }
 2276: 
 2277: void
 2278: ospf_external_lsa_refresh_type (struct ospf *ospf, u_char type, int force)
 2279: {
 2280:   struct route_node *rn;
 2281:   struct external_info *ei;
 2282: 
 2283:   if (type != DEFAULT_ROUTE)
 2284:     if (EXTERNAL_INFO(type))
 2285:       /* Refresh each redistributed AS-external-LSAs. */
 2286:       for (rn = route_top (EXTERNAL_INFO (type)); rn; rn = route_next (rn))
 2287: 	if ((ei = rn->info))
 2288: 	  if (!is_prefix_default (&ei->p))
 2289: 	    {
 2290: 	      struct ospf_lsa *lsa;
 2291: 
 2292: 	      if ((lsa = ospf_external_info_find_lsa (ospf, &ei->p)))
 2293: 		ospf_external_lsa_refresh (ospf, lsa, ei, force);
 2294: 	      else
 2295: 		ospf_external_lsa_originate (ospf, ei);
 2296: 	    }
 2297: }
 2298: 
 2299: /* Refresh AS-external-LSA. */
 2300: struct ospf_lsa *
 2301: ospf_external_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa,
 2302: 			   struct external_info *ei, int force)
 2303: {
 2304:   struct ospf_lsa *new;
 2305:   int changed;
 2306:   
 2307:   /* Check the AS-external-LSA should be originated. */
 2308:   if (!ospf_redistribute_check (ospf, ei, &changed))
 2309:     {
 2310:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 2311:         zlog_debug ("LSA[Type%d:%s]: Could not be refreshed, "
 2312:                    "redist check fail", 
 2313:                    lsa->data->type, inet_ntoa (lsa->data->id));
 2314:       ospf_external_lsa_flush (ospf, ei->type, &ei->p,
 2315: 			       ei->ifindex /*, ei->nexthop */);
 2316:       return NULL;
 2317:     }
 2318: 
 2319:   if (!changed && !force)
 2320:     {
 2321:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 2322:         zlog_debug ("LSA[Type%d:%s]: Not refreshed, not changed/forced",
 2323:                    lsa->data->type, inet_ntoa (lsa->data->id));
 2324:       return NULL;
 2325:     }
 2326: 
 2327:   /* Delete LSA from neighbor retransmit-list. */
 2328:   ospf_ls_retransmit_delete_nbr_as (ospf, lsa);
 2329: 
 2330:   /* Unregister AS-external-LSA from refresh-list. */
 2331:   ospf_refresher_unregister_lsa (ospf, lsa);
 2332: 
 2333:   new = ospf_external_lsa_new (ospf, ei, &lsa->data->id);
 2334:   
 2335:   if (new == NULL)
 2336:     {
 2337:       if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 2338: 	zlog_debug ("LSA[Type%d:%s]: Could not be refreshed", lsa->data->type,
 2339: 		   inet_ntoa (lsa->data->id));
 2340:       return NULL;
 2341:     }
 2342:   
 2343:   new->data->ls_seqnum = lsa_seqnum_increment (lsa);
 2344: 
 2345:   ospf_lsa_install (ospf, NULL, new);	/* As type-5. */
 2346: 
 2347:   /* Flood LSA through AS. */
 2348:   ospf_flood_through_as (ospf, NULL, new);
 2349: 
 2350:   /* If any attached NSSA, install as Type-7, flood to all NSSA Areas */
 2351:   if (ospf->anyNSSA && !(CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT)))
 2352:     ospf_install_flood_nssa (ospf, new, ei); /* Install/Flood per new rules */
 2353: 
 2354:   /* Register self-originated LSA to refresh queue. 
 2355:    * Translated LSAs should not be registered, but refreshed upon 
 2356:    * refresh of the Type-7
 2357:    */
 2358:   if ( !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT) )
 2359:     ospf_refresher_register_lsa (ospf, new);
 2360: 
 2361:   /* Debug logging. */
 2362:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 2363:     {
 2364:       zlog_debug ("LSA[Type%d:%s]: AS-external-LSA refresh",
 2365:                  new->data->type, inet_ntoa (new->data->id));
 2366:       ospf_lsa_header_dump (new->data);
 2367:     }
 2368: 
 2369:   return new;
 2370: }
 2371: 
 2372: 
 2373: /* LSA installation functions. */
 2374: 
 2375: /* Install router-LSA to an area. */
 2376: static struct ospf_lsa *
 2377: ospf_router_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
 2378:                          int rt_recalc)
 2379: {
 2380:   struct ospf_area *area = new->area;
 2381: 
 2382:   /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
 2383:      The entire routing table must be recalculated, starting with
 2384:      the shortest path calculations for each area (not just the
 2385:      area whose link-state database has changed). 
 2386:   */
 2387: 
 2388:   if (IS_LSA_SELF (new))
 2389:     {
 2390: 
 2391:       /* Only install LSA if it is originated/refreshed by us.
 2392:        * If LSA was received by flooding, the RECEIVED flag is set so do
 2393:        * not link the LSA */
 2394:       if (CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
 2395: 	return new; /* ignore stale LSA */
 2396: 
 2397:       /* Set self-originated router-LSA. */
 2398:       ospf_lsa_unlock (&area->router_lsa_self);
 2399:       area->router_lsa_self = ospf_lsa_lock (new);
 2400: 
 2401:       ospf_refresher_register_lsa (ospf, new);
 2402:     }
 2403:   if (rt_recalc)
 2404:     ospf_spf_calculate_schedule (ospf);
 2405: 
 2406:   return new;
 2407: }
 2408: 
 2409: #define OSPF_INTERFACE_TIMER_ON(T,F,V) \
 2410: 	if (!(T)) \
 2411: 	  (T) = thread_add_timer (master, (F), oi, (V))
 2412: 
 2413: /* Install network-LSA to an area. */
 2414: static struct ospf_lsa *
 2415: ospf_network_lsa_install (struct ospf *ospf,
 2416: 			  struct ospf_interface *oi, 
 2417: 			  struct ospf_lsa *new,
 2418: 			  int rt_recalc)
 2419: {
 2420: 
 2421:   /* RFC 2328 Section 13.2 Router-LSAs and network-LSAs
 2422:      The entire routing table must be recalculated, starting with
 2423:      the shortest path calculations for each area (not just the
 2424:      area whose link-state database has changed). 
 2425:   */
 2426:   if (IS_LSA_SELF (new))
 2427:     {
 2428:       /* We supposed that when LSA is originated by us, we pass the int
 2429: 	 for which it was originated. If LSA was received by flooding,
 2430: 	 the RECEIVED flag is set, so we do not link the LSA to the int. */
 2431:       if (CHECK_FLAG (new->flags, OSPF_LSA_RECEIVED))
 2432: 	return new; /* ignore stale LSA */
 2433: 
 2434:       ospf_lsa_unlock (&oi->network_lsa_self);
 2435:       oi->network_lsa_self = ospf_lsa_lock (new);
 2436:       ospf_refresher_register_lsa (ospf, new);
 2437:     }
 2438:   if (rt_recalc)
 2439:     ospf_spf_calculate_schedule (ospf);
 2440: 
 2441:   return new;
 2442: }
 2443: 
 2444: /* Install summary-LSA to an area. */
 2445: static struct ospf_lsa *
 2446: ospf_summary_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
 2447: 			  int rt_recalc)
 2448: {
 2449:   if (rt_recalc && !IS_LSA_SELF (new))
 2450:     {
 2451:       /* RFC 2328 Section 13.2 Summary-LSAs
 2452: 	 The best route to the destination described by the summary-
 2453: 	 LSA must be recalculated (see Section 16.5).  If this
 2454: 	 destination is an AS boundary router, it may also be
 2455: 	 necessary to re-examine all the AS-external-LSAs.
 2456:       */
 2457: 
 2458: #if 0
 2459:       /* This doesn't exist yet... */
 2460:       ospf_summary_incremental_update(new); */
 2461: #else /* #if 0 */
 2462:       ospf_spf_calculate_schedule (ospf);
 2463: #endif /* #if 0 */
 2464:  
 2465:       if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
 2466: 	zlog_debug ("ospf_summary_lsa_install(): SPF scheduled");
 2467:     }
 2468: 
 2469:   if (IS_LSA_SELF (new))
 2470:     ospf_refresher_register_lsa (ospf, new);
 2471: 
 2472:   return new;
 2473: }
 2474: 
 2475: /* Install ASBR-summary-LSA to an area. */
 2476: static struct ospf_lsa *
 2477: ospf_summary_asbr_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
 2478: 			       int rt_recalc)
 2479: {
 2480:   if (rt_recalc && !IS_LSA_SELF (new))
 2481:     {
 2482:       /* RFC 2328 Section 13.2 Summary-LSAs
 2483: 	 The best route to the destination described by the summary-
 2484: 	 LSA must be recalculated (see Section 16.5).  If this
 2485: 	 destination is an AS boundary router, it may also be
 2486: 	 necessary to re-examine all the AS-external-LSAs.
 2487:       */
 2488: #if 0
 2489:       /* These don't exist yet... */
 2490:       ospf_summary_incremental_update(new);
 2491:       /* Isn't this done by the above call? 
 2492: 	 - RFC 2328 Section 16.5 implies it should be */
 2493:       /* ospf_ase_calculate_schedule(); */
 2494: #else  /* #if 0 */
 2495:       ospf_spf_calculate_schedule (ospf);
 2496: #endif /* #if 0 */
 2497:     }
 2498: 
 2499:   /* register LSA to refresh-list. */
 2500:   if (IS_LSA_SELF (new))
 2501:     ospf_refresher_register_lsa (ospf, new);
 2502: 
 2503:   return new;
 2504: }
 2505: 
 2506: /* Install AS-external-LSA. */
 2507: static struct ospf_lsa *
 2508: ospf_external_lsa_install (struct ospf *ospf, struct ospf_lsa *new,
 2509: 			   int rt_recalc)
 2510: {
 2511:   ospf_ase_register_external_lsa (new, ospf);
 2512:   /* If LSA is not self-originated, calculate an external route. */
 2513:   if (rt_recalc)
 2514:     {
 2515:       /* RFC 2328 Section 13.2 AS-external-LSAs
 2516:             The best route to the destination described by the AS-
 2517:             external-LSA must be recalculated (see Section 16.6).
 2518:       */
 2519: 
 2520:       if (!IS_LSA_SELF (new))
 2521:         ospf_ase_incremental_update (ospf, new);
 2522:     }
 2523: 
 2524:   if (new->data->type == OSPF_AS_NSSA_LSA)
 2525:     {
 2526:       /* There is no point to register selforiginate Type-7 LSA for
 2527:        * refreshing. We rely on refreshing Type-5 LSA's 
 2528:        */
 2529:       if (IS_LSA_SELF (new))
 2530:         return new;
 2531:       else
 2532:         {
 2533:           /* Try refresh type-5 translated LSA for this LSA, if one exists.
 2534:            * New translations will be taken care of by the abr_task.
 2535:            */ 
 2536:           ospf_translated_nssa_refresh (ospf, new, NULL);
 2537:         }
 2538:     }
 2539: 
 2540:   /* Register self-originated LSA to refresh queue. 
 2541:    * Leave Translated LSAs alone if NSSA is enabled
 2542:    */
 2543:   if (IS_LSA_SELF (new) && !CHECK_FLAG (new->flags, OSPF_LSA_LOCAL_XLT ) )
 2544:     ospf_refresher_register_lsa (ospf, new);
 2545: 
 2546:   return new;
 2547: }
 2548: 
 2549: void
 2550: ospf_discard_from_db (struct ospf *ospf,
 2551: 		      struct ospf_lsdb *lsdb, struct ospf_lsa *lsa)
 2552: {
 2553:   struct ospf_lsa *old;
 2554:   
 2555:   if (!lsdb)
 2556:     {
 2557:       zlog_warn ("%s: Called with NULL lsdb!", __func__);
 2558:       if (!lsa)
 2559:         zlog_warn ("%s: and NULL LSA!", __func__);
 2560:       else
 2561:         zlog_warn ("LSA[Type%d:%s]: not associated with LSDB!",
 2562:                    lsa->data->type, inet_ntoa (lsa->data->id));
 2563:       return;
 2564:     }
 2565:   
 2566:   old = ospf_lsdb_lookup (lsdb, lsa);
 2567: 
 2568:   if (!old)
 2569:     return;
 2570: 
 2571:   if (old->refresh_list >= 0)
 2572:     ospf_refresher_unregister_lsa (ospf, old);
 2573: 
 2574:   switch (old->data->type)
 2575:     {
 2576:     case OSPF_AS_EXTERNAL_LSA:
 2577:       ospf_ase_unregister_external_lsa (old, ospf);
 2578:       ospf_ls_retransmit_delete_nbr_as (ospf, old);
 2579:       break;
 2580: #ifdef HAVE_OPAQUE_LSA
 2581:     case OSPF_OPAQUE_AS_LSA:
 2582:       ospf_ls_retransmit_delete_nbr_as (ospf, old);
 2583:       break;
 2584: #endif /* HAVE_OPAQUE_LSA */
 2585:     case OSPF_AS_NSSA_LSA:
 2586:       ospf_ls_retransmit_delete_nbr_area (old->area, old);
 2587:       ospf_ase_unregister_external_lsa (old, ospf);
 2588:       break;
 2589:     default:
 2590:       ospf_ls_retransmit_delete_nbr_area (old->area, old);
 2591:       break;
 2592:     }
 2593: 
 2594:   ospf_lsa_maxage_delete (ospf, old);
 2595:   ospf_lsa_discard (old);
 2596: }
 2597: 
 2598: struct ospf_lsa *
 2599: ospf_lsa_install (struct ospf *ospf, struct ospf_interface *oi,
 2600: 		  struct ospf_lsa *lsa)
 2601: {
 2602:   struct ospf_lsa *new = NULL;
 2603:   struct ospf_lsa *old = NULL;
 2604:   struct ospf_lsdb *lsdb = NULL;
 2605:   int rt_recalc;
 2606: 
 2607:   /* Set LSDB. */
 2608:   switch (lsa->data->type)
 2609:     {
 2610:       /* kevinm */
 2611:     case OSPF_AS_NSSA_LSA:
 2612:       if (lsa->area)
 2613: 	lsdb = lsa->area->lsdb;
 2614:       else
 2615: 	lsdb = ospf->lsdb;
 2616:       break;
 2617:     case OSPF_AS_EXTERNAL_LSA:
 2618: #ifdef HAVE_OPAQUE_LSA
 2619:     case OSPF_OPAQUE_AS_LSA:
 2620: #endif /* HAVE_OPAQUE_LSA */
 2621:       lsdb = ospf->lsdb;
 2622:       break;
 2623:     default:
 2624:       lsdb = lsa->area->lsdb;
 2625:       break;
 2626:     }
 2627: 
 2628:   assert (lsdb);
 2629: 
 2630:   /*  RFC 2328 13.2.  Installing LSAs in the database
 2631: 
 2632:         Installing a new LSA in the database, either as the result of
 2633:         flooding or a newly self-originated LSA, may cause the OSPF
 2634:         routing table structure to be recalculated.  The contents of the
 2635:         new LSA should be compared to the old instance, if present.  If
 2636:         there is no difference, there is no need to recalculate the
 2637:         routing table. When comparing an LSA to its previous instance,
 2638:         the following are all considered to be differences in contents:
 2639: 
 2640:             o   The LSA's Options field has changed.
 2641: 
 2642:             o   One of the LSA instances has LS age set to MaxAge, and
 2643:                 the other does not.
 2644: 
 2645:             o   The length field in the LSA header has changed.
 2646: 
 2647:             o   The body of the LSA (i.e., anything outside the 20-byte
 2648:                 LSA header) has changed. Note that this excludes changes
 2649:                 in LS Sequence Number and LS Checksum.
 2650: 
 2651:   */
 2652:   /* Look up old LSA and determine if any SPF calculation or incremental
 2653:      update is needed */
 2654:   old = ospf_lsdb_lookup (lsdb, lsa);
 2655: 
 2656:   /* Do comparision and record if recalc needed. */
 2657:   rt_recalc = 0;
 2658:   if (  old == NULL || ospf_lsa_different(old, lsa))
 2659:     rt_recalc = 1;
 2660: 
 2661:   /*
 2662:      Sequence number check (Section 14.1 of rfc 2328)
 2663:      "Premature aging is used when it is time for a self-originated
 2664:       LSA's sequence number field to wrap.  At this point, the current
 2665:       LSA instance (having LS sequence number MaxSequenceNumber) must
 2666:       be prematurely aged and flushed from the routing domain before a
 2667:       new instance with sequence number equal to InitialSequenceNumber
 2668:       can be originated. "
 2669:    */
 2670: 
 2671:   if (ntohl(lsa->data->ls_seqnum) - 1 == OSPF_MAX_SEQUENCE_NUMBER)
 2672:     {
 2673:       if (ospf_lsa_is_self_originated(ospf, lsa))
 2674:         {
 2675:           lsa->data->ls_seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER);
 2676:           
 2677:           if (!IS_LSA_MAXAGE(lsa))
 2678:             lsa->flags |= OSPF_LSA_PREMATURE_AGE;
 2679:           lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
 2680:       	
 2681:           if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
 2682:             {
 2683:       	      zlog_debug ("ospf_lsa_install() Premature Aging "
 2684: 		         "lsa 0x%p, seqnum 0x%x",
 2685: 		         lsa, ntohl(lsa->data->ls_seqnum));
 2686:       	      ospf_lsa_header_dump (lsa->data);
 2687:             }
 2688:         }
 2689:       else
 2690:         {
 2691:           if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 2692:             {
 2693:       	      zlog_debug ("ospf_lsa_install() got an lsa with seq 0x80000000 "
 2694: 		         "that was not self originated. Ignoring\n");
 2695:       	      ospf_lsa_header_dump (lsa->data);
 2696:             }
 2697: 	  return old;
 2698:         }
 2699:     }
 2700: 
 2701:   /* discard old LSA from LSDB */
 2702:   if (old != NULL)
 2703:     ospf_discard_from_db (ospf, lsdb, lsa);
 2704: 
 2705:   /* Calculate Checksum if self-originated?. */
 2706:   if (IS_LSA_SELF (lsa))
 2707:     ospf_lsa_checksum (lsa->data);
 2708: 
 2709:   /* Insert LSA to LSDB. */
 2710:   ospf_lsdb_add (lsdb, lsa);
 2711:   lsa->lsdb = lsdb;
 2712: 
 2713:   /* Do LSA specific installation process. */
 2714:   switch (lsa->data->type)
 2715:     {
 2716:     case OSPF_ROUTER_LSA:
 2717:       new = ospf_router_lsa_install (ospf, lsa, rt_recalc);
 2718:       break;
 2719:     case OSPF_NETWORK_LSA:
 2720:       assert (oi);
 2721:       new = ospf_network_lsa_install (ospf, oi, lsa, rt_recalc);
 2722:       break;
 2723:     case OSPF_SUMMARY_LSA:
 2724:       new = ospf_summary_lsa_install (ospf, lsa, rt_recalc);
 2725:       break;
 2726:     case OSPF_ASBR_SUMMARY_LSA:
 2727:       new = ospf_summary_asbr_lsa_install (ospf, lsa, rt_recalc);
 2728:       break;
 2729:     case OSPF_AS_EXTERNAL_LSA:
 2730:       new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
 2731:       break;
 2732: #ifdef HAVE_OPAQUE_LSA
 2733:     case OSPF_OPAQUE_LINK_LSA:
 2734:       if (IS_LSA_SELF (lsa))
 2735: 	lsa->oi = oi; /* Specify outgoing ospf-interface for this LSA. */
 2736:       else
 2737:         {
 2738:           /* Incoming "oi" for this LSA has set at LSUpd reception. */
 2739:         }
 2740:       /* Fallthrough */
 2741:     case OSPF_OPAQUE_AREA_LSA:
 2742:     case OSPF_OPAQUE_AS_LSA:
 2743:       new = ospf_opaque_lsa_install (lsa, rt_recalc);
 2744:       break;
 2745: #endif /* HAVE_OPAQUE_LSA */
 2746:     case OSPF_AS_NSSA_LSA:
 2747:       new = ospf_external_lsa_install (ospf, lsa, rt_recalc);
 2748:     default: /* type-6,8,9....nothing special */
 2749:       break;
 2750:     }
 2751: 
 2752:   if (new == NULL)
 2753:     return new;  /* Installation failed, cannot proceed further -- endo. */
 2754: 
 2755:   /* Debug logs. */
 2756:   if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
 2757:     {
 2758:       char area_str[INET_ADDRSTRLEN];
 2759: 
 2760:       switch (lsa->data->type)
 2761:         {
 2762:         case OSPF_AS_EXTERNAL_LSA:
 2763: #ifdef HAVE_OPAQUE_LSA
 2764:         case OSPF_OPAQUE_AS_LSA:
 2765: #endif /* HAVE_OPAQUE_LSA */
 2766:         case OSPF_AS_NSSA_LSA:
 2767:           zlog_debug ("LSA[%s]: Install %s",
 2768:                  dump_lsa_key (new),
 2769:                  LOOKUP (ospf_lsa_type_msg, new->data->type));
 2770:           break;
 2771:         default:
 2772: 	  strcpy (area_str, inet_ntoa (new->area->area_id));
 2773:           zlog_debug ("LSA[%s]: Install %s to Area %s",
 2774:                  dump_lsa_key (new),
 2775:                  LOOKUP (ospf_lsa_type_msg, new->data->type), area_str);
 2776:           break;
 2777:         }
 2778:     }
 2779: 
 2780:   /* 
 2781:      If received LSA' ls_age is MaxAge, or lsa is being prematurely aged
 2782:      (it's getting flushed out of the area), set LSA on MaxAge LSA list. 
 2783:    */
 2784:   if (IS_LSA_MAXAGE (new))
 2785:     {
 2786:       if (IS_DEBUG_OSPF (lsa, LSA_INSTALL))
 2787:         zlog_debug ("LSA[Type%d:%s]: Install LSA 0x%p, MaxAge",
 2788:                    new->data->type, 
 2789:                    inet_ntoa (new->data->id), 
 2790:                    lsa);
 2791:       ospf_lsa_maxage (ospf, lsa);
 2792:     }
 2793: 
 2794:   return new;
 2795: }
 2796: 
 2797: 
 2798: static int
 2799: ospf_check_nbr_status (struct ospf *ospf)
 2800: {
 2801:   struct listnode *node, *nnode;
 2802:   struct ospf_interface *oi;
 2803:   
 2804:   for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
 2805:     {
 2806:       struct route_node *rn;
 2807:       struct ospf_neighbor *nbr;
 2808: 
 2809:       if (ospf_if_is_enable (oi))
 2810: 	for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
 2811:           if ((nbr = rn->info) != NULL)
 2812: 	    if (nbr->state == NSM_Exchange || nbr->state == NSM_Loading)
 2813: 	      {
 2814: 		route_unlock_node (rn);
 2815: 		return 0;
 2816: 	      }
 2817:     }
 2818: 
 2819:   return 1;
 2820: }
 2821: 
 2822: 
 2823: 
 2824: static int
 2825: ospf_maxage_lsa_remover (struct thread *thread)
 2826: {
 2827:   struct ospf *ospf = THREAD_ARG (thread);
 2828:   struct ospf_lsa *lsa;
 2829:   struct route_node *rn;
 2830:   int reschedule = 0;
 2831: 
 2832:   ospf->t_maxage = NULL;
 2833: 
 2834:   if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2835:     zlog_debug ("LSA[MaxAge]: remover Start");
 2836: 
 2837:   reschedule = !ospf_check_nbr_status (ospf);
 2838: 
 2839:   if (!reschedule)
 2840:     for (rn = route_top(ospf->maxage_lsa); rn; rn = route_next(rn))
 2841:       {
 2842: 	if ((lsa = rn->info) == NULL)
 2843: 	  {
 2844: 	    continue;
 2845: 	  }
 2846: 
 2847:         if (lsa->retransmit_counter > 0)
 2848:           {
 2849:             reschedule = 1;
 2850:             continue;
 2851:           }
 2852:         
 2853:         /* TODO: maybe convert this function to a work-queue */
 2854:         if (thread_should_yield (thread))
 2855:           OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover, 0);
 2856:           
 2857:         /* Remove LSA from the LSDB */
 2858:         if (IS_LSA_SELF (lsa))
 2859:           if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2860:             zlog_debug ("LSA[Type%d:%s]: LSA 0x%lx is self-originated: ",
 2861:                        lsa->data->type, inet_ntoa (lsa->data->id), (u_long)lsa);
 2862: 
 2863:         if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2864:           zlog_debug ("LSA[Type%d:%s]: MaxAge LSA removed from list",
 2865:                      lsa->data->type, inet_ntoa (lsa->data->id));
 2866: 
 2867: 	if (CHECK_FLAG (lsa->flags, OSPF_LSA_PREMATURE_AGE))
 2868:           {
 2869:             if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2870:               zlog_debug ("originating new lsa for lsa 0x%p\n", lsa);
 2871:             ospf_lsa_refresh (ospf, lsa);
 2872:           }
 2873: 
 2874: 	/* Remove from lsdb. */
 2875: 	if (lsa->lsdb)
 2876: 	  {
 2877: 	    ospf_discard_from_db (ospf, lsa->lsdb, lsa);
 2878: 	    ospf_lsdb_delete (lsa->lsdb, lsa);
 2879:           }
 2880:         else
 2881:           zlog_warn ("%s: LSA[Type%d:%s]: No associated LSDB!", __func__,
 2882:                      lsa->data->type, inet_ntoa (lsa->data->id));
 2883:       }
 2884: 
 2885:   /*    A MaxAge LSA must be removed immediately from the router's link
 2886:         state database as soon as both a) it is no longer contained on any
 2887:         neighbor Link state retransmission lists and b) none of the router's
 2888:         neighbors are in states Exchange or Loading. */
 2889:   if (reschedule)
 2890:     OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover,
 2891:                    ospf->maxage_delay);
 2892: 
 2893:   return 0;
 2894: }
 2895: 
 2896: void
 2897: ospf_lsa_maxage_delete (struct ospf *ospf, struct ospf_lsa *lsa)
 2898: {
 2899:   struct route_node *rn;
 2900:   struct prefix_ls lsa_prefix;
 2901: 
 2902:   ls_prefix_set (&lsa_prefix, lsa);
 2903: 
 2904:   if ((rn = route_node_lookup(ospf->maxage_lsa,
 2905: 			      (struct prefix *)&lsa_prefix)))
 2906:     {
 2907:       if (rn->info == lsa)
 2908: 	{
 2909: 	  UNSET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
 2910: 	  ospf_lsa_unlock (&lsa); /* maxage_lsa */
 2911: 	  rn->info = NULL;
 2912: 	  route_unlock_node (rn); /* route_node_lookup */
 2913: 	}
 2914: 	  route_unlock_node (rn); /* route_node_lookup */
 2915:     }
 2916: }
 2917: 
 2918: /* Add LSA onto the MaxAge list, and schedule for removal.
 2919:  * This does *not* lead to the LSA being flooded, that must be taken
 2920:  * care of elsewhere, see, e.g., ospf_lsa_flush* (which are callers of this
 2921:  * function).
 2922:  */
 2923: void
 2924: ospf_lsa_maxage (struct ospf *ospf, struct ospf_lsa *lsa)
 2925: {
 2926:   struct prefix_ls lsa_prefix;
 2927:   struct route_node *rn;
 2928: 
 2929:   /* When we saw a MaxAge LSA flooded to us, we put it on the list
 2930:      and schedule the MaxAge LSA remover. */
 2931:   if (CHECK_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE))
 2932:     {
 2933:       if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2934: 	zlog_debug ("LSA[Type%d:%s]: %p already exists on MaxAge LSA list",
 2935: 		   lsa->data->type, inet_ntoa (lsa->data->id), lsa);
 2936:       return;
 2937:     }
 2938: 
 2939:   ls_prefix_set (&lsa_prefix, lsa);
 2940:   if ((rn = route_node_get (ospf->maxage_lsa,
 2941: 			    (struct prefix *)&lsa_prefix)) != NULL)
 2942:     {
 2943:       if (rn->info != NULL)
 2944: 	{
 2945: 	  route_unlock_node (rn);
 2946: 	}
 2947:       else
 2948: 	{
 2949: 	  rn->info = ospf_lsa_lock(lsa);
 2950: 	  SET_FLAG(lsa->flags, OSPF_LSA_IN_MAXAGE);
 2951: 	}
 2952:     }
 2953:   else
 2954:     {
 2955:       zlog_err("Unable to allocate memory for maxage lsa\n");
 2956:       assert(0);
 2957:     }
 2958: 
 2959:   if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2960:     zlog_debug ("LSA[%s]: MaxAge LSA remover scheduled.", dump_lsa_key (lsa));
 2961: 
 2962:   OSPF_TIMER_ON (ospf->t_maxage, ospf_maxage_lsa_remover,
 2963:                  ospf->maxage_delay);
 2964: }
 2965: 
 2966: static int
 2967: ospf_lsa_maxage_walker_remover (struct ospf *ospf, struct ospf_lsa *lsa)
 2968: {
 2969:   /* Stay away from any Local Translated Type-7 LSAs */
 2970:   if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
 2971:     return 0;
 2972: 
 2973:   if (IS_LSA_MAXAGE (lsa))
 2974:     /* Self-originated LSAs should NOT time-out instead,
 2975:        they're flushed and submitted to the max_age list explicitly. */
 2976:     if (!ospf_lsa_is_self_originated (ospf, lsa))
 2977:       {
 2978: 	if (IS_DEBUG_OSPF (lsa, LSA_FLOODING))
 2979: 	  zlog_debug("LSA[%s]: is MaxAge", dump_lsa_key (lsa));
 2980: 
 2981:         switch (lsa->data->type)
 2982:           {
 2983: #ifdef HAVE_OPAQUE_LSA
 2984:           case OSPF_OPAQUE_LINK_LSA:
 2985:           case OSPF_OPAQUE_AREA_LSA:
 2986:           case OSPF_OPAQUE_AS_LSA:
 2987:             /*
 2988:              * As a general rule, whenever network topology has changed
 2989:              * (due to an LSA removal in this case), routing recalculation
 2990:              * should be triggered. However, this is not true for opaque
 2991:              * LSAs. Even if an opaque LSA instance is going to be removed
 2992:              * from the routing domain, it does not mean a change in network
 2993:              * topology, and thus, routing recalculation is not needed here.
 2994:              */
 2995:             break;
 2996: #endif /* HAVE_OPAQUE_LSA */
 2997:           case OSPF_AS_EXTERNAL_LSA:
 2998:           case OSPF_AS_NSSA_LSA:
 2999: 	    ospf_ase_incremental_update (ospf, lsa);
 3000:             break;
 3001:           default:
 3002: 	    ospf_spf_calculate_schedule (ospf);
 3003:             break;
 3004:           }
 3005: 	ospf_lsa_maxage (ospf, lsa);
 3006:       }
 3007: 
 3008:   if (IS_LSA_MAXAGE (lsa) && !ospf_lsa_is_self_originated (ospf, lsa))
 3009:     if (LS_AGE (lsa) > OSPF_LSA_MAXAGE + 30)
 3010:       printf ("Eek! Shouldn't happen!\n");
 3011: 
 3012:   return 0;
 3013: }
 3014: 
 3015: /* Periodical check of MaxAge LSA. */
 3016: int
 3017: ospf_lsa_maxage_walker (struct thread *thread)
 3018: {
 3019:   struct ospf *ospf = THREAD_ARG (thread);
 3020:   struct route_node *rn;
 3021:   struct ospf_lsa *lsa;
 3022:   struct ospf_area *area;
 3023:   struct listnode *node, *nnode;
 3024: 
 3025:   ospf->t_maxage_walker = NULL;
 3026: 
 3027:   for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
 3028:     {
 3029:       LSDB_LOOP (ROUTER_LSDB (area), rn, lsa)
 3030: 	ospf_lsa_maxage_walker_remover (ospf, lsa);
 3031:       LSDB_LOOP (NETWORK_LSDB (area), rn, lsa)
 3032: 	ospf_lsa_maxage_walker_remover (ospf, lsa);
 3033:       LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
 3034: 	ospf_lsa_maxage_walker_remover (ospf, lsa);
 3035:       LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
 3036: 	ospf_lsa_maxage_walker_remover (ospf, lsa);
 3037: #ifdef HAVE_OPAQUE_LSA
 3038:       LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
 3039: 	ospf_lsa_maxage_walker_remover (ospf, lsa);
 3040:       LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
 3041: 	ospf_lsa_maxage_walker_remover (ospf, lsa);
 3042: #endif /* HAVE_OPAQUE_LSA */
 3043:       LSDB_LOOP (NSSA_LSDB (area), rn, lsa)
 3044:         ospf_lsa_maxage_walker_remover (ospf, lsa);
 3045:     }
 3046: 
 3047:   /* for AS-external-LSAs. */
 3048:   if (ospf->lsdb)
 3049:     {
 3050:       LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
 3051: 	ospf_lsa_maxage_walker_remover (ospf, lsa);
 3052: #ifdef HAVE_OPAQUE_LSA
 3053:       LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
 3054: 	ospf_lsa_maxage_walker_remover (ospf, lsa);
 3055: #endif /* HAVE_OPAQUE_LSA */
 3056:     }
 3057: 
 3058:   OSPF_TIMER_ON (ospf->t_maxage_walker, ospf_lsa_maxage_walker,
 3059: 		 OSPF_LSA_MAXAGE_CHECK_INTERVAL);
 3060:   return 0;
 3061: }
 3062: 
 3063: struct ospf_lsa *
 3064: ospf_lsa_lookup_by_prefix (struct ospf_lsdb *lsdb, u_char type,
 3065: 			   struct prefix_ipv4 *p, struct in_addr router_id)
 3066: {
 3067:   struct ospf_lsa *lsa;
 3068:   struct in_addr mask, id;
 3069:   struct lsa_header_mask
 3070:   {
 3071:     struct lsa_header header;
 3072:     struct in_addr mask;
 3073:   } *hmask;
 3074: 
 3075:   lsa = ospf_lsdb_lookup_by_id (lsdb, type, p->prefix, router_id);
 3076:   if (lsa == NULL)
 3077:     return NULL;
 3078: 
 3079:   masklen2ip (p->prefixlen, &mask);
 3080: 
 3081:   hmask = (struct lsa_header_mask *) lsa->data;
 3082: 
 3083:   if (mask.s_addr != hmask->mask.s_addr)
 3084:     {
 3085:       id.s_addr = p->prefix.s_addr | (~mask.s_addr);
 3086:       lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, router_id);
 3087:       if (!lsa)
 3088:         return NULL;
 3089:     }
 3090: 
 3091:   return lsa;
 3092: }
 3093: 
 3094: struct ospf_lsa *
 3095: ospf_lsa_lookup (struct ospf_area *area, u_int32_t type,
 3096:                  struct in_addr id, struct in_addr adv_router)
 3097: {
 3098:   struct ospf *ospf = ospf_lookup();
 3099:   assert(ospf);
 3100: 
 3101:   switch (type)
 3102:     {
 3103:     case OSPF_ROUTER_LSA:
 3104:     case OSPF_NETWORK_LSA:
 3105:     case OSPF_SUMMARY_LSA:
 3106:     case OSPF_ASBR_SUMMARY_LSA:
 3107:     case OSPF_AS_NSSA_LSA:
 3108: #ifdef HAVE_OPAQUE_LSA
 3109:     case OSPF_OPAQUE_LINK_LSA:
 3110:     case OSPF_OPAQUE_AREA_LSA:
 3111: #endif /* HAVE_OPAQUE_LSA */
 3112:       return ospf_lsdb_lookup_by_id (area->lsdb, type, id, adv_router);
 3113:     case OSPF_AS_EXTERNAL_LSA:
 3114: #ifdef HAVE_OPAQUE_LSA
 3115:     case OSPF_OPAQUE_AS_LSA:
 3116: #endif /* HAVE_OPAQUE_LSA */
 3117:       return ospf_lsdb_lookup_by_id (ospf->lsdb, type, id, adv_router);
 3118:     default:
 3119:       break;
 3120:     }
 3121: 
 3122:   return NULL;
 3123: }
 3124: 
 3125: struct ospf_lsa *
 3126: ospf_lsa_lookup_by_id (struct ospf_area *area, u_int32_t type, 
 3127:                        struct in_addr id)
 3128: {
 3129:   struct ospf_lsa *lsa;
 3130:   struct route_node *rn;
 3131: 
 3132:   switch (type)
 3133:     {
 3134:     case OSPF_ROUTER_LSA:
 3135:       return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
 3136:     case OSPF_NETWORK_LSA:
 3137:       for (rn = route_top (NETWORK_LSDB (area)); rn; rn = route_next (rn))
 3138: 	if ((lsa = rn->info))
 3139: 	  if (IPV4_ADDR_SAME (&lsa->data->id, &id))
 3140: 	    {
 3141: 	      route_unlock_node (rn);
 3142: 	      return lsa;
 3143: 	    }
 3144:       break;
 3145:     case OSPF_SUMMARY_LSA:
 3146:     case OSPF_ASBR_SUMMARY_LSA:
 3147:       /* Currently not used. */
 3148:       assert (1);
 3149:       return ospf_lsdb_lookup_by_id (area->lsdb, type, id, id);
 3150:     case OSPF_AS_EXTERNAL_LSA:
 3151:     case OSPF_AS_NSSA_LSA:
 3152: #ifdef HAVE_OPAQUE_LSA
 3153:     case OSPF_OPAQUE_LINK_LSA:
 3154:     case OSPF_OPAQUE_AREA_LSA:
 3155:     case OSPF_OPAQUE_AS_LSA:
 3156:       /* Currently not used. */
 3157:       break;
 3158: #endif /* HAVE_OPAQUE_LSA */
 3159:     default:
 3160:       break;
 3161:     }
 3162: 
 3163:   return NULL;
 3164: }
 3165: 
 3166: struct ospf_lsa *
 3167: ospf_lsa_lookup_by_header (struct ospf_area *area, struct lsa_header *lsah)
 3168: {
 3169:   struct ospf_lsa *match;
 3170: 
 3171: #ifdef HAVE_OPAQUE_LSA
 3172:   /*
 3173:    * Strictly speaking, the LSA-ID field for Opaque-LSAs (type-9/10/11)
 3174:    * is redefined to have two subfields; opaque-type and opaque-id.
 3175:    * However, it is harmless to treat the two sub fields together, as if
 3176:    * they two were forming a unique LSA-ID.
 3177:    */
 3178: #endif /* HAVE_OPAQUE_LSA */
 3179: 
 3180:   match = ospf_lsa_lookup (area, lsah->type, lsah->id, lsah->adv_router);
 3181: 
 3182:   if (match == NULL)
 3183:     if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
 3184:       zlog_debug ("LSA[Type%d:%s]: Lookup by header, NO MATCH",
 3185: 		 lsah->type, inet_ntoa (lsah->id));
 3186: 
 3187:   return match;
 3188: }
 3189: 
 3190: /* return +n, l1 is more recent.
 3191:    return -n, l2 is more recent.
 3192:    return 0, l1 and l2 is identical. */
 3193: int
 3194: ospf_lsa_more_recent (struct ospf_lsa *l1, struct ospf_lsa *l2)
 3195: {
 3196:   int r;
 3197:   int x, y;
 3198: 
 3199:   if (l1 == NULL && l2 == NULL)
 3200:     return 0;
 3201:   if (l1 == NULL)
 3202:     return -1;
 3203:   if (l2 == NULL)
 3204:     return 1;
 3205: 
 3206:   /* compare LS sequence number. */
 3207:   x = (int) ntohl (l1->data->ls_seqnum);
 3208:   y = (int) ntohl (l2->data->ls_seqnum);
 3209:   if (x > y)
 3210:     return 1;
 3211:   if (x < y)
 3212:     return -1;
 3213: 
 3214:   /* compare LS checksum. */
 3215:   r = ntohs (l1->data->checksum) - ntohs (l2->data->checksum);
 3216:   if (r)
 3217:     return r;
 3218: 
 3219:   /* compare LS age. */
 3220:   if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
 3221:     return 1;
 3222:   else if (!IS_LSA_MAXAGE (l1) && IS_LSA_MAXAGE (l2))
 3223:     return -1;
 3224: 
 3225:   /* compare LS age with MaxAgeDiff. */
 3226:   if (LS_AGE (l1) - LS_AGE (l2) > OSPF_LSA_MAXAGE_DIFF)
 3227:     return -1;
 3228:   else if (LS_AGE (l2) - LS_AGE (l1) > OSPF_LSA_MAXAGE_DIFF)
 3229:     return 1;
 3230: 
 3231:   /* LSAs are identical. */
 3232:   return 0;
 3233: }
 3234: 
 3235: /* If two LSAs are different, return 1, otherwise return 0. */
 3236: int
 3237: ospf_lsa_different (struct ospf_lsa *l1, struct ospf_lsa *l2)
 3238: {
 3239:   char *p1, *p2;
 3240:   assert (l1);
 3241:   assert (l2);
 3242:   assert (l1->data);
 3243:   assert (l2->data);
 3244: 
 3245:   if (l1->data->options != l2->data->options)
 3246:     return 1;
 3247: 
 3248:   if (IS_LSA_MAXAGE (l1) && !IS_LSA_MAXAGE (l2))
 3249:     return 1;
 3250: 
 3251:   if (IS_LSA_MAXAGE (l2) && !IS_LSA_MAXAGE (l1))
 3252:     return 1;
 3253: 
 3254:   if (l1->data->length != l2->data->length)
 3255:     return 1;
 3256: 
 3257:   if (l1->data->length ==  0)
 3258:     return 1;
 3259: 
 3260:   if (CHECK_FLAG ((l1->flags ^ l2->flags), OSPF_LSA_RECEIVED))
 3261:     return 1; /* May be a stale LSA in the LSBD */
 3262: 
 3263:   assert ( ntohs(l1->data->length) > OSPF_LSA_HEADER_SIZE);
 3264: 
 3265:   p1 = (char *) l1->data;
 3266:   p2 = (char *) l2->data;
 3267: 
 3268:   if (memcmp (p1 + OSPF_LSA_HEADER_SIZE, p2 + OSPF_LSA_HEADER_SIZE,
 3269:               ntohs( l1->data->length ) - OSPF_LSA_HEADER_SIZE) != 0)
 3270:     return 1;
 3271: 
 3272:   return 0;
 3273: }
 3274: 
 3275: #ifdef ORIGINAL_CODING
 3276: void
 3277: ospf_lsa_flush_self_originated (struct ospf_neighbor *nbr,
 3278:                                 struct ospf_lsa *self,
 3279:                                 struct ospf_lsa *new)
 3280: {
 3281:   u_int32_t seqnum;
 3282: 
 3283:   /* Adjust LS Sequence Number. */
 3284:   seqnum = ntohl (new->data->ls_seqnum) + 1;
 3285:   self->data->ls_seqnum = htonl (seqnum);
 3286: 
 3287:   /* Recalculate LSA checksum. */
 3288:   ospf_lsa_checksum (self->data);
 3289: 
 3290:   /* Reflooding LSA. */
 3291:   /*  RFC2328  Section 13.3
 3292: 	    On non-broadcast networks, separate	Link State Update
 3293: 	    packets must be sent, as unicasts, to each adjacent	neighbor
 3294: 	    (i.e., those in state Exchange or greater).	 The destination
 3295: 	    IP addresses for these packets are the neighbors' IP
 3296: 	    addresses.   */
 3297:   if (nbr->oi->type == OSPF_IFTYPE_NBMA)
 3298:     {
 3299:       struct route_node *rn;
 3300:       struct ospf_neighbor *onbr;
 3301: 
 3302:       for (rn = route_top (nbr->oi->nbrs); rn; rn = route_next (rn))
 3303: 	if ((onbr = rn->info) != NULL)
 3304: 	  if (onbr != nbr->oi->nbr_self && onbr->status >= NSM_Exchange)
 3305: 	    ospf_ls_upd_send_lsa (onbr, self, OSPF_SEND_PACKET_DIRECT);
 3306:     }
 3307:   else
 3308:   ospf_ls_upd_send_lsa (nbr, self, OSPF_SEND_PACKET_INDIRECT);
 3309: 
 3310:   if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 3311:     zlog_debug ("LSA[Type%d:%s]: Flush self-originated LSA",
 3312: 	       self->data->type, inet_ntoa (self->data->id));
 3313: }
 3314: #else /* ORIGINAL_CODING */
 3315: static int
 3316: ospf_lsa_flush_schedule (struct ospf *ospf, struct ospf_lsa *lsa)
 3317: {
 3318:   if (lsa == NULL || !IS_LSA_SELF (lsa))
 3319:     return 0;
 3320: 
 3321:   if (IS_DEBUG_OSPF_EVENT)
 3322:     zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH", lsa->data->type, inet_ntoa (lsa->data->id));
 3323: 
 3324:   /* Force given lsa's age to MaxAge. */
 3325:   lsa->data->ls_age = htons (OSPF_LSA_MAXAGE);
 3326: 
 3327:   switch (lsa->data->type)
 3328:     {
 3329: #ifdef HAVE_OPAQUE_LSA
 3330:     /* Opaque wants to be notified of flushes */
 3331:     case OSPF_OPAQUE_LINK_LSA:
 3332:     case OSPF_OPAQUE_AREA_LSA:
 3333:     case OSPF_OPAQUE_AS_LSA:
 3334:       ospf_opaque_lsa_refresh (lsa);
 3335:       break;
 3336: #endif /* HAVE_OPAQUE_LSA */
 3337:     default:
 3338:       ospf_refresher_unregister_lsa (ospf, lsa);
 3339:       ospf_lsa_flush (ospf, lsa);
 3340:       break;
 3341:     }
 3342: 
 3343:   return 0;
 3344: }
 3345: 
 3346: void
 3347: ospf_flush_self_originated_lsas_now (struct ospf *ospf)
 3348: {
 3349:   struct listnode *node, *nnode;
 3350:   struct listnode *node2, *nnode2;
 3351:   struct ospf_area *area;
 3352:   struct ospf_interface *oi;
 3353:   struct ospf_lsa *lsa;
 3354:   struct route_node *rn;
 3355:   int need_to_flush_ase = 0;
 3356: 
 3357:   for (ALL_LIST_ELEMENTS (ospf->areas, node, nnode, area))
 3358:     {
 3359:       if ((lsa = area->router_lsa_self) != NULL)
 3360:         {
 3361:           if (IS_DEBUG_OSPF_EVENT)
 3362:             zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
 3363:                         lsa->data->type, inet_ntoa (lsa->data->id));
 3364:           
 3365:           ospf_refresher_unregister_lsa (ospf, lsa);
 3366:           ospf_lsa_flush_area (lsa, area);
 3367:           ospf_lsa_unlock (&area->router_lsa_self);
 3368:           area->router_lsa_self = NULL;
 3369:         }
 3370: 
 3371:       for (ALL_LIST_ELEMENTS (area->oiflist, node2, nnode2, oi))
 3372:         {
 3373:           if ((lsa = oi->network_lsa_self) != NULL
 3374:                &&   oi->state == ISM_DR
 3375:                &&   oi->full_nbrs > 0)
 3376:             {
 3377:               if (IS_DEBUG_OSPF_EVENT)
 3378:                 zlog_debug ("LSA[Type%d:%s]: Schedule self-originated LSA to FLUSH",
 3379:                             lsa->data->type, inet_ntoa (lsa->data->id));
 3380:               
 3381:               ospf_refresher_unregister_lsa (ospf, oi->network_lsa_self);
 3382:               ospf_lsa_flush_area (oi->network_lsa_self, area);
 3383:               ospf_lsa_unlock (&oi->network_lsa_self);
 3384:               oi->network_lsa_self = NULL;
 3385:             }
 3386: 
 3387:           if (oi->type != OSPF_IFTYPE_VIRTUALLINK
 3388:           &&  area->external_routing == OSPF_AREA_DEFAULT)
 3389:             need_to_flush_ase = 1;
 3390:         }
 3391: 
 3392:       LSDB_LOOP (SUMMARY_LSDB (area), rn, lsa)
 3393: 	ospf_lsa_flush_schedule (ospf, lsa);
 3394:       LSDB_LOOP (ASBR_SUMMARY_LSDB (area), rn, lsa)
 3395: 	ospf_lsa_flush_schedule (ospf, lsa);
 3396: #ifdef HAVE_OPAQUE_LSA
 3397:       LSDB_LOOP (OPAQUE_LINK_LSDB (area), rn, lsa)
 3398: 	ospf_lsa_flush_schedule (ospf, lsa);
 3399:       LSDB_LOOP (OPAQUE_AREA_LSDB (area), rn, lsa)
 3400: 	ospf_lsa_flush_schedule (ospf, lsa);
 3401: #endif /* HAVE_OPAQUE_LSA */
 3402:     }
 3403: 
 3404:   if (need_to_flush_ase)
 3405:     {
 3406:       LSDB_LOOP (EXTERNAL_LSDB (ospf), rn, lsa)
 3407: 	ospf_lsa_flush_schedule (ospf, lsa);
 3408: #ifdef HAVE_OPAQUE_LSA
 3409:       LSDB_LOOP (OPAQUE_AS_LSDB (ospf), rn, lsa)
 3410: 	ospf_lsa_flush_schedule (ospf, lsa);
 3411: #endif /* HAVE_OPAQUE_LSA */
 3412:     }
 3413: 
 3414:   /*
 3415:    * Make sure that the MaxAge LSA remover is executed immediately,
 3416:    * without conflicting to other threads.
 3417:    */
 3418:   if (ospf->t_maxage != NULL)
 3419:     {
 3420:       OSPF_TIMER_OFF (ospf->t_maxage);
 3421:       thread_execute (master, ospf_maxage_lsa_remover, ospf, 0);
 3422:     }
 3423: 
 3424:   return;
 3425: }
 3426: #endif /* ORIGINAL_CODING */
 3427: 
 3428: /* If there is self-originated LSA, then return 1, otherwise return 0. */
 3429: /* An interface-independent version of ospf_lsa_is_self_originated */
 3430: int 
 3431: ospf_lsa_is_self_originated (struct ospf *ospf, struct ospf_lsa *lsa)
 3432: {
 3433:   struct listnode *node;
 3434:   struct ospf_interface *oi;
 3435: 
 3436:   /* This LSA is already checked. */
 3437:   if (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED))
 3438:     return IS_LSA_SELF (lsa);
 3439: 
 3440:   /* Make sure LSA is self-checked. */
 3441:   SET_FLAG (lsa->flags, OSPF_LSA_SELF_CHECKED);
 3442: 
 3443:   /* AdvRouter and Router ID is the same. */
 3444:   if (IPV4_ADDR_SAME (&lsa->data->adv_router, &ospf->router_id))
 3445:     SET_FLAG (lsa->flags, OSPF_LSA_SELF);
 3446: 
 3447:   /* LSA is router-LSA. */
 3448:   else if (lsa->data->type == OSPF_ROUTER_LSA &&
 3449:       IPV4_ADDR_SAME (&lsa->data->id, &ospf->router_id))
 3450:     SET_FLAG (lsa->flags, OSPF_LSA_SELF);
 3451: 
 3452:   /* LSA is network-LSA.  Compare Link ID with all interfaces. */
 3453:   else if (lsa->data->type == OSPF_NETWORK_LSA)
 3454:     for (ALL_LIST_ELEMENTS_RO (ospf->oiflist, node, oi))
 3455:       {
 3456: 	/* Ignore virtual link. */
 3457:         if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
 3458: 	  if (oi->address->family == AF_INET)
 3459: 	    if (IPV4_ADDR_SAME (&lsa->data->id, &oi->address->u.prefix4))
 3460: 	      {
 3461: 		/* to make it easier later */
 3462: 		SET_FLAG (lsa->flags, OSPF_LSA_SELF);
 3463: 		return IS_LSA_SELF (lsa);
 3464: 	      }
 3465:       }
 3466: 
 3467:   return IS_LSA_SELF (lsa);
 3468: }
 3469: 
 3470: /* Get unique Link State ID. */
 3471: struct in_addr
 3472: ospf_lsa_unique_id (struct ospf *ospf,
 3473: 		    struct ospf_lsdb *lsdb, u_char type, struct prefix_ipv4 *p)
 3474: {
 3475:   struct ospf_lsa *lsa;
 3476:   struct in_addr mask, id;
 3477: 
 3478:   id = p->prefix;
 3479: 
 3480:   /* Check existence of LSA instance. */
 3481:   lsa = ospf_lsdb_lookup_by_id (lsdb, type, id, ospf->router_id);
 3482:   if (lsa)
 3483:     {
 3484:       struct as_external_lsa *al = (struct as_external_lsa *) lsa->data;
 3485:       if (ip_masklen (al->mask) == p->prefixlen)
 3486: 	{
 3487: 	  if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 3488: 	    zlog_debug ("ospf_lsa_unique_id(): "
 3489: 		       "Can't get Link State ID for %s/%d",
 3490: 		       inet_ntoa (p->prefix), p->prefixlen);
 3491: 	  /*	  id.s_addr = 0; */
 3492: 	  id.s_addr = 0xffffffff;
 3493: 	  return id;
 3494: 	}
 3495:       /* Masklen differs, then apply wildcard mask to Link State ID. */
 3496:       else
 3497: 	{
 3498: 	  masklen2ip (p->prefixlen, &mask);
 3499: 
 3500: 	  id.s_addr = p->prefix.s_addr | (~mask.s_addr);
 3501: 	  lsa = ospf_lsdb_lookup_by_id (ospf->lsdb, type,
 3502: 				       id, ospf->router_id);
 3503: 	  if (lsa)
 3504: 	    {
 3505: 	      if (IS_DEBUG_OSPF (lsa, LSA_GENERATE))
 3506: 		zlog_debug ("ospf_lsa_unique_id(): "
 3507: 			   "Can't get Link State ID for %s/%d",
 3508: 			   inet_ntoa (p->prefix), p->prefixlen);
 3509: 	      /* 	      id.s_addr = 0; */
 3510: 	      id.s_addr = 0xffffffff;
 3511: 	      return id;
 3512: 	    }
 3513: 	}
 3514:     }
 3515: 
 3516:   return id;
 3517: }
 3518: 
 3519: 
 3520: #define LSA_ACTION_FLOOD_AREA 1
 3521: #define LSA_ACTION_FLUSH_AREA 2
 3522: 
 3523: struct lsa_action
 3524: {
 3525:   u_char action;
 3526:   struct ospf_area *area;
 3527:   struct ospf_lsa *lsa;
 3528: };
 3529: 
 3530: static int
 3531: ospf_lsa_action (struct thread *t)
 3532: {
 3533:   struct lsa_action *data;
 3534: 
 3535:   data = THREAD_ARG (t);
 3536: 
 3537:   if (IS_DEBUG_OSPF (lsa, LSA) == OSPF_DEBUG_LSA)
 3538:     zlog_debug ("LSA[Action]: Performing scheduled LSA action: %d",
 3539: 	       data->action);
 3540: 
 3541:   switch (data->action)
 3542:     {
 3543:     case LSA_ACTION_FLOOD_AREA:
 3544:       ospf_flood_through_area (data->area, NULL, data->lsa);
 3545:       break;
 3546:     case LSA_ACTION_FLUSH_AREA:
 3547:       ospf_lsa_flush_area (data->lsa, data->area);
 3548:       break;
 3549:     }
 3550: 
 3551:   ospf_lsa_unlock (&data->lsa); /* Message */
 3552:   XFREE (MTYPE_OSPF_MESSAGE, data);
 3553:   return 0;
 3554: }
 3555: 
 3556: void
 3557: ospf_schedule_lsa_flood_area (struct ospf_area *area, struct ospf_lsa *lsa)
 3558: {
 3559:   struct lsa_action *data;
 3560: 
 3561:   data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
 3562:   data->action = LSA_ACTION_FLOOD_AREA;
 3563:   data->area = area;
 3564:   data->lsa  = ospf_lsa_lock (lsa); /* Message / Flood area */
 3565: 
 3566:   thread_add_event (master, ospf_lsa_action, data, 0);
 3567: }
 3568: 
 3569: void
 3570: ospf_schedule_lsa_flush_area (struct ospf_area *area, struct ospf_lsa *lsa)
 3571: {
 3572:   struct lsa_action *data;
 3573: 
 3574:   data = XCALLOC (MTYPE_OSPF_MESSAGE, sizeof (struct lsa_action));
 3575:   data->action = LSA_ACTION_FLUSH_AREA;
 3576:   data->area = area;
 3577:   data->lsa  = ospf_lsa_lock (lsa); /* Message / Flush area */
 3578: 
 3579:   thread_add_event (master, ospf_lsa_action, data, 0);
 3580: }
 3581: 
 3582: 
 3583: /* LSA Refreshment functions. */
 3584: struct ospf_lsa *
 3585: ospf_lsa_refresh (struct ospf *ospf, struct ospf_lsa *lsa)
 3586: {
 3587:   struct external_info *ei;
 3588:   struct ospf_lsa *new = NULL;
 3589:   assert (CHECK_FLAG (lsa->flags, OSPF_LSA_SELF));
 3590:   assert (IS_LSA_SELF (lsa));
 3591:   assert (lsa->lock > 0);
 3592: 
 3593:   switch (lsa->data->type)
 3594:     {
 3595:       /* Router and Network LSAs are processed differently. */
 3596:     case OSPF_ROUTER_LSA:
 3597:       new = ospf_router_lsa_refresh (lsa);
 3598:       break;
 3599:     case OSPF_NETWORK_LSA: 
 3600:       new = ospf_network_lsa_refresh (lsa);
 3601:       break;
 3602:     case OSPF_SUMMARY_LSA:
 3603:       new = ospf_summary_lsa_refresh (ospf, lsa);
 3604:       break;
 3605:     case OSPF_ASBR_SUMMARY_LSA:
 3606:       new = ospf_summary_asbr_lsa_refresh (ospf, lsa);
 3607:       break;
 3608:     case OSPF_AS_EXTERNAL_LSA:
 3609:       /* Translated from NSSA Type-5s are refreshed when 
 3610:        * from refresh of Type-7 - do not refresh these directly.
 3611:        */
 3612:       if (CHECK_FLAG (lsa->flags, OSPF_LSA_LOCAL_XLT))
 3613:         break;
 3614:       ei = ospf_external_info_check (lsa);
 3615:       if (ei)
 3616:         new = ospf_external_lsa_refresh (ospf, lsa, ei, LSA_REFRESH_FORCE);
 3617:       else
 3618:         ospf_lsa_flush_as (ospf, lsa);
 3619:       break;
 3620: #ifdef HAVE_OPAQUE_LSA
 3621:     case OSPF_OPAQUE_LINK_LSA:
 3622:     case OSPF_OPAQUE_AREA_LSA:
 3623:     case OSPF_OPAQUE_AS_LSA:
 3624:       new = ospf_opaque_lsa_refresh (lsa);
 3625:       break;
 3626: #endif /* HAVE_OPAQUE_LSA */
 3627:     default:
 3628:       break;
 3629:     }
 3630:   return new;
 3631: }
 3632: 
 3633: void
 3634: ospf_refresher_register_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
 3635: {
 3636:   u_int16_t index, current_index;
 3637:   
 3638:   assert (lsa->lock > 0);
 3639:   assert (IS_LSA_SELF (lsa));
 3640: 
 3641:   if (lsa->refresh_list < 0)
 3642:     {
 3643:       int delay;
 3644: 
 3645:       if (LS_AGE (lsa) == 0 &&
 3646: 	  ntohl (lsa->data->ls_seqnum) == OSPF_INITIAL_SEQUENCE_NUMBER)
 3647: 	/* Randomize first update by  OSPF_LS_REFRESH_SHIFT factor */ 
 3648: 	delay = OSPF_LS_REFRESH_SHIFT + (random () % OSPF_LS_REFRESH_TIME);
 3649:       else
 3650: 	/* Randomize another updates by +-OSPF_LS_REFRESH_JITTER factor */
 3651: 	delay = OSPF_LS_REFRESH_TIME - LS_AGE (lsa) - OSPF_LS_REFRESH_JITTER
 3652: 	  + (random () % (2*OSPF_LS_REFRESH_JITTER)); 
 3653: 
 3654:       if (delay < 0)
 3655: 	delay = 0;
 3656: 
 3657:       current_index = ospf->lsa_refresh_queue.index + (quagga_time (NULL)
 3658:                 - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
 3659:       
 3660:       index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY)
 3661: 	      % (OSPF_LSA_REFRESHER_SLOTS);
 3662: 
 3663:       if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
 3664: 	zlog_debug ("LSA[Refresh]: lsa %s with age %d added to index %d",
 3665: 		   inet_ntoa (lsa->data->id), LS_AGE (lsa), index);
 3666:       if (!ospf->lsa_refresh_queue.qs[index])
 3667: 	ospf->lsa_refresh_queue.qs[index] = list_new ();
 3668:       listnode_add (ospf->lsa_refresh_queue.qs[index],
 3669:                     ospf_lsa_lock (lsa)); /* lsa_refresh_queue */
 3670:       lsa->refresh_list = index;
 3671:       if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
 3672:         zlog_debug ("LSA[Refresh:%s]: ospf_refresher_register_lsa(): "
 3673:                    "setting refresh_list on lsa %p (slod %d)", 
 3674:                    inet_ntoa (lsa->data->id), lsa, index);
 3675:     }
 3676: }
 3677: 
 3678: void
 3679: ospf_refresher_unregister_lsa (struct ospf *ospf, struct ospf_lsa *lsa)
 3680: {
 3681:   assert (lsa->lock > 0);
 3682:   assert (IS_LSA_SELF (lsa));
 3683:   if (lsa->refresh_list >= 0)
 3684:     {
 3685:       struct list *refresh_list = ospf->lsa_refresh_queue.qs[lsa->refresh_list];
 3686:       listnode_delete (refresh_list, lsa);
 3687:       if (!listcount (refresh_list))
 3688: 	{
 3689: 	  list_free (refresh_list);
 3690: 	  ospf->lsa_refresh_queue.qs[lsa->refresh_list] = NULL;
 3691: 	}
 3692:       ospf_lsa_unlock (&lsa); /* lsa_refresh_queue */
 3693:       lsa->refresh_list = -1;
 3694:     }
 3695: }
 3696: 
 3697: int
 3698: ospf_lsa_refresh_walker (struct thread *t)
 3699: {
 3700:   struct list *refresh_list;
 3701:   struct listnode *node, *nnode;
 3702:   struct ospf *ospf = THREAD_ARG (t);
 3703:   struct ospf_lsa *lsa;
 3704:   int i;
 3705:   struct list *lsa_to_refresh = list_new ();
 3706: 
 3707:   if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
 3708:     zlog_debug ("LSA[Refresh]:ospf_lsa_refresh_walker(): start");
 3709: 
 3710:   
 3711:   i = ospf->lsa_refresh_queue.index;
 3712:   
 3713:   /* Note: if clock has jumped backwards, then time change could be negative,
 3714:      so we are careful to cast the expression to unsigned before taking
 3715:      modulus. */
 3716:   ospf->lsa_refresh_queue.index =
 3717:    ((unsigned long)(ospf->lsa_refresh_queue.index +
 3718: 		    (quagga_time (NULL) - ospf->lsa_refresher_started)
 3719: 		    / OSPF_LSA_REFRESHER_GRANULARITY))
 3720: 		    % OSPF_LSA_REFRESHER_SLOTS;
 3721: 
 3722:   if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
 3723:     zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): next index %d",
 3724: 	       ospf->lsa_refresh_queue.index);
 3725: 
 3726:   for (;i != ospf->lsa_refresh_queue.index;
 3727:        i = (i + 1) % OSPF_LSA_REFRESHER_SLOTS)
 3728:     {
 3729:       if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
 3730: 	zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): "
 3731: 	           "refresh index %d", i);
 3732: 
 3733:       refresh_list = ospf->lsa_refresh_queue.qs [i];
 3734:       
 3735:       assert (i >= 0);
 3736: 
 3737:       ospf->lsa_refresh_queue.qs [i] = NULL;
 3738: 
 3739:       if (refresh_list)
 3740: 	{
 3741: 	  for (ALL_LIST_ELEMENTS (refresh_list, node, nnode, lsa))
 3742: 	    {
 3743: 	      if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
 3744: 		zlog_debug ("LSA[Refresh:%s]: ospf_lsa_refresh_walker(): "
 3745: 		           "refresh lsa %p (slot %d)", 
 3746: 		           inet_ntoa (lsa->data->id), lsa, i);
 3747: 	      
 3748: 	      assert (lsa->lock > 0);
 3749: 	      list_delete_node (refresh_list, node);
 3750: 	      lsa->refresh_list = -1;
 3751: 	      listnode_add (lsa_to_refresh, lsa);
 3752: 	    }
 3753: 	  list_free (refresh_list);
 3754: 	}
 3755:     }
 3756: 
 3757:   ospf->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
 3758: 					   ospf, ospf->lsa_refresh_interval);
 3759:   ospf->lsa_refresher_started = quagga_time (NULL);
 3760: 
 3761:   for (ALL_LIST_ELEMENTS (lsa_to_refresh, node, nnode, lsa))
 3762:     {
 3763:       ospf_lsa_refresh (ospf, lsa);
 3764:       assert (lsa->lock > 0);
 3765:       ospf_lsa_unlock (&lsa); /* lsa_refresh_queue & temp for lsa_to_refresh*/
 3766:     }
 3767:   
 3768:   list_delete (lsa_to_refresh);
 3769:   
 3770:   if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
 3771:     zlog_debug ("LSA[Refresh]: ospf_lsa_refresh_walker(): end");
 3772:   
 3773:   return 0;
 3774: }
 3775: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>