Blame SOURCES/0169-bootp-New-net_bootp6-command.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Michael Chang <mchang@suse.com>
8631a2
Date: Sun, 10 Jul 2016 23:46:06 +0800
8631a2
Subject: [PATCH] bootp: New net_bootp6 command
8631a2
8631a2
Implement new net_bootp6 command for IPv6 network auto configuration via the
8631a2
DHCPv6 protocol (RFC3315).
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/bootp.c              | 1048 ++++++++++++++++++++++++++++++------
8631a2
 grub-core/net/drivers/efi/efinet.c |   20 +-
8631a2
 grub-core/net/ip.c                 |   39 ++
8631a2
 include/grub/efi/api.h             |    2 +-
8631a2
 include/grub/net.h                 |   91 ++--
8631a2
 5 files changed, 994 insertions(+), 206 deletions(-)
8631a2
8631a2
diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c
8631a2
index 4e55adc557b..ff1d7776e7f 100644
8631a2
--- a/grub-core/net/bootp.c
8631a2
+++ b/grub-core/net/bootp.c
8631a2
@@ -25,6 +25,98 @@
8631a2
 #include <grub/net/udp.h>
8631a2
 #include <grub/net/url.h>
8631a2
 #include <grub/datetime.h>
8631a2
+#include <grub/time.h>
8631a2
+#include <grub/list.h>
8631a2
+
8631a2
+static int
8631a2
+dissect_url (const char *url, char **proto, char **host, char **path)
8631a2
+{
8631a2
+  const char *p, *ps;
8631a2
+  grub_size_t l;
8631a2
+
8631a2
+  *proto = *host = *path = NULL;
8631a2
+  ps = p = url;
8631a2
+
8631a2
+  while ((p = grub_strchr (p, ':')))
8631a2
+    {
8631a2
+      if (grub_strlen (p) < sizeof ("://") - 1)
8631a2
+	break;
8631a2
+      if (grub_memcmp (p, "://", sizeof ("://") - 1) == 0)
8631a2
+	{
8631a2
+	  l = p - ps;
8631a2
+	  *proto = grub_malloc (l + 1);
8631a2
+	  if (!*proto)
8631a2
+	    {
8631a2
+	      grub_print_error ();
8631a2
+	      return 0;
8631a2
+	    }
8631a2
+
8631a2
+	  grub_memcpy (*proto, ps, l);
8631a2
+	  (*proto)[l] = '\0';
8631a2
+	  p +=  sizeof ("://") - 1;
8631a2
+	  break;
8631a2
+	}
8631a2
+      ++p;
8631a2
+    }
8631a2
+
8631a2
+  if (!*proto)
8631a2
+    {
8631a2
+      grub_dprintf ("bootp", "url: %s is not valid, protocol not found\n", url);
8631a2
+      return 0;
8631a2
+    }
8631a2
+
8631a2
+  ps = p;
8631a2
+  p = grub_strchr (p, '/');
8631a2
+
8631a2
+  if (!p)
8631a2
+    {
8631a2
+      grub_dprintf ("bootp", "url: %s is not valid, host/path not found\n", url);
8631a2
+      grub_free (*proto);
8631a2
+      *proto = NULL;
8631a2
+      return 0;
8631a2
+    }
8631a2
+
8631a2
+  l = p - ps;
8631a2
+
8631a2
+  if (l > 2 && ps[0] == '[' && ps[l - 1] == ']')
8631a2
+    {
8631a2
+      *host = grub_malloc (l - 1);
8631a2
+      if (!*host)
8631a2
+	{
8631a2
+	  grub_print_error ();
8631a2
+	  grub_free (*proto);
8631a2
+	  *proto = NULL;
8631a2
+	  return 0;
8631a2
+	}
8631a2
+      grub_memcpy (*host, ps + 1, l - 2);
8631a2
+      (*host)[l - 2] = 0;
8631a2
+    }
8631a2
+  else
8631a2
+    {
8631a2
+      *host = grub_malloc (l + 1);
8631a2
+      if (!*host)
8631a2
+	{
8631a2
+	  grub_print_error ();
8631a2
+	  grub_free (*proto);
8631a2
+	  *proto = NULL;
8631a2
+	  return 0;
8631a2
+	}
8631a2
+      grub_memcpy (*host, ps, l);
8631a2
+      (*host)[l] = 0;
8631a2
+    }
8631a2
+
8631a2
+  *path = grub_strdup (p);
8631a2
+  if (!*path)
8631a2
+    {
8631a2
+      grub_print_error ();
8631a2
+      grub_free (*host);
8631a2
+      grub_free (*proto);
8631a2
+      *host = NULL;
8631a2
+      *proto = NULL;
8631a2
+      return 0;
8631a2
+    }
8631a2
+  return 1;
8631a2
+}
8631a2
 
8631a2
 static char *
