Blame SOURCES/0093-efinet-Setting-network-from-UEFI-device-path.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Michael Chang <mchang@suse.com>
8e15ce
Date: Sun, 10 Jul 2016 23:46:31 +0800
8e15ce
Subject: [PATCH] efinet: Setting network from UEFI device path
8e15ce
8e15ce
The PXE Base Code protocol used to obtain cached PXE DHCPACK packet is no
8e15ce
longer provided for HTTP Boot. Instead, we have to get the HTTP boot
8e15ce
information from the device path nodes defined in following UEFI Specification
8e15ce
sections.
8e15ce
8e15ce
 9.3.5.12 IPv4 Device Path
8e15ce
 9.3.5.13 IPv6 Device Path
8e15ce
 9.3.5.23 Uniform Resource Identifiers (URI) Device Path
8e15ce
8e15ce
This patch basically does:
8e15ce
8e15ce
include/grub/efi/api.h:
8e15ce
Add new structure of Uniform Resource Identifiers (URI) Device Path
8e15ce
8e15ce
grub-core/net/drivers/efi/efinet.c:
8e15ce
Check if PXE Base Code is available, if not it will try to obtain the netboot
8e15ce
information from the device path where the image booted from. The DHCPACK
8e15ce
packet is recoverd from the information in device patch and feed into the same
8e15ce
DHCP packet processing functions to ensure the network interface is setting up
8e15ce
the same way it used to be.
8e15ce
8e15ce
Signed-off-by: Michael Chang <mchang@suse.com>
8e15ce
Signed-off-by: Ken Lin <ken.lin@hpe.com>
8e15ce
---
8e15ce
 grub-core/net/drivers/efi/efinet.c | 284 +++++++++++++++++++++++++++++++++++--
8e15ce
 include/grub/efi/api.h             |  11 ++
8e15ce
 2 files changed, 280 insertions(+), 15 deletions(-)
8e15ce
8e15ce
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
8e15ce
index 014e5bf9802..8171ecaa5e4 100644
8e15ce
--- a/grub-core/net/drivers/efi/efinet.c
8e15ce
+++ b/grub-core/net/drivers/efi/efinet.c
8e15ce
@@ -26,6 +26,7 @@
8e15ce
 #include <grub/i18n.h>
8e15ce
 #include <grub/lib/hexdump.h>
8e15ce
 #include <grub/types.h>
8e15ce
+#include <grub/net/netbuff.h>
8e15ce
 
8e15ce
 GRUB_MOD_LICENSE ("GPLv3+");
8e15ce
 
8e15ce
@@ -331,6 +332,227 @@ grub_efinet_findcards (void)
8e15ce
   grub_free (handles);
8e15ce
 }
8e15ce
 
