bgp: add "debug bgp allow-martians" next hops and related code/commands
diff --git a/bgpd/bgp_attr.c b/bgpd/bgp_attr.c
index f34e649..42da7ff 100644
--- a/bgpd/bgp_attr.c
+++ b/bgpd/bgp_attr.c
@@ -1249,7 +1249,8 @@
      gets ignored in any of these cases. */
   nexthop_n = stream_get_ipv4 (peer->ibuf);
   nexthop_h = ntohl (nexthop_n);
-  if (IPV4_NET0 (nexthop_h) || IPV4_NET127 (nexthop_h) || IPV4_CLASS_DE (nexthop_h))
+  if ((IPV4_NET0 (nexthop_h) || IPV4_NET127 (nexthop_h) || IPV4_CLASS_DE (nexthop_h))
+      && !BGP_DEBUG (allow_martians, ALLOW_MARTIANS)) /* loopbacks may be used in testing */
     {
       char buf[INET_ADDRSTRLEN];
       inet_ntop (AF_INET, &nexthop_n, buf, INET_ADDRSTRLEN);
diff --git a/bgpd/bgp_debug.c b/bgpd/bgp_debug.c
index 60e2777..90a378b 100644
--- a/bgpd/bgp_debug.c
+++ b/bgpd/bgp_debug.c
@@ -46,6 +46,7 @@
 unsigned long conf_bgp_debug_update;
 unsigned long conf_bgp_debug_normal;
 unsigned long conf_bgp_debug_zebra;
+unsigned long conf_bgp_debug_allow_martians;
 
 unsigned long term_bgp_debug_as4;
 unsigned long term_bgp_debug_fsm;
@@ -56,6 +57,7 @@
 unsigned long term_bgp_debug_update;
 unsigned long term_bgp_debug_normal;
 unsigned long term_bgp_debug_zebra;
+unsigned long term_bgp_debug_allow_martians;
 
 /* messages for BGP-4 status */
 const struct message bgp_status_msg[] = 
@@ -725,6 +727,48 @@
        BGP_STR
        "BGP Zebra messages\n")
 
+DEFUN (debug_bgp_allow_martians,
+       debug_bgp_allow_martians_cmd,
+       "debug bgp allow-martians",
+       DEBUG_STR
+       BGP_STR
+       "BGP allow martian next hops\n")
+{
+  if (vty->node == CONFIG_NODE)
+    DEBUG_ON (allow_martians, ALLOW_MARTIANS);
+  else
+    {
+      TERM_DEBUG_ON (allow_martians, ALLOW_MARTIANS);
+      vty_out (vty, "BGP allow_martian next hop debugging is on%s", VTY_NEWLINE);
+    }
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_debug_bgp_allow_martians,
+       no_debug_bgp_allow_martians_cmd,
+       "no debug bgp allow-martians",
+       NO_STR
+       DEBUG_STR
+       BGP_STR
+       "BGP allow martian next hops\n")
+{
+  if (vty->node == CONFIG_NODE)
+    DEBUG_OFF (allow_martians, ALLOW_MARTIANS);
+  else
+    {
+      TERM_DEBUG_OFF (allow_martians, ALLOW_MARTIANS);
+      vty_out (vty, "BGP allow martian next hop debugging is off%s", VTY_NEWLINE);
+    }
+  return CMD_SUCCESS;
+}
+
+ALIAS (no_debug_bgp_allow_martians,
+       undebug_bgp_allow_martians_cmd,
+       "undebug bgp allow-martians",
+       UNDEBUG_STR
+       BGP_STR
+       "BGP allow martian next hops\n")
+
 DEFUN (no_debug_bgp_all,
        no_debug_bgp_all_cmd,
        "no debug all bgp",
@@ -743,6 +787,7 @@
   TERM_DEBUG_OFF (fsm, FSM);
   TERM_DEBUG_OFF (filter, FILTER);
   TERM_DEBUG_OFF (zebra, ZEBRA);
+  TERM_DEBUG_OFF (allow_martians, ALLOW_MARTIANS);
   vty_out (vty, "All possible debugging has been turned off%s", VTY_NEWLINE);
       
   return CMD_SUCCESS;
@@ -786,6 +831,8 @@
     vty_out (vty, "  BGP as4 debugging is on%s", VTY_NEWLINE);
   if (BGP_DEBUG (as4, AS4_SEGMENT))
     vty_out (vty, "  BGP as4 aspath segment debugging is on%s", VTY_NEWLINE);
+  if (BGP_DEBUG (allow_martians, ALLOW_MARTIANS))
+    vty_out (vty, "  BGP allow martian next hop debugging is on%s", VTY_NEWLINE);
   vty_out (vty, "%s", VTY_NEWLINE);
   return CMD_SUCCESS;
 }