8631a2
 grub_env_write_readonly (struct grub_env_var *var __attribute__ ((unused)),
8631a2
@@ -345,178 +437,578 @@ grub_net_configure_by_dhcp_ack (const char *name,
8631a2
   return inter;
8631a2
 }
8631a2
 
8631a2
-struct grub_net_network_level_interface *
8631a2
-grub_net_configure_by_dhcpv6_ack (const char *name,
8631a2
-				  struct grub_net_card *card,
8631a2
-				  grub_net_interface_flags_t flags
8631a2
-				    __attribute__((__unused__)),
8631a2
-				  const grub_net_link_level_address_t *hwaddr,
8631a2
-				  const struct grub_net_dhcpv6_packet *packet,
8631a2
-				  int is_def, char **device, char **path)
8631a2
-{
8631a2
-  struct grub_net_network_level_interface *inter = NULL;
8631a2
-  struct grub_net_network_level_address addr;
8631a2
-  int mask = -1;
8631a2
-
8631a2
-  if (!device || !path)
8631a2
+/* The default netbuff size for sending DHCPv6 packets which should be
8631a2
+   large enough to hold the information */
8631a2
+#define GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE 512
8631a2
+
8631a2
+struct grub_dhcp6_options
8631a2
+{
8631a2
+  grub_uint8_t *client_duid;
8631a2
+  grub_uint16_t client_duid_len;
8631a2
+  grub_uint8_t *server_duid;
8631a2
+  grub_uint16_t server_duid_len;
8631a2
+  grub_uint32_t iaid;
8631a2
+  grub_uint32_t t1;
8631a2
+  grub_uint32_t t2;
8631a2
+  grub_net_network_level_address_t *ia_addr;
8631a2
+  grub_uint32_t preferred_lifetime;
8631a2
+  grub_uint32_t valid_lifetime;
8631a2
+  grub_net_network_level_address_t *dns_server_addrs;
8631a2
+  grub_uint16_t num_dns_server;
8631a2
+  char *boot_file_proto;
8631a2
+  char *boot_file_server_ip;
8631a2
+  char *boot_file_path;
8631a2
+};
8631a2
+
8631a2
+typedef struct grub_dhcp6_options *grub_dhcp6_options_t;
8631a2
+
8631a2
+struct grub_dhcp6_session
8631a2
+{
8631a2
+  struct grub_dhcp6_session *next;
8631a2
+  struct grub_dhcp6_session **prev;
8631a2
+  grub_uint32_t iaid;
8631a2
+  grub_uint32_t transaction_id:24;
8631a2
+  grub_uint64_t start_time;
8631a2
+  struct grub_net_dhcp6_option_duid_ll duid;
8631a2
+  struct grub_net_network_level_interface *iface;
8631a2
+
8631a2
+  /* The associated dhcpv6 options */
8631a2
+  grub_dhcp6_options_t adv;
8631a2
+  grub_dhcp6_options_t reply;
8631a2
+};
8631a2
+
8631a2
+typedef struct grub_dhcp6_session *grub_dhcp6_session_t;
8631a2
+
8631a2
+typedef void (*dhcp6_option_hook_fn) (const struct grub_net_dhcp6_option *opt, void *data);
8631a2
+
8631a2
+static void
8631a2
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt, grub_size_t size,
8631a2
+		      dhcp6_option_hook_fn hook, void *hook_data);
8631a2
+
8631a2
+static void
8631a2
+parse_dhcp6_iaaddr (const struct grub_net_dhcp6_option *opt, void *data)
8631a2
+{
8631a2
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t )data;
8631a2
+
8631a2
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
8631a2
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
8631a2
+
8631a2
+  if (code == GRUB_NET_DHCP6_OPTION_IAADDR)
8631a2
+    {
8631a2
+      const struct grub_net_dhcp6_option_iaaddr *iaaddr;
8631a2
+      iaaddr = (const struct grub_net_dhcp6_option_iaaddr *)opt->data;
8631a2
+
8631a2
+      if (len < sizeof (*iaaddr))
8631a2
+	{
8631a2
+	  grub_dprintf ("bootp", "DHCPv6: code %u with insufficient length %u\n", code, len);
8631a2
+	  return;
8631a2
+	}
8631a2
+      if (!dhcp6->ia_addr)
8631a2
+	{
8631a2
+	  dhcp6->ia_addr = grub_malloc (sizeof(*dhcp6->ia_addr));
8631a2
+	  dhcp6->ia_addr->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
8631a2
+	  dhcp6->ia_addr->ipv6[0] = grub_get_unaligned64 (iaaddr->addr);
8631a2
+	  dhcp6->ia_addr->ipv6[1] = grub_get_unaligned64 (iaaddr->addr + 8);
8631a2
+	  dhcp6->preferred_lifetime = grub_be_to_cpu32 (iaaddr->preferred_lifetime);
8631a2
+	  dhcp6->valid_lifetime = grub_be_to_cpu32 (iaaddr->valid_lifetime);
8631a2
+	}
8631a2
+    }
8631a2
+}
8631a2
+
8631a2
+static void
8631a2
+parse_dhcp6_option (const struct grub_net_dhcp6_option *opt, void *data)
8631a2
+{
8631a2
+  grub_dhcp6_options_t dhcp6 = (grub_dhcp6_options_t)data;
8631a2
+  grub_uint16_t code = grub_be_to_cpu16 (opt->code);
8631a2
+  grub_uint16_t len = grub_be_to_cpu16 (opt->len);
8631a2
+
8631a2
+  switch (code)
8631a2
+    {
8631a2
+      case GRUB_NET_DHCP6_OPTION_CLIENTID:
8631a2
+
8631a2
+	if (dhcp6->client_duid || !len)
8631a2
+	  {
8631a2
+	    grub_dprintf ("bootp", "Skipped DHCPv6 CLIENTID with length %u\n", len);
8631a2
+	    break;
8631a2
+	  }
8631a2
+	dhcp6->client_duid = grub_malloc (len);
8631a2
+	grub_memcpy (dhcp6->client_duid, opt->data, len);
8631a2
+	dhcp6->client_duid_len = len;
8631a2
+	break;
8631a2
+
8631a2
+      case GRUB_NET_DHCP6_OPTION_SERVERID:
8631a2
+
8631a2
+	if (dhcp6->server_duid || !len)
8631a2
+	  {
8631a2
+	    grub_dprintf ("bootp", "Skipped DHCPv6 SERVERID with length %u\n", len);
8631a2
+	    break;
8631a2
+	  }
8631a2
+	dhcp6->server_duid = grub_malloc (len);
8631a2
+	grub_memcpy (dhcp6->server_duid, opt->data, len);
8631a2
+	dhcp6->server_duid_len = len;
8631a2
+	break;
8631a2
+
8631a2
+      case GRUB_NET_DHCP6_OPTION_IA_NA:
8631a2
+	{
8631a2
+	  const struct grub_net_dhcp6_option_iana *ia_na;
8631a2
+	  grub_uint16_t data_len;
8631a2
+
8631a2
+	  if (dhcp6->iaid || len < sizeof (*ia_na))
8631a2
+	    {
8631a2
+	      grub_dprintf ("bootp", "Skipped DHCPv6 IA_NA with length %u\n", len);
8631a2
+	      break;
8631a2
+	    }
8631a2
+	  ia_na = (const struct grub_net_dhcp6_option_iana *)opt->data;
8631a2
+	  dhcp6->iaid = grub_be_to_cpu32 (ia_na->iaid);
8631a2
+	  dhcp6->t1 = grub_be_to_cpu32 (ia_na->t1);
8631a2
+	  dhcp6->t2 = grub_be_to_cpu32 (ia_na->t2);
8631a2
+
8631a2
+	  data_len = len - sizeof (*ia_na);
8631a2
+	  if (data_len)
8631a2
+	    foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)ia_na->data, data_len, parse_dhcp6_iaaddr, dhcp6);
8631a2
+	}
8631a2
+	break;
8631a2
+
8631a2
+      case GRUB_NET_DHCP6_OPTION_DNS_SERVERS:
8631a2
+	{
8631a2
+	  const grub_uint8_t *po;
8631a2
+	  grub_uint16_t ln;
8631a2
+	  grub_net_network_level_address_t *la;
8631a2
+
8631a2
+	  if (!len || len & 0xf)
8631a2
+	    {
8631a2
+	      grub_dprintf ("bootp", "Skip invalid length DHCPv6 DNS_SERVERS \n");
8631a2
+	      break;
8631a2
+	    }
8631a2
+	  dhcp6->num_dns_server = ln = len >> 4;
8631a2
+	  dhcp6->dns_server_addrs = la = grub_zalloc (ln * sizeof (*la));
8631a2
+
8631a2
+	  for (po = opt->data; ln > 0; po += 0x10, la++, ln--)
8631a2
+	    {
8631a2
+	      la->type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
8631a2
+	      la->ipv6[0] = grub_get_unaligned64 (po);
8631a2
+	      la->ipv6[1] = grub_get_unaligned64 (po + 8);
8631a2
+	      la->option = DNS_OPTION_PREFER_IPV6;
8631a2
+	    }
8631a2
+	}
8631a2
+	break;
8631a2
+
8631a2
+      case GRUB_NET_DHCP6_OPTION_BOOTFILE_URL:
8631a2
+	dissect_url ((const char *)opt->data,
8631a2
+		      &dhcp6->boot_file_proto,
8631a2
+		      &dhcp6->boot_file_server_ip,
8631a2
+		      &dhcp6->boot_file_path);
8631a2
+	break;
8631a2
+
8631a2
+      default:
8631a2
+	break;
8631a2
+    }
8631a2
+}
8631a2
+
8631a2
+static void
8631a2
+foreach_dhcp6_option (const struct grub_net_dhcp6_option *opt, grub_size_t size, dhcp6_option_hook_fn hook, void *hook_data)
8631a2
+{
8631a2
+  while (size)
8631a2
+    {
8631a2
+      grub_uint16_t code, len;
8631a2
+
8631a2
+      if (size < sizeof (*opt))
8631a2
+	{
8631a2
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped with remaining size %" PRIxGRUB_SIZE "\n", size);
8631a2
+	  break;
8631a2
+	}
8631a2
+      size -= sizeof (*opt);
8631a2
+      len = grub_be_to_cpu16 (opt->len);
8631a2
+      code = grub_be_to_cpu16 (opt->code);
8631a2
+      if (size < len)
8631a2
+	{
8631a2
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped at out of bound length %u for option %u\n", len, code);
8631a2
+	  break;
8631a2
+	}
8631a2
+      if (!len)
8631a2
+	{
8631a2
+	  grub_dprintf ("bootp", "DHCPv6: Options stopped at zero length option %u\n", code);
8631a2
+	  break;
8631a2
+	}
8631a2
+      else
8631a2
+	{
8631a2
+	  if (hook)
8631a2
+	    hook (opt, hook_data);
8631a2
+	  size -= len;
8631a2
+	  opt = (const struct grub_net_dhcp6_option *)((grub_uint8_t *)opt + len + sizeof (*opt));
8631a2
+	}
8631a2
+    }
8631a2
+}
8631a2
+
8631a2
+static grub_dhcp6_options_t
8631a2
+grub_dhcp6_options_get (const struct grub_net_dhcp6_packet *v6h,
8631a2
+			grub_size_t size)
8631a2
+{
8631a2
+  grub_dhcp6_options_t options;
8631a2
+
8631a2
+  if (size < sizeof (*v6h))
8631a2
+    {
8631a2
+      grub_error (GRUB_ERR_OUT_OF_RANGE, N_("DHCPv6 packet size too small"));
8631a2
+      return NULL;
8631a2
+    }
8631a2
+
8631a2
+  options = grub_zalloc (sizeof(*options));
8631a2
+  if (!options)
8631a2
     return NULL;
8631a2
 
8631a2
-  *device = 0;
8631a2
-  *path = 0;
8631a2
-
8631a2
-  grub_dprintf ("net", "mac address is %02x:%02x:%02x:%02x:%02x:%02x\n",
8631a2
-		hwaddr->mac[0], hwaddr->mac[1], hwaddr->mac[2],
8631a2
-		hwaddr->mac[3], hwaddr->mac[4], hwaddr->mac[5]);
8631a2
-
8631a2
-  if (is_def)
8631a2
-    grub_net_default_server = 0;
8631a2
-
8631a2
-  if (is_def && !grub_net_default_server && packet)
8631a2
+  foreach_dhcp6_option ((const struct grub_net_dhcp6_option *)v6h->dhcp_options,
8631a2
+		       size - sizeof (*v6h), parse_dhcp6_option, options);
8631a2
+
8631a2
+  return options;
8631a2
+}
8631a2
+
8631a2
+static void
8631a2
+grub_dhcp6_options_free (grub_dhcp6_options_t options)
8631a2
+{
8631a2
+  if (options->client_duid)
8631a2
+    grub_free (options->client_duid);
8631a2
+  if (options->server_duid)
8631a2
+    grub_free (options->server_duid);
8631a2
+  if (options->ia_addr)
8631a2
+    grub_free (options->ia_addr);
8631a2
+  if (options->dns_server_addrs)
8631a2
+    grub_free (options->dns_server_addrs);
8631a2
+  if (options->boot_file_proto)
8631a2
+    grub_free (options->boot_file_proto);
8631a2
+  if (options->boot_file_server_ip)
8631a2
+    grub_free (options->boot_file_server_ip);
8631a2
+  if (options->boot_file_path)
8631a2
+    grub_free (options->boot_file_path);
8631a2
+
8631a2
+  grub_free (options);
8631a2
+}
8631a2
+
8631a2
+static grub_dhcp6_session_t grub_dhcp6_sessions;
8631a2
+#define FOR_DHCP6_SESSIONS_SAFE(var, next) FOR_LIST_ELEMENTS_SAFE (var, next, grub_dhcp6_sessions)
8631a2
+#define FOR_DHCP6_SESSIONS(var) FOR_LIST_ELEMENTS (var, grub_dhcp6_sessions)
8631a2
+
8631a2
+static void
8631a2
+grub_net_configure_by_dhcp6_info (const char *name,
8631a2
+	  struct grub_net_card *card,
8631a2
+	  grub_dhcp6_options_t dhcp6,
8631a2
+	  int is_def,
8631a2
+	  int flags,
8631a2
+	  struct grub_net_network_level_interface **ret_inf)
8631a2
+{
8631a2
+  grub_net_network_level_netaddress_t netaddr;
8631a2
+  struct grub_net_network_level_interface *inf;
8631a2
+
8631a2
+  if (dhcp6->ia_addr)
8631a2
     {
8631a2
-      const grub_uint8_t *options = packet->dhcp_options;
8631a2
-      unsigned int option_max = 1024 - OFFSET_OF (dhcp_options, packet);
8631a2
-      unsigned int i;
8631a2
-
8631a2
-      for (i = 0; i < option_max - sizeof (grub_net_dhcpv6_option_t); )
8631a2
-	{
8631a2
-	  grub_uint16_t num, len;
8631a2
-	  grub_net_dhcpv6_option_t *opt =
8631a2
-	    (grub_net_dhcpv6_option_t *)(options + i);
8631a2
-
8631a2
-	  num = grub_be_to_cpu16(opt->option_num);
8631a2
-	  len = grub_be_to_cpu16(opt->option_len);
8631a2
-
8631a2
-	  grub_dprintf ("net", "got dhcpv6 option %d len %d\n", num, len);
8631a2
-
8631a2
-	  if (len == 0)
8631a2
-	    break;
8631a2
-
8631a2
-	  if (len + i > 1024)
8631a2
-	    break;
8631a2
-
8631a2
-	  if (num == GRUB_NET_DHCP6_BOOTFILE_URL)
8631a2
-	    {
8631a2
-	      char *scheme, *userinfo, *host, *file;
8631a2
-	      char *tmp;
8631a2
-	      int hostlen;
8631a2
-	      int port;
8631a2
-	      int rc = extract_url_info ((const char *)opt->option_data,
8631a2
-					 (grub_size_t)len,
8631a2
-					 &scheme, &userinfo, &host, &port,
8631a2
-					 &file;;
8631a2
-	      if (rc < 0)
8631a2
-		continue;
8631a2
-
8631a2
-	      /* right now this only handles tftp. */
8631a2
-	      if (grub_strcmp("tftp", scheme))
8631a2
-		{
8631a2
-		  grub_free (scheme);
8631a2
-		  grub_free (userinfo);
8631a2
-		  grub_free (host);
8631a2
-		  grub_free (file);
8631a2
-		  continue;
8631a2
-		}
8631a2
-	      grub_free (userinfo);
8631a2
-
8631a2
-	      hostlen = grub_strlen (host);
8631a2
-	      if (hostlen > 2 && host[0] == '[' && host[hostlen-1] == ']')
8631a2
-		{
8631a2
-		  tmp = host+1;
8631a2
-		  host[hostlen-1] = '\0';
8631a2
-		}
8631a2
-	      else
8631a2
-		tmp = host;
8631a2
+      inf = grub_net_add_addr (name, card, dhcp6->ia_addr, &card->default_address, flags);
8631a2
 
8631a2
-	      *device = grub_xasprintf ("%s,%s", scheme, tmp);
8631a2
-	      grub_free (scheme);
8631a2
-	      grub_free (host);
8631a2
+      netaddr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
8631a2
+      netaddr.ipv6.base[0] = dhcp6->ia_addr->ipv6[0];
8631a2
+      netaddr.ipv6.base[1] = 0;
8631a2
+      netaddr.ipv6.masksize = 64;
8631a2
+      grub_net_add_route (name, netaddr, inf);
8631a2
 
8631a2
-	      if (file && *file)
8631a2
-		{
8631a2
-		  tmp = grub_strrchr (file, '/');
8631a2
-		  if (tmp)
8631a2
-		    *(tmp+1) = '\0';
8631a2
-		  else
8631a2
-		    file[0] = '\0';
8631a2
-		}
8631a2
-	      else if (!file)
8631a2
-		file = grub_strdup ("");
8631a2
-
8631a2
-	      if (file[0] == '/')
8631a2
-		{
8631a2
-		  *path = grub_strdup (file+1);
8631a2
-		  grub_free (file);
8631a2
-		}
8631a2
-	      else
8631a2
-		*path = file;
8631a2
-	    }
8631a2
-	  else if (num == GRUB_NET_DHCP6_IA_NA)
8631a2
-	    {
8631a2
-	      const grub_net_dhcpv6_option_t *ia_na_opt;
8631a2
-	      const grub_net_dhcpv6_opt_ia_na_t *ia_na =
8631a2
-		(const grub_net_dhcpv6_opt_ia_na_t *)opt;
8631a2
-	      unsigned int left = len - OFFSET_OF (options, ia_na);
8631a2
-	      unsigned int j;
8631a2
-
8631a2
-	      if ((grub_uint8_t *)ia_na + left >
8631a2
-		  (grub_uint8_t *)options + option_max)
8631a2
-		left -= ((grub_uint8_t *)ia_na + left)
8631a2
-		        - ((grub_uint8_t *)options + option_max);
8631a2
-
8631a2
-	      if (len < OFFSET_OF (option_data, opt)
8631a2
-			+ sizeof (grub_net_dhcpv6_option_t))
8631a2
-		{
8631a2
-		  grub_dprintf ("net",
8631a2
-				"found dhcpv6 ia_na option with no address\n");
8631a2
-		  continue;
8631a2
-		}
8631a2
-
8631a2
-	      for (j = 0; left > sizeof (grub_net_dhcpv6_option_t); )
8631a2
-		{
8631a2
-		  ia_na_opt = (const grub_net_dhcpv6_option_t *)
8631a2
-			       (ia_na->options + j);
8631a2
-		  grub_uint16_t ia_na_opt_num, ia_na_opt_len;
8631a2
-
8631a2
-		  ia_na_opt_num = grub_be_to_cpu16 (ia_na_opt->option_num);
8631a2
-		  ia_na_opt_len = grub_be_to_cpu16 (ia_na_opt->option_len);
8631a2
-		  if (ia_na_opt_len == 0)
8631a2
-		    break;
8631a2
-		  if (j + ia_na_opt_len > left)
8631a2
-		    break;
8631a2
-		  if (ia_na_opt_num == GRUB_NET_DHCP6_IA_ADDRESS)
8631a2
-		    {
8631a2
-		      const grub_net_dhcpv6_opt_ia_address_t *ia_addr;
8631a2
-
8631a2
-		      ia_addr = (const grub_net_dhcpv6_opt_ia_address_t *)
8631a2
-				 ia_na_opt;
8631a2
-		      addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
8631a2
-		      grub_memcpy(addr.ipv6, ia_addr->ipv6_address,
8631a2
-				  sizeof (ia_addr->ipv6_address));
8631a2
-		      inter = grub_net_add_addr (name, card, &addr, hwaddr, 0);
8631a2
-		    }
8631a2
-
8631a2
-		  j += ia_na_opt_len;
8631a2
-		  left -= ia_na_opt_len;
8631a2
-		}
8631a2
-	    }
8631a2
+      if (ret_inf)
8631a2
+	*ret_inf = inf;
8631a2
+    }
8631a2
 
8631a2
-	  i += len + 4;
8631a2
-	}
8631a2
+  if (dhcp6->dns_server_addrs)
8631a2
+    {
8631a2
+      grub_uint16_t i;
8631a2
 
8631a2
-      grub_print_error ();
8631a2
+      for (i = 0; i < dhcp6->num_dns_server; ++i)
8631a2
+	grub_net_add_dns_server (dhcp6->dns_server_addrs + i);
8631a2
     }
8631a2
 
8631a2
-  if (is_def)
8631a2
+  if (dhcp6->boot_file_path)
8631a2
+    grub_env_set_net_property (name, "boot_file", dhcp6->boot_file_path,
8631a2
+			  grub_strlen (dhcp6->boot_file_path));
8631a2
+
8631a2
+  if (is_def && dhcp6->boot_file_server_ip)
8631a2
     {
8631a2
+      grub_net_default_server = grub_strdup (dhcp6->boot_file_server_ip);
8631a2
       grub_env_set ("net_default_interface", name);
8631a2
       grub_env_export ("net_default_interface");
8631a2
     }
8631a2
+}
8631a2
+
8631a2
+static void
8631a2
+grub_dhcp6_session_add (struct grub_net_network_level_interface *iface,
8631a2
+			grub_uint32_t iaid)
8631a2
+{
8631a2
+  grub_dhcp6_session_t se;
8631a2
+  struct grub_datetime date;
8631a2
+  grub_err_t err;
8631a2
+  grub_int32_t t = 0;
8631a2
+
8631a2
+  se = grub_malloc (sizeof (*se));
8631a2
+
8631a2
+  err = grub_get_datetime (&date);
8631a2
+  if (err || !grub_datetime2unixtime (&date, &t))
8631a2
+    {
8631a2
+      grub_errno = GRUB_ERR_NONE;
8631a2
+      t = 0;
8631a2
+    }
8631a2
+
8631a2
+  se->iface = iface;
8631a2
+  se->iaid = iaid;
8631a2
+  se->transaction_id = t;
8631a2
+  se->start_time = grub_get_time_ms ();
8631a2
+  se->duid.type = grub_cpu_to_be16_compile_time (3) ;
8631a2
+  se->duid.hw_type = grub_cpu_to_be16_compile_time (1);
8631a2
+  grub_memcpy (&se->duid.hwaddr, &iface->hwaddress.mac, sizeof (se->duid.hwaddr));
8631a2
+  se->adv = NULL;
8631a2
+  se->reply = NULL;
8631a2
+  grub_list_push (GRUB_AS_LIST_P (&grub_dhcp6_sessions), GRUB_AS_LIST (se));
8631a2
+}
8631a2
+
8631a2
+static void
8631a2
+grub_dhcp6_session_remove (grub_dhcp6_session_t se)
8631a2
+{
8631a2
+  grub_list_remove (GRUB_AS_LIST (se));
8631a2
+  if (se->adv)
8631a2
+    grub_dhcp6_options_free (se->adv);
8631a2
+  if (se->reply)
8631a2
+    grub_dhcp6_options_free (se->reply);
8631a2
+  grub_free (se);
8631a2
+}
8631a2
+
8631a2
+static void
8631a2
+grub_dhcp6_session_remove_all (void)
8631a2
+{
8631a2
+  grub_dhcp6_session_t se, next;
8631a2
+
8631a2
+  FOR_DHCP6_SESSIONS_SAFE (se, next)
8631a2
+    {
8631a2
+      grub_dhcp6_session_remove (se);
8631a2
+    }
8631a2
+  grub_dhcp6_sessions = NULL;
8631a2
+}
8631a2
+
8631a2
+static grub_err_t
8631a2
+grub_dhcp6_session_configure_network (grub_dhcp6_session_t se)
8631a2
+{
8631a2
+  char *name;
8631a2
 
8631a2
-    if (inter)
8631a2
-      grub_net_add_ipv6_local (inter, mask);
8631a2
-    return inter;
8631a2
+  name = grub_xasprintf ("%s:dhcp6", se->iface->card->name);
8631a2
+  if (!name)
8631a2
+    return grub_errno;
8631a2
+
8631a2
+  grub_net_configure_by_dhcp6_info (name, se->iface->card, se->reply, 1, 0, 0);
8631a2
+  grub_free (name);
8631a2
+
8631a2
+  return GRUB_ERR_NONE;
8631a2
 }
8631a2
 
8631a2
+static grub_err_t
8631a2
+grub_dhcp6_session_send_request (grub_dhcp6_session_t se)
8631a2
+{
8631a2
+  struct grub_net_buff *nb;
8631a2
+  struct grub_net_dhcp6_option *opt;
8631a2
+  struct grub_net_dhcp6_packet *v6h;
8631a2
+  struct grub_net_dhcp6_option_iana *ia_na;
8631a2
+  struct grub_net_dhcp6_option_iaaddr *iaaddr;
8631a2
+  struct udphdr *udph;
8631a2
+  grub_net_network_level_address_t multicast;
8631a2
+  grub_net_link_level_address_t ll_multicast;
8631a2
+  grub_uint64_t elapsed;
8631a2
+  struct grub_net_network_level_interface *inf = se->iface;
8631a2
+  grub_dhcp6_options_t dhcp6 = se->adv;
8631a2
+  grub_err_t err = GRUB_ERR_NONE;
8631a2
+
8631a2
+  multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
8631a2
+  multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
8631a2
+  multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
8631a2
+
8631a2
+  err = grub_net_link_layer_resolve (inf, &multicast, &ll_multicast);
8631a2
+  if (err)
8631a2
+    return err;
8631a2
+
8631a2
+  nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
8631a2
+
8631a2
+  if (!nb)
8631a2
+    return grub_errno;
8631a2
+
8631a2
+  err = grub_netbuff_reserve (nb, GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
8631a2
+  if (err)
8631a2
+    {
8631a2
+      grub_netbuff_free (nb);
8631a2
+      return err;
8631a2
+    }
8631a2
+
8631a2
+  err = grub_netbuff_push (nb, dhcp6->client_duid_len + sizeof (*opt));
8631a2
+  if (err)
8631a2
+    {
8631a2
+      grub_netbuff_free (nb);
8631a2
+      return err;
8631a2
+    }
8631a2
+  opt = (struct grub_net_dhcp6_option *)nb->data;
8631a2
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID);
8631a2
+  opt->len = grub_cpu_to_be16 (dhcp6->client_duid_len);
8631a2
+  grub_memcpy (opt->data, dhcp6->client_duid , dhcp6->client_duid_len);
8631a2
+
8631a2
+  err = grub_netbuff_push (nb, dhcp6->server_duid_len + sizeof (*opt));
8631a2
+  if (err)
8631a2
+    {
8631a2
+      grub_netbuff_free (nb);
8631a2
+      return err;
8631a2
+    }
8631a2
+  opt = (struct grub_net_dhcp6_option *)nb->data;
8631a2
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_SERVERID);
8631a2
+  opt->len = grub_cpu_to_be16 (dhcp6->server_duid_len);
8631a2
+  grub_memcpy (opt->data, dhcp6->server_duid , dhcp6->server_duid_len);
8631a2
+
8631a2
+  err = grub_netbuff_push (nb, sizeof (*ia_na) + sizeof (*opt));
8631a2
+  if (err)
8631a2
+    {
8631a2
+      grub_netbuff_free (nb);
8631a2
+      return err;
8631a2
+    }
8631a2
+
8631a2
+  if (dhcp6->ia_addr)
8631a2
+    {
8631a2
+      err = grub_netbuff_push (nb, sizeof(*iaaddr) + sizeof (*opt));
8631a2
+      if (err)
8631a2
+	{
8631a2
+	  grub_netbuff_free (nb);
8631a2
+	  return err;
8631a2
+	}
8631a2
+    }
8631a2
+  opt = (struct grub_net_dhcp6_option *)nb->data;
8631a2
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
8631a2
+  opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
8631a2
+  if (dhcp6->ia_addr)
8631a2
+    opt->len += grub_cpu_to_be16 (sizeof(*iaaddr) + sizeof (*opt));
8631a2
+
8631a2
+  ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
8631a2
+  ia_na->iaid = grub_cpu_to_be32 (dhcp6->iaid);
8631a2
+
8631a2
+  ia_na->t1 = grub_cpu_to_be32 (dhcp6->t1);
8631a2
+  ia_na->t2 = grub_cpu_to_be32 (dhcp6->t2);
8631a2
+
8631a2
+  if (dhcp6->ia_addr)
8631a2
+    {
8631a2
+      opt = (struct grub_net_dhcp6_option *)ia_na->data;
8631a2
+      opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IAADDR);
8631a2
+      opt->len = grub_cpu_to_be16 (sizeof (*iaaddr));
8631a2
+      iaaddr = (struct grub_net_dhcp6_option_iaaddr *)opt->data;
8631a2
+      grub_set_unaligned64 (iaaddr->addr, dhcp6->ia_addr->ipv6[0]);
8631a2
+      grub_set_unaligned64 (iaaddr->addr + 8, dhcp6->ia_addr->ipv6[1]);
8631a2
+
8631a2
+      iaaddr->preferred_lifetime = grub_cpu_to_be32 (dhcp6->preferred_lifetime);
8631a2
+      iaaddr->valid_lifetime = grub_cpu_to_be32 (dhcp6->valid_lifetime);
8631a2
+    }
8631a2
+
8631a2
+  err = grub_netbuff_push (nb, sizeof (*opt) + 2 * sizeof (grub_uint16_t));
8631a2
+  if (err)
8631a2
+    {
8631a2
+      grub_netbuff_free (nb);
8631a2
+      return err;
8631a2
+    }
8631a2
+
8631a2
+  opt = (struct grub_net_dhcp6_option*) nb->data;
8631a2
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ORO);
8631a2
+  opt->len = grub_cpu_to_be16_compile_time (2 * sizeof (grub_uint16_t));
8631a2
+  grub_set_unaligned16 (opt->data, grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_BOOTFILE_URL));
8631a2
+  grub_set_unaligned16 (opt->data + 2, grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_DNS_SERVERS));
8631a2
+
8631a2
+  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
8631a2
+  if (err)
8631a2
+    {
8631a2
+      grub_netbuff_free (nb);
8631a2
+      return err;
8631a2
+    }
8631a2
+  opt = (struct grub_net_dhcp6_option*) nb->data;
8631a2
+  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);
8631a2
+  opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
8631a2
+
8631a2
+  /* the time is expressed in hundredths of a second */
8631a2
+  elapsed = grub_divmod64 (grub_get_time_ms () - se->start_time, 10, 0);
8631a2
+
8631a2
+  if (elapsed > 0xffff)
8631a2
+    elapsed = 0xffff;
8631a2
+
8631a2
+  grub_set_unaligned16 (opt->data,  grub_cpu_to_be16 ((grub_uint16_t)elapsed));
8631a2
+
8631a2
+  err = grub_netbuff_push (nb, sizeof (*v6h));
8631a2
+  if (err)
8631a2
+    {
8631a2
+      grub_netbuff_free (nb);
8631a2
+      return err;
8631a2
+    }
8631a2
+
8631a2
+  v6h = (struct grub_net_dhcp6_packet *) nb->data;
8631a2
+  v6h->message_type = GRUB_NET_DHCP6_REQUEST;
8631a2
+  v6h->transaction_id = se->transaction_id;
8631a2
+
8631a2
+  err = grub_netbuff_push (nb, sizeof (*udph));
8631a2
+  if (err)
8631a2
+    {
8631a2
+      grub_netbuff_free (nb);
8631a2
+      return err;
8631a2
+    }
8631a2
+
8631a2
+  udph = (struct udphdr *) nb->data;
8631a2
+  udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
8631a2
+  udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
8631a2
+  udph->chksum = 0;
8631a2
+  udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
8631a2
+
8631a2
+  udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
8631a2
+						 &inf->address,
8631a2
+						 &multicast);
8631a2
+  err = grub_net_send_ip_packet (inf, &multicast, &ll_multicast, nb,
8631a2
+				 GRUB_NET_IP_UDP);
8631a2
+
8631a2
+  grub_netbuff_free (nb);
8631a2
+
8631a2
+  return err;
8631a2
+}
8631a2
+
8631a2
+struct grub_net_network_level_interface *
8631a2
+grub_net_configure_by_dhcpv6_reply (const char *name,
8631a2
+	struct grub_net_card *card,
8631a2
+	grub_net_interface_flags_t flags,
8631a2
+	const struct grub_net_dhcp6_packet *v6h,
8631a2
+	grub_size_t size,
8631a2
+	int is_def,
8631a2
+	char **device, char **path)
8631a2
+{
8631a2
+  struct grub_net_network_level_interface *inf;
8631a2
+  grub_dhcp6_options_t dhcp6;
8631a2
+
8631a2
+  dhcp6 = grub_dhcp6_options_get (v6h, size);
8631a2
+  if (!dhcp6)
8631a2
+    {
8631a2
+      grub_print_error ();
8631a2
+      return NULL;
8631a2
+    }
8631a2
+
8631a2
+  grub_net_configure_by_dhcp6_info (name, card, dhcp6, is_def, flags, &inf);
8631a2
+
8631a2
+  if (device && dhcp6->boot_file_proto && dhcp6->boot_file_server_ip)
8631a2
+    {
8631a2
+      *device = grub_xasprintf ("%s,%s", dhcp6->boot_file_proto, dhcp6->boot_file_server_ip);
8631a2
+      grub_print_error ();
8631a2
+    }
8631a2
+  if (path && dhcp6->boot_file_path)
8631a2
+    {
8631a2
+      *path = grub_strdup (dhcp6->boot_file_path);
8631a2
+      grub_print_error ();
8631a2
+      if (*path)
8631a2
+	{
8631a2
+	  char *slash;
8631a2
+	  slash = grub_strrchr (*path, '/');
8631a2
+	  if (slash)
8631a2
+	    *slash = 0;
8631a2
+	  else
8631a2
+	    **path = 0;
8631a2
+	}
8631a2
+    }
8631a2
+
8631a2
+  grub_dhcp6_options_free (dhcp6);
8631a2
+  return inf;
8631a2
+}
8631a2
 
