Blame SOURCES/0175-efinet-Setting-DNS-server-from-UEFI-protocol.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Michael Chang <mchang@suse.com>
8631a2
Date: Thu, 14 Jul 2016 17:48:45 +0800
8631a2
Subject: [PATCH] efinet: Setting DNS server from UEFI protocol
8631a2
8631a2
In the URI device path node, any name rahter than address can be used for
8631a2
looking up the resources so that DNS service become needed to get answer of the
8631a2
name's address. Unfortunately the DNS is not defined in any of the device path
8631a2
nodes so that we use the EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL
8631a2
to obtain it.
8631a2
8631a2
These two protcols are defined the sections of UEFI specification.
8631a2
8631a2
 27.5 EFI IPv4 Configuration II Protocol
8631a2
 27.7 EFI IPv6 Configuration Protocol
8631a2
8631a2
include/grub/efi/api.h:
8631a2
Add new structure and protocol UUID of EFI_IP4_CONFIG2_PROTOCOL and
8631a2
EFI_IP6_CONFIG_PROTOCOL.
8631a2
8631a2
grub-core/net/drivers/efi/efinet.c:
8631a2
Use the EFI_IP4_CONFIG2_PROTOCOL and EFI_IP6_CONFIG_PROTOCOL to obtain the list
8631a2
of DNS server address for IPv4 and IPv6 respectively. The address of DNS
8631a2
servers is structured into DHCPACK packet and feed into the same DHCP packet
8631a2
processing functions to ensure the network interface is setting up the same way
8631a2
it used to be.
8631a2
8631a2
Signed-off-by: Michael Chang <mchang@suse.com>
8631a2
Signed-off-by: Ken Lin <ken.lin@hpe.com>
8631a2
---
8631a2
 grub-core/net/drivers/efi/efinet.c | 163 +++++++++++++++++++++++++++++++++++++
8631a2
 include/grub/efi/api.h             |  76 +++++++++++++++++
8631a2
 2 files changed, 239 insertions(+)
8631a2
8631a2
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
f6e916
index 3e51e1064..3d7750747 100644
8631a2
--- a/grub-core/net/drivers/efi/efinet.c
8631a2
+++ b/grub-core/net/drivers/efi/efinet.c
8631a2
@@ -34,6 +34,8 @@ GRUB_MOD_LICENSE ("GPLv3+");
8631a2
 /* GUID.  */
8631a2
 static grub_efi_guid_t net_io_guid = GRUB_EFI_SIMPLE_NETWORK_GUID;
8631a2
 static grub_efi_guid_t pxe_io_guid = GRUB_EFI_PXE_GUID;
8631a2
+static grub_efi_guid_t ip4_config_guid = GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID;
8631a2
+static grub_efi_guid_t ip6_config_guid = GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID;
8631a2
 
8631a2
 static grub_err_t
8631a2
 send_card_buffer (struct grub_net_card *dev,
8631a2
@@ -333,6 +335,125 @@ grub_efinet_findcards (void)
8631a2
   grub_free (handles);
8631a2
 }
8631a2
 