@@ -859,6 +906,12 @@
       write++;
     }
 
+  if (CONF_BGP_DEBUG (allow_martians, ALLOW_MARTIANS))
+    {
+      vty_out (vty, "debug bgp allow-martians%s", VTY_NEWLINE);
+      write++;
+    }
+
   return write;
 }
 
@@ -897,6 +950,8 @@
   install_element (CONFIG_NODE, &debug_bgp_normal_cmd);
   install_element (ENABLE_NODE, &debug_bgp_zebra_cmd);
   install_element (CONFIG_NODE, &debug_bgp_zebra_cmd);
+  install_element (ENABLE_NODE, &debug_bgp_allow_martians_cmd);
+  install_element (CONFIG_NODE, &debug_bgp_allow_martians_cmd);
 
   install_element (ENABLE_NODE, &no_debug_bgp_as4_cmd);
   install_element (ENABLE_NODE, &undebug_bgp_as4_cmd);
@@ -926,6 +981,9 @@
   install_element (ENABLE_NODE, &no_debug_bgp_zebra_cmd);
   install_element (ENABLE_NODE, &undebug_bgp_zebra_cmd);
   install_element (CONFIG_NODE, &no_debug_bgp_zebra_cmd);
+  install_element (ENABLE_NODE, &no_debug_bgp_allow_martians_cmd);
+  install_element (ENABLE_NODE, &undebug_bgp_allow_martians_cmd);
+  install_element (CONFIG_NODE, &no_debug_bgp_allow_martians_cmd);
   install_element (ENABLE_NODE, &no_debug_bgp_all_cmd);
   install_element (ENABLE_NODE, &undebug_bgp_all_cmd);
 }
diff --git a/bgpd/bgp_debug.h b/bgpd/bgp_debug.h
index ce8547b..42cbd7e 100644
--- a/bgpd/bgp_debug.h
+++ b/bgpd/bgp_debug.h
@@ -67,6 +67,7 @@
 extern unsigned long conf_bgp_debug_update;
 extern unsigned long conf_bgp_debug_normal;
 extern unsigned long conf_bgp_debug_zebra;
+extern unsigned long conf_bgp_debug_allow_martians;
 
 extern unsigned long term_bgp_debug_as4;
 extern unsigned long term_bgp_debug_fsm;
@@ -77,6 +78,7 @@
 extern unsigned long term_bgp_debug_update;
 extern unsigned long term_bgp_debug_normal;
 extern unsigned long term_bgp_debug_zebra;
+extern unsigned long term_bgp_debug_allow_martians;
 
 #define BGP_DEBUG_AS4                 0x01
 #define BGP_DEBUG_AS4_SEGMENT         0x02
@@ -90,6 +92,7 @@
 #define BGP_DEBUG_UPDATE_OUT          0x02
 #define BGP_DEBUG_NORMAL              0x01
 #define BGP_DEBUG_ZEBRA               0x01
+#define BGP_DEBUG_ALLOW_MARTIANS      0x01
 
 #define BGP_DEBUG_PACKET_SEND         0x01
 #define BGP_DEBUG_PACKET_SEND_DETAIL  0x02