8631a2
 void
8631a2
 grub_net_process_dhcp (struct grub_net_buff *nb,
8631a2
@@ -550,6 +1042,77 @@ grub_net_process_dhcp (struct grub_net_buff *nb,
8631a2
     }
8631a2
 }
8631a2
 
8631a2
+grub_err_t
8631a2
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
8631a2
+			struct grub_net_card *card __attribute__ ((unused)))
8631a2
+{
8631a2
+  const struct grub_net_dhcp6_packet *v6h;
8631a2
+  grub_dhcp6_session_t se;
8631a2
+  grub_size_t size;
8631a2
+  grub_dhcp6_options_t options;
8631a2
+
8631a2
+  v6h = (const struct grub_net_dhcp6_packet *) nb->data;
8631a2
+  size = nb->tail - nb->data;
8631a2
+
8631a2
+  options = grub_dhcp6_options_get (v6h, size);
8631a2
+  if (!options)
8631a2
+    return grub_errno;
8631a2
+
8631a2
+  if (!options->client_duid || !options->server_duid || !options->ia_addr)
8631a2
+    {
8631a2
+      grub_dhcp6_options_free (options);
8631a2
+      return grub_error (GRUB_ERR_BAD_ARGUMENT, "Bad DHCPv6 Packet");
8631a2
+    }
8631a2
+
8631a2
+  FOR_DHCP6_SESSIONS (se)
8631a2
+    {
8631a2
+      if (se->transaction_id == v6h->transaction_id &&
8631a2
+	  grub_memcmp (options->client_duid, &se->duid, sizeof (se->duid)) == 0 &&
8631a2
+	  se->iaid == options->iaid)
8631a2
+	break;
8631a2
+    }
8631a2
+
8631a2
+  if (!se)
8631a2
+    {
8631a2
+      grub_dprintf ("bootp", "DHCPv6 session not found\n");
8631a2
+      grub_dhcp6_options_free (options);
8631a2
+      return GRUB_ERR_NONE;
8631a2
+    }
8631a2
+
8631a2
+  if (v6h->message_type == GRUB_NET_DHCP6_ADVERTISE)
8631a2
+    {
8631a2
+      if (se->adv)
8631a2
+	{
8631a2
+	  grub_dprintf ("bootp", "Skipped DHCPv6 Advertised .. \n");
8631a2
+	  grub_dhcp6_options_free (options);
8631a2
+	  return GRUB_ERR_NONE;
8631a2
+	}
8631a2
+
8631a2
+      se->adv = options;
8631a2
+      return grub_dhcp6_session_send_request (se);
8631a2
+    }
8631a2
+  else if (v6h->message_type == GRUB_NET_DHCP6_REPLY)
8631a2
+    {
8631a2
+      if (!se->adv)
8631a2
+	{
8631a2
+	  grub_dprintf ("bootp", "Skipped DHCPv6 Reply .. \n");
8631a2
+	  grub_dhcp6_options_free (options);
8631a2
+	  return GRUB_ERR_NONE;
8631a2
+	}
8631a2
+
8631a2
+      se->reply = options;
8631a2
+      grub_dhcp6_session_configure_network (se);
8631a2
+      grub_dhcp6_session_remove (se);
8631a2
+      return GRUB_ERR_NONE;
8631a2
+    }
8631a2
+  else
8631a2
+    {
8631a2
+      grub_dhcp6_options_free (options);
8631a2
+    }
8631a2
+
8631a2
+  return GRUB_ERR_NONE;
8631a2
+}
8631a2
+
8631a2
 static grub_err_t