8e15ce
+static struct grub_net_buff *
8e15ce
+grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *use_ipv6)
8e15ce
+{
8e15ce
+  grub_efi_uint16_t uri_len;
8e15ce
+  grub_efi_device_path_t *ldp, *ddp;
8e15ce
+  grub_efi_uri_device_path_t *uri_dp;
8e15ce
+  struct grub_net_buff *nb;
8e15ce
+  grub_err_t err;
8e15ce
+
8e15ce
+  ddp = grub_efi_duplicate_device_path (dp);
8e15ce
+  if (!ddp)
8e15ce
+    return NULL;
8e15ce
+
8e15ce
+  ldp = grub_efi_find_last_device_path (ddp);
8e15ce
+
8e15ce
+  if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
8e15ce
+      || GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE)
8e15ce
+    {
8e15ce
+      grub_free (ddp);
8e15ce
+      return NULL;
8e15ce
+    }
8e15ce
+
8e15ce
+  uri_len = GRUB_EFI_DEVICE_PATH_LENGTH (ldp) > 4 ? GRUB_EFI_DEVICE_PATH_LENGTH (ldp) - 4  : 0;
8e15ce
+
8e15ce
+  if (!uri_len)
8e15ce
+    {
8e15ce
+      grub_free (ddp);
8e15ce
+      return NULL;
8e15ce
+    }
8e15ce
+
8e15ce
+  uri_dp = (grub_efi_uri_device_path_t *) ldp;
8e15ce
+
8e15ce
+  ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
8e15ce
+  ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
8e15ce
+  ldp->length = sizeof (*ldp);
8e15ce
+
8e15ce
+  ldp = grub_efi_find_last_device_path (ddp);
8e15ce
+
8e15ce
+  if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
8e15ce
+      || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
8e15ce
+          && GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE))
8e15ce
+    {
8e15ce
+      grub_free (ddp);
8e15ce
+      return NULL;
8e15ce
+    }
8e15ce
+
8e15ce
+  nb = grub_netbuff_alloc (512);
8e15ce
+  if (!nb)
8e15ce
+    {
8e15ce
+      grub_free (ddp);
8e15ce
+      return NULL;
8e15ce
+    }
8e15ce
+
8e15ce
+  if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE)
8e15ce
+    {
8e15ce
+      grub_efi_ipv4_device_path_t *ipv4 = (grub_efi_ipv4_device_path_t *) ldp;
8e15ce
+      struct grub_net_bootp_packet *bp;
8e15ce
+      grub_uint8_t *ptr;
8e15ce
+
8e15ce
+      bp = (struct grub_net_bootp_packet *) nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof (*bp) + 4);
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+
8e15ce
+      if (sizeof(bp->boot_file) < uri_len)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      grub_memcpy (bp->boot_file, uri_dp->uri, uri_len);
8e15ce
+      grub_memcpy (&bp->your_ip, ipv4->local_ip_address, sizeof (bp->your_ip));
8e15ce
+      grub_memcpy (&bp->server_ip, ipv4->remote_ip_address, sizeof (bp->server_ip));
8e15ce
+
8e15ce
+      bp->vendor[0] = GRUB_NET_BOOTP_RFC1048_MAGIC_0;
8e15ce
+      bp->vendor[1] = GRUB_NET_BOOTP_RFC1048_MAGIC_1;
8e15ce
+      bp->vendor[2] = GRUB_NET_BOOTP_RFC1048_MAGIC_2;
8e15ce
+      bp->vendor[3] = GRUB_NET_BOOTP_RFC1048_MAGIC_3;
8e15ce
+
8e15ce
+      ptr = nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof (ipv4->subnet_mask) + 2);
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      *ptr++ = GRUB_NET_BOOTP_NETMASK;
8e15ce
+      *ptr++ = sizeof (ipv4->subnet_mask);
8e15ce
+      grub_memcpy (ptr, ipv4->subnet_mask, sizeof (ipv4->subnet_mask));
8e15ce
+
8e15ce
+      ptr = nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof (ipv4->gateway_ip_address) + 2);
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      *ptr++ = GRUB_NET_BOOTP_ROUTER;
8e15ce
+      *ptr++ = sizeof (ipv4->gateway_ip_address);
8e15ce
+      grub_memcpy (ptr, ipv4->gateway_ip_address, sizeof (ipv4->gateway_ip_address));
8e15ce
+
8e15ce
+      ptr = nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof ("HTTPClient") + 1);
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      *ptr++ = GRUB_NET_BOOTP_VENDOR_CLASS_IDENTIFIER;
8e15ce
+      *ptr++ = sizeof ("HTTPClient") - 1;
8e15ce
+      grub_memcpy (ptr, "HTTPClient", sizeof ("HTTPClient") - 1);
8e15ce
+
8e15ce
+      ptr = nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, 1);
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      *ptr = GRUB_NET_BOOTP_END;
8e15ce
+      *use_ipv6 = 0;
8e15ce
+
8e15ce
+      ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
8e15ce
+      ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
8e15ce
+      ldp->length = sizeof (*ldp);
8e15ce
+      ldp = grub_efi_find_last_device_path (ddp);
8e15ce
+
8e15ce
+      if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) ==  GRUB_EFI_MAC_ADDRESS_DEVICE_PATH_SUBTYPE)
8e15ce
+	{
8e15ce
+	  grub_efi_mac_address_device_path_t *mac = (grub_efi_mac_address_device_path_t *) ldp;
8e15ce
+	  bp->hw_type = mac->if_type;
8e15ce
+	  bp->hw_len = sizeof (bp->mac_addr);
8e15ce
+	  grub_memcpy (bp->mac_addr, mac->mac_address, bp->hw_len);
8e15ce
+	}
8e15ce
+    }
8e15ce
+  else
8e15ce
+    {
8e15ce
+      grub_efi_ipv6_device_path_t *ipv6 = (grub_efi_ipv6_device_path_t *) ldp;
8e15ce
+
8e15ce
+      struct grub_net_dhcp6_packet *d6p;
8e15ce
+      struct grub_net_dhcp6_option *opt;
8e15ce
+      struct grub_net_dhcp6_option_iana *iana;
8e15ce
+      struct grub_net_dhcp6_option_iaaddr *iaaddr;
8e15ce
+
8e15ce
+      d6p = (struct grub_net_dhcp6_packet *)nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof(*d6p));
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      d6p->message_type = GRUB_NET_DHCP6_REPLY;
8e15ce
+
8e15ce
+      opt = (struct grub_net_dhcp6_option *)nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof(*opt));
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
8e15ce
+      opt->len = grub_cpu_to_be16_compile_time (sizeof(*iana) + sizeof(*opt) + sizeof(*iaaddr));
8e15ce
+
8e15ce
+      err = grub_netbuff_put (nb, sizeof(*iana));
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+
8e15ce
+      opt = (struct grub_net_dhcp6_option *)nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof(*opt));
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR);
8e15ce
+      opt->len = grub_cpu_to_be16_compile_time (sizeof (*iaaddr));
8e15ce
+
8e15ce
+      iaaddr = (struct grub_net_dhcp6_option_iaaddr *)nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof(*iaaddr));
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      grub_memcpy (iaaddr->addr, ipv6->local_ip_address, sizeof(ipv6->local_ip_address));
8e15ce
+
8e15ce
+      opt = (struct grub_net_dhcp6_option *)nb->tail;
8e15ce
+      err = grub_netbuff_put (nb, sizeof(*opt) + uri_len);
8e15ce
+      if (err)
8e15ce
+	{
8e15ce
+	  grub_free (ddp);
8e15ce
+	  grub_netbuff_free (nb);
8e15ce
+	  return NULL;
8e15ce
+	}
8e15ce
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL);
8e15ce
+      opt->len = grub_cpu_to_be16 (uri_len);
8e15ce
+      grub_memcpy (opt->data, uri_dp->uri, uri_len);
8e15ce
+
8e15ce
+      *use_ipv6 = 1;
8e15ce
+    }
8e15ce
+
8e15ce
+  grub_free (ddp);
8e15ce
+  return nb;
8e15ce
+}
8e15ce
+
8e15ce
 static void
