From 3891528b1a2979fcda2c17fd6987e85894504983 Mon Sep 17 00:00:00 2001
From: Mats Erik Andersson <gnu@gisladisker.se>
Date: Tue, 21 Jul 2015 23:04:46 +0200
Subject: [PATCH 04/60] ifconfig: Implement hardware list for BSD systems.

Let formats `gnu' and `osf' report tunnel endpoints.
---
 ChangeLog             |  26 ++++++++
 ifconfig/flags.c      |   3 +
 ifconfig/options.c    |   6 ++
 ifconfig/system/bsd.c | 143 ++++++++++++++++++++++++++++++++++++------
 4 files changed, 160 insertions(+), 18 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d0172fa8..47c70e3a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,29 @@
+2015-07-21  Mats Erik Andersson  <gnu@gisladisker.se>
+
+	ifconfig: Implement a hardware list for BSD systems.
+	Patterned on the hardware recognition already in use
+	by GNU/Linux, use a similar mechanism for all of BSD.
+
+	* ifconfig/system/bsd.c (print_hwaddr_ether): New function.
+	(ift_symbols): New variable.
+	(ift_findvalue): New function.
+	(system_fh_hwaddr_query, system_fh_hwaddr)
+	(system_fh_hwtype_query, system_fh_hwtype): Adjust mechanisms
+	to use the table `ift_symbols' via ift_findvalue().
+
+
+	ifconfig: Report tunnels in two more formats.
+
+	* ifconfig/options.c (formats) <gnu-one-entry format>:
+	Add two entries for the endpoints of tunnels.
+	<osf format>: Add a line to display tunnels.
+
+
+	ifconfig: Add a missing interface flag.
+
+	* ifconfig/flags.c [IFF_CANTCONFIG] (if_flags): Add the
+	extended interface flag `CANTCONFIG'.
+
 2015-07-20  Mats Erik Andersson  <gnu@gisladisker.se>
 
 	ifconfig: More info on tunnel interfaces.
diff --git a/ifconfig/flags.c b/ifconfig/flags.c
index ee4f6e02..003e535d 100644
--- a/ifconfig/flags.c
+++ b/ifconfig/flags.c
@@ -218,6 +218,9 @@ struct if_flag
 #ifdef IFF_SNAP			/* Ethernet driver outputs SNAP header.  */
     {"SNAP", IFF_SNAP, 0},
 #endif
+#ifdef IFF_CANTCONFIG
+    {"CANTCONFIG", IFF_CANTCONFIG, 0},
+#endif
 #ifdef IFF_MONITOR
     {"MONITOR", IFF_MONITOR, 0},
 #endif