8631a2
 grub_cmd_dhcpopt (struct grub_command *cmd __attribute__ ((unused)),
8631a2
 		  int argc, char **args)
8631a2
@@ -824,7 +1387,174 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
8631a2
   return err;
8631a2
 }
8631a2
 
8631a2
-static grub_command_t cmd_getdhcp, cmd_bootp;
8631a2
+static grub_err_t
8631a2
+grub_cmd_bootp6 (struct grub_command *cmd __attribute__ ((unused)),
8631a2
+		  int argc, char **args)
8631a2
+{
8631a2
+  struct grub_net_card *card;
8631a2
+  grub_uint32_t iaid = 0;
8631a2
+  int interval;
8631a2
+  grub_err_t err;
8631a2
+  grub_dhcp6_session_t se;
8631a2
+
8631a2
+  err = GRUB_ERR_NONE;
8631a2
+
8631a2
+  FOR_NET_CARDS (card)
8631a2
+  {
8631a2
+    struct grub_net_network_level_interface *iface;
8631a2
+
8631a2
+    if (argc > 0 && grub_strcmp (card->name, args[0]) != 0)
8631a2
+      continue;
8631a2
+
8631a2
+    iface = grub_net_ipv6_get_link_local (card, &card->default_address);
8631a2
+    if (!iface)
8631a2
+      {
8631a2
+	grub_dhcp6_session_remove_all ();
8631a2
+	return grub_errno;
8631a2
+      }
8631a2
+
8631a2
+    grub_dhcp6_session_add (iface, iaid++);
8631a2
+  }
8631a2
+
8631a2
+  for (interval = 200; interval < 10000; interval *= 2)
8631a2
+    {
8631a2
+      int done = 1;
8631a2
+
8631a2
+      FOR_DHCP6_SESSIONS (se)
8631a2
+	{
8631a2
+	  struct grub_net_buff *nb;
8631a2
+	  struct grub_net_dhcp6_option *opt;
8631a2
+	  struct grub_net_dhcp6_packet *v6h;
8631a2
+	  struct grub_net_dhcp6_option_duid_ll *duid;
8631a2
+	  struct grub_net_dhcp6_option_iana *ia_na;
8631a2
+	  grub_net_network_level_address_t multicast;
8631a2
+	  grub_net_link_level_address_t ll_multicast;
8631a2
+	  struct udphdr *udph;
8631a2
+
8631a2
+	  multicast.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6;
8631a2
+	  multicast.ipv6[0] = grub_cpu_to_be64_compile_time (0xff02ULL << 48);
8631a2
+	  multicast.ipv6[1] = grub_cpu_to_be64_compile_time (0x10002ULL);
8631a2
+
8631a2
+	  err = grub_net_link_layer_resolve (se->iface,
8631a2
+		    &multicast, &ll_multicast);
8631a2
+	  if (err)
8631a2
+	    {
8631a2
+	      grub_dhcp6_session_remove_all ();
8631a2
+	      return err;
8631a2
+	    }
8631a2
+
8631a2
+	  nb = grub_netbuff_alloc (GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
8631a2
+
8631a2
+	  if (!nb)
8631a2
+	    {
8631a2
+	      grub_dhcp6_session_remove_all ();
8631a2
+	      return grub_errno;
8631a2
+	    }
8631a2
+
8631a2
+	  err = grub_netbuff_reserve (nb, GRUB_DHCP6_DEFAULT_NETBUFF_ALLOC_SIZE);
8631a2
+	  if (err)
8631a2
+	    {
8631a2
+	      grub_dhcp6_session_remove_all ();
8631a2
+	      grub_netbuff_free (nb);
8631a2
+	      return err;
8631a2
+	    }
8631a2
+
8631a2
+	  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (grub_uint16_t));
8631a2
+	  if (err)
8631a2
+	    {
8631a2
+	      grub_dhcp6_session_remove_all ();
8631a2
+	      grub_netbuff_free (nb);
8631a2
+	      return err;
8631a2
+	    }
8631a2
+
8631a2
+	  opt = (struct grub_net_dhcp6_option *)nb->data;
8631a2
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_ELAPSED_TIME);
8631a2
+	  opt->len = grub_cpu_to_be16_compile_time (sizeof (grub_uint16_t));
8631a2
+	  grub_set_unaligned16 (opt->data, 0);
8631a2
+
8631a2
+	  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*duid));
8631a2
+	  if (err)
8631a2
+	    {
8631a2
+	      grub_dhcp6_session_remove_all ();
8631a2
+	      grub_netbuff_free (nb);
8631a2
+	      return err;
8631a2
+	    }
8631a2
+
8631a2
+	  opt = (struct grub_net_dhcp6_option *)nb->data;
8631a2
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_CLIENTID);
8631a2
+	  opt->len = grub_cpu_to_be16 (sizeof (*duid));
8631a2
+
8631a2
+	  duid = (struct grub_net_dhcp6_option_duid_ll *) opt->data;
8631a2
+	  grub_memcpy (duid, &se->duid, sizeof (*duid));
8631a2
+
8631a2
+	  err = grub_netbuff_push (nb, sizeof (*opt) + sizeof (*ia_na));
8631a2
+	  if (err)
8631a2
+	    {
8631a2
+	      grub_dhcp6_session_remove_all ();
8631a2
+	      grub_netbuff_free (nb);
8631a2
+	      return err;
8631a2
+	    }
8631a2
+
8631a2
+	  opt = (struct grub_net_dhcp6_option *)nb->data;
8631a2
+	  opt->code = grub_cpu_to_be16_compile_time (GRUB_NET_DHCP6_OPTION_IA_NA);
8631a2
+	  opt->len = grub_cpu_to_be16 (sizeof (*ia_na));
8631a2
+	  ia_na = (struct grub_net_dhcp6_option_iana *)opt->data;
8631a2
+	  ia_na->iaid = grub_cpu_to_be32 (se->iaid);
8631a2
+	  ia_na->t1 = 0;
8631a2
+	  ia_na->t2 = 0;
8631a2
+
8631a2
+	  err = grub_netbuff_push (nb, sizeof (*v6h));
8631a2
+	  if (err)
8631a2
+	    {
8631a2
+	      grub_dhcp6_session_remove_all ();
8631a2
+	      grub_netbuff_free (nb);
8631a2
+	      return err;
8631a2
+	    }
8631a2
+
8631a2
+	  v6h = (struct grub_net_dhcp6_packet *)nb->data;
8631a2
+	  v6h->message_type = GRUB_NET_DHCP6_SOLICIT;
8631a2
+	  v6h->transaction_id = se->transaction_id;
8631a2
+
8631a2
+	  grub_netbuff_push (nb, sizeof (*udph));
8631a2
+
8631a2
+	  udph = (struct udphdr *) nb->data;
8631a2
+	  udph->src = grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT);
8631a2
+	  udph->dst = grub_cpu_to_be16_compile_time (DHCP6_SERVER_PORT);
8631a2
+	  udph->chksum = 0;
8631a2
+	  udph->len = grub_cpu_to_be16 (nb->tail - nb->data);
8631a2
+
8631a2
+	  udph->chksum = grub_net_ip_transport_checksum (nb, GRUB_NET_IP_UDP,
8631a2
+			    &se->iface->address, &multicast);
8631a2
+
8631a2
+	  err = grub_net_send_ip_packet (se->iface, &multicast,
8631a2
+		    &ll_multicast, nb, GRUB_NET_IP_UDP);
8631a2
+	  done = 0;
8631a2
+	  grub_netbuff_free (nb);
8631a2
+
8631a2
+	  if (err)
8631a2
+	    {
8631a2
+	      grub_dhcp6_session_remove_all ();
8631a2
+	      return err;
8631a2
+	    }
8631a2
+	}
8631a2
+      if (!done)
8631a2
+	grub_net_poll_cards (interval, 0);
8631a2
+    }
8631a2
+
8631a2
+  FOR_DHCP6_SESSIONS (se)
8631a2
+    {
8631a2
+      grub_error_push ();
8631a2
+      err = grub_error (GRUB_ERR_FILE_NOT_FOUND,
8631a2
+			N_("couldn't autoconfigure %s"),
8631a2
+			se->iface->card->name);
8631a2
+    }
8631a2
+
8631a2
+  grub_dhcp6_session_remove_all ();
8631a2
+
8631a2
+  return err;
8631a2
+}
8631a2
+
8631a2
+static grub_command_t cmd_getdhcp, cmd_bootp, cmd_bootp6;
8631a2
 
