Blob Blame Raw
From 2f3846a557e4ddf83f44e166d959ee460bedf8e1 Mon Sep 17 00:00:00 2001
From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
Date: Tue, 27 Nov 2012 17:18:53 -0200
Subject: [PATCH 099/152] DHCP client ID and UUID options added.

---
 grub-core/net/bootp.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++-----
 include/grub/net.h    |  2 ++
 2 files changed, 81 insertions(+), 8 deletions(-)

diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c
index 6310ed4..88f2056 100644
--- a/grub-core/net/bootp.c
+++ b/grub-core/net/bootp.c
@@ -25,6 +25,49 @@
 #include <grub/net/udp.h>
 #include <grub/datetime.h>
 
+static char *
+grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)),
+			 const char *val __attribute__ ((unused)))
+{
+  return NULL;
+}
+
+static void
+set_env_limn_ro (const char *intername, const char *suffix,
+		 const char *value, grub_size_t len)
+{
+  char *varname, *varvalue;
+  char *ptr;
+  varname = grub_xasprintf ("net_%s_%s", intername, suffix);
+  if (!varname)
+    return;
+  for (ptr = varname; *ptr; ptr++)
+    if (*ptr == ':')
+      *ptr = '_';
+  varvalue = grub_malloc (len + 1);
+  if (!varvalue)
+    {
+      grub_free (varname);
+      return;
+    }
+
+  grub_memcpy (varvalue, value, len);
+  varvalue[len] = 0;
+  grub_env_set (varname, varvalue);
+  grub_register_variable_hook (varname, 0, grub_env_write_readonly);
+  grub_env_export (varname);
+  grub_free (varname);
+  grub_free (varvalue);
+}
+
+static char
+hexdigit (grub_uint8_t val)
+{
+  if (val < 10)
+    return val + '0';
+  return val + 'a' - 10;
+}
+
 static void
 parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask)
 {
@@ -55,6 +98,9 @@ parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask)
 
       taglength = *ptr++;
 
+      grub_dprintf("net", "DHCP option %u (0x%02x) found with length %u.\n",
+                   tagtype, tagtype, taglength);
+
       switch (tagtype)
 	{
 	case GRUB_NET_BOOTP_NETMASK:
@@ -120,6 +166,39 @@ parse_dhcp_vendor (const char *name, const void *vend, int limit, int *mask)
                                      taglength);
           break;
 
+        case GRUB_NET_BOOTP_CLIENT_ID:
+	  set_env_limn_ro (name, "clientid", (char *) ptr, taglength);
+          break;
+
+        case GRUB_NET_BOOTP_CLIENT_UUID:
+            {
+              if (taglength != 17)
+                break;
+
+              /* The format is 9cfe245e-d0c8-bd45-a79f-54ea5fbd3d97 */
+
+              ptr += 1;
+              taglength -= 1;
+
+              char *val = grub_malloc (2 * taglength + 4 + 1);
+              int i = 0;
+              int j = 0;
+              for (i = 0; i < taglength; i++)
+                {
+                  val[2 * i + j] = hexdigit (ptr[i] >> 4);
+                  val[2 * i + 1 + j] = hexdigit (ptr[i] & 0xf);
+
+                  if ((i == 3) || (i == 5) || (i == 7) || (i == 9))
+                    {
+                      j++;
+                      val[2 * i + 1+ j] = '-';
+                    }
+                }
+
+              set_env_limn_ro (name, "clientuuid", (char *) val, 2 * taglength + 4);
+            }
+          break;
+
 	  /* If you need any other options please contact GRUB
 	     development team.  */
 	}
@@ -288,14 +367,6 @@ grub_net_process_dhcp (struct grub_net_buff *nb,
     }
 }
 
-static char
-hexdigit (grub_uint8_t val)
-{
-  if (val < 10)
-    return val + '0';
-  return val + 'a' - 10;
-}
-
 static grub_err_t
 grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),
 		  int argc, char **args)
diff --git a/include/grub/net.h b/include/grub/net.h
index a799e6b..59e5975 100644
--- a/include/grub/net.h
+++ b/include/grub/net.h
@@ -433,6 +433,8 @@ enum
     GRUB_NET_BOOTP_DOMAIN = 0x0f,
     GRUB_NET_BOOTP_ROOT_PATH = 0x11,
     GRUB_NET_BOOTP_EXTENSIONS_PATH = 0x12,
+    GRUB_NET_BOOTP_CLIENT_ID = 0x3d,
+    GRUB_NET_BOOTP_CLIENT_UUID = 0x61,
     GRUB_NET_BOOTP_END = 0xff
   };
 
-- 
1.9.3