diff --git a/ifconfig/options.c b/ifconfig/options.c
index 2f20e75f..9478f310 100644
--- a/ifconfig/options.c
+++ b/ifconfig/options.c
@@ -84,6 +84,10 @@ struct format formats[] = {
    "${netmask?}{  netmask ${tab}{16}${netmask}${\\n}}"
    "${brdaddr?}{  broadcast ${tab}{16}${brdaddr}${\\n}}"
    "${dstaddr?}{  peer address ${tab}{16}${dstaddr}${\\n}}"
+   "${exists?}{tunnel?}{"
+     "${tunnel?}{  tunnel src ${tab}{16}${tunsrc}${\\n}}}"
+   "${exists?}{tunnel?}{"
+     "${tunnel?}{  tunnel dst ${tab}{16}${tundst}${\\n}}}"
    "${flags?}{  flags ${tab}{16}${flags}${\\n}}"
    "${mtu?}{  mtu ${tab}{16}${mtu}${\\n}}"
    "${metric?}{  metric ${tab}{16}${metric}${\\n}}"
@@ -182,6 +186,8 @@ struct format formats[] = {
    "${format}{check-existence}"
    "${ifdisplay?}{"
    "${name}: flags=${flags}{number}{%x}<${flags}{string}{,}>${\\n}"
+   "${exists?}{tunnel?}{"
+     "${tunnel?}{${\\t}inet tunnel src ${tunsrc} tunnel dst ${tundst}${\\n}}}"
    "${addr?}{${\\t}inet ${addr}"
    "${dstaddr?}{ --> ${dstaddr}}"
    " netmask ${netmask}{0}{%02x}${netmask}{1}{%02x}"
diff --git a/ifconfig/system/bsd.c b/ifconfig/system/bsd.c
index 910a75ef..1db525ba 100644
--- a/ifconfig/system/bsd.c
+++ b/ifconfig/system/bsd.c
@@ -200,6 +200,73 @@ struct ifmediareq ifm;
 
 struct if_nameindex* (*system_if_nameindex) (void) = if_nameindex;
 
+static void
+print_hwaddr_ether (format_data_t form, unsigned char *data)
+{
+  put_string (form, ether_ntoa ((struct ether_addr *) data));
+  had_output = 1;
+}
+
+struct ift_symbol
+{
+  const char *name;
+  const char *title;
+  int value;
+  void (*print_hwaddr) (format_data_t form, unsigned char *data);
+} ift_symbols[] =
+  {
+#ifdef IFT_ETHER		/* Ethernet CSMA/CD */
+# ifdef ETHERNAME
+    { "ETHER", ETHERNAME, IFT_ETHER, print_hwaddr_ether},
+# else
+    { "ETHER", "ether", IFT_ETHER, print_hwaddr_ether},
+# endif /* !ETHERNAME */
+#endif /* IFT_ETHER */
+#ifdef IFT_GIF			/* Generic tunnel (gif) */
+    { "IPGIF", "IPIP tunnel", IFT_GIF, NULL},
+#endif
+#ifdef IFT_FAITH		/* IPv6-to-IPv4 TCP relay capture */
+    { "FAITH", "TCP relay capture", IFT_FAITH, NULL},
+#endif
+#ifdef IFT_LOOP			/* Local loopback */
+    { "LOOPBACK", "Local loopback", IFT_LOOP, NULL},
+#endif
+#ifdef IFT_PFLOG		/* Packet filter logging */
+    { "PFLOG", "Packet filter logger", IFT_PFLOG, NULL},
+#endif
+#ifdef IFT_PFSYNC		/* Packet filter state synching */
+    { "PFSYNC", "Packet filter state synching", IFT_PFSYNC, NULL},
+#endif
+#ifdef IFT_PPP			/* Point-to-Point serial protocol */
+    { "PPP", "Point-to-Point over serial", IFT_PPP, NULL},
+#endif
+#ifdef IFT_SLIP			/* IP over generic TTY */
+    { "SLIP", "Serial line IP", IFT_SLIP, NULL},
+#endif
+#ifdef IFT_TUNNEL		/* Encapsulation (gre) */
+    { "IPGRE", "GRE over IP", IFT_TUNNEL, NULL},
+#endif
+    { NULL, NULL, 0, NULL}
+  };
+
+static struct ift_symbol *
+ift_findvalue (int value)
+{
+  struct ift_symbol *ift = ift_symbols;
+
+  while (ift->name != NULL)
+    {
+      if (ift->value == value)
+	break;
+      ift++;
+    }
+
+  if (ift->name)
+    return ift;
+  else
+    return NULL;
+}
+
 void
 system_fh_brdaddr_query (format_data_t form, int argc, char *argv[])
 {
@@ -274,11 +341,16 @@ system_fh_hwaddr_query (format_data_t form, int argc, char *argv[])
 	    continue;
 
 	  dl = (struct sockaddr_dl *) fp->ifa_addr;
-	  if (dl && (dl->sdl_len > 0) &&
-	      dl->sdl_type == IFT_ETHER)	/* XXX: More cases?  */
-	    missing = 0;
+	  if (dl && (dl->sdl_len > 0))
+	    {
+	      struct ift_symbol *ift = ift_findvalue (dl->sdl_type);
+
+	      if (ift && ift->print_hwaddr)
+		missing = 0;
+	    }
 	  break;
 	}
+
       select_arg (form, argc, argv, missing);
     }
 }
