isisd: Attached-bit in LSP header

Set/reset attached-bit in LSP header:
    This patch provides support for set/reset attached_bit in the LSP header.
    In IS-IS networks, routing inter-area traffic from L1 areas is
    accomplished by sending the traffic to the nearest L1/L2 router.
    A L1/L2 router identifies itself by setting an attach-bit (ATT-bit) in its (LSP).

    The ATT-bit in LSP can be changed using the set-attached-bit or
    no-set-attached-bit commands (similar to ‘set-overload-bit’ and
    'no set-overload-bit’) using telnet terminal in router configuration mode.

    Steps:
    enable
    configure terminal
    router isis <Routing area>
    set-attached-bit

    V2: Removed looping through area list as this well set the bit for all
        areas in the list. This implementation now looks exactly like the
        current overload bit implementation.

    Signed-off-by: Amritha Nambiar <amritha.nambiar@intel.com>
diff --git a/isisd/isis_lsp.c b/isisd/isis_lsp.c
index 88593de..0b1dac1 100644
--- a/isisd/isis_lsp.c
+++ b/isisd/isis_lsp.c
@@ -442,7 +442,7 @@
 }
 
 static u_int8_t
-lsp_bits_generate (int level, int overload_bit)
+lsp_bits_generate (int level, int overload_bit, int attached_bit)
 {
   u_int8_t lsp_bits = 0;
   if (level == IS_LEVEL_1)
@@ -451,6 +451,8 @@
     lsp_bits = IS_LEVEL_1_AND_2;
   if (overload_bit)
     lsp_bits |= overload_bit;
+  if (attached_bit)
+    lsp_bits |= attached_bit;
   return lsp_bits;
 }
 
@@ -1135,7 +1137,8 @@
       return lsp;
     }
   lsp = lsp_new (frag_id, ntohs(lsp0->lsp_header->rem_lifetime), 0,
-                 lsp_bits_generate (level, area->overload_bit), 0, level);
+                 lsp_bits_generate (level, area->overload_bit,
+                 area->attached_bit), 0, level);
   lsp->area = area;
   lsp->own_lsp = 1;
   lsp_insert (lsp, area->lspdb[level - 1]);
@@ -1589,7 +1592,8 @@
     }
   rem_lifetime = lsp_rem_lifetime (area, level);
   newlsp = lsp_new (lspid, rem_lifetime, seq_num,
-                    area->is_type | area->overload_bit, 0, level);
+                    area->is_type | area->overload_bit | area->attached_bit,
+                    0, level);
   newlsp->area = area;
   newlsp->own_lsp = 1;
 
@@ -1656,7 +1660,8 @@
 
   lsp_clear_data (lsp);
   lsp_build (lsp, area);
-  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, area->overload_bit);
+  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, area->overload_bit,
+                                                 area->attached_bit);
   rem_lifetime = lsp_rem_lifetime (area, level);
   lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
   lsp_seqnum_update (lsp);
@@ -1666,7 +1671,8 @@
   for (ALL_LIST_ELEMENTS_RO (lsp->lspu.frags, node, frag))
     {
       frag->lsp_header->lsp_bits = lsp_bits_generate (level,
-                                                      area->overload_bit);
+                                                      area->overload_bit,
+                                                      area->attached_bit);
       /* Set the lifetime values of all the fragments to the same value,
        * so that no fragment expires before the lsp is refreshed.
        */
@@ -1816,7 +1822,8 @@
 
   lsp->level = level;
   /* RFC3787  section 4 SHOULD not set overload bit in pseudo LSPs */
-  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, 0);
+  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, 0,
+                                                 circuit->area->attached_bit);
 
   /*
    * add self to IS neighbours 
@@ -1946,7 +1953,9 @@
 
   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 = lsp_new (lsp_id, rem_lifetime, 1,
+                 circuit->area->is_type | circuit->area->attached_bit,
+                 0, level);
   lsp->area = circuit->area;
 
   lsp_build_pseudo (lsp, circuit, level);
@@ -2012,7 +2021,8 @@
   lsp_build_pseudo (lsp, circuit, level);
 
   /* RFC3787  section 4 SHOULD not set overload bit in pseudo LSPs */
