nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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