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

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