isisd: merge osr/google-is-is

this is essentially half of a rewrite of isisd. please note that a lot
of things are still broken and isisd is not ready for production use.
diff --git a/isisd/AUTHORS b/isisd/AUTHORS
index d9f98b2..05fc0a5 100644
--- a/isisd/AUTHORS
+++ b/isisd/AUTHORS
@@ -1,3 +1,4 @@
-Sampo Saaristo <sambo@cs.tut.fi>
-Ofer Wald      <ofersf@islands.co.il>
-Hannes Gredler <hannes@gredler.at>
+Sampo Saaristo   <sambo@cs.tut.fi>
+Ofer Wald        <ofersf@islands.co.il>
+Hannes Gredler   <hannes@gredler.at>
+Subbaiah Venkata <svenkata@google.com>
diff --git a/isisd/dict.c b/isisd/dict.c
index a78b82a..bbcb421 100644
--- a/isisd/dict.c
+++ b/isisd/dict.c
@@ -15,17 +15,11 @@
  * contain a copyright notice related to this source.
  */
 
-#include <stdlib.h>
-#include <stddef.h>
 #include "zebra.h"
 #include "zassert.h"
-#define DICT_IMPLEMENTATION
+#include "memory.h"
 #include "dict.h"
 
-#ifdef KAZLIB_RCSID
-static const char rcsid[] = "Id: dict.c,v 1.40.2.7 2000/11/13 01:36:44 kaz";
-#endif
-
 /*
  * These macros provide short convenient names for structure members,
  * which are embellished with dict_ prefixes so that they are
@@ -243,7 +237,7 @@
 
 dict_t *dict_create(dictcount_t maxcount, dict_comp_t comp)
 {
-    dict_t *new = malloc(sizeof *new);
+    dict_t *new = XCALLOC(MTYPE_ISIS_DICT, sizeof(dict_t));
 
     if (new) {
 	new->compare = comp;
@@ -284,7 +278,7 @@
 void dict_destroy(dict_t *dict)
 {
     assert (dict_isempty(dict));
-    free(dict);
+    XFREE(MTYPE_ISIS_DICT, dict);
 }
 
 /*
@@ -307,9 +301,6 @@
 
 void dict_free(dict_t *dict)
 {
-#ifdef KAZLIB_OBSOLESCENT_DEBUG
-    assert ("call to obsolescent function dict_free()" && 0);
-#endif
     dict_free_nodes(dict);
 }
 
@@ -810,7 +801,7 @@
 
 int dict_alloc_insert(dict_t *dict, const void *key, void *data)
 {
-    dnode_t *node = dict->allocnode(dict->context);
+    dnode_t *node = dict->allocnode (dict->context);
 
     if (node) {
 	dnode_init(node, data);
@@ -946,17 +937,17 @@
 
 static dnode_t *dnode_alloc(void *context)
 {
-    return malloc(sizeof *dnode_alloc(NULL));
+    return XCALLOC(MTYPE_ISIS_DICT_NODE, sizeof(dnode_t));
 }
 
 static void dnode_free(dnode_t *node, void *context)
 {
-    free(node);
+    XFREE(MTYPE_ISIS_DICT_NODE, node);
 }
 
 dnode_t *dnode_create(void *data)
 {
-    dnode_t *new = malloc(sizeof *new);
+    dnode_t *new = XCALLOC(MTYPE_ISIS_DICT_NODE, sizeof(dnode_t));
     if (new) {
 	new->data = data;
 	new->parent = NULL;
@@ -978,7 +969,7 @@
 void dnode_destroy(dnode_t *dnode)
 {
     assert (!dnode_is_in_a_dict(dnode));
-    free(dnode);
+    XFREE(MTYPE_ISIS_DICT_NODE, dnode);
 }
 
 void *dnode_get(dnode_t *dnode)
@@ -1232,7 +1223,7 @@
 static char *dupstring(char *str)
 {
     int sz = strlen(str) + 1;
-    char *new = malloc(sz);
+    char *new = XCALLOC(MTYPE_ISIS_TMP, sz);
     if (new)
 	memcpy(new, str, sz);
     return new;
@@ -1347,7 +1338,7 @@
 	"s                      switch to non-functioning allocator\n"
 	"q                      quit";
 
-    for (i = 0; i < sizeof darray / sizeof *darray; i++)
+    for (i = 0; i < 10; i++)
 	dict_init(&darray[i], DICTCOUNT_T_MAX, comparef);
 
     for (;;) {
diff --git a/isisd/dict.h b/isisd/dict.h
index 9395d1c..93edb7d 100644
--- a/isisd/dict.h
+++ b/isisd/dict.h
@@ -22,9 +22,6 @@
 #define DICT_H
 
 #include <limits.h>
-#ifdef KAZLIB_SIDEEFFECT_DEBUG
-#include "sfx.h"
-#endif
 
 /*
  * Blurb for inclusion into C++ translation units
@@ -44,16 +41,12 @@
 typedef enum { dnode_red, dnode_black } dnode_color_t;
 
 typedef struct dnode_t {
-    #if defined(DICT_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
     struct dnode_t *dict_left;
     struct dnode_t *dict_right;
     struct dnode_t *dict_parent;
     dnode_color_t dict_color;
     const void *dict_key;
     void *dict_data;
-    #else
-    int dict_dummy;
-    #endif
 } dnode_t;
 
 typedef int (*dict_comp_t)(const void *, const void *);
@@ -61,7 +54,6 @@
 typedef void (*dnode_free_t)(dnode_t *, void *);
 
 typedef struct dict_t {
-    #if defined(DICT_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
     dnode_t dict_nilnode;
     dictcount_t dict_nodecount;
     dictcount_t dict_maxcount;
@@ -70,20 +62,13 @@
     dnode_free_t dict_freenode;
     void *dict_context;
     int dict_dupes;
-    #else
-    int dict_dummmy;
-    #endif
 } dict_t;
 
 typedef void (*dnode_process_t)(dict_t *, dnode_t *, void *);
 
 typedef struct dict_load_t {
-    #if defined(DICT_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
     dict_t *dict_dictptr;
     dnode_t dict_nilnode;
-    #else
-    int dict_dummmy;
-    #endif
 } dict_load_t;
 
 extern dict_t *dict_create(dictcount_t, dict_comp_t);
@@ -124,18 +109,12 @@
 extern void dict_load_end(dict_load_t *);
 extern void dict_merge(dict_t *, dict_t *);
 
-#if defined(DICT_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
-#ifdef KAZLIB_SIDEEFFECT_DEBUG
-#define dict_isfull(D) (SFX_CHECK(D)->dict_nodecount == (D)->dict_maxcount)
-#else
 #define dict_isfull(D) ((D)->dict_nodecount == (D)->dict_maxcount)
-#endif
 #define dict_count(D) ((D)->dict_nodecount)
 #define dict_isempty(D) ((D)->dict_nodecount == 0)
 #define dnode_get(N) ((N)->dict_data)
 #define dnode_getkey(N) ((N)->dict_key)
 #define dnode_put(N, X) ((N)->dict_data = (X))
-#endif
 
 #ifdef __cplusplus
 }
diff --git a/isisd/isis_adjacency.c b/isisd/isis_adjacency.c
index de34bea..468b0a6 100644
--- a/isisd/isis_adjacency.c
+++ b/isisd/isis_adjacency.c
@@ -36,6 +36,7 @@
 #include "isisd/include-netbsd/iso.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
+#include "isisd/isis_flags.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_circuit.h"
 #include "isisd/isis_adjacency.h"
@@ -43,6 +44,10 @@
 #include "isisd/isis_dr.h"
 #include "isisd/isis_dynhn.h"
 #include "isisd/isis_pdu.h"
+#include "isisd/isis_tlv.h"
+#include "isisd/isis_lsp.h"
+#include "isisd/isis_spf.h"
+#include "isisd/isis_events.h"
 
 extern struct isis *isis;
 
@@ -73,9 +78,9 @@
     }
 
   if (snpa) {
-  memcpy (adj->snpa, snpa, 6);
+    memcpy (adj->snpa, snpa, ETH_ALEN);
   } else {
-      memset (adj->snpa, ' ', 6);
+    memset (adj->snpa, ' ', ETH_ALEN);
   }
 
   adj->circuit = circuit;
@@ -125,37 +130,60 @@
 }
 
 void
-isis_delete_adj (struct isis_adjacency *adj, struct list *adjdb)
+isis_delete_adj (void *arg)
 {
+  struct isis_adjacency *adj = arg;
+
   if (!adj)
     return;
-  /* When we recieve a NULL list, we will know its p2p. */
-  if (adjdb)
-    listnode_delete (adjdb, adj);
 
-  THREAD_OFF (adj->t_expire);
+  THREAD_TIMER_OFF (adj->t_expire);
 
+  /* remove from SPF trees */
+  spftree_area_adj_del (adj->circuit->area, adj);
+
+  if (adj->area_addrs)
+    list_delete (adj->area_addrs);
   if (adj->ipv4_addrs)
     list_delete (adj->ipv4_addrs);
 #ifdef HAVE_IPV6
   if (adj->ipv6_addrs)
     list_delete (adj->ipv6_addrs);
 #endif
-  
+
   XFREE (MTYPE_ISIS_ADJACENCY, adj);
   return;
 }
 
+static const char *
+adj_state2string (int state)
+{
+
+  switch (state)
+    {
+    case ISIS_ADJ_INITIALIZING:
+      return "Initializing";
+    case ISIS_ADJ_UP:
+      return "Up";
+    case ISIS_ADJ_DOWN:
+      return "Down";
+    default:
+      return "Unknown";
+    }
+
+  return NULL;			/* not reached */
+}
+
 void
-isis_adj_state_change (struct isis_adjacency *adj, enum isis_adj_state state,
+isis_adj_state_change (struct isis_adjacency *adj, enum isis_adj_state new_state,
 		       const char *reason)
 {
   int old_state;
-  int level = adj->level;
+  int level;
   struct isis_circuit *circuit;
 
   old_state = adj->adj_state;
-  adj->adj_state = state;
+  adj->adj_state = new_state;
 
   circuit = adj->circuit;
 
@@ -163,42 +191,107 @@
     {
       zlog_debug ("ISIS-Adj (%s): Adjacency state change %d->%d: %s",
 		 circuit->area->area_tag,
-		 old_state, state, reason ? reason : "unspecified");
+		 old_state, new_state, reason ? reason : "unspecified");
+    }
+
+  if (circuit->area->log_adj_changes)
+    {
+      const char *adj_name;
+      struct isis_dynhn *dyn;
+
+      dyn = dynhn_find_by_id (adj->sysid);
+      if (dyn)
+	adj_name = (const char *)dyn->name.name;
+      else
+	adj_name = adj->sysid ? sysid_print (adj->sysid) : "unknown";
+
+      zlog_info ("%%ADJCHANGE: Adjacency to %s (%s) changed from %s to %s, %s",
+		 adj_name,
+		 adj->circuit ? adj->circuit->interface->name : "no circuit",
+		 adj_state2string (old_state),
+		 adj_state2string (new_state),
+		 reason ? reason : "unspecified");
     }
 
   if (circuit->circ_type == CIRCUIT_T_BROADCAST)
     {
-      if (state == ISIS_ADJ_UP)
-	circuit->upadjcount[level - 1]++;
-      if (state == ISIS_ADJ_DOWN)
-	{
-	  listnode_delete (adj->circuit->u.bc.adjdb[level - 1], adj);
-	  circuit->upadjcount[level - 1]--;
-	}
+      for (level = IS_LEVEL_1; level <= IS_LEVEL_2; level++)
+      {
+        if ((adj->level & level) == 0)
+          continue;
+        if (new_state == ISIS_ADJ_UP)
+        {
+          circuit->upadjcount[level - 1]++;
+          isis_event_adjacency_state_change (adj, new_state);
+          /* update counter & timers for debugging purposes */
+          adj->last_flap = time (NULL);
+          adj->flaps++;
+        }
+        else if (new_state == ISIS_ADJ_DOWN)
+        {
+          listnode_delete (circuit->u.bc.adjdb[level - 1], adj);
+          circuit->upadjcount[level - 1]--;
+          if (circuit->upadjcount[level - 1] == 0)
+            {
+              /* Clean lsp_queue when no adj is up. */
+              if (circuit->lsp_queue)
+                list_delete_all_node (circuit->lsp_queue);
+            }
+          isis_event_adjacency_state_change (adj, new_state);
+          isis_delete_adj (adj);
+        }
 
-      list_delete_all_node (circuit->u.bc.lan_neighs[level - 1]);
-      isis_adj_build_neigh_list (circuit->u.bc.adjdb[level - 1],
-				 circuit->u.bc.lan_neighs[level - 1]);
-    }
-  else if (state == ISIS_ADJ_UP)
-    {				/* p2p interface */
-      if (adj->sys_type == ISIS_SYSTYPE_UNKNOWN)
-	send_hello (circuit, 1);
+        if (circuit->u.bc.lan_neighs[level - 1])
+          {
+            list_delete_all_node (circuit->u.bc.lan_neighs[level - 1]);
+            isis_adj_build_neigh_list (circuit->u.bc.adjdb[level - 1],
+                                       circuit->u.bc.lan_neighs[level - 1]);
+          }
 
-      /* update counter & timers for debugging purposes */
-      adj->last_flap = time (NULL);
-      adj->flaps++;
+        /* On adjacency state change send new pseudo LSP if we are the DR */
+        if (circuit->u.bc.is_dr[level - 1])
+          lsp_regenerate_schedule_pseudo (circuit, level);
+      }
+    }
+  else if (circuit->circ_type == CIRCUIT_T_P2P)
+    {
+      for (level = IS_LEVEL_1; level <= IS_LEVEL_2; level++)
+      {
+        if ((adj->level & level) == 0)
+          continue;
+        if (new_state == ISIS_ADJ_UP)
+        {
+          circuit->upadjcount[level - 1]++;
+          isis_event_adjacency_state_change (adj, new_state);
 
-      /* 7.3.17 - going up on P2P -> send CSNP */
-      /* FIXME: yup, I know its wrong... but i will do it! (for now) */
-      send_csnp (circuit, 1);
-      send_csnp (circuit, 2);
+          if (adj->sys_type == ISIS_SYSTYPE_UNKNOWN)
+            send_hello (circuit, level);
+
+          /* update counter & timers for debugging purposes */
+          adj->last_flap = time (NULL);
+          adj->flaps++;
+
+          /* 7.3.17 - going up on P2P -> send CSNP */
+          /* FIXME: yup, I know its wrong... but i will do it! (for now) */
+          send_csnp (circuit, level);
+        }
+        else if (new_state == ISIS_ADJ_DOWN)
+        {
+          if (adj->circuit->u.p2p.neighbor == adj)
+            adj->circuit->u.p2p.neighbor = NULL;
+          circuit->upadjcount[level - 1]--;
+          if (circuit->upadjcount[level - 1] == 0)
+            {
+              /* Clean lsp_queue when no adj is up. */
+              if (circuit->lsp_queue)
+                list_delete_all_node (circuit->lsp_queue);
+            }
+          isis_event_adjacency_state_change (adj, new_state);
+          isis_delete_adj (adj);
+        }
+      }
     }
-  else if (state == ISIS_ADJ_DOWN)
-    {				/* p2p interface */
-      adj->circuit->u.p2p.neighbor = NULL;
-      isis_delete_adj (adj, NULL);
-    }
+
   return;
 }
 
@@ -225,7 +318,7 @@
 	      snpa_print (adj->snpa), adj->level, adj->hold_time);
   if (adj->ipv4_addrs && listcount (adj->ipv4_addrs) > 0)
     {
-      zlog_debug ("IPv4 Addresses:");
+      zlog_debug ("IPv4 Address(es):");
 
       for (ALL_LIST_ELEMENTS_RO (adj->ipv4_addrs, node, ipv4_addr))
         zlog_debug ("%s", inet_ntoa (*ipv4_addr));
@@ -234,7 +327,7 @@
 #ifdef HAVE_IPV6
   if (adj->ipv6_addrs && listcount (adj->ipv6_addrs) > 0)
     {
-      zlog_debug ("IPv6 Addresses:");
+      zlog_debug ("IPv6 Address(es):");
       for (ALL_LIST_ELEMENTS_RO (adj->ipv6_addrs, node, ipv6_addr))
 	{
 	  inet_ntop (AF_INET6, ipv6_addr, (char *)ip6, INET6_ADDRSTRLEN);
@@ -251,14 +344,12 @@
 isis_adj_expire (struct thread *thread)
 {
   struct isis_adjacency *adj;
-  int level;
 
   /*
    * Get the adjacency
    */
   adj = THREAD_ARG (thread);
   assert (adj);
-  level = adj->level;
   adj->t_expire = NULL;
 
   /* trigger the adj expire event */
@@ -267,32 +358,12 @@
   return 0;
 }
 
-static const char *
-adj_state2string (int state)
-{
-
-  switch (state)
-    {
-    case ISIS_ADJ_INITIALIZING:
-      return "Initializing";
-    case ISIS_ADJ_UP:
-      return "Up";
-    case ISIS_ADJ_DOWN:
-      return "Down";
-    default:
-      return "Unknown";
-    }
-
-  return NULL;			/* not reached */
-}
-
 /*
- * show clns/isis neighbor (detail)
+ * show isis neighbor [detail]
  */
-static void
-isis_adj_print_vty2 (struct isis_adjacency *adj, struct vty *vty, char detail)
+void
+isis_adj_print_vty (struct isis_adjacency *adj, struct vty *vty, char detail)
 {
-
 #ifdef HAVE_IPV6
   struct in6_addr *ipv6_addr;
   u_char ip6[INET6_ADDRSTRLEN];
@@ -335,10 +406,11 @@
   if (detail == ISIS_UI_LEVEL_DETAIL)
     {
       level = adj->level;
+      vty_out (vty, "%s", VTY_NEWLINE);
       if (adj->circuit)
-	vty_out (vty, "%s    Interface: %s", VTY_NEWLINE, adj->circuit->interface->name);	/* interface name */
+	vty_out (vty, "    Interface: %s", adj->circuit->interface->name);
       else
-	vty_out (vty, "NULL circuit!%s", VTY_NEWLINE);
+	vty_out (vty, "    Interface: NULL circuit");
       vty_out (vty, ", Level: %u", adj->level);	/* level */
       vty_out (vty, ", State: %s", adj_state2string (adj->adj_state));
       now = time (NULL);
@@ -347,40 +419,54 @@
 		 time2string (adj->last_upd + adj->hold_time - now));
       else
 	vty_out (vty, ", Expires in %s", time2string (adj->hold_time));
-      vty_out (vty, "%s    Adjacency flaps: %u", VTY_NEWLINE, adj->flaps);
+      vty_out (vty, "%s", VTY_NEWLINE);
+      vty_out (vty, "    Adjacency flaps: %u", adj->flaps);
       vty_out (vty, ", Last: %s ago", time2string (now - adj->last_flap));
-      vty_out (vty, "%s    Circuit type: %s",
-	       VTY_NEWLINE, circuit_t2string (adj->circuit_t));
+      vty_out (vty, "%s", VTY_NEWLINE);
+      vty_out (vty, "    Circuit type: %s", circuit_t2string (adj->circuit_t));
       vty_out (vty, ", Speaks: %s", nlpid2string (&adj->nlpids));
-      vty_out (vty, "%s    SNPA: %s", VTY_NEWLINE, snpa_print (adj->snpa));
-      dyn = dynhn_find_by_id (adj->lanid);
-      if (dyn)
-	vty_out (vty, ", LAN id: %s.%02x",
-		 dyn->name.name, adj->lanid[ISIS_SYS_ID_LEN]);
-      else
-	vty_out (vty, ", LAN id: %s.%02x",
-		 sysid_print (adj->lanid), adj->lanid[ISIS_SYS_ID_LEN]);
+      vty_out (vty, "%s", VTY_NEWLINE);
+      vty_out (vty, "    SNPA: %s", snpa_print (adj->snpa));
+      if (adj->circuit->circ_type == CIRCUIT_T_BROADCAST)
+      {
+        dyn = dynhn_find_by_id (adj->lanid);
+        if (dyn)
+          vty_out (vty, ", LAN id: %s.%02x",
+              dyn->name.name, adj->lanid[ISIS_SYS_ID_LEN]);
+        else
+          vty_out (vty, ", LAN id: %s.%02x",
+              sysid_print (adj->lanid), adj->lanid[ISIS_SYS_ID_LEN]);
 
-      vty_out (vty, "%s    Priority: %u",
-	       VTY_NEWLINE, adj->prio[adj->level - 1]);
+        vty_out (vty, "%s", VTY_NEWLINE);
+        vty_out (vty, "    LAN Priority: %u", adj->prio[adj->level - 1]);
 
-      vty_out (vty, ", %s, DIS flaps: %u, Last: %s ago%s",
-	       isis_disflag2string (adj->dis_record[ISIS_LEVELS + level - 1].
-				    dis), adj->dischanges[level - 1],
-	       time2string (now -
-			    (adj->dis_record[ISIS_LEVELS + level - 1].
-			     last_dis_change)), VTY_NEWLINE);
+        vty_out (vty, ", %s, DIS flaps: %u, Last: %s ago",
+            isis_disflag2string (adj->dis_record[ISIS_LEVELS + level - 1].
+              dis), adj->dischanges[level - 1],
+            time2string (now -
+              (adj->dis_record[ISIS_LEVELS + level - 1].
+               last_dis_change)));
+      }
+      vty_out (vty, "%s", VTY_NEWLINE);
 
+      if (adj->area_addrs && listcount (adj->area_addrs) > 0)
+        {
+          struct area_addr *area_addr;
+          vty_out (vty, "    Area Address(es):%s", VTY_NEWLINE);
+          for (ALL_LIST_ELEMENTS_RO (adj->area_addrs, node, area_addr))
+            vty_out (vty, "      %s%s", isonet_print (area_addr->area_addr,
+                     area_addr->addr_len), VTY_NEWLINE);
+        }
       if (adj->ipv4_addrs && listcount (adj->ipv4_addrs) > 0)
 	{
-	  vty_out (vty, "    IPv4 Addresses:%s", VTY_NEWLINE);
+	  vty_out (vty, "    IPv4 Address(es):%s", VTY_NEWLINE);
 	  for (ALL_LIST_ELEMENTS_RO (adj->ipv4_addrs, node, ip_addr))
             vty_out (vty, "      %s%s", inet_ntoa (*ip_addr), VTY_NEWLINE);
 	}
 #ifdef HAVE_IPV6
       if (adj->ipv6_addrs && listcount (adj->ipv6_addrs) > 0)
 	{
-	  vty_out (vty, "    IPv6 Addresses:%s", VTY_NEWLINE);
+	  vty_out (vty, "    IPv6 Address(es):%s", VTY_NEWLINE);
 	  for (ALL_LIST_ELEMENTS_RO (adj->ipv6_addrs, node, ipv6_addr))
 	    {
 	      inet_ntop (AF_INET6, ipv6_addr, (char *)ip6, INET6_ADDRSTRLEN);
@@ -394,53 +480,6 @@
 }
 
 void
-isis_adj_print_vty (struct isis_adjacency *adj, struct vty *vty)
-{
-  isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_BRIEF);
-}
-
-void
-isis_adj_print_vty_detail (struct isis_adjacency *adj, struct vty *vty)
-{
-  isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_DETAIL);
-}
-
-void
-isis_adj_print_vty_extensive (struct isis_adjacency *adj, struct vty *vty)
-{
-  isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_EXTENSIVE);
-}
-
-void
-isis_adj_p2p_print_vty (struct isis_adjacency *adj, struct vty *vty)
-{
-  isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_BRIEF);
-}
-
-void
-isis_adj_p2p_print_vty_detail (struct isis_adjacency *adj, struct vty *vty)
-{
-  isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_DETAIL);
-}
-
-void
-isis_adj_p2p_print_vty_extensive (struct isis_adjacency *adj, struct vty *vty)
-{
-  isis_adj_print_vty2 (adj, vty, ISIS_UI_LEVEL_EXTENSIVE);
-}
-
-void
-isis_adjdb_iterate (struct list *adjdb, void (*func) (struct isis_adjacency *,
-						      void *), void *arg)
-{
-  struct listnode *node, *nnode;
-  struct isis_adjacency *adj;
-
-  for (ALL_LIST_ELEMENTS (adjdb, node, nnode, adj))
-    (*func) (adj, arg);
-}
-
-void
 isis_adj_build_neigh_list (struct list *adjdb, struct list *list)
 {
   struct isis_adjacency *adj;
diff --git a/isisd/isis_adjacency.h b/isisd/isis_adjacency.h
index 99a8bb2..caa3107 100644
--- a/isisd/isis_adjacency.h
+++ b/isisd/isis_adjacency.h
@@ -44,6 +44,7 @@
 
 enum isis_adj_state
 {
+  ISIS_ADJ_UNKNOWN,
   ISIS_ADJ_INITIALIZING,
   ISIS_ADJ_UP,
   ISIS_ADJ_DOWN
@@ -83,8 +84,10 @@
   struct list *area_addrs;		/* areaAdressesOfNeighbour */
   struct nlpids nlpids;			/* protocols spoken ... */
   struct list *ipv4_addrs;
+  struct in_addr router_address;
 #ifdef HAVE_IPV6
   struct list *ipv6_addrs;
+  struct in6_addr router_address6;
 #endif				/* HAVE_IPV6 */
   u_char prio[ISIS_LEVELS];	/* priorityOfNeighbour for DIS */
   int circuit_t;		/* from hello PDU hdr */
@@ -103,25 +106,13 @@
 					     struct list *adjdb);
 struct isis_adjacency *isis_new_adj (u_char * id, u_char * snpa, int level,
 				     struct isis_circuit *circuit);
-void isis_delete_adj (struct isis_adjacency *adj, struct list *adjdb);
+void isis_delete_adj (void *adj);
 void isis_adj_state_change (struct isis_adjacency *adj,
 			    enum isis_adj_state state, const char *reason);
 void isis_adj_print (struct isis_adjacency *adj);
 int isis_adj_expire (struct thread *thread);
-void isis_adj_print_vty (struct isis_adjacency *adj, struct vty *vty);
-void isis_adj_print_vty_detail (struct isis_adjacency *adj, struct vty *vty);
-void isis_adj_print_vty_extensive (struct isis_adjacency *adj,
-				   struct vty *vty);
-void isis_adj_p2p_print_vty (struct isis_adjacency *adj, struct vty *vty);
-void isis_adj_p2p_print_vty_detail (struct isis_adjacency *adj,
-				    struct vty *vty);
-void isis_adj_p2p_print_vty_extensive (struct isis_adjacency *adj,
-				       struct vty *vty);
-
+void isis_adj_print_vty (struct isis_adjacency *adj, struct vty *vty, char detail);
 void isis_adj_build_neigh_list (struct list *adjdb, struct list *list);
 void isis_adj_build_up_list (struct list *adjdb, struct list *list);
-void isis_adjdb_iterate (struct list *adjdb,
-			 void (*func) (struct isis_adjacency *,
-				       void *), void *arg);
 
 #endif /* ISIS_ADJACENCY_H */
diff --git a/isisd/isis_bpf.c b/isisd/isis_bpf.c
index 05f1138..4d5b165 100644
--- a/isisd/isis_bpf.c
+++ b/isisd/isis_bpf.c
@@ -301,7 +301,16 @@
 isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
 {
   struct ether_header *eth;
-  int written;
+  int written, buflen;
+
+  buflen = stream_get_endp (circuit->snd_stream) + LLC_LEN + ETHER_HDR_LEN;
+  if (buflen > sizeof (sock_buff))
+    {
+      zlog_warn ("isis_send_pdu_bcast: sock_buff size %lu is less than "
+		 "output pdu size %d on circuit %s",
+		 sizeof (sock_buff), buflen, circuit->interface->name);
+      return ISIS_WARNING;
+    }
 
   stream_set_getp (circuit->snd_stream, 0);
 
@@ -328,9 +337,7 @@
 	  stream_get_endp (circuit->snd_stream));
 
   /* now we can send this */
-  written = write (circuit->fd, sock_buff,
-		   stream_get_endp (circuit->snd_stream) 
-		    + LLC_LEN + ETHER_HDR_LEN);
+  written = write (circuit->fd, sock_buff, buflen);
 
   return ISIS_OK;
 }
diff --git a/isisd/isis_circuit.c b/isisd/isis_circuit.c
index 99e2bf6..c09c3a2 100644
--- a/isisd/isis_circuit.c
+++ b/isisd/isis_circuit.c
@@ -44,6 +44,7 @@
 #include "isisd/include-netbsd/iso.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
+#include "isisd/isis_flags.h"
 #include "isisd/isis_circuit.h"
 #include "isisd/isis_tlv.h"
 #include "isisd/isis_lsp.h"
@@ -53,18 +54,13 @@
 #include "isisd/isis_constants.h"
 #include "isisd/isis_adjacency.h"
 #include "isisd/isis_dr.h"
-#include "isisd/isis_flags.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_csm.h"
 #include "isisd/isis_events.h"
 
-extern struct thread_master *master;
-extern struct isis *isis;
-
 /*
  * Prototypes.
  */
-void isis_circuit_down(struct isis_circuit *);
 int isis_interface_config_write(struct vty *);
 int isis_if_new_hook(struct interface *);
 int isis_if_delete_hook(struct interface *);
@@ -76,55 +72,63 @@
   int i;
 
   circuit = XCALLOC (MTYPE_ISIS_CIRCUIT, sizeof (struct isis_circuit));
-  if (circuit)
-    {
-      /* set default metrics for circuit */
-      for (i = 0; i < 2; i++)
-	{
-	  circuit->metrics[i].metric_default = DEFAULT_CIRCUIT_METRICS;
-	  circuit->metrics[i].metric_expense = METRICS_UNSUPPORTED;
-	  circuit->metrics[i].metric_error = METRICS_UNSUPPORTED;
-	  circuit->metrics[i].metric_delay = METRICS_UNSUPPORTED;
-	  circuit->te_metric[i] = DEFAULT_CIRCUIT_METRICS;
-	}
-    }
-  else
+  if (circuit == NULL)
     {
       zlog_err ("Can't malloc isis circuit");
       return NULL;
     }
 
+  /*
+   * Default values
+   */
+  circuit->is_type = IS_LEVEL_1_AND_2;
+  circuit->flags = 0;
+  circuit->pad_hellos = 1;
+  for (i = 0; i < 2; i++)
+    {
+      circuit->hello_interval[i] = DEFAULT_HELLO_INTERVAL;
+      circuit->hello_multiplier[i] = DEFAULT_HELLO_MULTIPLIER;
+      circuit->csnp_interval[i] = DEFAULT_CSNP_INTERVAL;
+      circuit->psnp_interval[i] = DEFAULT_PSNP_INTERVAL;
+      circuit->priority[i] = DEFAULT_PRIORITY;
+      circuit->metrics[i].metric_default = DEFAULT_CIRCUIT_METRIC;
+      circuit->metrics[i].metric_expense = METRICS_UNSUPPORTED;
+      circuit->metrics[i].metric_error = METRICS_UNSUPPORTED;
+      circuit->metrics[i].metric_delay = METRICS_UNSUPPORTED;
+      circuit->te_metric[i] = DEFAULT_CIRCUIT_METRIC;
+    }
+
   return circuit;
 }
 
 void
+isis_circuit_del (struct isis_circuit *circuit)
+{
+  if (!circuit)
+    return;
+
+  isis_circuit_if_unbind (circuit, circuit->interface);
+
+  /* and lastly the circuit itself */
+  XFREE (MTYPE_ISIS_CIRCUIT, circuit);
+
+  return;
+}
+
+void
 isis_circuit_configure (struct isis_circuit *circuit, struct isis_area *area)
 {
-  int i;
+  assert (area);
   circuit->area = area;
+
   /*
    * The level for the circuit is same as for the area, unless configured
    * otherwise.
    */
-  circuit->circuit_is_type = area->is_type;
-  /*
-   * Default values
-   */
-  for (i = 0; i < 2; i++)
-    {
-      circuit->hello_interval[i] = HELLO_INTERVAL;
-      circuit->hello_multiplier[i] = HELLO_MULTIPLIER;
-      circuit->csnp_interval[i] = CSNP_INTERVAL;
-      circuit->psnp_interval[i] = PSNP_INTERVAL;
-      circuit->u.bc.priority[i] = DEFAULT_PRIORITY;
-    }
-  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
-    {
-      circuit->u.bc.adjdb[0] = list_new ();
-      circuit->u.bc.adjdb[1] = list_new ();
-      circuit->u.bc.pad_hellos = 1;
-    }
-  circuit->lsp_interval = LSP_INTERVAL;
+  if (area->is_type != IS_LEVEL_1_AND_2 && area->is_type != circuit->is_type)
+    zlog_warn ("circut %s is_type %d mismatch with area %s is_type %d",
+               circuit->interface->name, circuit->is_type,
+               circuit->area->area_tag, area->is_type);
 
   /*
    * Add the circuit into area
@@ -132,25 +136,20 @@
   listnode_add (area->circuit_list, circuit);
 
   circuit->idx = flags_get_index (&area->flags);
-  circuit->lsp_queue = list_new ();
 
   return;
 }
 
 void
-isis_circuit_deconfigure (struct isis_circuit *circuit,
-			  struct isis_area *area)
+isis_circuit_deconfigure (struct isis_circuit *circuit, struct isis_area *area)
 {
-
-  /* destroy adjacencies */
-  if (circuit->u.bc.adjdb[0])
-    isis_adjdb_iterate (circuit->u.bc.adjdb[0], (void(*) (struct isis_adjacency *, void *)) isis_delete_adj, circuit->u.bc.adjdb[0]);
-  if (circuit->u.bc.adjdb[1])
-    isis_adjdb_iterate (circuit->u.bc.adjdb[1], (void(*) (struct isis_adjacency *, void *)) isis_delete_adj, circuit->u.bc.adjdb[1]);
-  /* Remove circuit from area */
-  listnode_delete (area->circuit_list, circuit);
   /* Free the index of SRM and SSN flags */
   flags_free_index (&area->flags, circuit->idx);
+  circuit->idx = 0;
+  /* Remove circuit from area */
+  assert (circuit->area == area);
+  listnode_delete (area->circuit_list, circuit);
+  circuit->area = NULL;
 
   return;
 }
@@ -166,8 +165,11 @@
 
   for (ALL_LIST_ELEMENTS_RO (list, node, circuit))
     if (circuit->interface == ifp)
-      return circuit;
-  
+      {
+        assert (ifp->info == circuit);
+        return circuit;
+      }
+
   return NULL;
 }
 
@@ -178,83 +180,77 @@
   struct listnode *node;
   struct isis_circuit *circuit;
 
-  if (!isis->area_list)
-    return NULL;
+  if (ifp->info)
+    return (struct isis_circuit *)ifp->info;
 
-  for (ALL_LIST_ELEMENTS_RO (isis->area_list, node, area))
+  if (isis->area_list)
     {
-      circuit = circuit_lookup_by_ifp (ifp, area->circuit_list);
-      if (circuit)
-	return circuit;
+      for (ALL_LIST_ELEMENTS_RO (isis->area_list, node, area))
+        {
+          circuit = circuit_lookup_by_ifp (ifp, area->circuit_list);
+          if (circuit)
+            return circuit;
+        }
     }
-
   return circuit_lookup_by_ifp (ifp, isis->init_circ_list);
 }
 
-void
-isis_circuit_del (struct isis_circuit *circuit)
+static struct isis_circuit *
+isis_circuit_lookup (struct vty *vty)
 {
+  struct interface *ifp;
+  struct isis_circuit *circuit;
 
-  if (!circuit)
-    return;
-
-  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+  ifp = (struct interface *) vty->index;
+  if (!ifp)
     {
-      /* destroy adjacency databases */
-      if (circuit->u.bc.adjdb[0])
-	list_delete (circuit->u.bc.adjdb[0]);
-      if (circuit->u.bc.adjdb[1])
-	list_delete (circuit->u.bc.adjdb[1]);
-      /* destroy neighbour lists */
-      if (circuit->u.bc.lan_neighs[0])
-	list_delete (circuit->u.bc.lan_neighs[0]);
-      if (circuit->u.bc.lan_neighs[1])
-	list_delete (circuit->u.bc.lan_neighs[1]);
-      /* destroy addresses */
+      vty_out (vty, "Invalid interface %s", VTY_NEWLINE);
+      return NULL;
     }
-  if (circuit->ip_addrs)
-    list_delete (circuit->ip_addrs);
-#ifdef HAVE_IPV6
-  if (circuit->ipv6_link)
-    list_delete (circuit->ipv6_link);
-  if (circuit->ipv6_non_link)
-    list_delete (circuit->ipv6_non_link);
-#endif /* HAVE_IPV6 */
 
-  /* and lastly the circuit itself */
-  XFREE (MTYPE_ISIS_CIRCUIT, circuit);
+  circuit = circuit_scan_by_ifp (ifp);
+  if (!circuit)
+    {
+      vty_out (vty, "ISIS is not enabled on circuit %s%s",
+               ifp->name, VTY_NEWLINE);
+      return NULL;
+    }
 
-  return;
+  return circuit;
 }
 
 void
 isis_circuit_add_addr (struct isis_circuit *circuit,
 		       struct connected *connected)
 {
+  struct listnode *node;
   struct prefix_ipv4 *ipv4;
   u_char buf[BUFSIZ];
 #ifdef HAVE_IPV6
   struct prefix_ipv6 *ipv6;
 #endif /* HAVE_IPV6 */
 
-  if (!circuit->ip_addrs)
-    circuit->ip_addrs = list_new ();
-#ifdef HAVE_IPV6
-  if (!circuit->ipv6_link)
-    circuit->ipv6_link = list_new ();
-  if (!circuit->ipv6_non_link)
-    circuit->ipv6_non_link = list_new ();
-#endif /* HAVE_IPV6 */
-
   memset (&buf, 0, BUFSIZ);
   if (connected->address->family == AF_INET)
     {
+      u_int32_t addr = connected->address->u.prefix4.s_addr;
+      addr = ntohl (addr);
+      if (IPV4_NET0(addr) ||
+          IPV4_NET127(addr) ||
+          IN_CLASSD(addr) ||
+          IPV4_LINKLOCAL(addr))
+        return;
+
+      for (ALL_LIST_ELEMENTS_RO (circuit->ip_addrs, node, ipv4))
+        if (prefix_same ((struct prefix *) ipv4, connected->address))
+          return;
+
       ipv4 = prefix_ipv4_new ();
       ipv4->prefixlen = connected->address->prefixlen;
       ipv4->prefix = connected->address->u.prefix4;
       listnode_add (circuit->ip_addrs, ipv4);
       if (circuit->area)
-	lsp_regenerate_schedule (circuit->area);
+        lsp_regenerate_schedule (circuit->area, circuit->is_type, 0);
 
 #ifdef EXTREME_DEBUG
       prefix2str (connected->address, buf, BUFSIZ);
@@ -265,6 +261,16 @@
 #ifdef HAVE_IPV6
   if (connected->address->family == AF_INET6)
     {
+      if (IN6_IS_ADDR_LOOPBACK(&connected->address->u.prefix6))
+        return;
+
+      for (ALL_LIST_ELEMENTS_RO (circuit->ipv6_link, node, ipv6))
+        if (prefix_same ((struct prefix *) ipv6, connected->address))
+          return;
+      for (ALL_LIST_ELEMENTS_RO (circuit->ipv6_non_link, node, ipv6))
+        if (prefix_same ((struct prefix *) ipv6, connected->address))
+          return;
+
       ipv6 = prefix_ipv6_new ();
       ipv6->prefixlen = connected->address->prefixlen;
       ipv6->prefix = connected->address->u.prefix6;
@@ -274,7 +280,7 @@
       else
 	listnode_add (circuit->ipv6_non_link, ipv6);
       if (circuit->area)
-	lsp_regenerate_schedule (circuit->area);
+        lsp_regenerate_schedule (circuit->area, circuit->is_type, 0);
 
 #ifdef EXTREME_DEBUG
       prefix2str (connected->address, buf, BUFSIZ);
@@ -306,20 +312,20 @@
       ipv4->prefix = connected->address->u.prefix4;
 
       for (ALL_LIST_ELEMENTS_RO (circuit->ip_addrs, node, ip))
-        if (prefix_same ((struct prefix *) ip, (struct prefix *) &ipv4))
+        if (prefix_same ((struct prefix *) ip, (struct prefix *) ipv4))
           break;
 
       if (ip)
 	{
 	  listnode_delete (circuit->ip_addrs, ip);
-	  if (circuit->area)
-	    lsp_regenerate_schedule (circuit->area);
+          if (circuit->area)
+            lsp_regenerate_schedule (circuit->area, circuit->is_type, 0);
 	}
       else
 	{
 	  prefix2str (connected->address, (char *)buf, BUFSIZ);
-	  zlog_warn("Nonexitant ip address %s removal attempt from circuit \
-		     %d", buf, circuit->circuit_id);
+	  zlog_warn ("Nonexitant ip address %s removal attempt from \
+                      circuit %d", buf, circuit->circuit_id);
 	}
     }
 #ifdef HAVE_IPV6
@@ -359,72 +365,105 @@
       if (!found)
 	{
 	  prefix2str (connected->address, (char *)buf, BUFSIZ);
-	  zlog_warn("Nonexitant ip address %s removal attempt from \
-		     circuit %d", buf, circuit->circuit_id);
+	  zlog_warn ("Nonexitant ip address %s removal attempt from \
+		      circuit %d", buf, circuit->circuit_id);
 	}
-      else
-	if (circuit->area)
-	  lsp_regenerate_schedule (circuit->area);
+      else if (circuit->area)
+	  lsp_regenerate_schedule (circuit->area, circuit->is_type, 0);
     }
 #endif /* HAVE_IPV6 */
   return;
 }
 
+static u_char
+isis_circuit_id_gen (struct interface *ifp)
+{
+  u_char id = 0;
+  char ifname[16];
+  unsigned int i;
+  int start = -1, end = -1;
+
+  /*
+   * Get a stable circuit id from ifname. This makes
+   * the ifindex from flapping when netdevs are created
+   * and deleted on the fly. Note that this circuit id
+   * is used in pseudo lsps so it is better to be stable.
+   * The following code works on any reasonanle ifname
+   * like: eth1 or trk-1.1 etc.
+   */
+  for (i = 0; i < strlen (ifp->name); i++)
+    {
+      if (isdigit(ifp->name[i]))
+        {
+          if (start < 0)
+            {
+              start = i;
+              end = i + 1;
+            }
+          else
+            {
+              end = i + 1;
+            }
+        }
+      else if (start >= 0)
+        break;
+    }
+
+  if ((start >= 0) && (end >= start) && (end - start) < 16)
+    {
+      memset (ifname, 0, 16);
+      strncpy (ifname, &ifp->name[start], end - start);
+      id = (u_char)atoi(ifname);
+    }
+
+  /* Try to be unique. */
+  if (!id)
+    id = (u_char)((ifp->ifindex & 0xff) | 0x80);
+
+  return id;
+}
+
 void
 isis_circuit_if_add (struct isis_circuit *circuit, struct interface *ifp)
 {
   struct listnode *node, *nnode;
   struct connected *conn;
 
-  circuit->interface = ifp;
-  ifp->info = circuit;
+  circuit->circuit_id = isis_circuit_id_gen (ifp);
 
-  circuit->circuit_id = ifp->ifindex % 255;	/* FIXME: Why not ? */
-
+  isis_circuit_if_bind (circuit, ifp);
   /*  isis_circuit_update_addrs (circuit, ifp); */
 
   if (if_is_broadcast (ifp))
     {
-      circuit->circ_type = CIRCUIT_T_BROADCAST;
-      /*
-       * Get the Hardware Address
-       */
-#ifdef HAVE_STRUCT_SOCKADDR_DL
-#ifndef SUNOS_5
-      if (circuit->interface->sdl.sdl_alen != ETHER_ADDR_LEN)
-	zlog_warn ("unsupported link layer");
+      if (circuit->circ_type_config == CIRCUIT_T_P2P)
+        circuit->circ_type = CIRCUIT_T_P2P;
       else
-	memcpy (circuit->u.bc.snpa, LLADDR (&circuit->interface->sdl),
-		ETH_ALEN);
-#endif
-#else
-      if (circuit->interface->hw_addr_len != ETH_ALEN)
-	{
-	  zlog_warn ("unsupported link layer");
-	}
-      else
-	{
-	  memcpy (circuit->u.bc.snpa, circuit->interface->hw_addr, ETH_ALEN);
-	}
-#ifdef EXTREME_DEGUG
-      zlog_debug ("isis_circuit_if_add: if_id %d, isomtu %d snpa %s",
-		 circuit->interface->ifindex, ISO_MTU (circuit),
-		 snpa_print (circuit->u.bc.snpa));
-
-#endif /* EXTREME_DEBUG */
-#endif /* HAVE_STRUCT_SOCKADDR_DL */
+        circuit->circ_type = CIRCUIT_T_BROADCAST;
     }
   else if (if_is_pointopoint (ifp))
     {
       circuit->circ_type = CIRCUIT_T_P2P;
     }
+  else if (if_is_loopback (ifp))
+    {
+      circuit->circ_type = CIRCUIT_T_LOOPBACK;
+      circuit->is_passive = 1;
+    }
   else
     {
       /* It's normal in case of loopback etc. */
       if (isis->debugs & DEBUG_EVENTS)
-	zlog_debug ("isis_circuit_if_add: unsupported media");
+        zlog_debug ("isis_circuit_if_add: unsupported media");
+      circuit->circ_type = CIRCUIT_T_UNKNOWN;
     }
 
+  circuit->ip_addrs = list_new ();
+#ifdef HAVE_IPV6
+  circuit->ipv6_link = list_new ();
+  circuit->ipv6_non_link = list_new ();
+#endif /* HAVE_IPV6 */
+
   for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, conn))
     isis_circuit_add_addr (circuit, conn);
 
@@ -432,88 +471,158 @@
 }
 
 void
-isis_circuit_update_params (struct isis_circuit *circuit,
-			    struct interface *ifp)
+isis_circuit_if_del (struct isis_circuit *circuit, struct interface *ifp)
 {
-  assert (circuit);
+  struct listnode *node, *nnode;
+  struct connected *conn;
 
-  if (circuit->circuit_id != ifp->ifindex)
+  assert (circuit->interface == ifp);
+
+  /* destroy addresses */
+  for (ALL_LIST_ELEMENTS (ifp->connected, node, nnode, conn))
+    isis_circuit_del_addr (circuit, conn);
+
+  if (circuit->ip_addrs)
     {
-      zlog_warn ("changing circuit_id %d->%d", circuit->circuit_id,
-		 ifp->ifindex);
-      circuit->circuit_id = ifp->ifindex % 255;
+      assert (listcount(circuit->ip_addrs) == 0);
+      list_delete (circuit->ip_addrs);
+      circuit->ip_addrs = NULL;
     }
 
-  /* FIXME: Why is this needed? shouldn't we compare to the area's mtu */
-  /* Ofer, this was here in case someone changes the mtu (e.g. with ifconfig) 
-     The areas MTU is the minimum of mtu's of circuits in the area
-     now we can't catch the change
-     if (circuit->mtu != ifp->mtu) {
-     zlog_warn ("changing circuit mtu %d->%d", circuit->mtu, 
-     ifp->mtu);    
-     circuit->mtu = ifp->mtu;
-     }
-   */
-  /*
-   * Get the Hardware Address
-   */
-#ifdef HAVE_STRUCT_SOCKADDR_DL
-#ifndef SUNOS_5
-  if (circuit->interface->sdl.sdl_alen != ETHER_ADDR_LEN)
-    zlog_warn ("unsupported link layer");
-  else
-    memcpy (circuit->u.bc.snpa, LLADDR (&circuit->interface->sdl), ETH_ALEN);
-#endif
-#else
-  if (circuit->interface->hw_addr_len != ETH_ALEN)
+#ifdef HAVE_IPV6
+  if (circuit->ipv6_link)
     {
-      zlog_warn ("unsupported link layer");
+      assert (listcount(circuit->ipv6_link) == 0);
+      list_delete (circuit->ipv6_link);
+      circuit->ipv6_link = NULL;
     }
-  else
-    {
-      if (memcmp (circuit->u.bc.snpa, circuit->interface->hw_addr, ETH_ALEN))
-	{
-	  zlog_warn ("changing circuit snpa %s->%s",
-		     snpa_print (circuit->u.bc.snpa),
-		     snpa_print (circuit->interface->hw_addr));
-	}
-    }
-#endif
 
-  if (if_is_broadcast (ifp))
+  if (circuit->ipv6_non_link)
     {
-      circuit->circ_type = CIRCUIT_T_BROADCAST;
+      assert (listcount(circuit->ipv6_non_link) == 0);
+      list_delete (circuit->ipv6_non_link);
+      circuit->ipv6_non_link = NULL;
     }
-  else if (if_is_pointopoint (ifp))
-    {
-      circuit->circ_type = CIRCUIT_T_P2P;
-    }
-  else
-    {
-      zlog_warn ("isis_circuit_update_params: unsupported media");
-    }
+#endif /* HAVE_IPV6 */
+
+  circuit->circ_type = CIRCUIT_T_UNKNOWN;
+  circuit->circuit_id = 0;
 
   return;
 }
 
 void
-isis_circuit_if_del (struct isis_circuit *circuit)
+isis_circuit_if_bind (struct isis_circuit *circuit, struct interface *ifp)
 {
-  circuit->interface->info = NULL;
+  assert (circuit != NULL);
+  assert (ifp != NULL);
+  if (circuit->interface)
+    assert (circuit->interface == ifp);
+  else
+    circuit->interface = ifp;
+  if (ifp->info)
+    assert (ifp->info == circuit);
+  else
+    ifp->info = circuit;
+}
+
+void
+isis_circuit_if_unbind (struct isis_circuit *circuit, struct interface *ifp)
+{
+  assert (circuit != NULL);
+  assert (ifp != NULL);
+  assert (circuit->interface == ifp);
+  assert (ifp->info == circuit);
   circuit->interface = NULL;
-
-  return;
+  ifp->info = NULL;
 }
 
-void
+static void
+isis_circuit_update_all_srmflags (struct isis_circuit *circuit, int is_set)
+{
+  struct isis_area *area;
+  struct isis_lsp *lsp;
+  dnode_t *dnode, *dnode_next;
+  int level;
+
+  assert (circuit);
+  area = circuit->area;
+  assert (area);
+  for (level = ISIS_LEVEL1; level <= ISIS_LEVEL2; level++)
+    {
+      if (level & circuit->is_type)
+        {
+          if (area->lspdb[level - 1] &&
+              dict_count (area->lspdb[level - 1]) > 0)
+            {
+              for (dnode = dict_first (area->lspdb[level - 1]);
+                   dnode != NULL; dnode = dnode_next)
+                {
+                  dnode_next = dict_next (area->lspdb[level - 1], dnode);
+                  lsp = dnode_get (dnode);
+                  if (is_set)
+                    {
+                      ISIS_SET_FLAG (lsp->SRMflags, circuit);
+                    }
+                  else
+                    {
+                      ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
+                    }
+                }
+            }
+        }
+    }
+}
+
+int
 isis_circuit_up (struct isis_circuit *circuit)
 {
+  int retv;
+
+  /* Set the flags for all the lsps of the circuit. */
+  isis_circuit_update_all_srmflags (circuit, 1);
+
+  if (circuit->state == C_STATE_UP)
+    return ISIS_OK;
+
+  if (circuit->is_passive)
+    return ISIS_OK;
 
   if (circuit->circ_type == CIRCUIT_T_BROADCAST)
     {
+      /*
+       * Get the Hardware Address
+       */
+#ifdef HAVE_STRUCT_SOCKADDR_DL
+#ifndef SUNOS_5
+      if (circuit->interface->sdl.sdl_alen != ETHER_ADDR_LEN)
+        zlog_warn ("unsupported link layer");
+      else
+        memcpy (circuit->u.bc.snpa, LLADDR (&circuit->interface->sdl),
+                ETH_ALEN);
+#endif
+#else
+      if (circuit->interface->hw_addr_len != ETH_ALEN)
+        {
+          zlog_warn ("unsupported link layer");
+        }
+      else
+        {
+          memcpy (circuit->u.bc.snpa, circuit->interface->hw_addr, ETH_ALEN);
+        }
+#ifdef EXTREME_DEGUG
+      zlog_debug ("isis_circuit_if_add: if_id %d, isomtu %d snpa %s",
+                  circuit->interface->ifindex, ISO_MTU (circuit),
+                  snpa_print (circuit->u.bc.snpa));
+#endif /* EXTREME_DEBUG */
+#endif /* HAVE_STRUCT_SOCKADDR_DL */
+
+      circuit->u.bc.adjdb[0] = list_new ();
+      circuit->u.bc.adjdb[1] = list_new ();
+
       if (circuit->area->min_bcast_mtu == 0 ||
-	  ISO_MTU (circuit) < circuit->area->min_bcast_mtu)
-	circuit->area->min_bcast_mtu = ISO_MTU (circuit);
+          ISO_MTU (circuit) < circuit->area->min_bcast_mtu)
+        circuit->area->min_bcast_mtu = ISO_MTU (circuit);
       /*
        * ISO 10589 - 8.4.1 Enabling of broadcast circuits
        */
@@ -524,98 +633,183 @@
 
       /* 8.4.1 a) commence sending of IIH PDUs */
 
-      if (circuit->circuit_is_type & IS_LEVEL_1)
-	{
-	  thread_add_event (master, send_lan_l1_hello, circuit, 0);
-	  circuit->u.bc.lan_neighs[0] = list_new ();
-	}
+      if (circuit->is_type & IS_LEVEL_1)
+        {
+          thread_add_event (master, send_lan_l1_hello, circuit, 0);
+          circuit->u.bc.lan_neighs[0] = list_new ();
+        }
 
-      if (circuit->circuit_is_type & IS_LEVEL_2)
-	{
-	  thread_add_event (master, send_lan_l2_hello, circuit, 0);
-	  circuit->u.bc.lan_neighs[1] = list_new ();
-	}
+      if (circuit->is_type & IS_LEVEL_2)
+        {
+          thread_add_event (master, send_lan_l2_hello, circuit, 0);
+          circuit->u.bc.lan_neighs[1] = list_new ();
+        }
 
       /* 8.4.1 b) FIXME: solicit ES - 8.4.6 */
       /* 8.4.1 c) FIXME: listen for ESH PDUs */
 
       /* 8.4.1 d) */
       /* dr election will commence in... */
-      if (circuit->circuit_is_type & IS_LEVEL_1)
-	THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[0], isis_run_dr_l1,
-			 circuit, 2 * circuit->hello_interval[0]);
-      if (circuit->circuit_is_type & IS_LEVEL_2)
-	THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[1], isis_run_dr_l2,
-			 circuit, 2 * circuit->hello_interval[1]);
+      if (circuit->is_type & IS_LEVEL_1)
+        THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[0], isis_run_dr_l1,
+            circuit, 2 * circuit->hello_interval[0]);
+      if (circuit->is_type & IS_LEVEL_2)
+        THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[1], isis_run_dr_l2,
+            circuit, 2 * circuit->hello_interval[1]);
     }
   else
     {
       /* initializing the hello send threads
        * for a ptp IF
        */
+      circuit->u.p2p.neighbor = NULL;
       thread_add_event (master, send_p2p_hello, circuit, 0);
-
     }
 
   /* initializing PSNP timers */
-  if (circuit->circuit_is_type & IS_LEVEL_1)
+  if (circuit->is_type & IS_LEVEL_1)
+    THREAD_TIMER_ON (master, circuit->t_send_psnp[0], send_l1_psnp, circuit,
+                     isis_jitter (circuit->psnp_interval[0], PSNP_JITTER));
+
+  if (circuit->is_type & IS_LEVEL_2)
+    THREAD_TIMER_ON (master, circuit->t_send_psnp[1], send_l2_psnp, circuit,
+                     isis_jitter (circuit->psnp_interval[1], PSNP_JITTER));
+
+  /* unified init for circuits; ignore warnings below this level */
+  retv = isis_sock_init (circuit);
+  if (retv != ISIS_OK)
     {
-      THREAD_TIMER_ON (master, circuit->t_send_psnp[0], send_l1_psnp, circuit,
-		       isis_jitter (circuit->psnp_interval[0], PSNP_JITTER));
+      isis_circuit_down (circuit);
+      return retv;
     }
 
-  if (circuit->circuit_is_type & IS_LEVEL_2)
-    {
-      THREAD_TIMER_ON (master, circuit->t_send_psnp[1], send_l2_psnp, circuit,
-		       isis_jitter (circuit->psnp_interval[1], PSNP_JITTER));
-    }
-
-  /* initialize the circuit streams */
+  /* initialize the circuit streams after opening connection */
   if (circuit->rcv_stream == NULL)
     circuit->rcv_stream = stream_new (ISO_MTU (circuit));
 
   if (circuit->snd_stream == NULL)
     circuit->snd_stream = stream_new (ISO_MTU (circuit));
 
-  /* unified init for circuits */
-  isis_sock_init (circuit);
-
 #ifdef GNU_LINUX
   THREAD_READ_ON (master, circuit->t_read, isis_receive, circuit,
-		  circuit->fd);
+                  circuit->fd);
 #else
   THREAD_TIMER_ON (master, circuit->t_read, isis_receive, circuit,
-		   circuit->fd);
+                   circuit->fd);
 #endif
-  return;
+
+  circuit->lsp_queue = list_new ();
+  circuit->lsp_queue_last_cleared = time (NULL);
+
+  return ISIS_OK;
 }
 
 void
 isis_circuit_down (struct isis_circuit *circuit)
 {
-  /* Cancel all active threads -- FIXME: wrong place */
-  /* HT: Read thread if GNU_LINUX, TIMER thread otherwise. */
-  THREAD_OFF (circuit->t_read);
+  if (circuit->state != C_STATE_UP)
+    return;
+
+  /* Clear the flags for all the lsps of the circuit. */
+  isis_circuit_update_all_srmflags (circuit, 0);
+
   if (circuit->circ_type == CIRCUIT_T_BROADCAST)
     {
+      /* destroy neighbour lists */
+      if (circuit->u.bc.lan_neighs[0])
+        {
+          list_delete (circuit->u.bc.lan_neighs[0]);
+          circuit->u.bc.lan_neighs[0] = NULL;
+        }
+      if (circuit->u.bc.lan_neighs[1])
+        {
+          list_delete (circuit->u.bc.lan_neighs[1]);
+          circuit->u.bc.lan_neighs[1] = NULL;
+        }
+      /* destroy adjacency databases */
+      if (circuit->u.bc.adjdb[0])
+        {
+          circuit->u.bc.adjdb[0]->del = isis_delete_adj;
+          list_delete (circuit->u.bc.adjdb[0]);
+          circuit->u.bc.adjdb[0] = NULL;
+        }
+      if (circuit->u.bc.adjdb[1])
+        {
+          circuit->u.bc.adjdb[1]->del = isis_delete_adj;
+          list_delete (circuit->u.bc.adjdb[1]);
+          circuit->u.bc.adjdb[1] = NULL;
+        }
+      if (circuit->u.bc.is_dr[0])
+        {
+          isis_dr_resign (circuit, 1);
+          circuit->u.bc.is_dr[0] = 0;
+        }
+      memset (circuit->u.bc.l1_desig_is, 0, ISIS_SYS_ID_LEN + 1);
+      if (circuit->u.bc.is_dr[1])
+        {
+          isis_dr_resign (circuit, 2);
+          circuit->u.bc.is_dr[1] = 0;
+        }
+      memset (circuit->u.bc.l2_desig_is, 0, ISIS_SYS_ID_LEN + 1);
+      memset (circuit->u.bc.snpa, 0, ETH_ALEN);
+
       THREAD_TIMER_OFF (circuit->u.bc.t_send_lan_hello[0]);
       THREAD_TIMER_OFF (circuit->u.bc.t_send_lan_hello[1]);
       THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[0]);
       THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[1]);
+      THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[0]);
+      THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[1]);
     }
   else if (circuit->circ_type == CIRCUIT_T_P2P)
     {
+      isis_delete_adj (circuit->u.p2p.neighbor);
+      circuit->u.p2p.neighbor = NULL;
       THREAD_TIMER_OFF (circuit->u.p2p.t_send_p2p_hello);
     }
 
-  if (circuit->t_send_psnp[0]) {
-    THREAD_TIMER_OFF (circuit->t_send_psnp[0]);
-  }
-  if (circuit->t_send_psnp[1]) {
-    THREAD_TIMER_OFF (circuit->t_send_psnp[1]);
-  }
+  /* Cancel all active threads */
+  THREAD_TIMER_OFF (circuit->t_send_csnp[0]);
+  THREAD_TIMER_OFF (circuit->t_send_csnp[1]);
+  THREAD_TIMER_OFF (circuit->t_send_psnp[0]);
+  THREAD_TIMER_OFF (circuit->t_send_psnp[1]);
+  THREAD_OFF (circuit->t_read);
+
+  if (circuit->lsp_queue)
+    {
+      circuit->lsp_queue->del = NULL;
+      list_delete (circuit->lsp_queue);
+      circuit->lsp_queue = NULL;
+    }
+
+  /* send one gratuitous hello to spead up convergence */
+  if (circuit->is_type & IS_LEVEL_1)
+    send_hello (circuit, IS_LEVEL_1);
+  if (circuit->is_type & IS_LEVEL_2)
+    send_hello (circuit, IS_LEVEL_2);
+
+  circuit->upadjcount[0] = 0;
+  circuit->upadjcount[1] = 0;
+
   /* close the socket */
-  close (circuit->fd);
+  if (circuit->fd)
+    {
+      close (circuit->fd);
+      circuit->fd = 0;
+    }
+
+  if (circuit->rcv_stream != NULL)
+    {
+      stream_free (circuit->rcv_stream);
+      circuit->rcv_stream = NULL;
+    }
+
+  if (circuit->snd_stream != NULL)
+    {
+      stream_free (circuit->snd_stream);
+      circuit->snd_stream = NULL;
+    }
+
+  thread_cancel_event (master, circuit);
 
   return;
 }
@@ -640,216 +834,346 @@
   return;
 }
 
+void
+isis_circuit_print_vty (struct isis_circuit *circuit, struct vty *vty,
+                        char detail)
+{
+  if (detail == ISIS_UI_LEVEL_BRIEF)
+    {
+      vty_out (vty, "  %-12s", circuit->interface->name);
+      vty_out (vty, "0x%-7x", circuit->circuit_id);
+      vty_out (vty, "%-9s", circuit_state2string (circuit->state));
+      vty_out (vty, "%-9s", circuit_type2string (circuit->circ_type));
+      vty_out (vty, "%-9s", circuit_t2string (circuit->is_type));
+      vty_out (vty, "%s", VTY_NEWLINE);
+    }
+
+  if (detail == ISIS_UI_LEVEL_DETAIL)
+    {
+      vty_out (vty, "  Interface: %s", circuit->interface->name);
+      vty_out (vty, ", State: %s", circuit_state2string (circuit->state));
+      if (circuit->is_passive)
+        vty_out (vty, ", Passive");
+      else
+        vty_out (vty, ", Active");
+      vty_out (vty, ", Circuit Id: 0x%x", circuit->circuit_id);
+      vty_out (vty, "%s", VTY_NEWLINE);
+      vty_out (vty, "    Type: %s", circuit_type2string (circuit->circ_type));
+      vty_out (vty, ", Level: %s", circuit_t2string (circuit->is_type));
+      if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+        vty_out (vty, ", SNPA: %-10s", snpa_print (circuit->u.bc.snpa));
+      vty_out (vty, "%s", VTY_NEWLINE);
+      if (circuit->is_type & IS_LEVEL_1)
+        {
+          vty_out (vty, "    Level-1 Information:%s", VTY_NEWLINE);
+          if (circuit->area->newmetric)
+            vty_out (vty, "      Metric: %d", circuit->te_metric[0]);
+          else
+            vty_out (vty, "      Metric: %d",
+                     circuit->metrics[0].metric_default);
+          if (!circuit->is_passive)
+            {
+              vty_out (vty, ", Active neighbors: %u%s",
+                       circuit->upadjcount[0], VTY_NEWLINE);
+              vty_out (vty, "      Hello interval: %u, "
+                            "Holddown count: %u %s%s",
+                       circuit->hello_interval[0],
+                       circuit->hello_multiplier[0],
+                       (circuit->pad_hellos ? "(pad)" : "(no-pad)"),
+                       VTY_NEWLINE);
+              vty_out (vty, "      CNSP interval: %u, "
+                            "PSNP interval: %u%s",
+                       circuit->csnp_interval[0],
+                       circuit->psnp_interval[0], VTY_NEWLINE);
+              if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+                vty_out (vty, "      LAN Priority: %u, %s%s",
+                         circuit->priority[0],
+                         (circuit->u.bc.is_dr[0] ? \
+                          "is DIS" : "is not DIS"), VTY_NEWLINE);
+            }
+          else
+            {
+              vty_out (vty, "%s", VTY_NEWLINE);
+            }
+        }
+      if (circuit->is_type & IS_LEVEL_2)
+        {
+          vty_out (vty, "    Level-2 Information:%s", VTY_NEWLINE);
+          if (circuit->area->newmetric)
+            vty_out (vty, "      Metric: %d", circuit->te_metric[1]);
+          else
+            vty_out (vty, "      Metric: %d",
+                     circuit->metrics[1].metric_default);
+          if (!circuit->is_passive)
+            {
+              vty_out (vty, ", Active neighbors: %u%s",
+                       circuit->upadjcount[1], VTY_NEWLINE);
+              vty_out (vty, "      Hello interval: %u, "
+                            "Holddown count: %u %s%s",
+                       circuit->hello_interval[1],
+                       circuit->hello_multiplier[1],
+                       (circuit->pad_hellos ? "(pad)" : "(no-pad)"),
+                       VTY_NEWLINE);
+              vty_out (vty, "      CNSP interval: %u, "
+                            "PSNP interval: %u%s",
+                       circuit->csnp_interval[1],
+                       circuit->psnp_interval[1], VTY_NEWLINE);
+              if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+                vty_out (vty, "      LAN Priority: %u, %s%s",
+                         circuit->priority[1],
+                         (circuit->u.bc.is_dr[1] ? \
+                          "is DIS" : "is not DIS"), VTY_NEWLINE);
+            }
+          else
+            {
+              vty_out (vty, "%s", VTY_NEWLINE);
+            }
+        }
+      if (circuit->ip_addrs && listcount (circuit->ip_addrs) > 0)
+        {
+          struct listnode *node;
+          struct prefix *ip_addr;
+          u_char buf[BUFSIZ];
+          vty_out (vty, "    IP Prefix(es):%s", VTY_NEWLINE);
+          for (ALL_LIST_ELEMENTS_RO (circuit->ip_addrs, node, ip_addr))
+            {
+              prefix2str (ip_addr, (char*)buf, BUFSIZ),
+              vty_out (vty, "      %s%s", buf, VTY_NEWLINE);
+            }
+        }
+      vty_out (vty, "%s", VTY_NEWLINE);
+    }
+  return;
+}
+
 int
 isis_interface_config_write (struct vty *vty)
 {
-
   int write = 0;
   struct listnode *node, *node2;
   struct interface *ifp;
   struct isis_area *area;
-  struct isis_circuit *c;
+  struct isis_circuit *circuit;
   int i;
 
   for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
-  {
-    /* IF name */
-    vty_out (vty, "interface %s%s", ifp->name, VTY_NEWLINE);
-    write++;
-    /* IF desc */
-    if (ifp->desc)
-      {
-	vty_out (vty, " description %s%s", ifp->desc, VTY_NEWLINE);
-	write++;
-      }
-    /* ISIS Circuit */
-    for (ALL_LIST_ELEMENTS_RO (isis->area_list, node2, area))
     {
-      c = circuit_lookup_by_ifp (ifp, area->circuit_list);
-      if (c)
-	{
-	  if (c->ip_router)
-	    {
-	      vty_out (vty, " ip router isis %s%s", area->area_tag,
-		       VTY_NEWLINE);
-	      write++;
-	    }
+      /* IF name */
+      vty_out (vty, "interface %s%s", ifp->name, VTY_NEWLINE);
+      write++;
+      /* IF desc */
+      if (ifp->desc)
+        {
+          vty_out (vty, " description %s%s", ifp->desc, VTY_NEWLINE);
+          write++;
+        }
+      /* ISIS Circuit */
+      for (ALL_LIST_ELEMENTS_RO (isis->area_list, node2, area))
+        {
+          circuit = circuit_lookup_by_ifp (ifp, area->circuit_list);
+          if (circuit == NULL)
+            continue;
+          if (circuit->ip_router)
+            {
+              vty_out (vty, " ip router isis %s%s", area->area_tag,
+                       VTY_NEWLINE);
+              write++;
+            }
+          if (circuit->is_passive)
+            {
+              vty_out (vty, " isis passive%s", VTY_NEWLINE);
+              write++;
+            }
+          if (circuit->circ_type_config == CIRCUIT_T_P2P)
+            {
+              vty_out (vty, " isis network point-to-point%s", VTY_NEWLINE);
+              write++;
+            }
 #ifdef HAVE_IPV6
-	  if (c->ipv6_router)
-	    {
-	      vty_out (vty, " ipv6 router isis %s%s", area->area_tag,
-		       VTY_NEWLINE);
-	      write++;
-	    }
+          if (circuit->ipv6_router)
+            {
+              vty_out (vty, " ipv6 router isis %s%s", area->area_tag,
+                  VTY_NEWLINE);
+              write++;
+            }
 #endif /* HAVE_IPV6 */
 
-	  /* ISIS - circuit type */
-	  if (c->circuit_is_type == IS_LEVEL_1)
-	    {
-	      vty_out (vty, " isis circuit-type level-1%s", VTY_NEWLINE);
-	      write++;
-	    }
-	  else
-	    {
-	      if (c->circuit_is_type == IS_LEVEL_2)
-		{
-		  vty_out (vty, " isis circuit-type level-2-only%s",
-			   VTY_NEWLINE);
-		  write++;
-		}
-	    }
+          /* ISIS - circuit type */
+          if (circuit->is_type == IS_LEVEL_1)
+            {
+              vty_out (vty, " isis circuit-type level-1%s", VTY_NEWLINE);
+              write++;
+            }
+          else
+            {
+              if (circuit->is_type == IS_LEVEL_2)
+                {
+                  vty_out (vty, " isis circuit-type level-2-only%s",
+                           VTY_NEWLINE);
+                  write++;
+                }
+            }
 
-	  /* ISIS - CSNP interval - FIXME: compare to cisco */
-	  if (c->csnp_interval[0] == c->csnp_interval[1])
-	    {
-	      if (c->csnp_interval[0] != CSNP_INTERVAL)
-		{
-		  vty_out (vty, " isis csnp-interval %d%s",
-			   c->csnp_interval[0], VTY_NEWLINE);
-		  write++;
-		}
-	    }
-	  else
-	    {
-	      for (i = 0; i < 2; i++)
-		{
-		  if (c->csnp_interval[1] != CSNP_INTERVAL)
-		    {
-		      vty_out (vty, " isis csnp-interval %d level-%d%s",
-			       c->csnp_interval[1], i + 1, VTY_NEWLINE);
-		      write++;
-		    }
-		}
-	    }
+          /* ISIS - CSNP interval */
+          if (circuit->csnp_interval[0] == circuit->csnp_interval[1])
+            {
+              if (circuit->csnp_interval[0] != DEFAULT_CSNP_INTERVAL)
+                {
+                  vty_out (vty, " isis csnp-interval %d%s",
+                           circuit->csnp_interval[0], VTY_NEWLINE);
+                  write++;
+                }
+            }
+          else
+          {
+            for (i = 0; i < 2; i++)
+              {
+                if (circuit->csnp_interval[i] != DEFAULT_CSNP_INTERVAL)
+                  {
+                    vty_out (vty, " isis csnp-interval %d level-%d%s",
+                             circuit->csnp_interval[i], i + 1, VTY_NEWLINE);
+                    write++;
+                  }
+              }
+          }
 
-	  /* ISIS - Hello padding - Defaults to true so only display if false */
-	  if (c->circ_type == CIRCUIT_T_BROADCAST && !c->u.bc.pad_hellos)
-	    {
-	      vty_out (vty, " no isis hello padding%s", VTY_NEWLINE);
-	      write++;
-	    }
+          /* ISIS - PSNP interval */
+          if (circuit->psnp_interval[0] == circuit->psnp_interval[1])
+            {
+              if (circuit->psnp_interval[0] != DEFAULT_PSNP_INTERVAL)
+                {
+                  vty_out (vty, " isis psnp-interval %d%s",
+                           circuit->psnp_interval[0], VTY_NEWLINE);
+                  write++;
+                }
+            }
+          else
+            {
+              for (i = 0; i < 2; i++)
+                {
+                  if (circuit->psnp_interval[i] != DEFAULT_PSNP_INTERVAL)
+                  {
+                    vty_out (vty, " isis psnp-interval %d level-%d%s",
+                             circuit->psnp_interval[i], i + 1, VTY_NEWLINE);
+                    write++;
+                  }
+                }
+            }
 
-	  /* ISIS - Hello interval - FIXME: compare to cisco */
-	  if (c->hello_interval[0] == c->hello_interval[1])
-	    {
-	      if (c->hello_interval[0] != HELLO_INTERVAL)
-		{
-		  vty_out (vty, " isis hello-interval %d%s",
-			   c->hello_interval[0], VTY_NEWLINE);
-		  write++;
-		}
-	    }
-	  else
-	    {
-	      for (i = 0; i < 2; i++)
-		{
-		  if (c->hello_interval[i] != HELLO_INTERVAL)
-		    {
-		      if (c->hello_interval[i] == HELLO_MINIMAL)
-			{
-			  vty_out (vty,
-				   " isis hello-interval minimal level-%d%s",
-				   i + 1, VTY_NEWLINE);
-			}
-		      else
-			{
-			  vty_out (vty, " isis hello-interval %d level-%d%s",
-				   c->hello_interval[i], i + 1, VTY_NEWLINE);
-			}
-		      write++;
-		    }
-		}
-	    }
+          /* ISIS - Hello padding - Defaults to true so only display if false */
+          if (circuit->pad_hellos == 0)
+            {
+              vty_out (vty, " no isis hello padding%s", VTY_NEWLINE);
+              write++;
+            }
 
-	  /* ISIS - Hello Multiplier */
-	  if (c->hello_multiplier[0] == c->hello_multiplier[1])
-	    {
-	      if (c->hello_multiplier[0] != HELLO_MULTIPLIER)
-		{
-		  vty_out (vty, " isis hello-multiplier %d%s",
-			   c->hello_multiplier[0], VTY_NEWLINE);
-		  write++;
-		}
-	    }
-	  else
-	    {
-	      for (i = 0; i < 2; i++)
-		{
-		  if (c->hello_multiplier[i] != HELLO_MULTIPLIER)
-		    {
-		      vty_out (vty, " isis hello-multiplier %d level-%d%s",
-			       c->hello_multiplier[i], i + 1, VTY_NEWLINE);
-		      write++;
-		    }
-		}
-	    }
-	  /* ISIS - Priority */
-	  if (c->circ_type == CIRCUIT_T_BROADCAST)
-	    {
-	      if (c->u.bc.priority[0] == c->u.bc.priority[1])
-		{
-		  if (c->u.bc.priority[0] != DEFAULT_PRIORITY)
-		    {
-		      vty_out (vty, " isis priority %d%s",
-			       c->u.bc.priority[0], VTY_NEWLINE);
-		      write++;
-		    }
-		}
-	      else
-		{
-		  for (i = 0; i < 2; i++)
-		    {
-		      if (c->u.bc.priority[i] != DEFAULT_PRIORITY)
-			{
-			  vty_out (vty, " isis priority %d level-%d%s",
-				   c->u.bc.priority[i], i + 1, VTY_NEWLINE);
-			  write++;
-			}
-		    }
-		}
-	    }
-	  /* ISIS - Metric */
-	  if (c->te_metric[0] == c->te_metric[1])
-	    {
-	      if (c->te_metric[0] != DEFAULT_CIRCUIT_METRICS)
-		{
-		  vty_out (vty, " isis metric %d%s", c->te_metric[0],
-			   VTY_NEWLINE);
-		  write++;
-		}
-	    }
-	  else
-	    {
-	      for (i = 0; i < 2; i++)
-		{
-		  if (c->te_metric[i] != DEFAULT_CIRCUIT_METRICS)
-		    {
-		      vty_out (vty, " isis metric %d level-%d%s",
-			       c->te_metric[i], i + 1, VTY_NEWLINE);
-		      write++;
-		    }
-		}
-	    }
-	  if (c->passwd.type==ISIS_PASSWD_TYPE_HMAC_MD5)
-	    {
-	      vty_out (vty, " isis password md5 %s%s", c->passwd.passwd,
-		       VTY_NEWLINE);
-	      write++;
-	    }
-	  else
-	    {
-	      if (c->passwd.type==ISIS_PASSWD_TYPE_CLEARTXT)
-		{
-		  vty_out (vty, " isis password clear %s%s", c->passwd.passwd,
-			   VTY_NEWLINE);
-		  write++;
-		}
-	    }
+          /* ISIS - Hello interval */
+          if (circuit->hello_interval[0] == circuit->hello_interval[1])
+            {
+              if (circuit->hello_interval[0] != DEFAULT_HELLO_INTERVAL)
+                {
+                  vty_out (vty, " isis hello-interval %d%s",
+                           circuit->hello_interval[0], VTY_NEWLINE);
+                  write++;
+                }
+            }
+          else
+            {
+              for (i = 0; i < 2; i++)
+                {
+                  if (circuit->hello_interval[i] != DEFAULT_HELLO_INTERVAL)
+                    {
+                      vty_out (vty, " isis hello-interval %d level-%d%s",
+                               circuit->hello_interval[i], i + 1, VTY_NEWLINE);
+                      write++;
+                    }
+                }
+            }
 
-	}
+          /* ISIS - Hello Multiplier */
+          if (circuit->hello_multiplier[0] == circuit->hello_multiplier[1])
+            {
+              if (circuit->hello_multiplier[0] != DEFAULT_HELLO_MULTIPLIER)
+                {
+                  vty_out (vty, " isis hello-multiplier %d%s",
+                           circuit->hello_multiplier[0], VTY_NEWLINE);
+                  write++;
+                }
+            }
+          else
+            {
+              for (i = 0; i < 2; i++)
+                {
+                  if (circuit->hello_multiplier[i] != DEFAULT_HELLO_MULTIPLIER)
+                    {
+                      vty_out (vty, " isis hello-multiplier %d level-%d%s",
+                               circuit->hello_multiplier[i], i + 1,
+                               VTY_NEWLINE);
+                      write++;
+                    }
+                }
+            }
+
+          /* ISIS - Priority */
+          if (circuit->priority[0] == circuit->priority[1])
+            {
+              if (circuit->priority[0] != DEFAULT_PRIORITY)
+                {
+                  vty_out (vty, " isis priority %d%s",
+                           circuit->priority[0], VTY_NEWLINE);
+                  write++;
+                }
+            }
+          else
+            {
+              for (i = 0; i < 2; i++)
+                {
+                  if (circuit->priority[i] != DEFAULT_PRIORITY)
+                    {
+                      vty_out (vty, " isis priority %d level-%d%s",
+                               circuit->priority[i], i + 1, VTY_NEWLINE);
+                      write++;
+                    }
+                }
+            }
+
+          /* ISIS - Metric */
+          if (circuit->te_metric[0] == circuit->te_metric[1])
+            {
+              if (circuit->te_metric[0] != DEFAULT_CIRCUIT_METRIC)
+                {
+                  vty_out (vty, " isis metric %d%s", circuit->te_metric[0],
+                           VTY_NEWLINE);
+                  write++;
+                }
+            }
+          else
+            {
+              for (i = 0; i < 2; i++)
+                {
+                  if (circuit->te_metric[i] != DEFAULT_CIRCUIT_METRIC)
+                    {
+                      vty_out (vty, " isis metric %d level-%d%s",
+                               circuit->te_metric[i], i + 1, VTY_NEWLINE);
+                      write++;
+                    }
+                }
+            }
+          if (circuit->passwd.type == ISIS_PASSWD_TYPE_HMAC_MD5)
+            {
+              vty_out (vty, " isis password md5 %s%s", circuit->passwd.passwd,
+                       VTY_NEWLINE);
+              write++;
+            }
+          else if (circuit->passwd.type == ISIS_PASSWD_TYPE_CLEARTXT)
+            {
+              vty_out (vty, " isis password clear %s%s", circuit->passwd.passwd,
+                       VTY_NEWLINE);
+              write++;
+            }
+        }
+      vty_out (vty, "!%s", VTY_NEWLINE);
     }
-    vty_out (vty, "!%s", VTY_NEWLINE);
-  }
 
   return write;
 }
@@ -862,58 +1186,45 @@
        "IS-IS Routing for IP\n"
        "Routing process tag\n")
 {
-  struct isis_circuit *c;
+  struct isis_circuit *circuit;
   struct interface *ifp;
   struct isis_area *area;
 
   ifp = (struct interface *) vty->index;
   assert (ifp);
 
-  area = isis_area_lookup (argv[0]);
-
-  /* Prevent more than one circuit per interface */
-  if (area)
-    c = circuit_lookup_by_ifp (ifp, area->circuit_list);
-  else
-    c = NULL;
-  if (c && (ifp->info != NULL))
+  /* Prevent more than one area per circuit */
+  circuit = circuit_scan_by_ifp (ifp);
+  if (circuit)
     {
-#ifdef HAVE_IPV6
-      if (c->ipv6_router == 0)
-	{
-#endif /* HAVE_IPV6 */
-	  /* FIXME: Find the way to warn only vty users. */
-	  /* vty_out (vty, "ISIS circuit is already defined%s", VTY_NEWLINE); */
-	  return CMD_WARNING;
-#ifdef HAVE_IPV6
-	}
-#endif /* HAVE_IPV6 */
+      if (circuit->ip_router == 1)
+        {
+          if (strcmp (circuit->area->area_tag, argv[0]))
+            {
+              vty_out (vty, "ISIS circuit is already defined on %s%s",
+                       circuit->area->area_tag, VTY_NEWLINE);
+              return CMD_ERR_NOTHING_TODO;
+            }
+          return CMD_SUCCESS;
+        }
     }
 
-  /* this is here for ciscopability */
-  if (!area)
+  if (isis_area_get (vty, argv[0]) != CMD_SUCCESS)
     {
-      /* FIXME: Find the way to warn only vty users. */
-      /* vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE); */
-      return CMD_WARNING;
+      vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
     }
+  area = vty->index;
 
-  if (!c)
-    {
-      c = circuit_lookup_by_ifp (ifp, isis->init_circ_list);
-      c = isis_csm_state_change (ISIS_ENABLE, c, area);
-      c->interface = ifp;	/* this is automatic */
-      ifp->info = c;		/* hardly related to the FSM */
-    }
+  circuit = isis_csm_state_change (ISIS_ENABLE, circuit, area);
+  isis_circuit_if_bind (circuit, ifp);
 
-  if (!c)
-    return CMD_WARNING;
-
-  c->ip_router = 1;
+  circuit->ip_router = 1;
   area->ip_circuits++;
-  circuit_update_nlpids (c);
+  circuit_update_nlpids (circuit);
 
   vty->node = INTERFACE_NODE;
+  vty->index = ifp;
 
   return CMD_SUCCESS;
 }
@@ -927,28 +1238,33 @@
        "IS-IS Routing for IP\n"
        "Routing process tag\n")
 {
-  struct isis_circuit *circuit = NULL;
   struct interface *ifp;
   struct isis_area *area;
-  struct listnode *node;
+  struct isis_circuit *circuit;
 
   ifp = (struct interface *) vty->index;
-  assert (ifp);
+  if (!ifp)
+    {
+      vty_out (vty, "Invalid interface %s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
 
   area = isis_area_lookup (argv[0]);
   if (!area)
     {
-      vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE);
-      return CMD_WARNING;
+      vty_out (vty, "Can't find ISIS instance %s%s",
+               argv[0], VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
     }
-  for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
-    if (circuit->interface == ifp)
-      break;
+
+  circuit = circuit_lookup_by_ifp (ifp, area->circuit_list);
   if (!circuit)
     {
-      vty_out (vty, "Can't find ISIS interface %s", VTY_NEWLINE);
-      return CMD_WARNING;
+      vty_out (vty, "ISIS is not enabled on circuit %s%s",
+               ifp->name, VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
     }
+
   circuit->ip_router = 0;
   area->ip_circuits--;
 #ifdef HAVE_IPV6
@@ -959,6 +1275,183 @@
   return CMD_SUCCESS;
 }
 
+#ifdef HAVE_IPV6
+DEFUN (ipv6_router_isis,
+       ipv6_router_isis_cmd,
+       "ipv6 router isis WORD",
+       "IPv6 interface subcommands\n"
+       "IPv6 Router interface commands\n"
+       "IS-IS Routing for IPv6\n"
+       "Routing process tag\n")
+{
+  struct isis_circuit *circuit;
+  struct interface *ifp;
+  struct isis_area *area;
+
+  ifp = (struct interface *) vty->index;
+  assert (ifp);
+
+  /* Prevent more than one area per circuit */
+  circuit = circuit_scan_by_ifp (ifp);
+  if (circuit)
+    {
+      if (circuit->ipv6_router == 1)
+      {
+        if (strcmp (circuit->area->area_tag, argv[0]))
+          {
+            vty_out (vty, "ISIS circuit is already defined for IPv6 on %s%s",
+                     circuit->area->area_tag, VTY_NEWLINE);
+            return CMD_ERR_NOTHING_TODO;
+          }
+        return CMD_SUCCESS;
+      }
+    }
+
+  if (isis_area_get (vty, argv[0]) != CMD_SUCCESS)
+    {
+      vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
+  area = vty->index;
+
+  circuit = isis_csm_state_change (ISIS_ENABLE, circuit, area);
+  isis_circuit_if_bind (circuit, ifp);
+
+  circuit->ipv6_router = 1;
+  area->ipv6_circuits++;
+  circuit_update_nlpids (circuit);
+
+  vty->node = INTERFACE_NODE;
+  vty->index = ifp;
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_ipv6_router_isis,
+       no_ipv6_router_isis_cmd,
+       "no ipv6 router isis WORD",
+       NO_STR
+       "IPv6 interface subcommands\n"
+       "IPv6 Router interface commands\n"
+       "IS-IS Routing for IPv6\n"
+       "Routing process tag\n")
+{
+  struct interface *ifp;
+  struct isis_area *area;
+  struct listnode *node;
+  struct isis_circuit *circuit;
+
+  ifp = (struct interface *) vty->index;
+  if (!ifp)
+    {
+      vty_out (vty, "Invalid interface %s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
+
+  area = isis_area_lookup (argv[0]);
+  if (!area)
+    {
+      vty_out (vty, "Can't find ISIS instance %s%s",
+               argv[0], VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
+
+  circuit = circuit_lookup_by_ifp (ifp, area->circuit_list);
+  if (!circuit)
+    {
+      vty_out (vty, "ISIS is not enabled on circuit %s%s",
+               ifp->name, VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
+
+  circuit->ipv6_router = 0;
+  area->ipv6_circuits--;
+  if (circuit->ip_router == 0)
+    isis_csm_state_change (ISIS_DISABLE, circuit, area);
+
+  return CMD_SUCCESS;
+}
+#endif /* HAVE_IPV6 */
+
+DEFUN (isis_passive,
+       isis_passive_cmd,
+       "isis passive",
+       "IS-IS commands\n"
+       "Configure the passive mode for interface\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  if (circuit->is_passive == 1)
+    return CMD_SUCCESS;
+
+  if (circuit->state != C_STATE_UP)
+    {
+      circuit->is_passive = 1;
+    }
+  else
+    {
+      struct isis_area *area = circuit->area;
+      isis_csm_state_change (ISIS_DISABLE, circuit, area);
+      circuit->is_passive = 1;
+      isis_csm_state_change (ISIS_ENABLE, circuit, area);
+    }
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_isis_passive,
+       no_isis_passive_cmd,
+       "no isis passive",
+       NO_STR
+       "IS-IS commands\n"
+       "Configure the passive mode for interface\n")
+{
+  struct interface *ifp;
+  struct isis_circuit *circuit;
+
+  ifp = (struct interface *) vty->index;
+  if (!ifp)
+    {
+      vty_out (vty, "Invalid interface %s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
+
+  /* FIXME: what is wrong with circuit = ifp->info ? */
+  circuit = circuit_scan_by_ifp (ifp);
+  if (!circuit)
+    {
+      vty_out (vty, "ISIS is not enabled on circuit %s%s",
+               ifp->name, VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
+
+  if (if_is_loopback(ifp))
+    {
+      vty_out (vty, "Can't set no passive for loopback interface%s",
+               VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  if (circuit->is_passive == 0)
+    return CMD_SUCCESS;
+
+  if (circuit->state != C_STATE_UP)
+    {
+      circuit->is_passive = 0;
+    }
+  else
+    {
+      struct isis_area *area = circuit->area;
+      isis_csm_state_change (ISIS_DISABLE, circuit, area);
+      circuit->is_passive = 0;
+      isis_csm_state_change (ISIS_ENABLE, circuit, area);
+    }
+
+  return CMD_SUCCESS;
+}
+
 DEFUN (isis_circuit_type,
        isis_circuit_type_cmd,
        "isis circuit-type (level-1|level-1-2|level-2-only)",
@@ -968,41 +1461,27 @@
        "Level-1-2 adjacencies are formed\n"
        "Level-2 only adjacencies are formed\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
-  int circuit_t;
-  int is_type;
+  int circuit_type;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  /* UGLY - will remove l8r */
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-
-  /* XXX what to do when ip_router_isis is not executed */
-  if (circuit->area == NULL)
-    return CMD_WARNING;
-
-  assert (circuit);
-
-  circuit_t = string2circuit_t (argv[0]);
-
-  if (!circuit_t)
+  circuit_type = string2circuit_t (argv[0]);
+  if (!circuit_type)
     {
       vty_out (vty, "Unknown circuit-type %s", VTY_NEWLINE);
-      return CMD_SUCCESS;
+      return CMD_ERR_AMBIGUOUS;
     }
 
-  is_type = circuit->area->is_type;
-  if (is_type == IS_LEVEL_1_AND_2 || is_type == circuit_t)
-    isis_event_circuit_type_change (circuit, circuit_t);
-  else
+  if (circuit->state == C_STATE_UP &&
+      circuit->area->is_type != IS_LEVEL_1_AND_2 &&
+      circuit->area->is_type != circuit_type)
     {
-      vty_out (vty, "invalid circuit level for area %s.%s",
-	       circuit->area->area_tag, VTY_NEWLINE);
+      vty_out (vty, "Invalid circuit level for area %s.%s",
+               circuit->area->area_tag, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
     }
+  isis_event_circuit_type_change (circuit, circuit_type);
 
   return CMD_SUCCESS;
 }
@@ -1017,22 +1496,19 @@
        "Level-1-2 adjacencies are formed\n"
        "Level-2 only adjacencies are formed\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-
-  assert (circuit);
+  int circuit_type;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   /*
-   * Set the circuits level to its default value which is that of the area
+   * Set the circuits level to its default value
    */
-  isis_event_circuit_type_change (circuit, circuit->area->is_type);
+  if (circuit->state == C_STATE_UP)
+    circuit_type = circuit->area->is_type;
+  else
+    circuit_type = IS_LEVEL_1_AND_2;
+  isis_event_circuit_type_change (circuit, circuit_type);
 
   return CMD_SUCCESS;
 }
@@ -1041,26 +1517,20 @@
        isis_passwd_md5_cmd,
        "isis password md5 WORD",
        "IS-IS commands\n"
-       "Configure the authentication password for interface\n"
-       "Authentication Type\n"
-       "Password\n")
+       "Configure the authentication password for a circuit\n"
+       "Authentication type\n"
+       "Circuit password\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int len;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   len = strlen (argv[0]);
   if (len > 254)
     {
       vty_out (vty, "Too long circuit password (>254)%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_AMBIGUOUS;
     }
   circuit->passwd.len = len;
   circuit->passwd.type = ISIS_PASSWD_TYPE_HMAC_MD5;
@@ -1073,26 +1543,20 @@
        isis_passwd_clear_cmd,
        "isis password clear WORD",
        "IS-IS commands\n"
-       "Configure the authentication password for interface\n"
-       "Authentication Type\n"
-       "Password\n")
+       "Configure the authentication password for a circuit\n"
+       "Authentication type\n"
+       "Circuit password\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int len;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   len = strlen (argv[0]);
   if (len > 254)
     {
       vty_out (vty, "Too long circuit password (>254)%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_AMBIGUOUS;
     }
   circuit->passwd.len = len;
   circuit->passwd.type = ISIS_PASSWD_TYPE_CLEARTXT;
@@ -1106,17 +1570,11 @@
        "no isis password",
        NO_STR
        "IS-IS commands\n"
-       "Configure the authentication password for interface\n")
+       "Configure the authentication password for a circuit\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   memset (&circuit->passwd, 0, sizeof (struct isis_passwd));
 
@@ -1130,22 +1588,21 @@
        "Set priority for Designated Router election\n"
        "Priority value\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int prio;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   prio = atoi (argv[0]);
+  if (prio < MIN_PRIORITY || prio > MAX_PRIORITY)
+    {
+      vty_out (vty, "Invalid priority %d - should be <0-127>%s",
+               prio, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
-  circuit->u.bc.priority[0] = prio;
-  circuit->u.bc.priority[1] = prio;
+  circuit->priority[0] = prio;
+  circuit->priority[1] = prio;
 
   return CMD_SUCCESS;
 }
@@ -1157,19 +1614,12 @@
        "IS-IS commands\n"
        "Set priority for Designated Router election\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->u.bc.priority[0] = DEFAULT_PRIORITY;
-  circuit->u.bc.priority[1] = DEFAULT_PRIORITY;
+  circuit->priority[0] = DEFAULT_PRIORITY;
+  circuit->priority[1] = DEFAULT_PRIORITY;
 
   return CMD_SUCCESS;
 }
@@ -1190,21 +1640,20 @@
        "Priority value\n"
        "Specify priority for level-1 routing\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int prio;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   prio = atoi (argv[0]);
+  if (prio < MIN_PRIORITY || prio > MAX_PRIORITY)
+    {
+      vty_out (vty, "Invalid priority %d - should be <0-127>%s",
+               prio, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
-  circuit->u.bc.priority[0] = prio;
+  circuit->priority[0] = prio;
 
   return CMD_SUCCESS;
 }
@@ -1217,18 +1666,11 @@
        "Set priority for Designated Router election\n"
        "Specify priority for level-1 routing\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->u.bc.priority[0] = DEFAULT_PRIORITY;
+  circuit->priority[0] = DEFAULT_PRIORITY;
 
   return CMD_SUCCESS;
 }
@@ -1250,21 +1692,20 @@
        "Priority value\n"
        "Specify priority for level-2 routing\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int prio;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   prio = atoi (argv[0]);
+  if (prio < MIN_PRIORITY || prio > MAX_PRIORITY)
+    {
+      vty_out (vty, "Invalid priority %d - should be <0-127>%s",
+               prio, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
-  circuit->u.bc.priority[1] = prio;
+  circuit->priority[1] = prio;
 
   return CMD_SUCCESS;
 }
@@ -1277,18 +1718,11 @@
        "Set priority for Designated Router election\n"
        "Specify priority for level-2 routing\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->u.bc.priority[1] = DEFAULT_PRIORITY;
+  circuit->priority[1] = DEFAULT_PRIORITY;
 
   return CMD_SUCCESS;
 }
@@ -1303,36 +1737,49 @@
        "Specify priority for level-2 routing\n")
 
 /* Metric command */
-  DEFUN (isis_metric,
+DEFUN (isis_metric,
        isis_metric_cmd,
        "isis metric <0-16777215>",
        "IS-IS commands\n"
        "Set default metric for circuit\n"
        "Default metric value\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int met;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   met = atoi (argv[0]);
 
+  /* RFC3787 section 5.1 */
+  if (circuit->area && circuit->area->oldmetric == 1 &&
+      met > MAX_NARROW_LINK_METRIC)
+    {
+      vty_out (vty, "Invalid metric %d - should be <0-63> "
+               "when narrow metric type enabled%s",
+               met, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  /* RFC4444 */
+  if (circuit->area && circuit->area->newmetric == 1 &&
+      met > MAX_WIDE_LINK_METRIC)
+    {
+      vty_out (vty, "Invalid metric %d - should be <0-16777215> "
+               "when wide metric type enabled%s",
+               met, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
   circuit->te_metric[0] = met;
   circuit->te_metric[1] = met;
 
-  if (met > 63)
-    met = 63;
-
   circuit->metrics[0].metric_default = met;
   circuit->metrics[1].metric_default = met;
 
+  if (circuit->area)
+    lsp_regenerate_schedule (circuit->area, circuit->is_type, 0);
+
   return CMD_SUCCESS;
 }
 
@@ -1343,21 +1790,17 @@
        "IS-IS commands\n"
        "Set default metric for circuit\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  circuit->te_metric[0] = DEFAULT_CIRCUIT_METRIC;
+  circuit->te_metric[1] = DEFAULT_CIRCUIT_METRIC;
+  circuit->metrics[0].metric_default = DEFAULT_CIRCUIT_METRIC;
+  circuit->metrics[1].metric_default = DEFAULT_CIRCUIT_METRIC;
 
-  circuit->te_metric[0] = DEFAULT_CIRCUIT_METRICS;
-  circuit->te_metric[1] = DEFAULT_CIRCUIT_METRICS;
-  circuit->metrics[0].metric_default = DEFAULT_CIRCUIT_METRICS;
-  circuit->metrics[1].metric_default = DEFAULT_CIRCUIT_METRICS;
+  if (circuit->area)
+    lsp_regenerate_schedule (circuit->area, circuit->is_type, 0);
 
   return CMD_SUCCESS;
 }
@@ -1370,34 +1813,175 @@
        "Set default metric for circuit\n"
        "Default metric value\n")
 
+DEFUN (isis_metric_l1,
+       isis_metric_l1_cmd,
+       "isis metric <0-16777215> level-1",
+       "IS-IS commands\n"
+       "Set default metric for circuit\n"
+       "Default metric value\n"
+       "Specify metric for level-1 routing\n")
+{
+  int met;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  met = atoi (argv[0]);
+
+  /* RFC3787 section 5.1 */
+  if (circuit->area && circuit->area->oldmetric == 1 &&
+      met > MAX_NARROW_LINK_METRIC)
+    {
+      vty_out (vty, "Invalid metric %d - should be <0-63> "
+               "when narrow metric type enabled%s",
+               met, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  /* RFC4444 */
+  if (circuit->area && circuit->area->newmetric == 1 &&
+      met > MAX_WIDE_LINK_METRIC)
+    {
+      vty_out (vty, "Invalid metric %d - should be <0-16777215> "
+               "when wide metric type enabled%s",
+               met, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  circuit->te_metric[0] = met;
+  circuit->metrics[0].metric_default = met;
+
+  if (circuit->area)
+    lsp_regenerate_schedule (circuit->area, IS_LEVEL_1, 0);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_isis_metric_l1,
+       no_isis_metric_l1_cmd,
+       "no isis metric level-1",
+       NO_STR
+       "IS-IS commands\n"
+       "Set default metric for circuit\n"
+       "Specify metric for level-1 routing\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  circuit->te_metric[0] = DEFAULT_CIRCUIT_METRIC;
+  circuit->metrics[0].metric_default = DEFAULT_CIRCUIT_METRIC;
+
+  if (circuit->area)
+    lsp_regenerate_schedule (circuit->area, IS_LEVEL_1, 0);
+
+  return CMD_SUCCESS;
+}
+
+ALIAS (no_isis_metric_l1,
+       no_isis_metric_l1_arg_cmd,
+       "no isis metric <0-16777215> level-1",
+       NO_STR
+       "IS-IS commands\n"
+       "Set default metric for circuit\n"
+       "Default metric value\n"
+       "Specify metric for level-1 routing\n")
+
+DEFUN (isis_metric_l2,
+       isis_metric_l2_cmd,
+       "isis metric <0-16777215> level-2",
+       "IS-IS commands\n"
+       "Set default metric for circuit\n"
+       "Default metric value\n"
+       "Specify metric for level-2 routing\n")
+{
+  int met;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  met = atoi (argv[0]);
+
+  /* RFC3787 section 5.1 */
+  if (circuit->area && circuit->area->oldmetric == 1 &&
+      met > MAX_NARROW_LINK_METRIC)
+    {
+      vty_out (vty, "Invalid metric %d - should be <0-63> "
+               "when narrow metric type enabled%s",
+               met, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  /* RFC4444 */
+  if (circuit->area && circuit->area->newmetric == 1 &&
+      met > MAX_WIDE_LINK_METRIC)
+    {
+      vty_out (vty, "Invalid metric %d - should be <0-16777215> "
+               "when wide metric type enabled%s",
+               met, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  circuit->te_metric[1] = met;
+  circuit->metrics[1].metric_default = met;
+
+  if (circuit->area)
+    lsp_regenerate_schedule (circuit->area, IS_LEVEL_2, 0);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_isis_metric_l2,
+       no_isis_metric_l2_cmd,
+       "no isis metric level-2",
+       NO_STR
+       "IS-IS commands\n"
+       "Set default metric for circuit\n"
+       "Specify metric for level-2 routing\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  circuit->te_metric[1] = DEFAULT_CIRCUIT_METRIC;
+  circuit->metrics[1].metric_default = DEFAULT_CIRCUIT_METRIC;
+
+  if (circuit->area)
+    lsp_regenerate_schedule (circuit->area, IS_LEVEL_2, 0);
+
+  return CMD_SUCCESS;
+}
+
+ALIAS (no_isis_metric_l2,
+       no_isis_metric_l2_arg_cmd,
+       "no isis metric <0-16777215> level-2",
+       NO_STR
+       "IS-IS commands\n"
+       "Set default metric for circuit\n"
+       "Default metric value\n"
+       "Specify metric for level-2 routing\n")
 /* end of metrics */
+
 DEFUN (isis_hello_interval,
        isis_hello_interval_cmd,
-       "isis hello-interval (<1-65535>|minimal)",
+       "isis hello-interval <1-600>",
        "IS-IS commands\n"
        "Set Hello interval\n"
        "Hello interval value\n"
        "Holdtime 1 seconds, interval depends on multiplier\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int interval;
-  char c;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
+  interval = atoi (argv[0]);
+  if (interval < MIN_HELLO_INTERVAL || interval > MAX_HELLO_INTERVAL)
     {
-      return CMD_WARNING;
+      vty_out (vty, "Invalid hello-interval %d - should be <1-600>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
     }
-  assert (circuit);
-  c = *argv[0];
-  if (isdigit ((int) c))
-    {
-      interval = atoi (argv[0]);
-    }
-  else
-    interval = HELLO_MINIMAL;	/* FIXME: should be calculated */
 
   circuit->hello_interval[0] = (u_int16_t) interval;
   circuit->hello_interval[1] = (u_int16_t) interval;
@@ -1412,27 +1996,19 @@
        "IS-IS commands\n"
        "Set Hello interval\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-
-  circuit->hello_interval[0] = HELLO_INTERVAL;	/* Default is 1 sec. */
-  circuit->hello_interval[1] = HELLO_INTERVAL;
+  circuit->hello_interval[0] = DEFAULT_HELLO_INTERVAL;
+  circuit->hello_interval[1] = DEFAULT_HELLO_INTERVAL;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_isis_hello_interval,
        no_isis_hello_interval_arg_cmd,
-       "no isis hello-interval (<1-65535>|minimal)",
+       "no isis hello-interval <1-600>",
        NO_STR
        "IS-IS commands\n"
        "Set Hello interval\n"
@@ -1441,33 +2017,25 @@
 
 DEFUN (isis_hello_interval_l1,
        isis_hello_interval_l1_cmd,
-       "isis hello-interval (<1-65535>|minimal) level-1",
+       "isis hello-interval <1-600> level-1",
        "IS-IS commands\n"
        "Set Hello interval\n"
        "Hello interval value\n"
        "Holdtime 1 second, interval depends on multiplier\n"
        "Specify hello-interval for level-1 IIHs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   long interval;
-  char c;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
+  interval = atoi (argv[0]);
+  if (interval < MIN_HELLO_INTERVAL || interval > MAX_HELLO_INTERVAL)
     {
-      return CMD_WARNING;
+      vty_out (vty, "Invalid hello-interval %ld - should be <1-600>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
     }
-  assert (circuit);
-
-  c = *argv[0];
-  if (isdigit ((int) c))
-    {
-      interval = atoi (argv[0]);
-    }
-  else
-    interval = HELLO_MINIMAL;
 
   circuit->hello_interval[0] = (u_int16_t) interval;
 
@@ -1482,26 +2050,18 @@
        "Set Hello interval\n"
        "Specify hello-interval for level-1 IIHs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-
-  circuit->hello_interval[0] = HELLO_INTERVAL;	/* Default is 1 sec. */
+  circuit->hello_interval[0] = DEFAULT_HELLO_INTERVAL;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_isis_hello_interval_l1,
        no_isis_hello_interval_l1_arg_cmd,
-       "no isis hello-interval (<1-65535>|minimal) level-1",
+       "no isis hello-interval <1-600> level-1",
        NO_STR
        "IS-IS commands\n"
        "Set Hello interval\n"
@@ -1511,33 +2071,25 @@
 
 DEFUN (isis_hello_interval_l2,
        isis_hello_interval_l2_cmd,
-       "isis hello-interval (<1-65535>|minimal) level-2",
+       "isis hello-interval <1-600> level-2",
        "IS-IS commands\n"
        "Set Hello interval\n"
        "Hello interval value\n"
        "Holdtime 1 second, interval depends on multiplier\n"
        "Specify hello-interval for level-2 IIHs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   long interval;
-  char c;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
+  interval = atoi (argv[0]);
+  if (interval < MIN_HELLO_INTERVAL || interval > MAX_HELLO_INTERVAL)
     {
-      return CMD_WARNING;
+      vty_out (vty, "Invalid hello-interval %ld - should be <1-600>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
     }
-  assert (circuit);
-
-  c = *argv[0];
-  if (isdigit ((int) c))
-    {
-      interval = atoi (argv[0]);
-    }
-  else
-    interval = HELLO_MINIMAL;
 
   circuit->hello_interval[1] = (u_int16_t) interval;
 
@@ -1552,26 +2104,18 @@
        "Set Hello interval\n"
        "Specify hello-interval for level-2 IIHs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-
-  circuit->hello_interval[1] = HELLO_INTERVAL;	/* Default is 1 sec. */
+  circuit->hello_interval[1] = DEFAULT_HELLO_INTERVAL;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_isis_hello_interval_l2,
        no_isis_hello_interval_l2_arg_cmd,
-       "no isis hello-interval (<1-65535>|minimal) level-2",
+       "no isis hello-interval <1-600> level-2",
        NO_STR
        "IS-IS commands\n"
        "Set Hello interval\n"
@@ -1581,24 +2125,23 @@
 
 DEFUN (isis_hello_multiplier,
        isis_hello_multiplier_cmd,
-       "isis hello-multiplier <3-1000>",
+       "isis hello-multiplier <2-100>",
        "IS-IS commands\n"
        "Set multiplier for Hello holding time\n"
        "Hello multiplier value\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int mult;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   mult = atoi (argv[0]);
+  if (mult < MIN_HELLO_MULTIPLIER || mult > MAX_HELLO_MULTIPLIER)
+    {
+      vty_out (vty, "Invalid hello-multiplier %d - should be <2-100>%s",
+               mult, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
   circuit->hello_multiplier[0] = (u_int16_t) mult;
   circuit->hello_multiplier[1] = (u_int16_t) mult;
@@ -1613,26 +2156,19 @@
        "IS-IS commands\n"
        "Set multiplier for Hello holding time\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->hello_multiplier[0] = HELLO_MULTIPLIER;
-  circuit->hello_multiplier[1] = HELLO_MULTIPLIER;
+  circuit->hello_multiplier[0] = DEFAULT_HELLO_MULTIPLIER;
+  circuit->hello_multiplier[1] = DEFAULT_HELLO_MULTIPLIER;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_isis_hello_multiplier,
        no_isis_hello_multiplier_arg_cmd,
-       "no isis hello-multiplier <3-1000>",
+       "no isis hello-multiplier <2-100>",
        NO_STR
        "IS-IS commands\n"
        "Set multiplier for Hello holding time\n"
@@ -1640,25 +2176,24 @@
 
 DEFUN (isis_hello_multiplier_l1,
        isis_hello_multiplier_l1_cmd,
-       "isis hello-multiplier <3-1000> level-1",
+       "isis hello-multiplier <2-100> level-1",
        "IS-IS commands\n"
        "Set multiplier for Hello holding time\n"
        "Hello multiplier value\n"
        "Specify hello multiplier for level-1 IIHs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int mult;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   mult = atoi (argv[0]);
+  if (mult < MIN_HELLO_MULTIPLIER || mult > MAX_HELLO_MULTIPLIER)
+    {
+      vty_out (vty, "Invalid hello-multiplier %d - should be <2-100>%s",
+               mult, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
   circuit->hello_multiplier[0] = (u_int16_t) mult;
 
@@ -1673,25 +2208,18 @@
        "Set multiplier for Hello holding time\n"
        "Specify hello multiplier for level-1 IIHs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->hello_multiplier[0] = HELLO_MULTIPLIER;
+  circuit->hello_multiplier[0] = DEFAULT_HELLO_MULTIPLIER;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_isis_hello_multiplier_l1,
        no_isis_hello_multiplier_l1_arg_cmd,
-       "no isis hello-multiplier <3-1000> level-1",
+       "no isis hello-multiplier <2-100> level-1",
        NO_STR
        "IS-IS commands\n"
        "Set multiplier for Hello holding time\n"
@@ -1700,25 +2228,24 @@
 
 DEFUN (isis_hello_multiplier_l2,
        isis_hello_multiplier_l2_cmd,
-       "isis hello-multiplier <3-1000> level-2",
+       "isis hello-multiplier <2-100> level-2",
        "IS-IS commands\n"
        "Set multiplier for Hello holding time\n"
        "Hello multiplier value\n"
        "Specify hello multiplier for level-2 IIHs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   int mult;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   mult = atoi (argv[0]);
+  if (mult < MIN_HELLO_MULTIPLIER || mult > MAX_HELLO_MULTIPLIER)
+    {
+      vty_out (vty, "Invalid hello-multiplier %d - should be <2-100>%s",
+               mult, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
   circuit->hello_multiplier[1] = (u_int16_t) mult;
 
@@ -1733,57 +2260,43 @@
        "Set multiplier for Hello holding time\n"
        "Specify hello multiplier for level-2 IIHs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->hello_multiplier[1] = HELLO_MULTIPLIER;
+  circuit->hello_multiplier[1] = DEFAULT_HELLO_MULTIPLIER;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_isis_hello_multiplier_l2,
        no_isis_hello_multiplier_l2_arg_cmd,
-       "no isis hello-multiplier <3-1000> level-2",
+       "no isis hello-multiplier <2-100> level-2",
        NO_STR
        "IS-IS commands\n"
        "Set multiplier for Hello holding time\n"
        "Hello multiplier value\n"
        "Specify hello multiplier for level-2 IIHs\n")
 
-DEFUN (isis_hello,
-       isis_hello_cmd,
+DEFUN (isis_hello_padding,
+       isis_hello_padding_cmd,
        "isis hello padding",
        "IS-IS commands\n"
        "Add padding to IS-IS hello packets\n"
        "Pad hello packets\n"
        "<cr>\n")
 {
-  struct interface *ifp;
-  struct isis_circuit *circuit;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->u.bc.pad_hellos = 1;
+  circuit->pad_hellos = 1;
 
   return CMD_SUCCESS;
 }
 
-DEFUN (no_isis_hello,
-       no_isis_hello_cmd,
+DEFUN (no_isis_hello_padding,
+       no_isis_hello_padding_cmd,
        "no isis hello padding",
        NO_STR
        "IS-IS commands\n"
@@ -1791,42 +2304,34 @@
        "Pad hello packets\n"
        "<cr>\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->u.bc.pad_hellos = 0;
+  circuit->pad_hellos = 0;
 
   return CMD_SUCCESS;
 }
 
 DEFUN (csnp_interval,
        csnp_interval_cmd,
-       "isis csnp-interval <0-65535>",
+       "isis csnp-interval <1-600>",
        "IS-IS commands\n"
        "Set CSNP interval in seconds\n"
        "CSNP interval value\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   unsigned long interval;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   interval = atol (argv[0]);
+  if (interval < MIN_CSNP_INTERVAL || interval > MAX_CSNP_INTERVAL)
+    {
+      vty_out (vty, "Invalid csnp-interval %lu - should be <1-600>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
   circuit->csnp_interval[0] = (u_int16_t) interval;
   circuit->csnp_interval[1] = (u_int16_t) interval;
@@ -1841,26 +2346,19 @@
        "IS-IS commands\n"
        "Set CSNP interval in seconds\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->csnp_interval[0] = CSNP_INTERVAL;
-  circuit->csnp_interval[1] = CSNP_INTERVAL;
+  circuit->csnp_interval[0] = DEFAULT_CSNP_INTERVAL;
+  circuit->csnp_interval[1] = DEFAULT_CSNP_INTERVAL;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_csnp_interval,
        no_csnp_interval_arg_cmd,
-       "no isis csnp-interval <0-65535>",
+       "no isis csnp-interval <1-600>",
        NO_STR
        "IS-IS commands\n"
        "Set CSNP interval in seconds\n"
@@ -1868,25 +2366,24 @@
 
 DEFUN (csnp_interval_l1,
        csnp_interval_l1_cmd,
-       "isis csnp-interval <0-65535> level-1",
+       "isis csnp-interval <1-600> level-1",
        "IS-IS commands\n"
        "Set CSNP interval in seconds\n"
        "CSNP interval value\n"
        "Specify interval for level-1 CSNPs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   unsigned long interval;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   interval = atol (argv[0]);
+  if (interval < MIN_CSNP_INTERVAL || interval > MAX_CSNP_INTERVAL)
+    {
+      vty_out (vty, "Invalid csnp-interval %lu - should be <1-600>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
   circuit->csnp_interval[0] = (u_int16_t) interval;
 
@@ -1901,25 +2398,18 @@
        "Set CSNP interval in seconds\n"
        "Specify interval for level-1 CSNPs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->csnp_interval[0] = CSNP_INTERVAL;
+  circuit->csnp_interval[0] = DEFAULT_CSNP_INTERVAL;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_csnp_interval_l1,
        no_csnp_interval_l1_arg_cmd,
-       "no isis csnp-interval <0-65535> level-1",
+       "no isis csnp-interval <1-600> level-1",
        NO_STR
        "IS-IS commands\n"
        "Set CSNP interval in seconds\n"
@@ -1928,25 +2418,24 @@
 
 DEFUN (csnp_interval_l2,
        csnp_interval_l2_cmd,
-       "isis csnp-interval <0-65535> level-2",
+       "isis csnp-interval <1-600> level-2",
        "IS-IS commands\n"
        "Set CSNP interval in seconds\n"
        "CSNP interval value\n"
        "Specify interval for level-2 CSNPs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
   unsigned long interval;
-
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
   interval = atol (argv[0]);
+  if (interval < MIN_CSNP_INTERVAL || interval > MAX_CSNP_INTERVAL)
+    {
+      vty_out (vty, "Invalid csnp-interval %lu - should be <1-600>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
 
   circuit->csnp_interval[1] = (u_int16_t) interval;
 
@@ -1961,153 +2450,285 @@
        "Set CSNP interval in seconds\n"
        "Specify interval for level-2 CSNPs\n")
 {
-  struct isis_circuit *circuit;
-  struct interface *ifp;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = vty->index;
-  circuit = ifp->info;
-  if (circuit == NULL)
-    {
-      return CMD_WARNING;
-    }
-  assert (circuit);
-
-  circuit->csnp_interval[1] = CSNP_INTERVAL;
+  circuit->csnp_interval[1] = DEFAULT_CSNP_INTERVAL;
 
   return CMD_SUCCESS;
 }
 
 ALIAS (no_csnp_interval_l2,
        no_csnp_interval_l2_arg_cmd,
-       "no isis csnp-interval <0-65535> level-2",
+       "no isis csnp-interval <1-600> level-2",
        NO_STR
        "IS-IS commands\n"
        "Set CSNP interval in seconds\n"
        "CSNP interval value\n"
        "Specify interval for level-2 CSNPs\n")
 
-#ifdef HAVE_IPV6
-DEFUN (ipv6_router_isis,
-       ipv6_router_isis_cmd,
-       "ipv6 router isis WORD",
-       "IPv6 interface subcommands\n"
-       "IPv6 Router interface commands\n"
-       "IS-IS Routing for IPv6\n"
-       "Routing process tag\n")
+DEFUN (psnp_interval,
+       psnp_interval_cmd,
+       "isis psnp-interval <1-120>",
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n"
+       "PSNP interval value\n")
 {
-  struct isis_circuit *c;
-  struct interface *ifp;
-  struct isis_area *area;
+  unsigned long interval;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = (struct interface *) vty->index;
-  assert (ifp);
-
-  area = isis_area_lookup (argv[0]);
-
-  /* Prevent more than one circuit per interface */
-  if (area)
-    c = circuit_lookup_by_ifp (ifp, area->circuit_list);
-  else
-    c = NULL;
-
-  if (c && (ifp->info != NULL))
+  interval = atol (argv[0]);
+  if (interval < MIN_PSNP_INTERVAL || interval > MAX_PSNP_INTERVAL)
     {
-      if (c->ipv6_router == 1)
-	{
-	  vty_out (vty, "ISIS circuit is already defined for IPv6%s",
-		   VTY_NEWLINE);
-	  return CMD_WARNING;
-	}
+      vty_out (vty, "Invalid psnp-interval %lu - should be <1-120>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
     }
 
-  /* this is here for ciscopability */
-  if (!area)
-    {
-      vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
-
-  if (!c)
-    {
-      c = circuit_lookup_by_ifp (ifp, isis->init_circ_list);
-      c = isis_csm_state_change (ISIS_ENABLE, c, area);
-      c->interface = ifp;
-      ifp->info = c;
-    }
-
-  if (!c)
-    return CMD_WARNING;
-
-  c->ipv6_router = 1;
-  area->ipv6_circuits++;
-  circuit_update_nlpids (c);
-
-  vty->node = INTERFACE_NODE;
+  circuit->psnp_interval[0] = (u_int16_t) interval;
+  circuit->psnp_interval[1] = (u_int16_t) interval;
 
   return CMD_SUCCESS;
 }
 
-DEFUN (no_ipv6_router_isis,
-       no_ipv6_router_isis_cmd,
-       "no ipv6 router isis WORD",
+DEFUN (no_psnp_interval,
+       no_psnp_interval_cmd,
+       "no isis psnp-interval",
        NO_STR
-       "IPv6 interface subcommands\n"
-       "IPv6 Router interface commands\n"
-       "IS-IS Routing for IPv6\n"
-       "Routing process tag\n")
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n")
 {
-  struct isis_circuit *c;
-  struct interface *ifp;
-  struct isis_area *area;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
 
-  ifp = (struct interface *) vty->index;
-  /* UGLY - will remove l8r
-     if (circuit == NULL) {
-     return CMD_WARNING;
-     } */
-  assert (ifp);
-
-  area = isis_area_lookup (argv[0]);
-  if (!area)
-    {
-      vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE);
-      return CMD_WARNING;
-    }
-
-  c = circuit_lookup_by_ifp (ifp, area->circuit_list);
-  if (!c)
-    return CMD_WARNING;
-
-  c->ipv6_router = 0;
-  area->ipv6_circuits--;
-  if (c->ip_router == 0)
-    isis_csm_state_change (ISIS_DISABLE, c, area);
+  circuit->psnp_interval[0] = DEFAULT_PSNP_INTERVAL;
+  circuit->psnp_interval[1] = DEFAULT_PSNP_INTERVAL;
 
   return CMD_SUCCESS;
 }
-#endif /* HAVE_IPV6 */
 
-static struct cmd_node interface_node = {
+ALIAS (no_psnp_interval,
+       no_psnp_interval_arg_cmd,
+       "no isis psnp-interval <1-120>",
+       NO_STR
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n"
+       "PSNP interval value\n")
+
+DEFUN (psnp_interval_l1,
+       psnp_interval_l1_cmd,
+       "isis psnp-interval <1-120> level-1",
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n"
+       "PSNP interval value\n"
+       "Specify interval for level-1 PSNPs\n")
+{
+  unsigned long interval;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  interval = atol (argv[0]);
+  if (interval < MIN_PSNP_INTERVAL || interval > MAX_PSNP_INTERVAL)
+    {
+      vty_out (vty, "Invalid psnp-interval %lu - should be <1-120>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  circuit->psnp_interval[0] = (u_int16_t) interval;
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_psnp_interval_l1,
+       no_psnp_interval_l1_cmd,
+       "no isis psnp-interval level-1",
+       NO_STR
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n"
+       "Specify interval for level-1 PSNPs\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  circuit->psnp_interval[0] = DEFAULT_PSNP_INTERVAL;
+
+  return CMD_SUCCESS;
+}
+
+ALIAS (no_psnp_interval_l1,
+       no_psnp_interval_l1_arg_cmd,
+       "no isis psnp-interval <1-120> level-1",
+       NO_STR
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n"
+       "PSNP interval value\n"
+       "Specify interval for level-1 PSNPs\n")
+
+DEFUN (psnp_interval_l2,
+       psnp_interval_l2_cmd,
+       "isis psnp-interval <1-120> level-2",
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n"
+       "PSNP interval value\n"
+       "Specify interval for level-2 PSNPs\n")
+{
+  unsigned long interval;
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  interval = atol (argv[0]);
+  if (interval < MIN_PSNP_INTERVAL || interval > MAX_PSNP_INTERVAL)
+    {
+      vty_out (vty, "Invalid psnp-interval %lu - should be <1-120>%s",
+               interval, VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  circuit->psnp_interval[1] = (u_int16_t) interval;
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_psnp_interval_l2,
+       no_psnp_interval_l2_cmd,
+       "no isis psnp-interval level-2",
+       NO_STR
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n"
+       "Specify interval for level-2 PSNPs\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  circuit->psnp_interval[1] = DEFAULT_PSNP_INTERVAL;
+
+  return CMD_SUCCESS;
+}
+
+ALIAS (no_psnp_interval_l2,
+       no_psnp_interval_l2_arg_cmd,
+       "no isis psnp-interval <1-120> level-2",
+       NO_STR
+       "IS-IS commands\n"
+       "Set PSNP interval in seconds\n"
+       "PSNP interval value\n"
+       "Specify interval for level-2 PSNPs\n")
+
+struct cmd_node interface_node = {
   INTERFACE_NODE,
   "%s(config-if)# ",
   1,
 };
 
+DEFUN (isis_network,
+       isis_network_cmd,
+       "isis network point-to-point",
+       "IS-IS commands\n"
+       "Set network type\n"
+       "point-to-point network type\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  /* RFC5309 section 4 */
+  if (circuit->circ_type == CIRCUIT_T_P2P)
+    return CMD_SUCCESS;
+
+  if (circuit->state != C_STATE_UP)
+    {
+      circuit->circ_type = CIRCUIT_T_P2P;
+      circuit->circ_type_config = CIRCUIT_T_P2P;
+    }
+  else
+    {
+      struct isis_area *area = circuit->area;
+      if (!if_is_broadcast (circuit->interface))
+        {
+          vty_out (vty, "isis network point-to-point "
+                   "is valid only on broadcast interfaces%s",
+                   VTY_NEWLINE);
+          return CMD_ERR_AMBIGUOUS;
+        }
+
+      isis_csm_state_change (ISIS_DISABLE, circuit, area);
+      circuit->circ_type = CIRCUIT_T_P2P;
+      circuit->circ_type_config = CIRCUIT_T_P2P;
+      isis_csm_state_change (ISIS_ENABLE, circuit, area);
+    }
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_isis_network,
+       no_isis_network_cmd,
+       "no isis network point-to-point",
+       NO_STR
+       "IS-IS commands\n"
+       "Set network type for circuit\n"
+       "point-to-point network type\n")
+{
+  struct isis_circuit *circuit = isis_circuit_lookup (vty);
+  if (!circuit)
+    return CMD_ERR_NO_MATCH;
+
+  /* RFC5309 section 4 */
+  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+    return CMD_SUCCESS;
+
+  if (circuit->state != C_STATE_UP)
+    {
+      circuit->circ_type = CIRCUIT_T_BROADCAST;
+      circuit->circ_type_config = CIRCUIT_T_BROADCAST;
+    }
+  else
+    {
+      struct isis_area *area = circuit->area;
+      if (circuit->interface &&
+          !if_is_broadcast (circuit->interface))
+      {
+        vty_out (vty, "no isis network point-to-point "
+                 "is valid only on broadcast interfaces%s",
+                 VTY_NEWLINE);
+        return CMD_ERR_AMBIGUOUS;
+      }
+
+      isis_csm_state_change (ISIS_DISABLE, circuit, area);
+      circuit->circ_type = CIRCUIT_T_BROADCAST;
+      circuit->circ_type_config = CIRCUIT_T_BROADCAST;
+      isis_csm_state_change (ISIS_ENABLE, circuit, area);
+    }
+
+  return CMD_SUCCESS;
+}
+
 int
 isis_if_new_hook (struct interface *ifp)
 {
-/* FIXME: Discuss if the circuit should be created here
-  ifp->info = XMALLOC (MTYPE_ISIS_IF_INFO, sizeof (struct isis_if_info)); */
-  ifp->info = NULL;
   return 0;
 }
 
 int
 isis_if_delete_hook (struct interface *ifp)
 {
-/* FIXME: Discuss if the circuit should be created here
-  XFREE (MTYPE_ISIS_IF_INFO, ifp->info);*/
-  ifp->info = NULL;
+  struct isis_circuit *circuit;
+  /* Clean up the circuit data */
+  if (ifp && ifp->info)
+    {
+      circuit = ifp->info;
+      isis_csm_state_change (IF_DOWN_FROM_Z, circuit, circuit->area);
+      isis_csm_state_change (ISIS_DISABLE, circuit, circuit->area);
+    }
+
   return 0;
 }
 
@@ -2122,6 +2743,7 @@
   /* Install interface node */
   install_node (&interface_node, isis_interface_config_write);
   install_element (CONFIG_NODE, &interface_cmd);
+  install_element (CONFIG_NODE, &no_interface_cmd);
 
   install_default (INTERFACE_NODE);
   install_element (INTERFACE_NODE, &interface_desc_cmd);
@@ -2130,6 +2752,9 @@
   install_element (INTERFACE_NODE, &ip_router_isis_cmd);
   install_element (INTERFACE_NODE, &no_ip_router_isis_cmd);
 
+  install_element (INTERFACE_NODE, &isis_passive_cmd);
+  install_element (INTERFACE_NODE, &no_isis_passive_cmd);
+
   install_element (INTERFACE_NODE, &isis_circuit_type_cmd);
   install_element (INTERFACE_NODE, &no_isis_circuit_type_cmd);
 
@@ -2150,6 +2775,12 @@
   install_element (INTERFACE_NODE, &isis_metric_cmd);
   install_element (INTERFACE_NODE, &no_isis_metric_cmd);
   install_element (INTERFACE_NODE, &no_isis_metric_arg_cmd);
+  install_element (INTERFACE_NODE, &isis_metric_l1_cmd);
+  install_element (INTERFACE_NODE, &no_isis_metric_l1_cmd);
+  install_element (INTERFACE_NODE, &no_isis_metric_l1_arg_cmd);
+  install_element (INTERFACE_NODE, &isis_metric_l2_cmd);
+  install_element (INTERFACE_NODE, &no_isis_metric_l2_cmd);
+  install_element (INTERFACE_NODE, &no_isis_metric_l2_arg_cmd);
 
   install_element (INTERFACE_NODE, &isis_hello_interval_cmd);
   install_element (INTERFACE_NODE, &no_isis_hello_interval_cmd);
@@ -2171,8 +2802,9 @@
   install_element (INTERFACE_NODE, &no_isis_hello_multiplier_l2_cmd);
   install_element (INTERFACE_NODE, &no_isis_hello_multiplier_l2_arg_cmd);
 
-  install_element (INTERFACE_NODE, &isis_hello_cmd);
-  install_element (INTERFACE_NODE, &no_isis_hello_cmd);
+  install_element (INTERFACE_NODE, &isis_hello_padding_cmd);
+  install_element (INTERFACE_NODE, &no_isis_hello_padding_cmd);
+
   install_element (INTERFACE_NODE, &csnp_interval_cmd);
   install_element (INTERFACE_NODE, &no_csnp_interval_cmd);
   install_element (INTERFACE_NODE, &no_csnp_interval_arg_cmd);
@@ -2183,6 +2815,19 @@
   install_element (INTERFACE_NODE, &no_csnp_interval_l2_cmd);
   install_element (INTERFACE_NODE, &no_csnp_interval_l2_arg_cmd);
 
+  install_element (INTERFACE_NODE, &psnp_interval_cmd);
+  install_element (INTERFACE_NODE, &no_psnp_interval_cmd);
+  install_element (INTERFACE_NODE, &no_psnp_interval_arg_cmd);
+  install_element (INTERFACE_NODE, &psnp_interval_l1_cmd);
+  install_element (INTERFACE_NODE, &no_psnp_interval_l1_cmd);
+  install_element (INTERFACE_NODE, &no_psnp_interval_l1_arg_cmd);
+  install_element (INTERFACE_NODE, &psnp_interval_l2_cmd);
+  install_element (INTERFACE_NODE, &no_psnp_interval_l2_cmd);
+  install_element (INTERFACE_NODE, &no_psnp_interval_l2_arg_cmd);
+
+  install_element (INTERFACE_NODE, &isis_network_cmd);
+  install_element (INTERFACE_NODE, &no_isis_network_cmd);
+
 #ifdef HAVE_IPV6
   install_element (INTERFACE_NODE, &ipv6_router_isis_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_router_isis_cmd);
diff --git a/isisd/isis_circuit.h b/isisd/isis_circuit.h
index f32d1dd..7ed481d 100644
--- a/isisd/isis_circuit.h
+++ b/isisd/isis_circuit.h
@@ -52,8 +52,6 @@
   u_char l1_desig_is[ISIS_SYS_ID_LEN + 1];	/* level-1 DR */
   u_char l2_desig_is[ISIS_SYS_ID_LEN + 1];	/* level-2 DR */
   struct thread *t_refresh_pseudo_lsp[2];	/* refresh pseudo-node LSPs */
-  int pad_hellos;		/* add padding to Hello PDUs ? */
-  u_char priority[2];		/* l1/2 IS Priority */
 };
 
 struct isis_p2p_info
@@ -65,7 +63,6 @@
 struct isis_circuit
 {
   int state;
-  int connected;
   u_char circuit_id;		/* l1/l2 p2p/bcast CircuitID */
   struct isis_area *area;	/* back pointer to the area */
   struct interface *interface;	/* interface info from z */
@@ -79,31 +76,36 @@
   struct thread *t_send_csnp[2];
   struct thread *t_send_psnp[2];
   struct list *lsp_queue;	/* LSPs to be txed (both levels) */
+  time_t lsp_queue_last_cleared;/* timestamp used to enforce transmit interval;
+                                 * for scalability, use one timestamp per 
+                                 * circuit, instead of one per lsp per circuit
+                                 */
   /* there is no real point in two streams, just for programming kicker */
   int (*rx) (struct isis_circuit * circuit, u_char * ssnpa);
   struct stream *rcv_stream;	/* Stream for receiving */
   int (*tx) (struct isis_circuit * circuit, int level);
   struct stream *snd_stream;	/* Stream for sending */
   int idx;			/* idx in S[RM|SN] flags */
-#define CIRCUIT_T_BROADCAST  0
-#define CIRCUIT_T_P2P        1
-#define CIRCUIT_T_STATIC_IN  2
-#define CIRCUIT_T_STATIC_OUT 3
-#define CIRCUIT_T_DA         4
+#define CIRCUIT_T_UNKNOWN    0
+#define CIRCUIT_T_BROADCAST  1
+#define CIRCUIT_T_P2P        2
+#define CIRCUIT_T_LOOPBACK   3
   int circ_type;		/* type of the physical interface */
+  int circ_type_config;		/* config type of the physical interface */
   union
   {
     struct isis_bcast_info bc;
     struct isis_p2p_info p2p;
   } u;
+  u_char priority[2];		/* l1/2 IS configured priority */
+  int pad_hellos;		/* add padding to Hello PDUs ? */
   char ext_domain;		/* externalDomain   (boolean) */
+  int lsp_regenerate_pending[ISIS_LEVELS];
   /* 
    * Configurables 
    */
   struct isis_passwd passwd;	/* Circuit rx/tx password */
-  long lsp_interval;
-  int manual_l2_only;		/* manualL2OnlyMode (boolean) */
-  int circuit_is_type;		/* circuit is type == level of circuit
+  int is_type;	                /* circuit is type == level of circuit
 				 * diffrenciated from circuit type (media) */
   u_int32_t hello_interval[2];	/* l1HelloInterval in msecs */
   u_int16_t hello_multiplier[2];	/* l1HelloMultiplier */
@@ -111,24 +113,17 @@
   u_int16_t psnp_interval[2];	/* level-1 psnp-interval in seconds */
   struct metric metrics[2];	/* l1XxxMetric */
   u_int32_t te_metric[2];
-  struct password *c_rx_passwds;	/* circuitReceivePasswords */
-  struct password *c_tc_passwd;	/* circuitTransmitPassword */
   int ip_router;		/* Route IP ? */
+  int is_passive;		/* Is Passive ? */
   struct list *ip_addrs;	/* our IP addresses */
 #ifdef HAVE_IPV6
   int ipv6_router;		/* Route IPv6 ? */
   struct list *ipv6_link;	/* our link local IPv6 addresses */
   struct list *ipv6_non_link;	/* our non-link local IPv6 addresses */
 #endif				/* HAVE_IPV6 */
-  /* 
-   * RFC 2973 IS-IS Mesh Groups 
-   */
-#define MESH_INACTIVE 0
-#define MESH_BLOCKED  1
-#define MESH_SET      2
-  int mesh_enabled;		/* meshGroupEnabled */
-  u_int16_t mesh_group;		/* meshGroup */
   u_int16_t upadjcount[2];
+#define ISIS_CIRCUIT_FLAPPED_AFTER_SPF 0x01
+  u_char flags;
   /*
    * Counters as in 10589--11.2.5.9
    */
@@ -142,25 +137,30 @@
 
 void isis_circuit_init (void);
 struct isis_circuit *isis_circuit_new (void);
+void isis_circuit_del (struct isis_circuit *circuit);
 struct isis_circuit *circuit_lookup_by_ifp (struct interface *ifp,
 					    struct list *list);
 struct isis_circuit *circuit_scan_by_ifp (struct interface *ifp);
-void isis_circuit_del (struct isis_circuit *circuit);
 void isis_circuit_configure (struct isis_circuit *circuit,
 			     struct isis_area *area);
-void isis_circuit_up (struct isis_circuit *circuit);
 void isis_circuit_deconfigure (struct isis_circuit *circuit,
 			       struct isis_area *area);
-
-int isis_circuit_destroy (struct isis_circuit *circuit);
 void isis_circuit_if_add (struct isis_circuit *circuit,
 			  struct interface *ifp);
-void isis_circuit_if_del (struct isis_circuit *circuit);
-void circuit_update_nlpids (struct isis_circuit *circuit);
-void isis_circuit_update_params (struct isis_circuit *circuit,
-				 struct interface *ifp);
+void isis_circuit_if_del (struct isis_circuit *circuit,
+			  struct interface *ifp);
+void isis_circuit_if_bind (struct isis_circuit *circuit,
+                           struct interface *ifp);
+void isis_circuit_if_unbind (struct isis_circuit *circuit,
+                             struct interface *ifp);
 void isis_circuit_add_addr (struct isis_circuit *circuit,
 			    struct connected *conn);
 void isis_circuit_del_addr (struct isis_circuit *circuit,
 			    struct connected *conn);
+int isis_circuit_up (struct isis_circuit *circuit);
+void isis_circuit_down (struct isis_circuit *);
+void circuit_update_nlpids (struct isis_circuit *circuit);
+void isis_circuit_print_vty (struct isis_circuit *circuit, struct vty *vty,
+                             char detail);
+
 #endif /* _ZEBRA_ISIS_CIRCUIT_H */
diff --git a/isisd/isis_common.h b/isisd/isis_common.h
index 334d339..d158961 100644
--- a/isisd/isis_common.h
+++ b/isisd/isis_common.h
@@ -21,6 +21,9 @@
  * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  */
 
+#ifndef ISIS_COMMON_H
+#define ISIS_COMMON_H
+
 /*
  * Area Address
  */
@@ -65,11 +68,4 @@
   u_char nlpids[4];		/* FIXME: enough ? */
 };
 
-/*
- * Flags structure for SSN and SRM flags
- */
-struct flags
-{
-  int maxindex;
-  struct list *free_idcs;
-};
+#endif
diff --git a/isisd/isis_constants.h b/isisd/isis_constants.h
index 1b75ba6..bb2c4b4 100644
--- a/isisd/isis_constants.h
+++ b/isisd/isis_constants.h
@@ -27,8 +27,10 @@
  * Architectural constant values from p. 35 of ISO/IEC 10589
  */
 
-#define MAX_LINK_METRIC               63
-#define MAX_PATH_METRIC               1023
+#define MAX_NARROW_LINK_METRIC        63
+#define MAX_NARROW_PATH_METRIC        1023
+#define MAX_WIDE_LINK_METRIC          0x00FFFFFF  /* RFC4444 */
+#define MAX_WIDE_PATH_METRIC          0xFE000000  /* RFC3787 */
 #define ISO_SAP                       0xFE
 #define INTRADOMAIN_ROUTEING_SELECTOR 0
 #define SEQUENCE_MODULUS              4294967296
@@ -38,7 +40,7 @@
  * implementation specific jitter values
  */
 
-#define IIH_JITTER                    25	/* % */
+#define IIH_JITTER                    10	/* % */
 #define MAX_AGE_JITTER                 5	/* % */
 #define MAX_LSP_GEN_JITTER             5	/* % */
 #define CSNP_JITTER                   10	/* % */
@@ -46,36 +48,59 @@
 
 #define RANDOM_SPREAD           100000.0
 
-/*
- * Default values
- * ISO - 10589
- * Section 7.3.21 - Parameters
- */
-#define MAX_AGE                       1200
-#define ZERO_AGE_LIFETIME             60
-#define MAX_LSP_GEN_INTERVAL          900
-#define MIN_LSP_GEN_INTERVAL          30
-#define MIN_LSP_TRANS_INTERVAL        5
-#define ISIS_MIN_LSP_LIFETIME         380
-#define CSNP_INTERVAL                 10
-#define PSNP_INTERVAL                 2
-#define ISIS_MAX_PATH_SPLITS          3
-
 #define ISIS_LEVELS                   2
 #define ISIS_LEVEL1                   1
 #define ISIS_LEVEL2                   2
 
-#define HELLO_INTERVAL                10
-#define HELLO_MINIMAL HELLO_INTERVAL
-#define HELLO_MULTIPLIER              3
+/*
+ * Default values
+ * ISO - 10589 Section 7.3.21 - Parameters
+ * RFC 4444
+ */
+#define MAX_AGE                       1200
+#define ZERO_AGE_LIFETIME             60
+#define MIN_LSP_LIFETIME              350
+#define MAX_LSP_LIFETIME              65535
+#define DEFAULT_LSP_LIFETIME          1200
+
+#define MIN_MAX_LSP_GEN_INTERVAL      1
+#define MAX_MAX_LSP_GEN_INTERVAL      65235
+#define DEFAULT_MAX_LSP_GEN_INTERVAL  900
+
+#define MIN_MIN_LSP_GEN_INTERVAL      1
+#define MAX_MIN_LSP_GEN_INTERVAL      120  /* RFC 4444 says 65535 */
+#define DEFAULT_MIN_LSP_GEN_INTERVAL  30
+
+#define MIN_LSP_TRANS_INTERVAL        5
+
+#define MIN_CSNP_INTERVAL             1
+#define MAX_CSNP_INTERVAL             600
+#define DEFAULT_CSNP_INTERVAL         10
+
+#define MIN_PSNP_INTERVAL             1
+#define MAX_PSNP_INTERVAL             120
+#define DEFAULT_PSNP_INTERVAL         2
+
+#define MIN_HELLO_INTERVAL            1
+#define MAX_HELLO_INTERVAL            600
+#define DEFAULT_HELLO_INTERVAL        3
+
+#define MIN_HELLO_MULTIPLIER          2
+#define MAX_HELLO_MULTIPLIER          100
+#define DEFAULT_HELLO_MULTIPLIER      10
+
+#define MIN_PRIORITY                  0
+#define MAX_PRIORITY                  127
 #define DEFAULT_PRIORITY              64
-/* different vendors implement different values 5-10 on average */
-#define LSP_GEN_INTERVAL_DEFAULT      10
-#define LSP_INTERVAL                  33	/* msecs */
-#define DEFAULT_CIRCUIT_METRICS 10
-#define METRICS_UNSUPPORTED 0x80
-#define PERIODIC_SPF_INTERVAL         60	/* at the top of my head */
-#define MINIMUM_SPF_INTERVAL           5	/* .. same here          */
+
+/* min and max metric varies by new vs old metric types */
+#define DEFAULT_CIRCUIT_METRIC        10
+
+#define METRICS_UNSUPPORTED           0x80
+
+#define MINIMUM_SPF_INTERVAL          1
+
+#define ISIS_MAX_PATH_SPLITS          64
 
 /*
  * NLPID values
@@ -104,6 +129,7 @@
 
 #define SNPA_ADDRSTRLEN 18
 #define ISIS_SYS_ID_LEN  6
+#define ISIS_NSEL_LEN    1
 #define SYSID_STRLEN    24
 
 /*
@@ -136,8 +162,8 @@
  * packets, using isomtu = mtu - LLC_LEN
  */
 #define ISO_MTU(C) \
-          (C->circ_type==CIRCUIT_T_BROADCAST) ? \
-          (C->interface->mtu - LLC_LEN) : (C->interface->mtu)
+          ((if_is_broadcast ((C)->interface)) ? \
+           (C->interface->mtu - LLC_LEN) : (C->interface->mtu))
 
 #ifndef ETH_ALEN
 #define ETH_ALEN 6
diff --git a/isisd/isis_csm.c b/isisd/isis_csm.c
index 6cdde46..5d74a71 100644
--- a/isisd/isis_csm.c
+++ b/isisd/isis_csm.c
@@ -36,6 +36,7 @@
 #include "isisd/include-netbsd/iso.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
+#include "isisd/isis_flags.h"
 #include "isisd/isis_circuit.h"
 #include "isisd/isis_tlv.h"
 #include "isisd/isis_lsp.h"
@@ -45,7 +46,6 @@
 #include "isisd/isis_constants.h"
 #include "isisd/isis_adjacency.h"
 #include "isisd/isis_dr.h"
-#include "isisd/isis_flags.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_csm.h"
 #include "isisd/isis_events.h"
@@ -85,6 +85,7 @@
     case C_STATE_NA:
       if (circuit)
 	zlog_warn ("Non-null circuit while state C_STATE_NA");
+      assert (circuit == NULL);
       switch (event)
 	{
 	case ISIS_ENABLE:
@@ -106,24 +107,29 @@
 	}
       break;
     case C_STATE_INIT:
+      assert (circuit);
       switch (event)
 	{
 	case ISIS_ENABLE:
 	  isis_circuit_configure (circuit, (struct isis_area *) arg);
-	  isis_circuit_up (circuit);
+	  if (isis_circuit_up (circuit) != ISIS_OK)
+	    {
+	      isis_circuit_deconfigure (circuit, (struct isis_area *) arg);
+	      break;
+	    }
 	  circuit->state = C_STATE_UP;
-	  circuit->connected = 1;
-	  isis_event_circuit_state_change (circuit, 1);
+	  isis_event_circuit_state_change (circuit, circuit->area, 1);
 	  listnode_delete (isis->init_circ_list, circuit);
 	  break;
 	case IF_UP_FROM_Z:
+          assert (circuit);
 	  zlog_warn ("circuit already connected");
 	  break;
 	case ISIS_DISABLE:
 	  zlog_warn ("circuit already disabled");
 	  break;
 	case IF_DOWN_FROM_Z:
-	  isis_circuit_if_del (circuit);
+	  isis_circuit_if_del (circuit, (struct interface *) arg);
 	  listnode_delete (isis->init_circ_list, circuit);
 	  isis_circuit_del (circuit);
 	  circuit = NULL;
@@ -131,19 +137,21 @@
 	}
       break;
     case C_STATE_CONF:
+      assert (circuit);
       switch (event)
 	{
 	case ISIS_ENABLE:
 	  zlog_warn ("circuit already enabled");
 	  break;
 	case IF_UP_FROM_Z:
-	  if (!circuit->connected) {
-	    isis_circuit_if_add (circuit, (struct interface *) arg);
-	    isis_circuit_up (circuit);
-	  }
+	  isis_circuit_if_add (circuit, (struct interface *) arg);
+	  if (isis_circuit_up (circuit) != ISIS_OK)
+            {
+              isis_circuit_if_del (circuit, (struct interface *) arg);
+	      break;
+            }
 	  circuit->state = C_STATE_UP;
-	  circuit->connected = 1;
-	  isis_event_circuit_state_change (circuit, 1);
+	  isis_event_circuit_state_change (circuit, circuit->area, 1);
 	  break;
 	case ISIS_DISABLE:
 	  isis_circuit_deconfigure (circuit, (struct isis_area *) arg);
@@ -156,6 +164,7 @@
 	}
       break;
     case C_STATE_UP:
+      assert (circuit);
       switch (event)
 	{
 	case ISIS_ENABLE:
@@ -165,14 +174,18 @@
 	  zlog_warn ("circuit already connected");
 	  break;
 	case ISIS_DISABLE:
+	  isis_circuit_down (circuit);
 	  isis_circuit_deconfigure (circuit, (struct isis_area *) arg);
-	  listnode_add (isis->init_circ_list, circuit);
 	  circuit->state = C_STATE_INIT;
-	  isis_event_circuit_state_change (circuit, 0);
+	  isis_event_circuit_state_change (circuit,
+                                           (struct isis_area *)arg, 0);
+	  listnode_add (isis->init_circ_list, circuit);
 	  break;
 	case IF_DOWN_FROM_Z:
+	  isis_circuit_down (circuit);
+          isis_circuit_if_del (circuit, (struct interface *) arg);
 	  circuit->state = C_STATE_CONF;
-	  isis_event_circuit_state_change (circuit, 0);
+	  isis_event_circuit_state_change (circuit, circuit->area, 0);
 	  break;
 	}
       break;
diff --git a/isisd/isis_dlpi.c b/isisd/isis_dlpi.c
index fe872a9..73b6d3e 100644
--- a/isisd/isis_dlpi.c
+++ b/isisd/isis_dlpi.c
@@ -442,12 +442,12 @@
    * 8.4.2 - Broadcast subnetwork IIH PDUs
    */
   retval = 0;
-  if (circuit->circuit_is_type & IS_LEVEL_1)
+  if (circuit->is_type & IS_LEVEL_1)
     {
       retval |= dlpimcast (fd, ALL_L1_ISS);
       retval |= dlpimcast (fd, ALL_ISS);
     }
-  if (circuit->circuit_is_type & IS_LEVEL_2)
+  if (circuit->is_type & IS_LEVEL_2)
     retval |= dlpimcast (fd, ALL_L2_ISS);
 
   if (retval != 0)
@@ -589,6 +589,16 @@
   dl_unitdata_req_t *dur = (dl_unitdata_req_t *)dlpi_ctl;
   char *dstaddr;
   u_short *dstsap;
+  int buflen;
+
+  buflen = stream_get_endp (circuit->snd_stream) + LLC_LEN;
+  if (buflen > sizeof (sock_buff))
+    {
+      zlog_warn ("isis_send_pdu_bcast: sock_buff size %lu is less than "
+		 "output pdu size %d on circuit %s",
+		 sizeof (sock_buff), buflen, circuit->interface->name);
+      return ISIS_WARNING;
+    }
 
   stream_set_getp (circuit->snd_stream, 0);
 
@@ -612,7 +622,7 @@
   else
     memcpy (dstaddr, ALL_L2_ISS, ETHERADDRL);
   /* Note: DLPI SAP values are in host byte order */
-  *dstsap = stream_get_endp (circuit->snd_stream) + LLC_LEN;
+  *dstsap = buflen;
 
   sock_buff[0] = ISO_SAP;
   sock_buff[1] = ISO_SAP;
@@ -620,7 +630,7 @@
   memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
 	  stream_get_endp (circuit->snd_stream));
   dlpisend (circuit->fd, dur, sizeof (*dur) + dur->dl_dest_addr_length,
-    sock_buff, stream_get_endp (circuit->snd_stream) + LLC_LEN, 0);
+	    sock_buff, buflen, 0);
   return ISIS_OK;
 }
 
diff --git a/isisd/isis_dr.c b/isisd/isis_dr.c
index 8d306c8..bc6ec11 100644
--- a/isisd/isis_dr.c
+++ b/isisd/isis_dr.c
@@ -47,9 +47,6 @@
 #include "isisd/isis_dr.h"
 #include "isisd/isis_events.h"
 
-extern struct isis *isis;
-extern struct thread_master *master;
-
 const char *
 isis_disflag2string (int disflag)
 {
@@ -137,15 +134,14 @@
   int biggest_prio = -1;
   int cmp_res, retval = ISIS_OK;
 
-  own_prio = circuit->u.bc.priority[level - 1];
+  own_prio = circuit->priority[level - 1];
   adjdb = circuit->u.bc.adjdb[level - 1];
 
   if (!adjdb)
     {
       zlog_warn ("isis_dr_elect() adjdb == NULL");
-      retval = ISIS_WARNING;
       list_delete (list);
-      goto out;
+      return ISIS_WARNING;
     }
   isis_adj_build_up_list (adjdb, list);
 
@@ -189,42 +185,34 @@
   if (!adj_dr)
     {
       /*
-       * Could not find the DR - means we are alone and thus the DR
+       * Could not find the DR - means we are alone. Resign if we were DR.
        */
-      if (!circuit->u.bc.is_dr[level - 1])
-	{
-	  list_delete (list);
-	  list = NULL;
-	  return isis_dr_commence (circuit, level);
-	}
-      goto out;
+      if (circuit->u.bc.is_dr[level - 1])
+        retval = isis_dr_resign (circuit, level);
+      list_delete (list);
+      return retval;
     }
 
   /*
    * Now we have the DR adjacency, compare it to self
    */
-  if (adj_dr->prio[level - 1] < own_prio
-      || (adj_dr->prio[level - 1] == own_prio
-	  && memcmp (adj_dr->snpa, circuit->u.bc.snpa, ETH_ALEN) < 0))
+  if (adj_dr->prio[level - 1] < own_prio ||
+      (adj_dr->prio[level - 1] == own_prio &&
+       memcmp (adj_dr->snpa, circuit->u.bc.snpa, ETH_ALEN) < 0))
     {
-      if (!circuit->u.bc.is_dr[level - 1])
-	{
-	  /*
-	   * We are the DR
-	   */
+      adj_dr->dis_record[level - 1].dis = ISIS_IS_NOT_DIS;
+      adj_dr->dis_record[level - 1].last_dis_change = time (NULL);
 
-	  /* rotate the history log */
-	  for (ALL_LIST_ELEMENTS_RO (list, node, adj))
-            isis_check_dr_change (adj, level);
+      /* rotate the history log */
+      for (ALL_LIST_ELEMENTS_RO (list, node, adj))
+        isis_check_dr_change (adj, level);
 
-	  /* commence */
-	  list_delete (list);
-	  return isis_dr_commence (circuit, level);
-	}
+      /* We are the DR, commence DR */
+      if (circuit->u.bc.is_dr[level - 1] == 0 && listcount (list) > 0)
+        retval = isis_dr_commence (circuit, level);
     }
   else
     {
-
       /* ok we have found the DIS - lets mark the adjacency */
       /* set flag for show output */
       adj_dr->dis_record[level - 1].dis = ISIS_IS_DIS;
@@ -240,16 +228,10 @@
       /*
        * We are not DR - if we were -> resign
        */
-
       if (circuit->u.bc.is_dr[level - 1])
-	{
-	  list_delete (list);
-	  return isis_dr_resign (circuit, level);
-	}
+        retval = isis_dr_resign (circuit, level);
     }
-out:
-  if (list)
-    list_delete (list);
+  list_delete (list);
   return retval;
 }
 
@@ -264,11 +246,12 @@
   circuit->u.bc.run_dr_elect[level - 1] = 0;
   THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[level - 1]);
   THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
+  circuit->lsp_regenerate_pending[level - 1] = 0;
 
   memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
   LSP_PSEUDO_ID (id) = circuit->circuit_id;
   LSP_FRAGMENT (id) = 0;
-  lsp_purge_dr (id, circuit, level);
+  lsp_purge_pseudo (id, circuit, level);
 
   if (level == 1)
     {
@@ -327,7 +310,7 @@
       if (LSP_PSEUDO_ID (old_dr))
 	{
 	  /* there was a dr elected, purge its LSPs from the db */
-	  lsp_purge_dr (old_dr, circuit, level);
+	  lsp_purge_pseudo (old_dr, circuit, level);
 	}
       memcpy (circuit->u.bc.l1_desig_is, isis->sysid, ISIS_SYS_ID_LEN);
       *(circuit->u.bc.l1_desig_is + ISIS_SYS_ID_LEN) = circuit->circuit_id;
@@ -335,7 +318,7 @@
       assert (circuit->circuit_id);	/* must be non-zero */
       /*    if (circuit->t_send_l1_psnp)
          thread_cancel (circuit->t_send_l1_psnp); */
-      lsp_l1_pseudo_generate (circuit);
+      lsp_generate_pseudo (circuit, 1);
 
       THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[0]);
       THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[0], isis_run_dr_l1,
@@ -353,7 +336,7 @@
       if (LSP_PSEUDO_ID (old_dr))
 	{
 	  /* there was a dr elected, purge its LSPs from the db */
-	  lsp_purge_dr (old_dr, circuit, level);
+	  lsp_purge_pseudo (old_dr, circuit, level);
 	}
       memcpy (circuit->u.bc.l2_desig_is, isis->sysid, ISIS_SYS_ID_LEN);
       *(circuit->u.bc.l2_desig_is + ISIS_SYS_ID_LEN) = circuit->circuit_id;
@@ -361,7 +344,7 @@
       assert (circuit->circuit_id);	/* must be non-zero */
       /*    if (circuit->t_send_l1_psnp)
          thread_cancel (circuit->t_send_l1_psnp); */
-      lsp_l2_pseudo_generate (circuit);
+      lsp_generate_pseudo (circuit, 2);
 
       THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[1]);
       THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[1], isis_run_dr_l2,
diff --git a/isisd/isis_dynhn.c b/isisd/isis_dynhn.c
index 0b758c8..ffb0d50 100644
--- a/isisd/isis_dynhn.c
+++ b/isisd/isis_dynhn.c
@@ -41,8 +41,6 @@
 #include "isisd/isis_misc.h"
 #include "isisd/isis_constants.h"
 
-extern struct isis *isis;
-extern struct thread_master *master;
 extern struct host host;
 
 struct list *dyn_cache = NULL;
@@ -51,7 +49,8 @@
 void
 dyn_cache_init (void)
 {
-  dyn_cache = list_new ();
+  if (dyn_cache == NULL)
+    dyn_cache = list_new ();
   THREAD_TIMER_ON (master, isis->t_dync_clean, dyn_cache_cleanup, NULL, 120);
   return;
 }
@@ -67,8 +66,8 @@
 
   for (ALL_LIST_ELEMENTS (dyn_cache, node, nnode, dyn))
     {
-      if ((now - dyn->refresh) < (MAX_AGE + 120))
-	continue;
+      if ((now - dyn->refresh) < MAX_LSP_LIFETIME)
+        continue;
 
       list_delete_node (dyn_cache, node);
       XFREE (MTYPE_ISIS_DYNHN, dyn);
@@ -91,6 +90,19 @@
   return NULL;
 }
 
+struct isis_dynhn *
+dynhn_find_by_name (const char *hostname)
+{
+  struct listnode *node = NULL;
+  struct isis_dynhn *dyn = NULL;
+
+  for (ALL_LIST_ELEMENTS_RO (dyn_cache, node, dyn))
+    if (strncmp ((char *)dyn->name.name, hostname, 255) == 0)
+      return dyn;
+
+  return NULL;
+}
+
 void
 isis_dynhn_insert (u_char * id, struct hostname *hostname, int level)
 {
@@ -122,6 +134,19 @@
   return;
 }
 
+void
+isis_dynhn_remove (u_char * id)
+{
+  struct isis_dynhn *dyn;
+
+  dyn = dynhn_find_by_id (id);
+  if (!dyn)
+    return;
+  listnode_delete (dyn_cache, dyn);
+  XFREE (MTYPE_ISIS_DYNHN, dyn);
+  return;
+}
+
 /*
  * Level  System ID      Dynamic Hostname  (notag)
  *  2     0000.0000.0001 foo-gw
diff --git a/isisd/isis_dynhn.h b/isisd/isis_dynhn.h
index 37a7b03..379c454 100644
--- a/isisd/isis_dynhn.h
+++ b/isisd/isis_dynhn.h
@@ -33,7 +33,9 @@
 
 void dyn_cache_init (void);
 void isis_dynhn_insert (u_char * id, struct hostname *hostname, int level);
+void isis_dynhn_remove (u_char * id);
 struct isis_dynhn *dynhn_find_by_id (u_char * id);
+struct isis_dynhn *dynhn_find_by_name (const char *hostname);
 void dynhn_print_all (struct vty *vty);
 
 #endif /* _ZEBRA_ISIS_DYNHN_H */
diff --git a/isisd/isis_events.c b/isisd/isis_events.c
index 4380092..3887b7c 100644
--- a/isisd/isis_events.c
+++ b/isisd/isis_events.c
@@ -30,11 +30,13 @@
 #include "hash.h"
 #include "prefix.h"
 #include "stream.h"
+#include "table.h"
 
 #include "isisd/dict.h"
 #include "isisd/include-netbsd/iso.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
+#include "isisd/isis_flags.h"
 #include "isisd/isis_circuit.h"
 #include "isisd/isis_tlv.h"
 #include "isisd/isis_lsp.h"
@@ -44,15 +46,11 @@
 #include "isisd/isis_constants.h"
 #include "isisd/isis_adjacency.h"
 #include "isisd/isis_dr.h"
-#include "isisd/isis_flags.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_csm.h"
 #include "isisd/isis_events.h"
 #include "isisd/isis_spf.h"
 
-extern struct thread_master *master;
-extern struct isis *isis;
-
 /* debug isis-spf spf-events 
  4w4d: ISIS-Spf (tlt): L2 SPF needed, new adjacency, from 0x609229F4
  4w4d: ISIS-Spf (tlt): L2, 0000.0000.0042.01-00 TLV contents changed, code 0x2
@@ -62,26 +60,59 @@
 */
 
 void
-isis_event_circuit_state_change (struct isis_circuit *circuit, int up)
+isis_event_circuit_state_change (struct isis_circuit *circuit,
+                                 struct isis_area *area, int up)
 {
-  struct isis_area *area;
-
-  area = circuit->area;
-  assert (area);
   area->circuit_state_changes++;
 
   if (isis->debugs & DEBUG_EVENTS)
-    zlog_debug ("ISIS-Evt (%s) circuit %s", circuit->area->area_tag,
-	       up ? "up" : "down");
+    zlog_debug ("ISIS-Evt (%s) circuit %s", area->area_tag,
+                up ? "up" : "down");
 
   /*
    * Regenerate LSPs this affects
    */
-  lsp_regenerate_schedule (area);
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 0);
 
   return;
 }
 
+static void
+area_resign_level (struct isis_area *area, int level)
+{
+  if (area->lspdb[level - 1])
+    {
+      lsp_db_destroy (area->lspdb[level - 1]);
+      area->lspdb[level - 1] = NULL;
+    }
+  if (area->spftree[level - 1])
+    {
+      isis_spftree_del (area->spftree[level - 1]);
+      area->spftree[level - 1] = NULL;
+    }
+#ifdef HAVE_IPV6
+  if (area->spftree6[level - 1])
+    {
+      isis_spftree_del (area->spftree6[level - 1]);
+      area->spftree6[level - 1] = NULL;
+    }
+#endif
+  if (area->route_table[level - 1])
+    {
+      route_table_finish (area->route_table[level - 1]);
+      area->route_table[level - 1] = NULL;
+    }
+#ifdef HAVE_IPV6
+  if (area->route_table6[level - 1])
+    {
+      route_table_finish (area->route_table6[level - 1]);
+      area->route_table6[level - 1] = NULL;
+    }
+#endif /* HAVE_IPV6 */
+
+  THREAD_TIMER_OFF (area->t_lsp_refresh[level - 1]);
+}
+
 void
 isis_event_system_type_change (struct isis_area *area, int newtype)
 {
@@ -96,59 +127,79 @@
     return;			/* No change */
 
   switch (area->is_type)
-    {
+  {
     case IS_LEVEL_1:
+      if (newtype == IS_LEVEL_2)
+        area_resign_level (area, IS_LEVEL_1);
+
       if (area->lspdb[1] == NULL)
-	area->lspdb[1] = lsp_db_init ();
-      lsp_l2_generate (area);
+        area->lspdb[1] = lsp_db_init ();
+      if (area->route_table[1] == NULL)
+        area->route_table[1] = route_table_init ();
+#ifdef HAVE_IPV6
+      if (area->route_table6[1] == NULL)
+        area->route_table6[1] = route_table_init ();
+#endif /* HAVE_IPV6 */
       break;
+
     case IS_LEVEL_1_AND_2:
       if (newtype == IS_LEVEL_1)
-	{
-	  lsp_db_destroy (area->lspdb[1]);
-	}
+        area_resign_level (area, IS_LEVEL_2);
       else
-	{
-	  lsp_db_destroy (area->lspdb[0]);
-	}
+        area_resign_level (area, IS_LEVEL_1);
       break;
+
     case IS_LEVEL_2:
+      if (newtype == IS_LEVEL_1)
+        area_resign_level (area, IS_LEVEL_2);
+
       if (area->lspdb[0] == NULL)
-	area->lspdb[0] = lsp_db_init ();
-      lsp_l1_generate (area);
+        area->lspdb[0] = lsp_db_init ();
+      if (area->route_table[0] == NULL)
+        area->route_table[0] = route_table_init ();
+#ifdef HAVE_IPV6
+      if (area->route_table6[0] == NULL)
+        area->route_table6[0] = route_table_init ();
+#endif /* HAVE_IPV6 */
       break;
+
     default:
       break;
-    }
+  }
 
   area->is_type = newtype;
-  for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
-    isis_event_circuit_type_change (circuit, newtype);
+
+  /* override circuit's is_type */
+  if (area->is_type != IS_LEVEL_1_AND_2)
+  {
+    for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
+      isis_event_circuit_type_change (circuit, newtype);
+  }
 
   spftree_area_init (area);
-  lsp_regenerate_schedule (area);
+
+  if (newtype & IS_LEVEL_1)
+    lsp_generate (area, IS_LEVEL_1);
+  if (newtype & IS_LEVEL_2)
+    lsp_generate (area, IS_LEVEL_2);
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
 
   return;
 }
 
-void
-isis_event_area_addr_change (struct isis_area *area)
-{
-
-}
-
 static void
 circuit_commence_level (struct isis_circuit *circuit, int level)
 {
   if (level == 1)
     {
-      THREAD_TIMER_ON (master, circuit->t_send_psnp[0], send_l1_psnp, circuit,
-		       isis_jitter (circuit->psnp_interval[0], PSNP_JITTER));
+      if (! circuit->is_passive)
+        THREAD_TIMER_ON (master, circuit->t_send_psnp[0], send_l1_psnp, circuit,
+		         isis_jitter (circuit->psnp_interval[0], PSNP_JITTER));
 
       if (circuit->circ_type == CIRCUIT_T_BROADCAST)
 	{
 	  THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[0], isis_run_dr_l1,
-			   circuit, 2 * circuit->hello_interval[1]);
+			   circuit, 2 * circuit->hello_interval[0]);
 
 	  THREAD_TIMER_ON (master, circuit->u.bc.t_send_lan_hello[0],
 			   send_lan_l1_hello, circuit,
@@ -160,8 +211,9 @@
     }
   else
     {
-      THREAD_TIMER_ON (master, circuit->t_send_psnp[1], send_l2_psnp, circuit,
-		       isis_jitter (circuit->psnp_interval[1], PSNP_JITTER));
+      if (! circuit->is_passive)
+        THREAD_TIMER_ON (master, circuit->t_send_psnp[1], send_l2_psnp, circuit,
+		         isis_jitter (circuit->psnp_interval[1], PSNP_JITTER));
 
       if (circuit->circ_type == CIRCUIT_T_BROADCAST)
 	{
@@ -194,6 +246,8 @@
       THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[idx]);
       THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[idx]);
       circuit->u.bc.run_dr_elect[idx] = 0;
+      list_delete (circuit->u.bc.lan_neighs[idx]);
+      circuit->u.bc.lan_neighs[idx] = NULL;
     }
 
   return;
@@ -202,14 +256,19 @@
 void
 isis_event_circuit_type_change (struct isis_circuit *circuit, int newtype)
 {
+  if (circuit->state != C_STATE_UP)
+  {
+    circuit->is_type = newtype;
+    return;
+  }
 
   if (isis->debugs & DEBUG_EVENTS)
     zlog_debug ("ISIS-Evt (%s) circuit type change %s -> %s",
 	       circuit->area->area_tag,
-	       circuit_t2string (circuit->circuit_is_type),
+	       circuit_t2string (circuit->is_type),
 	       circuit_t2string (newtype));
 
-  if (circuit->circuit_is_type == newtype)
+  if (circuit->is_type == newtype)
     return;			/* No change */
 
   if (!(newtype & circuit->area->is_type))
@@ -221,7 +280,7 @@
       return;
     }
 
-  switch (circuit->circuit_is_type)
+  switch (circuit->is_type)
     {
     case IS_LEVEL_1:
       if (newtype == IS_LEVEL_2)
@@ -243,8 +302,8 @@
       break;
     }
 
-  circuit->circuit_is_type = newtype;
-  lsp_regenerate_schedule (circuit->area);
+  circuit->is_type = newtype;
+  lsp_regenerate_schedule (circuit->area, IS_LEVEL_1 | IS_LEVEL_2, 0);
 
   return;
 }
@@ -286,7 +345,7 @@
 		adj->circuit->area->area_tag);
 
   /* LSP generation again */
-  lsp_regenerate_schedule (adj->circuit->area);
+  lsp_regenerate_schedule (adj->circuit->area, IS_LEVEL_1 | IS_LEVEL_2, 0);
 
   return;
 }
@@ -307,7 +366,7 @@
     zlog_debug ("ISIS-Evt (%s) DIS status change", circuit->area->area_tag);
 
   /* LSP generation again */
-  lsp_regenerate_schedule (circuit->area);
+  lsp_regenerate_schedule (circuit->area, IS_LEVEL_1 | IS_LEVEL_2, 0);
 
   return 0;
 }
diff --git a/isisd/isis_events.h b/isisd/isis_events.h
index 86bf051..c252f3d 100644
--- a/isisd/isis_events.h
+++ b/isisd/isis_events.h
@@ -26,13 +26,12 @@
  * Events related to area
  */
 void isis_event_system_type_change (struct isis_area *area, int newtype);
-void isis_event_area_addr_change (struct isis_area *area);
 
 /*
  * Events related to circuit
  */
 void isis_event_circuit_state_change (struct isis_circuit *circuit,
-				      int state);
+				      struct isis_area *area, int state);
 void isis_event_circuit_type_change (struct isis_circuit *circuit,
 				     int newtype);
 /*
diff --git a/isisd/isis_flags.c b/isisd/isis_flags.c
index 03c9110..ec0eaa4 100644
--- a/isisd/isis_flags.c
+++ b/isisd/isis_flags.c
@@ -36,11 +36,11 @@
   flags->free_idcs = NULL;
 }
 
-int
+long int
 flags_get_index (struct flags *flags)
 {
   struct listnode *node;
-  int index;
+  long int index;
 
   if (flags->free_idcs == NULL || flags->free_idcs->count == 0)
     {
@@ -49,7 +49,7 @@
   else
     {
       node = listhead (flags->free_idcs);
-      index = (int) listgetdata (node);
+      index = (long int) listgetdata (node);
       listnode_delete (flags->free_idcs, (void *) index);
       index--;
     }
@@ -58,7 +58,7 @@
 }
 
 void
-flags_free_index (struct flags *flags, int index)
+flags_free_index (struct flags *flags, long int index)
 {
   if (index + 1 == flags->maxindex)
     {
diff --git a/isisd/isis_flags.h b/isisd/isis_flags.h
index 13dd9e1..e2e42ad 100644
--- a/isisd/isis_flags.h
+++ b/isisd/isis_flags.h
@@ -26,28 +26,43 @@
 
 /* The grand plan is to support 1024 circuits so we have 32*32 bit flags
  * the support will be achived using the newest drafts */
-#define ISIS_MAX_CIRCUITS 32 /* = 1024 */	/*FIXME:defined in lsp.h as well */
+#define ISIS_MAX_CIRCUITS 32 /* = 1024 */
+
+/*
+ * Flags structure for SSN and SRM flags
+ */
+struct flags
+{
+  int maxindex;
+  struct list *free_idcs;
+};
 
 void flags_initialize (struct flags *flags);
-struct flags *new_flags (int size);
-int flags_get_index (struct flags *flags);
-void flags_free_index (struct flags *flags, int index);
-
+long int flags_get_index (struct flags *flags);
+void flags_free_index (struct flags *flags, long int index);
 int flags_any_set (u_int32_t * flags);
 
 #define ISIS_SET_FLAG(F,C) \
-        F[C->idx>>5] |= (1<<(C->idx & 0x1F));
+        { \
+          F[C->idx>>5] |= (1<<(C->idx & 0x1F)); \
+        }
 
 #define ISIS_CLEAR_FLAG(F,C) \
-        F[C->idx>>5] &= ~(1<<(C->idx & 0x1F));
+        { \
+          F[C->idx>>5] &= ~(1<<(C->idx & 0x1F)); \
+        }
 
-#define ISIS_CHECK_FLAG(F, C)  F[(C)->idx>>5] & (1<<(C->idx & 0x1F))
+#define ISIS_CHECK_FLAG(F, C)  (F[(C)->idx>>5] & (1<<(C->idx & 0x1F)))
 
 /* sets all u_32int_t flags to 1 */
 #define ISIS_FLAGS_SET_ALL(FLAGS) \
-        memset(FLAGS,0xFF,ISIS_MAX_CIRCUITS*4);
+        { \
+          memset(FLAGS,0xFF,ISIS_MAX_CIRCUITS*4); \
+        }
 
 #define ISIS_FLAGS_CLEAR_ALL(FLAGS) \
-        memset(FLAGS,0x00,ISIS_MAX_CIRCUITS*4);
+        { \
+          memset(FLAGS,0x00,ISIS_MAX_CIRCUITS*4); \
+        }
 
 #endif /* _ZEBRA_ISIS_FLAGS_H */
diff --git a/isisd/isis_lsp.c b/isisd/isis_lsp.c
index fd40bb3..5c1e993 100644
--- a/isisd/isis_lsp.c
+++ b/isisd/isis_lsp.c
@@ -34,10 +34,12 @@
 #include "hash.h"
 #include "if.h"
 #include "checksum.h"
+#include "md5.h"
 
 #include "isisd/dict.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
+#include "isisd/isis_flags.h"
 #include "isisd/isis_circuit.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_tlv.h"
@@ -45,7 +47,6 @@
 #include "isisd/isis_pdu.h"
 #include "isisd/isis_dynhn.h"
 #include "isisd/isis_misc.h"
-#include "isisd/isis_flags.h"
 #include "isisd/isis_csm.h"
 #include "isisd/isis_adjacency.h"
 #include "isisd/isis_spf.h"
@@ -54,15 +55,14 @@
 #include "spgrid.h"
 #endif
 
-#define LSP_MEMORY_PREASSIGN
-
-extern struct isis *isis;
-extern struct thread_master *master;
-extern struct in_addr router_id_zebra;
-
 /* staticly assigned vars for printing purposes */
 char lsp_bits_string[200];     /* FIXME: enough ? */
 
+static int lsp_l1_refresh (struct thread *thread);
+static int lsp_l2_refresh (struct thread *thread);
+static int lsp_l1_refresh_pseudo (struct thread *thread);
+static int lsp_l2_refresh_pseudo (struct thread *thread);
+
 int
 lsp_id_cmp (u_char * id1, u_char * id2)
 {
@@ -90,7 +90,7 @@
   zlog_debug ("searching db");
   for (dn = dict_first (lspdb); dn; dn = dict_next (lspdb, dn))
     {
-      zlog_debug ("%s\t%pX", rawlspid_print ((char *) dnode_getkey (dn)),
+      zlog_debug ("%s\t%pX", rawlspid_print ((u_char *) dnode_getkey (dn)),
 		  dnode_get (dn));
     }
 #endif /* EXTREME DEBUG */
@@ -109,54 +109,56 @@
   if (!lsp)
     return;
 
+  if (lsp->tlv_data.hostname)
+    isis_dynhn_remove (lsp->lsp_header->lsp_id);
+
   if (lsp->own_lsp)
     {
       if (lsp->tlv_data.nlpids)
-	XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.nlpids);
+        XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.nlpids);
       if (lsp->tlv_data.hostname)
-	XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.hostname);
+        XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.hostname);
+      if (lsp->tlv_data.router_id)
+        XFREE (MTYPE_ISIS_TLV, lsp->tlv_data.router_id);
     }
-  if (lsp->tlv_data.is_neighs)
-    list_delete (lsp->tlv_data.is_neighs);
-  if (lsp->tlv_data.te_is_neighs)
-    list_delete (lsp->tlv_data.te_is_neighs);
-  if (lsp->tlv_data.area_addrs)
-    list_delete (lsp->tlv_data.area_addrs);
-  if (lsp->tlv_data.es_neighs)
-    list_delete (lsp->tlv_data.es_neighs);
-  if (lsp->tlv_data.ipv4_addrs)
-    list_delete (lsp->tlv_data.ipv4_addrs);
-  if (lsp->tlv_data.ipv4_int_reachs)
-    list_delete (lsp->tlv_data.ipv4_int_reachs);
-  if (lsp->tlv_data.ipv4_ext_reachs)
-    list_delete (lsp->tlv_data.ipv4_ext_reachs);
-  if (lsp->tlv_data.te_ipv4_reachs)
-    list_delete (lsp->tlv_data.te_ipv4_reachs);
-#ifdef HAVE_IPV6
-  if (lsp->tlv_data.ipv6_addrs)
-    list_delete (lsp->tlv_data.ipv6_addrs);
-  if (lsp->tlv_data.ipv6_reachs)
-    list_delete (lsp->tlv_data.ipv6_reachs);
-#endif /* HAVE_IPV6 */
 
-  memset (&lsp->tlv_data, 0, sizeof (struct tlvs));
-
-  return;
+  free_tlvs (&lsp->tlv_data);
 }
 
 static void
 lsp_destroy (struct isis_lsp *lsp)
 {
+  struct listnode *cnode, *lnode, *lnnode;
+  struct isis_lsp *lsp_in_list;
+  struct isis_circuit *circuit;
+
   if (!lsp)
     return;
 
+  for (ALL_LIST_ELEMENTS_RO (lsp->area->circuit_list, cnode, circuit))
+    {
+      if (circuit->lsp_queue == NULL)
+        continue;
+      for (ALL_LIST_ELEMENTS (circuit->lsp_queue, lnode, lnnode, lsp_in_list))
+        if (lsp_in_list == lsp)
+          list_delete_node(circuit->lsp_queue, lnode);
+    }
+  ISIS_FLAGS_CLEAR_ALL (lsp->SSNflags);
+  ISIS_FLAGS_CLEAR_ALL (lsp->SRMflags);
+
   lsp_clear_data (lsp);
 
   if (LSP_FRAGMENT (lsp->lsp_header->lsp_id) == 0 && lsp->lspu.frags)
     {
       list_delete (lsp->lspu.frags);
+      lsp->lspu.frags = NULL;
     }
 
+  isis_spf_schedule (lsp->area, lsp->level);
+#ifdef HAVE_IPV6
+  isis_spf_schedule6 (lsp->area, lsp->level);
+#endif
+
   if (lsp->pdu)
     stream_free (lsp->pdu);
   XFREE (MTYPE_ISIS_LSP, lsp);
@@ -254,7 +256,7 @@
     {
       if (isis->debugs & DEBUG_SNP_PACKETS)
 	{
-	  zlog_debug ("ISIS-Snp (%s): LSP %s seq 0x%08x, cksum 0x%04x,"
+	  zlog_debug ("ISIS-Snp (%s): Compare LSP %s seq 0x%08x, cksum 0x%04x,"
 		      " lifetime %us",
 		      areatag,
 		      rawlspid_print (lsp->lsp_header->lsp_id),
@@ -273,7 +275,7 @@
     {
       if (isis->debugs & DEBUG_SNP_PACKETS)
 	{
-	  zlog_debug ("ISIS-Snp (%s): LSP %s seq 0x%08x, cksum 0x%04x,"
+	  zlog_debug ("ISIS-Snp (%s): Compare LSP %s seq 0x%08x, cksum 0x%04x,"
 		      " lifetime %us",
 		      areatag,
 		      rawlspid_print (lsp->lsp_header->lsp_id),
@@ -290,7 +292,7 @@
   if (isis->debugs & DEBUG_SNP_PACKETS)
     {
       zlog_debug
-	("ISIS-Snp (%s): LSP %s seq 0x%08x, cksum 0x%04x, lifetime %us",
+	("ISIS-Snp (%s): Compare LSP %s seq 0x%08x, cksum 0x%04x, lifetime %us",
 	 areatag, rawlspid_print (lsp->lsp_header->lsp_id), ntohl (seq_num),
 	 ntohs (checksum), ntohs (rem_lifetime));
       zlog_debug ("ISIS-Snp (%s):       is older than ours seq 0x%08x,"
@@ -303,6 +305,91 @@
   return LSP_OLDER;
 }
 
+static void
+lsp_auth_add (struct isis_lsp *lsp)
+{
+  struct isis_passwd *passwd;
+  unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
+
+  /*
+   * Add the authentication info if its present
+   */
+  (lsp->level == IS_LEVEL_1) ? (passwd = &lsp->area->area_passwd) :
+                               (passwd = &lsp->area->domain_passwd);
+  switch (passwd->type)
+    {
+      /* Cleartext */
+      case ISIS_PASSWD_TYPE_CLEARTXT:
+        memcpy (&lsp->tlv_data.auth_info, passwd, sizeof (struct isis_passwd));
+        tlv_add_authinfo (passwd->type, passwd->len, passwd->passwd, lsp->pdu);
+        break;
+
+      /* HMAC MD5 */
+      case ISIS_PASSWD_TYPE_HMAC_MD5:
+        /* Remember where TLV is written so we can later
+         * overwrite the MD5 hash */
+        lsp->auth_tlv_offset = stream_get_endp (lsp->pdu);
+        memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
+        lsp->tlv_data.auth_info.type = ISIS_PASSWD_TYPE_HMAC_MD5;
+        lsp->tlv_data.auth_info.len = ISIS_AUTH_MD5_SIZE;
+        memcpy (&lsp->tlv_data.auth_info.passwd, hmac_md5_hash,
+                ISIS_AUTH_MD5_SIZE);
+        tlv_add_authinfo (passwd->type, ISIS_AUTH_MD5_SIZE, hmac_md5_hash,
+                          lsp->pdu);
+        break;
+
+      default:
+        break;
+    }
+}
+
+static void
+lsp_auth_update (struct isis_lsp *lsp)
+{
+  struct isis_passwd *passwd;
+  unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
+  uint16_t checksum, rem_lifetime;
+
+  /* For HMAC MD5 we need to recompute the md5 hash and store it */
+  (lsp->level == IS_LEVEL_1) ? (passwd = &lsp->area->area_passwd) :
+                               (passwd = &lsp->area->domain_passwd);
+  if (passwd->type != ISIS_PASSWD_TYPE_HMAC_MD5)
+    return;
+
+  /*
+   * In transient conditions (when net is configured where authentication
+   * config and lsp regenerate schedule is not yet run), there could be
+   * an own_lsp with auth_tlv_offset set to 0. In such a case, simply
+   * return, when lsp_regenerate is run, lsp will have auth tlv.
+   */
+  if (lsp->auth_tlv_offset == 0)
+    return;
+
+  /*
+   * RFC 5304 set auth value, checksum and remaining lifetime to zero
+   * before computation and reset to old values after computation.
+   */
+  checksum = lsp->lsp_header->checksum;
+  rem_lifetime = lsp->lsp_header->rem_lifetime;
+  lsp->lsp_header->checksum = 0;
+  lsp->lsp_header->rem_lifetime = 0;
+  /* Set the authentication value as well to zero */
+  memset (STREAM_DATA (lsp->pdu) + lsp->auth_tlv_offset + 3,
+          0, ISIS_AUTH_MD5_SIZE);
+  /* Compute autentication value */
+  hmac_md5 (STREAM_DATA (lsp->pdu), stream_get_endp(lsp->pdu),
+            (unsigned char *) &passwd->passwd, passwd->len,
+            (caddr_t) &hmac_md5_hash);
+  /* Copy the hash into the stream */
+  memcpy (STREAM_DATA (lsp->pdu) + lsp->auth_tlv_offset + 3,
+          hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
+  memcpy (&lsp->tlv_data.auth_info.passwd, hmac_md5_hash,
+          ISIS_AUTH_MD5_SIZE);
+  /* Copy back the checksum and remaining lifetime */
+  lsp->lsp_header->checksum = checksum;
+  lsp->lsp_header->rem_lifetime = rem_lifetime;
+}
+
 void
 lsp_inc_seqnum (struct isis_lsp *lsp, u_int32_t seq_num)
 {
@@ -311,11 +398,25 @@
   if (seq_num == 0 || ntohl (lsp->lsp_header->seq_num) > seq_num)
     newseq = ntohl (lsp->lsp_header->seq_num) + 1;
   else
-    newseq = seq_num++;
+    newseq = seq_num + 1;
 
   lsp->lsp_header->seq_num = htonl (newseq);
-  fletcher_checksum (STREAM_DATA (lsp->pdu) + 12,
-		   ntohs (lsp->lsp_header->pdu_len) - 12, 12);
+
+  /* Recompute authentication and checksum information */
+  lsp_auth_update (lsp);
+  /* ISO 10589 - 7.3.11 Generation of the checksum
+   * The checksum shall be computed over all fields in the LSP which appear
+   * after the Remaining Lifetime field. This field (and those appearing
+   * before it) are excluded so that the LSP may be aged by systems without
+   * requiring recomputation.
+   */
+  fletcher_checksum(STREAM_DATA (lsp->pdu) + 12,
+                    ntohs (lsp->lsp_header->pdu_len) - 12, 12);
+
+  isis_spf_schedule (lsp->area, lsp->level);
+#ifdef HAVE_IPV6
+  isis_spf_schedule6 (lsp->area, lsp->level);
+#endif
 
   return;
 }
@@ -340,54 +441,40 @@
   return;
 }
 
-int
-isis_lsp_authinfo_check (struct stream *stream, struct isis_area *area,
-			 int pdulen, struct isis_passwd *passwd)
+static u_int8_t
+lsp_bits_generate (int level, int overload_bit)
 {
-  uint32_t expected = 0, found;
-  struct tlvs tlvs;
-  int retval = 0;
-
-  expected |= TLVFLAG_AUTH_INFO;
-  retval = parse_tlvs (area->area_tag, stream->data +
-		       ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN,
-		       pdulen - ISIS_FIXED_HDR_LEN
-		       - ISIS_LSP_HDR_LEN, &expected, &found, &tlvs);
-
-  if (retval || !(found & TLVFLAG_AUTH_INFO))
-    return 1;			/* Auth fail (parsing failed or no auth-tlv) */
-
-  switch (tlvs.auth_info.type)
-    {
-      case ISIS_PASSWD_TYPE_HMAC_MD5:
-	zlog_debug("Got LSP with ISIS_PASSWD_TYPE_HMAC_MD5");
-	break;
-      case ISIS_PASSWD_TYPE_CLEARTXT:
-	zlog_debug("Got LSP with ISIS_PASSWD_TYPE_CLEARTXT");
-	break;
-      default:
-	zlog_debug("Unknown authentication type in LSP");
-	break;
-    }
-
-  return 0;
-  /* return authentication_check (passwd, &tlvs.auth_info);*/
+  u_int8_t lsp_bits = 0;
+  if (level == IS_LEVEL_1)
+    lsp_bits = IS_LEVEL_1;
+  else
+    lsp_bits = IS_LEVEL_1_AND_2;
+  if (overload_bit)
+    lsp_bits |= overload_bit;
+  return lsp_bits;
 }
 
 static void
 lsp_update_data (struct isis_lsp *lsp, struct stream *stream,
-		 struct isis_area *area)
+                 struct isis_area *area, int level)
 {
   uint32_t expected = 0, found;
   int retval;
 
+  /* free the old lsp data */
+  lsp_clear_data (lsp);
+
   /* copying only the relevant part of our stream */
+  if (lsp->pdu != NULL)
+    stream_free (lsp->pdu);
   lsp->pdu = stream_dup (stream);
-  
+
   /* setting pointers to the correct place */
   lsp->isis_header = (struct isis_fixed_hdr *) (STREAM_DATA (lsp->pdu));
   lsp->lsp_header = (struct isis_link_state_hdr *) (STREAM_DATA (lsp->pdu) +
 						    ISIS_FIXED_HDR_LEN);
+  lsp->area = area;
+  lsp->level = level;
   lsp->age_out = ZERO_AGE_LIFETIME;
   lsp->installed = time (NULL);
   /*
@@ -396,8 +483,6 @@
   expected |= TLVFLAG_AUTH_INFO;
   expected |= TLVFLAG_AREA_ADDRS;
   expected |= TLVFLAG_IS_NEIGHS;
-  if ((lsp->lsp_header->lsp_bits & 3) == 3)	/* a level 2 LSP */
-    expected |= TLVFLAG_PARTITION_DESIG_LEVEL2_IS;
   expected |= TLVFLAG_NLPID;
   if (area->dynhostname)
     expected |= TLVFLAG_DYN_HOSTNAME;
@@ -415,57 +500,58 @@
   expected |= TLVFLAG_IPV6_REACHABILITY;
 #endif /* HAVE_IPV6 */
 
-  retval = parse_tlvs (area->area_tag, lsp->pdu->data +
-		       ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN,
-		       ntohs (lsp->lsp_header->pdu_len) - ISIS_FIXED_HDR_LEN
-		       - ISIS_LSP_HDR_LEN, &expected, &found, &lsp->tlv_data);
-
-  if (found & TLVFLAG_DYN_HOSTNAME)
+  retval = parse_tlvs (area->area_tag, STREAM_DATA (lsp->pdu) +
+                       ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN,
+                       ntohs (lsp->lsp_header->pdu_len) -
+                       ISIS_FIXED_HDR_LEN - ISIS_LSP_HDR_LEN,
+                       &expected, &found, &lsp->tlv_data,
+                       NULL);
+  if (retval != ISIS_OK)
     {
-      if (area->dynhostname)
-	isis_dynhn_insert (lsp->lsp_header->lsp_id, lsp->tlv_data.hostname,
-			   (lsp->lsp_header->lsp_bits & LSPBIT_IST) ==
-			   IS_LEVEL_1_AND_2 ? IS_LEVEL_2 :
-			   (lsp->lsp_header->lsp_bits & LSPBIT_IST));
+      zlog_warn ("Could not parse LSP");
+      return;
     }
 
+  if ((found & TLVFLAG_DYN_HOSTNAME) && (area->dynhostname))
+    {
+      isis_dynhn_insert (lsp->lsp_header->lsp_id, lsp->tlv_data.hostname,
+                         (lsp->lsp_header->lsp_bits & LSPBIT_IST) ==
+                          IS_LEVEL_1_AND_2 ? IS_LEVEL_2 : IS_LEVEL_1);
+    }
+
+  return;
 }
 
 void
-lsp_update (struct isis_lsp *lsp, struct isis_link_state_hdr *lsp_hdr,
-	    struct stream *stream, struct isis_area *area, int level)
+lsp_update (struct isis_lsp *lsp, struct stream *stream,
+            struct isis_area *area, int level)
 {
   dnode_t *dnode = NULL;
 
-  /* Remove old LSP from LSP database. */
+  /* Remove old LSP from database. This is required since the
+   * lsp_update_data will free the lsp->pdu (which has the key, lsp_id)
+   * and will update it with the new data in the stream. */
   dnode = dict_lookup (area->lspdb[level - 1], lsp->lsp_header->lsp_id);
   if (dnode)
     dnode_destroy (dict_delete (area->lspdb[level - 1], dnode));
 
-  /* free the old lsp data */
-  XFREE (MTYPE_STREAM_DATA, lsp->pdu);
-  lsp_clear_data (lsp);
-
   /* rebuild the lsp data */
-  lsp_update_data (lsp, stream, area);
+  lsp_update_data (lsp, stream, area, level);
 
-  /* set the new values for lsp header */
-  memcpy (lsp->lsp_header, lsp_hdr, ISIS_LSP_HDR_LEN);
-
-  if (dnode)
-    lsp_insert (lsp, area->lspdb[level - 1]);
+  /* insert the lsp back into the database */
+  lsp_insert (lsp, area->lspdb[level - 1]);
 }
 
 /* creation of LSP directly from what we received */
 struct isis_lsp *
 lsp_new_from_stream_ptr (struct stream *stream,
 			 u_int16_t pdu_len, struct isis_lsp *lsp0,
-			 struct isis_area *area)
+			 struct isis_area *area, int level)
 {
   struct isis_lsp *lsp;
 
   lsp = XCALLOC (MTYPE_ISIS_LSP, sizeof (struct isis_lsp));
-  lsp_update_data (lsp, stream, area);
+  lsp_update_data (lsp, stream, area, level);
 
   if (lsp0 == NULL)
     {
@@ -499,12 +585,8 @@
       zlog_warn ("lsp_new(): out of memory");
       return NULL;
     }
-#ifdef LSP_MEMORY_PREASSIGN
-  lsp->pdu = stream_new (1514);	/*Should be minimal mtu? yup... */
-#else
-  /* We need to do realloc on TLVs additions */
-  lsp->pdu = malloc (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
-#endif /* LSP_MEMORY_PREASSIGN */
+  /* FIXME: Should be minimal mtu? */
+  lsp->pdu = stream_new (1500);
   if (LSP_FRAGMENT (lsp_id) == 0)
     lsp->lspu.frags = list_new ();
   lsp->isis_header = (struct isis_fixed_hdr *) (STREAM_DATA (lsp->pdu));
@@ -512,7 +594,7 @@
     (STREAM_DATA (lsp->pdu) + ISIS_FIXED_HDR_LEN);
 
   /* at first we fill the FIXED HEADER */
-  (level == 1) ? fill_fixed_hdr (lsp->isis_header, L1_LINK_STATE) :
+  (level == IS_LEVEL_1) ? fill_fixed_hdr (lsp->isis_header, L1_LINK_STATE) :
     fill_fixed_hdr (lsp->isis_header, L2_LINK_STATE);
 
   /* now for the LSP HEADER */
@@ -529,9 +611,10 @@
   stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
 
   if (isis->debugs & DEBUG_EVENTS)
-    zlog_debug ("New LSP with ID %s-%02x-%02x seqnum %08x",
+    zlog_debug ("New LSP with ID %s-%02x-%02x len %d seqnum %08x",
 		sysid_print (lsp_id), LSP_PSEUDO_ID (lsp->lsp_header->lsp_id),
 		LSP_FRAGMENT (lsp->lsp_header->lsp_id),
+		ntohl (lsp->lsp_header->pdu_len),
 		ntohl (lsp->lsp_header->seq_num));
 
   return lsp;
@@ -541,6 +624,13 @@
 lsp_insert (struct isis_lsp *lsp, dict_t * lspdb)
 {
   dict_alloc_insert (lspdb, lsp->lsp_header->lsp_id, lsp);
+  if (lsp->lsp_header->seq_num != 0)
+    {
+      isis_spf_schedule (lsp->area, lsp->level);
+#ifdef HAVE_IPV6
+      isis_spf_schedule6 (lsp->area, lsp->level);
+#endif
+    }
 }
 
 /*
@@ -577,12 +667,13 @@
 }
 
 /*
- * Build a list of all LSPs bounded by start and stop ids
+ * Build a list of num_lsps LSPs bounded by start_id and stop_id.
  */
 void
-lsp_build_list (u_char * start_id, u_char * stop_id,
+lsp_build_list (u_char * start_id, u_char * stop_id, u_char num_lsps,
 		struct list *list, dict_t * lspdb)
 {
+  u_char count;
   dnode_t *first, *last, *curr;
 
   first = dict_lower_bound (lspdb, start_id);
@@ -594,14 +685,18 @@
   curr = first;
 
   listnode_add (list, first->dict_data);
+  count = 1;
 
   while (curr)
     {
       curr = dict_next (lspdb, curr);
       if (curr)
-	listnode_add (list, curr->dict_data);
-      if (curr == last)
-	break;
+        {
+          listnode_add (list, curr->dict_data);
+          count++;
+        }
+      if (count == num_lsps || curr == last)
+        break;
     }
 
   return;
@@ -611,11 +706,12 @@
  * Build a list of LSPs with SSN flag set for the given circuit
  */
 void
-lsp_build_list_ssn (struct isis_circuit *circuit, struct list *list,
-		    dict_t * lspdb)
+lsp_build_list_ssn (struct isis_circuit *circuit, u_char num_lsps,
+                    struct list *list, dict_t * lspdb)
 {
   dnode_t *dnode, *next;
   struct isis_lsp *lsp;
+  u_char count = 0;
 
   dnode = dict_first (lspdb);
   while (dnode != NULL)
@@ -623,7 +719,12 @@
       next = dict_next (lspdb, dnode);
       lsp = dnode_get (dnode);
       if (ISIS_CHECK_FLAG (lsp->SSNflags, circuit))
-	listnode_add (list, lsp);
+        {
+          listnode_add (list, lsp);
+          ++count;
+        }
+      if (count == num_lsps)
+        break;
       dnode = next;
     }
 
@@ -637,22 +738,11 @@
 
   if (lsp->lsp_header->rem_lifetime == 0)
     {
-      if (lsp->age_out != 0)
-	lsp->age_out--;
+      if (lsp->age_out > 0)
+        lsp->age_out--;
       return;
     }
 
-  /* If we are turning 0 */
-  /* ISO 10589 - 7.3.16.4 first paragraph */
-
-  if (ntohs (lsp->lsp_header->rem_lifetime) == 1)
-    {
-      /* 7.3.16.4 a) set SRM flags on all */
-      ISIS_FLAGS_SET_ALL (lsp->SRMflags);
-      /* 7.3.16.4 b) retain only the header FIXME  */
-      /* 7.3.16.4 c) record the time to purge FIXME (other way to do it) */
-    }
-
   lsp->lsp_header->rem_lifetime =
     htons (ntohs (lsp->lsp_header->rem_lifetime) - 1);
 }
@@ -669,13 +759,11 @@
     dyn = NULL;
 
   if (dyn)
-    sprintf ((char *)id, "%.14s", dyn->name.name);
-  else if (!memcmp (isis->sysid, lsp_id, ISIS_SYS_ID_LEN) & dynhost)
-    sprintf ((char *)id, "%.14s", unix_hostname ());
+      sprintf ((char *)id, "%.14s", dyn->name.name);
+  else if (!memcmp (isis->sysid, lsp_id, ISIS_SYS_ID_LEN) && dynhost)
+      sprintf ((char *)id, "%.14s", unix_hostname ());
   else
-    {
       memcpy (id, sysid_print (lsp_id), 15);
-    }
   if (frag)
     sprintf ((char *)trg, "%s.%02x-%02x", id, LSP_PSEUDO_ID (lsp_id),
 	     LSP_FRAGMENT (lsp_id));
@@ -707,30 +795,32 @@
 }
 
 /* this function prints the lsp on show isis database */
-static void
-lsp_print (dnode_t * node, struct vty *vty, char dynhost)
+void
+lsp_print (struct isis_lsp *lsp, struct vty *vty, char dynhost)
 {
-  struct isis_lsp *lsp = dnode_get (node);
   u_char LSPid[255];
+  char age_out[8];
 
   lspid_print (lsp->lsp_header->lsp_id, LSPid, dynhost, 1);
-  vty_out (vty, "%-21s%c   ", LSPid, lsp->own_lsp ? '*' : ' ');
-  vty_out (vty, "0x%08x   ", ntohl (lsp->lsp_header->seq_num));
-  vty_out (vty, "0x%04x      ", ntohs (lsp->lsp_header->checksum));
-
+  vty_out (vty, "%-21s%c  ", LSPid, lsp->own_lsp ? '*' : ' ');
+  vty_out (vty, "%5u   ", ntohs (lsp->lsp_header->pdu_len));
+  vty_out (vty, "0x%08x  ", ntohl (lsp->lsp_header->seq_num));
+  vty_out (vty, "0x%04x  ", ntohs (lsp->lsp_header->checksum));
   if (ntohs (lsp->lsp_header->rem_lifetime) == 0)
-    vty_out (vty, " (%2u)", lsp->age_out);
+    {
+      snprintf (age_out, 8, "(%u)", lsp->age_out);
+      age_out[7] = '\0';
+      vty_out (vty, "%7s   ", age_out);
+    }
   else
-    vty_out (vty, "%5u", ntohs (lsp->lsp_header->rem_lifetime));
-
-  vty_out (vty, "         %s%s",
-	   lsp_bits2string (&lsp->lsp_header->lsp_bits), VTY_NEWLINE);
+    vty_out (vty, " %5u    ", ntohs (lsp->lsp_header->rem_lifetime));
+  vty_out (vty, "%s%s",
+           lsp_bits2string (&lsp->lsp_header->lsp_bits), VTY_NEWLINE);
 }
 
-static void
-lsp_print_detail (dnode_t * node, struct vty *vty, char dynhost)
+void
+lsp_print_detail (struct isis_lsp *lsp, struct vty *vty, char dynhost)
 {
-  struct isis_lsp *lsp = dnode_get (node);
   struct area_addr *area_addr;
   int i;
   struct listnode *lnode;
@@ -751,7 +841,7 @@
   u_char ipv4_address[20];
 
   lspid_print (lsp->lsp_header->lsp_id, LSPid, dynhost, 1);
-  lsp_print (node, vty, dynhost);
+  lsp_print (lsp, vty, dynhost);
 
   /* for all area address */
   if (lsp->tlv_data.area_addrs)
@@ -771,11 +861,11 @@
 	    {
 	    case NLPID_IP:
 	    case NLPID_IPV6:
-	      vty_out (vty, "  NLPID:        0x%X%s",
+	      vty_out (vty, "  NLPID       : 0x%X%s",
 		       lsp->tlv_data.nlpids->nlpids[i], VTY_NEWLINE);
 	      break;
 	    default:
-	      vty_out (vty, "  NLPID:        %s%s", "unknown", VTY_NEWLINE);
+	      vty_out (vty, "  NLPID       : %s%s", "unknown", VTY_NEWLINE);
 	      break;
 	    }
 	}
@@ -784,33 +874,42 @@
   /* for the hostname tlv */
   if (lsp->tlv_data.hostname)
     {
-      memset (hostname, 0, sizeof (hostname));
+      bzero (hostname, sizeof (hostname));
       memcpy (hostname, lsp->tlv_data.hostname->name,
 	      lsp->tlv_data.hostname->namelen);
-      vty_out (vty, "  Hostname: %s%s", hostname, VTY_NEWLINE);
+      vty_out (vty, "  Hostname    : %s%s", hostname, VTY_NEWLINE);
     }
 
-  if (lsp->tlv_data.ipv4_addrs)
-    for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_addrs, lnode, ipv4_addr))
-      {
-	memcpy (ipv4_address, inet_ntoa (*ipv4_addr), sizeof (ipv4_address));
-	vty_out (vty, "  IP:        %s%s", ipv4_address, VTY_NEWLINE);
-      }
+  /* authentication tlv */
+  if (lsp->tlv_data.auth_info.type != ISIS_PASSWD_TYPE_UNUSED)
+    {
+      if (lsp->tlv_data.auth_info.type == ISIS_PASSWD_TYPE_HMAC_MD5)
+        vty_out (vty, "  Auth type   : md5%s", VTY_NEWLINE);
+      else if (lsp->tlv_data.auth_info.type == ISIS_PASSWD_TYPE_CLEARTXT)
+        vty_out (vty, "  Auth type   : clear text%s", VTY_NEWLINE);
+    }
 
   /* TE router id */
   if (lsp->tlv_data.router_id)
     {
       memcpy (ipv4_address, inet_ntoa (lsp->tlv_data.router_id->id),
 	      sizeof (ipv4_address));
-      vty_out (vty, "  Router ID: %s%s", ipv4_address, VTY_NEWLINE);
+      vty_out (vty, "  Router ID   : %s%s", ipv4_address, VTY_NEWLINE);
     }
 
+  if (lsp->tlv_data.ipv4_addrs)
+    for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_addrs, lnode, ipv4_addr))
+      {
+        memcpy (ipv4_address, inet_ntoa (*ipv4_addr), sizeof (ipv4_address));
+        vty_out (vty, "  IPv4 Address: %s%s", ipv4_address, VTY_NEWLINE);
+      }
+
   /* for the IS neighbor tlv */
   if (lsp->tlv_data.is_neighs)
     for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.is_neighs, lnode, is_neigh))
       {
 	lspid_print (is_neigh->neigh_id, LSPid, dynhost, 0);
-	vty_out (vty, "  Metric: %-10d IS %s%s",
+	vty_out (vty, "  Metric      : %-8d IS            : %s%s",
 		 is_neigh->metrics.metric_default, LSPid, VTY_NEWLINE);
       }
   
@@ -823,7 +922,7 @@
 	      sizeof (ipv4_reach_prefix));
       memcpy (ipv4_reach_mask, inet_ntoa (ipv4_reach->mask),
 	      sizeof (ipv4_reach_mask));
-      vty_out (vty, "  Metric: %-10d IP-Internal %s %s%s",
+      vty_out (vty, "  Metric      : %-8d IPv4-Internal : %s %s%s",
 	       ipv4_reach->metrics.metric_default, ipv4_reach_prefix,
 	       ipv4_reach_mask, VTY_NEWLINE);
     }
@@ -837,7 +936,7 @@
 	      sizeof (ipv4_reach_prefix));
       memcpy (ipv4_reach_mask, inet_ntoa (ipv4_reach->mask),
 	      sizeof (ipv4_reach_mask));
-      vty_out (vty, "  Metric: %-10d IP-External %s %s%s",
+      vty_out (vty, "  Metric      : %-8d IPv4-External : %s %s%s",
 	       ipv4_reach->metrics.metric_default, ipv4_reach_prefix,
 	       ipv4_reach_mask, VTY_NEWLINE);
     }
@@ -853,11 +952,11 @@
       inet_ntop (AF_INET6, &in6, (char *)buff, BUFSIZ);
       if ((ipv6_reach->control_info &&
 	   CTRL_INFO_DISTRIBUTION) == DISTRIBUTION_INTERNAL)
-	vty_out (vty, "  Metric: %-10d IPv6-Internal %s/%d%s",
+	vty_out (vty, "  Metric      : %-8d IPv6-Internal : %s/%d%s",
 		 ntohl (ipv6_reach->metric),
 		 buff, ipv6_reach->prefix_len, VTY_NEWLINE);
       else
-	vty_out (vty, "  Metric: %-10d IPv6-External %s/%d%s",
+	vty_out (vty, "  Metric      : %-8d IPv6-External : %s/%d%s",
 		 ntohl (ipv6_reach->metric),
 		 buff, ipv6_reach->prefix_len, VTY_NEWLINE);
     }
@@ -867,11 +966,9 @@
   if (lsp->tlv_data.te_is_neighs)
     for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.te_is_neighs, lnode, te_is_neigh))
     {
-      uint32_t metric;
-      memcpy (&metric, te_is_neigh->te_metric, 3);
       lspid_print (te_is_neigh->neigh_id, LSPid, dynhost, 0);
-      vty_out (vty, "  Metric: %-10d IS-Extended %s%s",
-	       ntohl (metric << 8), LSPid, VTY_NEWLINE);
+      vty_out (vty, "  Metric      : %-8d IS-Extended   : %s%s",
+	       GET_TE_METRIC(te_is_neigh), LSPid, VTY_NEWLINE);
     }
 
   /* TE IPv4 tlv */
@@ -880,12 +977,13 @@
 			       te_ipv4_reach))
     {
       /* FIXME: There should be better way to output this stuff. */
-      vty_out (vty, "  Metric: %-10d IP-Extended %s/%d%s",
+      vty_out (vty, "  Metric      : %-8d IPv4-Extended : %s/%d%s",
 	       ntohl (te_ipv4_reach->te_metric),
 	       inet_ntoa (newprefix2inaddr (&te_ipv4_reach->prefix_start,
 					    te_ipv4_reach->control)),
 	       te_ipv4_reach->control & 0x3F, VTY_NEWLINE);
     }
+  vty_out (vty, "%s", VTY_NEWLINE);
 
   return;
 }
@@ -898,10 +996,6 @@
   dnode_t *node = dict_first (lspdb), *next;
   int lsp_count = 0;
 
-  /* print the title, for both modes */
-  vty_out (vty, "LSP ID                   LSP Seq Num  LSP Checksum "
-	   "LSP Holdtime ATT/P/OL%s", VTY_NEWLINE);
-
   if (detail == ISIS_UI_LEVEL_BRIEF)
     {
       while (node != NULL)
@@ -909,7 +1003,7 @@
 	  /* I think it is unnecessary, so I comment it out */
 	  /* dict_contains (lspdb, node); */
 	  next = dict_next (lspdb, node);
-	  lsp_print (node, vty, dynhost);
+	  lsp_print (dnode_get (node), vty, dynhost);
 	  node = next;
 	  lsp_count++;
 	}
@@ -919,7 +1013,7 @@
       while (node != NULL)
 	{
 	  next = dict_next (lspdb, node);
-	  lsp_print_detail (node, vty, dynhost);
+	  lsp_print_detail (dnode_get (node), vty, dynhost);
 	  node = next;
 	  lsp_count++;
 	}
@@ -929,7 +1023,7 @@
 }
 
 #define FRAG_THOLD(S,T) \
-((STREAM_SIZE(S)*T)/100)
+  ((STREAM_SIZE(S)*T)/100)
 
 /* stream*, area->lsp_frag_threshold, increment */
 #define FRAG_NEEDED(S,T,I) \
@@ -948,16 +1042,32 @@
   if (!FRAG_NEEDED (lsp->pdu, frag_thold, listcount (*from) * tlvsize + 2))
     {
       tlv_build_func (*from, lsp->pdu);
-      *to = *from;
-      *from = NULL;
+      if (listcount (*to) != 0)
+	{
+	  struct listnode *node, *nextnode;
+	  void *elem;
+
+	  for (ALL_LIST_ELEMENTS (*from, node, nextnode, elem))
+	    {
+	      listnode_add (*to, elem);
+	      list_delete_node (*from, node);
+	    }
+	}
+      else
+	{
+	  list_free (*to);
+	  *to = *from;
+	  *from = NULL;
+	}
     }
   else if (!FRAG_NEEDED (lsp->pdu, frag_thold, tlvsize + 2))
     {
       /* fit all we can */
       count = FRAG_THOLD (lsp->pdu, frag_thold) - 2 -
 	(STREAM_SIZE (lsp->pdu) - STREAM_REMAIN (lsp->pdu));
-      if (count)
-	count = count / tlvsize;
+      count = count / tlvsize;
+      if (count > (int)listcount (*from))
+	count = listcount (*from);
       for (i = 0; i < count; i++)
 	{
 	  listnode_add (*to, listgetdata (listhead (*from)));
@@ -969,6 +1079,44 @@
   return;
 }
 
+static u_int16_t
+lsp_rem_lifetime (struct isis_area *area, int level)
+{
+  u_int16_t rem_lifetime;
+
+  /* Add jitter to configured LSP lifetime */
+  rem_lifetime = isis_jitter (area->max_lsp_lifetime[level - 1],
+                              MAX_AGE_JITTER);
+
+  /* No jitter if the max refresh will be less than configure gen interval */
+  if (area->lsp_gen_interval[level - 1] > (rem_lifetime - 300))
+    rem_lifetime = area->max_lsp_lifetime[level - 1];
+
+  return rem_lifetime;
+}
+
+static u_int16_t
+lsp_refresh_time (struct isis_lsp *lsp, u_int16_t rem_lifetime)
+{
+  struct isis_area *area = lsp->area;
+  int level = lsp->level;
+  u_int16_t refresh_time;
+
+  /* Add jitter to LSP refresh time */
+  refresh_time = isis_jitter (area->lsp_refresh[level - 1],
+                              MAX_LSP_GEN_JITTER);
+
+  /* RFC 4444 : make sure the refresh time is at least less than 300
+   * of the remaining lifetime and more than gen interval */
+  if (refresh_time <= area->lsp_gen_interval[level - 1] ||
+      refresh_time > (rem_lifetime - 300))
+    refresh_time = rem_lifetime - 300;
+
+  assert (area->lsp_gen_interval[level - 1] < refresh_time);
+
+  return refresh_time;
+}
+
 static struct isis_lsp *
 lsp_next_frag (u_char frag_num, struct isis_lsp *lsp0, struct isis_area *area,
 	       int level)
@@ -978,40 +1126,21 @@
 
   memcpy (frag_id, lsp0->lsp_header->lsp_id, ISIS_SYS_ID_LEN + 1);
   LSP_FRAGMENT (frag_id) = frag_num;
+  /* FIXME add authentication TLV for fragment LSPs */
   lsp = lsp_search (frag_id, area->lspdb[level - 1]);
   if (lsp)
     {
-      /*
-       * Clear the TLVs, but inherit the authinfo
-       */
+      /* Clear the TLVs */
       lsp_clear_data (lsp);
-      if (lsp0->tlv_data.auth_info.type)
-	{
-	  memcpy (&lsp->tlv_data.auth_info, &lsp->tlv_data.auth_info,
-		  sizeof (struct isis_passwd));
-	  tlv_add_authinfo (lsp->tlv_data.auth_info.type,
-			    lsp->tlv_data.auth_info.len,
-			    lsp->tlv_data.auth_info.passwd, lsp->pdu);
-	}
       return lsp;
     }
-  lsp = lsp_new (frag_id, area->max_lsp_lifetime[level - 1], 0, area->is_type,
-		 0, level);
+  lsp = lsp_new (frag_id, ntohs(lsp0->lsp_header->rem_lifetime), 0,
+                 lsp_bits_generate (level, area->overload_bit), 0, level);
+  lsp->area = area;
   lsp->own_lsp = 1;
   lsp_insert (lsp, area->lspdb[level - 1]);
   listnode_add (lsp0->lspu.frags, lsp);
   lsp->lspu.zero_lsp = lsp0;
-  /*
-   * Copy the authinfo from zero LSP
-   */
-  if (lsp0->tlv_data.auth_info.type)
-    {
-      memcpy (&lsp->tlv_data.auth_info, &lsp->tlv_data.auth_info,
-	      sizeof (struct isis_passwd));
-      tlv_add_authinfo (lsp->tlv_data.auth_info.type,
-			lsp->tlv_data.auth_info.len,
-			lsp->tlv_data.auth_info.passwd, lsp->pdu);
-    }
   return lsp;
 }
 
@@ -1020,7 +1149,7 @@
  * area->lsp_frag_threshold is exceeded.
  */
 static void
-lsp_build_nonpseudo (struct isis_lsp *lsp, struct isis_area *area)
+lsp_build (struct isis_lsp *lsp, struct isis_area *area)
 {
   struct is_neigh *is_neigh;
   struct te_is_neigh *te_is_neigh;
@@ -1037,8 +1166,27 @@
 #endif /* HAVE_IPV6 */
   struct tlvs tlv_data;
   struct isis_lsp *lsp0 = lsp;
-  struct isis_passwd *passwd;
   struct in_addr *routerid;
+  uint32_t expected = 0, found = 0;
+  uint32_t metric;
+  u_char zero_id[ISIS_SYS_ID_LEN + 1];
+  int retval = ISIS_OK;
+
+  /*
+   * Building the zero lsp
+   */
+  memset (zero_id, 0, ISIS_SYS_ID_LEN + 1);
+
+  /* Reset stream endp. Stream is always there and on every LSP refresh only
+   * TLV part of it is overwritten. So we must seek past header we will not
+   * touch. */
+  stream_reset (lsp->pdu);
+  stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
+
+  /*
+   * Add the authentication info if its present
+   */
+  lsp_auth_add (lsp);
 
   /*
    * First add the tlvs related to area
@@ -1048,6 +1196,9 @@
   if (lsp->tlv_data.area_addrs == NULL)
     lsp->tlv_data.area_addrs = list_new ();
   list_add_list (lsp->tlv_data.area_addrs, area->area_addrs);
+  if (listcount (lsp->tlv_data.area_addrs) > 0)
+    tlv_add_area_addrs (lsp->tlv_data.area_addrs, lsp->pdu);
+
   /* Protocols Supported */
   if (area->ip_circuits > 0
 #ifdef HAVE_IPV6
@@ -1070,7 +1221,9 @@
 	    NLPID_IPV6;
 	}
 #endif /* HAVE_IPV6 */
+      tlv_add_nlpid (lsp->tlv_data.nlpids, lsp->pdu);
     }
+
   /* Dynamic Hostname */
   if (area->dynhostname)
     {
@@ -1080,39 +1233,13 @@
       memcpy (lsp->tlv_data.hostname->name, unix_hostname (),
 	      strlen (unix_hostname ()));
       lsp->tlv_data.hostname->namelen = strlen (unix_hostname ());
+      tlv_add_dynamic_hostname (lsp->tlv_data.hostname, lsp->pdu);
     }
 
-  /*
-   * Building the zero lsp
-   */
-
-  /* Reset stream endp. Stream is always there and on every LSP refresh only
-   * TLV part of it is overwritten. So we must seek past header we will not
-   * touch. */
-  stream_reset (lsp->pdu);
-  stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
-
-  /*
-   * Add the authentication info if its present
-   */
-  (level == 1) ? (passwd = &area->area_passwd) :
-    (passwd = &area->domain_passwd);
-  if (passwd->type)
-    {
-      memcpy (&lsp->tlv_data.auth_info, passwd, sizeof (struct isis_passwd));
-      tlv_add_authinfo (passwd->type, passwd->len, passwd->passwd, lsp->pdu);
-    }
-  if (lsp->tlv_data.nlpids)
-    tlv_add_nlpid (lsp->tlv_data.nlpids, lsp->pdu);
-  if (lsp->tlv_data.hostname)
-    tlv_add_dynamic_hostname (lsp->tlv_data.hostname, lsp->pdu);
-  if (lsp->tlv_data.area_addrs && listcount (lsp->tlv_data.area_addrs) > 0)
-    tlv_add_area_addrs (lsp->tlv_data.area_addrs, lsp->pdu);
-
   /* IPv4 address and TE router ID TLVs. In case of the first one we don't
    * follow "C" vendor, but "J" vendor behavior - one IPv4 address is put into
    * LSP and this address is same as router id. */
-  if (router_id_zebra.s_addr != 0)
+  if (isis->router_id != 0)
     {
       if (lsp->tlv_data.ipv4_addrs == NULL)
 	{
@@ -1121,7 +1248,7 @@
 	}
 
       routerid = XMALLOC (MTYPE_ISIS_TLV, sizeof (struct in_addr));
-      routerid->s_addr = router_id_zebra.s_addr;
+      routerid->s_addr = isis->router_id;
       listnode_add (lsp->tlv_data.ipv4_addrs, routerid);
       tlv_add_in_addr (routerid, lsp->pdu, IPV4_ADDR);
 
@@ -1131,8 +1258,9 @@
 	{
 	  lsp->tlv_data.router_id = XMALLOC (MTYPE_ISIS_TLV,
 					     sizeof (struct in_addr));
-	  lsp->tlv_data.router_id->id.s_addr = router_id_zebra.s_addr;
-	  tlv_add_in_addr (&lsp->tlv_data.router_id->id, lsp->pdu, TE_ROUTER_ID);
+	  lsp->tlv_data.router_id->id.s_addr = isis->router_id;
+	  tlv_add_in_addr (&lsp->tlv_data.router_id->id, lsp->pdu,
+                           TE_ROUTER_ID);
 	}
     }
 
@@ -1141,7 +1269,7 @@
 #ifdef TOPOLOGY_GENERATE
   /* If topology exists (and we create topology for level 1 only), create
    * (hardcoded) link to topology. */
-  if (area->topology && level == 1)
+  if (area->topology && level == IS_LEVEL_1)
     {
       if (tlv_data.is_neighs == NULL)
 	{
@@ -1192,7 +1320,6 @@
 					    (ipv4->prefix.s_addr));
 		  listnode_add (tlv_data.ipv4_int_reachs, ipreach);
 		}
-	      tlv_data.ipv4_int_reachs->del = free_tlv;
 	    }
 	  if (area->newmetric)
 	    {
@@ -1220,6 +1347,7 @@
 		}
 	    }
 	}
+
 #ifdef HAVE_IPV6
       /*
        * Add IPv6 reachability of this circuit
@@ -1258,7 +1386,7 @@
       switch (circuit->circ_type)
 	{
 	case CIRCUIT_T_BROADCAST:
-	  if (level & circuit->circuit_is_type)
+	  if (level & circuit->is_type)
 	    {
 	      if (area->oldmetric)
 		{
@@ -1268,20 +1396,21 @@
 		      tlv_data.is_neighs->del = free_tlv;
 		    }
 		  is_neigh = XCALLOC (MTYPE_ISIS_TLV, sizeof (struct is_neigh));
-		  if (level == 1)
+		  if (level == IS_LEVEL_1)
 		    memcpy (is_neigh->neigh_id,
 			    circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1);
 		  else
 		    memcpy (is_neigh->neigh_id,
 			    circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1);
 		  is_neigh->metrics = circuit->metrics[level - 1];
-		  listnode_add (tlv_data.is_neighs, is_neigh);
-		  tlv_data.is_neighs->del = free_tlv;
+                  if (!memcmp (is_neigh->neigh_id, zero_id,
+                               ISIS_SYS_ID_LEN + 1))
+                    XFREE (MTYPE_ISIS_TLV, is_neigh);
+                  else
+                    listnode_add (tlv_data.is_neighs, is_neigh);
 		}
 	      if (area->newmetric)
 		{
-		  uint32_t metric;
-
 		  if (tlv_data.te_is_neighs == NULL)
 		    {
 		      tlv_data.te_is_neighs = list_new ();
@@ -1289,21 +1418,22 @@
 		    }
 		  te_is_neigh = XCALLOC (MTYPE_ISIS_TLV,
 					 sizeof (struct te_is_neigh));
-		  if (level == 1)
+		  if (level == IS_LEVEL_1)
 		    memcpy (te_is_neigh->neigh_id,
 			    circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1);
 		  else
 		    memcpy (te_is_neigh->neigh_id,
 			    circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1);
 		  if (area->oldmetric)
-		    metric =
-		      ((htonl(circuit->metrics[level - 1].metric_default) >> 8)
-			      & 0xffffff);
+		    metric = circuit->metrics[level - 1].metric_default;
 		  else
-		    metric = ((htonl(*circuit->te_metric) >> 8) & 0xffffff);
-
-		  memcpy (te_is_neigh->te_metric, &metric, 3);
-		  listnode_add (tlv_data.te_is_neighs, te_is_neigh);
+		    metric = circuit->te_metric[level - 1];
+		  SET_TE_METRIC(te_is_neigh, metric);
+                  if (!memcmp (te_is_neigh->neigh_id, zero_id,
+                               ISIS_SYS_ID_LEN + 1))
+                    XFREE (MTYPE_ISIS_TLV, te_is_neigh);
+                  else
+                    listnode_add (tlv_data.te_is_neighs, te_is_neigh);
 		}
 	    }
 	  break;
@@ -1335,21 +1465,14 @@
 		  te_is_neigh = XCALLOC (MTYPE_ISIS_TLV,
 					 sizeof (struct te_is_neigh));
 		  memcpy (te_is_neigh->neigh_id, nei->sysid, ISIS_SYS_ID_LEN);
-		  metric = ((htonl(*circuit->te_metric) >> 8) & 0xffffff);
-		  memcpy (te_is_neigh->te_metric, &metric, 3);
+		  metric = circuit->te_metric[level - 1];
+		  SET_TE_METRIC(te_is_neigh, metric);
 		  listnode_add (tlv_data.te_is_neighs, te_is_neigh);
 		}
 	    }
 	  break;
-	case CIRCUIT_T_STATIC_IN:
-	  zlog_warn ("lsp_area_create: unsupported circuit type");
-	  break;
-	case CIRCUIT_T_STATIC_OUT:
-	  zlog_warn ("lsp_area_create: unsupported circuit type");
-	  break;
-	case CIRCUIT_T_DA:
-	  zlog_warn ("lsp_area_create: unsupported circuit type");
-	  break;
+	case CIRCUIT_T_LOOPBACK:
+          break;
 	default:
 	  zlog_warn ("lsp_area_create: unknown circuit type");
 	}
@@ -1367,6 +1490,7 @@
 	lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
 			     lsp0, area, level);
     }
+
   /* FIXME: We pass maximum te_ipv4_reachability length to the lsp_tlv_fit()
    * for now. lsp_tlv_fit() needs to be fixed to deal with variable length
    * TLVs (sub TLVs!). */
@@ -1376,7 +1500,8 @@
 	lsp->tlv_data.te_ipv4_reachs = list_new ();
       lsp_tlv_fit (lsp, &tlv_data.te_ipv4_reachs,
 		   &lsp->tlv_data.te_ipv4_reachs,
-		   9, area->lsp_frag_threshold, tlv_add_te_ipv4_reachs);
+		   TE_IPV4_REACH_LEN, area->lsp_frag_threshold,
+		   tlv_add_te_ipv4_reachs);
       if (tlv_data.te_ipv4_reachs && listcount (tlv_data.te_ipv4_reachs))
 	lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
 			     lsp0, area, level);
@@ -1421,87 +1546,99 @@
 	lsp = lsp_next_frag (LSP_FRAGMENT (lsp->lsp_header->lsp_id) + 1,
 			     lsp0, area, level);
     }
+  lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
 
   free_tlvs (&tlv_data);
+
+  /* Validate the LSP */
+  retval = parse_tlvs (area->area_tag, STREAM_DATA (lsp->pdu) +
+                       ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN,
+                       stream_get_endp (lsp->pdu) -
+                       ISIS_FIXED_HDR_LEN - ISIS_LSP_HDR_LEN,
+                       &expected, &found, &tlv_data, NULL);
+  assert (retval == ISIS_OK);
+
   return;
 }
 
 /*
- * 7.3.7 Generation on non-pseudonode LSPs
+ * 7.3.7 and 7.3.9 Generation on non-pseudonode LSPs
  */
-static int
-lsp_generate_non_pseudo (struct isis_area *area, int level)
+int
+lsp_generate (struct isis_area *area, int level)
 {
   struct isis_lsp *oldlsp, *newlsp;
   u_int32_t seq_num = 0;
   u_char lspid[ISIS_SYS_ID_LEN + 2];
+  u_int16_t rem_lifetime, refresh_time;
+
+  if ((area == NULL) || (area->is_type & level) != level)
+    return ISIS_ERROR;
 
   memset (&lspid, 0, ISIS_SYS_ID_LEN + 2);
   memcpy (&lspid, isis->sysid, ISIS_SYS_ID_LEN);
 
   /* only builds the lsp if the area shares the level */
-  if ((area->is_type & level) == level)
+  oldlsp = lsp_search (lspid, area->lspdb[level - 1]);
+  if (oldlsp)
     {
-      oldlsp = lsp_search (lspid, area->lspdb[level - 1]);
-      if (oldlsp)
-	{
-	  seq_num = ntohl (oldlsp->lsp_header->seq_num);
-	  lsp_search_and_destroy (oldlsp->lsp_header->lsp_id,
-				  area->lspdb[level - 1]);
-	  /* FIXME: we should actually initiate a purge */
-	}
-      newlsp = lsp_new (lspid, area->max_lsp_lifetime[level - 1], seq_num,
-			area->is_type, 0, level);
-      newlsp->own_lsp = 1;
-
-      lsp_insert (newlsp, area->lspdb[level - 1]);
-      /* build_lsp_data (newlsp, area); */
-      lsp_build_nonpseudo (newlsp, area);
-      /* time to calculate our checksum */
-      lsp_seqnum_update (newlsp);
+      /* FIXME: we should actually initiate a purge */
+      seq_num = ntohl (oldlsp->lsp_header->seq_num);
+      lsp_search_and_destroy (oldlsp->lsp_header->lsp_id,
+                              area->lspdb[level - 1]);
     }
+  rem_lifetime = lsp_rem_lifetime (area, level);
+  newlsp = lsp_new (lspid, rem_lifetime, seq_num,
+                    area->is_type | area->overload_bit, 0, level);
+  newlsp->area = area;
+  newlsp->own_lsp = 1;
 
-  /* DEBUG_ADJ_PACKETS */
-  if (isis->debugs & DEBUG_ADJ_PACKETS)
+  lsp_insert (newlsp, area->lspdb[level - 1]);
+  /* build_lsp_data (newlsp, area); */
+  lsp_build (newlsp, area);
+  /* time to calculate our checksum */
+  lsp_seqnum_update (newlsp);
+  lsp_set_all_srmflags (newlsp);
+
+  refresh_time = lsp_refresh_time (newlsp, rem_lifetime);
+  THREAD_TIMER_OFF (area->t_lsp_refresh[level - 1]);
+  if (level == IS_LEVEL_1)
+    THREAD_TIMER_ON (master, area->t_lsp_refresh[level - 1],
+                     lsp_l1_refresh, area, refresh_time);
+  else if (level == IS_LEVEL_2)
+    THREAD_TIMER_ON (master, area->t_lsp_refresh[level - 1],
+                     lsp_l2_refresh, area, refresh_time);
+
+  if (isis->debugs & DEBUG_UPDATE_PACKETS)
     {
-      /* FIXME: is this place right? fix missing info */
-      zlog_debug ("ISIS-Upd (%s): Building L%d LSP", area->area_tag, level);
+      zlog_debug ("ISIS-Upd (%s): Building L%d LSP %s, len %d, "
+                  "seq 0x%08x, cksum 0x%04x, lifetime %us refresh %us",
+                  area->area_tag, level,
+                  rawlspid_print (newlsp->lsp_header->lsp_id),
+                  ntohl (newlsp->lsp_header->pdu_len),
+                  ntohl (newlsp->lsp_header->seq_num),
+                  ntohs (newlsp->lsp_header->checksum),
+                  ntohs (newlsp->lsp_header->rem_lifetime),
+                  refresh_time);
     }
 
   return ISIS_OK;
 }
 
 /*
- * 7.3.9 Generation of level 1 LSPs (non-pseudonode)
+ * Search own LSPs, update holding time and set SRM
  */
-int
-lsp_l1_generate (struct isis_area *area)
-{
-  THREAD_TIMER_ON (master, area->t_lsp_refresh[0], lsp_refresh_l1, area,
-		   MAX_LSP_GEN_INTERVAL);
-
-  return lsp_generate_non_pseudo (area, 1);
-}
-
-/*
- * 7.3.9 Generation of level 2 LSPs (non-pseudonode)
- */
-int
-lsp_l2_generate (struct isis_area *area)
-{
-  THREAD_TIMER_ON (master, area->t_lsp_refresh[1], lsp_refresh_l2, area,
-		   MAX_LSP_GEN_INTERVAL);
-
-  return lsp_generate_non_pseudo (area, 2);
-}
-
 static int
-lsp_non_pseudo_regenerate (struct isis_area *area, int level)
+lsp_regenerate (struct isis_area *area, int level)
 {
   dict_t *lspdb = area->lspdb[level - 1];
   struct isis_lsp *lsp, *frag;
   struct listnode *node;
   u_char lspid[ISIS_SYS_ID_LEN + 2];
+  u_int16_t rem_lifetime, refresh_time;
+
+  if ((area == NULL) || (area->is_type & level) != level)
+    return ISIS_ERROR;
 
   memset (lspid, 0, ISIS_SYS_ID_LEN + 2);
   memcpy (lspid, isis->sysid, ISIS_SYS_ID_LEN);
@@ -1510,180 +1647,148 @@
 
   if (!lsp)
     {
-      zlog_err
-	("ISIS-Upd (%s): lsp_non_pseudo_regenerate(): no L%d LSP found!",
-	 area->area_tag, level);
-
+      zlog_err ("ISIS-Upd (%s): lsp_regenerate: no L%d LSP found!",
+                area->area_tag, level);
       return ISIS_ERROR;
     }
 
   lsp_clear_data (lsp);
-  lsp_build_nonpseudo (lsp, area);
-  lsp->lsp_header->rem_lifetime = htons (isis_jitter
-					 (area->max_lsp_lifetime[level - 1],
-					  MAX_AGE_JITTER));
+  lsp_build (lsp, area);
+  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, area->overload_bit);
+  rem_lifetime = lsp_rem_lifetime (area, level);
+  lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
   lsp_seqnum_update (lsp);
 
+  lsp->last_generated = time (NULL);
+  lsp_set_all_srmflags (lsp);
+  for (ALL_LIST_ELEMENTS_RO (lsp->lspu.frags, node, frag))
+    {
+      frag->lsp_header->lsp_bits = lsp_bits_generate (level,
+                                                      area->overload_bit);
+      /* Set the lifetime values of all the fragments to the same value,
+       * so that no fragment expires before the lsp is refreshed.
+       */
+      frag->lsp_header->rem_lifetime = htons (rem_lifetime);
+      lsp_set_all_srmflags (frag);
+    }
+
+  refresh_time = lsp_refresh_time (lsp, rem_lifetime);
+  if (level == IS_LEVEL_1)
+    THREAD_TIMER_ON (master, area->t_lsp_refresh[level - 1],
+                     lsp_l1_refresh, area, refresh_time);
+  else if (level == IS_LEVEL_2)
+    THREAD_TIMER_ON (master, area->t_lsp_refresh[level - 1],
+                     lsp_l2_refresh, area, refresh_time);
+
   if (isis->debugs & DEBUG_UPDATE_PACKETS)
     {
-      zlog_debug ("ISIS-Upd (%s): refreshing our L%d LSP %s, "
-		  "seq 0x%08x, cksum 0x%04x lifetime %us",
-		  area->area_tag,
-		  level,
-		  rawlspid_print (lsp->lsp_header->lsp_id),
-		  ntohl (lsp->lsp_header->seq_num),
-		  ntohs (lsp->lsp_header->checksum),
-		  ntohs (lsp->lsp_header->rem_lifetime));
+      zlog_debug ("ISIS-Upd (%s): Refreshing our L%d LSP %s, len %d, "
+                  "seq 0x%08x, cksum 0x%04x, lifetime %us refresh %us",
+                  area->area_tag, level,
+                  rawlspid_print (lsp->lsp_header->lsp_id),
+                  ntohl (lsp->lsp_header->pdu_len),
+                  ntohl (lsp->lsp_header->seq_num),
+                  ntohs (lsp->lsp_header->checksum),
+                  ntohs (lsp->lsp_header->rem_lifetime),
+                  refresh_time);
     }
 
-  lsp->last_generated = time (NULL);
-  area->lsp_regenerate_pending[level - 1] = 0;
-  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
-  for (ALL_LIST_ELEMENTS_RO (lsp->lspu.frags, node, frag))
-    {
-      frag->lsp_header->rem_lifetime = htons (isis_jitter
-					      (area->
-					       max_lsp_lifetime[level - 1],
-					       MAX_AGE_JITTER));
-      ISIS_FLAGS_SET_ALL (frag->SRMflags);
-    }
-
-  if (area->ip_circuits)
-    isis_spf_schedule (area, level);
-#ifdef HAVE_IPV6
-  if (area->ipv6_circuits)
-    isis_spf_schedule6 (area, level);
-#endif
   return ISIS_OK;
 }
 
 /*
- * Done at least every MAX_LSP_GEN_INTERVAL. Search own LSPs, update holding
- * time and set SRM
+ * Something has changed or periodic refresh -> regenerate LSP
  */
-int
-lsp_refresh_l1 (struct thread *thread)
+static int
+lsp_l1_refresh (struct thread *thread)
 {
   struct isis_area *area;
-  unsigned long ref_time;
 
   area = THREAD_ARG (thread);
   assert (area);
 
   area->t_lsp_refresh[0] = NULL;
-  if (area->is_type & IS_LEVEL_1)
-    lsp_non_pseudo_regenerate (area, 1);
+  area->lsp_regenerate_pending[0] = 0;
 
-  ref_time = area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
-    MAX_LSP_GEN_INTERVAL : area->lsp_refresh[0];
+  if ((area->is_type & IS_LEVEL_1) == 0)
+    return ISIS_ERROR;
 
-  THREAD_TIMER_ON (master, area->t_lsp_refresh[0], lsp_refresh_l1, area,
-		   isis_jitter (ref_time, MAX_AGE_JITTER));
-
-  return ISIS_OK;
+  return lsp_regenerate (area, IS_LEVEL_1);
 }
 
-int
-lsp_refresh_l2 (struct thread *thread)
+static int
+lsp_l2_refresh (struct thread *thread)
 {
   struct isis_area *area;
-  unsigned long ref_time;
 
   area = THREAD_ARG (thread);
   assert (area);
 
   area->t_lsp_refresh[1] = NULL;
-  if (area->is_type & IS_LEVEL_2)
-    lsp_non_pseudo_regenerate (area, 2);
-
-  ref_time = area->lsp_refresh[1] > MAX_LSP_GEN_INTERVAL ?
-    MAX_LSP_GEN_INTERVAL : area->lsp_refresh[1];
-
-  THREAD_TIMER_ON (master, area->t_lsp_refresh[1], lsp_refresh_l2, area,
-		   isis_jitter (ref_time, MAX_AGE_JITTER));
-
-  return ISIS_OK;
-}
-
-/*
- * Something has changed -> regenerate LSP
- */
-
-static int
-lsp_l1_regenerate (struct thread *thread)
-{
-  struct isis_area *area;
-
-  area = THREAD_ARG (thread);
-  area->lsp_regenerate_pending[0] = 0;
-
-  return lsp_non_pseudo_regenerate (area, 1);
-}
-
-static int
-lsp_l2_regenerate (struct thread *thread)
-{
-  struct isis_area *area;
-
-  area = THREAD_ARG (thread);
   area->lsp_regenerate_pending[1] = 0;
 
-  return lsp_non_pseudo_regenerate (area, 2);
+  if ((area->is_type & IS_LEVEL_2) == 0)
+    return ISIS_ERROR;
+
+  return lsp_regenerate (area, IS_LEVEL_2);
 }
 
 int
-lsp_regenerate_schedule (struct isis_area *area)
+lsp_regenerate_schedule (struct isis_area *area, int level, int all_pseudo)
 {
   struct isis_lsp *lsp;
   u_char id[ISIS_SYS_ID_LEN + 2];
   time_t now, diff;
+  struct listnode *cnode;
+  struct isis_circuit *circuit;
+  int lvl;
+
+  if (area == NULL)
+    return ISIS_ERROR;
+
   memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
   LSP_PSEUDO_ID (id) = LSP_FRAGMENT (id) = 0;
   now = time (NULL);
-  /*
-   * First level 1
-   */
-  if (area->is_type & IS_LEVEL_1)
+
+  for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; lvl++)
     {
-      lsp = lsp_search (id, area->lspdb[0]);
-      if (!lsp || area->lsp_regenerate_pending[0])
-	goto L2;
+      if (!((level & lvl) && (area->is_type & lvl)))
+        continue;
+
+      if (area->lsp_regenerate_pending[lvl - 1])
+        continue;
+
+      lsp = lsp_search (id, area->lspdb[lvl - 1]);
+      if (!lsp)
+        continue;
+
       /*
        * Throttle avoidance
        */
+      THREAD_TIMER_OFF (area->t_lsp_refresh[lvl - 1]);
       diff = now - lsp->last_generated;
-      if (diff < MIN_LSP_GEN_INTERVAL)
-	{
-	  area->lsp_regenerate_pending[0] = 1;
-	  area->t_lsp_l1_regenerate=thread_add_timer (master, lsp_l1_regenerate, area,
-			    MIN_LSP_GEN_INTERVAL - diff);
-	  goto L2;
-	}
+      if (diff < area->lsp_gen_interval[lvl - 1])
+        {
+          area->lsp_regenerate_pending[lvl - 1] = 1;
+          if (lvl == IS_LEVEL_1)
+            THREAD_TIMER_ON (master, area->t_lsp_refresh[lvl - 1],
+                             lsp_l1_refresh, area,
+                             area->lsp_gen_interval[lvl - 1] - diff);
+          else if (lvl == IS_LEVEL_2)
+            THREAD_TIMER_ON (master, area->t_lsp_refresh[lvl - 1],
+                             lsp_l2_refresh, area,
+                             area->lsp_gen_interval[lvl - 1] - diff);
+        }
       else
-	lsp_non_pseudo_regenerate (area, 1);
+        {
+          lsp_regenerate (area, lvl);
+        }
     }
-  /*
-   * then 2
-   */
-L2:
-  if (area->is_type & IS_LEVEL_2)
+
+  if (all_pseudo)
     {
-      lsp = lsp_search (id, area->lspdb[1]);
-      if (!lsp || area->lsp_regenerate_pending[1])
-	return ISIS_OK;
-      /*
-       * Throttle avoidance
-       */
-      diff = now - lsp->last_generated;
-      if (diff < MIN_LSP_GEN_INTERVAL)
-	{
-	  area->lsp_regenerate_pending[1] = 1;
-	  area->t_lsp_l2_regenerate=thread_add_timer (master, lsp_l2_regenerate, area,
-			    MIN_LSP_GEN_INTERVAL - diff);
-	  return ISIS_OK;
-	}
-      else
-	lsp_non_pseudo_regenerate (area, 2);
+      for (ALL_LIST_ELEMENTS_RO (area->circuit_list, cnode, circuit))
+        lsp_regenerate_schedule_pseudo (circuit, level);
     }
 
   return ISIS_OK;
@@ -1706,19 +1811,10 @@
   struct es_neigh *es_neigh;
   struct list *adj_list;
   struct listnode *node;
-  struct isis_passwd *passwd;
-
-  assert (circuit);
-  assert (circuit->circ_type == CIRCUIT_T_BROADCAST);
-
-  if (!circuit->u.bc.is_dr[level - 1])
-    return;			/* we are not DIS on this circuit */
 
   lsp->level = level;
-  if (level == 1)
-    lsp->lsp_header->lsp_bits |= IS_LEVEL_1;
-  else
-    lsp->lsp_header->lsp_bits |= IS_LEVEL_2;
+  /* RFC3787  section 4 SHOULD not set overload bit in pseudo LSPs */
+  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, 0);
 
   /*
    * add self to IS neighbours 
@@ -1753,12 +1849,12 @@
 
   for (ALL_LIST_ELEMENTS_RO (adj_list, node, adj))
     {
-      if (adj->circuit_t & level)
+      if (adj->level & level)
 	{
-	  if ((level == 1 && adj->sys_type == ISIS_SYSTYPE_L1_IS) ||
-	      (level == 1 && adj->sys_type == ISIS_SYSTYPE_L2_IS &&
+	  if ((level == IS_LEVEL_1 && adj->sys_type == ISIS_SYSTYPE_L1_IS) ||
+	      (level == IS_LEVEL_1 && adj->sys_type == ISIS_SYSTYPE_L2_IS &&
 	      adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
-	      (level == 2 && adj->sys_type == ISIS_SYSTYPE_L2_IS))
+	      (level == IS_LEVEL_2 && adj->sys_type == ISIS_SYSTYPE_L2_IS))
 	    {
 	      /* an IS neighbour -> add it */
 	      if (circuit->area->oldmetric)
@@ -1776,7 +1872,7 @@
 		  listnode_add (lsp->tlv_data.te_is_neighs, te_is_neigh);
 		}
 	    }
-	  else if (level == 1 && adj->sys_type == ISIS_SYSTYPE_ES)
+	  else if (level == IS_LEVEL_1 && adj->sys_type == ISIS_SYSTYPE_ES)
 	    {
 	      /* an ES neigbour add it, if we are building level 1 LSP */
 	      /* FIXME: the tlv-format is hard to use here */
@@ -1792,6 +1888,7 @@
 	    }
 	}
     }
+  list_delete (adj_list);
 
   /* Reset endp of stream to overwrite only TLV part of it. */
   stream_reset (lsp->pdu);
@@ -1800,13 +1897,7 @@
   /*
    * Add the authentication info if it's present
    */
-  (level == 1) ? (passwd = &circuit->area->area_passwd) :
-    (passwd = &circuit->area->domain_passwd);
-  if (passwd->type)
-    {
-      memcpy (&lsp->tlv_data.auth_info, passwd, sizeof (struct isis_passwd));
-      tlv_add_authinfo (passwd->type, passwd->len, passwd->passwd, lsp->pdu);
-    }
+  lsp_auth_add (lsp);
 
   if (lsp->tlv_data.is_neighs && listcount (lsp->tlv_data.is_neighs) > 0)
     tlv_add_is_neighs (lsp->tlv_data.is_neighs, lsp->pdu);
@@ -1818,20 +1909,89 @@
     tlv_add_is_neighs (lsp->tlv_data.es_neighs, lsp->pdu);
 
   lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
-  fletcher_checksum (STREAM_DATA (lsp->pdu) + 12,
-		   ntohs (lsp->lsp_header->pdu_len) - 12, 12);
 
-  list_delete (adj_list);
+  /* Recompute authentication and checksum information */
+  lsp_auth_update (lsp);
+  fletcher_checksum(STREAM_DATA (lsp->pdu) + 12,
+                    ntohs (lsp->lsp_header->pdu_len) - 12, 12);
 
   return;
 }
 
-static int
-lsp_pseudo_regenerate (struct isis_circuit *circuit, int level)
+int
+lsp_generate_pseudo (struct isis_circuit *circuit, int level)
 {
   dict_t *lspdb = circuit->area->lspdb[level - 1];
   struct isis_lsp *lsp;
   u_char lsp_id[ISIS_SYS_ID_LEN + 2];
+  u_int16_t rem_lifetime, refresh_time;
+
+  if ((circuit->is_type & level) != level ||
+      (circuit->state != C_STATE_UP) ||
+      (circuit->circ_type != CIRCUIT_T_BROADCAST) ||
+      (circuit->u.bc.is_dr[level - 1] == 0))
+    return ISIS_ERROR;
+
+  memcpy (lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
+  LSP_FRAGMENT (lsp_id) = 0;
+  LSP_PSEUDO_ID (lsp_id) = circuit->circuit_id;
+
+  /*
+   * If for some reason have a pseudo LSP in the db already -> regenerate
+   */
+  if (lsp_search (lsp_id, lspdb))
+    return lsp_regenerate_schedule_pseudo (circuit, level);
+
+  rem_lifetime = lsp_rem_lifetime (circuit->area, level);
+  /* RFC3787  section 4 SHOULD not set overload bit in pseudo LSPs */
+  lsp = lsp_new (lsp_id, rem_lifetime, 1, circuit->area->is_type, 0, level);
+  lsp->area = circuit->area;
+
+  lsp_build_pseudo (lsp, circuit, level);
+
+  lsp->own_lsp = 1;
+  lsp_insert (lsp, lspdb);
+  lsp_set_all_srmflags (lsp);
+
+  refresh_time = lsp_refresh_time (lsp, rem_lifetime);
+  THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
+  circuit->lsp_regenerate_pending[level - 1] = 0;
+  if (level == IS_LEVEL_1)
+    THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[level - 1],
+                     lsp_l1_refresh_pseudo, circuit, refresh_time);
+  else if (level == IS_LEVEL_2)
+    THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[level - 1],
+                     lsp_l2_refresh_pseudo, circuit, refresh_time);
+
+  if (isis->debugs & DEBUG_UPDATE_PACKETS)
+    {
+      zlog_debug ("ISIS-Upd (%s): Building L%d Pseudo LSP %s, len %d, "
+                  "seq 0x%08x, cksum 0x%04x, lifetime %us, refresh %us",
+                  circuit->area->area_tag, level,
+                  rawlspid_print (lsp->lsp_header->lsp_id),
+                  ntohl (lsp->lsp_header->pdu_len),
+                  ntohl (lsp->lsp_header->seq_num),
+                  ntohs (lsp->lsp_header->checksum),
+                  ntohs (lsp->lsp_header->rem_lifetime),
+                  refresh_time);
+    }
+
+  return ISIS_OK;
+}
+
+static int
+lsp_regenerate_pseudo (struct isis_circuit *circuit, int level)
+{
+  dict_t *lspdb = circuit->area->lspdb[level - 1];
+  struct isis_lsp *lsp;
+  u_char lsp_id[ISIS_SYS_ID_LEN + 2];
+  u_int16_t rem_lifetime, refresh_time;
+
+  if ((circuit->is_type & level) != level ||
+      (circuit->state != C_STATE_UP) ||
+      (circuit->circ_type != CIRCUIT_T_BROADCAST) ||
+      (circuit->u.bc.is_dr[level - 1] == 0))
+    return ISIS_ERROR;
 
   memcpy (lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
   LSP_PSEUDO_ID (lsp_id) = circuit->circuit_id;
@@ -1841,151 +2001,154 @@
 
   if (!lsp)
     {
-      zlog_err ("lsp_pseudo_regenerate(): no l%d LSP %s found!", level,
-		rawlspid_print (lsp_id));
+      zlog_err ("lsp_regenerate_pseudo: no l%d LSP %s found!",
+                level, rawlspid_print (lsp_id));
       return ISIS_ERROR;
     }
   lsp_clear_data (lsp);
 
   lsp_build_pseudo (lsp, circuit, level);
 
-  lsp->lsp_header->rem_lifetime =
-    htons (isis_jitter (circuit->area->max_lsp_lifetime[level - 1],
-			MAX_AGE_JITTER));
-
+  /* RFC3787  section 4 SHOULD not set overload bit in pseudo LSPs */
+  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, 0);
+  rem_lifetime = lsp_rem_lifetime (circuit->area, level);
+  lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
   lsp_inc_seqnum (lsp, 0);
+  lsp->last_generated = time (NULL);
+  lsp_set_all_srmflags (lsp);
+
+  refresh_time = lsp_refresh_time (lsp, rem_lifetime);
+  if (level == IS_LEVEL_1)
+    THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[level - 1],
+                     lsp_l1_refresh_pseudo, circuit, refresh_time);
+  else if (level == IS_LEVEL_2)
+    THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[level - 1],
+                     lsp_l2_refresh_pseudo, circuit, refresh_time);
 
   if (isis->debugs & DEBUG_UPDATE_PACKETS)
     {
-      zlog_debug ("ISIS-Upd (%s): refreshing pseudo LSP L%d %s",
-		  circuit->area->area_tag, level,
-		  rawlspid_print (lsp->lsp_header->lsp_id));
+      zlog_debug ("ISIS-Upd (%s): Refreshing L%d Pseudo LSP %s, len %d, "
+                  "seq 0x%08x, cksum 0x%04x, lifetime %us, refresh %us",
+                  circuit->area->area_tag, level,
+                  rawlspid_print (lsp->lsp_header->lsp_id),
+                  ntohl (lsp->lsp_header->pdu_len),
+                  ntohl (lsp->lsp_header->seq_num),
+                  ntohs (lsp->lsp_header->checksum),
+                  ntohs (lsp->lsp_header->rem_lifetime),
+                  refresh_time);
     }
 
-  lsp->last_generated = time (NULL);
-  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
-
   return ISIS_OK;
 }
 
-int
+/*
+ * Something has changed or periodic refresh -> regenerate pseudo LSP
+ */
+static int
 lsp_l1_refresh_pseudo (struct thread *thread)
 {
   struct isis_circuit *circuit;
-  int retval;
-  unsigned long ref_time;
+  u_char id[ISIS_SYS_ID_LEN + 2];
 
   circuit = THREAD_ARG (thread);
 
-  if (!circuit->u.bc.is_dr[0])
-    return ISIS_ERROR;		/* FIXME: purge and such */
-
   circuit->u.bc.t_refresh_pseudo_lsp[0] = NULL;
+  circuit->lsp_regenerate_pending[0] = 0;
 
-  retval = lsp_pseudo_regenerate (circuit, 1);
+  if ((circuit->u.bc.is_dr[0] == 0) ||
+      (circuit->is_type & IS_LEVEL_1) == 0)
+    {
+      memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
+      LSP_PSEUDO_ID (id) = circuit->circuit_id;
+      LSP_FRAGMENT (id) = 0;
+      lsp_purge_pseudo (id, circuit, IS_LEVEL_1);
+      return ISIS_ERROR;
+    }
 
-  ref_time = circuit->area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
-    MAX_LSP_GEN_INTERVAL : circuit->area->lsp_refresh[0];
-
-  THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[0],
-		   lsp_l1_refresh_pseudo, circuit,
-		   isis_jitter (ref_time, MAX_AGE_JITTER));
-
-  return retval;
+  return lsp_regenerate_pseudo (circuit, IS_LEVEL_1);
 }
 
-int
-lsp_l1_pseudo_generate (struct isis_circuit *circuit)
-{
-  struct isis_lsp *lsp;
-  u_char id[ISIS_SYS_ID_LEN + 2];
-  unsigned long ref_time;
-
-  memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
-  LSP_FRAGMENT (id) = 0;
-  LSP_PSEUDO_ID (id) = circuit->circuit_id;
-
-  /*
-   * If for some reason have a pseudo LSP in the db already -> regenerate
-   */
-  if (lsp_search (id, circuit->area->lspdb[0]))
-    return lsp_pseudo_regenerate (circuit, 1);
-  lsp = lsp_new (id, circuit->area->max_lsp_lifetime[0],
-		 1, circuit->area->is_type, 0, 1);
-
-  lsp_build_pseudo (lsp, circuit, 1);
-
-  lsp->own_lsp = 1;
-  lsp_insert (lsp, circuit->area->lspdb[0]);
-  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
-
-  ref_time = circuit->area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
-    MAX_LSP_GEN_INTERVAL : circuit->area->lsp_refresh[0];
-
-  THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[0],
-		   lsp_l1_refresh_pseudo, circuit,
-		   isis_jitter (ref_time, MAX_AGE_JITTER));
-
-  return lsp_regenerate_schedule (circuit->area);
-}
-
-int
+static int
 lsp_l2_refresh_pseudo (struct thread *thread)
 {
   struct isis_circuit *circuit;
-  int retval;
-  unsigned long ref_time;
+  u_char id[ISIS_SYS_ID_LEN + 2];
+
   circuit = THREAD_ARG (thread);
 
-  if (!circuit->u.bc.is_dr[1])
-    return ISIS_ERROR;		/* FIXME: purge and such */
-
   circuit->u.bc.t_refresh_pseudo_lsp[1] = NULL;
+  circuit->lsp_regenerate_pending[1] = 0;
 
-  retval = lsp_pseudo_regenerate (circuit, 2);
+  if ((circuit->u.bc.is_dr[1] == 0) ||
+      (circuit->is_type & IS_LEVEL_2) == 0)
+    {
+      memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
+      LSP_PSEUDO_ID (id) = circuit->circuit_id;
+      LSP_FRAGMENT (id) = 0;
+      lsp_purge_pseudo (id, circuit, IS_LEVEL_2);
+      return ISIS_ERROR;
+    }
 
-  ref_time = circuit->area->lsp_refresh[1] > MAX_LSP_GEN_INTERVAL ?
-    MAX_LSP_GEN_INTERVAL : circuit->area->lsp_refresh[1];
-
-  THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[1],
-		   lsp_l2_refresh_pseudo, circuit,
-		   isis_jitter (ref_time, MAX_AGE_JITTER));
-
-  return retval;
+  return lsp_regenerate_pseudo (circuit, IS_LEVEL_2);
 }
 
 int
-lsp_l2_pseudo_generate (struct isis_circuit *circuit)
+lsp_regenerate_schedule_pseudo (struct isis_circuit *circuit, int level)
 {
   struct isis_lsp *lsp;
-  u_char id[ISIS_SYS_ID_LEN + 2];
-  unsigned long ref_time;
+  u_char lsp_id[ISIS_SYS_ID_LEN + 2];
+  time_t now, diff;
+  int lvl;
 
-  memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
-  LSP_FRAGMENT (id) = 0;
-  LSP_PSEUDO_ID (id) = circuit->circuit_id;
+  if (circuit == NULL ||
+      circuit->circ_type != CIRCUIT_T_BROADCAST ||
+      circuit->state != C_STATE_UP)
+    return ISIS_OK;
 
-  if (lsp_search (id, circuit->area->lspdb[1]))
-    return lsp_pseudo_regenerate (circuit, 2);
+  memcpy (lsp_id, isis->sysid, ISIS_SYS_ID_LEN);
+  LSP_PSEUDO_ID (lsp_id) = circuit->circuit_id;
+  LSP_FRAGMENT (lsp_id) = 0;
+  now = time (NULL);
 
-  lsp = lsp_new (id, circuit->area->max_lsp_lifetime[1],
-		 1, circuit->area->is_type, 0, 2);
+  for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; lvl++)
+    {
+      if (!((level & lvl) && (circuit->is_type & lvl)))
+        continue;
 
-  lsp_build_pseudo (lsp, circuit, 2);
+      if (circuit->u.bc.is_dr[lvl - 1] == 0 ||
+          circuit->lsp_regenerate_pending[lvl - 1])
+        continue;
 
-  ref_time = circuit->area->lsp_refresh[1] > MAX_LSP_GEN_INTERVAL ?
-    MAX_LSP_GEN_INTERVAL : circuit->area->lsp_refresh[1];
+      lsp = lsp_search (lsp_id, circuit->area->lspdb[lvl - 1]);
+      if (!lsp)
+        continue;
 
+      /*
+       * Throttle avoidance
+       */
+      THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1]);
+      diff = now - lsp->last_generated;
+      if (diff < circuit->area->lsp_gen_interval[lvl - 1])
+        {
+          circuit->lsp_regenerate_pending[lvl - 1] = 1;
+          if (lvl == IS_LEVEL_1)
+            THREAD_TIMER_ON (master,
+                             circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1],
+                             lsp_l1_refresh_pseudo, circuit,
+                             circuit->area->lsp_gen_interval[lvl - 1] - diff);
+          else if (lvl == IS_LEVEL_2)
+            THREAD_TIMER_ON (master,
+                             circuit->u.bc.t_refresh_pseudo_lsp[lvl - 1],
+                             lsp_l2_refresh_pseudo, circuit,
+                             circuit->area->lsp_gen_interval[lvl - 1] - diff);
+        }
+      else
+        {
+          lsp_regenerate_pseudo (circuit, lvl);
+        }
+    }
 
-  lsp->own_lsp = 1;
-  lsp_insert (lsp, circuit->area->lspdb[1]);
-  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
-
-  THREAD_TIMER_ON (master, circuit->u.bc.t_refresh_pseudo_lsp[1],
-		   lsp_l2_refresh_pseudo, circuit,
-		   isis_jitter (ref_time, MAX_AGE_JITTER));
-
-  return lsp_regenerate_schedule (circuit->area);
+  return ISIS_OK;
 }
 
 /*
@@ -2003,6 +2166,7 @@
   struct listnode *lspnode, *cnode;
   dnode_t *dnode, *dnode_next;
   int level;
+  u_int16_t rem_lifetime;
 
   lsp_list = list_new ();
 
@@ -2018,56 +2182,87 @@
   for (level = 0; level < ISIS_LEVELS; level++)
     {
       if (area->lspdb[level] && dict_count (area->lspdb[level]) > 0)
-	{
-	  dnode = dict_first (area->lspdb[level]);
-	  while (dnode != NULL)
-	    {
-	      dnode_next = dict_next (area->lspdb[level], dnode);
-	      lsp = dnode_get (dnode);
-	      lsp_set_time (lsp);
-	      if (lsp->age_out == 0)
-		{
+        {
+          for (dnode = dict_first (area->lspdb[level]);
+               dnode != NULL; dnode = dnode_next)
+            {
+              dnode_next = dict_next (area->lspdb[level], dnode);
+              lsp = dnode_get (dnode);
 
-		  zlog_debug ("ISIS-Upd (%s): L%u LSP %s seq 0x%08x aged out",
-			      area->area_tag,
-			      lsp->level,
-			      rawlspid_print (lsp->lsp_header->lsp_id),
-			      ntohl (lsp->lsp_header->seq_num));
+              /*
+               * The lsp rem_lifetime is kept at 0 for MaxAge or
+               * ZeroAgeLifetime depending on explicit purge or
+               * natural age out. So schedule spf only once when
+               * the first time rem_lifetime becomes 0.
+               */
+              rem_lifetime = ntohs(lsp->lsp_header->rem_lifetime);
+              lsp_set_time (lsp);
+
+              /*
+               * Schedule may run spf which should be done only after
+               * the lsp rem_lifetime becomes 0 for the first time.
+               * ISO 10589 - 7.3.16.4 first paragraph.
+               */
+              if (rem_lifetime == 1 && lsp->lsp_header->seq_num != 0)
+                {
+                  /* 7.3.16.4 a) set SRM flags on all */
+                  lsp_set_all_srmflags (lsp);
+                  /* 7.3.16.4 b) retain only the header FIXME  */
+                  /* 7.3.16.4 c) record the time to purge FIXME */
+                  /* run/schedule spf */
+                  /* isis_spf_schedule is called inside lsp_destroy() below;
+                   * so it is not needed here. */
+                  /* isis_spf_schedule (lsp->area, lsp->level); */
+                }
+
+              if (lsp->age_out == 0)
+                {
+                  zlog_debug ("ISIS-Upd (%s): L%u LSP %s seq 0x%08x aged out",
+                              area->area_tag,
+                              lsp->level,
+                              rawlspid_print (lsp->lsp_header->lsp_id),
+                              ntohl (lsp->lsp_header->seq_num));
 #ifdef TOPOLOGY_GENERATE
-		  if (lsp->from_topology)
-		    THREAD_TIMER_OFF (lsp->t_lsp_top_ref);
+                  if (lsp->from_topology)
+                    THREAD_TIMER_OFF (lsp->t_lsp_top_ref);
 #endif /* TOPOLOGY_GENERATE */
-		  lsp_destroy (lsp);
-		  dict_delete (area->lspdb[level], dnode);
-		}
-	      else if (flags_any_set (lsp->SRMflags))
-		listnode_add (lsp_list, lsp);
-	      dnode = dnode_next;
-	    }
+                  lsp_destroy (lsp);
+                  lsp = NULL;
+                  dict_delete_free (area->lspdb[level], dnode);
+                }
+              else if (flags_any_set (lsp->SRMflags))
+                listnode_add (lsp_list, lsp);
+            }
 
-	  /*
-	   * Send LSPs on circuits indicated by the SRMflags
-	   */
-	  if (listcount (lsp_list) > 0)
-	    {
+          /*
+           * Send LSPs on circuits indicated by the SRMflags
+           */
+          if (listcount (lsp_list) > 0)
+            {
               for (ALL_LIST_ELEMENTS_RO (area->circuit_list, cnode, circuit))
-		{
-		  if (circuit->state != C_STATE_UP)
-		    continue;
+                {
+                  int diff = time (NULL) - circuit->lsp_queue_last_cleared;
+                  if (circuit->lsp_queue == NULL ||
+                      diff < MIN_LSP_TRANS_INTERVAL)
+                    continue;
                   for (ALL_LIST_ELEMENTS_RO (lsp_list, lspnode, lsp))
-		    {
-		      if (ISIS_CHECK_FLAG (lsp->SRMflags, circuit))
-			{
-			  /* FIXME: if same or elder lsp is already in lsp
-			   * queue */
-			  listnode_add (circuit->lsp_queue, lsp);
-			  thread_add_event (master, send_lsp, circuit, 0);
-			}
-		    }
-		}
-	    }
-	  list_delete_all_node (lsp_list);
-	}
+                    {
+                      if (circuit->upadjcount[lsp->level - 1] &&
+                          ISIS_CHECK_FLAG (lsp->SRMflags, circuit))
+                        {
+                          /* Add the lsp only if it is not already in lsp
+                           * queue */
+                          if (! listnode_lookup (circuit->lsp_queue, lsp))
+                            {
+                              listnode_add (circuit->lsp_queue, lsp);
+                              thread_add_event (master, send_lsp, circuit, 0);
+                            }
+                        }
+                    }
+                }
+              list_delete_all_node (lsp_list);
+            }
+        }
     }
 
   list_delete (lsp_list);
@@ -2076,22 +2271,45 @@
 }
 
 void
-lsp_purge_dr (u_char * id, struct isis_circuit *circuit, int level)
+lsp_purge_pseudo (u_char * id, struct isis_circuit *circuit, int level)
 {
   struct isis_lsp *lsp;
+  u_int16_t seq_num;
+  u_int8_t lsp_bits;
 
   lsp = lsp_search (id, circuit->area->lspdb[level - 1]);
+  if (!lsp)
+    return;
 
-  if (lsp && lsp->purged == 0)
-    {
-      lsp->lsp_header->rem_lifetime = htons (0);
-      lsp->lsp_header->pdu_len =
-	htons (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
-      lsp->purged = 0;
-      fletcher_checksum (STREAM_DATA (lsp->pdu) + 12,
-		       ntohs (lsp->lsp_header->pdu_len) - 12, 12);
-      ISIS_FLAGS_SET_ALL (lsp->SRMflags);
-    }
+  /* store old values */
+  seq_num = lsp->lsp_header->seq_num;
+  lsp_bits = lsp->lsp_header->lsp_bits;
+
+  /* reset stream */
+  lsp_clear_data (lsp);
+  stream_reset (lsp->pdu);
+
+  /* update header */
+  lsp->lsp_header->pdu_len = htons (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
+  memcpy (lsp->lsp_header->lsp_id, id, ISIS_SYS_ID_LEN + 2);
+  lsp->lsp_header->checksum = 0;
+  lsp->lsp_header->seq_num = seq_num;
+  lsp->lsp_header->rem_lifetime = 0;
+  lsp->lsp_header->lsp_bits = lsp_bits;
+  lsp->level = level;
+  lsp->age_out = lsp->area->max_lsp_lifetime[level-1];
+  stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
+
+  /*
+   * Add and update the authentication info if its present
+   */
+  lsp_auth_add (lsp);
+  lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
+  lsp_auth_update (lsp);
+  fletcher_checksum(STREAM_DATA (lsp->pdu) + 12,
+                    ntohs (lsp->lsp_header->pdu_len) - 12, 12);
+
+  lsp_set_all_srmflags (lsp);
 
   return;
 }
@@ -2109,27 +2327,36 @@
   /*
    * We need to create the LSP to be purged 
    */
-  zlog_debug ("LSP PURGE NON EXIST");
   lsp = XCALLOC (MTYPE_ISIS_LSP, sizeof (struct isis_lsp));
-  /*FIXME: BUG BUG BUG! the lsp doesn't exist here! */
-  /*did smt here, maybe good probably not */
-  lsp->level = ((lsp_hdr->lsp_bits & LSPBIT_IST) == IS_LEVEL_1) ? 1 : 2;
-  lsp->pdu = stream_new (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
+  lsp->area = area;
+  lsp->level = ((lsp_hdr->lsp_bits & LSPBIT_IST) == IS_LEVEL_1) ?
+    IS_LEVEL_1 : IS_LEVEL_2;
+  /* FIXME: Should be minimal mtu? */
+  lsp->pdu = stream_new (1500);
   lsp->isis_header = (struct isis_fixed_hdr *) STREAM_DATA (lsp->pdu);
-  fill_fixed_hdr (lsp->isis_header, (lsp->level == 1) ? L1_LINK_STATE
+  fill_fixed_hdr (lsp->isis_header, (lsp->level == IS_LEVEL_1) ? L1_LINK_STATE
 		  : L2_LINK_STATE);
   lsp->lsp_header = (struct isis_link_state_hdr *) (STREAM_DATA (lsp->pdu) +
 						    ISIS_FIXED_HDR_LEN);
   memcpy (lsp->lsp_header, lsp_hdr, ISIS_LSP_HDR_LEN);
+  stream_forward_endp (lsp->pdu, ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
 
   /*
-   * Retain only LSP header
-   */
-  lsp->lsp_header->pdu_len = htons (ISIS_FIXED_HDR_LEN + ISIS_LSP_HDR_LEN);
-  /*
    * Set the remaining lifetime to 0
    */
   lsp->lsp_header->rem_lifetime = 0;
+
+  /*
+   * Add and update the authentication info if its present
+   */
+  lsp_auth_add (lsp);
+  lsp_auth_update (lsp);
+
+  /*
+   * Update the PDU length to header plus any authentication TLV.
+   */
+  lsp->lsp_header->pdu_len = htons (stream_get_endp (lsp->pdu));
+
   /*
    * Put the lsp into LSPdb
    */
@@ -2138,17 +2365,36 @@
   /*
    * Send in to whole area
    */
-  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
+  lsp_set_all_srmflags (lsp);
 
   return;
 }
 
+void lsp_set_all_srmflags (struct isis_lsp *lsp)
+{
+  struct listnode *node;
+  struct isis_circuit *circuit;
+
+  assert (lsp);
+
+  ISIS_FLAGS_CLEAR_ALL(lsp->SRMflags);
+
+  if (lsp->area)
+    {
+      struct list *circuit_list = lsp->area->circuit_list;
+      for (ALL_LIST_ELEMENTS_RO (circuit_list, node, circuit))
+        {
+          ISIS_SET_FLAG(lsp->SRMflags, circuit);
+        }
+    }
+}
+
 #ifdef TOPOLOGY_GENERATE
 static int
 top_lsp_refresh (struct thread *thread)
 {
   struct isis_lsp *lsp;
-  unsigned long ref_time;
+  u_int16_t rem_lifetime, refresh_time;
 
   lsp = THREAD_ARG (thread);
   assert (lsp);
@@ -2157,7 +2403,7 @@
 
   lsp_seqnum_update (lsp);
 
-  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
+  lsp_set_all_srmflags (lsp);
   if (isis->debugs & DEBUG_UPDATE_PACKETS)
     {
       zlog_debug ("ISIS-Upd (): refreshing Topology L1 %s",
@@ -2167,14 +2413,14 @@
   isis_dynhn_insert (lsp->lsp_header->lsp_id, lsp->tlv_data.hostname,
 		     IS_LEVEL_1);
 
-  lsp->lsp_header->rem_lifetime =
-    htons (isis_jitter (lsp->area->max_lsp_lifetime[0], MAX_AGE_JITTER));
+  lsp->lsp_header->lsp_bits = lsp_bits_generate (level,
+                                                 lsp->area->overload_bit);
+  rem_lifetime = lsp_rem_lifetime (lsp->area, IS_LEVEL_1);
+  lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
 
-  ref_time = lsp->area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
-    MAX_LSP_GEN_INTERVAL : lsp->area->lsp_refresh[0];
-
+  refresh_time = lsp_refresh_time (lsp, rem_lifetime);
   THREAD_TIMER_ON (master, lsp->t_lsp_top_ref, top_lsp_refresh, lsp,
-		   isis_jitter (ref_time, MAX_LSP_GEN_JITTER));
+		   lsp->area->lsp_refresh[0]);
 
   return ISIS_OK;
 }
@@ -2187,16 +2433,16 @@
   struct arc *arc;
   u_char lspid[ISIS_SYS_ID_LEN + 2];
   struct isis_lsp *lsp;
-  unsigned long ref_time;
+  u_int16_t rem_lifetime, refresh_time;
 
   /* first we find the maximal node */
   for (ALL_LIST_ELEMENTS_RO (area->topology, node, arc))
-  {
-    if (arc->from_node > max)
-      max = arc->from_node;
-    if (arc->to_node > max)
-      max = arc->to_node;
-  }
+    {
+      if (arc->from_node > max)
+        max = arc->from_node;
+      if (arc->to_node > max)
+        max = arc->to_node;
+    }
 
   for (i = 1; i < (max + 1); i++)
     {
@@ -2206,12 +2452,13 @@
       lspid[ISIS_SYS_ID_LEN - 1] = (i & 0xFF);
       lspid[ISIS_SYS_ID_LEN - 2] = ((i >> 8) & 0xFF);
 
-      lsp = lsp_new (lspid, isis_jitter (area->max_lsp_lifetime[0],
-		     MAX_AGE_JITTER), 1, IS_LEVEL_1, 0, 1);
+      rem_lifetime = lsp_rem_lifetime (area, IS_LEVEL_1);
+      lsp = lsp_new (lspid, rem_lifetime, 1, IS_LEVEL_1 | area->overload_bit,
+                     0, 1);
       if (!lsp)
 	return;
-      lsp->from_topology = 1;
       lsp->area = area;
+      lsp->from_topology = 1;
 
       /* Creating LSP data based on topology info. */
       build_topology_lsp_data (lsp, area, i);
@@ -2220,12 +2467,10 @@
       /* Take care of inserting dynamic hostname into cache. */
       isis_dynhn_insert (lspid, lsp->tlv_data.hostname, IS_LEVEL_1);
 
-      ref_time = area->lsp_refresh[0] > MAX_LSP_GEN_INTERVAL ?
-	MAX_LSP_GEN_INTERVAL : area->lsp_refresh[0];
-
+      refresh_time = lsp_refresh_time (lsp, rem_lifetime);
       THREAD_TIMER_ON (master, lsp->t_lsp_top_ref, top_lsp_refresh, lsp,
-		       isis_jitter (ref_time, MAX_LSP_GEN_JITTER));
-      ISIS_FLAGS_SET_ALL (lsp->SRMflags);
+		       refresh_time);
+      lsp_set_all_srmflags (lsp);
       lsp_insert (lsp, area->lspdb[0]);
     }
 }
@@ -2342,8 +2587,6 @@
 
       if (area->newmetric)
 	{
-	  uint32_t metric;
-
 	  if (tlv_data.te_is_neighs == NULL)
 	    {
 	      tlv_data.te_is_neighs = list_new ();
@@ -2354,8 +2597,7 @@
 		  ISIS_SYS_ID_LEN);
 	  te_is_neigh->neigh_id[ISIS_SYS_ID_LEN - 1] = (to_lsp & 0xFF);
 	  te_is_neigh->neigh_id[ISIS_SYS_ID_LEN - 2] = ((to_lsp >> 8) & 0xFF);
-	  metric = ((htonl(arc->distance) >> 8) & 0xffffff);
-	  memcpy (te_is_neigh->te_metric, &metric, 3);
+	  SET_TE_METRIC(te_is_neigh, arc->distance);
 	  listnode_add (tlv_data.te_is_neighs, te_is_neigh);
 	}
     }
diff --git a/isisd/isis_lsp.h b/isisd/isis_lsp.h
index 8d648d0..6e7f745 100644
--- a/isisd/isis_lsp.h
+++ b/isisd/isis_lsp.h
@@ -24,10 +24,6 @@
 #ifndef _ZEBRA_ISIS_LSP_H
 #define _ZEBRA_ISIS_LSP_H
 
-/* The grand plan is to support 1024 circuits so we have 32*32 bit flags
- * the support will be achived using the newest drafts */
-#define ISIS_MAX_CIRCUITS 32 /* = 1024 - FIXME:defined in flags.h as well */
-
 /* Structure for isis_lsp, this structure will only support the fixed
  * System ID (Currently 6) (atleast for now). In order to support more
  * We will have to split the header into two parts, and for readability
@@ -42,15 +38,13 @@
     struct list *frags;
     struct isis_lsp *zero_lsp;
   } lspu;
+  u_int32_t auth_tlv_offset;    /* authentication TLV position in the pdu */
   u_int32_t SRMflags[ISIS_MAX_CIRCUITS];
   u_int32_t SSNflags[ISIS_MAX_CIRCUITS];
-  u_int32_t rexmit_queue[ISIS_MAX_CIRCUITS];
   int level;			/* L1 or L2? */
-  int purged;			/* have purged this one */
   int scheduled;		/* scheduled for sending */
   time_t installed;
   time_t last_generated;
-  time_t last_sent;
   int own_lsp;
 #ifdef TOPOLOGY_GENERATE
   int from_topology;
@@ -58,7 +52,6 @@
 #endif
   /* used for 60 second counting when rem_lifetime is zero */
   int age_out;
-  /* FIXME: For now only topology LSP's use this. Is it helpful for others? */
   struct isis_area *area;
   struct tlvs tlv_data;		/* Simplifies TLV access */
 };
@@ -67,37 +60,32 @@
 void lsp_db_destroy (dict_t * lspdb);
 int lsp_tick (struct thread *thread);
 
-int lsp_l1_generate (struct isis_area *area);
-int lsp_l2_generate (struct isis_area *area);
-int lsp_refresh_l1 (struct thread *thread);
-int lsp_refresh_l2 (struct thread *thread);
-int lsp_regenerate_schedule (struct isis_area *area);
+int lsp_generate (struct isis_area *area, int level);
+int lsp_regenerate_schedule (struct isis_area *area, int level,
+                             int all_pseudo);
+int lsp_generate_pseudo (struct isis_circuit *circuit, int level);
+int lsp_regenerate_schedule_pseudo (struct isis_circuit *circuit, int level);
 
-int lsp_l1_pseudo_generate (struct isis_circuit *circuit);
-int lsp_l2_pseudo_generate (struct isis_circuit *circuit);
-int lsp_l1_refresh_pseudo (struct thread *thread);
-int lsp_l2_refresh_pseudo (struct thread *thread);
-int isis_lsp_authinfo_check (struct stream *stream, struct isis_area *area,
-			     int pdulen, struct isis_passwd *passwd);
 struct isis_lsp *lsp_new (u_char * lsp_id, u_int16_t rem_lifetime,
 			  u_int32_t seq_num, u_int8_t lsp_bits,
 			  u_int16_t checksum, int level);
 struct isis_lsp *lsp_new_from_stream_ptr (struct stream *stream,
 					  u_int16_t pdu_len,
 					  struct isis_lsp *lsp0,
-					  struct isis_area *area);
+					  struct isis_area *area,
+                                          int level);
 void lsp_insert (struct isis_lsp *lsp, dict_t * lspdb);
 struct isis_lsp *lsp_search (u_char * id, dict_t * lspdb);
 
-void lsp_build_list (u_char * start_id, u_char * stop_id,
+void lsp_build_list (u_char * start_id, u_char * stop_id, u_char num_lsps,
 		     struct list *list, dict_t * lspdb);
 void lsp_build_list_nonzero_ht (u_char * start_id, u_char * stop_id,
 				struct list *list, dict_t * lspdb);
-void lsp_build_list_ssn (struct isis_circuit *circuit, struct list *list,
-			 dict_t * lspdb);
+void lsp_build_list_ssn (struct isis_circuit *circuit, u_char num_lsps,
+                         struct list *list, dict_t * lspdb);
 
 void lsp_search_and_destroy (u_char * id, dict_t * lspdb);
-void lsp_purge_dr (u_char * id, struct isis_circuit *circuit, int level);
+void lsp_purge_pseudo (u_char * id, struct isis_circuit *circuit, int level);
 void lsp_purge_non_exist (struct isis_link_state_hdr *lsp_hdr,
 			  struct isis_area *area);
 
@@ -114,13 +102,18 @@
 int lsp_id_cmp (u_char * id1, u_char * id2);
 int lsp_compare (char *areatag, struct isis_lsp *lsp, u_int32_t seq_num,
 		 u_int16_t checksum, u_int16_t rem_lifetime);
-void lsp_update (struct isis_lsp *lsp, struct isis_link_state_hdr *lsp_hdr,
-		 struct stream *stream, struct isis_area *area, int level);
+void lsp_update (struct isis_lsp *lsp, struct stream *stream,
+                 struct isis_area *area, int level);
 void lsp_inc_seqnum (struct isis_lsp *lsp, u_int32_t seq_num);
+void lsp_print (struct isis_lsp *lsp, struct vty *vty, char dynhost);
+void lsp_print_detail (struct isis_lsp *lsp, struct vty *vty, char dynhost);
 int lsp_print_all (struct vty *vty, dict_t * lspdb, char detail,
 		   char dynhost);
 const char *lsp_bits2string (u_char *);
 
+/* sets SRMflags for all active circuits of an lsp */
+void lsp_set_all_srmflags (struct isis_lsp *lsp);
+
 #ifdef TOPOLOGY_GENERATE
 void generate_topology_lsps (struct isis_area *area);
 void remove_topology_lsps (struct isis_area *area);
diff --git a/isisd/isis_main.c b/isisd/isis_main.c
index 15d85d6..f5266aa 100644
--- a/isisd/isis_main.c
+++ b/isisd/isis_main.c
@@ -44,6 +44,9 @@
 #include "isisd/isis_circuit.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_dynhn.h"
+#include "isisd/isis_spf.h"
+#include "isisd/isis_route.h"
+#include "isisd/isis_zebra.h"
 
 /* Default configuration file name */
 #define ISISD_DEFAULT_CONFIG "isisd.conf"
@@ -67,7 +70,7 @@
   .vty_group = VTY_GROUP,
 #endif
   .caps_p = _caps_p,
-  .cap_num_p = 2,
+  .cap_num_p = sizeof (_caps_p) / sizeof (*_caps_p),
   .cap_num_i = 0
 };
 
@@ -154,7 +157,10 @@
   zlog_debug ("Reload");
   /* FIXME: Clean up func call here */
   vty_reset ();
+  (void) isisd_privs.change (ZPRIVS_RAISE);
   execve (_progpath, _argv, _envp);
+  zlog_err ("Reload failed: cannot exec %s: %s", _progpath,
+      safe_strerror (errno));
 }
 
 static void
@@ -325,28 +331,31 @@
   memory_init ();
   access_list_init();
   isis_init ();
-  dyn_cache_init ();
+  isis_circuit_init ();
+  isis_spf_cmds_init ();
+
+  /* create the global 'isis' instance */
+  isis_new (1);
+
+  isis_zebra_init ();
+
   sort_node ();
 
   /* parse config file */
   /* this is needed three times! because we have interfaces before the areas */
   vty_read_config (config_file, config_default);
-  vty_read_config (config_file, config_default);
-  vty_read_config (config_file, config_default);
 
   /* Start execution only if not in dry-run mode */
   if (dryrun)
     return(0);
   
   /* demonize */
-  if (daemon_mode && daemon (0, 0) < 0)
-    {
-      zlog_err("ISISd daemon failed: %s", strerror(errno));
-      exit (1);
-    }
+  if (daemon_mode)
+    daemon (0, 0);
 
   /* Process ID file creation. */
-  pid_output (pid_file);
+  if (pid_file[0] != '\0')
+    pid_output (pid_file);
 
   /* Make isis vty socket. */
   vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
diff --git a/isisd/isis_misc.c b/isisd/isis_misc.c
index 6b565bc..968fa05 100644
--- a/isisd/isis_misc.c
+++ b/isisd/isis_misc.c
@@ -32,7 +32,9 @@
 #include "isisd/dict.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
+#include "isisd/isis_flags.h"
 #include "isisd/isis_circuit.h"
+#include "isisd/isis_csm.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_misc.h"
 
@@ -40,6 +42,7 @@
 #include "isisd/isis_lsp.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_adjacency.h"
+#include "isisd/isis_dynhn.h"
 
 /* staticly assigned vars for printing purposes */
 struct in_addr new_prefix;
@@ -99,10 +102,10 @@
  * extract dot from the dotted str, and insert all the number in a buff 
  */
 int
-dotformat2buff (u_char * buff, const u_char * dotted)
+dotformat2buff (u_char * buff, const char * dotted)
 {
   int dotlen, len = 0;
-  const u_char *pos = dotted;
+  const char *pos = dotted;
   u_char number[3];
   int nextdotpos = 2;
 
@@ -157,10 +160,10 @@
  * conversion of XXXX.XXXX.XXXX to memory
  */
 int
-sysid2buff (u_char * buff, const u_char * dotted)
+sysid2buff (u_char * buff, const char * dotted)
 {
   int len = 0;
-  const u_char *pos = dotted;
+  const char *pos = dotted;
   u_char number[3];
 
   number[2] = '\0';
@@ -271,7 +274,7 @@
  * Returns 0 on error, IS-IS Circuit Type on ok
  */
 int
-string2circuit_t (const u_char * str)
+string2circuit_t (const char * str)
 {
 
   if (!str)
@@ -290,6 +293,42 @@
 }
 
 const char *
+circuit_state2string (int state)
+{
+
+  switch (state)
+    {
+    case C_STATE_INIT:
+      return "Init";
+    case C_STATE_CONF:
+      return "Config";
+    case C_STATE_UP:
+      return "Up";
+    default:
+      return "Unknown";
+    }
+  return NULL;
+}
+
+const char *
+circuit_type2string (int type)
+{
+
+  switch (type)
+    {
+    case CIRCUIT_T_P2P:
+      return "p2p";
+    case CIRCUIT_T_BROADCAST:
+      return "lan";
+    case CIRCUIT_T_LOOPBACK:
+      return "loopback";
+    default:
+      return "Unknown";
+    }
+  return NULL;
+}
+
+const char *
 circuit_t2string (int circuit_t)
 {
   switch (circuit_t)
@@ -498,7 +537,6 @@
 {
   static struct utsname names;
   const char *hostname;
-  extern struct host host;
 
   hostname = host.name;
   if (!hostname)
@@ -509,3 +547,87 @@
 
   return hostname;
 }
+
+/*
+ * Returns the dynamic hostname associated with the passed system ID.
+ * If no dynamic hostname found then returns formatted system ID.
+ */
+const char *
+print_sys_hostname (u_char *sysid)
+{
+  struct isis_dynhn *dyn;
+
+  if (!sysid)
+    return "nullsysid";
+
+  /* For our system ID return our host name */
+  if (memcmp(sysid, isis->sysid, ISIS_SYS_ID_LEN) == 0)
+    return unix_hostname();
+
+  dyn = dynhn_find_by_id (sysid);
+  if (dyn)
+    return (const char *)dyn->name.name;
+
+  return sysid_print (sysid);
+}
+
+/*
+ * This function is a generic utility that logs data of given length.
+ * Move this to a shared lib so that any protocol can use it.
+ */
+void
+zlog_dump_data (void *data, int len)
+{
+  int i;
+  unsigned char *p;
+  unsigned char c;
+  char bytestr[4];
+  char addrstr[10];
+  char hexstr[ 16*3 + 5];
+  char charstr[16*1 + 5];
+
+  p = data;
+  memset (bytestr, 0, sizeof(bytestr));
+  memset (addrstr, 0, sizeof(addrstr));
+  memset (hexstr, 0, sizeof(hexstr));
+  memset (charstr, 0, sizeof(charstr));
+
+  for (i = 1; i <= len; i++)
+  {
+    c = *p;
+    if (isalnum (c) == 0)
+      c = '.';
+
+    /* store address for this line */
+    if ((i % 16) == 1)
+      snprintf (addrstr, sizeof(addrstr), "%p", p);
+
+    /* store hex str (for left side) */
+    snprintf (bytestr, sizeof (bytestr), "%02X ", *p);
+    strncat (hexstr, bytestr, sizeof (hexstr) - strlen (hexstr) - 1);
+
+    /* store char str (for right side) */
+    snprintf (bytestr, sizeof (bytestr), "%c", c);
+    strncat (charstr, bytestr, sizeof (charstr) - strlen (charstr) - 1);
+
+    if ((i % 16) == 0)
+    {
+      /* line completed */
+      zlog_debug ("[%8.8s]   %-50.50s  %s", addrstr, hexstr, charstr);
+      hexstr[0] = 0;
+      charstr[0] = 0;
+    }
+    else if ((i % 8) == 0)
+    {
+      /* half line: add whitespaces */
+      strncat (hexstr, "  ", sizeof (hexstr) - strlen (hexstr) - 1);
+      strncat (charstr, " ", sizeof (charstr) - strlen (charstr) - 1);
+    }
+    p++; /* next byte */
+  }
+
+  /* print rest of buffer if not empty */
+  if (strlen (hexstr) > 0)
+    zlog_debug ("[%8.8s]   %-50.50s  %s", addrstr, hexstr, charstr);
+  return;
+}
diff --git a/isisd/isis_misc.h b/isisd/isis_misc.h
index d5003a8..0cd65a6 100644
--- a/isisd/isis_misc.h
+++ b/isisd/isis_misc.h
@@ -24,8 +24,10 @@
 #ifndef _ZEBRA_ISIS_MISC_H
 #define _ZEBRA_ISIS_MISC_H
 
-int string2circuit_t (const u_char *);
+int string2circuit_t (const char *);
 const char *circuit_t2string (int);
+const char *circuit_state2string (int state);
+const char *circuit_type2string (int type);
 const char *syst2string (int);
 struct in_addr newprefix2inaddr (u_char * prefix_start,
 				 u_char prefix_masklen);
@@ -33,8 +35,8 @@
  * Converting input to memory stored format
  * return value of 0 indicates wrong input
  */
-int dotformat2buff (u_char *, const u_char *);
-int sysid2buff (u_char *, const u_char *);
+int dotformat2buff (u_char *, const char *);
+int sysid2buff (u_char *, const char *);
 
 /*
  * Printing functions
@@ -46,6 +48,8 @@
 const char *time2string (u_int32_t);
 /* typedef struct nlpids nlpids; */
 char *nlpid2string (struct nlpids *);
+const char *print_sys_hostname (u_char *sysid);
+void zlog_dump_data (void *data, int len);
 
 /*
  * misc functions
@@ -57,7 +61,8 @@
 /*
  * macros
  */
-#define GETSYSID(A,L) (A->area_addr + (A->addr_len - (L + 1)))
+#define GETSYSID(A) (A->area_addr + (A->addr_len - \
+                                     (ISIS_SYS_ID_LEN + ISIS_NSEL_LEN)))
 
 /* used for calculating nice string representation instead of plain seconds */
 
diff --git a/isisd/isis_pdu.c b/isisd/isis_pdu.c
index d67df31..497fad2 100644
--- a/isisd/isis_pdu.c
+++ b/isisd/isis_pdu.c
@@ -39,12 +39,12 @@
 #include "isisd/include-netbsd/iso.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
+#include "isisd/isis_flags.h"
 #include "isisd/isis_adjacency.h"
 #include "isisd/isis_circuit.h"
 #include "isisd/isis_network.h"
 #include "isisd/isis_misc.h"
 #include "isisd/isis_dr.h"
-#include "isisd/isis_flags.h"
 #include "isisd/isis_tlv.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_dynhn.h"
@@ -54,9 +54,6 @@
 #include "isisd/isis_csm.h"
 #include "isisd/isis_events.h"
 
-extern struct thread_master *master;
-extern struct isis *isis;
-
 #define ISIS_MINIMUM_FIXED_HDR_LEN 15
 #define ISIS_MIN_PDU_LEN           13	/* partial seqnum pdu with id_len=2 */
 
@@ -65,7 +62,7 @@
 #endif /* PNBBY */
 
 /* Utility mask array. */
-static const u_char maskbit[] = {
+static u_char maskbit[] = {
   0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff
 };
 
@@ -169,44 +166,152 @@
   return retval;
 }
 
-
 /*
  * Verify authentication information
  * Support cleartext and HMAC MD5 authentication
  */
-int
-authentication_check (struct isis_passwd *remote, struct isis_passwd *local, struct isis_circuit* c)
+static int
+authentication_check (struct isis_passwd *remote, struct isis_passwd *local,
+                      struct stream *stream, uint32_t auth_tlv_offset)
 {
   unsigned char digest[ISIS_AUTH_MD5_SIZE];
 
-  if (c->passwd.type)
-    {
-      switch (c->passwd.type)
-    {
-	  case ISIS_PASSWD_TYPE_HMAC_MD5:
-	    /* HMAC MD5 (RFC 3567) */
-	    /* MD5 computation according to RFC 2104 */
-	    hmac_md5(c->rcv_stream->data, stream_get_endp(c->rcv_stream), (unsigned char *) &(local->passwd), c->passwd.len, (unsigned char *) &digest);
-	    return memcmp (digest, remote->passwd, ISIS_AUTH_MD5_SIZE);
-	    break;
+  /* Auth fail () - passwd type mismatch */
+  if (local->type != remote->type)
+    return ISIS_ERROR;
+
+  switch (local->type)
+  {
+    /* No authentication required */
+    case ISIS_PASSWD_TYPE_UNUSED:
+      break;
+
+    /* Cleartext (ISO 10589) */
     case ISIS_PASSWD_TYPE_CLEARTXT:
-	    /* Cleartext (ISO 10589) */
-	    if (local->len != remote->len)
-	return 1;		/* Auth fail () - passwd len mismatch */
-	    return memcmp (local->passwd, remote->passwd, local->len);
-      break;
+      /* Auth fail () - passwd len mismatch */
+      if (remote->len != local->len)
+        return ISIS_ERROR;
+      return memcmp (local->passwd, remote->passwd, local->len);
+
+    /* HMAC MD5 (RFC 3567) */
+    case ISIS_PASSWD_TYPE_HMAC_MD5:
+      /* Auth fail () - passwd len mismatch */
+      if (remote->len != ISIS_AUTH_MD5_SIZE)
+        return ISIS_ERROR;
+      /* Set the authentication value to 0 before the check */
+      memset (STREAM_DATA (stream) + auth_tlv_offset + 3, 0,
+              ISIS_AUTH_MD5_SIZE);
+      /* Compute the digest */
+      hmac_md5 (STREAM_DATA (stream), stream_get_endp (stream),
+                (unsigned char *) &(local->passwd), local->len,
+                (caddr_t) &digest);
+      /* Copy back the authentication value after the check */
+      memcpy (STREAM_DATA (stream) + auth_tlv_offset + 3,
+              remote->passwd, ISIS_AUTH_MD5_SIZE);
+      return memcmp (digest, remote->passwd, ISIS_AUTH_MD5_SIZE);
+
     default:
-      zlog_warn ("Unsupported authentication type");
-      break;
+      zlog_err ("Unsupported authentication type");
+      return ISIS_ERROR;
+  }
+
+  /* Authentication pass when no authentication is configured */
+  return ISIS_OK;
+}
+
+static int
+lsp_authentication_check (struct stream *stream, struct isis_area *area,
+                          int level, struct isis_passwd *passwd)
+{
+  struct isis_link_state_hdr *hdr;
+  uint32_t expected = 0, found = 0, auth_tlv_offset = 0;
+  uint16_t checksum, rem_lifetime, pdu_len;
+  struct tlvs tlvs;
+  int retval = ISIS_OK;
+
+  hdr = (struct isis_link_state_hdr *) (STREAM_PNT (stream));
+  pdu_len = ntohs (hdr->pdu_len);
+  expected |= TLVFLAG_AUTH_INFO;
+  auth_tlv_offset = stream_get_getp (stream) + ISIS_LSP_HDR_LEN;
+  retval = parse_tlvs (area->area_tag, STREAM_PNT (stream) + ISIS_LSP_HDR_LEN,
+                       pdu_len - ISIS_FIXED_HDR_LEN - ISIS_LSP_HDR_LEN,
+                       &expected, &found, &tlvs, &auth_tlv_offset);
+
+  if (retval != ISIS_OK)
+    {
+      zlog_err ("ISIS-Upd (%s): Parse failed L%d LSP %s, seq 0x%08x, "
+                "cksum 0x%04x, lifetime %us, len %u",
+                area->area_tag, level, rawlspid_print (hdr->lsp_id),
+                ntohl (hdr->seq_num), ntohs (hdr->checksum),
+                ntohs (hdr->rem_lifetime), pdu_len);
+      if ((isis->debugs & DEBUG_UPDATE_PACKETS) &&
+          (isis->debugs & DEBUG_PACKET_DUMP))
+        zlog_dump_data (STREAM_DATA (stream), stream_get_endp (stream));
+      return retval;
     }
+
+  if (!(found & TLVFLAG_AUTH_INFO))
+    {
+      zlog_err ("No authentication tlv in LSP");
+      return ISIS_ERROR;
     }
-  return 0; /* Authentication pass when no authentication is configured */
+
+  if (tlvs.auth_info.type != ISIS_PASSWD_TYPE_CLEARTXT &&
+      tlvs.auth_info.type != ISIS_PASSWD_TYPE_HMAC_MD5)
+    {
+      zlog_err ("Unknown authentication type in LSP");
+      return ISIS_ERROR;
+    }
+
+  /*
+   * RFC 5304 set checksum and remaining lifetime to zero before
+   * verification and reset to old values after verification.
+   */
+  checksum = hdr->checksum;
+  rem_lifetime = hdr->rem_lifetime;
+  hdr->checksum = 0;
+  hdr->rem_lifetime = 0;
+  retval = authentication_check (&tlvs.auth_info, passwd, stream,
+                                 auth_tlv_offset);
+  hdr->checksum = checksum;
+  hdr->rem_lifetime = rem_lifetime;
+
+  return retval;
 }
 
 /*
  * Processing helper functions
  */
 static void
+del_addr (void *val)
+{
+  XFREE (MTYPE_ISIS_TMP, val);
+}
+
+static void
+tlvs_to_adj_area_addrs (struct tlvs *tlvs, struct isis_adjacency *adj)
+{
+  struct listnode *node;
+  struct area_addr *area_addr, *malloced;
+
+  if (adj->area_addrs)
+    {
+      adj->area_addrs->del = del_addr;
+      list_delete (adj->area_addrs);
+    }
+  adj->area_addrs = list_new ();
+  if (tlvs->area_addrs)
+    {
+      for (ALL_LIST_ELEMENTS_RO (tlvs->area_addrs, node, area_addr))
+      {
+	malloced = XMALLOC (MTYPE_ISIS_TMP, sizeof (struct area_addr));
+	memcpy (malloced, area_addr, sizeof (struct area_addr));
+	listnode_add (adj->area_addrs, malloced);
+      }
+    }
+}
+
+static void
 tlvs_to_adj_nlpids (struct tlvs *tlvs, struct isis_adjacency *adj)
 {
   int i;
@@ -227,12 +332,6 @@
 }
 
 static void
-del_ip_addr (void *val)
-{
-  XFREE (MTYPE_ISIS_TMP, val);
-}
-
-static void
 tlvs_to_adj_ipv4_addrs (struct tlvs *tlvs, struct isis_adjacency *adj)
 {
   struct listnode *node;
@@ -240,7 +339,7 @@
 
   if (adj->ipv4_addrs)
     {
-      adj->ipv4_addrs->del = del_ip_addr;
+      adj->ipv4_addrs->del = del_addr;
       list_delete (adj->ipv4_addrs);
     }
   adj->ipv4_addrs = list_new ();
@@ -264,7 +363,7 @@
 
   if (adj->ipv6_addrs)
     {
-      adj->ipv6_addrs->del = del_ip_addr;
+      adj->ipv6_addrs->del = del_addr;
       list_delete (adj->ipv6_addrs);
     }
   adj->ipv6_addrs = list_new ();
@@ -297,9 +396,26 @@
   int retval = ISIS_OK;
   struct isis_p2p_hello_hdr *hdr;
   struct isis_adjacency *adj;
-  u_int32_t expected = 0, found;
+  u_int32_t expected = 0, found = 0, auth_tlv_offset = 0;
+  uint16_t pdu_len;
   struct tlvs tlvs;
 
+  if (isis->debugs & DEBUG_ADJ_PACKETS)
+    {
+      zlog_debug ("ISIS-Adj (%s): Rcvd P2P IIH on %s, cirType %s, cirID %u",
+                  circuit->area->area_tag, circuit->interface->name,
+                  circuit_t2string (circuit->is_type), circuit->circuit_id);
+      if (isis->debugs & DEBUG_PACKET_DUMP)
+        zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
+                        stream_get_endp (circuit->rcv_stream));
+    }
+
+  if (circuit->circ_type != CIRCUIT_T_P2P)
+    {
+      zlog_warn ("p2p hello on non p2p circuit");
+      return ISIS_WARNING;
+    }
+
   if ((stream_get_endp (circuit->rcv_stream) -
        stream_get_getp (circuit->rcv_stream)) < ISIS_P2PHELLO_HDRLEN)
     {
@@ -324,42 +440,25 @@
    * Get the header
    */
   hdr = (struct isis_p2p_hello_hdr *) STREAM_PNT (circuit->rcv_stream);
-  circuit->rcv_stream->getp += ISIS_P2PHELLO_HDRLEN;
+  pdu_len = ntohs (hdr->pdu_len);
 
-  /*  hdr.circuit_t = stream_getc (stream);
-     stream_get (hdr.source_id, stream, ISIS_SYS_ID_LEN);
-     hdr.hold_time = stream_getw (stream);
-     hdr.pdu_len   = stream_getw (stream);
-     hdr.local_id  = stream_getc (stream); */
+  if (pdu_len > ISO_MTU(circuit) ||
+      pdu_len > stream_get_endp (circuit->rcv_stream))
+    {
+      zlog_warn ("ISIS-Adj (%s): Rcvd P2P IIH from (%s) with "
+                 "invalid pdu length %d",
+                 circuit->area->area_tag, circuit->interface->name, pdu_len);
+      return ISIS_WARNING;
+    }
 
   /*
-   * My interpertation of the ISO, if no adj exists we will create one for 
-   * the circuit
+   * Set the stream endp to PDU length, ignoring additional padding
+   * introduced by transport chips.
    */
+  if (pdu_len < stream_get_endp (circuit->rcv_stream))
+    stream_set_endp (circuit->rcv_stream, pdu_len);
 
-  if (isis->debugs & DEBUG_ADJ_PACKETS)
-    {
-      zlog_debug ("ISIS-Adj (%s): Rcvd P2P IIH from (%s), cir type %s,"
-		  " cir id %02d, length %d",
-		  circuit->area->area_tag, circuit->interface->name,
-		  circuit_t2string (circuit->circuit_is_type),
-		  circuit->circuit_id, ntohs (hdr->pdu_len));
-    }
-
-  adj = circuit->u.p2p.neighbor;
-  if (!adj)
-    {
-      adj = isis_new_adj (hdr->source_id, NULL, 0, circuit);
-      if (adj == NULL)
-	return ISIS_ERROR;
-      circuit->u.p2p.neighbor = adj;
-      isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
-      adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
-    }
-
-  /* 8.2.6 Monitoring point-to-point adjacencies */
-  adj->hold_time = ntohs (hdr->hold_time);
-  adj->last_upd = time (NULL);
+  stream_forward_getp (circuit->rcv_stream, ISIS_P2PHELLO_HDRLEN);
 
   /*
    * Lets get the TLVS now
@@ -370,37 +469,93 @@
   expected |= TLVFLAG_IPV4_ADDR;
   expected |= TLVFLAG_IPV6_ADDR;
 
+  auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
   retval = parse_tlvs (circuit->area->area_tag,
 		       STREAM_PNT (circuit->rcv_stream),
-		       ntohs (hdr->pdu_len) - ISIS_P2PHELLO_HDRLEN
-		       - ISIS_FIXED_HDR_LEN, &expected, &found, &tlvs);
+		       pdu_len - ISIS_P2PHELLO_HDRLEN - ISIS_FIXED_HDR_LEN,
+                       &expected, &found, &tlvs, &auth_tlv_offset);
 
   if (retval > ISIS_WARNING)
     {
+      zlog_warn ("parse_tlvs() failed");
       free_tlvs (&tlvs);
       return retval;
     };
 
+  if (!(found & TLVFLAG_AREA_ADDRS))
+    {
+      zlog_warn ("No Area addresses TLV in P2P IS to IS hello");
+      free_tlvs (&tlvs);
+      return ISIS_WARNING;
+    }
+
   /* 8.2.5.1 c) Authentication */
   if (circuit->passwd.type)
     {
       if (!(found & TLVFLAG_AUTH_INFO) ||
-	  authentication_check (&tlvs.auth_info, &circuit->passwd, circuit))
-	{
-	  isis_event_auth_failure (circuit->area->area_tag,
-				   "P2P hello authentication failure",
-				   hdr->source_id);
-	  return ISIS_OK;
-	}
+          authentication_check (&tlvs.auth_info, &circuit->passwd,
+                                circuit->rcv_stream, auth_tlv_offset))
+        {
+          isis_event_auth_failure (circuit->area->area_tag,
+                                   "P2P hello authentication failure",
+                                   hdr->source_id);
+          free_tlvs (&tlvs);
+          return ISIS_OK;
+        }
     }
 
+  /*
+   * check if it's own interface ip match iih ip addrs
+   */
+  if ((found & TLVFLAG_IPV4_ADDR) == 0 ||
+      ip_match (circuit->ip_addrs, tlvs.ipv4_addrs) == 0)
+    {
+      zlog_warn ("ISIS-Adj: No usable IP interface addresses "
+                 "in LAN IIH from %s\n", circuit->interface->name);
+      free_tlvs (&tlvs);
+      return ISIS_WARNING;
+    }
+
+  /*
+   * My interpertation of the ISO, if no adj exists we will create one for
+   * the circuit
+   */
+  adj = circuit->u.p2p.neighbor;
+  if (!adj || adj->level != hdr->circuit_t)
+    {
+      if (!adj)
+        {
+          adj = isis_new_adj (hdr->source_id, NULL, hdr->circuit_t, circuit);
+          if (adj == NULL)
+            return ISIS_ERROR;
+        }
+      else
+        {
+          adj->level = hdr->circuit_t;
+        }
+      circuit->u.p2p.neighbor = adj;
+      isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
+      adj->sys_type = ISIS_SYSTYPE_UNKNOWN;
+    }
+
+  /* 8.2.6 Monitoring point-to-point adjacencies */
+  adj->hold_time = ntohs (hdr->hold_time);
+  adj->last_upd = time (NULL);
+
   /* we do this now because the adj may not survive till the end... */
+  tlvs_to_adj_area_addrs (&tlvs, adj);
+
+  /* which protocol are spoken ??? */
+  if (found & TLVFLAG_NLPID)
+    tlvs_to_adj_nlpids (&tlvs, adj);
 
   /* we need to copy addresses to the adj */
-  tlvs_to_adj_ipv4_addrs (&tlvs, adj);
+  if (found & TLVFLAG_IPV4_ADDR)
+    tlvs_to_adj_ipv4_addrs (&tlvs, adj);
 
 #ifdef HAVE_IPV6
-  tlvs_to_adj_ipv6_addrs (&tlvs, adj);
+  if (found & TLVFLAG_IPV6_ADDR)
+    tlvs_to_adj_ipv6_addrs (&tlvs, adj);
 #endif /* HAVE_IPV6 */
 
   /* lets take care of the expiry */
@@ -435,6 +590,7 @@
 		{
 		  /* (7) reject - wrong system type event */
 		  zlog_warn ("wrongSystemType");
+                  free_tlvs (&tlvs);
 		  return ISIS_WARNING;	/* Reject */
 		}
 	      else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
@@ -521,6 +677,7 @@
 		{
 		  /* (5) reject - wrong system type event */
 		  zlog_warn ("wrongSystemType");
+                  free_tlvs (&tlvs);
 		  return ISIS_WARNING;	/* Reject */
 		}
 	      else if ((adj->adj_usage == ISIS_ADJ_LEVEL1AND2) ||
@@ -553,7 +710,7 @@
 	}
     }
   /* 8.2.5.2 b) if no match was detected */
-  else
+  else if (listcount (circuit->area->area_addrs) > 0)
     {
       if (circuit->area->is_type == IS_LEVEL_1)
 	{
@@ -579,6 +736,7 @@
 		{
 		  /* (6) reject - Area Mismatch event */
 		  zlog_warn ("AreaMismatch");
+                  free_tlvs (&tlvs);
 		  return ISIS_WARNING;	/* Reject */
 		}
 	      else if (adj->adj_usage == ISIS_ADJ_LEVEL1)
@@ -631,6 +789,11 @@
 	    }
 	}
     }
+  else
+    {
+      /* down - area mismatch */
+      isis_adj_state_change (adj, ISIS_ADJ_DOWN, "Area Mismatch");
+    }
   /* 8.2.5.2 c) if the action was up - comparing circuit IDs */
   /* FIXME - Missing parts */
 
@@ -654,7 +817,15 @@
     }
 
   adj->circuit_t = hdr->circuit_t;
-  adj->level = hdr->circuit_t;
+
+  if (isis->debugs & DEBUG_ADJ_PACKETS)
+    {
+      zlog_debug ("ISIS-Adj (%s): Rcvd P2P IIH from (%s), cir type %s,"
+		  " cir id %02d, length %d",
+		  circuit->area->area_tag, circuit->interface->name,
+		  circuit_t2string (circuit->is_type),
+		  circuit->circuit_id, pdu_len);
+    }
 
   free_tlvs (&tlvs);
 
@@ -670,11 +841,28 @@
   int retval = ISIS_OK;
   struct isis_lan_hello_hdr hdr;
   struct isis_adjacency *adj;
-  u_int32_t expected = 0, found;
+  u_int32_t expected = 0, found = 0, auth_tlv_offset = 0;
   struct tlvs tlvs;
   u_char *snpa;
   struct listnode *node;
 
+  if (isis->debugs & DEBUG_ADJ_PACKETS)
+    {
+      zlog_debug ("ISIS-Adj (%s): Rcvd L%d LAN IIH on %s, cirType %s, "
+                  "cirID %u",
+                  circuit->area->area_tag, level, circuit->interface->name,
+                  circuit_t2string (circuit->is_type), circuit->circuit_id);
+      if (isis->debugs & DEBUG_PACKET_DUMP)
+        zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
+                        stream_get_endp (circuit->rcv_stream));
+    }
+
+  if (circuit->circ_type != CIRCUIT_T_BROADCAST)
+    {
+      zlog_warn ("lan hello on non broadcast circuit");
+      return ISIS_WARNING;
+    }
+
   if ((stream_get_endp (circuit->rcv_stream) -
        stream_get_getp (circuit->rcv_stream)) < ISIS_LANHELLO_HDRLEN)
     {
@@ -689,7 +877,7 @@
       return ISIS_WARNING;
     }
 
-  if (!accept_level (level, circuit->circuit_is_type))
+  if (!accept_level (level, circuit->is_type))
     {
       if (isis->debugs & DEBUG_ADJ_PACKETS)
 	{
@@ -721,13 +909,33 @@
   hdr.prio = stream_getc (circuit->rcv_stream);
   stream_get (hdr.lan_id, circuit->rcv_stream, ISIS_SYS_ID_LEN + 1);
 
-  if (hdr.circuit_t != IS_LEVEL_1 && hdr.circuit_t != IS_LEVEL_2 &&
-      hdr.circuit_t != IS_LEVEL_1_AND_2)
+  if (hdr.pdu_len > ISO_MTU(circuit) ||
+      hdr.pdu_len > stream_get_endp (circuit->rcv_stream))
     {
-      zlog_warn ("Level %d LAN Hello with Circuit Type %d", level,
-		 hdr.circuit_t);
+      zlog_warn ("ISIS-Adj (%s): Rcvd LAN IIH from (%s) with "
+                 "invalid pdu length %d",
+                 circuit->area->area_tag, circuit->interface->name,
+                 hdr.pdu_len);
+      return ISIS_WARNING;
+    }
+
+  /*
+   * Set the stream endp to PDU length, ignoring additional padding
+   * introduced by transport chips.
+   */
+  if (hdr.pdu_len < stream_get_endp (circuit->rcv_stream))
+    stream_set_endp (circuit->rcv_stream, hdr.pdu_len);
+
+  if (hdr.circuit_t != IS_LEVEL_1 &&
+      hdr.circuit_t != IS_LEVEL_2 &&
+      hdr.circuit_t != IS_LEVEL_1_AND_2 &&
+      (level & hdr.circuit_t) == 0)
+    {
+      zlog_err ("Level %d LAN Hello with Circuit Type %d", level,
+                hdr.circuit_t);
       return ISIS_ERROR;
     }
+
   /*
    * Then get the tlvs
    */
@@ -738,10 +946,12 @@
   expected |= TLVFLAG_IPV4_ADDR;
   expected |= TLVFLAG_IPV6_ADDR;
 
+  auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
   retval = parse_tlvs (circuit->area->area_tag,
-		       STREAM_PNT (circuit->rcv_stream),
-		       hdr.pdu_len - ISIS_LANHELLO_HDRLEN -
-		       ISIS_FIXED_HDR_LEN, &expected, &found, &tlvs);
+                       STREAM_PNT (circuit->rcv_stream),
+                       hdr.pdu_len - ISIS_LANHELLO_HDRLEN - ISIS_FIXED_HDR_LEN,
+                       &expected, &found, &tlvs,
+                       &auth_tlv_offset);
 
   if (retval > ISIS_WARNING)
     {
@@ -761,21 +971,24 @@
   if (circuit->passwd.type)
     {
       if (!(found & TLVFLAG_AUTH_INFO) ||
-	   authentication_check (&tlvs.auth_info, &circuit->passwd, circuit))
-	{
-	  isis_event_auth_failure (circuit->area->area_tag,
-				   "LAN hello authentication failure",
-				   hdr.source_id);
-	  retval = ISIS_WARNING;
-	  goto out;
-	}
+          authentication_check (&tlvs.auth_info, &circuit->passwd,
+                                circuit->rcv_stream, auth_tlv_offset))
+        {
+          isis_event_auth_failure (circuit->area->area_tag,
+                                   "LAN hello authentication failure",
+                                   hdr.source_id);
+          retval = ISIS_WARNING;
+          goto out;
+        }
     }
 
   /*
    * Accept the level 1 adjacency only if a match between local and
    * remote area addresses is found
    */
-  if (level == 1 && !area_match (circuit->area->area_addrs, tlvs.area_addrs))
+  if (listcount (circuit->area->area_addrs) == 0 ||
+      (level == IS_LEVEL_1 &&
+       area_match (circuit->area->area_addrs, tlvs.area_addrs) == 0))
     {
       if (isis->debugs & DEBUG_ADJ_PACKETS)
 	{
@@ -802,43 +1015,49 @@
   /*
    * check if it's own interface ip match iih ip addrs
    */
-  if (!(found & TLVFLAG_IPV4_ADDR)
-      || !ip_match (circuit->ip_addrs, tlvs.ipv4_addrs))
+  if ((found & TLVFLAG_IPV4_ADDR) == 0 ||
+      ip_match (circuit->ip_addrs, tlvs.ipv4_addrs) == 0)
     {
-      zlog_debug
-	("ISIS-Adj: No usable IP interface addresses in LAN IIH from %s\n",
-	 circuit->interface->name);
+      zlog_debug ("ISIS-Adj: No usable IP interface addresses "
+                  "in LAN IIH from %s\n", circuit->interface->name);
       retval = ISIS_WARNING;
       goto out;
     }
 
   adj = isis_adj_lookup (hdr.source_id, circuit->u.bc.adjdb[level - 1]);
-  if (!adj)
+  if ((adj == NULL) || (memcmp(adj->snpa, ssnpa, ETH_ALEN)) ||
+      (adj->level != level))
     {
-      /*
-       * Do as in 8.4.2.5
-       */
-      adj = isis_new_adj (hdr.source_id, ssnpa, level, circuit);
-      if (adj == NULL)
-	{
-	  retval = ISIS_ERROR;
-	  goto out;
-	}
-
-      adj->level = level;
+      if (!adj)
+        {
+          /*
+           * Do as in 8.4.2.5
+           */
+          adj = isis_new_adj (hdr.source_id, ssnpa, level, circuit);
+          if (adj == NULL)
+            {
+              retval = ISIS_ERROR;
+              goto out;
+            }
+        }
+      else
+        {
+          if (ssnpa) {
+            memcpy (adj->snpa, ssnpa, 6);
+          } else {
+            memset (adj->snpa, ' ', 6);
+          }
+          adj->level = level;
+        }
       isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING, NULL);
 
-      if (level == 1)
-	{
-	  adj->sys_type = ISIS_SYSTYPE_L1_IS;
-	}
+      if (level == IS_LEVEL_1)
+          adj->sys_type = ISIS_SYSTYPE_L1_IS;
       else
-	{
-	  adj->sys_type = ISIS_SYSTYPE_L2_IS;
-	}
+          adj->sys_type = ISIS_SYSTYPE_L2_IS;
       list_delete_all_node (circuit->u.bc.lan_neighs[level - 1]);
       isis_adj_build_neigh_list (circuit->u.bc.adjdb[level - 1],
-				 circuit->u.bc.lan_neighs[level - 1]);
+                                 circuit->u.bc.lan_neighs[level - 1]);
     }
 
   if(adj->dis_record[level-1].dis==ISIS_IS_DIS)
@@ -847,7 +1066,7 @@
       case 1:
 	if (memcmp (circuit->u.bc.l1_desig_is, hdr.lan_id, ISIS_SYS_ID_LEN + 1))
 	  {
-	    thread_add_event (master, isis_event_dis_status_change, circuit, 0);
+            thread_add_event (master, isis_event_dis_status_change, circuit, 0);
 	    memcpy (&circuit->u.bc.l1_desig_is, hdr.lan_id,
 		    ISIS_SYS_ID_LEN + 1);
 	  }
@@ -855,7 +1074,7 @@
       case 2:
 	if (memcmp (circuit->u.bc.l2_desig_is, hdr.lan_id, ISIS_SYS_ID_LEN + 1))
 	  {
-	    thread_add_event (master, isis_event_dis_status_change, circuit, 0);
+            thread_add_event (master, isis_event_dis_status_change, circuit, 0);
 	    memcpy (&circuit->u.bc.l2_desig_is, hdr.lan_id,
 		    ISIS_SYS_ID_LEN + 1);
 	  }
@@ -868,6 +1087,8 @@
 
   memcpy (adj->lanid, hdr.lan_id, ISIS_SYS_ID_LEN + 1);
 
+  tlvs_to_adj_area_addrs (&tlvs, adj);
+
   /* which protocol are spoken ??? */
   if (found & TLVFLAG_NLPID)
     tlvs_to_adj_nlpids (&tlvs, adj);
@@ -886,7 +1107,7 @@
   /* lets take care of the expiry */
   THREAD_TIMER_OFF (adj->t_expire);
   THREAD_TIMER_ON (master, adj->t_expire, isis_adj_expire, adj,
-		   (long) adj->hold_time);
+                   (long) adj->hold_time);
 
   /*
    * If the snpa for this circuit is found from LAN Neighbours TLV
@@ -894,31 +1115,48 @@
    */
 
   if (found & TLVFLAG_LAN_NEIGHS)
+  {
+    if (adj->adj_state != ISIS_ADJ_UP)
     {
-      if (adj->adj_state != ISIS_ADJ_UP)
-	{
-	  for (ALL_LIST_ELEMENTS_RO (tlvs.lan_neighs, node, snpa))
-	    if (!memcmp (snpa, circuit->u.bc.snpa, ETH_ALEN))
-	    {
-	      isis_adj_state_change (adj, ISIS_ADJ_UP,
-				     "own SNPA found in LAN Neighbours TLV");
-	    }
-	}
+      for (ALL_LIST_ELEMENTS_RO (tlvs.lan_neighs, node, snpa))
+      {
+        if (!memcmp (snpa, circuit->u.bc.snpa, ETH_ALEN))
+        {
+          isis_adj_state_change (adj, ISIS_ADJ_UP,
+                                 "own SNPA found in LAN Neighbours TLV");
+        }
+      }
     }
+    else
+    {
+      int found = 0;
+      for (ALL_LIST_ELEMENTS_RO (tlvs.lan_neighs, node, snpa))
+        if (!memcmp (snpa, circuit->u.bc.snpa, ETH_ALEN))
+        {
+          found = 1;
+          break;
+        }
+      if (found == 0)
+        isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING,
+                               "own SNPA not found in LAN Neighbours TLV");
+    }
+  }
+  else if (adj->adj_state == ISIS_ADJ_UP)
+  {
+    isis_adj_state_change (adj, ISIS_ADJ_INITIALIZING,
+                           "no LAN Neighbours TLV found");
+  }
 
 out:
-  /* DEBUG_ADJ_PACKETS */
   if (isis->debugs & DEBUG_ADJ_PACKETS)
     {
-      /* FIXME: is this place right? fix missing info */
       zlog_debug ("ISIS-Adj (%s): Rcvd L%d LAN IIH from %s on %s, cirType %s, "
 		  "cirID %u, length %ld",
 		  circuit->area->area_tag,
 		  level, snpa_print (ssnpa), circuit->interface->name,
-		  circuit_t2string (circuit->circuit_is_type),
+		  circuit_t2string (circuit->is_type),
 		  circuit->circuit_id,
-		  /* FIXME: use %z when we stop supporting old compilers. */
-		  (unsigned long) stream_get_endp (circuit->rcv_stream));
+		  stream_get_endp (circuit->rcv_stream));
     }
 
   free_tlvs (&tlvs);
@@ -940,8 +1178,18 @@
   int retval = ISIS_OK, comp = 0;
   u_char lspid[ISIS_SYS_ID_LEN + 2];
   struct isis_passwd *passwd;
+  uint16_t pdu_len;
 
-  /* Sanity check - FIXME: move to correct place */
+  if (isis->debugs & DEBUG_UPDATE_PACKETS)
+    {
+      zlog_debug ("ISIS-Upd (%s): Rcvd L%d LSP on %s, cirType %s, cirID %u",
+                  circuit->area->area_tag, level, circuit->interface->name,
+                  circuit_t2string (circuit->is_type), circuit->circuit_id);
+      if (isis->debugs & DEBUG_PACKET_DUMP)
+        zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
+                        stream_get_endp (circuit->rcv_stream));
+    }
+
   if ((stream_get_endp (circuit->rcv_stream) -
        stream_get_getp (circuit->rcv_stream)) < ISIS_LSP_HDR_LEN)
     {
@@ -951,28 +1199,56 @@
 
   /* Reference the header   */
   hdr = (struct isis_link_state_hdr *) STREAM_PNT (circuit->rcv_stream);
+  pdu_len = ntohs (hdr->pdu_len);
+
+  /* lsp length check */
+  if (pdu_len < ISIS_LSP_HDR_LEN ||
+      pdu_len > ISO_MTU(circuit) ||
+      pdu_len > stream_get_endp (circuit->rcv_stream))
+    {
+      zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP length %d",
+		  circuit->area->area_tag,
+		  rawlspid_print (hdr->lsp_id), pdu_len);
+
+      return ISIS_WARNING;
+    }
+
+  /*
+   * Set the stream endp to PDU length, ignoring additional padding
+   * introduced by transport chips.
+   */
+  if (pdu_len < stream_get_endp (circuit->rcv_stream))
+    stream_set_endp (circuit->rcv_stream, pdu_len);
 
   if (isis->debugs & DEBUG_UPDATE_PACKETS)
     {
       zlog_debug ("ISIS-Upd (%s): Rcvd L%d LSP %s, seq 0x%08x, cksum 0x%04x, "
-		  "lifetime %us, len %lu, on %s",
+		  "lifetime %us, len %u, on %s",
 		  circuit->area->area_tag,
 		  level,
 		  rawlspid_print (hdr->lsp_id),
 		  ntohl (hdr->seq_num),
 		  ntohs (hdr->checksum),
 		  ntohs (hdr->rem_lifetime),
-		  /* FIXME: use %z when we stop supporting old compilers. */
-		  (unsigned long) stream_get_endp (circuit->rcv_stream), 
+		  pdu_len,
 		  circuit->interface->name);
     }
 
-  assert (ntohs (hdr->pdu_len) > ISIS_LSP_HDR_LEN);
+  /* lsp is_type check */
+  if ((hdr->lsp_bits & IS_LEVEL_1_AND_2) != IS_LEVEL_1 &&
+      (hdr->lsp_bits & IS_LEVEL_1_AND_2) != IS_LEVEL_1_AND_2)
+    {
+      zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP is type %x",
+		  circuit->area->area_tag,
+		  rawlspid_print (hdr->lsp_id), hdr->lsp_bits);
+      /* continue as per RFC1122 Be liberal in what you accept, and
+       * conservative in what you send */
+    }
 
   /* Checksum sanity check - FIXME: move to correct place */
   /* 12 = sysid+pdu+remtime */
   if (iso_csum_verify (STREAM_PNT (circuit->rcv_stream) + 4,
-		       ntohs (hdr->pdu_len) - 12, &hdr->checksum))
+		       pdu_len - 12, &hdr->checksum))
     {
       zlog_debug ("ISIS-Upd (%s): LSP %s invalid LSP checksum 0x%04x",
 		  circuit->area->area_tag,
@@ -993,13 +1269,13 @@
     }
 
   /* 7.3.15.1 a) 2,3 - manualL2OnlyMode not implemented */
-  if (!accept_level (level, circuit->circuit_is_type))
+  if (!accept_level (level, circuit->is_type))
     {
       zlog_debug ("ISIS-Upd (%s): LSP %s received at level %d over circuit of"
 		  " type %s",
 		  circuit->area->area_tag,
 		  rawlspid_print (hdr->lsp_id),
-		  level, circuit_t2string (circuit->circuit_is_type));
+		  level, circuit_t2string (circuit->is_type));
 
       return ISIS_WARNING;
     }
@@ -1009,12 +1285,12 @@
   /* 7.3.15.1 a) 5 - maximum area match, can be ommited since we only use 3 */
 
   /* 7.3.15.1 a) 7 - password check */
-  (level == ISIS_LEVEL1) ? (passwd = &circuit->area->area_passwd) :
-    (passwd = &circuit->area->domain_passwd);
+  (level == IS_LEVEL_1) ? (passwd = &circuit->area->area_passwd) :
+                          (passwd = &circuit->area->domain_passwd);
   if (passwd->type)
     {
-      if (isis_lsp_authinfo_check (circuit->rcv_stream, circuit->area,
-				   ntohs (hdr->pdu_len), passwd))
+      if (lsp_authentication_check (circuit->rcv_stream, circuit->area,
+                                    level, passwd))
 	{
 	  isis_event_auth_failure (circuit->area->area_tag,
 				   "LSP authentication failure", hdr->lsp_id);
@@ -1035,7 +1311,6 @@
 
   /* 7.3.15.1 a) 6 - Must check that we have an adjacency of the same level  */
   /* for broadcast circuits, snpa should be compared */
-  /* FIXME : Point To Point */
 
   if (circuit->circ_type == CIRCUIT_T_BROADCAST)
     {
@@ -1052,7 +1327,6 @@
 	  return ISIS_WARNING;	/* Silently discard */
 	}
     }
-
   /* for non broadcast, we just need to find same level adj */
   else
     {
@@ -1063,13 +1337,15 @@
 	}
       else
 	{
-	  if (((level == 1) &&
+	  if (((level == IS_LEVEL_1) &&
 	       (circuit->u.p2p.neighbor->adj_usage == ISIS_ADJ_LEVEL2)) ||
-	      ((level == 2) &&
+	      ((level == IS_LEVEL_2) &&
 	       (circuit->u.p2p.neighbor->adj_usage == ISIS_ADJ_LEVEL1)))
 	    return ISIS_WARNING;	/* Silently discard */
+	  adj = circuit->u.p2p.neighbor;
 	}
     }
+
 dontcheckadj:
   /* 7.3.15.1 a) 7 - Passwords for level 1 - not implemented  */
 
@@ -1096,10 +1372,9 @@
 	      /* 7.3.16.4 b) 1)  */
 	      if (comp == LSP_NEWER)
 		{
-		  lsp_update (lsp, hdr, circuit->rcv_stream, circuit->area,
-			      level);
+                  lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
 		  /* ii */
-		  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
+                  lsp_set_all_srmflags (lsp);
 		  /* iii */
 		  ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
 		  /* v */
@@ -1123,38 +1398,25 @@
 		  ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
 		}
 	    }
-	  else
-	    {
-	      /* our own LSP -> 7.3.16.4 c) */
-	      if (LSP_PSEUDO_ID (lsp->lsp_header->lsp_id) !=
-		  circuit->circuit_id
-		  || (LSP_PSEUDO_ID (lsp->lsp_header->lsp_id) ==
-		      circuit->circuit_id
-		      && circuit->u.bc.is_dr[level - 1] == 1))
-		{
-		  lsp->lsp_header->seq_num = htonl (ntohl (hdr->seq_num) + 1);
-		  if (isis->debugs & DEBUG_UPDATE_PACKETS)
-		    zlog_debug ("LSP LEN: %d",
-				ntohs (lsp->lsp_header->pdu_len));
-		  fletcher_checksum (STREAM_DATA (lsp->pdu) + 12,
-				   ntohs (lsp->lsp_header->pdu_len) - 12, 12);
-		  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
-		  if (isis->debugs & DEBUG_UPDATE_PACKETS)
-		    zlog_debug ("ISIS-Upd (%s): (1) re-originating LSP %s new "
-				"seq 0x%08x", circuit->area->area_tag,
-				rawlspid_print (hdr->lsp_id),
-				ntohl (lsp->lsp_header->seq_num));
-		  lsp->lsp_header->rem_lifetime =
-		    htons (isis_jitter
-			   (circuit->area->max_lsp_lifetime[level - 1],
-			    MAX_AGE_JITTER));
-		}
-	      else
-		{
-		  /* Got purge for own pseudo-lsp, and we are not DR  */
-		  lsp_purge_dr (lsp->lsp_header->lsp_id, circuit, level);
-		}
-	    }
+          else if (lsp->lsp_header->rem_lifetime != 0)
+            {
+              /* our own LSP -> 7.3.16.4 c) */
+              if (comp == LSP_NEWER)
+                {
+                  lsp_inc_seqnum (lsp, ntohl (hdr->seq_num));
+                  lsp_set_all_srmflags (lsp);
+                }
+              else
+                {
+                  ISIS_SET_FLAG (lsp->SRMflags, circuit);
+                  ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
+                }
+              if (isis->debugs & DEBUG_UPDATE_PACKETS)
+                zlog_debug ("ISIS-Upd (%s): (1) re-originating LSP %s new "
+                            "seq 0x%08x", circuit->area->area_tag,
+                            rawlspid_print (hdr->lsp_id),
+                            ntohl (lsp->lsp_header->seq_num));
+            }
 	}
       return retval;
     }
@@ -1174,25 +1436,19 @@
        * has information that the current sequence number for source S is
        * "greater" than that held by S, ... */
 
-      else if (ntohl (hdr->seq_num) > ntohl (lsp->lsp_header->seq_num))
+      if (ntohl (hdr->seq_num) > ntohl (lsp->lsp_header->seq_num))
 	{
 	  /* 7.3.16.1  */
-	  lsp->lsp_header->seq_num = htonl (ntohl (hdr->seq_num) + 1);
-
-	  fletcher_checksum (STREAM_DATA (lsp->pdu) + 12,
-			   ntohs (lsp->lsp_header->pdu_len) - 12, 12);
-
-	  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
+          lsp_inc_seqnum (lsp, ntohl (hdr->seq_num));
 	  if (isis->debugs & DEBUG_UPDATE_PACKETS)
 	    zlog_debug ("ISIS-Upd (%s): (2) re-originating LSP %s new seq "
 			"0x%08x", circuit->area->area_tag,
 			rawlspid_print (hdr->lsp_id),
 			ntohl (lsp->lsp_header->seq_num));
-	  lsp->lsp_header->rem_lifetime =
-	    htons (isis_jitter
-		   (circuit->area->max_lsp_lifetime[level - 1],
-		    MAX_AGE_JITTER));
 	}
+      /* If the received LSP is older or equal,
+       * resend the LSP which will act as ACK */
+      lsp_set_all_srmflags (lsp);
     }
   else
     {
@@ -1201,22 +1457,6 @@
       /* 7.3.15.1 e) 1) LSP newer than the one in db or no LSP in db */
       if ((!lsp || comp == LSP_NEWER))
 	{
-	  int regenerate = (lsp == NULL);
-	  /* i */
-	  if (lsp)
-	    {
-#ifdef EXTREME_DEBUG
-	      zlog_debug ("level %d number is - %ld", level,
-			  circuit->area->lspdb[level - 1]->dict_nodecount);
-#endif /* EXTREME DEBUG */
-	      lsp_search_and_destroy (hdr->lsp_id,
-				      circuit->area->lspdb[level - 1]);
-	      /* exists, so we overwrite */
-#ifdef EXTREME_DEBUG
-	      zlog_debug ("level %d number is - %ld", level,
-			  circuit->area->lspdb[level - 1]->dict_nodecount);
-#endif /* EXTREME DEBUG */
-	    }
 	  /*
 	   * If this lsp is a frag, need to see if we have zero lsp present
 	   */
@@ -1227,18 +1467,24 @@
 	      lsp0 = lsp_search (lspid, circuit->area->lspdb[level - 1]);
 	      if (!lsp0)
 		{
-		  zlog_debug ("Got lsp frag, while zero lsp not database");
+		  zlog_debug ("Got lsp frag, while zero lsp not in database");
 		  return ISIS_OK;
 		}
 	    }
-	  lsp =
-	    lsp_new_from_stream_ptr (circuit->rcv_stream,
-				     ntohs (hdr->pdu_len), lsp0,
-				     circuit->area);
-	  lsp->level = level;
-	  lsp_insert (lsp, circuit->area->lspdb[level - 1]);
+	  /* i */
+	  if (!lsp)
+            {
+	      lsp = lsp_new_from_stream_ptr (circuit->rcv_stream,
+                                             pdu_len, lsp0,
+                                             circuit->area, level);
+              lsp_insert (lsp, circuit->area->lspdb[level - 1]);
+            }
+          else /* exists, so we overwrite */
+            {
+              lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
+            }
 	  /* ii */
-	  ISIS_FLAGS_SET_ALL (lsp->SRMflags);
+          lsp_set_all_srmflags (lsp);
 	  /* iii */
 	  ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
 
@@ -1246,19 +1492,14 @@
 	  if (circuit->circ_type != CIRCUIT_T_BROADCAST)
 	    ISIS_SET_FLAG (lsp->SSNflags, circuit);
 	  /* FIXME: v) */
-	    if (regenerate && circuit->u.bc.is_dr[level - 1]) {
-	    lsp_l1_pseudo_generate (circuit);
-	  }
 	}
       /* 7.3.15.1 e) 2) LSP equal to the one in db */
       else if (comp == LSP_EQUAL)
 	{
 	  ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
-	  lsp_update (lsp, hdr, circuit->rcv_stream, circuit->area, level);
+	  lsp_update (lsp, circuit->rcv_stream, circuit->area, level);
 	  if (circuit->circ_type != CIRCUIT_T_BROADCAST)
-	    {
-	      ISIS_SET_FLAG (lsp->SSNflags, circuit);
-	    }
+	    ISIS_SET_FLAG (lsp->SSNflags, circuit);
 	}
       /* 7.3.15.1 e) 3) LSP older than the one in db */
       else
@@ -1267,7 +1508,6 @@
 	  ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
 	}
     }
-
   return retval;
 }
 
@@ -1284,11 +1524,11 @@
   int retval = ISIS_OK;
   int cmp, own_lsp;
   char typechar = ' ';
-  int len;
+  uint16_t pdu_len;
   struct isis_adjacency *adj;
   struct isis_complete_seqnum_hdr *chdr = NULL;
   struct isis_partial_seqnum_hdr *phdr = NULL;
-  uint32_t found = 0, expected = 0;
+  uint32_t found = 0, expected = 0, auth_tlv_offset = 0;
   struct isis_lsp *lsp;
   struct lsp_entry *entry;
   struct listnode *node, *nnode;
@@ -1303,12 +1543,14 @@
       typechar = 'C';
       chdr =
 	(struct isis_complete_seqnum_hdr *) STREAM_PNT (circuit->rcv_stream);
-      circuit->rcv_stream->getp += ISIS_CSNP_HDRLEN;
-      len = ntohs (chdr->pdu_len);
-      if (len < ISIS_CSNP_HDRLEN)
+      stream_forward_getp (circuit->rcv_stream, ISIS_CSNP_HDRLEN);
+      pdu_len = ntohs (chdr->pdu_len);
+      if (pdu_len < ISIS_CSNP_HDRLEN ||
+          pdu_len > ISO_MTU(circuit) ||
+          pdu_len > stream_get_endp (circuit->rcv_stream))
 	{
-	  zlog_warn ("Received a CSNP with bogus length!");
-	  return ISIS_OK;
+	  zlog_warn ("Received a CSNP with bogus length %d", pdu_len);
+	  return ISIS_WARNING;
 	}
     }
   else
@@ -1316,15 +1558,24 @@
       typechar = 'P';
       phdr =
 	(struct isis_partial_seqnum_hdr *) STREAM_PNT (circuit->rcv_stream);
-      circuit->rcv_stream->getp += ISIS_PSNP_HDRLEN;
-      len = ntohs (phdr->pdu_len);
-      if (len < ISIS_PSNP_HDRLEN)
+      stream_forward_getp (circuit->rcv_stream, ISIS_PSNP_HDRLEN);
+      pdu_len = ntohs (phdr->pdu_len);
+      if (pdu_len < ISIS_PSNP_HDRLEN ||
+          pdu_len > ISO_MTU(circuit) ||
+          pdu_len > stream_get_endp (circuit->rcv_stream))
 	{
-	  zlog_warn ("Received a CSNP with bogus length!");
-	  return ISIS_OK;
+	  zlog_warn ("Received a CSNP with bogus length %d", pdu_len);
+	  return ISIS_WARNING;
 	}
     }
 
+  /*
+   * Set the stream endp to PDU length, ignoring additional padding
+   * introduced by transport chips.
+   */
+  if (pdu_len < stream_get_endp (circuit->rcv_stream))
+    stream_set_endp (circuit->rcv_stream, pdu_len);
+
   /* 7.3.15.2 a) 1 - external domain circuit will discard snp pdu */
   if (circuit->ext_domain)
     {
@@ -1338,7 +1589,7 @@
     }
 
   /* 7.3.15.2 a) 2,3 - manualL2OnlyMode not implemented */
-  if (!accept_level (level, circuit->circuit_is_type))
+  if (!accept_level (level, circuit->is_type))
     {
 
       zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP on %s, "
@@ -1347,26 +1598,23 @@
 		  level,
 		  typechar,
 		  circuit->interface->name,
-		  circuit_t2string (circuit->circuit_is_type), level);
+		  circuit_t2string (circuit->is_type), level);
 
       return ISIS_OK;
     }
 
   /* 7.3.15.2 a) 4 - not applicable for CSNP  only PSNPs on broadcast */
   if ((snp_type == ISIS_SNP_PSNP_FLAG) &&
-      (circuit->circ_type == CIRCUIT_T_BROADCAST))
+      (circuit->circ_type == CIRCUIT_T_BROADCAST) &&
+      (!circuit->u.bc.is_dr[level - 1]))
     {
-      if (!circuit->u.bc.is_dr[level - 1])
-	{
+      zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s, "
+                  "skipping: we are not the DIS",
+                  circuit->area->area_tag,
+                  level,
+                  typechar, snpa_print (ssnpa), circuit->interface->name);
 
-	  zlog_debug ("ISIS-Snp (%s): Rcvd L%d %cSNP from %s on %s, "
-		      "skipping: we are not the DIS",
-		      circuit->area->area_tag,
-		      level,
-		      typechar, snpa_print (ssnpa), circuit->interface->name);
-
-	  return ISIS_OK;
-	}
+      return ISIS_OK;
     }
 
   /* 7.3.15.2 a) 5 - need to make sure IDLength matches - already checked */
@@ -1396,7 +1644,10 @@
   else
     {
       if (!circuit->u.p2p.neighbor)
-	return ISIS_OK;		/* Silently discard */
+      {
+        zlog_warn ("no p2p neighbor on circuit %s", circuit->interface->name);
+        return ISIS_OK;		/* Silently discard */
+      }
     }
 
   /* 7.3.15.2 a) 8 - Passwords for level 1 - not implemented  */
@@ -1408,10 +1659,12 @@
   /* parse the SNP */
   expected |= TLVFLAG_LSP_ENTRIES;
   expected |= TLVFLAG_AUTH_INFO;
+
+  auth_tlv_offset = stream_get_getp (circuit->rcv_stream);
   retval = parse_tlvs (circuit->area->area_tag,
 		       STREAM_PNT (circuit->rcv_stream),
-		       len - circuit->rcv_stream->getp,
-		       &expected, &found, &tlvs);
+		       pdu_len - stream_get_getp (circuit->rcv_stream),
+		       &expected, &found, &tlvs, &auth_tlv_offset);
 
   if (retval > ISIS_WARNING)
     {
@@ -1420,7 +1673,7 @@
       return retval;
     }
 
-  if (level == 1)
+  if (level == IS_LEVEL_1)
     passwd = &circuit->area->area_passwd;
   else
     passwd = &circuit->area->domain_passwd;
@@ -1428,16 +1681,19 @@
   if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_RECV))
     {
       if (passwd->type)
-	{
-	  if (!(found & TLVFLAG_AUTH_INFO) ||
-	      authentication_check (&tlvs.auth_info, passwd, circuit))
-	    {
-	      isis_event_auth_failure (circuit->area->area_tag,
-				       "SNP authentication" " failure",
-				       phdr ? phdr->source_id : chdr->source_id);
-	      return ISIS_OK;
-	    }
-	}
+        {
+          if (!(found & TLVFLAG_AUTH_INFO) ||
+              authentication_check (&tlvs.auth_info, passwd,
+                                    circuit->rcv_stream, auth_tlv_offset))
+            {
+              isis_event_auth_failure (circuit->area->area_tag,
+                                       "SNP authentication" " failure",
+                                       phdr ? phdr->source_id :
+                                       chdr->source_id);
+              free_tlvs (&tlvs);
+              return ISIS_OK;
+            }
+        }
     }
 
   /* debug isis snp-packets */
@@ -1477,19 +1733,18 @@
 	    /* 7.3.15.2 b) 2) if it equals, clear SRM on p2p */
 	    if (cmp == LSP_EQUAL)
 	      {
-		if (circuit->circ_type != CIRCUIT_T_BROADCAST)
-		  ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
-		/* 7.3.15.2 b) 3) if it is older, clear SSN and set SRM */
+		/* if (circuit->circ_type != CIRCUIT_T_BROADCAST) */
+	        ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
 	      }
+	    /* 7.3.15.2 b) 3) if it is older, clear SSN and set SRM */
 	    else if (cmp == LSP_OLDER)
 	      {
 		ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
 		ISIS_SET_FLAG (lsp->SRMflags, circuit);
 	      }
+	    /* 7.3.15.2 b) 4) if it is newer, set SSN and clear SRM on p2p */
 	    else
 	      {
-		/* 7.3.15.2 b) 4) if it is newer, set SSN and clear SRM
-		 * on p2p */
 		if (own_lsp)
 		  {
 		    lsp_inc_seqnum (lsp, ntohl (entry->seq_num));
@@ -1498,8 +1753,8 @@
 		else
 		  {
 		    ISIS_SET_FLAG (lsp->SSNflags, circuit);
-		    if (circuit->circ_type != CIRCUIT_T_BROADCAST)
-		      ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
+		    /* if (circuit->circ_type != CIRCUIT_T_BROADCAST) */
+		    ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
 		  }
 	      }
 	  }
@@ -1512,7 +1767,9 @@
 	      {
 		lsp = lsp_new (entry->lsp_id, ntohs (entry->rem_lifetime),
 			       0, 0, entry->checksum, level);
+		lsp->area = circuit->area;
 		lsp_insert (lsp, circuit->area->lspdb[level - 1]);
+		ISIS_FLAGS_CLEAR_ALL (lsp->SRMflags);
 		ISIS_SET_FLAG (lsp->SSNflags, circuit);
 	      }
 	  }
@@ -1523,7 +1780,8 @@
   if (snp_type == ISIS_SNP_CSNP_FLAG)
     {
       /*
-       * Build a list from our own LSP db bounded with start_ and stop_lsp_id
+       * Build a list from our own LSP db bounded with
+       * start_lsp_id and stop_lsp_id
        */
       lsp_list = list_new ();
       lsp_build_list_nonzero_ht (chdr->start_lsp_id, chdr->stop_lsp_id,
@@ -1546,11 +1804,10 @@
 	}
       /* on remaining LSPs we set SRM (neighbor knew not of) */
       for (ALL_LIST_ELEMENTS_RO (lsp_list, node, lsp))
-      {
 	ISIS_SET_FLAG (lsp->SRMflags, circuit);
-      }
       /* lets free it */
-      list_free (lsp_list);
+      list_delete (lsp_list);
+
     }
 
   free_tlvs (&tlvs);
@@ -1560,6 +1817,16 @@
 static int
 process_csnp (int level, struct isis_circuit *circuit, u_char * ssnpa)
 {
+  if (isis->debugs & DEBUG_SNP_PACKETS)
+    {
+      zlog_debug ("ISIS-Snp (%s): Rcvd L%d CSNP on %s, cirType %s, cirID %u",
+                  circuit->area->area_tag, level, circuit->interface->name,
+                  circuit_t2string (circuit->is_type), circuit->circuit_id);
+      if (isis->debugs & DEBUG_PACKET_DUMP)
+        zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
+                        stream_get_endp (circuit->rcv_stream));
+    }
+
   /* Sanity check - FIXME: move to correct place */
   if ((stream_get_endp (circuit->rcv_stream) -
        stream_get_getp (circuit->rcv_stream)) < ISIS_CSNP_HDRLEN)
@@ -1574,10 +1841,20 @@
 static int
 process_psnp (int level, struct isis_circuit *circuit, u_char * ssnpa)
 {
+  if (isis->debugs & DEBUG_SNP_PACKETS)
+    {
+      zlog_debug ("ISIS-Snp (%s): Rcvd L%d PSNP on %s, cirType %s, cirID %u",
+                  circuit->area->area_tag, level, circuit->interface->name,
+                  circuit_t2string (circuit->is_type), circuit->circuit_id);
+      if (isis->debugs & DEBUG_PACKET_DUMP)
+        zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
+                        stream_get_endp (circuit->rcv_stream));
+    }
+
   if ((stream_get_endp (circuit->rcv_stream) -
        stream_get_getp (circuit->rcv_stream)) < ISIS_PSNP_HDRLEN)
     {
-      zlog_warn ("Packet too short");
+      zlog_warn ("Packet too short ( < %d)", ISIS_PSNP_HDRLEN);
       return ISIS_WARNING;
     }
 
@@ -1601,6 +1878,16 @@
   u_char neigh_len;
   u_char *sysid;
 
+  if (isis->debugs & DEBUG_ADJ_PACKETS)
+    {
+      zlog_debug ("ISIS-Adj (%s): Rcvd ISH on %s, cirType %s, cirID %u",
+                  circuit->area->area_tag, circuit->interface->name,
+                  circuit_t2string (circuit->is_type), circuit->circuit_id);
+      if (isis->debugs & DEBUG_PACKET_DUMP)
+        zlog_dump_data (STREAM_DATA (circuit->rcv_stream),
+                        stream_get_endp (circuit->rcv_stream));
+    }
+
   /* In this point in time we are not yet able to handle is_hellos
    * on lan - Sorry juniper...
    */
@@ -1665,7 +1952,6 @@
 isis_handle_pdu (struct isis_circuit *circuit, u_char * ssnpa)
 {
   struct isis_fixed_hdr *hdr;
-  struct esis_fixed_hdr *esis_hdr;
 
   int retval = ISIS_OK;
 
@@ -1676,7 +1962,7 @@
 
   if ((hdr->idrp != ISO10589_ISIS) && (hdr->idrp != ISO9542_ESIS))
     {
-      zlog_warn ("Not an IS-IS or ES-IS packet IDRP=%02x", hdr->idrp);
+      zlog_err ("Not an IS-IS or ES-IS packet IDRP=%02x", hdr->idrp);
       return ISIS_ERROR;
     }
 
@@ -1685,28 +1971,11 @@
    */
   if (hdr->idrp == ISO9542_ESIS)
     {
-      esis_hdr = (struct esis_fixed_hdr *) STREAM_DATA (circuit->rcv_stream);
-      stream_set_getp (circuit->rcv_stream, ESIS_FIXED_HDR_LEN);
-      /* FIXME: Need to do some acceptence tests */
-      /* example length... */
-      switch (esis_hdr->pdu_type)
-	{
-	case ESH_PDU:
-	  /* FIXME */
-	  break;
-	case ISH_PDU:
-	  zlog_debug ("AN ISH PDU!!");
-	  retval = process_is_hello (circuit);
-	  break;
-	default:
-	  return ISIS_ERROR;
-	}
-      return retval;
+      zlog_err ("No support for ES-IS packet IDRP=%02x", hdr->idrp);
+      return ISIS_ERROR;
     }
-  else
-    {
-      stream_set_getp (circuit->rcv_stream, ISIS_FIXED_HDR_LEN);
-    }
+  stream_set_getp (circuit->rcv_stream, ISIS_FIXED_HDR_LEN);
+
   /*
    * and then process it
    */
@@ -1737,6 +2006,14 @@
       zlog_warn ("Unsupported ISIS version %u", hdr->version2);
       return ISIS_WARNING;
     }
+
+  if (circuit->is_passive)
+    {
+      zlog_warn ("Received ISIS PDU on passive circuit %s",
+		 circuit->interface->name);
+      return ISIS_WARNING;
+    }
+
   /* either 3 or 0 */
   if ((hdr->max_area_addrs != 0)
       && (hdr->max_area_addrs != isis->max_area_addrs))
@@ -1797,9 +2074,6 @@
   circuit = THREAD_ARG (thread);
   assert (circuit);
 
-  if (!circuit->area)
-    return ISIS_OK;
-
   if (circuit->rcv_stream == NULL)
     circuit->rcv_stream = stream_new (ISO_MTU (circuit));
   else
@@ -1814,8 +2088,11 @@
   /* 
    * prepare for next packet. 
    */
-  THREAD_READ_ON (master, circuit->t_read, isis_receive, circuit,
-		  circuit->fd);
+  if (!circuit->is_passive)
+  {
+    THREAD_READ_ON (master, circuit->t_read, isis_receive, circuit,
+                    circuit->fd);
+  }
 
   return retval;
 }
@@ -1849,10 +2126,13 @@
   /* 
    * prepare for next packet. 
    */
-  circuit->t_read = thread_add_timer_msec (master, isis_receive, circuit,
-					   listcount
-					   (circuit->area->circuit_list) *
-					   100);
+  if (!circuit->is_passive)
+  {
+    circuit->t_read = thread_add_timer_msec (master, isis_receive, circuit,
+  					     listcount
+					     (circuit->area->circuit_list) *
+					     100);
+  }
 
   return retval;
 }
@@ -1927,14 +2207,13 @@
   struct isis_fixed_hdr fixed_hdr;
   struct isis_lan_hello_hdr hello_hdr;
   struct isis_p2p_hello_hdr p2p_hello_hdr;
-  char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
-
+  unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
+  unsigned long len_pointer, length, auth_tlv_offset = 0;
   u_int32_t interval;
-  unsigned long len_pointer, length, auth_tlv;
   int retval;
 
-  if (circuit->state != C_STATE_UP || circuit->interface == NULL)
-    return ISIS_WARNING;
+  if (circuit->is_passive)
+    return ISIS_OK;
 
   if (circuit->interface->mtu == 0)
     {
@@ -1948,7 +2227,7 @@
     stream_reset (circuit->snd_stream);
 
   if (circuit->circ_type == CIRCUIT_T_BROADCAST)
-    if (level == 1)
+    if (level == IS_LEVEL_1)
       fill_fixed_hdr_andstream (&fixed_hdr, L1_LAN_HELLO,
 				circuit->snd_stream);
     else
@@ -1963,12 +2242,9 @@
   memset (&hello_hdr, 0, sizeof (struct isis_lan_hello_hdr));
   interval = circuit->hello_multiplier[level - 1] *
     circuit->hello_interval[level - 1];
-  /* If we are the DIS then hello interval is divided by three, as is the hold-timer */
-  if (circuit->u.bc.is_dr[level - 1])
-    interval=interval/3;
   if (interval > USHRT_MAX)
     interval = USHRT_MAX;
-  hello_hdr.circuit_t = circuit->circuit_is_type;
+  hello_hdr.circuit_t = circuit->is_type;
   memcpy (hello_hdr.source_id, isis->sysid, ISIS_SYS_ID_LEN);
   hello_hdr.hold_time = htons ((u_int16_t) interval);
 
@@ -1985,13 +2261,13 @@
     }
   else
     {
-      hello_hdr.prio = circuit->u.bc.priority[level - 1];
-      if (level == 1 && circuit->u.bc.l1_desig_is)
+      hello_hdr.prio = circuit->priority[level - 1];
+      if (level == IS_LEVEL_1)
 	{
 	  memcpy (hello_hdr.lan_id, circuit->u.bc.l1_desig_is,
 		  ISIS_SYS_ID_LEN + 1);
 	}
-      else if (level == 2 && circuit->u.bc.l2_desig_is)
+      else if (level == IS_LEVEL_2)
 	{
 	  memcpy (hello_hdr.lan_id, circuit->u.bc.l2_desig_is,
 		  ISIS_SYS_ID_LEN + 1);
@@ -2000,68 +2276,73 @@
     }
 
   /*
-   * Then the variable length part 
+   * Then the variable length part.
    */
 
   /* add circuit password */
-  /* Cleartext */
-  if (circuit->passwd.type == ISIS_PASSWD_TYPE_CLEARTXT)
-    if (tlv_add_authinfo (ISIS_PASSWD_TYPE_CLEARTXT, circuit->passwd.len,
-			  circuit->passwd.passwd, circuit->snd_stream))
-      return ISIS_WARNING;
+  switch (circuit->passwd.type)
+  {
+    /* Cleartext */
+    case ISIS_PASSWD_TYPE_CLEARTXT:
+      if (tlv_add_authinfo (circuit->passwd.type, circuit->passwd.len,
+                            circuit->passwd.passwd, circuit->snd_stream))
+        return ISIS_WARNING;
+      break;
 
-  /* or HMAC MD5 */
-  if (circuit->passwd.type == ISIS_PASSWD_TYPE_HMAC_MD5)
-    {
+    /* HMAC MD5 */
+    case ISIS_PASSWD_TYPE_HMAC_MD5:
       /* Remember where TLV is written so we can later overwrite the MD5 hash */
-      auth_tlv = stream_get_endp (circuit->snd_stream);
+      auth_tlv_offset = stream_get_endp (circuit->snd_stream);
       memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
-      if (tlv_add_authinfo (ISIS_PASSWD_TYPE_HMAC_MD5, ISIS_AUTH_MD5_SIZE,
-			   hmac_md5_hash, circuit->snd_stream))
-	return ISIS_WARNING;
+      if (tlv_add_authinfo (circuit->passwd.type, ISIS_AUTH_MD5_SIZE,
+                            hmac_md5_hash, circuit->snd_stream))
+        return ISIS_WARNING;
+      break;
+
+    default:
+      break;
+  }
+
+  /*  Area Addresses TLV */
+  if (listcount (circuit->area->area_addrs) == 0)
+    return ISIS_WARNING;
+  if (tlv_add_area_addrs (circuit->area->area_addrs, circuit->snd_stream))
+    return ISIS_WARNING;
+
+  /*  LAN Neighbors TLV */
+  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+    {
+      if (level == IS_LEVEL_1 && circuit->u.bc.lan_neighs[0] &&
+          listcount (circuit->u.bc.lan_neighs[0]) > 0)
+	if (tlv_add_lan_neighs (circuit->u.bc.lan_neighs[0],
+				circuit->snd_stream))
+	  return ISIS_WARNING;
+      if (level == IS_LEVEL_2 && circuit->u.bc.lan_neighs[1] &&
+          listcount (circuit->u.bc.lan_neighs[1]) > 0)
+	if (tlv_add_lan_neighs (circuit->u.bc.lan_neighs[1],
+				circuit->snd_stream))
+	  return ISIS_WARNING;
     }
 
   /* Protocols Supported TLV */
   if (circuit->nlpids.count > 0)
     if (tlv_add_nlpid (&circuit->nlpids, circuit->snd_stream))
       return ISIS_WARNING;
-
-  /*  Area Addresses TLV */
-  assert (circuit->area);
-  if (circuit->area->area_addrs && circuit->area->area_addrs->count > 0)
-    if (tlv_add_area_addrs (circuit->area->area_addrs, circuit->snd_stream))
-      return ISIS_WARNING;
-
   /* IP interface Address TLV */
-  if (circuit->ip_router && circuit->ip_addrs && circuit->ip_addrs->count > 0)
+  if (circuit->ip_router && circuit->ip_addrs &&
+      listcount (circuit->ip_addrs) > 0)
     if (tlv_add_ip_addrs (circuit->ip_addrs, circuit->snd_stream))
       return ISIS_WARNING;
 
 #ifdef HAVE_IPV6
   /* IPv6 Interface Address TLV */
   if (circuit->ipv6_router && circuit->ipv6_link &&
-      circuit->ipv6_link->count > 0)
+      listcount (circuit->ipv6_link) > 0)
     if (tlv_add_ipv6_addrs (circuit->ipv6_link, circuit->snd_stream))
       return ISIS_WARNING;
 #endif /* HAVE_IPV6 */
 
-  /* Restart signaling, vendor C sends it too */
-  retval = add_tlv (211, 3, 0, circuit->snd_stream);
-
-  /*  LAN Neighbors TLV */
-  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
-    {
-      if (level == 1 && circuit->u.bc.lan_neighs[0]->count > 0)
-	if (tlv_add_lan_neighs (circuit->u.bc.lan_neighs[0],
-				circuit->snd_stream))
-	  return ISIS_WARNING;
-      if (level == 2 && circuit->u.bc.lan_neighs[1]->count > 0)
-	if (tlv_add_lan_neighs (circuit->u.bc.lan_neighs[1],
-				circuit->snd_stream))
-	  return ISIS_WARNING;
-    }
-
-  if (circuit->u.bc.pad_hellos)
+  if (circuit->pad_hellos)
     if (tlv_add_padding (circuit->snd_stream))
       return ISIS_WARNING;
 
@@ -2072,16 +2353,15 @@
   /* For HMAC MD5 we need to compute the md5 hash and store it */
   if (circuit->passwd.type == ISIS_PASSWD_TYPE_HMAC_MD5)
     {
-      hmac_md5(circuit->snd_stream->data, stream_get_endp(circuit->snd_stream), (unsigned char *) &circuit->passwd.passwd, circuit->passwd.len, (unsigned char *) &hmac_md5_hash);
+      hmac_md5 (STREAM_DATA (circuit->snd_stream),
+                stream_get_endp (circuit->snd_stream),
+                (unsigned char *) &circuit->passwd.passwd, circuit->passwd.len,
+                (caddr_t) &hmac_md5_hash);
       /* Copy the hash into the stream */
-      memcpy(circuit->snd_stream->data+auth_tlv+3,hmac_md5_hash,ISIS_AUTH_MD5_SIZE);
+      memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
+              hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
     }
 
-  retval = circuit->tx (circuit, level);
-  if (retval)
-    zlog_warn ("sending of LAN Level %d Hello failed", level);
-
-  /* DEBUG_ADJ_PACKETS */
   if (isis->debugs & DEBUG_ADJ_PACKETS)
     {
       if (circuit->circ_type == CIRCUIT_T_BROADCAST)
@@ -2089,24 +2369,26 @@
 	  zlog_debug ("ISIS-Adj (%s): Sent L%d LAN IIH on %s, length %ld",
 		      circuit->area->area_tag, level, circuit->interface->name,
 		      /* FIXME: use %z when we stop supporting old compilers. */
-		      (unsigned long) STREAM_SIZE (circuit->snd_stream));
+		      length);
 	}
       else
 	{
 	  zlog_debug ("ISIS-Adj (%s): Sent P2P IIH on %s, length %ld",
 		      circuit->area->area_tag, circuit->interface->name,
 		      /* FIXME: use %z when we stop supporting old compilers. */
-		      (unsigned long) STREAM_SIZE (circuit->snd_stream));
+		      length);
 	}
+      if (isis->debugs & DEBUG_PACKET_DUMP)
+        zlog_dump_data (STREAM_DATA (circuit->snd_stream),
+                        stream_get_endp (circuit->snd_stream));
     }
 
-  return retval;
-}
+  retval = circuit->tx (circuit, level);
+  if (retval != ISIS_OK)
+    zlog_err ("ISIS-Adj (%s): Send L%d IIH on %s failed",
+              circuit->area->area_tag, level, circuit->interface->name);
 
-static int
-send_lan_hello (struct isis_circuit *circuit, int level)
-{
-  return send_hello (circuit, level);
+  return retval;
 }
 
 int
@@ -2114,32 +2396,20 @@
 {
   struct isis_circuit *circuit;
   int retval;
-  unsigned long next_hello;
 
   circuit = THREAD_ARG (thread);
   assert (circuit);
-
-  if (!circuit->area) {
-    return ISIS_OK;
-  }
-
-  /* Pseudonode sends hellos three times more than the other nodes */
-  if (circuit->u.bc.is_dr[0])
-    next_hello=circuit->hello_interval[0]/3+1;
-  else
-    next_hello=circuit->hello_interval[0];
-
   circuit->u.bc.t_send_lan_hello[0] = NULL;
 
   if (circuit->u.bc.run_dr_elect[0])
     retval = isis_dr_elect (circuit, 1);
 
-  retval = send_lan_hello (circuit, 1);
+  retval = send_hello (circuit, 1);
 
   /* set next timer thread */
   THREAD_TIMER_ON (master, circuit->u.bc.t_send_lan_hello[0],
 		   send_lan_l1_hello, circuit,
-                  isis_jitter (next_hello, IIH_JITTER));
+		   isis_jitter (circuit->hello_interval[0], IIH_JITTER));
 
   return retval;
 }
@@ -2149,32 +2419,20 @@
 {
   struct isis_circuit *circuit;
   int retval;
-  unsigned long next_hello;
 
   circuit = THREAD_ARG (thread);
   assert (circuit);
-
-  if (!circuit->area) {
-    return ISIS_OK;
-  }
-
-  /* Pseudonode sends hellos three times more than the other nodes */
-  if (circuit->u.bc.is_dr[1])
-    next_hello=circuit->hello_interval[1]/3+1;
-  else
-    next_hello=circuit->hello_interval[1];
-
   circuit->u.bc.t_send_lan_hello[1] = NULL;
 
   if (circuit->u.bc.run_dr_elect[1])
     retval = isis_dr_elect (circuit, 2);
 
-  retval = send_lan_hello (circuit, 2);
+  retval = send_hello (circuit, 2);
 
   /* set next timer thread */
   THREAD_TIMER_ON (master, circuit->u.bc.t_send_lan_hello[1],
 		   send_lan_l2_hello, circuit,
-                  isis_jitter (next_hello, IIH_JITTER));
+		   isis_jitter (circuit->hello_interval[1], IIH_JITTER));
 
   return retval;
 }
@@ -2204,11 +2462,18 @@
 {
   struct isis_fixed_hdr fixed_hdr;
   struct isis_passwd *passwd;
-  int retval = ISIS_OK;
   unsigned long lenp;
   u_int16_t length;
+  unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
+  unsigned long auth_tlv_offset = 0;
+  int retval = ISIS_OK;
 
-  if (level == 1)
+  if (circuit->snd_stream == NULL)
+    circuit->snd_stream = stream_new (ISO_MTU (circuit));
+  else
+    stream_reset (circuit->snd_stream);
+
+  if (level == IS_LEVEL_1)
     fill_fixed_hdr_andstream (&fixed_hdr, L1_COMPLETE_SEQ_NUM,
 			      circuit->snd_stream);
   else
@@ -2232,84 +2497,244 @@
   /*
    * And TLVs
    */
-  if (level == 1)
+  if (level == IS_LEVEL_1)
     passwd = &circuit->area->area_passwd;
   else
     passwd = &circuit->area->domain_passwd;
 
   if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
-    if (passwd->type)
-      retval = tlv_add_authinfo (passwd->type, passwd->len,
-				 passwd->passwd, circuit->snd_stream);
-
-  if (!retval && lsps)
+  {
+    switch (passwd->type)
     {
-      retval = tlv_add_lsp_entries (lsps, circuit->snd_stream);
+      /* Cleartext */
+      case ISIS_PASSWD_TYPE_CLEARTXT:
+        if (tlv_add_authinfo (ISIS_PASSWD_TYPE_CLEARTXT, passwd->len,
+                              passwd->passwd, circuit->snd_stream))
+          return ISIS_WARNING;
+        break;
+
+        /* HMAC MD5 */
+      case ISIS_PASSWD_TYPE_HMAC_MD5:
+        /* Remember where TLV is written so we can later overwrite the MD5 hash */
+        auth_tlv_offset = stream_get_endp (circuit->snd_stream);
+        memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
+        if (tlv_add_authinfo (ISIS_PASSWD_TYPE_HMAC_MD5, ISIS_AUTH_MD5_SIZE,
+                              hmac_md5_hash, circuit->snd_stream))
+          return ISIS_WARNING;
+        break;
+
+      default:
+        break;
     }
+  }
+
+  retval = tlv_add_lsp_entries (lsps, circuit->snd_stream);
+  if (retval != ISIS_OK)
+    return retval;
+
   length = (u_int16_t) stream_get_endp (circuit->snd_stream);
-  assert (length >= ISIS_CSNP_HDRLEN);
   /* Update PU length */
   stream_putw_at (circuit->snd_stream, lenp, length);
 
+  /* For HMAC MD5 we need to compute the md5 hash and store it */
+  if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND) &&
+      passwd->type == ISIS_PASSWD_TYPE_HMAC_MD5)
+    {
+      hmac_md5 (STREAM_DATA (circuit->snd_stream),
+                stream_get_endp(circuit->snd_stream),
+                (unsigned char *) &passwd->passwd, passwd->len,
+                (caddr_t) &hmac_md5_hash);
+      /* Copy the hash into the stream */
+      memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
+              hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
+    }
+
   return retval;
 }
 
 /*
+ * Count the maximum number of lsps that can be accomodated by a given size.
+ */
+static uint16_t
+get_max_lsp_count (uint16_t size)
+{
+  uint16_t tlv_count;
+  uint16_t lsp_count;
+  uint16_t remaining_size;
+
+  /* First count the full size TLVs */
+  tlv_count = size / MAX_LSP_ENTRIES_TLV_SIZE;
+  lsp_count = tlv_count * (MAX_LSP_ENTRIES_TLV_SIZE / LSP_ENTRIES_LEN);
+
+  /* The last TLV, if any */
+  remaining_size = size % MAX_LSP_ENTRIES_TLV_SIZE;
+  if (remaining_size - 2 >= LSP_ENTRIES_LEN)
+    lsp_count += (remaining_size - 2) / LSP_ENTRIES_LEN;
+
+  return lsp_count;
+}
+
+/*
+ * Calculate the length of Authentication Info. TLV.
+ */
+static uint16_t
+auth_tlv_length (int level, struct isis_circuit *circuit)
+{
+  struct isis_passwd *passwd;
+  uint16_t length;
+
+  if (level == IS_LEVEL_1)
+    passwd = &circuit->area->area_passwd;
+  else
+    passwd = &circuit->area->domain_passwd;
+
+  /* Also include the length of TLV header */
+  length = AUTH_INFO_HDRLEN;
+  if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
+  {
+    switch (passwd->type)
+    {
+      /* Cleartext */
+      case ISIS_PASSWD_TYPE_CLEARTXT:
+        length += passwd->len;
+        break;
+
+        /* HMAC MD5 */
+      case ISIS_PASSWD_TYPE_HMAC_MD5:
+        length += ISIS_AUTH_MD5_SIZE;
+        break;
+
+      default:
+        break;
+    }
+  }
+
+  return length;
+}
+
+/*
+ * Calculate the maximum number of lsps that can be accomodated in a CSNP/PSNP.
+ */
+static uint16_t
+max_lsps_per_snp (int snp_type, int level, struct isis_circuit *circuit)
+{
+  int snp_hdr_len;
+  int auth_tlv_len;
+  uint16_t lsp_count;
+
+  snp_hdr_len = ISIS_FIXED_HDR_LEN;
+  if (snp_type == ISIS_SNP_CSNP_FLAG)
+    snp_hdr_len += ISIS_CSNP_HDRLEN;
+  else
+    snp_hdr_len += ISIS_PSNP_HDRLEN;
+
+  auth_tlv_len = auth_tlv_length (level, circuit);
+  lsp_count = get_max_lsp_count (
+      stream_get_size (circuit->snd_stream) - snp_hdr_len - auth_tlv_len);
+  return lsp_count;
+}
+
+/*
  * FIXME: support multiple CSNPs
  */
 
 int
 send_csnp (struct isis_circuit *circuit, int level)
 {
-  int retval = ISIS_OK;
   u_char start[ISIS_SYS_ID_LEN + 2];
   u_char stop[ISIS_SYS_ID_LEN + 2];
   struct list *list = NULL;
   struct listnode *node;
   struct isis_lsp *lsp;
+  u_char num_lsps, loop = 1;
+  int i, retval = ISIS_OK;
 
-  if (circuit->state != C_STATE_UP || circuit->interface == NULL)
-    return ISIS_WARNING;
+  if (circuit->area->lspdb[level - 1] == NULL ||
+      dict_count (circuit->area->lspdb[level - 1]) == 0)
+    return retval;
 
   memset (start, 0x00, ISIS_SYS_ID_LEN + 2);
   memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
 
-  if (circuit->area->lspdb[level - 1] &&
-      dict_count (circuit->area->lspdb[level - 1]) > 0)
+  num_lsps = max_lsps_per_snp (ISIS_SNP_CSNP_FLAG, level, circuit);
+
+  while (loop)
     {
       list = list_new ();
-      lsp_build_list (start, stop, list, circuit->area->lspdb[level - 1]);
-
-      if (circuit->snd_stream == NULL)
-	circuit->snd_stream = stream_new (ISO_MTU (circuit));
+      lsp_build_list (start, stop, num_lsps, list,
+                      circuit->area->lspdb[level - 1]);
+      /*
+       * Update the stop lsp_id before encoding this CSNP.
+       */
+      if (listcount (list) < num_lsps)
+        {
+          memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
+        }
       else
-	stream_reset (circuit->snd_stream);
+        {
+          node = listtail (list);
+          lsp = listgetdata (node);
+          memcpy (stop, lsp->lsp_header->lsp_id, ISIS_SYS_ID_LEN + 2);
+        }
 
       retval = build_csnp (level, start, stop, list, circuit);
+      if (retval != ISIS_OK)
+        {
+          zlog_err ("ISIS-Snp (%s): Build L%d CSNP on %s failed",
+                    circuit->area->area_tag, level, circuit->interface->name);
+          list_delete (list);
+          return retval;
+        }
 
       if (isis->debugs & DEBUG_SNP_PACKETS)
-	{
-	  zlog_debug ("ISIS-Snp (%s): Sent L%d CSNP on %s, length %ld",
-		     circuit->area->area_tag, level, circuit->interface->name,
-		     /* FIXME: use %z when we stop supporting old compilers. */
-		     (unsigned long) STREAM_SIZE (circuit->snd_stream));
-	  for (ALL_LIST_ELEMENTS_RO (list, node, lsp))
-	  {
-	    zlog_debug ("ISIS-Snp (%s):         CSNP entry %s, seq 0x%08x,"
-			" cksum 0x%04x, lifetime %us",
-			circuit->area->area_tag,
-			rawlspid_print (lsp->lsp_header->lsp_id),
-			ntohl (lsp->lsp_header->seq_num),
-			ntohs (lsp->lsp_header->checksum),
-			ntohs (lsp->lsp_header->rem_lifetime));
-	  }
-	}
+        {
+          zlog_debug ("ISIS-Snp (%s): Sent L%d CSNP on %s, length %ld",
+                      circuit->area->area_tag, level, circuit->interface->name,
+                      stream_get_endp (circuit->snd_stream));
+          for (ALL_LIST_ELEMENTS_RO (list, node, lsp))
+            {
+              zlog_debug ("ISIS-Snp (%s):         CSNP entry %s, seq 0x%08x,"
+                          " cksum 0x%04x, lifetime %us",
+                          circuit->area->area_tag,
+                          rawlspid_print (lsp->lsp_header->lsp_id),
+                          ntohl (lsp->lsp_header->seq_num),
+                          ntohs (lsp->lsp_header->checksum),
+                          ntohs (lsp->lsp_header->rem_lifetime));
+            }
+          if (isis->debugs & DEBUG_PACKET_DUMP)
+            zlog_dump_data (STREAM_DATA (circuit->snd_stream),
+                            stream_get_endp (circuit->snd_stream));
+        }
 
+      retval = circuit->tx (circuit, level);
+      if (retval != ISIS_OK)
+        {
+          zlog_err ("ISIS-Snp (%s): Send L%d CSNP on %s failed",
+                    circuit->area->area_tag, level,
+                    circuit->interface->name);
+          list_delete (list);
+          return retval;
+        }
+
+      /*
+       * Start lsp_id of the next CSNP should be one plus the
+       * stop lsp_id in this current CSNP.
+       */
+      memcpy (start, stop, ISIS_SYS_ID_LEN + 2);
+      loop = 0;
+      for (i = ISIS_SYS_ID_LEN + 1; i >= 0; --i)
+        {
+          if (start[i] < (u_char)0xff)
+            {
+              start[i] += 1;
+              loop = 1;
+              break;
+            }
+        }
+      memset (stop, 0xff, ISIS_SYS_ID_LEN + 2);
       list_delete (list);
-
-      if (retval == ISIS_OK)
-	retval = circuit->tx (circuit, level);
     }
+
   return retval;
 }
 
@@ -2325,7 +2750,9 @@
   circuit->t_send_csnp[0] = NULL;
 
   if (circuit->circ_type == CIRCUIT_T_BROADCAST && circuit->u.bc.is_dr[0])
+    {
       send_csnp (circuit, 1);
+    }
   /* set next timer thread */
   THREAD_TIMER_ON (master, circuit->t_send_csnp[0], send_l1_csnp, circuit,
 		   isis_jitter (circuit->csnp_interval[0], CSNP_JITTER));
@@ -2345,7 +2772,9 @@
   circuit->t_send_csnp[1] = NULL;
 
   if (circuit->circ_type == CIRCUIT_T_BROADCAST && circuit->u.bc.is_dr[1])
+    {
       send_csnp (circuit, 2);
+    }
   /* set next timer thread */
   THREAD_TIMER_ON (master, circuit->t_send_csnp[1], send_l2_csnp, circuit,
 		   isis_jitter (circuit->csnp_interval[1], CSNP_JITTER));
@@ -2359,12 +2788,19 @@
   struct isis_fixed_hdr fixed_hdr;
   unsigned long lenp;
   u_int16_t length;
-  int retval = 0;
   struct isis_lsp *lsp;
   struct isis_passwd *passwd;
   struct listnode *node;
+  unsigned char hmac_md5_hash[ISIS_AUTH_MD5_SIZE];
+  unsigned long auth_tlv_offset = 0;
+  int retval = ISIS_OK;
 
-  if (level == 1)
+  if (circuit->snd_stream == NULL)
+    circuit->snd_stream = stream_new (ISO_MTU (circuit));
+  else
+    stream_reset (circuit->snd_stream);
+
+  if (level == IS_LEVEL_1)
     fill_fixed_hdr_andstream (&fixed_hdr, L1_PARTIAL_SEQ_NUM,
 			      circuit->snd_stream);
   else
@@ -2383,20 +2819,40 @@
    * And TLVs
    */
 
-  if (level == 1)
+  if (level == IS_LEVEL_1)
     passwd = &circuit->area->area_passwd;
   else
     passwd = &circuit->area->domain_passwd;
 
   if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND))
-    if (passwd->type)
-      retval = tlv_add_authinfo (passwd->type, passwd->len,
-				 passwd->passwd, circuit->snd_stream);
-
-  if (!retval && lsps)
+  {
+    switch (passwd->type)
     {
-      retval = tlv_add_lsp_entries (lsps, circuit->snd_stream);
+      /* Cleartext */
+      case ISIS_PASSWD_TYPE_CLEARTXT:
+        if (tlv_add_authinfo (ISIS_PASSWD_TYPE_CLEARTXT, passwd->len,
+                              passwd->passwd, circuit->snd_stream))
+          return ISIS_WARNING;
+        break;
+
+        /* HMAC MD5 */
+      case ISIS_PASSWD_TYPE_HMAC_MD5:
+        /* Remember where TLV is written so we can later overwrite the MD5 hash */
+        auth_tlv_offset = stream_get_endp (circuit->snd_stream);
+        memset(&hmac_md5_hash, 0, ISIS_AUTH_MD5_SIZE);
+        if (tlv_add_authinfo (ISIS_PASSWD_TYPE_HMAC_MD5, ISIS_AUTH_MD5_SIZE,
+                              hmac_md5_hash, circuit->snd_stream))
+          return ISIS_WARNING;
+        break;
+
+      default:
+        break;
     }
+  }
+
+  retval = tlv_add_lsp_entries (lsps, circuit->snd_stream);
+  if (retval != ISIS_OK)
+    return retval;
 
   if (isis->debugs & DEBUG_SNP_PACKETS)
     {
@@ -2413,10 +2869,22 @@
     }
 
   length = (u_int16_t) stream_get_endp (circuit->snd_stream);
-  assert (length >= ISIS_PSNP_HDRLEN);
   /* Update PDU length */
   stream_putw_at (circuit->snd_stream, lenp, length);
 
+  /* For HMAC MD5 we need to compute the md5 hash and store it */
+  if (CHECK_FLAG(passwd->snp_auth, SNP_AUTH_SEND) &&
+      passwd->type == ISIS_PASSWD_TYPE_HMAC_MD5)
+    {
+      hmac_md5 (STREAM_DATA (circuit->snd_stream),
+                stream_get_endp(circuit->snd_stream),
+                (unsigned char *) &passwd->passwd, passwd->len,
+                (caddr_t) &hmac_md5_hash);
+      /* Copy the hash into the stream */
+      memcpy (STREAM_DATA (circuit->snd_stream) + auth_tlv_offset + 3,
+              hmac_md5_hash, ISIS_AUTH_MD5_SIZE);
+    }
+
   return ISIS_OK;
 }
 
@@ -2427,57 +2895,74 @@
 static int
 send_psnp (int level, struct isis_circuit *circuit)
 {
-  int retval = ISIS_OK;
   struct isis_lsp *lsp;
   struct list *list = NULL;
   struct listnode *node;
+  u_char num_lsps;
+  int retval = ISIS_OK;
 
-  if (circuit->state != C_STATE_UP || circuit->interface == NULL)
-    return ISIS_WARNING;
+  if (circuit->circ_type == CIRCUIT_T_BROADCAST &&
+      circuit->u.bc.is_dr[level - 1])
+    return ISIS_OK;
 
-  if ((circuit->circ_type == CIRCUIT_T_BROADCAST &&
-       !circuit->u.bc.is_dr[level - 1]) ||
-      circuit->circ_type != CIRCUIT_T_BROADCAST)
+  if (circuit->area->lspdb[level - 1] == NULL ||
+      dict_count (circuit->area->lspdb[level - 1]) == 0)
+    return ISIS_OK;
+
+  if (! circuit->snd_stream)
+    return ISIS_ERROR;
+
+  num_lsps = max_lsps_per_snp (ISIS_SNP_PSNP_FLAG, level, circuit);
+
+  while (1)
     {
+      list = list_new ();
+      lsp_build_list_ssn (circuit, num_lsps, list,
+                          circuit->area->lspdb[level - 1]);
 
-      if (circuit->area->lspdb[level - 1] &&
-	  dict_count (circuit->area->lspdb[level - 1]) > 0)
-	{
-	  list = list_new ();
-	  lsp_build_list_ssn (circuit, list, circuit->area->lspdb[level - 1]);
+      if (listcount (list) == 0)
+        {
+          list_delete (list);
+          return ISIS_OK;
+        }
 
-	  if (listcount (list) > 0)
-	    {
-	      if (circuit->snd_stream == NULL)
-		circuit->snd_stream = stream_new (ISO_MTU (circuit));
-	      else
-		stream_reset (circuit->snd_stream);
+      retval = build_psnp (level, circuit, list);
+      if (retval != ISIS_OK)
+        {
+          zlog_err ("ISIS-Snp (%s): Build L%d PSNP on %s failed",
+                    circuit->area->area_tag, level, circuit->interface->name);
+          list_delete (list);
+          return retval;
+        }
 
+      if (isis->debugs & DEBUG_SNP_PACKETS)
+        {
+          zlog_debug ("ISIS-Snp (%s): Sent L%d PSNP on %s, length %ld",
+                      circuit->area->area_tag, level,
+                      circuit->interface->name,
+                      stream_get_endp (circuit->snd_stream));
+          if (isis->debugs & DEBUG_PACKET_DUMP)
+            zlog_dump_data (STREAM_DATA (circuit->snd_stream),
+                            stream_get_endp (circuit->snd_stream));
+        }
 
-	      if (isis->debugs & DEBUG_SNP_PACKETS)
-		zlog_debug ("ISIS-Snp (%s): Sent L%d PSNP on %s, length %ld",
-			    circuit->area->area_tag, level,
-			    circuit->interface->name,
-			    /* FIXME: use %z when we stop supporting old
-			     * compilers. */
-			    (unsigned long) STREAM_SIZE (circuit->snd_stream));
+      retval = circuit->tx (circuit, level);
+      if (retval != ISIS_OK)
+        {
+          zlog_err ("ISIS-Snp (%s): Send L%d PSNP on %s failed",
+                    circuit->area->area_tag, level,
+                    circuit->interface->name);
+          list_delete (list);
+          return retval;
+        }
 
-	      retval = build_psnp (level, circuit, list);
-	      if (retval == ISIS_OK)
-		retval = circuit->tx (circuit, level);
-
-	      if (retval == ISIS_OK)
-		{
-		  /*
-		   * sending succeeded, we can clear SSN flags of this circuit
-		   * for the LSPs in list
-		   */
-		  for (ALL_LIST_ELEMENTS_RO (list, node, lsp))
-                    ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
-		}
-	    }
-	  list_delete (list);
-	}
+      /*
+       * sending succeeded, we can clear SSN flags of this circuit
+       * for the LSPs in list
+       */
+      for (ALL_LIST_ELEMENTS_RO (list, node, lsp))
+        ISIS_CLEAR_FLAG (lsp->SSNflags, circuit);
+      list_delete (list);
     }
 
   return retval;
@@ -2536,90 +3021,80 @@
   struct isis_circuit *circuit;
   struct isis_lsp *lsp;
   struct listnode *node;
-  int retval = 0;
+  int retval = ISIS_OK;
 
   circuit = THREAD_ARG (thread);
   assert (circuit);
 
-  if (circuit->state != C_STATE_UP || circuit->interface == NULL)
-    return ISIS_WARNING;
+  if (circuit->state != C_STATE_UP || circuit->is_passive == 1)
+  {
+    return retval;
+  }
 
   lsp = listgetdata ((node = listhead (circuit->lsp_queue)));
 
   /*
    * Do not send if levels do not match
    */
-  if (!(lsp->level & circuit->circuit_is_type))
-    goto dontsend;
+  if (!(lsp->level & circuit->is_type))
+    {
+      list_delete_node (circuit->lsp_queue, node);
+      return retval;
+    }
 
   /*
    * Do not send if we do not have adjacencies in state up on the circuit
    */
   if (circuit->upadjcount[lsp->level - 1] == 0)
-    goto dontsend;
-  /* only send if it needs sending */
-  if ((time (NULL) - lsp->last_sent) >=
-      circuit->area->lsp_gen_interval[lsp->level - 1])
     {
-
-      if (isis->debugs & DEBUG_UPDATE_PACKETS)
-	{
-	  zlog_debug
-	    ("ISIS-Upd (%s): Sent L%d LSP %s, seq 0x%08x, cksum 0x%04x,"
-	     " lifetime %us on %s", circuit->area->area_tag, lsp->level,
-	     rawlspid_print (lsp->lsp_header->lsp_id),
-	     ntohl (lsp->lsp_header->seq_num),
-	     ntohs (lsp->lsp_header->checksum),
-	     ntohs (lsp->lsp_header->rem_lifetime),
-	     circuit->interface->name);
-	}
-	/* copy our lsp to the send buffer */
-	stream_copy (circuit->snd_stream, lsp->pdu);
-
-	retval = circuit->tx (circuit, lsp->level);
-
-      /*
-       * If the sending succeeded, we can del the lsp from circuits
-       * lsp_queue
-       */
-      if (retval == ISIS_OK)
-	{
-	  list_delete_node (circuit->lsp_queue, node);
-
-	  /*
-	   * On broadcast circuits also the SRMflag can be cleared
-	   */
-	  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
-	    ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
-
-	  if (flags_any_set (lsp->SRMflags) == 0)
-	    {
-	      /*
-	       * need to remember when we were last sent
-	       */
-	      lsp->last_sent = time (NULL);
-	    }
-	}
-      else
-	{
-	  zlog_debug ("sending of level %d link state failed", lsp->level);
-	}
-    }
-  else
-    {
-      /* my belief is that if it wasn't his time, the lsp can be removed
-       * from the queue
-       */
-    dontsend:
       list_delete_node (circuit->lsp_queue, node);
+      return retval;
     }
-#if 0
+
+  /* copy our lsp to the send buffer */
+  stream_copy (circuit->snd_stream, lsp->pdu);
+
+  if (isis->debugs & DEBUG_UPDATE_PACKETS)
+    {
+      zlog_debug
+        ("ISIS-Upd (%s): Sent L%d LSP %s, seq 0x%08x, cksum 0x%04x,"
+         " lifetime %us on %s", circuit->area->area_tag, lsp->level,
+         rawlspid_print (lsp->lsp_header->lsp_id),
+         ntohl (lsp->lsp_header->seq_num),
+         ntohs (lsp->lsp_header->checksum),
+         ntohs (lsp->lsp_header->rem_lifetime),
+         circuit->interface->name);
+      if (isis->debugs & DEBUG_PACKET_DUMP)
+        zlog_dump_data (STREAM_DATA (circuit->snd_stream),
+                        stream_get_endp (circuit->snd_stream));
+    }
+
+  retval = circuit->tx (circuit, lsp->level);
+  if (retval != ISIS_OK)
+    {
+      zlog_err ("ISIS-Upd (%s): Send L%d LSP on %s failed",
+                circuit->area->area_tag, lsp->level,
+                circuit->interface->name);
+      return retval;
+    }
+
   /*
-   * If there are still LSPs send next one after lsp-interval (33 msecs)
+   * If the sending succeeded, we can del the lsp from circuits
+   * lsp_queue
    */
-  if (listcount (circuit->lsp_queue) > 0)
-    thread_add_timer (master, send_lsp, circuit, 1);
-#endif
+  list_delete_node (circuit->lsp_queue, node);
+
+  /* Set the last-cleared time if the queue is empty. */
+  /* TODO: Is is possible that new lsps keep being added to the queue
+   * that the queue is never empty? */
+  if (list_isempty (circuit->lsp_queue))
+    circuit->lsp_queue_last_cleared = time (NULL);
+
+  /*
+   * On broadcast circuits also the SRMflag can be cleared
+   */
+  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+    ISIS_CLEAR_FLAG (lsp->SRMflags, circuit);
 
   return retval;
 }
@@ -2638,8 +3113,8 @@
   else
     stream_reset (circuit->snd_stream);
 
-//  fill_llc_hdr (stream);
-  if (level == 1)
+  //  fill_llc_hdr (stream);
+  if (level == IS_LEVEL_1)
     fill_fixed_hdr_andstream (&fixed_hdr, L1_PARTIAL_SEQ_NUM,
 			      circuit->snd_stream);
   else
@@ -2664,7 +3139,10 @@
   stream_putw_at (circuit->snd_stream, lenp, length);
 
   retval = circuit->tx (circuit, level);
+  if (retval != ISIS_OK)
+    zlog_err ("ISIS-Upd (%s): Send L%d LSP PSNP on %s failed",
+              circuit->area->area_tag, level,
+              circuit->interface->name);
 
   return retval;
 }
-
diff --git a/isisd/isis_pdu.h b/isisd/isis_pdu.h
index c4c38e2..3eca731 100644
--- a/isisd/isis_pdu.h
+++ b/isisd/isis_pdu.h
@@ -95,7 +95,7 @@
   u_char version2;
   u_char reserved;
   u_char max_area_addrs;
-};
+} __attribute__ ((packed));
 
 #define ISIS_FIXED_HDR_LEN 8
 
@@ -114,7 +114,7 @@
  * +-------+-------+-------+-------+-------+-------+-------+-------+
  * |                        Holding  Time                          | 2     
  * +-------+-------+-------+-------+-------+-------+-------+-------+
- * |                        PDU Lenght                             | 2    
+ * |                        PDU Length                             | 2    
  * +-------+-------+-------+-------+-------+-------+-------+-------+
  * |   R   |                Priority                               | 1
  * +-------+-------+-------+-------+-------+-------+-------+-------+
@@ -142,7 +142,7 @@
  * +-------+-------+-------+-------+-------+-------+-------+-------+
  * +                        Holding  Time                          + 2     
  * +-------+-------+-------+-------+-------+-------+-------+-------+
- * +                        PDU Lenght                             + 2    
+ * +                        PDU Length                             + 2    
  * +-------+-------+-------+-------+-------+-------+-------+-------+
  * |                        Local Circuit ID                       | 1
  * +-------+-------+-------+-------+-------+-------+-------+-------+
@@ -186,12 +186,23 @@
 } __attribute__ ((packed));
 #define ISIS_LSP_HDR_LEN 19
 
+/*
+ * Since the length field of LSP Entries TLV is one byte long, and each LSP
+ * entry is LSP_ENTRIES_LEN (16) bytes long, the maximum number of LSP entries
+ * can be accomodated in a TLV is
+ * 255 / 16 = 15.
+ * 
+ * Therefore, the maximum length of the LSP Entries TLV is
+ * 16 * 15 + 2 (header) = 242 bytes.
+ */
+#define MAX_LSP_ENTRIES_TLV_SIZE 242
+
 #define L1_COMPLETE_SEQ_NUM  24
 #define L2_COMPLETE_SEQ_NUM  25
 /*
  *      L1 and L2 IS to IS complete sequence numbers PDU header
  * +-------+-------+-------+-------+-------+-------+-------+-------+
- * +                        PDU Lenght                             + 2    
+ * +                        PDU Length                             + 2    
  * +-------+-------+-------+-------+-------+-------+-------+-------+
  * +                        Source ID                              + id_len + 1
  * +-------+-------+-------+-------+-------+-------+-------+-------+
@@ -241,6 +252,8 @@
 #define ISIS_SNP_PSNP_FLAG 0
 #define ISIS_SNP_CSNP_FLAG 1
 
+#define ISIS_AUTH_MD5_SIZE       16U
+
 /*
  * Sending functions
  */
@@ -258,7 +271,4 @@
 void fill_fixed_hdr (struct isis_fixed_hdr *hdr, u_char pdu_type);
 int send_hello (struct isis_circuit *circuit, int level);
 
-#define ISIS_AUTH_MD5_SIZE       16U
-int authentication_check (struct isis_passwd *remote, struct isis_passwd *local, struct isis_circuit *c);
-
 #endif /* _ZEBRA_ISIS_PDU_H */
diff --git a/isisd/isis_pfpacket.c b/isisd/isis_pfpacket.c
index 8a5c3ed..42947b2 100644
--- a/isisd/isis_pfpacket.c
+++ b/isisd/isis_pfpacket.c
@@ -134,7 +134,7 @@
 
   circuit->fd = fd;
 
-  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+  if (if_is_broadcast (circuit->interface))
     {
       /*
        * Join to multicast groups
@@ -142,24 +142,22 @@
        * 8.4.2 - Broadcast subnetwork IIH PDUs
        * FIXME: is there a case only one will fail??
        */
-      if (circuit->circuit_is_type & IS_LEVEL_1)
-	{
-	  /* joining ALL_L1_ISS */
-	  retval = isis_multicast_join (circuit->fd, 1,
-					circuit->interface->ifindex);
-	  /* joining ALL_ISS */
-	  retval = isis_multicast_join (circuit->fd, 3,
-					circuit->interface->ifindex);
-	}
-      if (circuit->circuit_is_type & IS_LEVEL_2)
-	/* joining ALL_L2_ISS */
-	retval = isis_multicast_join (circuit->fd, 2,
-				      circuit->interface->ifindex);
+      if (circuit->is_type & IS_LEVEL_1)
+        /* joining ALL_L1_ISS */
+        retval = isis_multicast_join (circuit->fd, 1,
+                                      circuit->interface->ifindex);
+      if (circuit->is_type & IS_LEVEL_2)
+        /* joining ALL_L2_ISS */
+        retval = isis_multicast_join (circuit->fd, 2,
+                                      circuit->interface->ifindex);
+      /* joining ALL_ISS (used in RFC 5309 p2p-over-lan as well) */
+      retval = isis_multicast_join (circuit->fd, 3,
+                                    circuit->interface->ifindex);
     }
   else
     {
       retval =
-	isis_multicast_join (circuit->fd, 0, circuit->interface->ifindex);
+        isis_multicast_join (circuit->fd, 0, circuit->interface->ifindex);
     }
 
   return retval;
@@ -184,12 +182,13 @@
       goto end;
     }
 
-  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+  /* Assign Rx and Tx callbacks are based on real if type */
+  if (if_is_broadcast (circuit->interface))
     {
       circuit->tx = isis_send_pdu_bcast;
       circuit->rx = isis_recv_pdu_bcast;
     }
-  else if (circuit->circ_type == CIRCUIT_T_P2P)
+  else if (if_is_pointopoint (circuit->interface))
     {
       circuit->tx = isis_send_pdu_p2p;
       circuit->rx = isis_recv_pdu_p2p;
@@ -232,19 +231,16 @@
 			LLC_LEN, MSG_PEEK,
 			(struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
 
-  if (!circuit->area) {
-    return ISIS_OK;
-  }
-
   if (bytesread < 0)
     {
-      zlog_warn ("isis_recv_packet_bcast(): fd %d, recvfrom (): %s",
-		 circuit->fd, safe_strerror (errno));
-      zlog_warn ("circuit is %s", circuit->interface->name);
-      zlog_warn ("circuit fd %d", circuit->fd);
-      zlog_warn ("bytesread %d", bytesread);
+      zlog_warn ("isis_recv_packet_bcast(): ifname %s, fd %d, bytesread %d, "
+                 "recvfrom(): %s",
+                 circuit->interface->name, circuit->fd, bytesread,
+                 safe_strerror (errno));
       /* get rid of the packet */
-      bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
+      bytesread = recvfrom (circuit->fd, discard_buff, sizeof (discard_buff),
+                            MSG_DONTWAIT, (struct sockaddr *) &s_addr,
+                            (socklen_t *) &addr_len);
       return ISIS_WARNING;
     }
   /*
@@ -253,15 +249,22 @@
   if (!llc_check (llc) || s_addr.sll_pkttype == PACKET_OUTGOING)
     {
       /*  Read the packet into discard buff */
-      bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
+      bytesread = recvfrom (circuit->fd, discard_buff, sizeof (discard_buff),
+                            MSG_DONTWAIT, (struct sockaddr *) &s_addr,
+                            (socklen_t *) &addr_len);
       if (bytesread < 0)
-	zlog_warn ("isis_recv_pdu_bcast(): read() failed");
+	zlog_warn ("isis_recv_pdu_bcast(): recvfrom() failed");
       return ISIS_WARNING;
     }
 
   /* on lan we have to read to the static buff first */
-  bytesread = recvfrom (circuit->fd, sock_buff, circuit->interface->mtu, 0,
+  bytesread = recvfrom (circuit->fd, sock_buff, sizeof (sock_buff), MSG_DONTWAIT,
 			(struct sockaddr *) &s_addr, (socklen_t *) &addr_len);
+  if (bytesread < 0)
+    {
+      zlog_warn ("isis_recv_pdu_bcast(): recvfrom() failed");
+      return ISIS_WARNING;
+    }
 
   /* then we lose the LLC */
   stream_write (circuit->rcv_stream, sock_buff + LLC_LEN, bytesread - LLC_LEN);
@@ -289,9 +292,11 @@
   if (s_addr.sll_pkttype == PACKET_OUTGOING)
     {
       /*  Read the packet into discard buff */
-      bytesread = read (circuit->fd, discard_buff, sizeof (discard_buff));
+      bytesread = recvfrom (circuit->fd, discard_buff, sizeof (discard_buff),
+                            MSG_DONTWAIT, (struct sockaddr *) &s_addr,
+                            (socklen_t *) &addr_len);
       if (bytesread < 0)
-	zlog_warn ("isis_recv_pdu_p2p(): read() failed");
+	zlog_warn ("isis_recv_pdu_p2p(): recvfrom() failed");
       return ISIS_WARNING;
     }
 
@@ -313,6 +318,9 @@
 int
 isis_send_pdu_bcast (struct isis_circuit *circuit, int level)
 {
+  struct msghdr msg;
+  struct iovec iov[2];
+
   /* we need to do the LLC in here because of P2P circuits, which will
    * not need it
    */
@@ -325,7 +333,10 @@
   sa.sll_protocol = htons (stream_get_endp (circuit->snd_stream) + LLC_LEN);
   sa.sll_ifindex = circuit->interface->ifindex;
   sa.sll_halen = ETH_ALEN;
-  if (level == 1)
+  /* RFC5309 section 4.1 recommends ALL_ISS */
+  if (circuit->circ_type == CIRCUIT_T_P2P)
+    memcpy (&sa.sll_addr, ALL_ISS, ETH_ALEN);
+  else if (level == 1)
     memcpy (&sa.sll_addr, ALL_L1_ISS, ETH_ALEN);
   else
     memcpy (&sa.sll_addr, ALL_L2_ISS, ETH_ALEN);
@@ -336,14 +347,17 @@
   sock_buff[1] = 0xFE;
   sock_buff[2] = 0x03;
 
-  /* then we copy the data */
-  memcpy (sock_buff + LLC_LEN, circuit->snd_stream->data,
-	  stream_get_endp (circuit->snd_stream));
+  memset (&msg, 0, sizeof (msg));
+  msg.msg_name = &sa;
+  msg.msg_namelen = sizeof (struct sockaddr_ll);
+  msg.msg_iov = iov;
+  msg.msg_iovlen = 2;
+  iov[0].iov_base = sock_buff;
+  iov[0].iov_len = LLC_LEN;
+  iov[1].iov_base = circuit->snd_stream->data;
+  iov[1].iov_len = stream_get_endp (circuit->snd_stream);
 
-  /* now we can send this */
-  written = sendto (circuit->fd, sock_buff,
-		    stream_get_endp(circuit->snd_stream) + LLC_LEN, 0,
-		    (struct sockaddr *) &sa, sizeof (struct sockaddr_ll));
+  written = sendmsg (circuit->fd, &msg, 0);
 
   return ISIS_OK;
 }
@@ -351,7 +365,6 @@
 int
 isis_send_pdu_p2p (struct isis_circuit *circuit, int level)
 {
-
   int written = 1;
   struct sockaddr_ll sa;
 
diff --git a/isisd/isis_route.c b/isisd/isis_route.c
index 1286486..c99d958 100644
--- a/isisd/isis_route.c
+++ b/isisd/isis_route.c
@@ -36,6 +36,7 @@
 
 #include "isis_constants.h"
 #include "isis_common.h"
+#include "isis_flags.h"
 #include "dict.h"
 #include "isisd.h"
 #include "isis_misc.h"
@@ -48,9 +49,6 @@
 #include "isis_route.h"
 #include "isis_zebra.h"
 
-extern struct isis *isis;
-extern struct thread_master *master;
-
 static struct isis_nexthop *
 isis_nexthop_create (struct in_addr *ip, unsigned int ifindex)
 {
@@ -246,6 +244,7 @@
 	{
 	  nh = isis_nexthop_create (ipv4_addr,
 				    adj->circuit->interface->ifindex);
+          nh->router_address = adj->router_address;
 	  listnode_add (nexthops, nh);
 	}
     }
@@ -269,6 +268,7 @@
 	{
 	  nh6 = isis_nexthop6_create (ipv6_addr,
 				      adj->circuit->interface->ifindex);
+          nh6->router_address6 = adj->router_address6;
 	  listnode_add (nexthops6, nh6);
 	}
     }
@@ -276,8 +276,8 @@
 #endif /* HAVE_IPV6 */
 
 static struct isis_route_info *
-isis_route_info_new (uint32_t cost, uint32_t depth, u_char family,
-		     struct list *adjacencies)
+isis_route_info_new (struct prefix *prefix, uint32_t cost, uint32_t depth,
+                     struct list *adjacencies)
 {
   struct isis_route_info *rinfo;
   struct isis_adjacency *adj;
@@ -290,18 +290,34 @@
       return NULL;
     }
 
-  if (family == AF_INET)
+  if (prefix->family == AF_INET)
     {
       rinfo->nexthops = list_new ();
       for (ALL_LIST_ELEMENTS_RO (adjacencies, node, adj))
-        adjinfo2nexthop (rinfo->nexthops, adj);
+        {
+          /* check for force resync this route */
+          if (CHECK_FLAG (adj->circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
+            SET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
+          /* update neighbor router address */
+          if (depth == 2 && prefix->prefixlen == 32)
+            adj->router_address = prefix->u.prefix4;
+          adjinfo2nexthop (rinfo->nexthops, adj);
+        }
     }
 #ifdef HAVE_IPV6
-  if (family == AF_INET6)
+  if (prefix->family == AF_INET6)
     {
       rinfo->nexthops6 = list_new ();
       for (ALL_LIST_ELEMENTS_RO (adjacencies, node, adj))
-        adjinfo2nexthop6 (rinfo->nexthops6, adj);
+        {
+          /* check for force resync this route */
+          if (CHECK_FLAG (adj->circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF))
+            SET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
+          /* update neighbor router address */
+          if (depth == 2 && prefix->prefixlen == 128)
+            adj->router_address6 = prefix->u.prefix6;
+          adjinfo2nexthop6 (rinfo->nexthops6, adj);
+        }
     }
 
 #endif /* HAVE_IPV6 */
@@ -353,18 +369,25 @@
 #ifdef HAVE_IPV6
   struct isis_nexthop6 *nexthop6;
 #endif /* HAVE_IPV6 */
+
+  if (!CHECK_FLAG (old->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
+    return 0;
+
+  if (CHECK_FLAG (new->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC))
+    return 0;
+
   if (!isis_route_info_same_attrib (new, old))
     return 0;
 
   if (family == AF_INET)
     {
       for (ALL_LIST_ELEMENTS_RO (new->nexthops, node, nexthop))
-        if (nexthoplookup (old->nexthops, &nexthop->ip, nexthop->ifindex) 
+        if (nexthoplookup (old->nexthops, &nexthop->ip, nexthop->ifindex)
               == 0)
           return 0;
 
       for (ALL_LIST_ELEMENTS_RO (old->nexthops, node, nexthop))
-        if (nexthoplookup (new->nexthops, &nexthop->ip, nexthop->ifindex) 
+        if (nexthoplookup (new->nexthops, &nexthop->ip, nexthop->ifindex)
              == 0)
           return 0;
     }
@@ -386,65 +409,6 @@
   return 1;
 }
 
-static void
-isis_nexthops_merge (struct list *new, struct list *old)
-{
-  struct listnode *node;
-  struct isis_nexthop *nexthop;
-
-  for (ALL_LIST_ELEMENTS_RO (new, node, nexthop))
-    {
-      if (nexthoplookup (old, &nexthop->ip, nexthop->ifindex))
-	continue;
-      listnode_add (old, nexthop);
-      nexthop->lock++;
-    }
-}
-
-#ifdef HAVE_IPV6
-static void
-isis_nexthops6_merge (struct list *new, struct list *old)
-{
-  struct listnode *node;
-  struct isis_nexthop6 *nexthop6;
-
-  for (ALL_LIST_ELEMENTS_RO (new, node, nexthop6))
-    {
-      if (nexthop6lookup (old, &nexthop6->ip6, nexthop6->ifindex))
-	continue;
-      listnode_add (old, nexthop6);
-      nexthop6->lock++;
-    }
-}
-#endif /* HAVE_IPV6 */
-
-static void
-isis_route_info_merge (struct isis_route_info *new,
-		       struct isis_route_info *old, u_char family)
-{
-  if (family == AF_INET)
-    isis_nexthops_merge (new->nexthops, old->nexthops);
-#ifdef HAVE_IPV6
-  else if (family == AF_INET6)
-    isis_nexthops6_merge (new->nexthops6, old->nexthops6);
-#endif /* HAVE_IPV6 */
-
-  return;
-}
-
-static int
-isis_route_info_prefer_new (struct isis_route_info *new,
-			    struct isis_route_info *old)
-{
-  if (!CHECK_FLAG (old->flag, ISIS_ROUTE_FLAG_ACTIVE))
-    return 1;
-
-  if (new->cost < old->cost)
-    return 1;
-
-  return 0;
-}
-
 struct isis_route_info *
 isis_route_create (struct prefix *prefix, u_int32_t cost, u_int32_t depth,
 		   struct list *adjacencies, struct isis_area *area,
@@ -459,7 +423,7 @@
   /* for debugs */
   prefix2str (prefix, (char *) buff, BUFSIZ);
 
-  rinfo_new = isis_route_info_new (cost, depth, family, adjacencies);
+  rinfo_new = isis_route_info_new (prefix, cost, depth, adjacencies);
   if (!rinfo_new)
     {
       zlog_err ("ISIS-Rte (%s): isis_route_create: out of memory!",
@@ -479,68 +443,32 @@
   if (!rinfo_old)
     {
       if (isis->debugs & DEBUG_RTE_EVENTS)
-	zlog_debug ("ISIS-Rte (%s) route created: %s", area->area_tag, buff);
-      SET_FLAG (rinfo_new->flag, ISIS_ROUTE_FLAG_ACTIVE);
-      route_node->info = rinfo_new;
-      return rinfo_new;
-    }
-
-  if (isis->debugs & DEBUG_RTE_EVENTS)
-    zlog_debug ("ISIS-Rte (%s) route already exists: %s", area->area_tag,
-	       buff);
-
-  if (isis_route_info_same (rinfo_new, rinfo_old, family))
-    {
-      if (isis->debugs & DEBUG_RTE_EVENTS)
-	zlog_debug ("ISIS-Rte (%s) route unchanged: %s", area->area_tag, buff);
-      isis_route_info_delete (rinfo_new);
-      route_info = rinfo_old;
-    }
-  else if (isis_route_info_same_attrib (rinfo_new, rinfo_old))
-    {
-      /* merge the nexthop lists */
-      if (isis->debugs & DEBUG_RTE_EVENTS)
-	zlog_debug ("ISIS-Rte (%s) route changed (same attribs): %s",
-		   area->area_tag, buff);
-#ifdef EXTREME_DEBUG
-      if (family == AF_INET)
-	{
-	  zlog_debug ("Old nexthops");
-	  nexthops_print (rinfo_old->nexthops);
-	  zlog_debug ("New nexthops");
-	  nexthops_print (rinfo_new->nexthops);
-	}
-      else if (family == AF_INET6)
-	{
-	  zlog_debug ("Old nexthops");
-	  nexthops6_print (rinfo_old->nexthops6);
-	  zlog_debug ("New nexthops");
-	  nexthops6_print (rinfo_new->nexthops6);
-	}
-#endif /* EXTREME_DEBUG */
-      isis_route_info_merge (rinfo_new, rinfo_old, family);
-      isis_route_info_delete (rinfo_new);
-      route_info = rinfo_old;
-      UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC);
+        zlog_debug ("ISIS-Rte (%s) route created: %s", area->area_tag, buff);
+      route_info = rinfo_new;
+      UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
     }
   else
     {
-      if (isis_route_info_prefer_new (rinfo_new, rinfo_old))
-	{
-	  if (isis->debugs & DEBUG_RTE_EVENTS)
-	    zlog_debug ("ISIS-Rte (%s) route changed: %s", area->area_tag,
-			buff);
-	  isis_route_info_delete (rinfo_old);
-	  route_info = rinfo_new;
-	}
+      if (isis->debugs & DEBUG_RTE_EVENTS)
+        zlog_debug ("ISIS-Rte (%s) route already exists: %s", area->area_tag,
+                   buff);
+      if (isis_route_info_same (rinfo_new, rinfo_old, family))
+        {
+          if (isis->debugs & DEBUG_RTE_EVENTS)
+            zlog_debug ("ISIS-Rte (%s) route unchanged: %s", area->area_tag,
+                        buff);
+          isis_route_info_delete (rinfo_new);
+          route_info = rinfo_old;
+        }
       else
-	{
-	  if (isis->debugs & DEBUG_RTE_EVENTS)
-	    zlog_debug ("ISIS-Rte (%s) route rejected: %s", area->area_tag,
-			buff);
-	  isis_route_info_delete (rinfo_new);
-	  route_info = rinfo_old;
-	}
+        {
+          if (isis->debugs & DEBUG_RTE_EVENTS)
+            zlog_debug ("ISIS-Rte (%s) route changed: %s", area->area_tag,
+                        buff);
+          isis_route_info_delete (rinfo_old);
+          route_info = rinfo_new;
+          UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
+        }
     }
 
   SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ACTIVE);
@@ -570,7 +498,7 @@
       return;
     }
 
-  if (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC))
+  if (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
     {
       UNSET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
       if (isis->debugs & DEBUG_RTE_EVENTS)
@@ -600,10 +528,12 @@
       if (isis->debugs & DEBUG_RTE_EVENTS)
 	{
 	  prefix2str (&rnode->p, (char *) buff, BUFSIZ);
-	  zlog_debug ("ISIS-Rte (%s): route validate: %s %s %s",
+	  zlog_debug ("ISIS-Rte (%s): route validate: %s %s %s %s",
 		      area->area_tag,
-		      (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC) ?
-		      "sync'ed" : "nosync"),
+		      (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED) ?
+		      "synced" : "not-synced"),
+		      (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC) ?
+		      "resync" : "not-resync"),
 		      (CHECK_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE) ?
 		      "active" : "inactive"), buff);
 	}
@@ -706,41 +636,55 @@
 
 /* Walk through route tables and propagate necessary changes into RIB. In case
  * of L1L2 area, level tables have to be merged at first. */
-int
-isis_route_validate (struct thread *thread)
+void
+isis_route_validate (struct isis_area *area)
 {
-  struct isis_area *area;
-
-  area = THREAD_ARG (thread);
+  struct listnode *node;
+  struct isis_circuit *circuit;
 
   if (area->is_type == IS_LEVEL_1)
-    { 
-      isis_route_validate_table (area, area->route_table[0]);
-      goto validate_ipv6;
-    }
-  if (area->is_type == IS_LEVEL_2)
-    {
-      isis_route_validate_table (area, area->route_table[1]);
-      goto validate_ipv6;
-    }
+    isis_route_validate_table (area, area->route_table[0]);
+  else if (area->is_type == IS_LEVEL_2)
+    isis_route_validate_table (area, area->route_table[1]);
+  else
+    isis_route_validate_merge (area, AF_INET);
 
-  isis_route_validate_merge (area, AF_INET);
-
-validate_ipv6:
 #ifdef HAVE_IPV6
   if (area->is_type == IS_LEVEL_1)
-    {
-      isis_route_validate_table (area, area->route_table6[0]);
-      return ISIS_OK;
-    }
-  if (area->is_type == IS_LEVEL_2)
-    {
-      isis_route_validate_table (area, area->route_table6[1]);
-      return ISIS_OK;
-    }
-
-  isis_route_validate_merge (area, AF_INET6);
+    isis_route_validate_table (area, area->route_table6[0]);
+  else if (area->is_type == IS_LEVEL_2)
+    isis_route_validate_table (area, area->route_table6[1]);
+  else
+    isis_route_validate_merge (area, AF_INET6);
 #endif
 
-  return ISIS_OK;
+  /* walk all circuits and reset any spf specific flags */
+  for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
+    UNSET_FLAG(circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF);
+
+  return;
+}
+
+void
+isis_route_invalidate_table (struct isis_area *area, struct route_table *table)
+{
+  struct route_node *rode;
+  struct isis_route_info *rinfo;
+  for (rode = route_top (table); rode; rode = route_next (rode))
+    {
+      if (rode->info == NULL)
+        continue;
+      rinfo = rode->info;
+
+      UNSET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
+    }
+}
+
+void
+isis_route_invalidate (struct isis_area *area)
+{
+  if (area->is_type & IS_LEVEL_1)
+    isis_route_invalidate_table (area, area->route_table[0]);
+  if (area->is_type & IS_LEVEL_2)
+    isis_route_invalidate_table (area, area->route_table[1]);
 }
diff --git a/isisd/isis_route.h b/isisd/isis_route.h
index 4eac79b..5adea22 100644
--- a/isisd/isis_route.h
+++ b/isisd/isis_route.h
@@ -30,6 +30,7 @@
 {
   unsigned int ifindex;
   struct in6_addr ip6;
+  struct in6_addr router_address6;
   unsigned int lock;
 };
 #endif /* HAVE_IPV6 */
@@ -38,13 +39,15 @@
 {
   unsigned int ifindex;
   struct in_addr ip;
+  struct in_addr router_address;
   unsigned int lock;
 };
 
 struct isis_route_info
 {
-#define ISIS_ROUTE_FLAG_ZEBRA_SYNC 0x01
-#define ISIS_ROUTE_FLAG_ACTIVE     0x02
+#define ISIS_ROUTE_FLAG_ACTIVE       0x01  /* active route for the prefix */
+#define ISIS_ROUTE_FLAG_ZEBRA_SYNCED 0x02  /* set when route synced to zebra */
+#define ISIS_ROUTE_FLAG_ZEBRA_RESYNC 0x04  /* set when route needs to sync */
   u_char flag;
   u_int32_t cost;
   u_int32_t depth;
@@ -59,6 +62,9 @@
 					   struct list *adjacencies,
 					   struct isis_area *area, int level);
 
-int isis_route_validate (struct thread *thread);
+void isis_route_validate (struct isis_area *area);
+void isis_route_invalidate_table (struct isis_area *area,
+                                  struct route_table *table);
+void isis_route_invalidate (struct isis_area *area);
 
 #endif /* _ZEBRA_ISIS_ROUTE_H */
diff --git a/isisd/isis_routemap.c b/isisd/isis_routemap.c
index cff0fa3..558d391 100644
--- a/isisd/isis_routemap.c
+++ b/isisd/isis_routemap.c
@@ -35,6 +35,7 @@
 
 #include "isis_constants.h"
 #include "isis_common.h"
+#include "isis_flags.h"
 #include "dict.h"
 #include "isisd.h"
 #include "isis_misc.h"
diff --git a/isisd/isis_spf.c b/isisd/isis_spf.c
index 5d0b161..198104a 100644
--- a/isisd/isis_spf.c
+++ b/isisd/isis_spf.c
@@ -36,6 +36,7 @@
 
 #include "isis_constants.h"
 #include "isis_common.h"
+#include "isis_flags.h"
 #include "dict.h"
 #include "isisd.h"
 #include "isis_misc.h"
@@ -49,10 +50,6 @@
 #include "isis_route.h"
 #include "isis_csm.h"
 
-extern struct isis *isis;
-extern struct thread_master *master;
-extern struct host host;
-
 int isis_run_spf_l1 (struct thread *thread);
 int isis_run_spf_l2 (struct thread *thread);
 
@@ -113,7 +110,6 @@
   return;
 }
 
-#ifdef EXTREME_DEBUG
 static const char *
 vtype2string (enum vertextype vtype)
 {
@@ -164,12 +160,12 @@
     {
     case VTYPE_PSEUDO_IS:
     case VTYPE_PSEUDO_TE_IS:
-      return rawlspid_print (vertex->N.id);
+      return print_sys_hostname (vertex->N.id);
       break;
     case VTYPE_NONPSEUDO_IS:
     case VTYPE_NONPSEUDO_TE_IS:
     case VTYPE_ES:
-      return sysid_print (vertex->N.id);
+      return print_sys_hostname (vertex->N.id);
       break;
     case VTYPE_IPREACH_INTERNAL:
     case VTYPE_IPREACH_EXTERNAL:
@@ -186,77 +182,6 @@
 
   return (char *) buff;
 }
-#endif /* EXTREME_DEBUG */
-
-static struct isis_spftree *
-isis_spftree_new ()
-{
-  struct isis_spftree *tree;
-
-  tree = XCALLOC (MTYPE_ISIS_SPFTREE, sizeof (struct isis_spftree));
-  if (tree == NULL)
-    {
-      zlog_err ("ISIS-Spf: isis_spftree_new Out of memory!");
-      return NULL;
-    }
-
-  tree->tents = list_new ();
-  tree->paths = list_new ();
-  return tree;
-}
-
-static void
-isis_vertex_del (struct isis_vertex *vertex)
-{
-  list_delete (vertex->Adj_N);
-
-  XFREE (MTYPE_ISIS_VERTEX, vertex);
-
-  return;
-}
-
-#if 0 /* HT: Not used yet. */
-static void
-isis_spftree_del (struct isis_spftree *spftree)
-{
-  spftree->tents->del = (void (*)(void *)) isis_vertex_del;
-  list_delete (spftree->tents);
-
-  spftree->paths->del = (void (*)(void *)) isis_vertex_del;
-  list_delete (spftree->paths);
-
-  XFREE (MTYPE_ISIS_SPFTREE, spftree);
-
-  return;
-}
-#endif 
-
-void
-spftree_area_init (struct isis_area *area)
-{
-  if ((area->is_type & IS_LEVEL_1) && area->spftree[0] == NULL)
-    {
-      area->spftree[0] = isis_spftree_new ();
-#ifdef HAVE_IPV6
-      area->spftree6[0] = isis_spftree_new ();
-#endif
-
-      /*    thread_add_timer (master, isis_run_spf_l1, area, 
-         isis_jitter (PERIODIC_SPF_INTERVAL, 10)); */
-    }
-
-  if ((area->is_type & IS_LEVEL_2) && area->spftree[1] == NULL)
-    {
-      area->spftree[1] = isis_spftree_new ();
-#ifdef HAVE_IPV6
-      area->spftree6[1] = isis_spftree_new ();
-#endif
-      /*    thread_add_timer (master, isis_run_spf_l2, area, 
-         isis_jitter (PERIODIC_SPF_INTERVAL, 10)); */
-    }
-
-  return;
-}
 
 static struct isis_vertex *
 isis_vertex_new (void *id, enum vertextype vtype)
@@ -297,38 +222,225 @@
     }
 
   vertex->Adj_N = list_new ();
+  vertex->parents = list_new ();
+  vertex->children = list_new ();
 
   return vertex;
 }
 
+static void
+isis_vertex_del (struct isis_vertex *vertex)
+{
+  list_delete (vertex->Adj_N);
+  vertex->Adj_N = NULL;
+  list_delete (vertex->parents);
+  vertex->parents = NULL;
+  list_delete (vertex->children);
+  vertex->children = NULL;
+
+  memset(vertex, 0, sizeof(struct isis_vertex));
+  XFREE (MTYPE_ISIS_VERTEX, vertex);
+
+  return;
+}
+
+static void
+isis_vertex_adj_del (struct isis_vertex *vertex, struct isis_adjacency *adj)
+{
+  struct listnode *node, *nextnode;
+  if (!vertex)
+    return;
+  for (node = listhead (vertex->Adj_N); node; node = nextnode)
+  {
+    nextnode = listnextnode(node);
+    if (listgetdata(node) == adj)
+      list_delete_node(vertex->Adj_N, node);
+  }
+  return;
+}
+
+struct isis_spftree *
+isis_spftree_new (struct isis_area *area)
+{
+  struct isis_spftree *tree;
+
+  tree = XCALLOC (MTYPE_ISIS_SPFTREE, sizeof (struct isis_spftree));
+  if (tree == NULL)
+    {
+      zlog_err ("ISIS-Spf: isis_spftree_new Out of memory!");
+      return NULL;
+    }
+
+  tree->tents = list_new ();
+  tree->paths = list_new ();
+  tree->area = area;
+  tree->last_run_timestamp = 0;
+  tree->last_run_duration = 0;
+  tree->runcount = 0;
+  tree->pending = 0;
+  return tree;
+}
+
+void
+isis_spftree_del (struct isis_spftree *spftree)
+{
+  THREAD_TIMER_OFF (spftree->t_spf);
+
+  spftree->tents->del = (void (*)(void *)) isis_vertex_del;
+  list_delete (spftree->tents);
+  spftree->tents = NULL;
+
+  spftree->paths->del = (void (*)(void *)) isis_vertex_del;
+  list_delete (spftree->paths);
+  spftree->paths = NULL;
+
+  XFREE (MTYPE_ISIS_SPFTREE, spftree);
+
+  return;
+}
+
+void
+isis_spftree_adj_del (struct isis_spftree *spftree, struct isis_adjacency *adj)
+{
+  struct listnode *node;
+  if (!adj)
+    return;
+  for (node = listhead (spftree->tents); node; node = listnextnode (node))
+    isis_vertex_adj_del (listgetdata (node), adj);
+  for (node = listhead (spftree->paths); node; node = listnextnode (node))
+    isis_vertex_adj_del (listgetdata (node), adj);
+  return;
+}
+
+void
+spftree_area_init (struct isis_area *area)
+{
+  if (area->is_type & IS_LEVEL_1)
+  {
+    if (area->spftree[0] == NULL)
+      area->spftree[0] = isis_spftree_new (area);
+#ifdef HAVE_IPV6
+    if (area->spftree6[0] == NULL)
+      area->spftree6[0] = isis_spftree_new (area);
+#endif
+  }
+
+  if (area->is_type & IS_LEVEL_2)
+  {
+    if (area->spftree[1] == NULL)
+      area->spftree[1] = isis_spftree_new (area);
+#ifdef HAVE_IPV6
+    if (area->spftree6[1] == NULL)
+      area->spftree6[1] = isis_spftree_new (area);
+#endif
+  }
+
+  return;
+}
+
+void
+spftree_area_del (struct isis_area *area)
+{
+  if (area->is_type & IS_LEVEL_1)
+  {
+    if (area->spftree[0] != NULL)
+    {
+      isis_spftree_del (area->spftree[0]);
+      area->spftree[0] = NULL;
+    }
+#ifdef HAVE_IPV6
+    if (area->spftree6[0])
+    {
+      isis_spftree_del (area->spftree6[0]);
+      area->spftree6[0] = NULL;
+    }
+#endif
+  }
+
+  if (area->is_type & IS_LEVEL_2)
+  {
+    if (area->spftree[1] != NULL)
+    {
+      isis_spftree_del (area->spftree[1]);
+      area->spftree[1] = NULL;
+    }
+#ifdef HAVE_IPV6
+    if (area->spftree[1] != NULL)
+    {
+      isis_spftree_del (area->spftree6[1]);
+      area->spftree6[1] = NULL;
+    }
+#endif
+  }
+
+  return;
+}
+
+void
+spftree_area_adj_del (struct isis_area *area, struct isis_adjacency *adj)
+{
+  if (area->is_type & IS_LEVEL_1)
+  {
+    if (area->spftree[0] != NULL)
+      isis_spftree_adj_del (area->spftree[0], adj);
+#ifdef HAVE_IPV6
+    if (area->spftree6[0] != NULL)
+      isis_spftree_adj_del (area->spftree6[0], adj);
+#endif
+  }
+
+  if (area->is_type & IS_LEVEL_2)
+  {
+    if (area->spftree[1] != NULL)
+      isis_spftree_adj_del (area->spftree[1], adj);
+#ifdef HAVE_IPV6
+    if (area->spftree6[1] != NULL)
+      isis_spftree_adj_del (area->spftree6[1], adj);
+#endif
+  }
+
+  return;
+}
+
+/* 
+ * Find the system LSP: returns the LSP in our LSP database 
+ * associated with the given system ID.
+ */
+static struct isis_lsp *
+isis_root_system_lsp (struct isis_area *area, int level, u_char *sysid)
+{
+  struct isis_lsp *lsp;
+  u_char lspid[ISIS_SYS_ID_LEN + 2];
+
+  memcpy (lspid, sysid, ISIS_SYS_ID_LEN);
+  LSP_PSEUDO_ID (lspid) = 0;
+  LSP_FRAGMENT (lspid) = 0;
+  lsp = lsp_search (lspid, area->lspdb[level - 1]);
+  if (lsp && lsp->lsp_header->rem_lifetime != 0)
+    return lsp;
+  return NULL;
+}
+
 /*
  * Add this IS to the root of SPT
  */
-static void
-isis_spf_add_self (struct isis_spftree *spftree, struct isis_area *area,
-		   int level)
+static struct isis_vertex *
+isis_spf_add_root (struct isis_spftree *spftree, int level, u_char *sysid)
 {
   struct isis_vertex *vertex;
   struct isis_lsp *lsp;
-  u_char lspid[ISIS_SYS_ID_LEN + 2];
 #ifdef EXTREME_DEBUG
   u_char buff[BUFSIZ];
 #endif /* EXTREME_DEBUG */
-  memcpy (lspid, isis->sysid, ISIS_SYS_ID_LEN);
-  LSP_PSEUDO_ID (lspid) = 0;
-  LSP_FRAGMENT (lspid) = 0;
 
-  lsp = lsp_search (lspid, area->lspdb[level - 1]);
-
+  lsp = isis_root_system_lsp (spftree->area, level, sysid);
   if (lsp == NULL)
     zlog_warn ("ISIS-Spf: could not find own l%d LSP!", level);
 
-  if (!area->oldmetric)
-    vertex = isis_vertex_new (isis->sysid, VTYPE_NONPSEUDO_TE_IS);
+  if (!spftree->area->oldmetric)
+    vertex = isis_vertex_new (sysid, VTYPE_NONPSEUDO_TE_IS);
   else
-    vertex = isis_vertex_new (isis->sysid, VTYPE_NONPSEUDO_IS);
-
-  vertex->lsp = lsp;
+    vertex = isis_vertex_new (sysid, VTYPE_NONPSEUDO_IS);
 
   listnode_add (spftree->paths, vertex);
 
@@ -338,7 +450,7 @@
 	      vertex->depth, vertex->d_N);
 #endif /* EXTREME_DEBUG */
 
-  return;
+  return vertex;
 }
 
 static struct isis_vertex *
@@ -390,26 +502,40 @@
  */
 static struct isis_vertex *
 isis_spf_add2tent (struct isis_spftree *spftree, enum vertextype vtype,
-		   void *id, struct isis_adjacency *adj, u_int32_t cost,
-		   int depth, int family)
+		   void *id, uint32_t cost, int depth, int family,
+		   struct isis_adjacency *adj, struct isis_vertex *parent)
 {
   struct isis_vertex *vertex, *v;
   struct listnode *node;
+  struct isis_adjacency *parent_adj;
 #ifdef EXTREME_DEBUG
   u_char buff[BUFSIZ];
 #endif
 
+  assert (isis_find_vertex (spftree->paths, id, vtype) == NULL);
+  assert (isis_find_vertex (spftree->tents, id, vtype) == NULL);
   vertex = isis_vertex_new (id, vtype);
   vertex->d_N = cost;
   vertex->depth = depth;
 
-  if (adj)
+  if (parent) {
+    listnode_add (vertex->parents, parent);
+    if (listnode_lookup (parent->children, vertex) == NULL)
+      listnode_add (parent->children, vertex);
+  }
+
+  if (parent && parent->Adj_N && listcount(parent->Adj_N) > 0) {
+    for (ALL_LIST_ELEMENTS_RO (parent->Adj_N, node, parent_adj))
+      listnode_add (vertex->Adj_N, parent_adj);
+  } else if (adj) {
     listnode_add (vertex->Adj_N, adj);
+  }
 
 #ifdef EXTREME_DEBUG
-  zlog_debug ("ISIS-Spf: add to TENT  %s %s depth %d dist %d",
+  zlog_debug ("ISIS-Spf: add to TENT %s %s %s depth %d dist %d adjcount %d",
+              print_sys_hostname (vertex->N.id),
 	      vtype2string (vertex->type), vid2string (vertex, buff),
-	      vertex->depth, vertex->d_N);
+	      vertex->depth, vertex->d_N, listcount(vertex->Adj_N));
 #endif /* EXTREME_DEBUG */
 
   if (list_isempty (spftree->tents))
@@ -417,8 +543,8 @@
       listnode_add (spftree->tents, vertex);
       return vertex;
     }
-  
-  /* XXX: This cant use the standard ALL_LIST_ELEMENT macro */
+
+  /* XXX: This cant use the standard ALL_LIST_ELEMENTS macro */
   for (node = listhead (spftree->tents); node; node = listnextnode (node))
     {
       v = listgetdata (node);
@@ -427,35 +553,24 @@
 	  list_add_node_prev (spftree->tents, node, vertex);
 	  break;
 	}
-      else if (v->d_N == vertex->d_N)
+      else if (v->d_N == vertex->d_N && v->type > vertex->type)
 	{
 	  /*  Tie break, add according to type */
-	  while (v && v->d_N == vertex->d_N && v->type > vertex->type)
-	    {
-	      if (v->type > vertex->type)
-		{
-		  break;
-		}
-              /* XXX: this seems dubious, node is the loop iterator */
-	      node = listnextnode (node);
-	      (node) ? (v = listgetdata (node)) : (v = NULL);
-	    }
-	  list_add_node_prev (spftree->tents, node, vertex);
-	  break;
-	}
-      else if (node->next == NULL)
-	{
-	  list_add_node_next (spftree->tents, node, vertex);
+          list_add_node_prev (spftree->tents, node, vertex);
 	  break;
 	}
     }
+
+  if (node == NULL)
+      listnode_add (spftree->tents, vertex);
+
   return vertex;
 }
 
-static struct isis_vertex *
+static void
 isis_spf_add_local (struct isis_spftree *spftree, enum vertextype vtype,
-		    void *id, struct isis_adjacency *adj, u_int32_t cost,
-		    int family)
+		    void *id, struct isis_adjacency *adj, uint32_t cost,
+		    int family, struct isis_vertex *parent)
 {
   struct isis_vertex *vertex;
 
@@ -471,40 +586,65 @@
 	  /*       d) */
 	  if (listcount (vertex->Adj_N) > ISIS_MAX_PATH_SPLITS)
 	    remove_excess_adjs (vertex->Adj_N);
+	  if (parent && (listnode_lookup (vertex->parents, parent) == NULL))
+	    listnode_add (vertex->parents, parent);
+	  if (parent && (listnode_lookup (parent->children, vertex) == NULL))
+	    listnode_add (parent->children, vertex);
+	  return;
 	}
-      /*         f) */
-      else if (vertex->d_N > cost)
+      else if (vertex->d_N < cost)
 	{
-	  listnode_delete (spftree->tents, vertex);
-	  goto add2tent;
+	  /*       e) do nothing */
+	  return;
 	}
-      /*       e) do nothing */
-      return vertex;
+      else {  /* vertex->d_N > cost */
+	  /*         f) */
+	  struct listnode *pnode, *pnextnode;
+	  struct isis_vertex *pvertex;
+	  listnode_delete (spftree->tents, vertex);
+	  assert (listcount (vertex->children) == 0);
+	  for (ALL_LIST_ELEMENTS (vertex->parents, pnode, pnextnode, pvertex))
+	    listnode_delete(pvertex->children, vertex);
+	  isis_vertex_del (vertex);
+      }
     }
 
-add2tent:
-  return isis_spf_add2tent (spftree, vtype, id, adj, cost, 1, family);
+  isis_spf_add2tent (spftree, vtype, id, cost, 1, family, adj, parent);
+  return;
 }
 
 static void
 process_N (struct isis_spftree *spftree, enum vertextype vtype, void *id,
-	   u_int16_t dist, u_int16_t depth, struct isis_adjacency *adj,
-	   int family)
+	   uint32_t dist, uint16_t depth, int family,
+	   struct isis_vertex *parent)
 {
   struct isis_vertex *vertex;
 #ifdef EXTREME_DEBUG
   u_char buff[255];
 #endif
 
+  assert (spftree && parent);
+
+  /* RFC3787 section 5.1 */
+  if (spftree->area->newmetric == 1)
+    {
+      if (dist > MAX_WIDE_PATH_METRIC)
+        return;
+    }
   /* C.2.6 b)    */
-  if (dist > MAX_PATH_METRIC)
-    return;
+  else if (spftree->area->oldmetric == 1)
+    {
+      if (dist > MAX_NARROW_PATH_METRIC)
+        return;
+    }
+
   /*       c)    */
   vertex = isis_find_vertex (spftree->paths, id, vtype);
   if (vertex)
     {
 #ifdef EXTREME_DEBUG
-      zlog_debug ("ISIS-Spf: process_N  %s %s dist %d already found from PATH",
+      zlog_debug ("ISIS-Spf: process_N %s %s %s dist %d already found from PATH",
+	          print_sys_hostname (vertex->N.id),
 		  vtype2string (vtype), vid2string (vertex, buff), dist);
 #endif /* EXTREME_DEBUG */
       assert (dist >= vertex->d_N);
@@ -517,16 +657,26 @@
     {
       /*        1) */
 #ifdef EXTREME_DEBUG
-      zlog_debug ("ISIS-Spf: process_N  %s %s dist %d",
-		  vtype2string (vtype), vid2string (vertex, buff), dist);
+      zlog_debug ("ISIS-Spf: process_N %s %s %s dist %d parent %s adjcount %d",
+	          print_sys_hostname (vertex->N.id),
+                  vtype2string (vtype), vid2string (vertex, buff), dist,
+                  (parent ? print_sys_hostname (parent->N.id) : "null"),
+                  (parent ? listcount (parent->Adj_N) : 0));
 #endif /* EXTREME_DEBUG */
       if (vertex->d_N == dist)
 	{
-	  if (adj)
-	    listnode_add (vertex->Adj_N, adj);
+	  struct listnode *node;
+	  struct isis_adjacency *parent_adj;
+	  for (ALL_LIST_ELEMENTS_RO (parent->Adj_N, node, parent_adj))
+	    if (listnode_lookup(vertex->Adj_N, parent_adj) == NULL)
+	      listnode_add (vertex->Adj_N, parent_adj);
 	  /*      2) */
 	  if (listcount (vertex->Adj_N) > ISIS_MAX_PATH_SPLITS)
 	    remove_excess_adjs (vertex->Adj_N);
+	  if (listnode_lookup (vertex->parents, parent) == NULL)
+	    listnode_add (vertex->parents, parent);
+	  if (listnode_lookup (parent->children, vertex) == NULL)
+	    listnode_add (parent->children, vertex);
 	  /*      3) */
 	  return;
 	}
@@ -537,11 +687,23 @@
 	}
       else
 	{
+	  struct listnode *pnode, *pnextnode;
+	  struct isis_vertex *pvertex;
 	  listnode_delete (spftree->tents, vertex);
+	  assert (listcount (vertex->children) == 0);
+	  for (ALL_LIST_ELEMENTS (vertex->parents, pnode, pnextnode, pvertex))
+	    listnode_delete(pvertex->children, vertex);
+	  isis_vertex_del (vertex);
 	}
     }
 
-  isis_spf_add2tent (spftree, vtype, id, adj, dist, depth, family);
+#ifdef EXTREME_DEBUG
+  zlog_debug ("ISIS-Spf: process_N add2tent %s %s dist %d parent %s",
+              print_sys_hostname(id), vtype2string (vtype), dist,
+              (parent ? print_sys_hostname (parent->N.id) : "null"));
+#endif /* EXTREME_DEBUG */
+
+  isis_spf_add2tent (spftree, vtype, id, dist, depth, family, NULL, parent);
   return;
 }
 
@@ -551,10 +713,10 @@
 static int
 isis_spf_process_lsp (struct isis_spftree *spftree, struct isis_lsp *lsp,
 		      uint32_t cost, uint16_t depth, int family,
-		      struct isis_adjacency *adj)
+		      u_char *root_sysid, struct isis_vertex *parent)
 {
   struct listnode *node, *fragnode = NULL;
-  u_int16_t dist;
+  uint32_t dist;
   struct is_neigh *is_neigh;
   struct te_is_neigh *te_is_neigh;
   struct ipv4_reachability *ipreach;
@@ -564,114 +726,121 @@
 #ifdef HAVE_IPV6
   struct ipv6_reachability *ip6reach;
 #endif /* HAVE_IPV6 */
+  static const u_char null_sysid[ISIS_SYS_ID_LEN];
 
-  if (lsp->tlv_data.nlpids == NULL || !speaks (lsp->tlv_data.nlpids, family))
+  if (!speaks (lsp->tlv_data.nlpids, family))
     return ISIS_OK;
 
 lspfragloop:
   if (lsp->lsp_header->seq_num == 0)
     {
-      zlog_warn ("isis_spf_process_lsp(): lsp with 0 seq_num"
-		 " - do not process");
+      zlog_warn ("isis_spf_process_lsp(): lsp with 0 seq_num - ignore");
       return ISIS_WARNING;
     }
 
-  if (!ISIS_MASK_LSP_OL_BIT (lsp->lsp_header->lsp_bits))
-    {
-      if (lsp->tlv_data.is_neighs)
-	{
-          for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.is_neighs, node, is_neigh))
-	    {
-	      /* C.2.6 a) */
-	      /* Two way connectivity */
-	      if (!memcmp (is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN))
-		continue;
-	      dist = cost + is_neigh->metrics.metric_default;
-	      vtype = LSP_PSEUDO_ID (is_neigh->neigh_id) ? VTYPE_PSEUDO_IS
-		: VTYPE_NONPSEUDO_IS;
-	      process_N (spftree, vtype, (void *) is_neigh->neigh_id, dist,
-			 depth + 1, adj, family);
-	    }
-	}
-      if (lsp->tlv_data.te_is_neighs)
-	{
-	  for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.te_is_neighs, node,
-				     te_is_neigh))
-	    {
-	      uint32_t metric;
-	      if (!memcmp (te_is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN))
-		continue;
-	      memcpy (&metric, te_is_neigh->te_metric, 3);
-	      dist = cost + ntohl (metric << 8);
-	      vtype = LSP_PSEUDO_ID (te_is_neigh->neigh_id) ? VTYPE_PSEUDO_TE_IS
-		: VTYPE_NONPSEUDO_TE_IS;
-	      process_N (spftree, vtype, (void *) te_is_neigh->neigh_id, dist,
-			 depth + 1, adj, family);
-	    }
-	}
-      if (family == AF_INET && lsp->tlv_data.ipv4_int_reachs)
-	{
-	  prefix.family = AF_INET;
-          for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_int_reachs, 
-                                     node, ipreach))
-	    {
-	      dist = cost + ipreach->metrics.metric_default;
-	      vtype = VTYPE_IPREACH_INTERNAL;
-	      prefix.u.prefix4 = ipreach->prefix;
-	      prefix.prefixlen = ip_masklen (ipreach->mask);
-	      process_N (spftree, vtype, (void *) &prefix, dist, depth + 1,
-			 adj, family);
-	    }
-	}
+#ifdef EXTREME_DEBUG
+      zlog_debug ("ISIS-Spf: process_lsp %s", print_sys_hostname(lsp->lsp_header->lsp_id));
+#endif /* EXTREME_DEBUG */
 
-      if (family == AF_INET && lsp->tlv_data.ipv4_ext_reachs)
-	{
-	  prefix.family = AF_INET;
-          for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_ext_reachs,
-                                     node, ipreach))
-	    {
-	      dist = cost + ipreach->metrics.metric_default;
-	      vtype = VTYPE_IPREACH_EXTERNAL;
-	      prefix.u.prefix4 = ipreach->prefix;
-	      prefix.prefixlen = ip_masklen (ipreach->mask);
-	      process_N (spftree, vtype, (void *) &prefix, dist, depth + 1,
-			 adj, family);
-	    }
-	}
-      if (family == AF_INET && lsp->tlv_data.te_ipv4_reachs)
-	{
-	  prefix.family = AF_INET;
-	  for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.te_ipv4_reachs,
-				     node, te_ipv4_reach))
-	    {
-	      dist = cost + ntohl (te_ipv4_reach->te_metric);
-	      vtype = VTYPE_IPREACH_TE;
-	      prefix.u.prefix4 = newprefix2inaddr (&te_ipv4_reach->prefix_start,
-						   te_ipv4_reach->control);
-	      prefix.prefixlen = (te_ipv4_reach->control & 0x3F);
-	      process_N (spftree, vtype, (void *) &prefix, dist, depth + 1,
-			 adj, family);
-	    }
-	}
-#ifdef HAVE_IPV6
-      if (family == AF_INET6 && lsp->tlv_data.ipv6_reachs)
-	{
-	  prefix.family = AF_INET6;
-          for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv6_reachs, 
-                                     node, ip6reach))
-	    {
-	      dist = cost + ip6reach->metric;
-	      vtype = (ip6reach->control_info & CTRL_INFO_DISTRIBUTION) ?
-		VTYPE_IP6REACH_EXTERNAL : VTYPE_IP6REACH_INTERNAL;
-	      prefix.prefixlen = ip6reach->prefix_len;
-	      memcpy (&prefix.u.prefix6.s6_addr, ip6reach->prefix,
-		      PSIZE (ip6reach->prefix_len));
-	      process_N (spftree, vtype, (void *) &prefix, dist, depth + 1,
-			 adj, family);
-	    }
-	}
-#endif /* HAVE_IPV6 */
+  if (!ISIS_MASK_LSP_OL_BIT (lsp->lsp_header->lsp_bits))
+  {
+    if (lsp->tlv_data.is_neighs)
+    {
+      for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.is_neighs, node, is_neigh))
+      {
+        /* C.2.6 a) */
+        /* Two way connectivity */
+        if (!memcmp (is_neigh->neigh_id, root_sysid, ISIS_SYS_ID_LEN))
+          continue;
+        if (!memcmp (is_neigh->neigh_id, null_sysid, ISIS_SYS_ID_LEN))
+          continue;
+        dist = cost + is_neigh->metrics.metric_default;
+        vtype = LSP_PSEUDO_ID (is_neigh->neigh_id) ? VTYPE_PSEUDO_IS
+          : VTYPE_NONPSEUDO_IS;
+        process_N (spftree, vtype, (void *) is_neigh->neigh_id, dist,
+            depth + 1, family, parent);
+      }
     }
+    if (lsp->tlv_data.te_is_neighs)
+    {
+      for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.te_is_neighs, node,
+            te_is_neigh))
+      {
+        if (!memcmp (te_is_neigh->neigh_id, root_sysid, ISIS_SYS_ID_LEN))
+          continue;
+        if (!memcmp (te_is_neigh->neigh_id, null_sysid, ISIS_SYS_ID_LEN))
+          continue;
+        dist = cost + GET_TE_METRIC(te_is_neigh);
+        vtype = LSP_PSEUDO_ID (te_is_neigh->neigh_id) ? VTYPE_PSEUDO_TE_IS
+          : VTYPE_NONPSEUDO_TE_IS;
+        process_N (spftree, vtype, (void *) te_is_neigh->neigh_id, dist,
+            depth + 1, family, parent);
+      }
+    }
+  }
+
+  if (family == AF_INET && lsp->tlv_data.ipv4_int_reachs)
+  {
+    prefix.family = AF_INET;
+    for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_int_reachs, node, ipreach))
+    {
+      dist = cost + ipreach->metrics.metric_default;
+      vtype = VTYPE_IPREACH_INTERNAL;
+      prefix.u.prefix4 = ipreach->prefix;
+      prefix.prefixlen = ip_masklen (ipreach->mask);
+      apply_mask (&prefix);
+      process_N (spftree, vtype, (void *) &prefix, dist, depth + 1,
+                 family, parent);
+    }
+  }
+  if (family == AF_INET && lsp->tlv_data.ipv4_ext_reachs)
+  {
+    prefix.family = AF_INET;
+    for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv4_ext_reachs, node, ipreach))
+    {
+      dist = cost + ipreach->metrics.metric_default;
+      vtype = VTYPE_IPREACH_EXTERNAL;
+      prefix.u.prefix4 = ipreach->prefix;
+      prefix.prefixlen = ip_masklen (ipreach->mask);
+      apply_mask (&prefix);
+      process_N (spftree, vtype, (void *) &prefix, dist, depth + 1,
+                 family, parent);
+    }
+  }
+  if (family == AF_INET && lsp->tlv_data.te_ipv4_reachs)
+  {
+    prefix.family = AF_INET;
+    for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.te_ipv4_reachs,
+                               node, te_ipv4_reach))
+    {
+      dist = cost + ntohl (te_ipv4_reach->te_metric);
+      vtype = VTYPE_IPREACH_TE;
+      prefix.u.prefix4 = newprefix2inaddr (&te_ipv4_reach->prefix_start,
+                                           te_ipv4_reach->control);
+      prefix.prefixlen = (te_ipv4_reach->control & 0x3F);
+      apply_mask (&prefix);
+      process_N (spftree, vtype, (void *) &prefix, dist, depth + 1,
+                 family, parent);
+    }
+  }
+#ifdef HAVE_IPV6
+  if (family == AF_INET6 && lsp->tlv_data.ipv6_reachs)
+  {
+    prefix.family = AF_INET6;
+    for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.ipv6_reachs, node, ip6reach))
+    {
+      dist = cost + ip6reach->metric;
+      vtype = (ip6reach->control_info & CTRL_INFO_DISTRIBUTION) ?
+        VTYPE_IP6REACH_EXTERNAL : VTYPE_IP6REACH_INTERNAL;
+      prefix.prefixlen = ip6reach->prefix_len;
+      memcpy (&prefix.u.prefix6.s6_addr, ip6reach->prefix,
+              PSIZE (ip6reach->prefix_len));
+      apply_mask (&prefix);
+      process_N (spftree, vtype, (void *) &prefix, dist, depth + 1,
+                 family, parent);
+    }
+  }
+#endif /* HAVE_IPV6 */
 
   if (fragnode == NULL)
     fragnode = listhead (lsp->lspu.frags);
@@ -689,14 +858,16 @@
 
 static int
 isis_spf_process_pseudo_lsp (struct isis_spftree *spftree,
-			     struct isis_lsp *lsp, uint16_t cost,
+			     struct isis_lsp *lsp, uint32_t cost,
 			     uint16_t depth, int family,
-			     struct isis_adjacency *adj)
+			     u_char *root_sysid,
+			     struct isis_vertex *parent)
 {
   struct listnode *node, *fragnode = NULL;
   struct is_neigh *is_neigh;
   struct te_is_neigh *te_is_neigh;
   enum vertextype vtype;
+  uint32_t dist;
 
 pseudofragloop:
 
@@ -707,41 +878,36 @@
       return ISIS_WARNING;
     }
 
+#ifdef EXTREME_DEBUG
+      zlog_debug ("ISIS-Spf: process_pseudo_lsp %s",
+                  print_sys_hostname(lsp->lsp_header->lsp_id));
+#endif /* EXTREME_DEBUG */
+
+  /* RFC3787 section 4 SHOULD ignore overload bit in pseudo LSPs */
+
   if (lsp->tlv_data.is_neighs)
     for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.is_neighs, node, is_neigh))
       {
-	vtype = LSP_PSEUDO_ID (is_neigh->neigh_id) ? VTYPE_PSEUDO_IS
-	  : VTYPE_NONPSEUDO_IS;
 	/* Two way connectivity */
-	if (!memcmp (is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN))
+	if (!memcmp (is_neigh->neigh_id, root_sysid, ISIS_SYS_ID_LEN))
 	  continue;
-	if ((depth > 0 || isis_find_vertex
-	    (spftree->tents, (void *) is_neigh->neigh_id, vtype) == NULL)
-	    && isis_find_vertex (spftree->paths, (void *) is_neigh->neigh_id,
-			       vtype) == NULL)
-	  {
-	    /* C.2.5 i) */
-	    isis_spf_add2tent (spftree, vtype, is_neigh->neigh_id, adj,
-			     cost, depth, family);
-	  }
+        dist = cost + is_neigh->metrics.metric_default;
+        vtype = LSP_PSEUDO_ID (is_neigh->neigh_id) ? VTYPE_PSEUDO_IS
+          : VTYPE_NONPSEUDO_IS;
+        process_N (spftree, vtype, (void *) is_neigh->neigh_id, dist,
+            depth + 1, family, parent);
       }
   if (lsp->tlv_data.te_is_neighs)
     for (ALL_LIST_ELEMENTS_RO (lsp->tlv_data.te_is_neighs, node, te_is_neigh))
       {
-	vtype = LSP_PSEUDO_ID (te_is_neigh->neigh_id) ? VTYPE_PSEUDO_TE_IS
-	  : VTYPE_NONPSEUDO_TE_IS;
 	/* Two way connectivity */
-	if (!memcmp (te_is_neigh->neigh_id, isis->sysid, ISIS_SYS_ID_LEN))
+	if (!memcmp (te_is_neigh->neigh_id, root_sysid, ISIS_SYS_ID_LEN))
 	  continue;
-	if ((depth > 0 || isis_find_vertex
-	     (spftree->tents, (void *) te_is_neigh->neigh_id, vtype) == NULL)
-	    && isis_find_vertex (spftree->paths, (void *) te_is_neigh->neigh_id,
-				 vtype) == NULL)
-	  {
-	    /* C.2.5 i) */
-	    isis_spf_add2tent (spftree, vtype, te_is_neigh->neigh_id, adj,
-			       cost, depth, family);
-	  }
+        dist = cost + GET_TE_METRIC(te_is_neigh);
+        vtype = LSP_PSEUDO_ID (te_is_neigh->neigh_id) ? VTYPE_PSEUDO_TE_IS
+          : VTYPE_NONPSEUDO_TE_IS;
+        process_N (spftree, vtype, (void *) te_is_neigh->neigh_id, dist,
+            depth + 1, family, parent);
       }
 
   if (fragnode == NULL)
@@ -759,10 +925,10 @@
 }
 
 static int
-isis_spf_preload_tent (struct isis_spftree *spftree,
-		       struct isis_area *area, int level, int family)
+isis_spf_preload_tent (struct isis_spftree *spftree, int level,
+		       int family, u_char *root_sysid,
+		       struct isis_vertex *parent)
 {
-  struct isis_vertex *vertex;
   struct isis_circuit *circuit;
   struct listnode *cnode, *anode, *ipnode;
   struct isis_adjacency *adj;
@@ -773,15 +939,16 @@
   struct prefix prefix;
   int retval = ISIS_OK;
   u_char lsp_id[ISIS_SYS_ID_LEN + 2];
+  static u_char null_lsp_id[ISIS_SYS_ID_LEN + 2];
 #ifdef HAVE_IPV6
   struct prefix_ipv6 *ipv6;
 #endif /* HAVE_IPV6 */
 
-  for (ALL_LIST_ELEMENTS_RO (area->circuit_list, cnode, circuit))
+  for (ALL_LIST_ELEMENTS_RO (spftree->area->circuit_list, cnode, circuit))
     {
       if (circuit->state != C_STATE_UP)
 	continue;
-      if (!(circuit->circuit_is_type & level))
+      if (!(circuit->is_type & level))
 	continue;
       if (family == AF_INET && !circuit->ip_router)
 	continue;
@@ -799,8 +966,9 @@
 	    {
 	      prefix.u.prefix4 = ipv4->prefix;
 	      prefix.prefixlen = ipv4->prefixlen;
+              apply_mask (&prefix);
 	      isis_spf_add_local (spftree, VTYPE_IPREACH_INTERNAL, &prefix,
-				  NULL, 0, family);
+				  NULL, 0, family, parent);
 	    }
 	}
 #ifdef HAVE_IPV6
@@ -811,8 +979,9 @@
 	    {
 	      prefix.prefixlen = ipv6->prefixlen;
 	      prefix.u.prefix6 = ipv6->prefix;
+              apply_mask (&prefix);
 	      isis_spf_add_local (spftree, VTYPE_IP6REACH_INTERNAL,
-				  &prefix, NULL, 0, family);
+				  &prefix, NULL, 0, family, parent);
 	    }
 	}
 #endif /* HAVE_IPV6 */
@@ -832,40 +1001,41 @@
 			    level, circuit->interface->name);
 	      continue;
 	    }
-	  anode = listhead (adj_list);
-	  while (anode)
+          for (ALL_LIST_ELEMENTS_RO (adj_list, anode, adj))
 	    {
-	      adj = listgetdata (anode);
 	      if (!speaks (&adj->nlpids, family))
-		{
-		  anode = listnextnode (anode);
 		  continue;
-		}
 	      switch (adj->sys_type)
 		{
 		case ISIS_SYSTYPE_ES:
 		  isis_spf_add_local (spftree, VTYPE_ES, adj->sysid, adj,
-				      circuit->te_metric[level - 1], family);
+				      circuit->te_metric[level - 1],
+				      family, parent);
 		  break;
 		case ISIS_SYSTYPE_IS:
 		case ISIS_SYSTYPE_L1_IS:
 		case ISIS_SYSTYPE_L2_IS:
-		  vertex =
-		    isis_spf_add_local (spftree, VTYPE_NONPSEUDO_IS,
-					adj->sysid, adj,
-					circuit->te_metric[level - 1], family);
+		  isis_spf_add_local (spftree,
+                                      spftree->area->oldmetric ?
+                                      VTYPE_NONPSEUDO_IS :
+                                      VTYPE_NONPSEUDO_TE_IS,
+                                      adj->sysid, adj,
+                                      circuit->te_metric[level - 1],
+                                      family, parent);
 		  memcpy (lsp_id, adj->sysid, ISIS_SYS_ID_LEN);
 		  LSP_PSEUDO_ID (lsp_id) = 0;
 		  LSP_FRAGMENT (lsp_id) = 0;
-		  lsp = lsp_search (lsp_id, area->lspdb[level - 1]);
-		  if (!lsp)
-		    zlog_warn ("No lsp found for IS adjacency");
+		  lsp = lsp_search (lsp_id, spftree->area->lspdb[level - 1]);
+                  if (lsp == NULL || lsp->lsp_header->rem_lifetime == 0)
+                    zlog_warn ("ISIS-Spf: No LSP %s found for IS adjacency "
+                        "L%d on %s (ID %u)",
+			rawlspid_print (lsp_id), level,
+			circuit->interface->name, circuit->circuit_id);
 		  break;
 		case ISIS_SYSTYPE_UNKNOWN:
 		default:
 		  zlog_warn ("isis_spf_preload_tent unknow adj type");
 		}
-	      anode = listnextnode (anode);
 	    }
 	  list_delete (adj_list);
 	  /*
@@ -875,23 +1045,36 @@
 	    memcpy (lsp_id, circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1);
 	  else
 	    memcpy (lsp_id, circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1);
-	  lsp = lsp_search (lsp_id, area->lspdb[level - 1]);
+	  /* can happen during DR reboot */
+	  if (memcmp (lsp_id, null_lsp_id, ISIS_SYS_ID_LEN + 1) == 0)
+	    {
+	      if (isis->debugs & DEBUG_SPF_EVENTS)
+		zlog_debug ("ISIS-Spf: No L%d DR on %s (ID %d)",
+		    level, circuit->interface->name, circuit->circuit_id);
+	      continue;
+	    }
 	  adj = isis_adj_lookup (lsp_id, adjdb);
 	  /* if no adj, we are the dis or error */
 	  if (!adj && !circuit->u.bc.is_dr[level - 1])
 	    {
-	      zlog_warn ("ISIS-Spf: No adjacency found for DR");
+              zlog_warn ("ISIS-Spf: No adjacency found from root "
+                  "to L%d DR %s on %s (ID %d)",
+		  level, rawlspid_print (lsp_id),
+		  circuit->interface->name, circuit->circuit_id);
+              continue;
 	    }
-	  else if (lsp == NULL || lsp->lsp_header->rem_lifetime == 0)
+	  lsp = lsp_search (lsp_id, spftree->area->lspdb[level - 1]);
+	  if (lsp == NULL || lsp->lsp_header->rem_lifetime == 0)
 	    {
-	      zlog_warn ("ISIS-Spf: No lsp found for DR");
+	      zlog_warn ("ISIS-Spf: No lsp (%p) found from root "
+                  "to L%d DR %s on %s (ID %d)",
+		  lsp, level, rawlspid_print (lsp_id), 
+		  circuit->interface->name, circuit->circuit_id);
+              continue;
 	    }
-	  else
-	    {
-	      isis_spf_process_pseudo_lsp (spftree, lsp,
-                                           circuit->te_metric[level - 1], 0, family, adj);
-
-	    }
+	  isis_spf_process_pseudo_lsp (spftree, lsp,
+                                       circuit->te_metric[level - 1], 0,
+                                       family, root_sysid, parent);
 	}
       else if (circuit->circ_type == CIRCUIT_T_P2P)
 	{
@@ -902,28 +1085,36 @@
 	    {
 	    case ISIS_SYSTYPE_ES:
 	      isis_spf_add_local (spftree, VTYPE_ES, adj->sysid, adj,
-				  circuit->te_metric[level - 1], family);
+				  circuit->te_metric[level - 1], family,
+				  parent);
 	      break;
 	    case ISIS_SYSTYPE_IS:
 	    case ISIS_SYSTYPE_L1_IS:
 	    case ISIS_SYSTYPE_L2_IS:
 	      if (speaks (&adj->nlpids, family))
-		isis_spf_add_local (spftree, VTYPE_NONPSEUDO_IS, adj->sysid,
+		isis_spf_add_local (spftree,
+				    spftree->area->oldmetric ?
+                                    VTYPE_NONPSEUDO_IS :
+				    VTYPE_NONPSEUDO_TE_IS,
+                                    adj->sysid,
 				    adj, circuit->te_metric[level - 1],
-				    family);
+				    family, parent);
 	      break;
 	    case ISIS_SYSTYPE_UNKNOWN:
 	    default:
-	      zlog_warn ("isis_spf_preload_tent unknow adj type");
+	      zlog_warn ("isis_spf_preload_tent unknown adj type");
 	      break;
 	    }
 	}
+      else if (circuit->circ_type == CIRCUIT_T_LOOPBACK)
+	{
+          continue;
+        }
       else
 	{
 	  zlog_warn ("isis_spf_preload_tent unsupported media");
 	  retval = ISIS_WARNING;
 	}
-
     }
 
   return retval;
@@ -935,25 +1126,30 @@
  */
 static void
 add_to_paths (struct isis_spftree *spftree, struct isis_vertex *vertex,
-	      struct isis_area *area, int level)
+	      int level)
 {
-#ifdef EXTREME_DEBUG
   u_char buff[BUFSIZ];
-#endif /* EXTREME_DEBUG */
+
+  if (isis_find_vertex (spftree->paths, vertex->N.id, vertex->type))
+    return;
   listnode_add (spftree->paths, vertex);
 
 #ifdef EXTREME_DEBUG
-  zlog_debug ("ISIS-Spf: added  %s %s depth %d dist %d to PATHS",
+  zlog_debug ("ISIS-Spf: added %s %s %s depth %d dist %d to PATHS",
+              print_sys_hostname (vertex->N.id),
 	      vtype2string (vertex->type), vid2string (vertex, buff),
 	      vertex->depth, vertex->d_N);
 #endif /* EXTREME_DEBUG */
+
   if (vertex->type > VTYPE_ES)
     {
       if (listcount (vertex->Adj_N) > 0)
 	isis_route_create ((struct prefix *) &vertex->N.prefix, vertex->d_N,
-			   vertex->depth, vertex->Adj_N, area, level);
+			   vertex->depth, vertex->Adj_N, spftree->area, level);
       else if (isis->debugs & DEBUG_SPF_EVENTS)
-	zlog_debug ("ISIS-Spf: no adjacencies do not install route");
+	zlog_debug ("ISIS-Spf: no adjacencies do not install route for "
+                    "%s depth %d dist %d", vid2string (vertex, buff),
+                    vertex->depth, vertex->d_N);
     }
 
   return;
@@ -966,23 +1162,27 @@
   list_delete_all_node (spftree->tents);
   list_delete_all_node (spftree->paths);
   spftree->tents->del = spftree->paths->del = NULL;
-
   return;
 }
 
 static int
-isis_run_spf (struct isis_area *area, int level, int family)
+isis_run_spf (struct isis_area *area, int level, int family, u_char *sysid)
 {
   int retval = ISIS_OK;
   struct listnode *node;
   struct isis_vertex *vertex;
+  struct isis_vertex *root_vertex;
   struct isis_spftree *spftree = NULL;
   u_char lsp_id[ISIS_SYS_ID_LEN + 2];
   struct isis_lsp *lsp;
-  struct isis_adjacency *adj = NULL;
   struct route_table *table = NULL;
-  struct route_node *rode;
-  struct isis_route_info *rinfo;
+  struct timespec time_now;
+  unsigned long long start_time, end_time;
+
+  /* Get time that can't roll backwards. */
+  clock_gettime(CLOCK_MONOTONIC, &time_now);
+  start_time = time_now.tv_sec;
+  start_time = (start_time * 1000000) + (time_now.tv_nsec / 1000);
 
   if (family == AF_INET)
     spftree = area->spftree[level - 1];
@@ -990,8 +1190,8 @@
   else if (family == AF_INET6)
     spftree = area->spftree6[level - 1];
 #endif
-
   assert (spftree);
+  assert (sysid);
 
   /* Make all routes in current route table inactive. */
   if (family == AF_INET)
@@ -1001,71 +1201,66 @@
     table = area->route_table6[level - 1];
 #endif
 
-  for (rode = route_top (table); rode; rode = route_next (rode))
-    {
-      if (rode->info == NULL)
-        continue;
-      rinfo = rode->info;
-
-      UNSET_FLAG (rinfo->flag, ISIS_ROUTE_FLAG_ACTIVE);
-    }
+  isis_route_invalidate_table (area, table);
 
   /*
    * C.2.5 Step 0
    */
   init_spt (spftree);
   /*              a) */
-  isis_spf_add_self (spftree, area, level);
+  root_vertex = isis_spf_add_root (spftree, level, sysid);
   /*              b) */
-  retval = isis_spf_preload_tent (spftree, area, level, family);
+  retval = isis_spf_preload_tent (spftree, level, family, sysid, root_vertex);
+  if (retval != ISIS_OK)
+    {
+      zlog_warn ("ISIS-Spf: failed to load TENT SPF-root:%s", print_sys_hostname(sysid));
+      goto out;
+    }
 
   /*
    * C.2.7 Step 2
    */
   if (listcount (spftree->tents) == 0)
     {
-      zlog_warn ("ISIS-Spf: TENT is empty");
+      zlog_warn ("ISIS-Spf: TENT is empty SPF-root:%s", print_sys_hostname(sysid));
       goto out;
     }
 
   while (listcount (spftree->tents) > 0)
     {
-      /* C.2.7 a) 1) */
       node = listhead (spftree->tents);
       vertex = listgetdata (node);
 
-      /* C.2.7 a) 2) */
+#ifdef EXTREME_DEBUG
+  zlog_debug ("ISIS-Spf: get TENT node %s %s depth %d dist %d to PATHS",
+              print_sys_hostname (vertex->N.id),
+	      vtype2string (vertex->type), vertex->depth, vertex->d_N);
+#endif /* EXTREME_DEBUG */
+
+      /* Remove from tent list and add to paths list */
       list_delete_node (spftree->tents, node);
-
-      /* C.2.7 a) 3) */
-      if (isis_find_vertex (spftree->paths, vertex->N.id, vertex->type))
-	continue;
-      add_to_paths (spftree, vertex, area, level);
-
-      if (vertex->type == VTYPE_PSEUDO_IS ||
-	  vertex->type == VTYPE_NONPSEUDO_IS ||
-	  vertex->type == VTYPE_PSEUDO_TE_IS ||
-	  vertex->type == VTYPE_NONPSEUDO_TE_IS )
-	{
-	  if (listcount(vertex->Adj_N) == 0) {
-	    continue;
-	  }
-	  adj = listgetdata(vertex->Adj_N->head);
-
+      add_to_paths (spftree, vertex, level);
+      switch (vertex->type)
+        {
+	case VTYPE_PSEUDO_IS:
+	case VTYPE_NONPSEUDO_IS:
+	case VTYPE_PSEUDO_TE_IS:
+	case VTYPE_NONPSEUDO_TE_IS:
 	  memcpy (lsp_id, vertex->N.id, ISIS_SYS_ID_LEN + 1);
 	  LSP_FRAGMENT (lsp_id) = 0;
 	  lsp = lsp_search (lsp_id, area->lspdb[level - 1]);
-	  if (lsp)
+	  if (lsp && lsp->lsp_header->rem_lifetime != 0)
 	    {
 	      if (LSP_PSEUDO_ID (lsp_id))
 		{
 		  isis_spf_process_pseudo_lsp (spftree, lsp, vertex->d_N,
-					       vertex->depth, family, adj);
+					       vertex->depth, family, sysid,
+					       vertex);
 		}
 	      else
 		{
 		  isis_spf_process_lsp (spftree, lsp, vertex->d_N,
-					vertex->depth, family, adj);
+					vertex->depth, family, sysid, vertex);
 		}
 	    }
 	  else
@@ -1073,13 +1268,21 @@
 	      zlog_warn ("ISIS-Spf: No LSP found for %s",
 			 rawlspid_print (lsp_id));
 	    }
+	  break;
+	default:;
 	}
     }
 
 out:
-  thread_add_event (master, isis_route_validate, area, 0);
-  spftree->lastrun = time (NULL);
+  isis_route_validate (area);
   spftree->pending = 0;
+  spftree->runcount++;
+  spftree->last_run_timestamp = time (NULL);
+  clock_gettime(CLOCK_MONOTONIC, &time_now);
+  end_time = time_now.tv_sec;
+  end_time = (end_time * 1000000) + (time_now.tv_nsec / 1000);
+  spftree->last_run_duration = end_time - start_time;
+
 
   return retval;
 }
@@ -1094,6 +1297,7 @@
   assert (area);
 
   area->spftree[0]->t_spf = NULL;
+  area->spftree[0]->pending = 0;
 
   if (!(area->is_type & IS_LEVEL_1))
     {
@@ -1107,10 +1311,7 @@
     zlog_debug ("ISIS-Spf (%s) L1 SPF needed, periodic SPF", area->area_tag);
 
   if (area->ip_circuits)
-    retval = isis_run_spf (area, 1, AF_INET);
-
-  THREAD_TIMER_ON (master, area->spftree[0]->t_spf, isis_run_spf_l1, area,
-		   isis_jitter (PERIODIC_SPF_INTERVAL, 10));
+    retval = isis_run_spf (area, 1, AF_INET, isis->sysid);
 
   return retval;
 }
@@ -1125,6 +1326,7 @@
   assert (area);
 
   area->spftree[1]->t_spf = NULL;
+  area->spftree[1]->pending = 0;
 
   if (!(area->is_type & IS_LEVEL_2))
     {
@@ -1137,10 +1339,7 @@
     zlog_debug ("ISIS-Spf (%s) L2 SPF needed, periodic SPF", area->area_tag);
 
   if (area->ip_circuits)
-    retval = isis_run_spf (area, 2, AF_INET);
-
-  THREAD_TIMER_ON (master, area->spftree[1]->t_spf, isis_run_spf_l2, area,
-		   isis_jitter (PERIODIC_SPF_INTERVAL, 10));
+    retval = isis_run_spf (area, 2, AF_INET, isis->sysid);
 
   return retval;
 }
@@ -1148,53 +1347,40 @@
 int
 isis_spf_schedule (struct isis_area *area, int level)
 {
-  int retval = ISIS_OK;
   struct isis_spftree *spftree = area->spftree[level - 1];
-  time_t diff, now = time (NULL);
+  time_t now = time (NULL);
+  int diff = now - spftree->last_run_timestamp;
+
+  assert (diff >= 0);
+  assert (area->is_type & level);
+
+  if (isis->debugs & DEBUG_SPF_EVENTS)
+    zlog_debug ("ISIS-Spf (%s) L%d SPF schedule called, lastrun %d sec ago",
+                area->area_tag, level, diff);
 
   if (spftree->pending)
-    return retval;
-
-  diff = now - spftree->lastrun;
-
-  /* FIXME: let's wait a minute before doing the SPF */
-  if (now - isis->uptime < 60 || isis->uptime == 0)
-    {
-      if (level == 1)
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf_l1, area, 60);
-      else
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf_l2, area, 60);
-
-      spftree->pending = 1;
-      return retval;
-    }
+    return ISIS_OK;
 
   THREAD_TIMER_OFF (spftree->t_spf);
 
-  if (diff < MINIMUM_SPF_INTERVAL)
-    {
-      if (level == 1)
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf_l1, area,
-			 MINIMUM_SPF_INTERVAL - diff);
-      else
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf_l2, area,
-			 MINIMUM_SPF_INTERVAL - diff);
+  /* wait configured min_spf_interval before doing the SPF */
+  if (diff >= area->min_spf_interval[level-1])
+      return isis_run_spf (area, level, AF_INET, isis->sysid);
 
-      spftree->pending = 1;
-    }
+  if (level == 1)
+    THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf_l1, area,
+                     area->min_spf_interval[0] - diff);
   else
-    {
-      spftree->pending = 0;
-      retval = isis_run_spf (area, level, AF_INET);
-      if (level == 1)
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf_l1, area,
-			 isis_jitter (PERIODIC_SPF_INTERVAL, 10));
-      else
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf_l2, area,
-			 isis_jitter (PERIODIC_SPF_INTERVAL, 10));
-    }
+    THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf_l2, area,
+                     area->min_spf_interval[1] - diff);
 
-  return retval;
+  if (isis->debugs & DEBUG_SPF_EVENTS)
+    zlog_debug ("ISIS-Spf (%s) L%d SPF scheduled %d sec from now",
+                area->area_tag, level, area->min_spf_interval[level-1] - diff);
+
+  spftree->pending = 1;
+
+  return ISIS_OK;
 }
 
 #ifdef HAVE_IPV6
@@ -1208,11 +1394,12 @@
   assert (area);
 
   area->spftree6[0]->t_spf = NULL;
+  area->spftree6[0]->pending = 0;
 
   if (!(area->is_type & IS_LEVEL_1))
     {
       if (isis->debugs & DEBUG_SPF_EVENTS)
-	zlog_warn ("ISIS-SPF (%s) area does not share level", area->area_tag);
+        zlog_warn ("ISIS-SPF (%s) area does not share level", area->area_tag);
       return ISIS_WARNING;
     }
 
@@ -1220,10 +1407,7 @@
     zlog_debug ("ISIS-Spf (%s) L1 SPF needed, periodic SPF", area->area_tag);
 
   if (area->ipv6_circuits)
-    retval = isis_run_spf (area, 1, AF_INET6);
-
-  THREAD_TIMER_ON (master, area->spftree6[0]->t_spf, isis_run_spf6_l1, area,
-		   isis_jitter (PERIODIC_SPF_INTERVAL, 10));
+    retval = isis_run_spf (area, 1, AF_INET6, isis->sysid);
 
   return retval;
 }
@@ -1238,6 +1422,7 @@
   assert (area);
 
   area->spftree6[1]->t_spf = NULL;
+  area->spftree6[1]->pending = 0;
 
   if (!(area->is_type & IS_LEVEL_2))
     {
@@ -1250,10 +1435,7 @@
     zlog_debug ("ISIS-Spf (%s) L2 SPF needed, periodic SPF.", area->area_tag);
 
   if (area->ipv6_circuits)
-    retval = isis_run_spf (area, 2, AF_INET6);
-
-  THREAD_TIMER_ON (master, area->spftree6[1]->t_spf, isis_run_spf6_l2, area,
-		   isis_jitter (PERIODIC_SPF_INTERVAL, 10));
+    retval = isis_run_spf (area, 2, AF_INET6, isis->sysid);
 
   return retval;
 }
@@ -1263,104 +1445,110 @@
 {
   int retval = ISIS_OK;
   struct isis_spftree *spftree = area->spftree6[level - 1];
-  time_t diff, now = time (NULL);
+  time_t now = time (NULL);
+  time_t diff = now - spftree->last_run_timestamp;
+
+  assert (diff >= 0);
+  assert (area->is_type & level);
+
+  if (isis->debugs & DEBUG_SPF_EVENTS)
+    zlog_debug ("ISIS-Spf (%s) L%d SPF schedule called, lastrun %d sec ago",
+                area->area_tag, level, diff);
 
   if (spftree->pending)
-    return retval;
+    return ISIS_OK;
 
-  diff = now - spftree->lastrun;
-
-  /* FIXME: let's wait a minute before doing the SPF */
-  if (now - isis->uptime < 60 || isis->uptime == 0)
-    {
-      if (level == 1)
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf6_l1, area, 60);
-      else
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf6_l2, area, 60);
-
-      spftree->pending = 1;
-      return retval;
-    }
-  
   THREAD_TIMER_OFF (spftree->t_spf);
 
-  if (diff < MINIMUM_SPF_INTERVAL)
-    {
-      if (level == 1)
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf6_l1, area,
-			 MINIMUM_SPF_INTERVAL - diff);
-      else
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf6_l2, area,
-			 MINIMUM_SPF_INTERVAL - diff);
+  /* wait configured min_spf_interval before doing the SPF */
+  if (diff >= area->min_spf_interval[level-1])
+      return isis_run_spf (area, level, AF_INET6, isis->sysid);
 
-      spftree->pending = 1;
-    }
+  if (level == 1)
+    THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf6_l1, area,
+                     area->min_spf_interval[0] - diff);
   else
-    {
-      spftree->pending = 0;
-      retval = isis_run_spf (area, level, AF_INET6);
+    THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf6_l2, area,
+                     area->min_spf_interval[1] - diff);
 
-      if (level == 1)
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf6_l1, area,
-			 isis_jitter (PERIODIC_SPF_INTERVAL, 10));
-      else
-	THREAD_TIMER_ON (master, spftree->t_spf, isis_run_spf6_l2, area,
-			 isis_jitter (PERIODIC_SPF_INTERVAL, 10));
-    }
+  if (isis->debugs & DEBUG_SPF_EVENTS)
+    zlog_debug ("ISIS-Spf (%s) L%d SPF scheduled %d sec from now",
+                area->area_tag, level, area->min_spf_interval[level-1] - diff);
+
+  spftree->pending = 1;
 
   return retval;
 }
 #endif
 
 static void
-isis_print_paths (struct vty *vty, struct list *paths)
+isis_print_paths (struct vty *vty, struct list *paths, u_char *root_sysid)
 {
   struct listnode *node;
+  struct listnode *anode;
   struct isis_vertex *vertex;
-  struct isis_dynhn *dyn, *nh_dyn = NULL;
   struct isis_adjacency *adj;
-#if 0
   u_char buff[255];
-#endif /* 0 */
 
-  vty_out (vty, "System Id            Metric     Next-Hop"
-	   "             Interface   SNPA%s", VTY_NEWLINE);
+  vty_out (vty, "Vertex               Type         Metric "
+                "Next-Hop             Interface Parent%s", VTY_NEWLINE);
 
-  for (ALL_LIST_ELEMENTS_RO (paths, node, vertex))
-    {
-      if (vertex->type != VTYPE_NONPSEUDO_IS)
-	continue;
-      if (memcmp (vertex->N.id, isis->sysid, ISIS_SYS_ID_LEN) == 0)
-	{
-	  vty_out (vty, "%s             --%s", host.name?host.name:"",
-		   VTY_NEWLINE);
-	}
-      else
-	{
-	  dyn = dynhn_find_by_id ((u_char *) vertex->N.id);
-	  adj = listgetdata (listhead (vertex->Adj_N));
-	  if (adj)
-	    {
-	      nh_dyn = dynhn_find_by_id (adj->sysid);
-	      vty_out (vty, "%-20s %-10u %-20s %-11s %-5s%s",
-		       (dyn != NULL) ? dyn->name.name :
-		       (const u_char *)rawlspid_print ((u_char *) vertex->N.id),
-		       vertex->d_N, (nh_dyn != NULL) ? nh_dyn->name.name :
-		       (const u_char *)rawlspid_print (adj->sysid),
-		       adj->circuit->interface->name,
-		       snpa_print (adj->snpa), VTY_NEWLINE);
+  for (ALL_LIST_ELEMENTS_RO (paths, node, vertex)) {
+      if (memcmp (vertex->N.id, root_sysid, ISIS_SYS_ID_LEN) == 0) {
+	vty_out (vty, "%-20s %-12s %-6s", print_sys_hostname (root_sysid),
+	         "", "");
+	vty_out (vty, "%-30s", "");
+      } else {
+	int rows = 0;
+	vty_out (vty, "%-20s %-12s %-6u ", vid2string (vertex, buff),
+	         vtype2string (vertex->type), vertex->d_N);
+	for (ALL_LIST_ELEMENTS_RO (vertex->Adj_N, anode, adj)) {
+	  if (adj) {
+	    if (rows) {
+		vty_out (vty, "%s", VTY_NEWLINE);
+		vty_out (vty, "%-20s %-12s %-6s ", "", "", "");
 	    }
-	  else
-	    {
-	      vty_out (vty, "%s              %u %s", dyn ? dyn->name.name :
-		       (const u_char *) rawlspid_print (vertex->N.id),
-		       vertex->d_N, VTY_NEWLINE);
-	    }
+	    vty_out (vty, "%-20s %-9s ",
+		     print_sys_hostname (adj->sysid),
+		     adj->circuit->interface->name);
+	    ++rows;
+	  }
 	}
+	if (rows == 0)
+	  vty_out (vty, "%-30s ", "");
+      }
+
+      /* Print list of parents for the ECMP DAG */
+      if (listcount (vertex->parents) > 0) {
+	struct listnode *pnode;
+	struct isis_vertex *pvertex;
+	int rows = 0;
+	for (ALL_LIST_ELEMENTS_RO (vertex->parents, pnode, pvertex)) {
+	  if (rows) {
+	    vty_out (vty, "%s", VTY_NEWLINE);
+	    vty_out (vty, "%-72s", "");
+	  }
+	  vty_out (vty, "%s(%d)",
+	           vid2string (pvertex, buff), pvertex->type);
+	  ++rows;
+	}
+      } else {
+	vty_out (vty, "  NULL ");
+      }
+
 #if 0
-      vty_out (vty, "%s %s %u %s", vtype2string (vertex->type),
-	       vid2string (vertex, buff), vertex->d_N, VTY_NEWLINE);
+      if (listcount (vertex->children) > 0) {
+	  struct listnode *cnode;
+	  struct isis_vertex *cvertex;
+	  for (ALL_LIST_ELEMENTS_RO (vertex->children, cnode, cvertex)) {
+	      vty_out (vty, "%s", VTY_NEWLINE);
+	      vty_out (vty, "%-72s", "");
+	      vty_out (vty, "%s(%d) ", 
+	               vid2string (cvertex, buff), cvertex->type);
+	    }
+	}
 #endif
+      vty_out (vty, "%s", VTY_NEWLINE);
     }
 }
 
@@ -1390,7 +1578,8 @@
 	    {
 	      vty_out (vty, "IS-IS paths to level-%d routers that speak IP%s",
 		       level + 1, VTY_NEWLINE);
-	      isis_print_paths (vty, area->spftree[level]->paths);
+	      isis_print_paths (vty, area->spftree[level]->paths, isis->sysid);
+	      vty_out (vty, "%s", VTY_NEWLINE);
 	    }
 #ifdef HAVE_IPV6
 	  if (area->ipv6_circuits > 0 && area->spftree6[level]
@@ -1399,10 +1588,13 @@
 	      vty_out (vty,
 		       "IS-IS paths to level-%d routers that speak IPv6%s",
 		       level + 1, VTY_NEWLINE);
-	      isis_print_paths (vty, area->spftree6[level]->paths);
+	      isis_print_paths (vty, area->spftree6[level]->paths, isis->sysid);
+	      vty_out (vty, "%s", VTY_NEWLINE);
 	    }
 #endif /* HAVE_IPV6 */
 	}
+
+      vty_out (vty, "%s", VTY_NEWLINE);
     }
 
   return CMD_SUCCESS;
@@ -1432,7 +1624,8 @@
 	{
 	  vty_out (vty, "IS-IS paths to level-1 routers that speak IP%s",
 		   VTY_NEWLINE);
-	  isis_print_paths (vty, area->spftree[0]->paths);
+	  isis_print_paths (vty, area->spftree[0]->paths, isis->sysid);
+	  vty_out (vty, "%s", VTY_NEWLINE);
 	}
 #ifdef HAVE_IPV6
       if (area->ipv6_circuits > 0 && area->spftree6[0]
@@ -1440,9 +1633,11 @@
 	{
 	  vty_out (vty, "IS-IS paths to level-1 routers that speak IPv6%s",
 		   VTY_NEWLINE);
-	  isis_print_paths (vty, area->spftree6[0]->paths);
+	  isis_print_paths (vty, area->spftree6[0]->paths, isis->sysid);
+	  vty_out (vty, "%s", VTY_NEWLINE);
 	}
 #endif /* HAVE_IPV6 */
+      vty_out (vty, "%s", VTY_NEWLINE);
     }
 
   return CMD_SUCCESS;
@@ -1472,7 +1667,8 @@
 	{
 	  vty_out (vty, "IS-IS paths to level-2 routers that speak IP%s",
 		   VTY_NEWLINE);
-	  isis_print_paths (vty, area->spftree[1]->paths);
+	  isis_print_paths (vty, area->spftree[1]->paths, isis->sysid);
+	  vty_out (vty, "%s", VTY_NEWLINE);
 	}
 #ifdef HAVE_IPV6
       if (area->ipv6_circuits > 0 && area->spftree6[1]
@@ -1480,9 +1676,11 @@
 	{
 	  vty_out (vty, "IS-IS paths to level-2 routers that speak IPv6%s",
 		   VTY_NEWLINE);
-	  isis_print_paths (vty, area->spftree6[1]->paths);
+	  isis_print_paths (vty, area->spftree6[1]->paths, isis->sysid);
+	  vty_out (vty, "%s", VTY_NEWLINE);
 	}
 #endif /* HAVE_IPV6 */
+      vty_out (vty, "%s", VTY_NEWLINE);
     }
 
   return CMD_SUCCESS;
diff --git a/isisd/isis_spf.h b/isisd/isis_spf.h
index 6bdab2d..aa543b7 100644
--- a/isisd/isis_spf.h
+++ b/isisd/isis_spf.h
@@ -54,25 +54,33 @@
     struct prefix prefix;
   } N;
 
-  struct isis_lsp *lsp;
   u_int32_t d_N;		/* d(N) Distance from this IS      */
   u_int16_t depth;		/* The depth in the imaginary tree */
-
-  struct list *Adj_N;		/* {Adj(N)}  */
+  struct list *Adj_N;		/* {Adj(N)} next hop or neighbor list */
+  struct list *parents;         /* list of parents for ECMP */
+  struct list *children;        /* list of children used for tree dump */
 };
 
 struct isis_spftree
 {
   struct thread *t_spf;		/* spf threads */
-  time_t lastrun;		/* for scheduling */
-  int pending;			/* already scheduled */
   struct list *paths;		/* the SPT */
   struct list *tents;		/* TENT */
-
-  u_int32_t timerun;		/* statistics */
+  struct isis_area *area;       /* back pointer to area */
+  int pending;			/* already scheduled */
+  unsigned int runcount;        /* number of runs since uptime */
+  time_t last_run_timestamp;    /* last run timestamp for scheduling */
+  time_t last_run_duration;     /* last run duration in msec */
 };
 
+struct isis_spftree * isis_spftree_new (struct isis_area *area);
+void isis_spftree_del (struct isis_spftree *spftree);
+void isis_spftree_adj_del (struct isis_spftree *spftree,
+                           struct isis_adjacency *adj);
 void spftree_area_init (struct isis_area *area);
+void spftree_area_del (struct isis_area *area);
+void spftree_area_adj_del (struct isis_area *area,
+                           struct isis_adjacency *adj);
 int isis_spf_schedule (struct isis_area *area, int level);
 void isis_spf_cmds_init (void);
 #ifdef HAVE_IPV6
diff --git a/isisd/isis_tlv.c b/isisd/isis_tlv.c
index 3fc717e..bb57bd6 100644
--- a/isisd/isis_tlv.c
+++ b/isisd/isis_tlv.c
@@ -43,13 +43,6 @@
 #include "isisd/isis_pdu.h"
 #include "isisd/isis_lsp.h"
 
-extern struct isis *isis;
-
-/*
- * Prototypes.
- */
-int add_tlv (u_char, u_char, u_char *, struct stream *);
-
 void
 free_tlv (void *val)
 {
@@ -75,10 +68,10 @@
     list_delete (tlvs->es_neighs);
   if (tlvs->lsp_entries)
     list_delete (tlvs->lsp_entries);
-  if (tlvs->lan_neighs)
-    list_delete (tlvs->lan_neighs);
   if (tlvs->prefix_neighs)
     list_delete (tlvs->prefix_neighs);
+  if (tlvs->lan_neighs)
+    list_delete (tlvs->lan_neighs);
   if (tlvs->ipv4_addrs)
     list_delete (tlvs->ipv4_addrs);
   if (tlvs->ipv4_int_reachs)
@@ -93,7 +86,9 @@
   if (tlvs->ipv6_reachs)
     list_delete (tlvs->ipv6_reachs);
 #endif /* HAVE_IPV6 */
-  
+
+  memset (tlvs, 0, sizeof (struct tlvs));
+
   return;
 }
 
@@ -103,7 +98,7 @@
  */
 int
 parse_tlvs (char *areatag, u_char * stream, int size, u_int32_t * expected,
-	    u_int32_t * found, struct tlvs *tlvs)
+	    u_int32_t * found, struct tlvs *tlvs, u_int32_t *auth_tlv_offset)
 {
   u_char type, length;
   struct lan_neigh *lan_nei;
@@ -122,7 +117,7 @@
 #endif /* HAVE_IPV6 */
   u_char virtual;
   int value_len, retval = ISIS_OK;
-  u_char *pnt = stream;
+  u_char *start = stream, *pnt = stream;
 
   *found = 0;
   memset (tlvs, 0, sizeof (struct tlvs));
@@ -443,14 +438,22 @@
 	  if (*expected & TLVFLAG_AUTH_INFO)
 	    {
 	      tlvs->auth_info.type = *pnt;
-	      tlvs->auth_info.len = length-1;
+              if (length == 0)
+                {
+                  zlog_warn ("ISIS-TLV (%s): TLV (type %d, length %d) "
+                             "incorrect.", areatag, type, length);
+                  return ISIS_WARNING;
+                }
+              --length;
+	      tlvs->auth_info.len = length;
 	      pnt++;
-	      memcpy (tlvs->auth_info.passwd, pnt, length - 1);
-	     /* Fill authentication with 0 for later computation
-	      * of MD5 (RFC 5304, 2)
-	      */
-	     memset (pnt, 0, length - 1);
-	      pnt += length - 1;
+	      memcpy (tlvs->auth_info.passwd, pnt, length);
+              /* Return the authentication tlv pos for later computation
+               * of MD5 (RFC 5304, 2)
+               */
+              if (auth_tlv_offset)
+                *auth_tlv_offset += (pnt - start - 3);
+              pnt += length;
 	    }
 	  else
 	    {
@@ -734,10 +737,14 @@
 int
 add_tlv (u_char tag, u_char len, u_char * value, struct stream *stream)
 {
-
-  if (STREAM_SIZE (stream) - stream_get_endp (stream) < (unsigned) len + 2)
+  if ((stream_get_size (stream) - stream_get_endp (stream)) <
+      (((unsigned)len) + 2))
     {
-      zlog_warn ("No room for TLV of type %d", tag);
+      zlog_warn ("No room for TLV of type %d "
+                 "(total size %d available %d required %d)",
+                 tag, (int)stream_get_size (stream),
+                 (int)(stream_get_size (stream) - stream_get_endp (stream)),
+                 len+2);
       return ISIS_WARNING;
     }
 
@@ -745,7 +752,7 @@
   stream_putc (stream, len);	/* LENGTH */
   stream_put (stream, value, (int) len);	/* VALUE */
 
-#ifdef EXTREME_TLV_DEBUG
+#ifdef EXTREME_DEBUG
   zlog_debug ("Added TLV %d len %d", tag, len);
 #endif /* EXTREME DEBUG */
   return ISIS_OK;
@@ -877,7 +884,7 @@
 }
 
 int
-tlv_add_authinfo (char auth_type, char auth_len, u_char *auth_value,
+tlv_add_authinfo (u_char auth_type, u_char auth_len, u_char *auth_value,
 		  struct stream *stream)
 {
   u_char value[255];
@@ -1006,7 +1013,6 @@
       pos += IPV4_MAX_BYTELEN;
     }
 
-
   return add_tlv (IPV4_INT_REACHABILITY, pos - value, value, stream);
 }
 
@@ -1027,7 +1033,7 @@
       if (pos - value + (5 + prefix_size) > 255)
 	{
 	  retval =
-	    add_tlv (IPV4_INT_REACHABILITY, pos - value, value, stream);
+	    add_tlv (TE_IPV4_REACHABILITY, pos - value, value, stream);
 	  if (retval != ISIS_OK)
 	    return retval;
 	  pos = value;
@@ -1110,7 +1116,7 @@
   /*
    * How many times can we add full padding ?
    */
-  fullpads = (STREAM_SIZE (stream) - stream_get_endp (stream)) / 257;
+  fullpads = (stream_get_size (stream) - stream_get_endp (stream)) / 257;
   for (i = 0; i < fullpads; i++)
     {
       if (!stream_putc (stream, (u_char) PADDING))	/* TAG */
@@ -1120,7 +1126,7 @@
       stream_put (stream, NULL, 255);		/* zero padding */
     }
 
-  left = STREAM_SIZE (stream) - stream_get_endp (stream);
+  left = stream_get_size (stream) - stream_get_endp (stream);
 
   if (left < 2)
     return ISIS_OK;
diff --git a/isisd/isis_tlv.h b/isisd/isis_tlv.h
index fc9f35f..e092f4d 100644
--- a/isisd/isis_tlv.h
+++ b/isisd/isis_tlv.h
@@ -30,7 +30,7 @@
  * Name                   Value  IIH LSP SNP Status
  *                               LAN
  * ____________________________________________________________________________
- * 
+ *
  * Area Addresses             1   y   y   n  ISO10589
  * IIS Neighbors              2   n   y   n  ISO10589
  * ES Neighbors               3   n   y   n  ISO10589
@@ -39,52 +39,52 @@
  * LSP Entries                9   n   n   y  ISO10589
  * Authentication            10   y   y   y  ISO10589, RFC3567
  * Checksum                  12   y   n   y  RFC3358
- * TE IS Reachability        22   n   y   n  RFC3784
+ * TE IS Reachability        22   n   y   n  RFC5305
  * IS Alias                  24   n   y   n  RFC3786
  * IP Int. Reachability     128   n   y   n  RFC1195
  * Protocols Supported      129   y   y   n  RFC1195
  * IP Ext. Reachability     130   n   y   n  RFC1195
  * IDRPI                    131   n   y   y  RFC1195
  * IP Interface Address     132   y   y   n  RFC1195
- * TE Router ID             134   n   y   n  RFC3784
- * Extended IP Reachability 135   n   y   n  RFC3784
+ * TE Router ID             134   n   y   n  RFC5305
+ * Extended IP Reachability 135   n   y   n  RFC5305
  * Dynamic Hostname         137   n   y   n  RFC2763
- * Shared Risk Link Group   138   n   y   y  draft-ietf-isis-gmpls-extensions
+ * Shared Risk Link Group   138   n   y   y  RFC5307
  * Restart TLV              211   y   n   n  RFC3847
- * MT IS Reachability       222   n   y   n  draft-ietf-isis-wg-multi-topology
- * MT Supported             229   y   y   n  draft-ietf-isis-wg-multi-topology
- * IPv6 Interface Address   232   y   y   n  draft-ietf-isis_ipv6
- * MT IP Reachability       235   n   y   n  draft-ietf-isis-wg-multi-topology
- * IPv6 IP Reachability     236   n   y   n  draft-ietf-isis_ipv6
- * MT IPv6 IP Reachability  237   n   y   n  draft-ietf-isis-wg-multi-topology
+ * MT IS Reachability       222   n   y   n  RFC5120
+ * MT Supported             229   y   y   n  RFC5120
+ * IPv6 Interface Address   232   y   y   n  RFC5308
+ * MT IP Reachability       235   n   y   n  RFC5120
+ * IPv6 IP Reachability     236   n   y   n  RFC5308
+ * MT IPv6 IP Reachability  237   n   y   n  RFC5120
  * P2P Adjacency State      240   y   n   n  RFC3373
  * IIH Sequence Number      241   y   n   n  draft-shen-isis-iih-sequence
  * Router Capability        242   -   -   -  draft-ietf-isis-caps
  *
- * 
+ *
  * IS Reachability sub-TLVs we (should) support.
  * ____________________________________________________________________________
  * Name                           Value   Status
  * ____________________________________________________________________________
- * Administartive group (color)       3   RFC3784
- * Link Local/Remote Identifiers      4   draft-ietf-isis-gmpls-extensions
- * IPv4 interface address             6   RFC3784
- * IPv4 neighbor address              8   RFC3784
- * Maximum link bandwidth             9   RFC3784
- * Reservable link bandwidth         10   RFC3784
- * Unreserved bandwidth              11   RFC3784
- * TE Default metric                 18   RFC3784
- * Link Protection Type              20   draft-ietf-isis-gmpls-extensions
- * Interface Switching Capability    21   draft-ietf-isis-gmpls-extensions
+ * Administartive group (color)       3   RFC5305
+ * Link Local/Remote Identifiers      4   RFC5307
+ * IPv4 interface address             6   RFC5305
+ * IPv4 neighbor address              8   RFC5305
+ * Maximum link bandwidth             9   RFC5305
+ * Reservable link bandwidth         10   RFC5305
+ * Unreserved bandwidth              11   RFC5305
+ * TE Default metric                 18   RFC5305
+ * Link Protection Type              20   RFC5307
+ * Interface Switching Capability    21   RFC5307
  *
- * 
+ *
  * IP Reachability sub-TLVs we (should) support.
  * ____________________________________________________________________________
  * Name                           Value   Status
  * ____________________________________________________________________________
- * 32bit administrative tag           1   draft-ietf-isis-admin-tags
- * 64bit administrative tag           2   draft-ietf-isis-admin-tags
- * Management prefix color          117   draft-ietf-isis-wg-multi-topology
+ * 32bit administrative tag           1   RFC5130
+ * 64bit administrative tag           2   RFC5130
+ * Management prefix color          117   RFC5120
  */
 
 #define AREA_ADDRESSES            1
@@ -110,11 +110,14 @@
 #define IPV6_REACHABILITY         236
 #define WAY3_HELLO                240
 
+#define AUTH_INFO_HDRLEN          3
+
 #define IS_NEIGHBOURS_LEN (ISIS_SYS_ID_LEN + 5)
 #define LAN_NEIGHBOURS_LEN 6
 #define LSP_ENTRIES_LEN (10 + ISIS_SYS_ID_LEN)	/* FIXME: should be entry */
 #define IPV4_REACH_LEN 12
 #define IPV6_REACH_LEN 22
+#define TE_IPV4_REACH_LEN 9
 
 /* struct for neighbor */
 struct is_neigh
@@ -131,6 +134,15 @@
   u_char sub_tlvs_length;
 };
 
+/* Decode and encode three-octet metric into host byte order integer */
+#define GET_TE_METRIC(t) \
+  (((unsigned)(t)->te_metric[0]<<16) | ((t)->te_metric[1]<<8) | \
+   (t)->te_metric[2])
+#define SET_TE_METRIC(t, m) \
+  (((t)->te_metric[0] = (m) >> 16), \
+   ((t)->te_metric[1] = (m) >> 8), \
+   ((t)->te_metric[2] = (m)))
+
 /* struct for es neighbors */
 struct es_neigh
 {
@@ -213,7 +225,6 @@
   u_char prefix_len;
   u_char prefix[16];
 };
-#endif /* HAVE_IPV6 */
 
 /* bits in control_info */
 #define CTRL_INFO_DIRECTION    0x80
@@ -223,12 +234,17 @@
 #define DISTRIBUTION_INTERNAL  0
 #define DISTRIBUTION_EXTERNAL  1
 #define CTRL_INFO_SUBTLVS      0x20
+#endif /* HAVE_IPV6 */
 
 /*
  * Pointer to each tlv type, filled by parse_tlvs()
  */
 struct tlvs
 {
+  struct checksum *checksum;
+  struct hostname *hostname;
+  struct nlpids *nlpids;
+  struct te_router_id *router_id;
   struct list *area_addrs;
   struct list *is_neighs;
   struct list *te_is_neighs;
@@ -236,14 +252,10 @@
   struct list *lsp_entries;
   struct list *prefix_neighs;
   struct list *lan_neighs;
-  struct checksum *checksum;
-  struct nlpids *nlpids;
   struct list *ipv4_addrs;
   struct list *ipv4_int_reachs;
   struct list *ipv4_ext_reachs;
   struct list *te_ipv4_reachs;
-  struct hostname *hostname;
-  struct te_router_id *router_id;
 #ifdef HAVE_IPV6
   struct list *ipv6_addrs;
   struct list *ipv6_reachs;
@@ -281,7 +293,9 @@
 void init_tlvs (struct tlvs *tlvs, uint32_t expected);
 void free_tlvs (struct tlvs *tlvs);
 int parse_tlvs (char *areatag, u_char * stream, int size,
-		u_int32_t * expected, u_int32_t * found, struct tlvs *tlvs);
+		u_int32_t * expected, u_int32_t * found, struct tlvs *tlvs,
+                u_int32_t * auth_tlv_offset);
+int add_tlv (u_char, u_char, u_char *, struct stream *);
 void free_tlv (void *val);
 
 int tlv_add_area_addrs (struct list *area_addrs, struct stream *stream);
@@ -290,7 +304,7 @@
 int tlv_add_lan_neighs (struct list *lan_neighs, struct stream *stream);
 int tlv_add_nlpid (struct nlpids *nlpids, struct stream *stream);
 int tlv_add_checksum (struct checksum *checksum, struct stream *stream);
-int tlv_add_authinfo (char auth_type, char authlen, u_char *auth_value,
+int tlv_add_authinfo (u_char auth_type, u_char authlen, u_char *auth_value,
 		      struct stream *stream);
 int tlv_add_ip_addrs (struct list *ip_addrs, struct stream *stream);
 int tlv_add_in_addr (struct in_addr *, struct stream *stream, u_char tag);
diff --git a/isisd/isis_zebra.c b/isisd/isis_zebra.c
index d5ccef9..2df7462 100644
--- a/isisd/isis_zebra.c
+++ b/isisd/isis_zebra.c
@@ -36,30 +36,37 @@
 #include "isisd/dict.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
+#include "isisd/isis_flags.h"
+#include "isisd/isis_misc.h"
+#include "isisd/isis_circuit.h"
+#include "isisd/isis_tlv.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_circuit.h"
 #include "isisd/isis_csm.h"
+#include "isisd/isis_lsp.h"
 #include "isisd/isis_route.h"
 #include "isisd/isis_zebra.h"
 
 struct zclient *zclient = NULL;
 
-extern struct thread_master *master;
-extern struct isis *isis;
-
-struct in_addr router_id_zebra;
-
 /* Router-id update message from zebra. */
 static int
 isis_router_id_update_zebra (int command, struct zclient *zclient,
 			     zebra_size_t length)
 {
+  struct isis_area *area;
+  struct listnode *node;
   struct prefix router_id;
 
-  zebra_router_id_update_read (zclient->ibuf,&router_id);
-  router_id_zebra = router_id.u.prefix4;
+  zebra_router_id_update_read (zclient->ibuf, &router_id);
+  if (isis->router_id == router_id.u.prefix4.s_addr)
+    return 0;
 
-  /* FIXME: Do we react somehow? */
+  isis->router_id = router_id.u.prefix4.s_addr;
+  for (ALL_LIST_ELEMENTS_RO (isis->area_list, node, area))
+    if (listcount (area->area_addrs) > 0)
+      lsp_regenerate_schedule (area, area->is_type, 0);
+
   return 0;
 }
 
@@ -100,53 +107,28 @@
     zlog_debug ("Zebra I/F delete: %s index %d flags %ld metric %d mtu %d",
 		ifp->name, ifp->ifindex, (long)ifp->flags, ifp->metric, ifp->mtu);
 
+  isis_csm_state_change (IF_DOWN_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
 
   /* Cannot call if_delete because we should retain the pseudo interface
      in case there is configuration info attached to it. */
   if_delete_retain(ifp);
 
-  isis_csm_state_change (IF_DOWN_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
-
   ifp->ifindex = IFINDEX_INTERNAL;
 
   return 0;
 }
 
-static struct interface *
-zebra_interface_if_lookup (struct stream *s)
-{
-  char ifname_tmp[INTERFACE_NAMSIZ];
-
-  /* Read interface name. */
-  stream_get (ifname_tmp, s, INTERFACE_NAMSIZ);
-
-  /* And look it up. */
-  return if_lookup_by_name_len(ifname_tmp,
-			       strnlen(ifname_tmp, INTERFACE_NAMSIZ));
-}
-
 static int
 isis_zebra_if_state_up (int command, struct zclient *zclient,
 			zebra_size_t length)
 {
   struct interface *ifp;
 
-  ifp = zebra_interface_if_lookup (zclient->ibuf);
+  ifp = zebra_interface_state_read (zclient->ibuf);
 
-  if (!ifp)
+  if (ifp == NULL)
     return 0;
 
-  if (if_is_operative (ifp))
-    {
-      zebra_interface_if_set_value (zclient->ibuf, ifp);
-      /* HT: This is wrong actually. We can't assume that circuit exist
-       * if we delete circuit during if_state_down event. Needs rethink.
-       * TODO */
-      isis_circuit_update_params (circuit_scan_by_ifp (ifp), ifp);
-      return 0;
-    }
-
-  zebra_interface_if_set_value (zclient->ibuf, ifp);
   isis_csm_state_change (IF_UP_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
 
   return 0;
@@ -157,17 +139,17 @@
 			  zebra_size_t length)
 {
   struct interface *ifp;
+  struct isis_circuit *circuit;
 
-  ifp = zebra_interface_if_lookup (zclient->ibuf);
+  ifp = zebra_interface_state_read (zclient->ibuf);
 
   if (ifp == NULL)
     return 0;
 
-  if (if_is_operative (ifp))
-    {
-      zebra_interface_if_set_value (zclient->ibuf, ifp);
-      isis_csm_state_change (IF_DOWN_FROM_Z, circuit_scan_by_ifp (ifp), ifp);
-    }
+  circuit = isis_csm_state_change (IF_DOWN_FROM_Z, circuit_scan_by_ifp (ifp),
+                                   ifp);
+  if (circuit)
+    SET_FLAG(circuit->flags, ISIS_CIRCUIT_FLAPPED_AFTER_SPF);
 
   return 0;
 }
@@ -251,7 +233,7 @@
   struct isis_nexthop *nexthop;
   struct listnode *node;
 
-  if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC))
+  if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
     return;
 
   if (zclient->redist[ZEBRA_ROUTE_ISIS])
@@ -307,7 +289,8 @@
 
       stream_putw_at (stream, 0, stream_get_endp (stream));
       zclient_send_message(zclient);
-      SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC);
+      SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
+      UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
     }
 }
 
@@ -329,7 +312,7 @@
       prefix4.prefix = prefix->u.prefix4;
       zapi_ipv4_route (ZEBRA_IPV4_ROUTE_DELETE, zclient, &prefix4, &api);
     }
-  UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC);
+  UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
 
   return;
 }
@@ -347,7 +330,7 @@
   struct listnode *node;
   struct prefix_ipv6 prefix6;
 
-  if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC))
+  if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
     return;
 
   api.type = ZEBRA_ROUTE_ISIS;
@@ -410,7 +393,8 @@
       prefix6.prefixlen = prefix->prefixlen;
       memcpy (&prefix6.prefix, &prefix->u.prefix6, sizeof (struct in6_addr));
       zapi_ipv6_route (ZEBRA_IPV6_ROUTE_ADD, zclient, &prefix6, &api);
-      SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC);
+      SET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
+      UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_RESYNC);
     }
 
   XFREE (MTYPE_ISIS_TMP, nexthop_list);
@@ -431,7 +415,7 @@
   struct listnode *node;
   struct prefix_ipv6 prefix6;
 
-  if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC))
+  if (CHECK_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED))
     return;
 
   api.type = ZEBRA_ROUTE_ISIS;
@@ -488,7 +472,7 @@
       prefix6.prefixlen = prefix->prefixlen;
       memcpy (&prefix6.prefix, &prefix->u.prefix6, sizeof (struct in6_addr));
       zapi_ipv6_route (ZEBRA_IPV6_ROUTE_DELETE, zclient, &prefix6, &api);
-      UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNC);
+      UNSET_FLAG (route_info->flag, ISIS_ROUTE_FLAG_ZEBRA_SYNCED);
     }
 
   XFREE (MTYPE_ISIS_TMP, nexthop_list);
diff --git a/isisd/isisd.c b/isisd/isisd.c
index 20a3280..e8e3d9f 100644
--- a/isisd/isisd.c
+++ b/isisd/isisd.c
@@ -27,6 +27,7 @@
 #include "command.h"
 #include "log.h"
 #include "memory.h"
+#include "time.h"
 #include "linklist.h"
 #include "if.h"
 #include "hash.h"
@@ -38,8 +39,9 @@
 #include "isisd/include-netbsd/iso.h"
 #include "isisd/isis_constants.h"
 #include "isisd/isis_common.h"
-#include "isisd/isis_circuit.h"
 #include "isisd/isis_flags.h"
+#include "isisd/isis_circuit.h"
+#include "isisd/isis_csm.h"
 #include "isisd/isisd.h"
 #include "isisd/isis_dynhn.h"
 #include "isisd/isis_adjacency.h"
@@ -52,7 +54,6 @@
 #include "isisd/isis_route.h"
 #include "isisd/isis_zebra.h"
 #include "isisd/isis_events.h"
-#include "isisd/isis_csm.h"
 
 #ifdef TOPOLOGY_GENERATE
 #include "spgrid.h"
@@ -60,19 +61,17 @@
 #endif /* TOPOLOGY_GENERATE */
 
 struct isis *isis = NULL;
-extern struct thread_master *master;
 
 /*
  * Prototypes.
  */
-void isis_new(unsigned long);
-struct isis_area *isis_area_create(void);
 int isis_area_get(struct vty *, const char *);
 int isis_area_destroy(struct vty *, const char *);
-int area_net_title(struct vty *, const u_char *);
-int area_clear_net_title(struct vty *, const u_char *);
-int show_clns_neigh(struct vty *, char);
-void print_debug(struct vty *, int, int);
+int area_net_title(struct vty *, const char *);
+int area_clear_net_title(struct vty *, const char *);
+int show_isis_interface_common(struct vty *, const char *ifname, char);
+int show_isis_neighbor_common(struct vty *, const char *id, char);
+int clear_isis_neighbor_common(struct vty *, const char *id);
 int isis_config_write(struct vty *);
 
 
@@ -85,8 +84,8 @@
    * Default values
    */
   isis->max_area_addrs = 3;
-
   isis->process_id = process_id;
+  isis->router_id = 0;
   isis->area_list = list_new ();
   isis->init_circ_list = list_new ();
   isis->uptime = time (NULL);
@@ -94,6 +93,7 @@
 #ifdef HAVE_IPV6
   isis->nexthops6 = list_new ();
 #endif /* HAVE_IPV6 */
+  dyn_cache_init ();
   /*
    * uncomment the next line for full debugs
    */
@@ -101,7 +101,7 @@
 }
 
 struct isis_area *
-isis_area_create ()
+isis_area_create (const char *area_tag)
 {
   struct isis_area *area;
 
@@ -115,36 +115,48 @@
     area->is_type = IS_LEVEL_1;
   else
     area->is_type = IS_LEVEL_1_AND_2;
+
   /*
    * intialize the databases
    */
-  area->lspdb[0] = lsp_db_init ();
-  area->lspdb[1] = lsp_db_init ();
+  if (area->is_type & IS_LEVEL_1)
+    {
+      area->lspdb[0] = lsp_db_init ();
+      area->route_table[0] = route_table_init ();
+#ifdef HAVE_IPV6
+      area->route_table6[0] = route_table_init ();
+#endif /* HAVE_IPV6 */
+    }
+  if (area->is_type & IS_LEVEL_2)
+    {
+      area->lspdb[1] = lsp_db_init ();
+      area->route_table[1] = route_table_init ();
+#ifdef HAVE_IPV6
+      area->route_table6[1] = route_table_init ();
+#endif /* HAVE_IPV6 */
+    }
 
   spftree_area_init (area);
-  area->route_table[0] = route_table_init ();
-  area->route_table[1] = route_table_init ();
-#ifdef HAVE_IPV6
-  area->route_table6[0] = route_table_init ();
-  area->route_table6[1] = route_table_init ();
-#endif /* HAVE_IPV6 */
+
   area->circuit_list = list_new ();
   area->area_addrs = list_new ();
   THREAD_TIMER_ON (master, area->t_tick, lsp_tick, area, 1);
   flags_initialize (&area->flags);
+
   /*
    * Default values
    */
-  area->max_lsp_lifetime[0] = MAX_AGE;	/* 1200 */
-  area->max_lsp_lifetime[1] = MAX_AGE;	/* 1200 */
-  area->lsp_gen_interval[0] = LSP_GEN_INTERVAL_DEFAULT;
-  area->lsp_gen_interval[1] = LSP_GEN_INTERVAL_DEFAULT;
-  area->lsp_refresh[0] = MAX_LSP_GEN_INTERVAL;	/* 900 */
-  area->lsp_refresh[1] = MAX_LSP_GEN_INTERVAL;	/* 900 */
+  area->max_lsp_lifetime[0] = DEFAULT_LSP_LIFETIME;	/* 1200 */
+  area->max_lsp_lifetime[1] = DEFAULT_LSP_LIFETIME;	/* 1200 */
+  area->lsp_refresh[0] = DEFAULT_MAX_LSP_GEN_INTERVAL;	/* 900 */
+  area->lsp_refresh[1] = DEFAULT_MAX_LSP_GEN_INTERVAL;	/* 900 */
+  area->lsp_gen_interval[0] = DEFAULT_MIN_LSP_GEN_INTERVAL;
+  area->lsp_gen_interval[1] = DEFAULT_MIN_LSP_GEN_INTERVAL;
   area->min_spf_interval[0] = MINIMUM_SPF_INTERVAL;
   area->min_spf_interval[1] = MINIMUM_SPF_INTERVAL;
   area->dynhostname = 1;
-  area->oldmetric = 1;
+  area->oldmetric = 0;
+  area->newmetric = 1;
   area->lsp_frag_threshold = 90;
 #ifdef TOPOLOGY_GENERATE
   memcpy (area->topology_baseis, DEFAULT_TOPOLOGY_BASEIS, ISIS_SYS_ID_LEN);
@@ -153,6 +165,10 @@
   /* FIXME: Think of a better way... */
   area->min_bcast_mtu = 1497;
 
+  area->area_tag = strdup (area_tag);
+  listnode_add (isis->area_list, area);
+  area->isis = isis;
+
   return area;
 }
 
@@ -185,9 +201,7 @@
       return CMD_SUCCESS;
     }
 
-  area = isis_area_create ();
-  area->area_tag = strdup (area_tag);
-  listnode_add (isis->area_list, area);
+  area = isis_area_create (area_tag);
 
   if (isis->debugs & DEBUG_EVENTS)
     zlog_debug ("New IS-IS area instance %s", area->area_tag);
@@ -204,50 +218,100 @@
   struct isis_area *area;
   struct listnode *node, *nnode;
   struct isis_circuit *circuit;
+  struct area_addr *addr;
 
   area = isis_area_lookup (area_tag);
 
   if (area == NULL)
     {
       vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_NO_MATCH;
     }
 
   if (area->circuit_list)
     {
       for (ALL_LIST_ELEMENTS (area->circuit_list, node, nnode, circuit))
-	{
-	  /* The fact that it's in circuit_list means that it was configured */
-	  isis_csm_state_change (ISIS_DISABLE, circuit, area);
-	  isis_circuit_down (circuit);
-	  isis_circuit_deconfigure (circuit, area);
-	}
-      
+        {
+          circuit->ip_router = 0;
+#ifdef HAVE_IPV6
+          circuit->ipv6_router = 0;
+#endif
+          isis_csm_state_change (ISIS_DISABLE, circuit, area);
+        }
       list_delete (area->circuit_list);
+      area->circuit_list = NULL;
     }
-  listnode_delete (isis->area_list, area);
+
+  if (area->lspdb[0] != NULL)
+    {
+      lsp_db_destroy (area->lspdb[0]);
+      area->lspdb[0] = NULL;
+    }
+  if (area->lspdb[1] != NULL)
+    {
+      lsp_db_destroy (area->lspdb[1]);
+      area->lspdb[1] = NULL;
+    }
+
+  spftree_area_del (area);
+
+  /* invalidate and validate would delete all routes from zebra */
+  isis_route_invalidate (area);
+  isis_route_validate (area);
+
+  if (area->route_table[0])
+    {
+      route_table_finish (area->route_table[0]);
+      area->route_table[0] = NULL;
+    }
+  if (area->route_table[1])
+    {
+      route_table_finish (area->route_table[1]);
+      area->route_table[1] = NULL;
+    }
+#ifdef HAVE_IPV6
+  if (area->route_table6[0])
+    {
+      route_table_finish (area->route_table6[0]);
+      area->route_table6[0] = NULL;
+    }
+  if (area->route_table6[1])
+    {
+      route_table_finish (area->route_table6[1]);
+      area->route_table6[1] = NULL;
+    }
+#endif /* HAVE_IPV6 */
+
+  for (ALL_LIST_ELEMENTS (area->area_addrs, node, nnode, addr))
+    {
+      list_delete_node (area->area_addrs, node);
+      XFREE (MTYPE_ISIS_AREA_ADDR, addr);
+    }
+  area->area_addrs = NULL;
 
   THREAD_TIMER_OFF (area->t_tick);
-  if (area->t_remove_aged)
-    thread_cancel (area->t_remove_aged);
   THREAD_TIMER_OFF (area->t_lsp_refresh[0]);
   THREAD_TIMER_OFF (area->t_lsp_refresh[1]);
 
-  THREAD_TIMER_OFF (area->spftree[0]->t_spf);
-  THREAD_TIMER_OFF (area->spftree[1]->t_spf);
+  thread_cancel_event (master, area);
 
-  THREAD_TIMER_OFF (area->t_lsp_l1_regenerate);
-  THREAD_TIMER_OFF (area->t_lsp_l2_regenerate);
+  listnode_delete (isis->area_list, area);
+
+  free (area->area_tag);
 
   XFREE (MTYPE_ISIS_AREA, area);
 
-  isis->sysid_set=0;
+  if (listcount (isis->area_list) == 0)
+    {
+      memset (isis->sysid, 0, ISIS_SYS_ID_LEN);
+      isis->sysid_set = 0;
+    }
 
   return CMD_SUCCESS;
 }
 
 int
-area_net_title (struct vty *vty, const u_char *net_title)
+area_net_title (struct vty *vty, const char *net_title)
 {
   struct isis_area *area;
   struct area_addr *addr;
@@ -260,7 +324,7 @@
   if (!area)
     {
       vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_NO_MATCH;
     }
 
   /* We check that we are not over the maximal number of addresses */
@@ -268,7 +332,7 @@
     {
       vty_out (vty, "Maximum of area addresses (%d) already reached %s",
 	       isis->max_area_addrs, VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_NOTHING_TODO;
     }
 
   addr = XMALLOC (MTYPE_ISIS_AREA_ADDR, sizeof (struct area_addr));
@@ -280,10 +344,18 @@
 #endif /* EXTREME_DEBUG */
   if (addr->addr_len < 8 || addr->addr_len > 20)
     {
-      zlog_warn ("area address must be at least 8..20 octets long (%d)",
-		 addr->addr_len);
+      vty_out (vty, "area address must be at least 8..20 octets long (%d)%s",
+               addr->addr_len, VTY_NEWLINE);
       XFREE (MTYPE_ISIS_AREA_ADDR, addr);
-      return CMD_WARNING;
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  if (addr->area_addr[addr->addr_len-1] != 0)
+    {
+      vty_out (vty, "nsel byte (last byte) in area address must be 0%s",
+               VTY_NEWLINE);
+      XFREE (MTYPE_ISIS_AREA_ADDR, addr);
+      return CMD_ERR_AMBIGUOUS;
     }
 
   if (isis->sysid_set == 0)
@@ -291,7 +363,7 @@
       /*
        * First area address - get the SystemID for this router
        */
-      memcpy (isis->sysid, GETSYSID (addr, ISIS_SYS_ID_LEN), ISIS_SYS_ID_LEN);
+      memcpy (isis->sysid, GETSYSID (addr), ISIS_SYS_ID_LEN);
       isis->sysid_set = 1;
       if (isis->debugs & DEBUG_EVENTS)
 	zlog_debug ("Router has SystemID %s", sysid_print (isis->sysid));
@@ -301,20 +373,19 @@
       /*
        * Check that the SystemID portions match
        */
-      if (memcmp (isis->sysid, GETSYSID (addr, ISIS_SYS_ID_LEN),
-		  ISIS_SYS_ID_LEN))
+      if (memcmp (isis->sysid, GETSYSID (addr), ISIS_SYS_ID_LEN))
 	{
 	  vty_out (vty,
 		   "System ID must not change when defining additional area"
 		   " addresses%s", VTY_NEWLINE);
 	  XFREE (MTYPE_ISIS_AREA_ADDR, addr);
-	  return CMD_WARNING;
+	  return CMD_ERR_AMBIGUOUS;
 	}
 
       /* now we see that we don't already have this address */
       for (ALL_LIST_ELEMENTS_RO (area->area_addrs, node, addrp))
 	{
-	  if ((addrp->addr_len + ISIS_SYS_ID_LEN + 1) != (addr->addr_len))
+	  if ((addrp->addr_len + ISIS_SYS_ID_LEN + ISIS_NSEL_LEN) != (addr->addr_len))
 	    continue;
 	  if (!memcmp (addrp->area_addr, addr->area_addr, addr->addr_len))
 	    {
@@ -322,26 +393,28 @@
 	      return CMD_SUCCESS;	/* silent fail */
 	    }
 	}
-
     }
+
   /*
    * Forget the systemID part of the address
    */
-  addr->addr_len -= (ISIS_SYS_ID_LEN + 1);
+  addr->addr_len -= (ISIS_SYS_ID_LEN + ISIS_NSEL_LEN);
   listnode_add (area->area_addrs, addr);
 
   /* only now we can safely generate our LSPs for this area */
   if (listcount (area->area_addrs) > 0)
     {
-      lsp_l1_generate (area);
-      lsp_l2_generate (area);
+      if (area->is_type & IS_LEVEL_1)
+        lsp_generate (area, IS_LEVEL_1);
+      if (area->is_type & IS_LEVEL_2)
+        lsp_generate (area, IS_LEVEL_2);
     }
 
   return CMD_SUCCESS;
 }
 
 int
-area_clear_net_title (struct vty *vty, const u_char *net_title)
+area_clear_net_title (struct vty *vty, const char *net_title)
 {
   struct isis_area *area;
   struct area_addr addr, *addrp = NULL;
@@ -352,7 +425,7 @@
   if (!area)
     {
       vty_out (vty, "Can't find ISIS instance %s", VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_NO_MATCH;
     }
 
   addr.addr_len = dotformat2buff (buff, net_title);
@@ -360,13 +433,13 @@
     {
       vty_out (vty, "Unsupported area address length %d, should be 8...20 %s",
 	       addr.addr_len, VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_AMBIGUOUS;
     }
 
   memcpy (addr.area_addr, buff, (int) addr.addr_len);
 
   for (ALL_LIST_ELEMENTS_RO (area->area_addrs, node, addrp))
-    if (addrp->addr_len == addr.addr_len &&
+    if ((addrp->addr_len + ISIS_SYS_ID_LEN + 1) == addr.addr_len &&
 	!memcmp (addrp->area_addr, addr.area_addr, addr.addr_len))
     break;
 
@@ -374,26 +447,36 @@
     {
       vty_out (vty, "No area address %s for area %s %s", net_title,
 	       area->area_tag, VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_NO_MATCH;
     }
 
   listnode_delete (area->area_addrs, addrp);
+  XFREE (MTYPE_ISIS_AREA_ADDR, addrp);
+
+  /*
+   * Last area address - reset the SystemID for this router
+   */
+  if (listcount (area->area_addrs) == 0)
+    {
+      memset (isis->sysid, 0, ISIS_SYS_ID_LEN);
+      isis->sysid_set = 0;
+      if (isis->debugs & DEBUG_EVENTS)
+        zlog_debug ("Router has no SystemID");
+    }
 
   return CMD_SUCCESS;
 }
 
 /*
- * 'show clns neighbors' command
+ * 'show isis interface' command
  */
 
 int
-show_clns_neigh (struct vty *vty, char detail)
+show_isis_interface_common (struct vty *vty, const char *ifname, char detail)
 {
   struct listnode *anode, *cnode;
   struct isis_area *area;
   struct isis_circuit *circuit;
-  struct list *db;
-  int i;
 
   if (!isis)
     {
@@ -406,92 +489,246 @@
       vty_out (vty, "Area %s:%s", area->area_tag, VTY_NEWLINE);
 
       if (detail == ISIS_UI_LEVEL_BRIEF)
-	vty_out (vty, "  System Id           Interface   L  State        "
-		 "Holdtime SNPA%s", VTY_NEWLINE);
+        vty_out (vty, "  Interface   CircId   State    Type     Level%s",
+                 VTY_NEWLINE);
 
       for (ALL_LIST_ELEMENTS_RO (area->circuit_list, cnode, circuit))
-	{
-	  if (circuit->circ_type == CIRCUIT_T_BROADCAST)
-	    {
-	      for (i = 0; i < 2; i++)
-		{
-		  db = circuit->u.bc.adjdb[i];
-		  if (db && db->count)
-		    {
-		      if (detail == ISIS_UI_LEVEL_BRIEF)
-			isis_adjdb_iterate (db,
-					    (void (*)
-					     (struct isis_adjacency *,
-					      void *)) isis_adj_print_vty,
-					    vty);
-		      if (detail == ISIS_UI_LEVEL_DETAIL)
-			isis_adjdb_iterate (db,
-					    (void (*)
-					     (struct isis_adjacency *,
-					      void *))
-					    isis_adj_print_vty_detail, vty);
-		      if (detail == ISIS_UI_LEVEL_EXTENSIVE)
-			isis_adjdb_iterate (db,
-					    (void (*)
-					     (struct isis_adjacency *,
-					      void *))
-					    isis_adj_print_vty_extensive,
-					    vty);
-		    }
-		}
-	    }
-	  else if (circuit->circ_type == CIRCUIT_T_P2P &&
-		   circuit->u.p2p.neighbor)
-	    {
-	      if (detail == ISIS_UI_LEVEL_BRIEF)
-		isis_adj_p2p_print_vty (circuit->u.p2p.neighbor, vty);
-	      if (detail == ISIS_UI_LEVEL_DETAIL)
-		isis_adj_p2p_print_vty_detail (circuit->u.p2p.neighbor, vty);
-	      if (detail == ISIS_UI_LEVEL_EXTENSIVE)
-		isis_adj_p2p_print_vty_extensive (circuit->u.p2p.neighbor,
-						  vty);
-	    }
-	}
+        if (!ifname)
+          isis_circuit_print_vty (circuit, vty, detail);
+        else if (strcmp(circuit->interface->name, ifname) == 0)
+          isis_circuit_print_vty (circuit, vty, detail);
     }
 
   return CMD_SUCCESS;
 }
 
-DEFUN (show_clns_neighbors,
-       show_clns_neighbors_cmd,
-       "show clns neighbors",
+DEFUN (show_isis_interface,
+       show_isis_interface_cmd,
+       "show isis interface",
        SHOW_STR
-       "clns network information\n"
-       "CLNS neighbor adjacencies\n")
+       "ISIS network information\n"
+       "ISIS interface\n")
 {
-  return show_clns_neigh (vty, ISIS_UI_LEVEL_BRIEF);
+  return show_isis_interface_common (vty, NULL, ISIS_UI_LEVEL_BRIEF);
 }
 
-ALIAS (show_clns_neighbors,
-       show_isis_neighbors_cmd,
-       "show isis neighbors",
+DEFUN (show_isis_interface_detail,
+       show_isis_interface_detail_cmd,
+       "show isis interface detail",
        SHOW_STR
-       "IS-IS network information\n"
-       "IS-IS neighbor adjacencies\n")
-
-DEFUN (show_clns_neighbors_detail,
-       show_clns_neighbors_detail_cmd,
-       "show clns neighbors detail",
-       SHOW_STR
-       "clns network information\n"
-       "CLNS neighbor adjacencies\n"
+       "ISIS network information\n"
+       "ISIS interface\n"
        "show detailed information\n")
 {
-  return show_clns_neigh (vty, ISIS_UI_LEVEL_DETAIL);
+  return show_isis_interface_common (vty, NULL, ISIS_UI_LEVEL_DETAIL);
 }
 
-ALIAS (show_clns_neighbors_detail,
-       show_isis_neighbors_detail_cmd,
-       "show isis neighbors detail",
+DEFUN (show_isis_interface_arg,
+       show_isis_interface_arg_cmd,
+       "show isis interface WORD",
        SHOW_STR
-       "IS-IS network information\n"
-       "IS-IS neighbor adjacencies\n"
+       "ISIS network information\n"
+       "ISIS interface\n"
+       "ISIS interface name\n")
+{
+  return show_isis_interface_common (vty, argv[0], ISIS_UI_LEVEL_DETAIL);
+}
+
+/*
+ * 'show isis neighbor' command
+ */
+
+int
+show_isis_neighbor_common (struct vty *vty, const char *id, char detail)
+{
+  struct listnode *anode, *cnode, *node;
+  struct isis_area *area;
+  struct isis_circuit *circuit;
+  struct list *adjdb;
+  struct isis_adjacency *adj;
+  struct isis_dynhn *dynhn;
+  u_char sysid[ISIS_SYS_ID_LEN];
+  int i;
+
+  if (!isis)
+    {
+      vty_out (vty, "IS-IS Routing Process not enabled%s", VTY_NEWLINE);
+      return CMD_SUCCESS;
+    }
+
+  memset (sysid, 0, ISIS_SYS_ID_LEN);
+  if (id)
+    {
+      if (sysid2buff (sysid, id) == 0)
+        {
+          dynhn = dynhn_find_by_name (id);
+          if (dynhn == NULL)
+            {
+              vty_out (vty, "Invalid system id %s%s", id, VTY_NEWLINE);
+              return CMD_SUCCESS;
+            }
+          memcpy (sysid, dynhn->id, ISIS_SYS_ID_LEN);
+        }
+    }
+
+  for (ALL_LIST_ELEMENTS_RO (isis->area_list, anode, area))
+    {
+      vty_out (vty, "Area %s:%s", area->area_tag, VTY_NEWLINE);
+
+      if (detail == ISIS_UI_LEVEL_BRIEF)
+        vty_out (vty, "  System Id           Interface   L  State"
+                      "        Holdtime SNPA%s", VTY_NEWLINE);
+
+      for (ALL_LIST_ELEMENTS_RO (area->circuit_list, cnode, circuit))
+        {
+          if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+            {
+              for (i = 0; i < 2; i++)
+                {
+                  adjdb = circuit->u.bc.adjdb[i];
+                  if (adjdb && adjdb->count)
+                    {
+                      for (ALL_LIST_ELEMENTS_RO (adjdb, node, adj))
+                        if (!id || !memcmp (adj->sysid, sysid,
+                                            ISIS_SYS_ID_LEN))
+                          isis_adj_print_vty (adj, vty, detail);
+                    }
+                }
+            }
+          else if (circuit->circ_type == CIRCUIT_T_P2P &&
+                   circuit->u.p2p.neighbor)
+            {
+              adj = circuit->u.p2p.neighbor;
+              if (!id || !memcmp (adj->sysid, sysid, ISIS_SYS_ID_LEN))
+                isis_adj_print_vty (adj, vty, detail);
+            }
+        }
+    }
+
+  return CMD_SUCCESS;
+}
+
+/*
+ * 'clear isis neighbor' command
+ */
+int
+clear_isis_neighbor_common (struct vty *vty, const char *id)
+{
+  struct listnode *anode, *cnode, *cnextnode, *node, *nnode;
+  struct isis_area *area;
+  struct isis_circuit *circuit;
+  struct list *adjdb;
+  struct isis_adjacency *adj;
+  struct isis_dynhn *dynhn;
+  u_char sysid[ISIS_SYS_ID_LEN];
+  int i;
+
+  if (!isis)
+    {
+      vty_out (vty, "IS-IS Routing Process not enabled%s", VTY_NEWLINE);
+      return CMD_SUCCESS;
+    }
+
+  memset (sysid, 0, ISIS_SYS_ID_LEN);
+  if (id)
+    {
+      if (sysid2buff (sysid, id) == 0)
+        {
+          dynhn = dynhn_find_by_name (id);
+          if (dynhn == NULL)
+            {
+              vty_out (vty, "Invalid system id %s%s", id, VTY_NEWLINE);
+              return CMD_SUCCESS;
+            }
+          memcpy (sysid, dynhn->id, ISIS_SYS_ID_LEN);
+        }
+    }
+
+  for (ALL_LIST_ELEMENTS_RO (isis->area_list, anode, area))
+    {
+      for (ALL_LIST_ELEMENTS (area->circuit_list, cnode, cnextnode, circuit))
+        {
+          if (circuit->circ_type == CIRCUIT_T_BROADCAST)
+            {
+              for (i = 0; i < 2; i++)
+                {
+                  adjdb = circuit->u.bc.adjdb[i];
+                  if (adjdb && adjdb->count)
+                    {
+                      for (ALL_LIST_ELEMENTS (adjdb, node, nnode, adj))
+                        if (!id || !memcmp (adj->sysid, sysid, ISIS_SYS_ID_LEN))
+                          isis_adj_state_change (adj, ISIS_ADJ_DOWN,
+                                                 "clear user request");
+                    }
+                }
+            }
+          else if (circuit->circ_type == CIRCUIT_T_P2P &&
+                   circuit->u.p2p.neighbor)
+            {
+              adj = circuit->u.p2p.neighbor;
+              if (!id || !memcmp (adj->sysid, sysid, ISIS_SYS_ID_LEN))
+                isis_adj_state_change (adj, ISIS_ADJ_DOWN,
+                                       "clear user request");
+            }
+        }
+    }
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (show_isis_neighbor,
+       show_isis_neighbor_cmd,
+       "show isis neighbor",
+       SHOW_STR
+       "ISIS network information\n"
+       "ISIS neighbor adjacencies\n")
+{
+  return show_isis_neighbor_common (vty, NULL, ISIS_UI_LEVEL_BRIEF);
+}
+
+DEFUN (show_isis_neighbor_detail,
+       show_isis_neighbor_detail_cmd,
+       "show isis neighbor detail",
+       SHOW_STR
+       "ISIS network information\n"
+       "ISIS neighbor adjacencies\n"
        "show detailed information\n")
+{
+  return show_isis_neighbor_common (vty, NULL, ISIS_UI_LEVEL_DETAIL);
+}
+
+DEFUN (show_isis_neighbor_arg,
+       show_isis_neighbor_arg_cmd,
+       "show isis neighbor WORD",
+       SHOW_STR
+       "ISIS network information\n"
+       "ISIS neighbor adjacencies\n"
+       "System id\n")
+{
+  return show_isis_neighbor_common (vty, argv[0], ISIS_UI_LEVEL_DETAIL);
+}
+
+DEFUN (clear_isis_neighbor,
+       clear_isis_neighbor_cmd,
+       "clear isis neighbor",
+       CLEAR_STR
+       "Reset ISIS network information\n"
+       "Reset ISIS neighbor adjacencies\n")
+{
+  return clear_isis_neighbor_common (vty, NULL);
+}
+
+DEFUN (clear_isis_neighbor_arg,
+       clear_isis_neighbor_arg_cmd,
+       "clear isis neighbor WORD",
+       CLEAR_STR
+       "ISIS network information\n"
+       "ISIS neighbor adjacencies\n"
+       "System id\n")
+{
+  return clear_isis_neighbor_common (vty, argv[0]);
+}
+
 /*
  * 'isis debug', 'show debugging'
  */
@@ -535,7 +772,8 @@
 	     VTY_NEWLINE);
   if (flags & DEBUG_EVENTS)
     vty_out (vty, "IS-IS Event debugging is %s%s", onoffs, VTY_NEWLINE);
-
+  if (flags & DEBUG_PACKET_DUMP)
+    vty_out (vty, "IS-IS Packet dump debugging is %s%s", onoffs, VTY_NEWLINE);
 }
 
 DEFUN (show_debugging,
@@ -617,6 +855,11 @@
       vty_out (vty, "debug isis events%s", VTY_NEWLINE);
       write++;
     }
+  if (flags & DEBUG_PACKET_DUMP)
+    {
+      vty_out (vty, "debug isis packet-dump%s", VTY_NEWLINE);
+      write++;
+    }
 
   return write;
 }
@@ -803,7 +1046,6 @@
   return CMD_SUCCESS;
 }
 
-
 DEFUN (debug_isis_spfstats,
        debug_isis_spfstats_cmd,
        "debug isis spf-statistics ",
@@ -908,6 +1150,32 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (debug_isis_packet_dump,
+       debug_isis_packet_dump_cmd,
+       "debug isis packet-dump",
+       DEBUG_STR
+       "IS-IS information\n"
+       "IS-IS packet dump\n")
+{
+  isis->debugs |= DEBUG_PACKET_DUMP;
+  print_debug (vty, DEBUG_PACKET_DUMP, 1);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_debug_isis_packet_dump,
+       no_debug_isis_packet_dump_cmd,
+       "no debug isis packet-dump",
+       UNDEBUG_STR
+       "IS-IS information\n"
+       "IS-IS packet dump\n")
+{
+  isis->debugs &= ~DEBUG_PACKET_DUMP;
+  print_debug (vty, DEBUG_PACKET_DUMP, 0);
+
+  return CMD_SUCCESS;
+}
+
 DEFUN (show_hostname,
        show_hostname_cmd,
        "show isis hostname",
@@ -920,42 +1188,292 @@
   return CMD_SUCCESS;
 }
 
-DEFUN (show_database,
-       show_database_cmd,
-       "show isis database",
-       SHOW_STR "IS-IS information\n" "IS-IS link state database\n")
+static void
+vty_out_timestr(struct vty *vty, time_t uptime)
+{
+  struct tm *tm;
+  time_t difftime = time (NULL);
+  difftime -= uptime;
+  tm = gmtime (&difftime);
+
+#define ONE_DAY_SECOND 60*60*24
+#define ONE_WEEK_SECOND 60*60*24*7
+  if (difftime < ONE_DAY_SECOND)
+    vty_out (vty,  "%02d:%02d:%02d", 
+        tm->tm_hour, tm->tm_min, tm->tm_sec);
+  else if (difftime < ONE_WEEK_SECOND)
+    vty_out (vty, "%dd%02dh%02dm", 
+        tm->tm_yday, tm->tm_hour, tm->tm_min);
+  else
+    vty_out (vty, "%02dw%dd%02dh", 
+        tm->tm_yday/7,
+        tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
+  vty_out (vty, " ago");
+}
+
+DEFUN (show_isis_summary,
+       show_isis_summary_cmd,
+       "show isis summary",
+       SHOW_STR "IS-IS information\n" "IS-IS summary\n")
+{
+  struct listnode *node, *node2;
+  struct isis_area *area;
+  struct isis_spftree *spftree;
+  int level;
+
+  if (isis == NULL)
+  {
+    vty_out (vty, "ISIS is not running%s", VTY_NEWLINE);
+    return CMD_SUCCESS;
+  }
+
+  vty_out (vty, "Process Id      : %ld%s", isis->process_id,
+      VTY_NEWLINE);
+  if (isis->sysid_set)
+    vty_out (vty, "System Id       : %s%s", sysid_print (isis->sysid),
+        VTY_NEWLINE);
+
+  vty_out (vty, "Up time         : ");
+  vty_out_timestr(vty, isis->uptime);
+  vty_out (vty, "%s", VTY_NEWLINE);
+
+  if (isis->area_list)
+    vty_out (vty, "Number of areas : %d%s", isis->area_list->count,
+        VTY_NEWLINE);
+
+  for (ALL_LIST_ELEMENTS_RO (isis->area_list, node, area))
+  {
+    vty_out (vty, "Area %s:%s", area->area_tag ? area->area_tag : "null",
+        VTY_NEWLINE);
+
+    if (listcount (area->area_addrs) > 0)
+    {
+      struct area_addr *area_addr;
+      for (ALL_LIST_ELEMENTS_RO (area->area_addrs, node2, area_addr))
+      {
+        vty_out (vty, "  Net: %s%s",
+            isonet_print (area_addr->area_addr,
+              area_addr->addr_len + ISIS_SYS_ID_LEN +
+              1), VTY_NEWLINE);
+      }
+    }
+
+    for (level = ISIS_LEVEL1; level <= ISIS_LEVELS; level++)
+    {
+      if ((area->is_type & level) == 0)
+        continue;
+
+      vty_out (vty, "  Level-%d:%s", level, VTY_NEWLINE);
+      spftree = area->spftree[level - 1];
+      if (spftree->pending)
+        vty_out (vty, "    IPv4 SPF: (pending)%s", VTY_NEWLINE);
+      else
+        vty_out (vty, "    IPv4 SPF:%s", VTY_NEWLINE);
+
+      vty_out (vty, "      minimum interval  : %d%s",
+          area->min_spf_interval[level - 1], VTY_NEWLINE);
+
+      vty_out (vty, "      last run elapsed  : ");
+      vty_out_timestr(vty, spftree->last_run_timestamp);
+      vty_out (vty, "%s", VTY_NEWLINE);
+
+      vty_out (vty, "      last run duration : %u usec%s",
+               (u_int32_t)spftree->last_run_duration, VTY_NEWLINE);
+
+      vty_out (vty, "      run count         : %d%s",
+          spftree->runcount, VTY_NEWLINE);
+
+#ifdef HAVE_IPV6
+      spftree = area->spftree6[level - 1];
+      if (spftree->pending)
+        vty_out (vty, "    IPv6 SPF: (pending)%s", VTY_NEWLINE);
+      else
+        vty_out (vty, "    IPv6 SPF:%s", VTY_NEWLINE);
+
+      vty_out (vty, "      minimum interval  : %d%s",
+          area->min_spf_interval[level - 1], VTY_NEWLINE);
+
+      vty_out (vty, "      last run elapsed  : ");
+      vty_out_timestr(vty, spftree->last_run_timestamp);
+      vty_out (vty, "%s", VTY_NEWLINE);
+
+      vty_out (vty, "      last run duration : %u msec%s",
+               spftree->last_run_duration, VTY_NEWLINE);
+
+      vty_out (vty, "      run count         : %d%s",
+          spftree->runcount, VTY_NEWLINE);
+#endif
+    }
+  }
+  vty_out (vty, "%s", VTY_NEWLINE);
+
+  return CMD_SUCCESS;
+}
+
+/*
+ * This function supports following display options:
+ * [ show isis database [detail] ]
+ * [ show isis database <sysid> [detail] ]
+ * [ show isis database <hostname> [detail] ]
+ * [ show isis database <sysid>.<pseudo-id> [detail] ]
+ * [ show isis database <hostname>.<pseudo-id> [detail] ]
+ * [ show isis database <sysid>.<pseudo-id>-<fragment-number> [detail] ]
+ * [ show isis database <hostname>.<pseudo-id>-<fragment-number> [detail] ]
+ * [ show isis database detail <sysid> ]
+ * [ show isis database detail <hostname> ]
+ * [ show isis database detail <sysid>.<pseudo-id> ]
+ * [ show isis database detail <hostname>.<pseudo-id> ]
+ * [ show isis database detail <sysid>.<pseudo-id>-<fragment-number> ]
+ * [ show isis database detail <hostname>.<pseudo-id>-<fragment-number> ]
+ */
+static int
+show_isis_database (struct vty *vty, const char *argv, int ui_level)
 {
   struct listnode *node;
   struct isis_area *area;
+  struct isis_lsp *lsp;
+  struct isis_dynhn *dynhn;
+  const char *pos = argv;
+  u_char lspid[ISIS_SYS_ID_LEN+2];
+  char sysid[255];
+  u_char number[3];
   int level, lsp_count;
 
   if (isis->area_list->count == 0)
     return CMD_SUCCESS;
 
+  memset (&lspid, 0, ISIS_SYS_ID_LEN);
+  memset (&sysid, 0, 255);
+
+  /*
+   * extract fragment and pseudo id from the string argv
+   * in the forms:
+   * (a) <systemid/hostname>.<pseudo-id>-<framenent> or
+   * (b) <systemid/hostname>.<pseudo-id> or
+   * (c) <systemid/hostname> or
+   * Where systemid is in the form:
+   * xxxx.xxxx.xxxx
+   */
+  if (argv)
+     strncpy (sysid, argv, 254);
+  if (argv && strlen (argv) > 3)
+    {
+      pos = argv + strlen (argv) - 3;
+      if (strncmp (pos, "-", 1) == 0)
+      {
+        memcpy (number, ++pos, 2);
+        lspid[ISIS_SYS_ID_LEN+1] = (u_char) strtol ((char *)number, NULL, 16);
+        pos -= 4;
+        if (strncmp (pos, ".", 1) != 0)
+          return CMD_ERR_AMBIGUOUS;
+      }
+      if (strncmp (pos, ".", 1) == 0)
+      {
+        memcpy (number, ++pos, 2);
+        lspid[ISIS_SYS_ID_LEN] = (u_char) strtol ((char *)number, NULL, 16);
+        sysid[pos - argv - 1] = '\0';
+      }
+    }
+
   for (ALL_LIST_ELEMENTS_RO (isis->area_list, node, area))
     {
       vty_out (vty, "Area %s:%s", area->area_tag ? area->area_tag : "null",
-	       VTY_NEWLINE);
+               VTY_NEWLINE);
+
       for (level = 0; level < ISIS_LEVELS; level++)
-	{
-	  if (area->lspdb[level] && dict_count (area->lspdb[level]) > 0)
-	    {
-	      vty_out (vty, "IS-IS Level-%d link-state database:%s",
-		       level + 1, VTY_NEWLINE);
+        {
+          if (area->lspdb[level] && dict_count (area->lspdb[level]) > 0)
+            {
+              lsp = NULL;
+              if (argv != NULL)
+                {
+                  /*
+                   * Try to find the lsp-id if the argv string is in
+                   * the form hostname.<pseudo-id>-<fragment>
+                   */
+                  if (sysid2buff (lspid, sysid))
+                    {
+                      lsp = lsp_search (lspid, area->lspdb[level]);
+                    }
+                  else if ((dynhn = dynhn_find_by_name (sysid)))
+                    {
+                      memcpy (lspid, dynhn->id, ISIS_SYS_ID_LEN);
+                      lsp = lsp_search (lspid, area->lspdb[level]);
+                    }
+                  else if (strncmp(unix_hostname (), sysid, 15) == 0)
+                    {
+                      memcpy (lspid, isis->sysid, ISIS_SYS_ID_LEN);
+                      lsp = lsp_search (lspid, area->lspdb[level]);
+                    }
+                }
 
-	      lsp_count = lsp_print_all (vty, area->lspdb[level],
-					 ISIS_UI_LEVEL_BRIEF,
-					 area->dynhostname);
+              if (lsp != NULL || argv == NULL)
+                {
+                  vty_out (vty, "IS-IS Level-%d link-state database:%s",
+                           level + 1, VTY_NEWLINE);
 
-	      vty_out (vty, "%s    %u LSPs%s%s",
-		       VTY_NEWLINE, lsp_count, VTY_NEWLINE, VTY_NEWLINE);
-	    }
-	}
+                  /* print the title in all cases */
+                  vty_out (vty, "LSP ID                  PduLen  "
+                           "SeqNumber   Chksum  Holdtime  ATT/P/OL%s",
+                           VTY_NEWLINE);
+                }
+
+              if (lsp)
+                {
+                  if (ui_level == ISIS_UI_LEVEL_DETAIL)
+                    lsp_print_detail (lsp, vty, area->dynhostname);
+                  else
+                    lsp_print (lsp, vty, area->dynhostname);
+                }
+              else if (argv == NULL)
+                {
+                  lsp_count = lsp_print_all (vty, area->lspdb[level],
+                                             ui_level,
+                                             area->dynhostname);
+
+                  vty_out (vty, "    %u LSPs%s%s",
+                           lsp_count, VTY_NEWLINE, VTY_NEWLINE);
+                }
+            }
+        }
     }
 
   return CMD_SUCCESS;
 }
 
+DEFUN (show_database_brief,
+       show_database_cmd,
+       "show isis database",
+       SHOW_STR
+       "IS-IS information\n"
+       "IS-IS link state database\n")
+{
+  return show_isis_database (vty, NULL, ISIS_UI_LEVEL_BRIEF);
+}
+
+DEFUN (show_database_lsp_brief,
+       show_database_arg_cmd,
+       "show isis database WORD",
+       SHOW_STR
+       "IS-IS information\n"
+       "IS-IS link state database\n"
+       "LSP ID\n")
+{
+  return show_isis_database (vty, argv[0], ISIS_UI_LEVEL_BRIEF);
+}
+
+DEFUN (show_database_lsp_detail,
+       show_database_arg_detail_cmd,
+       "show isis database WORD detail",
+       SHOW_STR
+       "IS-IS information\n"
+       "IS-IS link state database\n"
+       "LSP ID\n"
+       "Detailed information\n")
+{
+  return show_isis_database (vty, argv[0], ISIS_UI_LEVEL_DETAIL);
+}
+
 DEFUN (show_database_detail,
        show_database_detail_cmd,
        "show isis database detail",
@@ -963,35 +1481,19 @@
        "IS-IS information\n"
        "IS-IS link state database\n")
 {
-  struct listnode *node;
-  struct isis_area *area;
-  int level, lsp_count;
+  return show_isis_database (vty, NULL, ISIS_UI_LEVEL_DETAIL);
+}
 
-  if (isis->area_list->count == 0)
-    return CMD_SUCCESS;
-
-  for (ALL_LIST_ELEMENTS_RO (isis->area_list, node, area))
-    {
-      vty_out (vty, "Area %s:%s", area->area_tag ? area->area_tag : "null",
-	       VTY_NEWLINE);
-      for (level = 0; level < ISIS_LEVELS; level++)
-	{
-	  if (area->lspdb[level] && dict_count (area->lspdb[level]) > 0)
-	    {
-	      vty_out (vty, "IS-IS Level-%d Link State Database:%s",
-		       level + 1, VTY_NEWLINE);
-
-	      lsp_count = lsp_print_all (vty, area->lspdb[level],
-					 ISIS_UI_LEVEL_DETAIL,
-					 area->dynhostname);
-
-	      vty_out (vty, "%s    %u LSPs%s%s",
-		       VTY_NEWLINE, lsp_count, VTY_NEWLINE, VTY_NEWLINE);
-	    }
-	}
-    }
-
-  return CMD_SUCCESS;
+DEFUN (show_database_detail_lsp,
+       show_database_detail_arg_cmd,
+       "show isis database detail WORD",
+       SHOW_STR
+       "IS-IS information\n"
+       "IS-IS link state database\n"
+       "Detailed information\n"
+       "LSP ID\n")
+{
+  return show_isis_database (vty, argv[0], ISIS_UI_LEVEL_DETAIL);
 }
 
 /* 
@@ -1043,10 +1545,11 @@
   return area_clear_net_title (vty, argv[0]);
 }
 
-DEFUN (area_passwd,
-       area_passwd_cmd,
-       "area-password WORD",
+DEFUN (area_passwd_md5,
+       area_passwd_md5_cmd,
+       "area-password md5 WORD",
        "Configure the authentication password for an area\n"
+       "Authentication type\n"
        "Area password\n")
 {
   struct isis_area *area;
@@ -1056,16 +1559,75 @@
 
   if (!area)
     {
-      vty_out (vty, "Cant find IS-IS instance%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      vty_out (vty, "Can't find IS-IS instance%s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
     }
 
   len = strlen (argv[0]);
   if (len > 254)
     {
       vty_out (vty, "Too long area password (>254)%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_AMBIGUOUS;
     }
+
+  area->area_passwd.len = (u_char) len;
+  area->area_passwd.type = ISIS_PASSWD_TYPE_HMAC_MD5;
+  strncpy ((char *)area->area_passwd.passwd, argv[0], 255);
+
+  if (argc > 1)
+    {
+      SET_FLAG(area->area_passwd.snp_auth, SNP_AUTH_SEND);
+      if (strncmp(argv[1], "v", 1) == 0)
+	SET_FLAG(area->area_passwd.snp_auth, SNP_AUTH_RECV);
+      else
+	UNSET_FLAG(area->area_passwd.snp_auth, SNP_AUTH_RECV);
+    }
+  else
+    {
+      UNSET_FLAG(area->area_passwd.snp_auth, SNP_AUTH_SEND);
+      UNSET_FLAG(area->area_passwd.snp_auth, SNP_AUTH_RECV);
+    }
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
+
+  return CMD_SUCCESS;
+}
+
+ALIAS (area_passwd_md5,
+       area_passwd_md5_snpauth_cmd,
+       "area-password md5 WORD authenticate snp (send-only|validate)",
+       "Configure the authentication password for an area\n"
+       "Authentication type\n"
+       "Area password\n"
+       "Authentication\n"
+       "SNP PDUs\n"
+       "Send but do not check PDUs on receiving\n"
+       "Send and check PDUs on receiving\n");
+
+DEFUN (area_passwd_clear,
+       area_passwd_clear_cmd,
+       "area-password clear WORD",
+       "Configure the authentication password for an area\n"
+       "Authentication type\n"
+       "Area password\n")
+{
+  struct isis_area *area;
+  int len;
+
+  area = vty->index;
+
+  if (!area)
+    {
+      vty_out (vty, "Can't find IS-IS instance%s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
+
+  len = strlen (argv[0]);
+  if (len > 254)
+    {
+      vty_out (vty, "Too long area password (>254)%s", VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
   area->area_passwd.len = (u_char) len;
   area->area_passwd.type = ISIS_PASSWD_TYPE_CLEARTXT;
   strncpy ((char *)area->area_passwd.passwd, argv[0], 255);
@@ -1083,14 +1645,16 @@
       UNSET_FLAG(area->area_passwd.snp_auth, SNP_AUTH_SEND);
       UNSET_FLAG(area->area_passwd.snp_auth, SNP_AUTH_RECV);
     }
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
 
   return CMD_SUCCESS;
 }
 
-ALIAS (area_passwd,
-       area_passwd_snpauth_cmd,
-       "area-password WORD authenticate snp (send-only|validate)",
+ALIAS (area_passwd_clear,
+       area_passwd_clear_snpauth_cmd,
+       "area-password clear WORD authenticate snp (send-only|validate)",
        "Configure the authentication password for an area\n"
+       "Authentication type\n"
        "Area password\n"
        "Authentication\n"
        "SNP PDUs\n"
@@ -1109,19 +1673,21 @@
 
   if (!area)
     {
-      vty_out (vty, "Cant find IS-IS instance%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      vty_out (vty, "Can't find IS-IS instance%s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
     }
 
   memset (&area->area_passwd, 0, sizeof (struct isis_passwd));
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
 
   return CMD_SUCCESS;
 }
 
-DEFUN (domain_passwd,
-       domain_passwd_cmd,
-       "domain-password WORD",
+DEFUN (domain_passwd_md5,
+       domain_passwd_md5_cmd,
+       "domain-password md5 WORD",
        "Set the authentication password for a routing domain\n"
+       "Authentication type\n"
        "Routing domain password\n")
 {
   struct isis_area *area;
@@ -1131,16 +1697,75 @@
 
   if (!area)
     {
-      vty_out (vty, "Cant find IS-IS instance%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      vty_out (vty, "Can't find IS-IS instance%s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
     }
 
   len = strlen (argv[0]);
   if (len > 254)
     {
       vty_out (vty, "Too long area password (>254)%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      return CMD_ERR_AMBIGUOUS;
     }
+
+  area->domain_passwd.len = (u_char) len;
+  area->domain_passwd.type = ISIS_PASSWD_TYPE_HMAC_MD5;
+  strncpy ((char *)area->domain_passwd.passwd, argv[0], 255);
+
+  if (argc > 1)
+    {
+      SET_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_SEND);
+      if (strncmp(argv[1], "v", 1) == 0)
+	SET_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_RECV);
+      else
+	UNSET_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_RECV);
+    }
+  else
+    {
+      UNSET_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_SEND);
+      UNSET_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_RECV);
+    }
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
+
+  return CMD_SUCCESS;
+}
+
+ALIAS (domain_passwd_md5,
+       domain_passwd_md5_snpauth_cmd,
+       "domain-password md5 WORD authenticate snp (send-only|validate)",
+       "Set the authentication password for a routing domain\n"
+       "Authentication type\n"
+       "Routing domain password\n"
+       "Authentication\n"
+       "SNP PDUs\n"
+       "Send but do not check PDUs on receiving\n"
+       "Send and check PDUs on receiving\n");
+
+DEFUN (domain_passwd_clear,
+       domain_passwd_clear_cmd,
+       "domain-password clear WORD",
+       "Set the authentication password for a routing domain\n"
+       "Authentication type\n"
+       "Routing domain password\n")
+{
+  struct isis_area *area;
+  int len;
+
+  area = vty->index;
+
+  if (!area)
+    {
+      vty_out (vty, "Can't find IS-IS instance%s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
+    }
+
+  len = strlen (argv[0]);
+  if (len > 254)
+    {
+      vty_out (vty, "Too long area password (>254)%s", VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
   area->domain_passwd.len = (u_char) len;
   area->domain_passwd.type = ISIS_PASSWD_TYPE_CLEARTXT;
   strncpy ((char *)area->domain_passwd.passwd, argv[0], 255);
@@ -1158,14 +1783,16 @@
       UNSET_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_SEND);
       UNSET_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_RECV);
     }
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
 
   return CMD_SUCCESS;
 }
 
-ALIAS (domain_passwd,
-       domain_passwd_snpauth_cmd,
-       "domain-password WORD authenticate snp (send-only|validate)",
+ALIAS (domain_passwd_clear,
+       domain_passwd_clear_snpauth_cmd,
+       "domain-password clear WORD authenticate snp (send-only|validate)",
        "Set the authentication password for a routing domain\n"
+       "Authentication type\n"
        "Routing domain password\n"
        "Authentication\n"
        "SNP PDUs\n"
@@ -1174,7 +1801,7 @@
 
 DEFUN (no_domain_passwd,
        no_domain_passwd_cmd,
-       "no domain-password WORD",
+       "no domain-password",
        NO_STR
        "Set the authentication password for a routing domain\n")
 {
@@ -1184,11 +1811,12 @@
 
   if (!area)
     {
-      vty_out (vty, "Cant find IS-IS instance%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      vty_out (vty, "Can't find IS-IS instance%s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
     }
 
   memset (&area->domain_passwd, 0, sizeof (struct isis_passwd));
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
 
   return CMD_SUCCESS;
 }
@@ -1208,8 +1836,8 @@
 
   if (!area)
     {
-      vty_out (vty, "Cant find IS-IS instance%s", VTY_NEWLINE);
-      return CMD_WARNING;
+      vty_out (vty, "Can't find IS-IS instance%s", VTY_NEWLINE);
+      return CMD_ERR_NO_MATCH;
     }
 
   type = string2circuit_t (argv[0]);
@@ -1240,8 +1868,9 @@
   assert (area);
 
   /*
-   * Put the is-type back to default. Which is level-1-2 on first
-   * circuit for the area level-1 for the rest
+   * Put the is-type back to defaults:
+   * - level-1-2 on first area
+   * - level-1 for the rest
    */
   if (listgetdata (listhead (isis->area_list)) == area)
     type = IS_LEVEL_1_AND_2;
@@ -1253,6 +1882,36 @@
   return CMD_SUCCESS;
 }
 
+static int
+set_lsp_gen_interval (struct vty *vty, struct isis_area *area,
+                      uint16_t interval, int level)
+{
+  int lvl;
+
+  for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; ++lvl)
+    {
+      if (!(lvl & level))
+        continue;
+
+      if (interval >= area->lsp_refresh[lvl-1])
+        {
+          vty_out (vty, "LSP gen interval %us must be less than "
+                   "the LSP refresh interval %us%s",
+                   interval, area->lsp_refresh[lvl-1], VTY_NEWLINE);
+          return CMD_ERR_AMBIGUOUS;
+        }
+    }
+
+  for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; ++lvl)
+    {
+      if (!(lvl & level))
+        continue;
+      area->lsp_gen_interval[lvl-1] = interval;
+    }
+
+  return CMD_SUCCESS;
+}
+
 DEFUN (lsp_gen_interval,
        lsp_gen_interval_cmd,
        "lsp-gen-interval <1-120>",
@@ -1261,15 +1920,12 @@
 {
   struct isis_area *area;
   uint16_t interval;
+  int level;
 
   area = vty->index;
-  assert (area);
-
   interval = atoi (argv[0]);
-  area->lsp_gen_interval[0] = interval;
-  area->lsp_gen_interval[1] = interval;
-
-  return CMD_SUCCESS;
+  level = IS_LEVEL_1 | IS_LEVEL_2;
+  return set_lsp_gen_interval (vty, area, interval, level);
 }
 
 DEFUN (no_lsp_gen_interval,
@@ -1279,14 +1935,13 @@
        "Minimum interval between regenerating same LSP\n")
 {
   struct isis_area *area;
+  uint16_t interval;
+  int level;
 
   area = vty->index;
-  assert (area);
-
-  area->lsp_gen_interval[0] = LSP_GEN_INTERVAL_DEFAULT;
-  area->lsp_gen_interval[1] = LSP_GEN_INTERVAL_DEFAULT;
-
-  return CMD_SUCCESS;
+  interval = DEFAULT_MIN_LSP_GEN_INTERVAL;
+  level = IS_LEVEL_1 | IS_LEVEL_2;
+  return set_lsp_gen_interval (vty, area, interval, level);
 }
 
 ALIAS (no_lsp_gen_interval,
@@ -1305,14 +1960,12 @@
 {
   struct isis_area *area;
   uint16_t interval;
+  int level;
 
   area = vty->index;
-  assert (area);
-
   interval = atoi (argv[0]);
-  area->lsp_gen_interval[0] = interval;
-
-  return CMD_SUCCESS;
+  level = IS_LEVEL_1;
+  return set_lsp_gen_interval (vty, area, interval, level);
 }
 
 DEFUN (no_lsp_gen_interval_l1,
@@ -1323,13 +1976,13 @@
        "Set interval for level 1 only\n")
 {
   struct isis_area *area;
+  uint16_t interval;
+  int level;
 
   area = vty->index;
-  assert (area);
-
-  area->lsp_gen_interval[0] = LSP_GEN_INTERVAL_DEFAULT;
-
-  return CMD_SUCCESS;
+  interval = DEFAULT_MIN_LSP_GEN_INTERVAL;
+  level = IS_LEVEL_1;
+  return set_lsp_gen_interval (vty, area, interval, level);
 }
 
 ALIAS (no_lsp_gen_interval_l1,
@@ -1348,15 +2001,13 @@
        "Minimum interval in seconds\n")
 {
   struct isis_area *area;
-  int interval;
+  uint16_t interval;
+  int level;
 
   area = vty->index;
-  assert (area);
-
   interval = atoi (argv[0]);
-  area->lsp_gen_interval[1] = interval;
-
-  return CMD_SUCCESS;
+  level = IS_LEVEL_2;
+  return set_lsp_gen_interval (vty, area, interval, level);
 }
 
 DEFUN (no_lsp_gen_interval_l2,
@@ -1367,15 +2018,13 @@
        "Set interval for level 2 only\n")
 {
   struct isis_area *area;
-  int interval;
+  uint16_t interval;
+  int level;
 
   area = vty->index;
-  assert (area);
-
-  interval = atoi (argv[0]);
-  area->lsp_gen_interval[1] = LSP_GEN_INTERVAL_DEFAULT;
-
-  return CMD_SUCCESS;
+  interval = DEFAULT_MIN_LSP_GEN_INTERVAL;
+  level = IS_LEVEL_2;
+  return set_lsp_gen_interval (vty, area, interval, level);
 }
 
 ALIAS (no_lsp_gen_interval_l2,
@@ -1386,6 +2035,44 @@
        "Set interval for level 2 only\n"
        "Minimum interval in seconds\n")
 
+static int
+validate_metric_style_narrow (struct vty *vty, struct isis_area *area)
+{
+  struct isis_circuit *circuit;
+  struct listnode *node;
+  
+  if (! vty)
+    return CMD_ERR_AMBIGUOUS;
+
+  if (! area)
+    {
+      vty_out (vty, "ISIS area is invalid%s", VTY_NEWLINE);
+      return CMD_ERR_AMBIGUOUS;
+    }
+
+  for (ALL_LIST_ELEMENTS_RO (area->circuit_list, node, circuit))
+    {
+      if ((area->is_type & IS_LEVEL_1) &&
+          (circuit->is_type & IS_LEVEL_1) &&
+          (circuit->metrics[0].metric_default > MAX_NARROW_LINK_METRIC))
+        {
+          vty_out (vty, "ISIS circuit %s metric is invalid%s",
+                   circuit->interface->name, VTY_NEWLINE);
+          return CMD_ERR_AMBIGUOUS;
+        }
+      if ((area->is_type & IS_LEVEL_2) &&
+          (circuit->is_type & IS_LEVEL_2) &&
+          (circuit->metrics[1].metric_default > MAX_NARROW_LINK_METRIC))
+        {
+          vty_out (vty, "ISIS circuit %s metric is invalid%s",
+                   circuit->interface->name, VTY_NEWLINE);
+          return CMD_ERR_AMBIGUOUS;
+        }
+    }
+
+  return CMD_SUCCESS;
+}
+
 DEFUN (metric_style,
        metric_style_cmd,
        "metric-style (narrow|transition|wide)",
@@ -1395,6 +2082,7 @@
        "Use new style of TLVs to carry wider metric\n")
 {
   struct isis_area *area;
+  int ret;
 
   area = vty->index;
   assert (area);
@@ -1411,6 +2099,10 @@
     }
   else if (strncmp (argv[0], "n", 1) == 0)
     {
+      ret = validate_metric_style_narrow (vty, area);
+      if (ret != CMD_SUCCESS)
+        return ret;
+
       area->newmetric = 0;
       area->oldmetric = 1;
     }
@@ -1425,10 +2117,15 @@
        "Use old-style (ISO 10589) or new-style packet formats\n")
 {
   struct isis_area *area;
+  int ret;
 
   area = vty->index;
   assert (area);
 
+  ret = validate_metric_style_narrow (vty, area);
+  if (ret != CMD_SUCCESS)
+    return ret;
+
   /* Default is narrow metric. */
   area->newmetric = 0;
   area->oldmetric = 1;
@@ -1436,6 +2133,40 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (set_overload_bit,
+       set_overload_bit_cmd,
+       "set-overload-bit",
+       "Set overload bit to avoid any transit traffic\n"
+       "Set overload bit\n")
+{
+  struct isis_area *area;
+
+  area = vty->index;
+  assert (area);
+
+  area->overload_bit = LSPBIT_OL;
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_set_overload_bit,
+       no_set_overload_bit_cmd,
+       "no set-overload-bit",
+       "Reset overload bit to accept transit traffic\n"
+       "Reset overload bit\n")
+{
+  struct isis_area *area;
+
+  area = vty->index;
+  assert (area);
+
+  area->overload_bit = 0;
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
+
+  return CMD_SUCCESS;
+}
+
 DEFUN (dynamic_hostname,
        dynamic_hostname_cmd,
        "hostname dynamic",
@@ -1447,7 +2178,11 @@
   area = vty->index;
   assert (area);
 
-  area->dynhostname = 1;
+  if (!area->dynhostname)
+   {
+     area->dynhostname = 1;
+     lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 0);
+   }
 
   return CMD_SUCCESS;
 }
@@ -1464,7 +2199,11 @@
   area = vty->index;
   assert (area);
 
-  area->dynhostname = 0;
+  if (area->dynhostname)
+    {
+      area->dynhostname = 0;
+      lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 0);
+    }
 
   return CMD_SUCCESS;
 }
@@ -1591,7 +2330,360 @@
        "Set interval for level 2 only\n"
        "Minimum interval between consecutive SPFs in seconds\n")
 
+static int
+set_lsp_max_lifetime (struct vty *vty, struct isis_area *area,
+                      uint16_t interval, int level)
+{
+  int lvl;
+  int set_refresh_interval[ISIS_LEVELS] = {0, 0};
+  uint16_t refresh_interval;
+
+  refresh_interval = interval - 300;
+
+  for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; lvl++)
+    {
+      if (!(lvl & level))
+        continue;
+      if (refresh_interval < area->lsp_refresh[lvl-1])
+        {
+          vty_out (vty, "Level %d Max LSP lifetime %us must be 300s greater than "
+                   "the configured LSP refresh interval %us%s",
+                   lvl, interval, area->lsp_refresh[lvl-1], VTY_NEWLINE);
+          vty_out (vty, "Automatically reducing level %d LSP refresh interval "
+                   "to %us%s", lvl, refresh_interval, VTY_NEWLINE);
+          set_refresh_interval[lvl-1] = 1;
+
+          if (refresh_interval <= area->lsp_gen_interval[lvl-1])
+            {
+              vty_out (vty, "LSP refresh interval %us must be greater than "
+                       "the configured LSP gen interval %us%s",
+                       refresh_interval, area->lsp_gen_interval[lvl-1],
+                       VTY_NEWLINE);
+              return CMD_ERR_AMBIGUOUS;
+            }
+        }
+    }
+
+  for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; lvl++)
+    {
+      if (!(lvl & level))
+        continue;
+      area->max_lsp_lifetime[lvl-1] = interval;
+      /* Automatically reducing lsp_refresh_interval to interval - 300 */
+      if (set_refresh_interval[lvl-1])
+        area->lsp_refresh[lvl-1] = refresh_interval;
+    }
+
+  lsp_regenerate_schedule (area, level, 1);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (max_lsp_lifetime,
+       max_lsp_lifetime_cmd,
+       "max-lsp-lifetime <350-65535>",
+       "Maximum LSP lifetime\n"
+       "LSP lifetime in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = atoi (argv[0]);
+  level = IS_LEVEL_1 | IS_LEVEL_2;
+  return set_lsp_max_lifetime (vty, area, interval, level);
+}
+
+DEFUN (no_max_lsp_lifetime,
+       no_max_lsp_lifetime_cmd,
+       "no max-lsp-lifetime",
+       NO_STR
+       "LSP lifetime in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = DEFAULT_LSP_LIFETIME;
+  level = IS_LEVEL_1 | IS_LEVEL_2;
+  return set_lsp_max_lifetime (vty, area, interval, level);
+}
+
+ALIAS (no_max_lsp_lifetime,
+       no_max_lsp_lifetime_arg_cmd,
+       "no max-lsp-lifetime <350-65535>",
+       NO_STR
+       "Maximum LSP lifetime\n"
+       "LSP lifetime in seconds\n")
+
+DEFUN (max_lsp_lifetime_l1,
+       max_lsp_lifetime_l1_cmd,
+       "max-lsp-lifetime level-1 <350-65535>",
+       "Maximum LSP lifetime for Level 1 only\n"
+       "LSP lifetime for Level 1 only in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = atoi (argv[0]);
+  level = IS_LEVEL_1;
+  return set_lsp_max_lifetime (vty, area, interval, level);
+}
+
+DEFUN (no_max_lsp_lifetime_l1,
+       no_max_lsp_lifetime_l1_cmd,
+       "no max-lsp-lifetime level-1",
+       NO_STR
+       "LSP lifetime for Level 1 only in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = DEFAULT_LSP_LIFETIME;
+  level = IS_LEVEL_1;
+  return set_lsp_max_lifetime (vty, area, interval, level);
+}
+
+ALIAS (no_max_lsp_lifetime_l1,
+       no_max_lsp_lifetime_l1_arg_cmd,
+       "no max-lsp-lifetime level-1 <350-65535>",
+       NO_STR
+       "Maximum LSP lifetime for Level 1 only\n"
+       "LSP lifetime for Level 1 only in seconds\n")
+
+DEFUN (max_lsp_lifetime_l2,
+       max_lsp_lifetime_l2_cmd,
+       "max-lsp-lifetime level-2 <350-65535>",
+       "Maximum LSP lifetime for Level 2 only\n"
+       "LSP lifetime for Level 2 only in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = atoi (argv[0]);
+  level = IS_LEVEL_2;
+  return set_lsp_max_lifetime (vty, area, interval, level);
+}
+
+DEFUN (no_max_lsp_lifetime_l2,
+       no_max_lsp_lifetime_l2_cmd,
+       "no max-lsp-lifetime level-2",
+       NO_STR
+       "LSP lifetime for Level 2 only in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = DEFAULT_LSP_LIFETIME;
+  level = IS_LEVEL_2;
+  return set_lsp_max_lifetime (vty, area, interval, level);
+}
+
+ALIAS (no_max_lsp_lifetime_l2,
+       no_max_lsp_lifetime_l2_arg_cmd,
+       "no max-lsp-lifetime level-2 <350-65535>",
+       NO_STR
+       "Maximum LSP lifetime for Level 2 only\n"
+       "LSP lifetime for Level 2 only in seconds\n")
+
+static int
+set_lsp_refresh_interval (struct vty *vty, struct isis_area *area,
+                          uint16_t interval, int level)
+{
+  int lvl;
+
+  for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; ++lvl)
+    {
+      if (!(lvl & level))
+        continue;
+      if (interval <= area->lsp_gen_interval[lvl-1])
+        {
+          vty_out (vty, "LSP refresh interval %us must be greater than "
+                   "the configured LSP gen interval %us%s",
+                   interval, area->lsp_gen_interval[lvl-1],
+                   VTY_NEWLINE);
+          return CMD_ERR_AMBIGUOUS;
+        }
+      if (interval > (area->max_lsp_lifetime[lvl-1] - 300))
+        {
+          vty_out (vty, "LSP refresh interval %us must be less than "
+                   "the configured LSP lifetime %us less 300%s",
+                   interval, area->max_lsp_lifetime[lvl-1],
+                   VTY_NEWLINE);
+          return CMD_ERR_AMBIGUOUS;
+        }
+    }
+
+  for (lvl = IS_LEVEL_1; lvl <= IS_LEVEL_2; ++lvl)
+    {
+      if (!(lvl & level))
+        continue;
+      area->lsp_refresh[lvl-1] = interval;
+    }
+  lsp_regenerate_schedule (area, level, 1);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (lsp_refresh_interval,
+       lsp_refresh_interval_cmd,
+       "lsp-refresh-interval <1-65235>",
+       "LSP refresh interval\n"
+       "LSP refresh interval in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = atoi (argv[0]);
+  level = IS_LEVEL_1 | IS_LEVEL_2;
+  return set_lsp_refresh_interval (vty, area, interval, level);
+}
+
+DEFUN (no_lsp_refresh_interval,
+       no_lsp_refresh_interval_cmd,
+       "no lsp-refresh-interval",
+       NO_STR
+       "LSP refresh interval in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = DEFAULT_MAX_LSP_GEN_INTERVAL;
+  level = IS_LEVEL_1 | IS_LEVEL_2;
+  return set_lsp_refresh_interval (vty, area, interval, level);
+}
+
+ALIAS (no_lsp_refresh_interval,
+       no_lsp_refresh_interval_arg_cmd,
+       "no lsp-refresh-interval <1-65235>",
+       NO_STR
+       "LSP refresh interval\n"
+       "LSP refresh interval in seconds\n")
+
+DEFUN (lsp_refresh_interval_l1,
+       lsp_refresh_interval_l1_cmd,
+       "lsp-refresh-interval level-1 <1-65235>",
+       "LSP refresh interval for Level 1 only\n"
+       "LSP refresh interval for Level 1 only in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = atoi (argv[0]);
+  level = IS_LEVEL_1;
+  return set_lsp_refresh_interval (vty, area, interval, level);
+}
+
+DEFUN (no_lsp_refresh_interval_l1,
+       no_lsp_refresh_interval_l1_cmd,
+       "no lsp-refresh-interval level-1",
+       NO_STR
+       "LSP refresh interval for Level 1 only in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = DEFAULT_MAX_LSP_GEN_INTERVAL;
+  level = IS_LEVEL_1;
+  return set_lsp_refresh_interval (vty, area, interval, level);
+}
+
+ALIAS (no_lsp_refresh_interval_l1,
+       no_lsp_refresh_interval_l1_arg_cmd,
+       "no lsp-refresh-interval level-1 <1-65235>",
+       NO_STR
+       "LSP refresh interval for Level 1 only\n"
+       "LSP refresh interval for Level 1 only in seconds\n")
+
+DEFUN (lsp_refresh_interval_l2,
+       lsp_refresh_interval_l2_cmd,
+       "lsp-refresh-interval level-2 <1-65235>",
+       "LSP refresh interval for Level 2 only\n"
+       "LSP refresh interval for Level 2 only in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = atoi (argv[0]);
+  level = IS_LEVEL_2;
+  return set_lsp_refresh_interval (vty, area, interval, level);
+}
+
+DEFUN (no_lsp_refresh_interval_l2,
+       no_lsp_refresh_interval_l2_cmd,
+       "no lsp-refresh-interval level-2",
+       NO_STR
+       "LSP refresh interval for Level 2 only in seconds\n")
+{
+  struct isis_area *area;
+  uint16_t interval;
+  int level;
+
+  area = vty->index;
+  interval = DEFAULT_MAX_LSP_GEN_INTERVAL;
+  level = IS_LEVEL_2;
+  return set_lsp_refresh_interval (vty, area, interval, level);
+}
+
+ALIAS (no_lsp_refresh_interval_l2,
+       no_lsp_refresh_interval_l2_arg_cmd,
+       "no lsp-refresh-interval level-2 <1-65235>",
+       NO_STR
+       "LSP refresh interval for Level 2 only\n"
+       "LSP refresh interval for Level 2 only in seconds\n")
+
+DEFUN (log_adj_changes,
+       log_adj_changes_cmd,
+       "log-adjacency-changes",
+       "Log changes in adjacency state\n")
+{
+  struct isis_area *area;
+
+  area = vty->index;
+  assert (area);
+
+  area->log_adj_changes = 1;
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_log_adj_changes,
+       no_log_adj_changes_cmd,
+       "no log-adjacency-changes",
+       "Stop logging changes in adjacency state\n")
+{
+  struct isis_area *area;
+
+  area = vty->index;
+  assert (area);
+
+  area->log_adj_changes = 0;
+
+  return CMD_SUCCESS;
+}
+
 #ifdef TOPOLOGY_GENERATE
+
 DEFUN (topology_generate_grid,
        topology_generate_grid_cmd,
        "topology generate grid <1-100> <1-100> <1-65000> [param] [param] "
@@ -1623,7 +2715,7 @@
       generate_topology_lsps (area);
       /* Regenerate L1 LSP to get two way connection to the generated
        * topology. */
-      lsp_regenerate_schedule (area);
+      lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
     }
 
   return CMD_SUCCESS;
@@ -1633,7 +2725,7 @@
        show_isis_generated_topology_cmd,
        "show isis generated-topologies",
        SHOW_STR
-       "CLNS network information\n"
+       "ISIS network information\n"
        "Show generated topologies\n")
 {
   struct isis_area *area;
@@ -1717,183 +2809,9 @@
   area->topology_basedynh = strndup (argv[0], 16); 
   return CMD_SUCCESS;
 }
+
 #endif /* TOPOLOGY_GENERATE */
 
-DEFUN (lsp_lifetime,
-       lsp_lifetime_cmd,
-       "lsp-lifetime <380-65535>",
-       "Maximum LSP lifetime\n"
-       "LSP lifetime in seconds\n")
-{
-  struct isis_area *area;
-  uint16_t interval;
-
-  area = vty->index;
-  assert (area);
-
-  interval = atoi (argv[0]);
-
-  if (interval < ISIS_MIN_LSP_LIFETIME)
-    {
-      vty_out (vty, "LSP lifetime (%us) below %us%s",
-	       interval, ISIS_MIN_LSP_LIFETIME, VTY_NEWLINE);
-
-      return CMD_WARNING;
-    }
-
-
-  area->max_lsp_lifetime[0] = interval;
-  area->max_lsp_lifetime[1] = interval;
-  area->lsp_refresh[0] = interval - 300;
-  area->lsp_refresh[1] = interval - 300;
-
-  if (area->t_lsp_refresh[0])
-    {
-      thread_cancel (area->t_lsp_refresh[0]);
-      thread_execute (master, lsp_refresh_l1, area, 0);
-    }
-
-  if (area->t_lsp_refresh[1])
-    {
-      thread_cancel (area->t_lsp_refresh[1]);
-      thread_execute (master, lsp_refresh_l2, area, 0);
-    }
-
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (no_lsp_lifetime,
-       no_lsp_lifetime_cmd,
-       "no lsp-lifetime",
-       NO_STR
-       "LSP lifetime in seconds\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  area->max_lsp_lifetime[0] = MAX_AGE;	/* 1200s */
-  area->max_lsp_lifetime[1] = MAX_AGE;	/* 1200s */
-  area->lsp_refresh[0] = MAX_LSP_GEN_INTERVAL;	/*  900s */
-  area->lsp_refresh[1] = MAX_LSP_GEN_INTERVAL;	/*  900s */
-
-  return CMD_SUCCESS;
-}
-
-ALIAS (no_lsp_lifetime,
-       no_lsp_lifetime_arg_cmd,
-       "no lsp-lifetime <380-65535>",
-       NO_STR
-       "Maximum LSP lifetime\n"
-       "LSP lifetime in seconds\n")
-
-DEFUN (lsp_lifetime_l1,
-       lsp_lifetime_l1_cmd,
-       "lsp-lifetime level-1 <380-65535>",
-       "Maximum LSP lifetime for Level 1 only\n"
-       "LSP lifetime for Level 1 only in seconds\n")
-{
-  struct isis_area *area;
-  uint16_t interval;
-
-  area = vty->index;
-  assert (area);
-
-  interval = atoi (argv[0]);
-
-  if (interval < ISIS_MIN_LSP_LIFETIME)
-    {
-      vty_out (vty, "Level-1 LSP lifetime (%us) below %us%s",
-	       interval, ISIS_MIN_LSP_LIFETIME, VTY_NEWLINE);
-
-      return CMD_WARNING;
-    }
-
-
-  area->max_lsp_lifetime[0] = interval;
-  area->lsp_refresh[0] = interval - 300;
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (no_lsp_lifetime_l1,
-       no_lsp_lifetime_l1_cmd,
-       "no lsp-lifetime level-1",
-       NO_STR
-       "LSP lifetime for Level 1 only in seconds\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  area->max_lsp_lifetime[0] = MAX_AGE;	/* 1200s */
-  area->lsp_refresh[0] = MAX_LSP_GEN_INTERVAL;	/*  900s */
-
-  return CMD_SUCCESS;
-}
-
-ALIAS (no_lsp_lifetime_l1,
-       no_lsp_lifetime_l1_arg_cmd,
-       "no lsp-lifetime level-1 <380-65535>",
-       NO_STR
-       "Maximum LSP lifetime for Level 1 only\n"
-       "LSP lifetime for Level 1 only in seconds\n")
-
-DEFUN (lsp_lifetime_l2,
-       lsp_lifetime_l2_cmd,
-       "lsp-lifetime level-2 <380-65535>",
-       "Maximum LSP lifetime for Level 2 only\n"
-       "LSP lifetime for Level 2 only in seconds\n")
-{
-  struct isis_area *area;
-  uint16_t interval;
-
-  area = vty->index;
-  assert (area);
-
-  interval = atoi (argv[0]);
-
-  if (interval < ISIS_MIN_LSP_LIFETIME)
-    {
-      vty_out (vty, "Level-2 LSP lifetime (%us) below %us%s",
-	       interval, ISIS_MIN_LSP_LIFETIME, VTY_NEWLINE);
-
-      return CMD_WARNING;
-    }
-
-  area->max_lsp_lifetime[1] = interval;
-  area->lsp_refresh[1] = interval - 300;
-
-  return CMD_SUCCESS;
-}
-
-DEFUN (no_lsp_lifetime_l2,
-       no_lsp_lifetime_l2_cmd,
-       "no lsp-lifetime level-2",
-       NO_STR
-       "LSP lifetime for Level 2 only in seconds\n")
-{
-  struct isis_area *area;
-
-  area = vty->index;
-  assert (area);
-
-  area->max_lsp_lifetime[1] = MAX_AGE;	/* 1200s */
-  area->lsp_refresh[1] = MAX_LSP_GEN_INTERVAL;	/*  900s */
-
-  return CMD_SUCCESS;
-}
-
-ALIAS (no_lsp_lifetime_l2,
-       no_lsp_lifetime_l2_arg_cmd,
-       "no lsp-lifetime level-2 <380-65535>",
-       NO_STR
-       "Maximum LSP lifetime for Level 2 only\n"
-       "LSP lifetime for Level 2 only in seconds\n")
-
 /* IS-IS configuration write function */
 int
 isis_config_write (struct vty *vty)
@@ -1939,25 +2857,27 @@
 	      vty_out (vty, " metric-style transition%s", VTY_NEWLINE);
 	    write++;
 	  }
-
+	/* ISIS - overload-bit */
+	if (area->overload_bit)
+	  {
+	    vty_out (vty, " set-overload-bit%s", VTY_NEWLINE);
+	    write++;
+	  }
 	/* ISIS - Area is-type (level-1-2 is default) */
 	if (area->is_type == IS_LEVEL_1)
 	  {
 	    vty_out (vty, " is-type level-1%s", VTY_NEWLINE);
 	    write++;
 	  }
-	else
+	else if (area->is_type == IS_LEVEL_2)
 	  {
-	    if (area->is_type == IS_LEVEL_2)
-	      {
-		vty_out (vty, " is-type level-2-only%s", VTY_NEWLINE);
-		write++;
-	      }
+	    vty_out (vty, " is-type level-2-only%s", VTY_NEWLINE);
+	    write++;
 	  }
 	/* ISIS - Lsp generation interval */
 	if (area->lsp_gen_interval[0] == area->lsp_gen_interval[1])
 	  {
-	    if (area->lsp_gen_interval[0] != LSP_GEN_INTERVAL_DEFAULT)
+	    if (area->lsp_gen_interval[0] != DEFAULT_MIN_LSP_GEN_INTERVAL)
 	      {
 		vty_out (vty, " lsp-gen-interval %d%s",
 			 area->lsp_gen_interval[0], VTY_NEWLINE);
@@ -1966,13 +2886,13 @@
 	  }
 	else
 	  {
-	    if (area->lsp_gen_interval[0] != LSP_GEN_INTERVAL_DEFAULT)
+	    if (area->lsp_gen_interval[0] != DEFAULT_MIN_LSP_GEN_INTERVAL)
 	      {
 		vty_out (vty, " lsp-gen-interval level-1 %d%s",
 			 area->lsp_gen_interval[0], VTY_NEWLINE);
 		write++;
 	      }
-	    if (area->lsp_gen_interval[1] != LSP_GEN_INTERVAL_DEFAULT)
+	    if (area->lsp_gen_interval[1] != DEFAULT_MIN_LSP_GEN_INTERVAL)
 	      {
 		vty_out (vty, " lsp-gen-interval level-2 %d%s",
 			 area->lsp_gen_interval[1], VTY_NEWLINE);
@@ -1982,28 +2902,53 @@
 	/* ISIS - LSP lifetime */
 	if (area->max_lsp_lifetime[0] == area->max_lsp_lifetime[1])
 	  {
-	    if (area->max_lsp_lifetime[0] != MAX_AGE)
+	    if (area->max_lsp_lifetime[0] != DEFAULT_LSP_LIFETIME)
 	      {
-		vty_out (vty, " lsp-lifetime %u%s", area->max_lsp_lifetime[0],
+		vty_out (vty, " max-lsp-lifetime %u%s", area->max_lsp_lifetime[0],
 			 VTY_NEWLINE);
 		write++;
 	      }
 	  }
 	else
 	  {
-	    if (area->max_lsp_lifetime[0] != MAX_AGE)
+	    if (area->max_lsp_lifetime[0] != DEFAULT_LSP_LIFETIME)
 	      {
-		vty_out (vty, " lsp-lifetime level-1 %u%s",
+		vty_out (vty, " max-lsp-lifetime level-1 %u%s",
 			 area->max_lsp_lifetime[0], VTY_NEWLINE);
 		write++;
 	      }
-	    if (area->max_lsp_lifetime[1] != MAX_AGE)
+	    if (area->max_lsp_lifetime[1] != DEFAULT_LSP_LIFETIME)
 	      {
-		vty_out (vty, " lsp-lifetime level-2 %u%s",
+		vty_out (vty, " max-lsp-lifetime level-2 %u%s",
 			 area->max_lsp_lifetime[1], VTY_NEWLINE);
 		write++;
 	      }
 	  }
+	/* ISIS - LSP refresh interval */
+	if (area->lsp_refresh[0] == area->lsp_refresh[1])
+	  {
+	    if (area->lsp_refresh[0] != DEFAULT_MAX_LSP_GEN_INTERVAL)
+	      {
+		vty_out (vty, " lsp-refresh-interval %u%s", area->lsp_refresh[0],
+			 VTY_NEWLINE);
+		write++;
+	      }
+	  }
+	else
+	  {
+	    if (area->lsp_refresh[0] != DEFAULT_MAX_LSP_GEN_INTERVAL)
+	      {
+		vty_out (vty, " lsp-refresh-interval level-1 %u%s",
+			 area->lsp_refresh[0], VTY_NEWLINE);
+		write++;
+	      }
+	    if (area->lsp_refresh[1] != DEFAULT_MAX_LSP_GEN_INTERVAL)
+	      {
+		vty_out (vty, " lsp-refresh-interval level-2 %u%s",
+			 area->lsp_refresh[1], VTY_NEWLINE);
+		write++;
+	      }
+	  }
 	/* Minimum SPF interval. */
 	if (area->min_spf_interval[0] == area->min_spf_interval[1])
 	  {
@@ -2030,9 +2975,9 @@
 	      }
 	  }
 	/* Authentication passwords. */
-	if (area->area_passwd.len > 0)
+	if (area->area_passwd.type == ISIS_PASSWD_TYPE_HMAC_MD5)
 	  {
-	    vty_out(vty, " area-password %s", area->area_passwd.passwd);
+	    vty_out(vty, " area-password md5 %s", area->area_passwd.passwd);
 	    if (CHECK_FLAG(area->area_passwd.snp_auth, SNP_AUTH_SEND))
 	      {
 		vty_out(vty, " authenticate snp ");
@@ -2043,10 +2988,40 @@
 	      }
 	    vty_out(vty, "%s", VTY_NEWLINE);
 	    write++; 
-	  }  
-	if (area->domain_passwd.len > 0)
+	  }
+        else if (area->area_passwd.type == ISIS_PASSWD_TYPE_CLEARTXT)
+          {
+	    vty_out(vty, " area-password clear %s", area->area_passwd.passwd);
+	    if (CHECK_FLAG(area->area_passwd.snp_auth, SNP_AUTH_SEND))
+	      {
+		vty_out(vty, " authenticate snp ");
+		if (CHECK_FLAG(area->area_passwd.snp_auth, SNP_AUTH_RECV))
+		  vty_out(vty, "validate");
+		else
+		  vty_out(vty, "send-only");
+	      }
+	    vty_out(vty, "%s", VTY_NEWLINE);
+	    write++; 
+          }
+	if (area->domain_passwd.type == ISIS_PASSWD_TYPE_HMAC_MD5)
 	  {
-	    vty_out(vty, " domain-password %s", area->domain_passwd.passwd);
+            vty_out(vty, " domain-password md5 %s",
+                    area->domain_passwd.passwd);
+	    if (CHECK_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_SEND))
+	      {
+		vty_out(vty, " authenticate snp ");
+		if (CHECK_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_RECV))
+		  vty_out(vty, "validate");
+		else
+		  vty_out(vty, "send-only");
+	      }
+	    vty_out(vty, "%s", VTY_NEWLINE);
+	    write++;
+	  }
+        else if (area->domain_passwd.type == ISIS_PASSWD_TYPE_CLEARTXT)
+	  {
+	    vty_out(vty, " domain-password clear %s",
+                    area->domain_passwd.passwd); 
 	    if (CHECK_FLAG(area->domain_passwd.snp_auth, SNP_AUTH_SEND))
 	      {
 		vty_out(vty, " authenticate snp ");
@@ -2059,12 +3034,18 @@
 	    write++;
 	  }
 
+	if (area->log_adj_changes)
+	  {
+	    vty_out (vty, " log-adjacency-changes%s", VTY_NEWLINE);
+	    write++;
+	  }
+
 #ifdef TOPOLOGY_GENERATE
 	if (memcmp (area->topology_baseis, DEFAULT_TOPOLOGY_BASEIS,
 		    ISIS_SYS_ID_LEN))
 	  {
 	    vty_out (vty, " topology base-is %s%s",
-		     sysid_print (area->topology_baseis), VTY_NEWLINE);
+		     sysid_print ((u_char *)area->topology_baseis), VTY_NEWLINE);
 	    write++;
 	  }
 	if (area->topology_basedynh)
@@ -2087,7 +3068,7 @@
   return write;
 }
 
-static struct cmd_node isis_node = {
+struct cmd_node isis_node = {
   ISIS_NODE,
   "%s(config-router)# ",
   1
@@ -2099,23 +3080,43 @@
   /* Install IS-IS top node */
   install_node (&isis_node, isis_config_write);
 
-  install_element (VIEW_NODE, &show_clns_neighbors_cmd);
-  install_element (VIEW_NODE, &show_isis_neighbors_cmd);
-  install_element (VIEW_NODE, &show_clns_neighbors_detail_cmd);
-  install_element (VIEW_NODE, &show_isis_neighbors_detail_cmd);
+  install_element (VIEW_NODE, &show_isis_summary_cmd);
+
+  install_element (VIEW_NODE, &show_isis_interface_cmd);
+  install_element (VIEW_NODE, &show_isis_interface_detail_cmd);
+  install_element (VIEW_NODE, &show_isis_interface_arg_cmd);
+
+  install_element (VIEW_NODE, &show_isis_neighbor_cmd);
+  install_element (VIEW_NODE, &show_isis_neighbor_detail_cmd);
+  install_element (VIEW_NODE, &show_isis_neighbor_arg_cmd);
+  install_element (VIEW_NODE, &clear_isis_neighbor_cmd);
+  install_element (VIEW_NODE, &clear_isis_neighbor_arg_cmd);
 
   install_element (VIEW_NODE, &show_hostname_cmd);
   install_element (VIEW_NODE, &show_database_cmd);
+  install_element (VIEW_NODE, &show_database_arg_cmd);
+  install_element (VIEW_NODE, &show_database_arg_detail_cmd);
   install_element (VIEW_NODE, &show_database_detail_cmd);
+  install_element (VIEW_NODE, &show_database_detail_arg_cmd);
 
-  install_element (ENABLE_NODE, &show_clns_neighbors_cmd);
-  install_element (ENABLE_NODE, &show_isis_neighbors_cmd);
-  install_element (ENABLE_NODE, &show_clns_neighbors_detail_cmd);
-  install_element (ENABLE_NODE, &show_isis_neighbors_detail_cmd);
+  install_element (ENABLE_NODE, &show_isis_summary_cmd);
+
+  install_element (ENABLE_NODE, &show_isis_interface_cmd);
+  install_element (ENABLE_NODE, &show_isis_interface_detail_cmd);
+  install_element (ENABLE_NODE, &show_isis_interface_arg_cmd);
+
+  install_element (ENABLE_NODE, &show_isis_neighbor_cmd);
+  install_element (ENABLE_NODE, &show_isis_neighbor_detail_cmd);
+  install_element (ENABLE_NODE, &show_isis_neighbor_arg_cmd);
+  install_element (ENABLE_NODE, &clear_isis_neighbor_cmd);
+  install_element (ENABLE_NODE, &clear_isis_neighbor_arg_cmd);
 
   install_element (ENABLE_NODE, &show_hostname_cmd);
   install_element (ENABLE_NODE, &show_database_cmd);
+  install_element (ENABLE_NODE, &show_database_arg_cmd);
+  install_element (ENABLE_NODE, &show_database_arg_detail_cmd);
   install_element (ENABLE_NODE, &show_database_detail_cmd);
+  install_element (ENABLE_NODE, &show_database_detail_arg_cmd);
   install_element (ENABLE_NODE, &show_debugging_cmd);
 
   install_node (&debug_node, config_write_debug);
@@ -2142,6 +3143,8 @@
   install_element (ENABLE_NODE, &no_debug_isis_rtevents_cmd);
   install_element (ENABLE_NODE, &debug_isis_events_cmd);
   install_element (ENABLE_NODE, &no_debug_isis_events_cmd);
+  install_element (ENABLE_NODE, &debug_isis_packet_dump_cmd);
+  install_element (ENABLE_NODE, &no_debug_isis_packet_dump_cmd);
 
   install_element (CONFIG_NODE, &debug_isis_adj_cmd);
   install_element (CONFIG_NODE, &no_debug_isis_adj_cmd);
@@ -2165,6 +3168,8 @@
   install_element (CONFIG_NODE, &no_debug_isis_rtevents_cmd);
   install_element (CONFIG_NODE, &debug_isis_events_cmd);
   install_element (CONFIG_NODE, &no_debug_isis_events_cmd);
+  install_element (CONFIG_NODE, &debug_isis_packet_dump_cmd);
+  install_element (CONFIG_NODE, &no_debug_isis_packet_dump_cmd);
 
   install_element (CONFIG_NODE, &router_isis_cmd);
   install_element (CONFIG_NODE, &no_router_isis_cmd);
@@ -2177,12 +3182,16 @@
   install_element (ISIS_NODE, &is_type_cmd);
   install_element (ISIS_NODE, &no_is_type_cmd);
 
-  install_element (ISIS_NODE, &area_passwd_cmd);
-  install_element (ISIS_NODE, &area_passwd_snpauth_cmd);
+  install_element (ISIS_NODE, &area_passwd_md5_cmd);
+  install_element (ISIS_NODE, &area_passwd_md5_snpauth_cmd);
+  install_element (ISIS_NODE, &area_passwd_clear_cmd);
+  install_element (ISIS_NODE, &area_passwd_clear_snpauth_cmd);
   install_element (ISIS_NODE, &no_area_passwd_cmd);
 
-  install_element (ISIS_NODE, &domain_passwd_cmd);
-  install_element (ISIS_NODE, &domain_passwd_snpauth_cmd);
+  install_element (ISIS_NODE, &domain_passwd_md5_cmd);
+  install_element (ISIS_NODE, &domain_passwd_md5_snpauth_cmd);
+  install_element (ISIS_NODE, &domain_passwd_clear_cmd);
+  install_element (ISIS_NODE, &domain_passwd_clear_snpauth_cmd);
   install_element (ISIS_NODE, &no_domain_passwd_cmd);
 
   install_element (ISIS_NODE, &lsp_gen_interval_cmd);
@@ -2205,21 +3214,38 @@
   install_element (ISIS_NODE, &no_spf_interval_l2_cmd);
   install_element (ISIS_NODE, &no_spf_interval_l2_arg_cmd);
 
-  install_element (ISIS_NODE, &lsp_lifetime_cmd);
-  install_element (ISIS_NODE, &no_lsp_lifetime_cmd);
-  install_element (ISIS_NODE, &no_lsp_lifetime_arg_cmd);
-  install_element (ISIS_NODE, &lsp_lifetime_l1_cmd);
-  install_element (ISIS_NODE, &no_lsp_lifetime_l1_cmd);
-  install_element (ISIS_NODE, &no_lsp_lifetime_l1_arg_cmd);
-  install_element (ISIS_NODE, &lsp_lifetime_l2_cmd);
-  install_element (ISIS_NODE, &no_lsp_lifetime_l2_cmd);
-  install_element (ISIS_NODE, &no_lsp_lifetime_l2_arg_cmd);
+  install_element (ISIS_NODE, &max_lsp_lifetime_cmd);
+  install_element (ISIS_NODE, &no_max_lsp_lifetime_cmd);
+  install_element (ISIS_NODE, &no_max_lsp_lifetime_arg_cmd);
+  install_element (ISIS_NODE, &max_lsp_lifetime_l1_cmd);
+  install_element (ISIS_NODE, &no_max_lsp_lifetime_l1_cmd);
+  install_element (ISIS_NODE, &no_max_lsp_lifetime_l1_arg_cmd);
+  install_element (ISIS_NODE, &max_lsp_lifetime_l2_cmd);
+  install_element (ISIS_NODE, &no_max_lsp_lifetime_l2_cmd);
+  install_element (ISIS_NODE, &no_max_lsp_lifetime_l2_arg_cmd);
+
+  install_element (ISIS_NODE, &lsp_refresh_interval_cmd);
+  install_element (ISIS_NODE, &no_lsp_refresh_interval_cmd);
+  install_element (ISIS_NODE, &no_lsp_refresh_interval_arg_cmd);
+  install_element (ISIS_NODE, &lsp_refresh_interval_l1_cmd);
+  install_element (ISIS_NODE, &no_lsp_refresh_interval_l1_cmd);
+  install_element (ISIS_NODE, &no_lsp_refresh_interval_l1_arg_cmd);
+  install_element (ISIS_NODE, &lsp_refresh_interval_l2_cmd);
+  install_element (ISIS_NODE, &no_lsp_refresh_interval_l2_cmd);
+  install_element (ISIS_NODE, &no_lsp_refresh_interval_l2_arg_cmd);
+
+  install_element (ISIS_NODE, &set_overload_bit_cmd);
+  install_element (ISIS_NODE, &no_set_overload_bit_cmd);
 
   install_element (ISIS_NODE, &dynamic_hostname_cmd);
   install_element (ISIS_NODE, &no_dynamic_hostname_cmd);
 
   install_element (ISIS_NODE, &metric_style_cmd);
   install_element (ISIS_NODE, &no_metric_style_cmd);
+
+  install_element (ISIS_NODE, &log_adj_changes_cmd);
+  install_element (ISIS_NODE, &no_log_adj_changes_cmd);
+
 #ifdef TOPOLOGY_GENERATE
   install_element (ISIS_NODE, &topology_generate_grid_cmd);
   install_element (ISIS_NODE, &topology_baseis_cmd);
@@ -2229,9 +3255,4 @@
   install_element (VIEW_NODE, &show_isis_generated_topology_cmd);
   install_element (ENABLE_NODE, &show_isis_generated_topology_cmd);
 #endif /* TOPOLOGY_GENERATE */
-
-  isis_new (0);
-  isis_circuit_init ();
-  isis_zebra_init ();
-  isis_spf_cmds_init ();
 }
diff --git a/isisd/isisd.h b/isisd/isisd.h
index b17982e..5db485f 100644
--- a/isisd/isisd.h
+++ b/isisd/isisd.h
@@ -40,6 +40,7 @@
   u_long process_id;
   int sysid_set;
   u_char sysid[ISIS_SYS_ID_LEN];	/* SystemID for this IS */
+  u_int32_t router_id;          /* Router ID from zebra */
   struct list *area_list;	/* list of IS-IS areas */
   struct list *init_circ_list;
   struct list *nexthops;	/* IPv4 next hops from this IS */
@@ -78,6 +79,8 @@
 #endif
 };
 
+extern struct isis *isis;
+
 struct isis_area
 {
   struct isis *isis;				  /* back pointer */
@@ -92,11 +95,8 @@
   struct list *circuit_list;	/* IS-IS circuits */
   struct flags flags;
   struct thread *t_tick;	/* LSP walker */
-  struct thread *t_remove_aged;
-  struct thread *t_lsp_l1_regenerate;
-  struct thread *t_lsp_l2_regenerate;
-  int lsp_regenerate_pending[ISIS_LEVELS];
   struct thread *t_lsp_refresh[ISIS_LEVELS];
+  int lsp_regenerate_pending[ISIS_LEVELS];
 
   /*
    * Configurables 
@@ -114,6 +114,8 @@
   struct list *area_addrs;
   u_int16_t max_lsp_lifetime[ISIS_LEVELS];
   char is_type;			/* level-1 level-1-2 or level-2-only */
+  /* are we overloaded? */
+  char overload_bit;
   u_int16_t lsp_refresh[ISIS_LEVELS];
   /* minimum time allowed before lsp retransmission */
   u_int16_t lsp_gen_interval[ISIS_LEVELS];
@@ -122,6 +124,8 @@
   /* the percentage of LSP mtu size used, before generating a new frag */
   int lsp_frag_threshold;
   int ip_circuits;
+  /* logging adjacency changes? */
+  u_char log_adj_changes;
 #ifdef HAVE_IPV6
   int ipv6_circuits;
 #endif				/* HAVE_IPV6 */
@@ -130,14 +134,21 @@
 
 #ifdef TOPOLOGY_GENERATE
   struct list *topology;
-  char topology_baseis[ISIS_SYS_ID_LEN];  /* IS for the first IS emulated. */
+  u_char topology_baseis[ISIS_SYS_ID_LEN];  /* IS for the first IS emulated. */
   char *topology_basedynh;                /* Dynamic hostname base. */
   char top_params[200];                   /* FIXME: what is reasonable? */
 #endif /* TOPOLOGY_GENERATE */
 };
 
 void isis_init (void);
+void isis_new(unsigned long);
+struct isis_area *isis_area_create(const char *);
 struct isis_area *isis_area_lookup (const char *);
+int isis_area_get (struct vty *vty, const char *area_tag);
+void print_debug(struct vty *, int, int);
+
+/* Master of threads. */
+extern struct thread_master *master;
 
 #define DEBUG_ADJ_PACKETS                (1<<0)
 #define DEBUG_CHECKSUM_ERRORS            (1<<1)
@@ -151,5 +162,6 @@
 #define DEBUG_RTE_EVENTS                 (1<<9)
 #define DEBUG_EVENTS                     (1<<10)
 #define DEBUG_ZEBRA                      (1<<11)
+#define DEBUG_PACKET_DUMP                (1<<12)
 
 #endif /* ISISD_H */
diff --git a/lib/if.c b/lib/if.c
index 1e99ffb..e26aa04 100644
--- a/lib/if.c
+++ b/lib/if.c
@@ -146,7 +146,7 @@
     (*if_master.if_delete_hook) (ifp);
 
   /* Free connected address list */
-  list_delete (ifp->connected);
+  list_delete_all_node (ifp->connected);
 }
 
 /* Delete and free interface structure. */
@@ -157,6 +157,8 @@
 
   if_delete_retain(ifp);
 
+  list_free (ifp->connected);
+
   XFREE (MTYPE_IF, ifp);
 }
 
diff --git a/lib/linklist.h b/lib/linklist.h
index cc6867c..f0ae362 100644
--- a/lib/linklist.h
+++ b/lib/linklist.h
@@ -54,9 +54,9 @@
   void (*del) (void *val);
 };
 
-#define listnextnode(X) ((X)->next)
-#define listhead(X) ((X)->head)
-#define listtail(X) ((X)->tail)
+#define listnextnode(X) ((X) ? ((X)->next) : NULL)
+#define listhead(X) ((X) ? ((X)->head) : NULL)
+#define listtail(X) ((X) ? ((X)->tail) : NULL)
 #define listcount(X) ((X)->count)
 #define list_isempty(X) ((X)->head == NULL && (X)->tail == NULL)
 #define listgetdata(X) (assert((X)->data != NULL), (X)->data)
@@ -88,10 +88,10 @@
  * It is safe to delete the listnode using this macro.
  */
 #define ALL_LIST_ELEMENTS(list,node,nextnode,data) \
-  (node) = listhead(list); \
+  (node) = listhead(list), ((data) = NULL); \
   (node) != NULL && \
     ((data) = listgetdata(node),(nextnode) = listnextnode(node), 1); \
-  (node) = (nextnode)
+  (node) = (nextnode), ((data) = NULL)
 
 /* read-only list iteration macro.
  * Usage: as per ALL_LIST_ELEMENTS, but not safe to delete the listnode Only
@@ -100,9 +100,9 @@
  * of previous macro.
  */
 #define ALL_LIST_ELEMENTS_RO(list,node,data) \
-  (node) = listhead(list); \
+  (node) = listhead(list), ((data) = NULL);\
   (node) != NULL && ((data) = listgetdata(node), 1); \
-  (node) = listnextnode(node)
+  (node) = listnextnode(node), ((data) = NULL)
 
 /* these *do not* cleanup list nodes and referenced data, as the functions
  * do - these macros simply {de,at}tach a listnode from/to a list.
diff --git a/lib/md5.h b/lib/md5.h
index 3ce83a6..a03bf22 100644
--- a/lib/md5.h
+++ b/lib/md5.h
@@ -83,6 +83,7 @@
 } while (0)
 
 /* From RFC 2104 */
-void hmac_md5(unsigned char* text, int text_len, unsigned char* key, int key_len, caddr_t digest);
+void hmac_md5(unsigned char* text, int text_len, unsigned char* key,
+              int key_len, caddr_t digest);
 
 #endif /* ! _LIBZEBRA_MD5_H_*/
diff --git a/lib/memtypes.c b/lib/memtypes.c
index 245c8d8..acbd16b 100644
--- a/lib/memtypes.c
+++ b/lib/memtypes.c
@@ -248,6 +248,8 @@
   { MTYPE_ISIS_ROUTE_INFO,    "ISIS route info"			},
   { MTYPE_ISIS_NEXTHOP,       "ISIS nexthop"			},
   { MTYPE_ISIS_NEXTHOP6,      "ISIS nexthop6"			},
+  { MTYPE_ISIS_DICT,          "ISIS dictionary"			},
+  { MTYPE_ISIS_DICT_NODE,     "ISIS dictionary node"		},
   { -1, NULL },
 };
 
diff --git a/lib/stream.c b/lib/stream.c
index 983330f..b226a25 100644
--- a/lib/stream.c
+++ b/lib/stream.c
@@ -52,7 +52,7 @@
  * using stream_put..._at() functions.
  */
 #define STREAM_WARN_OFFSETS(S) \
-  zlog_warn ("&(struct stream): %p, size: %lu, endp: %lu, getp: %lu\n", \
+  zlog_warn ("&(struct stream): %p, size: %lu, getp: %lu, endp: %lu\n", \
              (S), \
              (unsigned long) (S)->size, \
              (unsigned long) (S)->getp, \
@@ -214,6 +214,20 @@
   s->getp = pos;
 }
 
+void
+stream_set_endp (struct stream *s, size_t pos)
+{
+  STREAM_VERIFY_SANE(s);
+
+  if (!GETP_VALID (s, pos))
+    {
+      STREAM_BOUND_WARN (s, "set endp");
+      pos = s->endp;
+    }
+
+  s->endp = pos;
+}
+
 /* Forward pointer. */
 void
 stream_forward_getp (struct stream *s, size_t size)
@@ -934,9 +948,9 @@
 
       if (fifo->head == NULL)
 	fifo->tail = NULL;
-    }
 
-  fifo->count--;
+      fifo->count--;
+    }
 
   return s; 
 }
diff --git a/lib/stream.h b/lib/stream.h
index 3e4ba7b..f10aa6d 100644
--- a/lib/stream.h
+++ b/lib/stream.h
@@ -146,6 +146,7 @@
 extern u_char *stream_get_data (struct stream *);
 
 extern void stream_set_getp (struct stream *, size_t);
+extern void stream_set_endp (struct stream *, size_t);
 extern void stream_forward_getp (struct stream *, size_t);
 extern void stream_forward_endp (struct stream *, size_t);
 
diff --git a/lib/zclient.c b/lib/zclient.c
index 3521d99..61c6f73 100644
--- a/lib/zclient.c
+++ b/lib/zclient.c
@@ -654,24 +654,8 @@
   /* Lookup/create interface by name. */
   ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ));
 
-  /* Read interface's index. */
-  ifp->ifindex = stream_getl (s);
+  zebra_interface_if_set_value (s, ifp);
 
-  /* Read interface's value. */
-  ifp->status = stream_getc (s);
-  ifp->flags = stream_getq (s);
-  ifp->metric = stream_getl (s);
-  ifp->mtu = stream_getl (s);
-  ifp->mtu6 = stream_getl (s);
-  ifp->bandwidth = stream_getl (s);
-#ifdef HAVE_STRUCT_SOCKADDR_DL
-  stream_get (&ifp->sdl, s, sizeof (ifp->sdl));
-#else
-  ifp->hw_addr_len = stream_getl (s);
-  if (ifp->hw_addr_len)
-    stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
-#endif /* HAVE_STRUCT_SOCKADDR_DL */
-  
   return ifp;
 }
 
@@ -699,16 +683,7 @@
   if (! ifp)
      return NULL;
 
-  /* Read interface's index. */
-  ifp->ifindex = stream_getl (s);
-
-  /* Read interface's value. */
-  ifp->status = stream_getc (s);
-  ifp->flags = stream_getq (s);
-  ifp->metric = stream_getl (s);
-  ifp->mtu = stream_getl (s);
-  ifp->mtu6 = stream_getl (s);
-  ifp->bandwidth = stream_getl (s);
+  zebra_interface_if_set_value (s, ifp);
 
   return ifp;
 }
@@ -758,6 +733,13 @@
   ifp->mtu = stream_getl (s);
   ifp->mtu6 = stream_getl (s);
   ifp->bandwidth = stream_getl (s);
+#ifdef HAVE_STRUCT_SOCKADDR_DL
+  stream_get (&ifp->sdl, s, sizeof (ifp->sdl));
+#else
+  ifp->hw_addr_len = stream_getl (s);
+  if (ifp->hw_addr_len)
+    stream_get (ifp->hw_addr, s, ifp->hw_addr_len);
+#endif /* HAVE_STRUCT_SOCKADDR_DL */
 }
 
 static int
diff --git a/zebra/rib.h b/zebra/rib.h
index 2872fc0..1b85c81 100644
--- a/zebra/rib.h
+++ b/zebra/rib.h
@@ -225,6 +225,10 @@
 extern struct nexthop *nexthop_blackhole_add (struct rib *);
 extern struct nexthop *nexthop_ipv4_add (struct rib *, struct in_addr *,
 					 struct in_addr *);
+extern struct nexthop *nexthop_ipv4_ifindex_add (struct rib *,
+                                                 struct in_addr *,
+                                                 struct in_addr *,
+                                                 unsigned int);
 extern void rib_lookup_and_dump (struct prefix_ipv4 *);
 extern void rib_lookup_and_pushup (struct prefix_ipv4 *);
 extern void rib_dump (const char *, const struct prefix_ipv4 *, const struct rib *);
diff --git a/zebra/rt_netlink.c b/zebra/rt_netlink.c
index f48df2b..73097bf 100644
--- a/zebra/rt_netlink.c
+++ b/zebra/rt_netlink.c
@@ -32,6 +32,7 @@
 #include "prefix.h"
 #include "connected.h"
 #include "table.h"
+#include "memory.h"
 #include "rib.h"
 #include "thread.h"
 #include "privs.h"
@@ -426,6 +427,37 @@
     }
 }
 
+/* Utility function to parse hardware link-layer address and update ifp */
+static void
+netlink_interface_update_hw_addr (struct rtattr **tb, struct interface *ifp)
+{
+  int i;
+
+  if (tb[IFLA_ADDRESS])
+    {
+      int hw_addr_len;
+
+      hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]);
+
+      if (hw_addr_len > INTERFACE_HWADDR_MAX)
+        zlog_warn ("Hardware address is too large: %d", hw_addr_len);
+      else
+        {
+          ifp->hw_addr_len = hw_addr_len;
+          memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len);
+
+          for (i = 0; i < hw_addr_len; i++)
+            if (ifp->hw_addr[i] != 0)
+              break;
+
+          if (i == hw_addr_len)
+            ifp->hw_addr_len = 0;
+          else
+            ifp->hw_addr_len = hw_addr_len;
+        }
+    }
+}
+
 /* Called from interface_lookup_netlink().  This function is only used
    during bootstrap. */
 static int
@@ -436,7 +468,6 @@
   struct rtattr *tb[IFLA_MAX + 1];
   struct interface *ifp;
   char *name;
-  int i;
 
   ifi = NLMSG_DATA (h);
 
@@ -474,30 +505,7 @@
 
   /* Hardware type and address. */
   ifp->hw_type = ifi->ifi_type;
-
-  if (tb[IFLA_ADDRESS])
-    {
-      int hw_addr_len;
-
-      hw_addr_len = RTA_PAYLOAD (tb[IFLA_ADDRESS]);
-
-      if (hw_addr_len > INTERFACE_HWADDR_MAX)
-        zlog_warn ("Hardware address is too large: %d", hw_addr_len);
-      else
-        {
-          ifp->hw_addr_len = hw_addr_len;
-          memcpy (ifp->hw_addr, RTA_DATA (tb[IFLA_ADDRESS]), hw_addr_len);
-
-          for (i = 0; i < hw_addr_len; i++)
-            if (ifp->hw_addr[i] != 0)
-              break;
-
-          if (i == hw_addr_len)
-            ifp->hw_addr_len = 0;
-          else
-            ifp->hw_addr_len = hw_addr_len;
-        }
-    }
+  netlink_interface_update_hw_addr (tb, ifp);
 
   if_add_update (ifp);
 
@@ -709,7 +717,6 @@
   if (tb[RTA_PREFSRC])
     src = RTA_DATA (tb[RTA_PREFSRC]);
 
-  /* Multipath treatment is needed. */
   if (tb[RTA_GATEWAY])
     gate = RTA_DATA (tb[RTA_GATEWAY]);
 
@@ -723,7 +730,64 @@
       memcpy (&p.prefix, dest, 4);
       p.prefixlen = rtm->rtm_dst_len;
 
-      rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, src, index, table, metric, 0, SAFI_UNICAST);
+      if (!tb[RTA_MULTIPATH])
+          rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, flags, &p, gate, src, index,
+                        table, metric, 0, SAFI_UNICAST);
+      else
+        {
+          /* This is a multipath route */
+
+          struct rib *rib;
+          struct rtnexthop *rtnh =
+            (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]);
+
+          len = RTA_PAYLOAD (tb[RTA_MULTIPATH]);
+
+          rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
+          rib->type = ZEBRA_ROUTE_KERNEL;
+          rib->distance = 0;
+          rib->flags = flags;
+          rib->metric = metric;
+          rib->table = table;
+          rib->nexthop_num = 0;
+          rib->uptime = time (NULL);
+
+          for (;;)
+            {
+              if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len)
+                break;
+
+              rib->nexthop_num++;
+              index = rtnh->rtnh_ifindex;
+              gate = 0;
+              if (rtnh->rtnh_len > sizeof (*rtnh))
+                {
+                  memset (tb, 0, sizeof (tb));
+                  netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh),
+                                        rtnh->rtnh_len - sizeof (*rtnh));
+                  if (tb[RTA_GATEWAY])
+                    gate = RTA_DATA (tb[RTA_GATEWAY]);
+                }
+
+              if (gate)
+                {
+                  if (index)
+                    nexthop_ipv4_ifindex_add (rib, gate, src, index);
+                  else
+                    nexthop_ipv4_add (rib, gate, src);
+                }
+              else
+                nexthop_ifindex_add (rib, index);
+
+              len -= NLMSG_ALIGN(rtnh->rtnh_len);
+              rtnh = RTNH_NEXT(rtnh);
+            }
+
+          if (rib->nexthop_num == 0)
+            XFREE (MTYPE_RIB, rib);
+          else
+            rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST);
+        }
     }
 #ifdef HAVE_IPV6
   if (rtm->rtm_family == AF_INET6)
@@ -867,7 +931,66 @@
         }
 
       if (h->nlmsg_type == RTM_NEWROUTE)
-        rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, src, index, table, metric, 0, SAFI_UNICAST);
+        {
+          if (!tb[RTA_MULTIPATH])
+            rib_add_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, src, index, table,
+                          metric, 0, SAFI_UNICAST);
+          else
+            {
+              /* This is a multipath route */
+
+              struct rib *rib;
+              struct rtnexthop *rtnh =
+                (struct rtnexthop *) RTA_DATA (tb[RTA_MULTIPATH]);
+
+              len = RTA_PAYLOAD (tb[RTA_MULTIPATH]);
+
+              rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
+              rib->type = ZEBRA_ROUTE_KERNEL;
+              rib->distance = 0;
+              rib->flags = 0;
+              rib->metric = metric;
+              rib->table = table;
+              rib->nexthop_num = 0;
+              rib->uptime = time (NULL);
+
+              for (;;)
+                {
+                  if (len < (int) sizeof (*rtnh) || rtnh->rtnh_len > len)
+                    break;
+
+                  rib->nexthop_num++;
+                  index = rtnh->rtnh_ifindex;
+                  gate = 0;
+                  if (rtnh->rtnh_len > sizeof (*rtnh))
+                    {
+                      memset (tb, 0, sizeof (tb));
+                      netlink_parse_rtattr (tb, RTA_MAX, RTNH_DATA (rtnh),
+                                            rtnh->rtnh_len - sizeof (*rtnh));
+                      if (tb[RTA_GATEWAY])
+                        gate = RTA_DATA (tb[RTA_GATEWAY]);
+                    }
+
+                  if (gate)
+                    {
+                      if (index)
+                        nexthop_ipv4_ifindex_add (rib, gate, src, index);
+                      else
+                        nexthop_ipv4_add (rib, gate, src);
+                    }
+                  else
+                    nexthop_ifindex_add (rib, index);
+
+                  len -= NLMSG_ALIGN(rtnh->rtnh_len);
+                  rtnh = RTNH_NEXT(rtnh);
+                }
+
+              if (rib->nexthop_num == 0)
+                XFREE (MTYPE_RIB, rib);
+              else
+                rib_add_ipv4_multipath (&p, rib, SAFI_UNICAST);
+            }
+        }
       else
         rib_delete_ipv4 (ZEBRA_ROUTE_KERNEL, 0, &p, gate, index, table, SAFI_UNICAST);
     }
@@ -960,6 +1083,8 @@
           ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
           ifp->metric = 1;
 
+          netlink_interface_update_hw_addr (tb, ifp);
+
           /* If new link is added. */
           if_add_update (ifp);
         }
@@ -970,6 +1095,8 @@
           ifp->mtu6 = ifp->mtu = *(int *) RTA_DATA (tb[IFLA_MTU]);
           ifp->metric = 1;
 
+          netlink_interface_update_hw_addr (tb, ifp);
+
           if (if_is_operative (ifp))
             {
               ifp->flags = ifi->ifi_flags & 0x0000fffff;
diff --git a/zebra/zebra_rib.c b/zebra/zebra_rib.c
index f7f4d0a..2fa439c 100644
--- a/zebra/zebra_rib.c
+++ b/zebra/zebra_rib.c
@@ -232,7 +232,7 @@
   return nexthop;
 }
 
-static struct nexthop *
+struct nexthop *
 nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4, 
                           struct in_addr *src, unsigned int ifindex)
 {
@@ -1279,14 +1279,30 @@
 static void
 rib_queue_add (struct zebra_t *zebra, struct route_node *rn)
 {
+  char buf[INET_ADDRSTRLEN];
+  assert (zebra && rn);
   
   if (IS_ZEBRA_DEBUG_RIB_Q)
-    {
-      char buf[INET6_ADDRSTRLEN];
+    inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN);
 
-      zlog_info ("%s: %s/%d: work queue added", __func__,
-		 inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN),
-		 rn->p.prefixlen);
+  /* Pointless to queue a route_node with no RIB entries to add or remove */
+  if (!rn->info)
+    {
+      zlog_debug ("%s: called for route_node (%p, %d) with no ribs",
+                  __func__, rn, rn->lock);
+      zlog_backtrace(LOG_DEBUG);
+      return;
+    }
+
+  if (IS_ZEBRA_DEBUG_RIB_Q)
+    zlog_info ("%s: %s/%d: work queue added", __func__, buf, rn->p.prefixlen);
+
+  assert (zebra);
+
+  if (zebra->ribq == NULL)
+    {
+      zlog_err ("%s: work_queue does not exist!", __func__);
+      return;
     }
 
   /*
@@ -1301,6 +1317,11 @@
     work_queue_add (zebra->ribq, zebra->mq);
 
   rib_meta_queue_add (zebra->mq, rn);
+
+  if (IS_ZEBRA_DEBUG_RIB_Q)
+    zlog_debug ("%s: %s/%d: rn %p queued", __func__, buf, rn->p.prefixlen, rn);
+
+  return;
 }
 
 /* Create new meta queue.
@@ -1328,6 +1349,8 @@
 static void
 rib_queue_init (struct zebra_t *zebra)
 {
+  assert (zebra);
+  
   if (! (zebra->ribq = work_queue_new (zebra->master, 
                                        "route_node processing")))
     {
@@ -1343,7 +1366,11 @@
   zebra->ribq->spec.hold = rib_process_hold_time;
   
   if (!(zebra->mq = meta_queue_new ()))
+  {
     zlog_err ("%s: could not initialise meta queue!", __func__);
+    return;
+  }
+  return;
 }
 
 /* RIB updates are processed via a queue of pointers to route_nodes.
@@ -2893,6 +2920,62 @@
   rib_sweep_table (vrf_table (AFI_IP, SAFI_UNICAST, 0));
   rib_sweep_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0));
 }
+
+/* Delete routes learned from a given client.  */
+/* TODO(wsun) May need to split the sweep process into multiple batches,
+ * so that the process won't take too long if the table is large. */
+static void
+rib_sweep_client_table (struct route_table *table, int rib_type)
+{
+  struct route_node *rn;
+  struct rib *rib;
+  struct rib *next;
+  int ret = 0;
+
+  if (table)
+    for (rn = route_top (table); rn; rn = route_next (rn))
+      for (rib = rn->info; rib; rib = next)
+	{
+	  next = rib->next;
+
+	  if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED))
+	    continue;
+
+	  if (rib->type == rib_type)
+            if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED))
+	      {
+                /* TODO(wsun) Is this mandatory? What about graceful restart/
+                 * non-stop forwarding */
+	        ret = rib_uninstall_kernel (rn, rib);
+	        if (! ret)
+                  rib_delnode (rn, rib);
+                else
+                  zlog_err ("%s: could not delete routes from kernel!",
+                            __func__);
+	      }
+            else
+              {
+                /* Always delete the node. */
+                rib_delnode (rn, rib);
+              }
+	}
+}
+
+/* Sweep all routes learned from a given client from RIB tables.  */
+void
+rib_sweep_client_route (struct zserv *client)
+{
+  assert(client);
+  int route_type = client->route_type;
+  if (route_type != ZEBRA_ROUTE_MAX)
+    {
+      zlog_debug ("%s: Removing existing routes from client type %d",
+                  __func__, route_type);
+      rib_sweep_client_table (vrf_table (AFI_IP, SAFI_UNICAST, 0), route_type);
+      rib_sweep_client_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0), route_type);
+    }
+}
+
 
 /* Remove specific by protocol routes from 'table'. */
 static unsigned long
diff --git a/zebra/zserv.c b/zebra/zserv.c
index 672dee8..b1f539d 100644
--- a/zebra/zserv.c
+++ b/zebra/zserv.c
@@ -140,6 +140,30 @@
   stream_putw (s, cmd);
 }
 
+static void
+zserv_encode_interface (struct stream *s, struct interface *ifp)
+{
+  /* Interface information. */
+  stream_put (s, ifp->name, INTERFACE_NAMSIZ);
+  stream_putl (s, ifp->ifindex);
+  stream_putc (s, ifp->status);
+  stream_putq (s, ifp->flags);
+  stream_putl (s, ifp->metric);
+  stream_putl (s, ifp->mtu);
+  stream_putl (s, ifp->mtu6);
+  stream_putl (s, ifp->bandwidth);
+#ifdef HAVE_STRUCT_SOCKADDR_DL
+  stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
+#else
+  stream_putl (s, ifp->hw_addr_len);
+  if (ifp->hw_addr_len)
+    stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
+#endif /* HAVE_STRUCT_SOCKADDR_DL */
+
+  /* Write packet size. */
+  stream_putw_at (s, 0, stream_get_endp (s));
+}
+
 /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
 /*
  * This function is called in the following situations:
@@ -163,28 +187,8 @@
   s = client->obuf;
   stream_reset (s);
 
-  /* Message type. */
   zserv_create_header (s, ZEBRA_INTERFACE_ADD);
-
-  /* Interface information. */
-  stream_put (s, ifp->name, INTERFACE_NAMSIZ);
-  stream_putl (s, ifp->ifindex);
-  stream_putc (s, ifp->status);
-  stream_putq (s, ifp->flags);
-  stream_putl (s, ifp->metric);
-  stream_putl (s, ifp->mtu);
-  stream_putl (s, ifp->mtu6);
-  stream_putl (s, ifp->bandwidth);
-#ifdef HAVE_STRUCT_SOCKADDR_DL
-  stream_put (s, &ifp->sdl, sizeof (ifp->sdl));
-#else
-  stream_putl (s, ifp->hw_addr_len);
-  if (ifp->hw_addr_len)
-    stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
-#endif /* HAVE_STRUCT_SOCKADDR_DL */
-
-  /* Write packet size. */
-  stream_putw_at (s, 0, stream_get_endp (s));
+  zserv_encode_interface (s, ifp);
 
   return zebra_server_send_message(client);
 }
@@ -201,21 +205,9 @@
 
   s = client->obuf;
   stream_reset (s);
-  
-  zserv_create_header (s, ZEBRA_INTERFACE_DELETE);
-  
-  /* Interface information. */
-  stream_put (s, ifp->name, INTERFACE_NAMSIZ);
-  stream_putl (s, ifp->ifindex);
-  stream_putc (s, ifp->status);
-  stream_putq (s, ifp->flags);
-  stream_putl (s, ifp->metric);
-  stream_putl (s, ifp->mtu);
-  stream_putl (s, ifp->mtu6);
-  stream_putl (s, ifp->bandwidth);
 
-  /* Write packet length. */
-  stream_putw_at (s, 0, stream_get_endp (s));
+  zserv_create_header (s, ZEBRA_INTERFACE_DELETE);
+  zserv_encode_interface (s, ifp);
 
   return zebra_server_send_message (client);
 }
@@ -328,19 +320,7 @@
   stream_reset (s);
 
   zserv_create_header (s, cmd);
-
-  /* Interface information. */
-  stream_put (s, ifp->name, INTERFACE_NAMSIZ);
-  stream_putl (s, ifp->ifindex);
-  stream_putc (s, ifp->status);
-  stream_putq (s, ifp->flags);
-  stream_putl (s, ifp->metric);
-  stream_putl (s, ifp->mtu);
-  stream_putl (s, ifp->mtu6);
-  stream_putl (s, ifp->bandwidth);
-
-  /* Write packet size. */
-  stream_putw_at (s, 0, stream_get_endp (s));
+  zserv_encode_interface (s, ifp);
 
   return zebra_server_send_message(client);
 }
@@ -761,6 +741,13 @@
   
   /* Type, flags, message. */
   rib->type = stream_getc (s);
+  /* Update client's route type if it is not done yet. */
+  /* It is done here since only zread_ipv4/6_add() and
+   * zread_ipv4/6_delete() decode Zebra messages and retrieve
+   * route types. */
+  if (client->route_type == ZEBRA_ROUTE_MAX)
+    client->route_type = rib->type;
+
   rib->flags = stream_getc (s);
   message = stream_getc (s); 
   safi = stream_getw (s);
@@ -798,10 +785,10 @@
 	    case ZEBRA_NEXTHOP_IPV6:
 	      stream_forward_getp (s, IPV6_MAX_BYTELEN);
 	      break;
-      case ZEBRA_NEXTHOP_BLACKHOLE:
-        nexthop_blackhole_add (rib);
-        break;
-	    }
+            case ZEBRA_NEXTHOP_BLACKHOLE:
+              nexthop_blackhole_add (rib);
+              break;
+            }
 	}
     }
 
@@ -826,7 +813,7 @@
   int i;
   struct stream *s;
   struct zapi_ipv4 api;
-  struct in_addr nexthop;
+  struct in_addr nexthop, *nexthop_p;
   unsigned long ifindex;
   struct prefix_ipv4 p;
   u_char nexthop_num;
@@ -836,6 +823,7 @@
   s = client->ibuf;
   ifindex = 0;
   nexthop.s_addr = 0;
+  nexthop_p = NULL;
 
   /* Type, flags, message. */
   api.type = stream_getc (s);
@@ -869,6 +857,7 @@
 	      break;
 	    case ZEBRA_NEXTHOP_IPV4:
 	      nexthop.s_addr = stream_get_ipv4 (s);
+	      nexthop_p = &nexthop;
 	      break;
 	    case ZEBRA_NEXTHOP_IPV6:
 	      stream_forward_getp (s, IPV6_MAX_BYTELEN);
@@ -889,7 +878,7 @@
   else
     api.metric = 0;
     
-  rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex,
+  rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
 		   client->rtm_table, api.safi);
   return 0;
 }
@@ -935,6 +924,11 @@
 
   /* Type, flags, message. */
   api.type = stream_getc (s);
+  /* Update the route type of the client. 
+   * Same as in zread_ipv4_add(). */
+  if (client->route_type == ZEBRA_ROUTE_MAX)
+    client->route_type = api.type;
+
   api.flags = stream_getc (s);
   api.message = stream_getc (s);
   api.safi = stream_getw (s);
@@ -1133,6 +1127,14 @@
 static void
 zebra_client_close (struct zserv *client)
 {
+  struct stream *s;
+
+  /* Sweep all routes learned from the client first. */
+  rib_sweep_client_route(client);
+  /* Reset the route type. It may not be necessary since the
+   * whole client will be freed. */
+  client->route_type = ZEBRA_ROUTE_MAX;
+
   /* Close file descriptor. */
   if (client->sock)
     {
@@ -1172,6 +1174,9 @@
 
   /* Make client input/output buffer. */
   client->sock = sock;
+  /* Set the default route type to ZEBRA_ROUTE_MAX; it will be updated
+   * once new routes are received. */
+  client->route_type = ZEBRA_ROUTE_MAX;
   client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
   client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
   client->wb = buffer_new(0);
diff --git a/zebra/zserv.h b/zebra/zserv.h
index 5e8bcca..3d7ebbc 100644
--- a/zebra/zserv.h
+++ b/zebra/zserv.h
@@ -38,6 +38,10 @@
   /* Client file descriptor. */
   int sock;
 
+  /* Client route type. */
+  /* Assuming each client contains only one type of route. */
+  int route_type;
+
   /* Input/output buffer to the client. */
   struct stream *ibuf;
   struct stream *obuf;