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

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