8e15ce
 grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8e15ce
 			  char **path)
8e15ce
@@ -346,7 +568,11 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8e15ce
   {
8e15ce
     grub_efi_device_path_t *cdp;
8e15ce
     struct grub_efi_pxe *pxe;
8e15ce
-    struct grub_efi_pxe_mode *pxe_mode;
8e15ce
+    struct grub_efi_pxe_mode *pxe_mode = NULL;
8e15ce
+    grub_uint8_t *packet_buf;
8e15ce
+    grub_size_t packet_bufsz ;
8e15ce
+    int ipv6;
8e15ce
+    struct grub_net_buff *nb = NULL;
8e15ce
 
8e15ce
     if (card->driver != &efidriver)
8e15ce
       continue;
8e15ce
@@ -370,11 +596,21 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8e15ce
          */
8e15ce
 	if (GRUB_EFI_DEVICE_PATH_TYPE (ldp) != GRUB_EFI_MESSAGING_DEVICE_PATH_TYPE
8e15ce
 	    || (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV4_DEVICE_PATH_SUBTYPE
8e15ce
-		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE))
8e15ce
+		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_IPV6_DEVICE_PATH_SUBTYPE
8e15ce
+		&& GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) != GRUB_EFI_URI_DEVICE_PATH_SUBTYPE))
8e15ce
 	  continue;
8e15ce
 	dup_dp = grub_efi_duplicate_device_path (dp);
8e15ce
 	if (!dup_dp)
8e15ce
 	  continue;
8e15ce
+
8e15ce
+	if (GRUB_EFI_DEVICE_PATH_SUBTYPE (ldp) == GRUB_EFI_URI_DEVICE_PATH_SUBTYPE)
8e15ce
+	  {
8e15ce
+	    dup_ldp = grub_efi_find_last_device_path (dup_dp);
8e15ce
+	    dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
8e15ce
+	    dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
8e15ce
+	    dup_ldp->length = sizeof (*dup_ldp);
8e15ce
+	  }
8e15ce
+
8e15ce
 	dup_ldp = grub_efi_find_last_device_path (dup_dp);
8e15ce
 	dup_ldp->type = GRUB_EFI_END_DEVICE_PATH_TYPE;
8e15ce
 	dup_ldp->subtype = GRUB_EFI_END_ENTIRE_DEVICE_PATH_SUBTYPE;
8e15ce
@@ -387,23 +623,37 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8e15ce
 