@@ -304,11 +376,16 @@ system_fh_hwaddr (format_data_t form, int argc _GL_UNUSED_PARAMETER,
 	    continue;
 
 	  dl = (struct sockaddr_dl *) fp->ifa_addr;
-	  if (dl && (dl->sdl_len > 0) &&
-	      dl->sdl_type == IFT_ETHER)	/* XXX: More cases?  */
+	  if (dl && (dl->sdl_len > 0))
 	    {
-	      missing = 0;
-	      put_string (form, ether_ntoa ((struct ether_addr *) LLADDR (dl)));
+	      struct ift_symbol *ift = ift_findvalue (dl->sdl_type);
+
+	      if (ift && ift->print_hwaddr)
+		{
+		  missing = 0;
+		  ift->print_hwaddr (form,
+				     (unsigned char *) LLADDR (dl));
+		}
 	    }
 	  break;
 	}
@@ -320,19 +397,44 @@ system_fh_hwaddr (format_data_t form, int argc _GL_UNUSED_PARAMETER,
 void
 system_fh_hwtype_query (format_data_t form, int argc, char *argv[])
 {
-  system_fh_hwaddr_query (form, argc, argv);
+  int missing = 1;
+
+  ESTABLISH_IFADDRS
+  if (ifp)
+    {
+      struct ifaddrs *fp;
+      struct sockaddr_dl *dl = NULL;
+
+      for (fp = ifp; fp; fp = fp->ifa_next)
+	{
+	  if (fp->ifa_addr->sa_family != AF_LINK ||
+	      strcmp (fp->ifa_name, form->ifr->ifr_name))
+	    continue;
+
+	  dl = (struct sockaddr_dl *) fp->ifa_addr;
+	  if (dl && (dl->sdl_len > 0))
+	    {
+	      struct ift_symbol *ift = ift_findvalue (dl->sdl_type);
+
+	      if (ift && ift->title)
+		missing = 0;
+	    }
+	  break;
+	}
+    }
+
+  select_arg (form, argc, argv, missing);
 }
 
 void
 system_fh_hwtype (format_data_t form, int argc _GL_UNUSED_PARAMETER,
 		  char *argv[] _GL_UNUSED_PARAMETER)
 {
+  int found = 0;
+
   ESTABLISH_IFADDRS
-  if (!ifp)
-    put_string (form, "(hwtype unknown)");
-  else
+  if (ifp)
     {
-      int found = 0;
       struct ifaddrs *fp;
       struct sockaddr_dl *dl = NULL;
 
@@ -343,17 +445,22 @@ system_fh_hwtype (format_data_t form, int argc _GL_UNUSED_PARAMETER,
 	    continue;
 
 	  dl = (struct sockaddr_dl *) fp->ifa_addr;
-	  if (dl && (dl->sdl_len > 0) &&
-	      dl->sdl_type == IFT_ETHER)	/* XXX: More cases?  */
+	  if (dl && (dl->sdl_len > 0))
 	    {
-	      found = 1;
-	      put_string (form, ETHERNAME);
+	      struct ift_symbol *ift = ift_findvalue (dl->sdl_type);
+
+	      if (ift && ift->title)
+		{
+		  found = 1;
+		  put_string (form, ift->title);
+		}
 	    }
 	  break;
 	}
-      if (!found)
-	put_string (form, "(unknown hwtype)");
     }
+
+  if (!found)
+    put_string (form, "(unknown hwtype)");
 }
 
 /* Lookup structures provided by the system, each decoding
-- 
2.26.0.292.g33ef6b2f38