-  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, 0);
+  lsp->lsp_header->lsp_bits = lsp_bits_generate (level, 0,
+                                                 circuit->area->attached_bit);
   rem_lifetime = lsp_rem_lifetime (circuit->area, level);
   lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
   lsp_inc_seqnum (lsp, 0);
@@ -2416,7 +2426,8 @@
 		     IS_LEVEL_1);
 
   lsp->lsp_header->lsp_bits = lsp_bits_generate (lsp->level,
-                                                 lsp->area->overload_bit);
+                                                 lsp->area->overload_bit,
+                                                 lsp->area->attached_bit);
   rem_lifetime = lsp_rem_lifetime (lsp->area, IS_LEVEL_1);
   lsp->lsp_header->rem_lifetime = htons (rem_lifetime);
 
@@ -2455,8 +2466,8 @@
       lspid[ISIS_SYS_ID_LEN - 2] = ((i >> 8) & 0xFF);
 
       rem_lifetime = lsp_rem_lifetime (area, IS_LEVEL_1);
-      lsp = lsp_new (lspid, rem_lifetime, 1, IS_LEVEL_1 | area->overload_bit,
-                     0, 1);
+      lsp = lsp_new (lspid, rem_lifetime, 1, IS_LEVEL_1 | area->overload_bit
+                     | area->attached_bit, 0, 1);
       if (!lsp)
 	return;
       lsp->area = area;
diff --git a/isisd/isisd.c b/isisd/isisd.c
index 898dfd2..20b8e50 100644
--- a/isisd/isisd.c
+++ b/isisd/isisd.c
@@ -2170,6 +2170,39 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (set_attached_bit,
+       set_attached_bit_cmd,
+       "set-attached-bit",
+       "Set attached bit to identify as L1/L2 router for inter-area traffic\n"
+       "Set attached bit\n")
+{
+  struct isis_area *area;
+
+  area = vty->index;
+  assert (area);
+
+  area->attached_bit = LSPBIT_ATT;
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_set_attached_bit,
+       no_set_attached_bit_cmd,
+       "no set-attached-bit",
+       "Reset attached bit\n")
+{
+  struct isis_area *area;
+
+  area = vty->index;
+  assert (area);
+
+  area->attached_bit = 0;
+  lsp_regenerate_schedule (area, IS_LEVEL_1 | IS_LEVEL_2, 1);
+
+  return CMD_SUCCESS;
+}
+
 DEFUN (dynamic_hostname,
        dynamic_hostname_cmd,
        "hostname dynamic",
@@ -3245,6 +3278,9 @@
   install_element (ISIS_NODE, &set_overload_bit_cmd);
   install_element (ISIS_NODE, &no_set_overload_bit_cmd);
 
+  install_element (ISIS_NODE, &set_attached_bit_cmd);
+  install_element (ISIS_NODE, &no_set_attached_bit_cmd);
+
   install_element (ISIS_NODE, &dynamic_hostname_cmd);
   install_element (ISIS_NODE, &no_dynamic_hostname_cmd);
 
diff --git a/isisd/isisd.h b/isisd/isisd.h
index 5db485f..838a08b 100644
--- a/isisd/isisd.h
+++ b/isisd/isisd.h
@@ -116,6 +116,8 @@
   char is_type;			/* level-1 level-1-2 or level-2-only */
   /* are we overloaded? */
   char overload_bit;
+  /* L1/L2 router identifier for inter-area traffic */
+  char attached_bit;
   u_int16_t lsp_refresh[ISIS_LEVELS];
   /* minimum time allowed before lsp retransmission */
   u_int16_t lsp_gen_interval[ISIS_LEVELS];