8631a2
 void
8631a2
 grub_bootp_init (void)
8631a2
@@ -835,6 +1565,9 @@ grub_bootp_init (void)
8631a2
   cmd_getdhcp = grub_register_command ("net_get_dhcp_option", grub_cmd_dhcpopt,
8631a2
 				       N_("VAR INTERFACE NUMBER DESCRIPTION"),
8631a2
 				       N_("retrieve DHCP option and save it into VAR. If VAR is - then print the value."));
8631a2
+  cmd_bootp6 = grub_register_command ("net_bootp6", grub_cmd_bootp6,
8631a2
+				     N_("[CARD]"),
8631a2
+				     N_("perform a DHCPv6 autoconfiguration"));
8631a2
 }
8631a2
 
8631a2
 void
8631a2
@@ -842,4 +1575,5 @@ grub_bootp_fini (void)
8631a2
 {
8631a2
   grub_unregister_command (cmd_getdhcp);
8631a2
   grub_unregister_command (cmd_bootp);
8631a2
+  grub_unregister_command (cmd_bootp6);
8631a2
 }
8631a2
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
8631a2
index cd6dba79f63..6fd0a4f4998 100644
8631a2
--- a/grub-core/net/drivers/efi/efinet.c
8631a2
+++ b/grub-core/net/drivers/efi/efinet.c
8631a2
@@ -393,9 +393,6 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8631a2
     pxe_mode = pxe->mode;