8e15ce
     pxe = grub_efi_open_protocol (hnd, &pxe_io_guid,
8e15ce
 				  GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
8e15ce
-    if (! pxe)
8e15ce
-      continue;
8e15ce
+    if (!pxe)
8e15ce
+      {
8e15ce
+	nb = grub_efinet_create_dhcp_ack_from_device_path (dp, &ipv6);
8e15ce
+	if (!nb)
8e15ce
+	  {
8e15ce
+	    grub_print_error ();
8e15ce
+	    continue;
8e15ce
+	  }
8e15ce
+	packet_buf = nb->head;
8e15ce
+	packet_bufsz = nb->tail - nb->head;
8e15ce
+      }
8e15ce
+    else
8e15ce
+      {
8e15ce
+	pxe_mode = pxe->mode;
8e15ce
+	packet_buf = (grub_uint8_t *) &pxe_mode->dhcp_ack;
8e15ce
+	packet_bufsz = sizeof (pxe_mode->dhcp_ack);
8e15ce
+	ipv6 = pxe_mode->using_ipv6;
8e15ce
+      }
8e15ce
 
8e15ce
-    pxe_mode = pxe->mode;
8e15ce
-    if (pxe_mode->using_ipv6)
8e15ce
+    if (ipv6)
8e15ce
       {
8e15ce
 	grub_dprintf ("efinet", "using ipv6 and dhcpv6\n");
8e15ce
-	grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
8e15ce
-		      pxe_mode->dhcp_ack_received ? "yes" : "no",
8e15ce
-		      pxe_mode->dhcp_ack_received ? "" : " cannot continue");
8e15ce
-	if (!pxe_mode->dhcp_ack_received)
8e15ce
-	  continue;
8e15ce
+	if (pxe_mode)
8e15ce
+	  grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
8e15ce
+			pxe_mode->dhcp_ack_received ? "yes" : "no",
8e15ce
+			pxe_mode->dhcp_ack_received ? "" : " cannot continue");
8e15ce
 
8e15ce
 	grub_net_configure_by_dhcpv6_reply (card->name, card, 0,
8e15ce
 					    (struct grub_net_dhcp6_packet *)
8e15ce
-					    &pxe_mode->dhcp_ack,
8e15ce
-					    sizeof (pxe_mode->dhcp_ack),
8e15ce
+					    packet_buf,
8e15ce
+					    packet_bufsz,
8e15ce
 					    1, device, path);
8e15ce
 	if (grub_errno)
8e15ce
 	  grub_print_error ();
8e15ce
@@ -417,11 +667,15 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8e15ce
 	grub_dprintf ("efinet", "using ipv4 and dhcp\n");
8e15ce
 	grub_net_configure_by_dhcp_ack (card->name, card, 0,
8e15ce
 					(struct grub_net_bootp_packet *)
8e15ce
-					&pxe_mode->dhcp_ack,
8e15ce
-					sizeof (pxe_mode->dhcp_ack),
8e15ce
+					packet_buf,
8e15ce
+					packet_bufsz,
8e15ce
 					1, device, path);
8e15ce
 	grub_dprintf ("efinet", "device: `%s' path: `%s'\n", *device, *path);
8e15ce
       }
8e15ce
+
8e15ce
+    if (nb)
8e15ce
+      grub_netbuff_free (nb);
8e15ce
+
8e15ce
     return;
8e15ce
   }
8e15ce
 }
8e15ce
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
8e15ce
index 91ab528e4d0..4a51667adb1 100644
8e15ce
--- a/include/grub/efi/api.h
8e15ce
+++ b/include/grub/efi/api.h
8e15ce
@@ -864,6 +864,8 @@ struct grub_efi_ipv4_device_path
8e15ce
   grub_efi_uint16_t remote_port;
8e15ce
   grub_efi_uint16_t protocol;
8e15ce
   grub_efi_uint8_t static_ip_address;
8e15ce
+  grub_efi_ipv4_address_t gateway_ip_address;
8e15ce
+  grub_efi_ipv4_address_t subnet_mask;
8e15ce
 } GRUB_PACKED;
8e15ce
 typedef struct grub_efi_ipv4_device_path grub_efi_ipv4_device_path_t;
8e15ce
 
8e15ce
@@ -918,6 +920,15 @@ struct grub_efi_sata_device_path
8e15ce
 } GRUB_PACKED;
8e15ce
 typedef struct grub_efi_sata_device_path grub_efi_sata_device_path_t;
8e15ce
 
8e15ce
+#define GRUB_EFI_URI_DEVICE_PATH_SUBTYPE		24
8e15ce
+
8e15ce
+struct grub_efi_uri_device_path
8e15ce
+{
8e15ce
+  grub_efi_device_path_t header;
8e15ce
+  grub_efi_uint8_t uri[0];
8e15ce
+} GRUB_PACKED;
8e15ce
+typedef struct grub_efi_uri_device_path grub_efi_uri_device_path_t;
8e15ce
+
8e15ce
 #define GRUB_EFI_VENDOR_MESSAGING_DEVICE_PATH_SUBTYPE	10
8e15ce
 
8e15ce
 /* Media Device Path.  */