8631a2
+static grub_efi_handle_t
8631a2
+grub_efi_locate_device_path (grub_efi_guid_t *protocol, grub_efi_device_path_t *device_path,
8631a2
+			    grub_efi_device_path_t **r_device_path)
8631a2
+{
8631a2
+  grub_efi_handle_t handle;
8631a2
+  grub_efi_status_t status;
8631a2
+
8631a2
+  status = efi_call_3 (grub_efi_system_table->boot_services->locate_device_path,
8631a2
+		      protocol, &device_path, &handle);
8631a2
+
8631a2
+  if (status != GRUB_EFI_SUCCESS)
8631a2
+    return 0;
8631a2
+
8631a2
+  if (r_device_path)
8631a2
+    *r_device_path = device_path;
8631a2
+
8631a2
+  return handle;
8631a2
+}
8631a2
+
8631a2
+static grub_efi_ipv4_address_t *
8631a2
+grub_dns_server_ip4_address (grub_efi_device_path_t *dp, grub_efi_uintn_t *num_dns)
8631a2
+{
8631a2
+  grub_efi_handle_t hnd;
8631a2
+  grub_efi_status_t status;
8631a2
+  grub_efi_ip4_config2_protocol_t *conf;
8631a2
+  grub_efi_ipv4_address_t *addrs;
8631a2
+  grub_efi_uintn_t data_size = 1 * sizeof (grub_efi_ipv4_address_t);
8631a2
+
8631a2
+  hnd = grub_efi_locate_device_path (&ip4_config_guid, dp, NULL);
8631a2
+
8631a2
+  if (!hnd)
8631a2
+    return 0;
8631a2
+
8631a2
+  conf = grub_efi_open_protocol (hnd, &ip4_config_guid,
8631a2
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
8631a2
+
8631a2
+  if (!conf)
8631a2
+    return 0;
8631a2
+
8631a2
+  addrs  = grub_malloc (data_size);
8631a2
+  if (!addrs)
8631a2
+    return 0;
8631a2
+
8631a2
+  status = efi_call_4 (conf->get_data, conf,
8631a2
+		      GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
8631a2
+		      &data_size, addrs);
8631a2
+
8631a2
+  if (status == GRUB_EFI_BUFFER_TOO_SMALL)
8631a2
+    {
8631a2
+      grub_free (addrs);
8631a2
+      addrs  = grub_malloc (data_size);
8631a2
+      if (!addrs)
8631a2
+	return 0;
8631a2
+
8631a2
+      status = efi_call_4 (conf->get_data,  conf,
8631a2
+			  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
8631a2
+			  &data_size, addrs);
8631a2
+    }
8631a2
+
8631a2
+  if (status != GRUB_EFI_SUCCESS)
8631a2
+    {
8631a2
+      grub_free (addrs);
8631a2
+      return 0;
8631a2
+    }
8631a2
+
8631a2
+  *num_dns = data_size / sizeof (grub_efi_ipv4_address_t);
8631a2
+  return addrs;
8631a2
+}
8631a2
+
8631a2
+static grub_efi_ipv6_address_t *
8631a2
+grub_dns_server_ip6_address (grub_efi_device_path_t *dp, grub_efi_uintn_t *num_dns)
8631a2
+{
8631a2
+  grub_efi_handle_t hnd;
8631a2
+  grub_efi_status_t status;
8631a2
+  grub_efi_ip6_config_protocol_t *conf;
8631a2
+  grub_efi_ipv6_address_t *addrs;
8631a2
+  grub_efi_uintn_t data_size = 1 * sizeof (grub_efi_ipv6_address_t);
8631a2
+
8631a2
+  hnd = grub_efi_locate_device_path (&ip6_config_guid, dp, NULL);
8631a2
+
8631a2
+  if (!hnd)
8631a2
+    return 0;
8631a2
+
8631a2
+  conf = grub_efi_open_protocol (hnd, &ip6_config_guid,
8631a2
+				GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
8631a2
+
8631a2
+  if (!conf)
8631a2
+    return 0;
8631a2
+
8631a2
+  addrs  = grub_malloc (data_size);
8631a2
+  if (!addrs)
8631a2
+    return 0;
8631a2
+
8631a2
+  status = efi_call_4 (conf->get_data, conf,
8631a2
+		      GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
8631a2
+		      &data_size, addrs);
8631a2
+
8631a2
+  if (status == GRUB_EFI_BUFFER_TOO_SMALL)
8631a2
+    {
8631a2
+      grub_free (addrs);
8631a2
+      addrs  = grub_malloc (data_size);
8631a2
+      if (!addrs)
8631a2
+	return 0;
8631a2
+
8631a2
+      status = efi_call_4 (conf->get_data,  conf,
8631a2
+			  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
8631a2
+			  &data_size, addrs);
8631a2
+    }
8631a2
+
8631a2
+  if (status != GRUB_EFI_SUCCESS)
8631a2
+    {
8631a2
+      grub_free (addrs);
8631a2
+      return 0;
8631a2
+    }
8631a2
+
8631a2
+  *num_dns = data_size / sizeof (grub_efi_ipv6_address_t);
8631a2
+  return addrs;
8631a2
+}
8631a2
+
8631a2
 static struct grub_net_buff *
8631a2
 grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *use_ipv6)
8631a2
 {
8631a2
@@ -391,6 +512,8 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
8631a2
       grub_efi_ipv4_device_path_t *ipv4 = (grub_efi_ipv4_device_path_t *) ldp;
8631a2
       struct grub_net_bootp_packet *bp;
8631a2
       grub_uint8_t *ptr;
8631a2
+      grub_efi_ipv4_address_t *dns;
8631a2
+      grub_efi_uintn_t num_dns;
8631a2
 
8631a2
       bp = (struct grub_net_bootp_packet *) nb->tail;
8631a2
       err = grub_netbuff_put (nb, sizeof (*bp) + 4);
8631a2
@@ -452,6 +575,25 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
8631a2
       *ptr++ = sizeof ("HTTPClient") - 1;
8631a2
       grub_memcpy (ptr, "HTTPClient", sizeof ("HTTPClient") - 1);
8631a2
 
8631a2
+      dns = grub_dns_server_ip4_address (dp, &num_dns);
8631a2
+      if (dns)
8631a2
+	{
8631a2
+	  grub_efi_uintn_t size_dns = sizeof (*dns) * num_dns;
8631a2
+
8631a2
+	  ptr = nb->tail;
8631a2
+	  err = grub_netbuff_put (nb, size_dns + 2);
8631a2
+	  if (err)
8631a2
+	    {
8631a2
+	      grub_free (ddp);
8631a2
+	      grub_netbuff_free (nb);
8631a2
+	      return NULL;
8631a2
+	    }
8631a2
+	  *ptr++ = GRUB_NET_BOOTP_DNS;
8631a2
+	  *ptr++ = size_dns;
8631a2
+	  grub_memcpy (ptr, dns, size_dns);
8631a2
+	  grub_free (dns);
8631a2
+	}
8631a2
+
8631a2
       ptr = nb->tail;
8631a2
       err = grub_netbuff_put (nb, 1);
8631a2
       if (err)
8631a2
@@ -484,6 +626,8 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
8631a2
       struct grub_net_dhcp6_option *opt;
8631a2
       struct grub_net_dhcp6_option_iana *iana;
8631a2
       struct grub_net_dhcp6_option_iaaddr *iaaddr;
8631a2
+      grub_efi_ipv6_address_t *dns;
8631a2
+      grub_efi_uintn_t num_dns;
8631a2
 
8631a2
       d6p = (struct grub_net_dhcp6_packet *)nb->tail;
8631a2
       err = grub_netbuff_put (nb, sizeof(*d6p));
8631a2
@@ -547,6 +691,25 @@ grub_efinet_create_dhcp_ack_from_device_path (grub_efi_device_path_t *dp, int *u
8631a2
       opt->len = grub_cpu_to_be16 (uri_len);
8631a2
       grub_memcpy (opt->data, uri_dp->uri, uri_len);
8631a2
 
8631a2
+      dns = grub_dns_server_ip6_address (dp, &num_dns);
8631a2
+      if (dns)
8631a2
+	{
8631a2
+	  grub_efi_uintn_t size_dns = sizeof (*dns) * num_dns;
8631a2
+
8631a2
+	  opt = (struct grub_net_dhcp6_option *)nb->tail;
8631a2
+	  err = grub_netbuff_put (nb, sizeof(*opt) + size_dns);
8631a2
+	  if (err)
8631a2
+	  {
8631a2
+	    grub_free (ddp);
8631a2
+	    grub_netbuff_free (nb);
8631a2
+	    return NULL;
8631a2
+	  }
8631a2
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS);
8631a2
+	  opt->len = grub_cpu_to_be16 (size_dns);
8631a2
+	  grub_memcpy (opt->data, dns, size_dns);
8631a2
+	  grub_free (dns);
8631a2
+	}
8631a2
+
8631a2
       *use_ipv6 = 1;
8631a2
     }
8631a2
 
8631a2
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
f6e916
index eb6bb5085..dd3b07eac 100644
8631a2
--- a/include/grub/efi/api.h
8631a2
+++ b/include/grub/efi/api.h
8631a2
@@ -334,6 +334,16 @@
8631a2
       { 0x8B, 0x8C, 0xE2, 0x1B, 0x01, 0xAE, 0xF2, 0xB7 } \
8631a2
   }