8631a2
     if (pxe_mode->using_ipv6)
8631a2
       {
8631a2
-	grub_net_link_level_address_t hwaddr;
8631a2
-	struct grub_net_network_level_interface *intf;
8631a2
-
8631a2
 	grub_dprintf ("efinet", "using ipv6 and dhcpv6\n");
8631a2
 	grub_dprintf ("efinet", "dhcp_ack_received: %s%s\n",
8631a2
 		      pxe_mode->dhcp_ack_received ? "yes" : "no",
8631a2
@@ -403,15 +400,14 @@ grub_efi_net_config_real (grub_efi_handle_t hnd, char **device,
8631a2
 	if (!pxe_mode->dhcp_ack_received)
8631a2
 	  continue;
8631a2
 
8631a2
-	hwaddr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-	grub_memcpy (hwaddr.mac,
8631a2
-		     card->efi_net->mode->current_address,
8631a2
-		     sizeof (hwaddr.mac));
8631a2
-
8631a2
-	intf = grub_net_configure_by_dhcpv6_ack (card->name, card, 0, &hwaddr,
8631a2
-	      (const struct grub_net_dhcpv6_packet *)&pxe_mode->dhcp_ack.dhcpv6,
8631a2
-	      1, device, path);
8631a2
-	if (intf && device && path)
8631a2
+	grub_net_configure_by_dhcpv6_reply (card->name, card, 0,
8631a2
+					    (struct grub_net_dhcp6_packet *)
8631a2
+					    &pxe_mode->dhcp_ack,
8631a2
+					    sizeof (pxe_mode->dhcp_ack),
8631a2
+					    1, device, path);
8631a2
+	if (grub_errno)
8631a2
+	  grub_print_error ();
8631a2
+	if (device && path)
8631a2
 	  grub_dprintf ("efinet", "device: `%s' path: `%s'\n", *device, *path);
8631a2
       }
8631a2
     else
8631a2
diff --git a/grub-core/net/ip.c b/grub-core/net/ip.c
8631a2
index b2ca74b6eb1..9a4e589aa39 100644
8631a2
--- a/grub-core/net/ip.c
8631a2
+++ b/grub-core/net/ip.c
8631a2
@@ -239,6 +239,45 @@ handle_dgram (struct grub_net_buff *nb,
8631a2
   {
8631a2
     struct udphdr *udph;
8631a2
     udph = (struct udphdr *) nb->data;
8631a2
+
8631a2
+    if (proto == GRUB_NET_IP_UDP && udph->dst == grub_cpu_to_be16_compile_time (DHCP6_CLIENT_PORT))
8631a2
+      {
8631a2
+	if (udph->chksum)
8631a2
+	  {
8631a2
+	    grub_uint16_t chk, expected;
8631a2
+	    chk = udph->chksum;
8631a2
+	    udph->chksum = 0;
8631a2
+	    expected = grub_net_ip_transport_checksum (nb,
8631a2
+						       GRUB_NET_IP_UDP,
8631a2
+						       source,
8631a2
+						       dest);
8631a2
+	    if (expected != chk)
8631a2
+	      {
8631a2
+		grub_dprintf ("net", "Invalid UDP checksum. "
8631a2
+			      "Expected %x, got %x\n",
8631a2
+			      grub_be_to_cpu16 (expected),
8631a2
+			      grub_be_to_cpu16 (chk));
8631a2
+		grub_netbuff_free (nb);
8631a2
+		return GRUB_ERR_NONE;
8631a2
+	      }
8631a2
+	    udph->chksum = chk;
8631a2
+	  }
8631a2
+
8631a2
+	err = grub_netbuff_pull (nb, sizeof (*udph));
8631a2
+	if (err)
8631a2
+	  {
8631a2
+	    grub_netbuff_free (nb);
8631a2
+	    return err;
8631a2
+	  }
8631a2
+
8631a2
+	err = grub_net_process_dhcp6 (nb, card);
8631a2
+	if (err)
8631a2
+	  grub_print_error ();
8631a2
+
8631a2
+	grub_netbuff_free (nb);
8631a2
+	return GRUB_ERR_NONE;
8631a2
+      }
8631a2
+
8631a2
     if (proto == GRUB_NET_IP_UDP && grub_be_to_cpu16 (udph->dst) == 68)
8631a2
       {
8631a2
 	const struct grub_net_bootp_packet *bootp;
8631a2
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
8631a2
index e5b521bd9be..1250d493e25 100644
8631a2
--- a/include/grub/efi/api.h
8631a2
+++ b/include/grub/efi/api.h
8631a2
@@ -1507,7 +1507,7 @@ typedef struct grub_efi_pxe_ip_filter
8631a2
 {
8631a2
   grub_efi_uint8_t filters;
8631a2
   grub_efi_uint8_t ip_count;
8631a2
-  grub_efi_uint8_t reserved;
8631a2
+  grub_efi_uint16_t reserved;
8631a2
   grub_efi_ip_address_t ip_list[GRUB_EFI_PXE_MAX_IPCNT];
8631a2
 } grub_efi_pxe_ip_filter_t;
8631a2
 
8631a2
diff --git a/include/grub/net.h b/include/grub/net.h
8631a2
index f4cd86e582f..5f78b22e109 100644
8631a2
--- a/include/grub/net.h
8631a2
+++ b/include/grub/net.h
8631a2
@@ -447,50 +447,65 @@ struct grub_net_bootp_packet
8631a2
   grub_uint8_t vendor[0];
8631a2
 } GRUB_PACKED;
8631a2
 
8631a2
-enum
8631a2
-  {
8631a2
-    GRUB_NET_DHCP6_IA_NA = 3,
8631a2
-    GRUB_NET_DHCP6_IA_ADDRESS = 5,
8631a2
-    GRUB_NET_DHCP6_BOOTFILE_URL = 59,
8631a2
-  };
8631a2
-
8631a2
-struct grub_net_dhcpv6_option
8631a2
+struct grub_net_dhcp6_packet
8631a2
 {
8631a2
-  grub_uint16_t option_num;
8631a2
-  grub_uint16_t option_len;
8631a2
-  grub_uint8_t option_data[];
8631a2
+  grub_uint32_t message_type:8;
8631a2
+  grub_uint32_t transaction_id:24;
8631a2
+  grub_uint8_t dhcp_options[0];
8631a2
 } GRUB_PACKED;
8631a2
-typedef struct grub_net_dhcpv6_option grub_net_dhcpv6_option_t;
8631a2
 
8631a2
-struct grub_net_dhcpv6_opt_ia_na
8631a2
-{
8631a2
-  grub_uint16_t option_num;
8631a2
-  grub_uint16_t option_len;
8631a2
+struct grub_net_dhcp6_option {
8631a2
+  grub_uint16_t code;
8631a2
+  grub_uint16_t len;
8631a2
+  grub_uint8_t data[0];
8631a2
+} GRUB_PACKED;
8631a2
+
8631a2
+struct grub_net_dhcp6_option_iana {
8631a2
   grub_uint32_t iaid;
8631a2
   grub_uint32_t t1;
8631a2
   grub_uint32_t t2;
8631a2
-  grub_uint8_t options[];
8631a2
+  grub_uint8_t data[0];
8631a2
 } GRUB_PACKED;
8631a2
-typedef struct grub_net_dhcpv6_opt_ia_na grub_net_dhcpv6_opt_ia_na_t;
8631a2
 
8631a2
-struct grub_net_dhcpv6_opt_ia_address
8631a2
-{
8631a2
-  grub_uint16_t option_num;
8631a2
-  grub_uint16_t option_len;
8631a2
-  grub_uint64_t ipv6_address[2];
8631a2
+struct grub_net_dhcp6_option_iaaddr {
8631a2
+  grub_uint8_t addr[16];
8631a2
   grub_uint32_t preferred_lifetime;
8631a2
   grub_uint32_t valid_lifetime;
8631a2
-  grub_uint8_t options[];
8631a2
+  grub_uint8_t data[0];
8631a2
 } GRUB_PACKED;
8631a2
-typedef struct grub_net_dhcpv6_opt_ia_address grub_net_dhcpv6_opt_ia_address_t;
8631a2
 
8631a2
-struct grub_net_dhcpv6_packet
8631a2
+struct grub_net_dhcp6_option_duid_ll
8631a2
 {
8631a2
-  grub_uint32_t message_type:8;
8631a2
-  grub_uint32_t transaction_id:24;
8631a2
-  grub_uint8_t dhcp_options[1024];
8631a2
+  grub_uint16_t type;
8631a2
+  grub_uint16_t hw_type;
8631a2
+  grub_uint8_t hwaddr[6];
8631a2
 } GRUB_PACKED;
8631a2
-typedef struct grub_net_dhcpv6_packet grub_net_dhcpv6_packet_t;
8631a2
+
8631a2
+enum
8631a2
+  {
8631a2
+    GRUB_NET_DHCP6_SOLICIT = 1,
8631a2
+    GRUB_NET_DHCP6_ADVERTISE = 2,
8631a2
+    GRUB_NET_DHCP6_REQUEST = 3,
8631a2
+    GRUB_NET_DHCP6_REPLY = 7
8631a2
+  };
8631a2
+
8631a2
+enum
8631a2
+  {
8631a2
+    DHCP6_CLIENT_PORT = 546,
8631a2
+    DHCP6_SERVER_PORT = 547
8631a2
+  };
8631a2
+
8631a2
+enum
8631a2
+  {
8631a2
+    GRUB_NET_DHCP6_OPTION_CLIENTID = 1,
8631a2
+    GRUB_NET_DHCP6_OPTION_SERVERID = 2,
8631a2
+    GRUB_NET_DHCP6_OPTION_IA_NA = 3,
8631a2
+    GRUB_NET_DHCP6_OPTION_IAADDR = 5,
8631a2
+    GRUB_NET_DHCP6_OPTION_ORO = 6,
8631a2
+    GRUB_NET_DHCP6_OPTION_ELAPSED_TIME = 8,
8631a2
+    GRUB_NET_DHCP6_OPTION_DNS_SERVERS = 23,
8631a2
+    GRUB_NET_DHCP6_OPTION_BOOTFILE_URL = 59
8631a2
+  };
8631a2
 
8631a2
 #define	GRUB_NET_BOOTP_RFC1048_MAGIC_0	0x63
8631a2
 #define	GRUB_NET_BOOTP_RFC1048_MAGIC_1	0x82
8631a2
@@ -521,12 +536,12 @@ grub_net_configure_by_dhcp_ack (const char *name,
8631a2
 				int is_def, char **device, char **path);
8631a2
 
8631a2
 struct grub_net_network_level_interface *
8631a2
-grub_net_configure_by_dhcpv6_ack (const char *name,
8631a2
-				 struct grub_net_card *card,
8631a2
-				 grub_net_interface_flags_t flags,
8631a2
-				 const grub_net_link_level_address_t *hwaddr,
8631a2
-				 const struct grub_net_dhcpv6_packet *packet,
8631a2
-				 int is_def, char **device, char **path);
8631a2
+grub_net_configure_by_dhcpv6_reply (const char *name,
8631a2
+				    struct grub_net_card *card,
8631a2
+				    grub_net_interface_flags_t flags,
8631a2
+				    const struct grub_net_dhcp6_packet *v6,
8631a2
+				    grub_size_t size,
8631a2
+				    int is_def, char **device, char **path);
8631a2
 
8631a2
 int
8631a2
 grub_ipv6_get_masksize(grub_uint16_t *mask);
8631a2
@@ -543,6 +558,10 @@ void
8631a2
 grub_net_process_dhcp (struct grub_net_buff *nb,
8631a2
 		       struct grub_net_card *card);
8631a2
 
8631a2
+grub_err_t
8631a2
+grub_net_process_dhcp6 (struct grub_net_buff *nb,
8631a2
+			struct grub_net_card *card);
8631a2
+
8631a2
 int
8631a2
 grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,
8631a2
 		     const grub_net_link_level_address_t *b);