Annotation of embedaddon/quagga/ospfd/ospf_lsa.c, revision 1.1.1.4

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

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