nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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