8631a2
 
8631a2
+#define GRUB_EFI_IP4_CONFIG2_PROTOCOL_GUID \
8631a2
+  { 0x5b446ed1, 0xe30b, 0x4faa, \
8631a2
+      { 0x87, 0x1a, 0x36, 0x54, 0xec, 0xa3, 0x60, 0x80 } \
8631a2
+  }
8631a2
+
8631a2
+#define GRUB_EFI_IP6_CONFIG_PROTOCOL_GUID \
8631a2
+  { 0x937fe521, 0x95ae, 0x4d1a, \
8631a2
+      { 0x89, 0x29, 0x48, 0xbc, 0xd9, 0x0a, 0xd3, 0x1a } \
8631a2
+  }
8631a2
+
8631a2
 struct grub_efi_sal_system_table
8631a2
 {
8631a2
   grub_uint32_t signature;
8631a2
@@ -1838,6 +1848,72 @@ struct grub_efi_block_io
8631a2
 };
8631a2
 typedef struct grub_efi_block_io grub_efi_block_io_t;
8631a2
 
8631a2
+enum grub_efi_ip4_config2_data_type {
8631a2
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_INTERFACEINFO,
8631a2
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_POLICY,
8631a2
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MANUAL_ADDRESS,
8631a2
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_GATEWAY,
8631a2
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_DNSSERVER,
8631a2
+  GRUB_EFI_IP4_CONFIG2_DATA_TYPE_MAXIMUM
8631a2
+};
8631a2
+typedef enum grub_efi_ip4_config2_data_type grub_efi_ip4_config2_data_type_t;
8631a2
+
8631a2
+struct grub_efi_ip4_config2_protocol
8631a2
+{
8631a2
+  grub_efi_status_t (*set_data) (struct grub_efi_ip4_config2_protocol *this,
8631a2
+				 grub_efi_ip4_config2_data_type_t data_type,
8631a2
+				 grub_efi_uintn_t data_size,
8631a2
+				 void *data);
8631a2
+
8631a2
+  grub_efi_status_t (*get_data) (struct grub_efi_ip4_config2_protocol *this,
8631a2
+				 grub_efi_ip4_config2_data_type_t data_type,
8631a2
+				 grub_efi_uintn_t *data_size,
8631a2
+				 void *data);
8631a2
+
8631a2
+  grub_efi_status_t (*register_data_notify) (struct grub_efi_ip4_config2_protocol *this,
8631a2
+					     grub_efi_ip4_config2_data_type_t data_type,
8631a2
+					     grub_efi_event_t event);
8631a2
+
8631a2
+  grub_efi_status_t (*unregister_datanotify) (struct grub_efi_ip4_config2_protocol *this,
8631a2
+					     grub_efi_ip4_config2_data_type_t data_type,
8631a2
+					     grub_efi_event_t event);
8631a2
+};
8631a2
+typedef struct grub_efi_ip4_config2_protocol grub_efi_ip4_config2_protocol_t;
8631a2
+
8631a2
+enum grub_efi_ip6_config_data_type {
8631a2
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_INTERFACEINFO,
8631a2
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_ALT_INTERFACEID,
8631a2
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_POLICY,
8631a2
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DUP_ADDR_DETECT_TRANSMITS,
8631a2
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_MANUAL_ADDRESS,
8631a2
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_GATEWAY,
8631a2
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_DNSSERVER,
8631a2
+  GRUB_EFI_IP6_CONFIG_DATA_TYPE_MAXIMUM
8631a2
+};
8631a2
+typedef enum grub_efi_ip6_config_data_type grub_efi_ip6_config_data_type_t;
8631a2
+
8631a2
+struct grub_efi_ip6_config_protocol
8631a2
+{
8631a2
+  grub_efi_status_t (*set_data) (struct grub_efi_ip6_config_protocol *this,
8631a2
+				 grub_efi_ip6_config_data_type_t data_type,
8631a2
+				 grub_efi_uintn_t data_size,
8631a2
+				 void *data);
8631a2
+
8631a2
+  grub_efi_status_t (*get_data) (struct grub_efi_ip6_config_protocol *this,
8631a2
+				 grub_efi_ip6_config_data_type_t data_type,
8631a2
+				 grub_efi_uintn_t *data_size,
8631a2
+				 void *data);
8631a2
+
8631a2
+  grub_efi_status_t (*register_data_notify) (struct grub_efi_ip6_config_protocol *this,
8631a2
+					     grub_efi_ip6_config_data_type_t data_type,
8631a2
+					     grub_efi_event_t event);
8631a2
+
8631a2
+  grub_efi_status_t (*unregister_datanotify) (struct grub_efi_ip6_config_protocol *this,
8631a2
+					     grub_efi_ip6_config_data_type_t data_type,
8631a2
+					     grub_efi_event_t event);
8631a2
+};
8631a2
+typedef struct grub_efi_ip6_config_protocol grub_efi_ip6_config_protocol_t;
8631a2
+
8631a2
 #if (GRUB_TARGET_SIZEOF_VOID_P == 4) || defined (__ia64__) \
8631a2
   || defined (__aarch64__) || defined (__MINGW64__) || defined (__CYGWIN__)
8631a2