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

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