nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone
8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Andrzej Kacprowski <andrzej.kacprowski@intel.com>
8631a2
Date: Fri, 21 Apr 2017 10:06:20 +0200
8631a2
Subject: [PATCH] Add support for non-Ethernet network cards
8631a2
8631a2
This patch replaces fixed 6-byte link layer address with
8631a2
up to 32-byte variable sized address.
8631a2
This allows supporting Infiniband and Omni-Path fabric
8631a2
which use 20-byte address, but other network card types
8631a2
can also take advantage of this change.
8631a2
The network card driver is responsible for replacing L2
8631a2
header provided by grub2 if needed.
8631a2
This approach is compatible with UEFI network stack which
8631a2
also allows up to 32-byte variable size link address.
8631a2
8631a2
The BOOTP/DHCP packet format is limited to 16 byte client
8631a2
hardware address, if link address is more that 16-bytes
8631a2
then chaddr field in BOOTP it will be set to 0 as per rfc4390.
8631a2
8631a2
Resolves: rhbz#1370642
8631a2
8631a2
Signed-off-by: Andrzej Kacprowski <andrzej.kacprowski@intel.com>
8631a2
8631a2
Conflicts:
8631a2
	grub-core/net/ip.c
8631a2
---
8631a2
 grub-core/net/arp.c                    | 155 ++++++++++++++++++++++-----------
8631a2
 grub-core/net/bootp.c                  |  14 ++-
8631a2
 grub-core/net/drivers/efi/efinet.c     |   8 +-
8631a2
 grub-core/net/drivers/emu/emunet.c     |   1 +
8631a2
 grub-core/net/drivers/i386/pc/pxe.c    |  13 +--
8631a2
 grub-core/net/drivers/ieee1275/ofnet.c |   2 +
8631a2
 grub-core/net/drivers/uboot/ubootnet.c |   1 +
8631a2
 grub-core/net/ethernet.c               |  88 +++++++++----------
8631a2
 grub-core/net/icmp6.c                  |  15 ++--
8631a2
 grub-core/net/ip.c                     |   4 +-
8631a2
 grub-core/net/net.c                    |  48 +++++-----
8631a2
 include/grub/net.h                     |  19 ++--
8631a2
 12 files changed, 216 insertions(+), 152 deletions(-)
8631a2
8631a2
diff --git a/grub-core/net/arp.c b/grub-core/net/arp.c
f6e916
index 54306e3b1..67b409a8a 100644
8631a2
--- a/grub-core/net/arp.c
8631a2
+++ b/grub-core/net/arp.c
8631a2
@@ -31,22 +31,12 @@ enum
8631a2
     ARP_REPLY = 2
8631a2
   };
8631a2
 
8631a2
-enum
8631a2
-  {
8631a2
-    /* IANA ARP constant to define hardware type as ethernet. */
8631a2
-    GRUB_NET_ARPHRD_ETHERNET = 1
8631a2
-  };
8631a2
-
8631a2
-struct arppkt {
8631a2
+struct arphdr {
8631a2
   grub_uint16_t hrd;
8631a2
   grub_uint16_t pro;
8631a2
   grub_uint8_t hln;
8631a2
   grub_uint8_t pln;
8631a2
   grub_uint16_t op;
8631a2
-  grub_uint8_t sender_mac[6];
8631a2
-  grub_uint32_t sender_ip;
8631a2
-  grub_uint8_t recv_mac[6];
8631a2
-  grub_uint32_t recv_ip;
8631a2
 } GRUB_PACKED;
8631a2
 
8631a2
 static int have_pending;
8631a2
@@ -57,12 +47,16 @@ grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
8631a2
 			   const grub_net_network_level_address_t *proto_addr)
8631a2
 {
8631a2
   struct grub_net_buff nb;
8631a2
-  struct arppkt *arp_packet;
8631a2
+  struct arphdr *arp_header;
8631a2
   grub_net_link_level_address_t target_mac_addr;
8631a2
   grub_err_t err;
8631a2
   int i;
8631a2
   grub_uint8_t *nbd;
8631a2
   grub_uint8_t arp_data[128];
8631a2
+  grub_uint8_t hln;
8631a2
+  grub_uint8_t pln;
8631a2
+  grub_uint8_t arp_packet_len;
8631a2
+  grub_uint8_t *tmp_ptr;
8631a2
 
8631a2
   if (proto_addr->type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4)
8631a2
     return grub_error (GRUB_ERR_BUG, "unsupported address family");
8631a2
@@ -73,23 +67,39 @@ grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
8631a2
   grub_netbuff_clear (&nb);
8631a2
   grub_netbuff_reserve (&nb, 128);
8631a2
 
8631a2
-  err = grub_netbuff_push (&nb, sizeof (*arp_packet));
8631a2
+  hln = inf->card->default_address.len;
8631a2
+  pln = sizeof (proto_addr->ipv4);
8631a2
+  arp_packet_len = sizeof (*arp_header) + 2 * (hln + pln);
8631a2
+
8631a2
+  err = grub_netbuff_push (&nb, arp_packet_len);
8631a2
   if (err)
8631a2
     return err;
8631a2
 
8631a2
-  arp_packet = (struct arppkt *) nb.data;
8631a2
-  arp_packet->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
8631a2
-  arp_packet->hln = 6;
8631a2
-  arp_packet->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
8631a2
-  arp_packet->pln = 4;
8631a2
-  arp_packet->op = grub_cpu_to_be16_compile_time (ARP_REQUEST);
8631a2
-  /* Sender hardware address.  */
8631a2
-  grub_memcpy (arp_packet->sender_mac, &inf->hwaddress.mac, 6);
8631a2
-  arp_packet->sender_ip = inf->address.ipv4;
8631a2
-  grub_memset (arp_packet->recv_mac, 0, 6);
8631a2
-  arp_packet->recv_ip = proto_addr->ipv4;
8631a2
-  /* Target protocol address */
8631a2
-  grub_memset (&target_mac_addr.mac, 0xff, 6);
8631a2
+  arp_header = (struct arphdr *) nb.data;
8631a2
+  arp_header->hrd = grub_cpu_to_be16 (inf->card->default_address.type);
8631a2
+  arp_header->hln = hln;
8631a2
+  arp_header->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
8631a2
+  arp_header->pln = pln;
8631a2
+  arp_header->op = grub_cpu_to_be16_compile_time (ARP_REQUEST);
8631a2
+  tmp_ptr = nb.data + sizeof (*arp_header);
8631a2
+
8631a2
+  /* The source hardware address. */
8631a2
+  grub_memcpy (tmp_ptr, inf->hwaddress.mac, hln);
8631a2
+  tmp_ptr += hln;
8631a2
+
8631a2
+  /* The source protocol address. */
8631a2
+  grub_memcpy (tmp_ptr, &inf->address.ipv4, pln);
8631a2
+  tmp_ptr += pln;
8631a2
+
8631a2
+  /* The target hardware address. */
8631a2
+  grub_memset (tmp_ptr, 0, hln);
8631a2
+  tmp_ptr += hln;
8631a2
+
8631a2
+  /* The target protocol address */
8631a2
+  grub_memcpy (tmp_ptr, &proto_addr->ipv4, pln);
8631a2
+  tmp_ptr += pln;
8631a2
+
8631a2
+  grub_memset (&target_mac_addr.mac, 0xff, hln);
8631a2
 
8631a2
   nbd = nb.data;
8631a2
   send_ethernet_packet (inf, &nb, target_mac_addr, GRUB_NET_ETHERTYPE_ARP);
8631a2
@@ -114,28 +124,53 @@ grub_err_t
8631a2
 grub_net_arp_receive (struct grub_net_buff *nb, struct grub_net_card *card,
8631a2
                       grub_uint16_t *vlantag)
8631a2
 {
8631a2
-  struct arppkt *arp_packet = (struct arppkt *) nb->data;
8631a2
+  struct arphdr *arp_header = (struct arphdr *) nb->data;
8631a2
   grub_net_network_level_address_t sender_addr, target_addr;
8631a2
   grub_net_link_level_address_t sender_mac_addr;
8631a2
   struct grub_net_network_level_interface *inf;
8631a2
+  grub_uint16_t hw_type;
8631a2
+  grub_uint8_t hln;
8631a2
+  grub_uint8_t pln;
8631a2
+  grub_uint8_t arp_packet_len;
8631a2
+  grub_uint8_t *tmp_ptr;
8631a2
 
8631a2
-  if (arp_packet->pro != grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP)
8631a2
-      || arp_packet->pln != 4 || arp_packet->hln != 6
8631a2
-      || nb->tail - nb->data < (int) sizeof (*arp_packet))
8631a2
+  hw_type = card->default_address.type;
8631a2
+  hln = card->default_address.len;
8631a2
+  pln = sizeof(sender_addr.ipv4);
8631a2
+  arp_packet_len = sizeof (*arp_header) + 2 * (pln + hln);
8631a2
+
8631a2
+  if (arp_header->pro != grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP)
8631a2
+      || arp_header->hrd != grub_cpu_to_be16 (hw_type)
8631a2
+      || arp_header->hln != hln || arp_header->pln != pln
8631a2
+      || nb->tail - nb->data < (int) arp_packet_len) {
8631a2
     return GRUB_ERR_NONE;
8631a2
+  }
8631a2
 
8631a2
+  tmp_ptr =  nb->data + sizeof (*arp_header);
8631a2
+
8631a2
+  /* The source hardware address. */
8631a2
+  sender_mac_addr.type = hw_type;
8631a2
+  sender_mac_addr.len = hln;
8631a2
+  grub_memcpy (sender_mac_addr.mac, tmp_ptr, hln);
8631a2
+  tmp_ptr += hln;
8631a2
+
8631a2
+  /* The source protocol address. */
8631a2
   sender_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
8631a2
+  grub_memcpy(&sender_addr.ipv4, tmp_ptr, pln);
8631a2
+  tmp_ptr += pln;
8631a2
+
8631a2
+  grub_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1);
8631a2
+
8631a2
+  /* The target hardware address. */
8631a2
+  tmp_ptr += hln;
8631a2
+
8631a2
+  /* The target protocol address. */
8631a2
   target_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
8631a2
-  sender_addr.ipv4 = arp_packet->sender_ip;
8631a2
-  target_addr.ipv4 = arp_packet->recv_ip;
8631a2
-  if (arp_packet->sender_ip == pending_req)
8631a2
+  grub_memcpy(&target_addr.ipv4, tmp_ptr, pln);
8631a2
+
8631a2
+  if (sender_addr.ipv4 == pending_req)
8631a2
     have_pending = 1;
8631a2
 
8631a2
-  sender_mac_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-  grub_memcpy (sender_mac_addr.mac, arp_packet->sender_mac,
8631a2
-	       sizeof (sender_mac_addr.mac));
8631a2
-  grub_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1);
8631a2
-
8631a2
   FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
8631a2
   {
8631a2
     /* Verify vlantag id */
8631a2
@@ -148,11 +183,11 @@ grub_net_arp_receive (struct grub_net_buff *nb, struct grub_net_card *card,
8631a2
 
8631a2
     /* Am I the protocol address target? */
8631a2
     if (grub_net_addr_cmp (&inf->address, &target_addr) == 0
8631a2
-	&& arp_packet->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
8631a2
+	&& arp_header->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
8631a2
       {
8631a2
 	grub_net_link_level_address_t target;
8631a2
 	struct grub_net_buff nb_reply;
8631a2
-	struct arppkt *arp_reply;
8631a2
+	struct arphdr *arp_reply;
8631a2
 	grub_uint8_t arp_data[128];
8631a2
 	grub_err_t err;
8631a2
 
8631a2
@@ -161,25 +196,39 @@ grub_net_arp_receive (struct grub_net_buff *nb, struct grub_net_card *card,
8631a2
 	grub_netbuff_clear (&nb_reply);
8631a2
 	grub_netbuff_reserve (&nb_reply, 128);
8631a2
 
8631a2
-	err = grub_netbuff_push (&nb_reply, sizeof (*arp_packet));
8631a2
+	err = grub_netbuff_push (&nb_reply, arp_packet_len);
8631a2
 	if (err)
8631a2
 	  return err;
8631a2
 
8631a2
-	arp_reply = (struct arppkt *) nb_reply.data;
8631a2
+	arp_reply = (struct arphdr *) nb_reply.data;
8631a2
 
8631a2
-	arp_reply->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
8631a2
+	arp_reply->hrd = grub_cpu_to_be16 (hw_type);
8631a2
 	arp_reply->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
8631a2
-	arp_reply->pln = 4;
8631a2
-	arp_reply->hln = 6;
8631a2
+	arp_reply->pln = pln;
8631a2
+	arp_reply->hln = hln;
8631a2
 	arp_reply->op = grub_cpu_to_be16_compile_time (ARP_REPLY);
8631a2
-	arp_reply->sender_ip = arp_packet->recv_ip;
8631a2
-	arp_reply->recv_ip = arp_packet->sender_ip;
8631a2
-	arp_reply->hln = 6;
8631a2
-
8631a2
-	target.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-	grub_memcpy (target.mac, arp_packet->sender_mac, 6);
8631a2
-	grub_memcpy (arp_reply->sender_mac, inf->hwaddress.mac, 6);
8631a2
-	grub_memcpy (arp_reply->recv_mac, arp_packet->sender_mac, 6);
8631a2
+
8631a2
+	tmp_ptr = nb_reply.data + sizeof (*arp_reply);
8631a2
+
8631a2
+	/* The source hardware address. */
8631a2
+	grub_memcpy (tmp_ptr, inf->hwaddress.mac, hln);
8631a2
+	tmp_ptr += hln;
8631a2
+
8631a2
+	/* The source protocol address. */
8631a2
+	grub_memcpy (tmp_ptr, &target_addr.ipv4, pln);
8631a2
+	tmp_ptr += pln;
8631a2
+
8631a2
+	/* The target hardware address. */
8631a2
+	grub_memcpy (tmp_ptr, sender_mac_addr.mac, hln);
8631a2
+	tmp_ptr += hln;
8631a2
+
8631a2
+	/* The target protocol address */
8631a2
+	grub_memcpy (tmp_ptr, &sender_addr.ipv4, pln);
8631a2
+	tmp_ptr += pln;
8631a2
+
8631a2
+	target.type = hw_type;
8631a2
+	target.len = hln;
8631a2
+	grub_memcpy (target.mac, sender_mac_addr.mac, hln);
8631a2
 
8631a2
 	/* Change operation to REPLY and send packet */
8631a2
 	send_ethernet_packet (inf, &nb_reply, target, GRUB_NET_ETHERTYPE_ARP);
8631a2
diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c
f6e916
index 2869482fe..4e55adc55 100644
8631a2
--- a/grub-core/net/bootp.c
8631a2
+++ b/grub-core/net/bootp.c
8631a2
@@ -219,7 +219,6 @@ grub_net_configure_by_dhcp_ack (const char *name,
8631a2
 				int is_def, char **device, char **path)
8631a2
 {
8631a2
   grub_net_network_level_address_t addr;
8631a2
-  grub_net_link_level_address_t hwaddr;
8631a2
   struct grub_net_network_level_interface *inter;
8631a2
   int mask = -1;
8631a2
   char server_ip[sizeof ("xxx.xxx.xxx.xxx")];
8631a2
@@ -232,12 +231,8 @@ grub_net_configure_by_dhcp_ack (const char *name,
8631a2
   if (path)
8631a2
     *path = 0;
8631a2
 
8631a2
-  grub_memcpy (hwaddr.mac, bp->mac_addr,
8631a2
-	       bp->hw_len < sizeof (hwaddr.mac) ? bp->hw_len
8631a2
-	       : sizeof (hwaddr.mac));
8631a2
-  hwaddr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-
8631a2
-  inter = grub_net_add_addr (name, card, &addr, &hwaddr, flags);
8631a2
+  grub_dprintf("dhcp", "configuring dhcp for %s\n", name);
8631a2
+  inter = grub_net_add_addr (name, card, &addr, &card->default_address, flags);
8631a2
   if (!inter)
8631a2
     return 0;
8631a2
 
8631a2
@@ -770,7 +765,8 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
8631a2
 	  grub_memset (pack, 0, sizeof (*pack) + 64);
8631a2
 	  pack->opcode = 1;
8631a2
 	  pack->hw_type = 1;
8631a2
-	  pack->hw_len = 6;
8631a2
+	  pack->hw_len = ifaces[j].hwaddress.len > 16 ? 0
8631a2
+						      : ifaces[j].hwaddress.len;
8631a2
 	  err = grub_get_datetime (&date);
8631a2
 	  if (err || !grub_datetime2unixtime (&date, &t))
8631a2
 	    {
8631a2
@@ -781,7 +777,7 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
8631a2
 	  ifaces[j].dhcp_xid = pack->xid;
8631a2
 	  pack->seconds = grub_cpu_to_be16 (t);
8631a2
 
8631a2
-	  grub_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, 6); 
8631a2
+	  grub_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, pack->hw_len);
8631a2
 
8631a2
 	  grub_netbuff_push (nb, sizeof (*udph));
8631a2
 
8631a2
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
f6e916
index a4daaa460..cd6dba79f 100644
8631a2
--- a/grub-core/net/drivers/efi/efinet.c
8631a2
+++ b/grub-core/net/drivers/efi/efinet.c
8631a2
@@ -280,6 +280,9 @@ grub_efinet_findcards (void)
8631a2
 	/* This should not happen... Why?  */
8631a2
 	continue;
8631a2
 
8631a2
+      if (net->mode->hwaddr_size > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
8631a2
+	continue;
8631a2
+
8631a2
       if (net->mode->state == GRUB_EFI_NETWORK_STOPPED
8631a2
 	  && efi_call_1 (net->start, net) != GRUB_EFI_SUCCESS)
8631a2
 	continue;
8631a2
@@ -316,10 +319,11 @@ grub_efinet_findcards (void)
8631a2
       card->name = grub_xasprintf ("efinet%d", i++);
8631a2
       card->driver = &efidriver;
8631a2
       card->flags = 0;
8631a2
-      card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
+      card->default_address.type = net->mode->if_type;
8631a2
+      card->default_address.len = net->mode->hwaddr_size;
8631a2
       grub_memcpy (card->default_address.mac,
8631a2
 		   net->mode->current_address,
8631a2
-		   sizeof (card->default_address.mac));
8631a2
+		   net->mode->hwaddr_size);
8631a2
       card->efi_net = net;
8631a2
       card->efi_handle = *handle;
8631a2
 
8631a2
diff --git a/grub-core/net/drivers/emu/emunet.c b/grub-core/net/drivers/emu/emunet.c
f6e916
index b19492086..5b6c5e16a 100644
8631a2
--- a/grub-core/net/drivers/emu/emunet.c
8631a2
+++ b/grub-core/net/drivers/emu/emunet.c
8631a2
@@ -46,6 +46,7 @@ static struct grub_net_card emucard =
8631a2
     .mtu = 1500,
8631a2
     .default_address = {
8631a2
 			 .type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
8631a2
+			 . len = 6,
8631a2
 			 {.mac = {0, 1, 2, 3, 4, 5}}
8631a2
 		       },
8631a2
     .flags = 0
8631a2
diff --git a/grub-core/net/drivers/i386/pc/pxe.c b/grub-core/net/drivers/i386/pc/pxe.c
f6e916
index 3f4152d03..9f8fb4b6d 100644
8631a2
--- a/grub-core/net/drivers/i386/pc/pxe.c
8631a2
+++ b/grub-core/net/drivers/i386/pc/pxe.c
8631a2
@@ -386,20 +386,21 @@ GRUB_MOD_INIT(pxe)
8631a2
   grub_memset (ui, 0, sizeof (*ui));
8631a2
   grub_pxe_call (GRUB_PXENV_UNDI_GET_INFORMATION, ui, pxe_rm_entry);
8631a2
 
8631a2
+  grub_pxe_card.default_address.len = 6;
8631a2
   grub_memcpy (grub_pxe_card.default_address.mac, ui->current_addr,
8631a2
-	       sizeof (grub_pxe_card.default_address.mac));
8631a2
-  for (i = 0; i < sizeof (grub_pxe_card.default_address.mac); i++)
8631a2
+	       grub_pxe_card.default_address.len);
8631a2
+  for (i = 0; i < grub_pxe_card.default_address.len; i++)
8631a2
     if (grub_pxe_card.default_address.mac[i] != 0)
8631a2
       break;
8631a2
-  if (i != sizeof (grub_pxe_card.default_address.mac))
8631a2
+  if (i != grub_pxe_card.default_address.len)
8631a2
     {
8631a2
-      for (i = 0; i < sizeof (grub_pxe_card.default_address.mac); i++)
8631a2
+      for (i = 0; i < grub_pxe_card.default_address.len; i++)
8631a2
 	if (grub_pxe_card.default_address.mac[i] != 0xff)
8631a2
 	  break;
8631a2
     }
8631a2
-  if (i == sizeof (grub_pxe_card.default_address.mac))
8631a2
+  if (i == grub_pxe_card.default_address.len)
8631a2
     grub_memcpy (grub_pxe_card.default_address.mac, ui->permanent_addr,
8631a2
-		 sizeof (grub_pxe_card.default_address.mac));
8631a2
+		 grub_pxe_card.default_address.len);
8631a2
   grub_pxe_card.mtu = ui->mtu;
8631a2
 
8631a2
   grub_pxe_card.default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c
f6e916
index 3df75357a..ba50415f5 100644
8631a2
--- a/grub-core/net/drivers/ieee1275/ofnet.c
8631a2
+++ b/grub-core/net/drivers/ieee1275/ofnet.c
8631a2
@@ -160,6 +160,7 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
8631a2
   grub_uint16_t vlantag = 0;
8631a2
 
8631a2
   hw_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
+  hw_addr.len = 6;
8631a2
 
8631a2
   args = bootpath + grub_strlen (devpath) + 1;
8631a2
   do
8631a2
@@ -503,6 +504,7 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
8631a2
     grub_memcpy (&lla.mac, pprop, 6);
8631a2
 
8631a2
   lla.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
+  lla.len = 6;
8631a2
   card->default_address = lla;
8631a2
 
8631a2
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
8631a2
diff --git a/grub-core/net/drivers/uboot/ubootnet.c b/grub-core/net/drivers/uboot/ubootnet.c
f6e916
index 056052e40..22ebcbf21 100644
8631a2
--- a/grub-core/net/drivers/uboot/ubootnet.c
8631a2
+++ b/grub-core/net/drivers/uboot/ubootnet.c
8631a2
@@ -131,6 +131,7 @@ GRUB_MOD_INIT (ubootnet)
8631a2
 
8631a2
       grub_memcpy (&(card->default_address.mac), &devinfo->di_net.hwaddr, 6);
8631a2
       card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
+      card->default_address.len = 6;
8631a2
 
8631a2
       card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
8631a2
       card->txbuf = grub_zalloc (card->txbufsize);
8631a2
diff --git a/grub-core/net/ethernet.c b/grub-core/net/ethernet.c
f6e916
index 4d7ceed6f..9aae83a5e 100644
8631a2
--- a/grub-core/net/ethernet.c
8631a2
+++ b/grub-core/net/ethernet.c
8631a2
@@ -29,13 +29,6 @@
8631a2
 
8631a2
 #define LLCADDRMASK 0x7f
8631a2
 
8631a2
-struct etherhdr
8631a2
-{
8631a2
-  grub_uint8_t dst[6];
8631a2
-  grub_uint8_t src[6];
8631a2
-  grub_uint16_t type;
8631a2
-} GRUB_PACKED;
8631a2
-
8631a2
 struct llchdr
8631a2
 {
8631a2
   grub_uint8_t dsap;
8631a2
@@ -55,13 +48,15 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
8631a2
 		      grub_net_link_level_address_t target_addr,
8631a2
 		      grub_net_ethertype_t ethertype)
8631a2
 {
8631a2
-  struct etherhdr *eth;
8631a2
+  grub_uint8_t *eth;
8631a2
   grub_err_t err;
8631a2
-  grub_uint8_t etherhdr_size;
8631a2
-  grub_uint16_t vlantag_id = VLANTAG_IDENTIFIER;
8631a2
+  grub_uint32_t vlantag = 0;
8631a2
+  grub_uint8_t hw_addr_len = inf->card->default_address.len;
8631a2
+  grub_uint8_t etherhdr_size = 2 * hw_addr_len + 2;
8631a2
 
8631a2
-  etherhdr_size = sizeof (*eth);
8631a2
-  COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
8631a2
+  /* Source and destination link addresses + ethertype + vlan tag */
8631a2
+  COMPILE_TIME_ASSERT ((GRUB_NET_MAX_LINK_ADDRESS_SIZE * 2 + 2 + 4) <
8631a2
+		       GRUB_NET_MAX_LINK_HEADER_SIZE);
8631a2
 
8631a2
   /* Increase ethernet header in case of vlantag */
8631a2
   if (inf->vlantag != 0)
8631a2
@@ -70,11 +65,22 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
8631a2
   err = grub_netbuff_push (nb, etherhdr_size);
8631a2
   if (err)
8631a2
     return err;
8631a2
-  eth = (struct etherhdr *) nb->data;
8631a2
-  grub_memcpy (eth->dst, target_addr.mac, 6);
8631a2
-  grub_memcpy (eth->src, inf->hwaddress.mac, 6);
8631a2
+  eth = nb->data;
8631a2
+  grub_memcpy (eth, target_addr.mac, hw_addr_len);
8631a2
+  eth += hw_addr_len;
8631a2
+  grub_memcpy (eth, inf->hwaddress.mac, hw_addr_len);
8631a2
+  eth += hw_addr_len;
8631a2
+
8631a2
+  /* Check if a vlan-tag is present. */
8631a2
+  if (vlantag != 0)
8631a2
+    {
8631a2
+      *((grub_uint32_t *)eth) = grub_cpu_to_be32 (vlantag);
8631a2
+      eth += sizeof (vlantag);
8631a2
+    }
8631a2
+
8631a2
+  /* Write ethertype */
8631a2
+  *((grub_uint16_t*) eth) = grub_cpu_to_be16 (ethertype);
8631a2
 
8631a2
-  eth->type = grub_cpu_to_be16 (ethertype);
8631a2
   if (!inf->card->opened)
8631a2
     {
8631a2
       err = GRUB_ERR_NONE;
8631a2
@@ -85,18 +91,6 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
8631a2
       inf->card->opened = 1;
8631a2
     }
8631a2
 
8631a2
-  /* Check and add a vlan-tag if needed. */
8631a2
-  if (inf->vlantag != 0)
8631a2
-    {
8631a2
-      /* Move eth type to the right */
8631a2
-      grub_memcpy ((char *) nb->data + etherhdr_size - 2,
8631a2
-                   (char *) nb->data + etherhdr_size - 6, 2);
8631a2
-
8631a2
-      /* Add the tag in the middle */
8631a2
-      grub_memcpy ((char *) nb->data + etherhdr_size - 6, &vlantag_id, 2);
8631a2
-      grub_memcpy ((char *) nb->data + etherhdr_size - 4, (char *) &(inf->vlantag), 2);
8631a2
-    }
8631a2
-
8631a2
   return inf->card->driver->send (inf->card, nb);
8631a2
 }
8631a2
 
8631a2
@@ -104,31 +98,40 @@ grub_err_t
8631a2
 grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
8631a2
 			       struct grub_net_card *card)
8631a2
 {
8631a2
-  struct etherhdr *eth;
8631a2
+  grub_uint8_t *eth;
8631a2
   struct llchdr *llch;
8631a2
   struct snaphdr *snaph;
8631a2
   grub_net_ethertype_t type;
8631a2
   grub_net_link_level_address_t hwaddress;
8631a2
   grub_net_link_level_address_t src_hwaddress;
8631a2
   grub_err_t err;
8631a2
-  grub_uint8_t etherhdr_size = sizeof (*eth);
8631a2
+  grub_uint8_t hw_addr_len = card->default_address.len;
8631a2
+  grub_uint8_t etherhdr_size = 2 * hw_addr_len + 2;
8631a2
   grub_uint16_t vlantag = 0;
8631a2
 
8631a2
+  eth = nb->data;
8631a2
 
8631a2
-  /* Check if a vlan-tag is present. If so, the ethernet header is 4 bytes */
8631a2
-  /* longer than the original one. The vlantag id is extracted and the header */
8631a2
-  /* is reseted to the original size. */
8631a2
-  if (grub_get_unaligned16 (nb->data + etherhdr_size - 2) == VLANTAG_IDENTIFIER)
8631a2
+  hwaddress.type = card->default_address.type;
8631a2
+  hwaddress.len = hw_addr_len;
8631a2
+  grub_memcpy (hwaddress.mac, eth, hw_addr_len);
8631a2
+  eth += hw_addr_len;
8631a2
+
8631a2
+  src_hwaddress.type = card->default_address.type;
8631a2
+  src_hwaddress.len = hw_addr_len;
8631a2
+  grub_memcpy (src_hwaddress.mac, eth, hw_addr_len);
8631a2
+  eth += hw_addr_len;
8631a2
+
8631a2
+  type = grub_be_to_cpu16 (*(grub_uint16_t*)(eth));
8631a2
+  if (type == VLANTAG_IDENTIFIER)
8631a2
     {
8631a2
-      vlantag = grub_get_unaligned16 (nb->data + etherhdr_size);
8631a2
+      /* Skip vlan tag */
8631a2
+      eth += 2;
8631a2
+      vlantag = grub_be_to_cpu16 (*(grub_uint16_t*)(eth));
8631a2
       etherhdr_size += 4;
8631a2
-      /* Move eth type to the original position */
8631a2
-      grub_memcpy((char *) nb->data + etherhdr_size - 6,
8631a2
-                  (char *) nb->data + etherhdr_size - 2, 2);
8631a2
+      eth += 2;
8631a2
+      type = grub_be_to_cpu16 (*(grub_uint16_t*)(eth));
8631a2
     }
8631a2
 
8631a2
-  eth = (struct etherhdr *) nb->data;
8631a2
-  type = grub_be_to_cpu16 (eth->type);
8631a2
   err = grub_netbuff_pull (nb, etherhdr_size);
8631a2
   if (err)
8631a2
     return err;
8631a2
@@ -148,11 +151,6 @@ grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
8631a2
 	}
8631a2
     }
8631a2
 
8631a2
-  hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-  grub_memcpy (hwaddress.mac, eth->dst, sizeof (hwaddress.mac));
8631a2
-  src_hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-  grub_memcpy (src_hwaddress.mac, eth->src, sizeof (src_hwaddress.mac));
8631a2
-
8631a2
   switch (type)
8631a2
     {
8631a2
       /* ARP packet. */
8631a2
diff --git a/grub-core/net/icmp6.c b/grub-core/net/icmp6.c
f6e916
index 2cbd95dce..56a3ec5c8 100644
8631a2
--- a/grub-core/net/icmp6.c
8631a2
+++ b/grub-core/net/icmp6.c
8631a2
@@ -231,8 +231,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
8631a2
 		&& ohdr->len == 1)
8631a2
 	      {
8631a2
 		grub_net_link_level_address_t ll_address;
8631a2
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
8631a2
+		ll_address.type = card->default_address.type;
8631a2
+		ll_address.len = card->default_address.len;
8631a2
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
8631a2
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
8631a2
 	      }
8631a2
 	  }
8631a2
@@ -335,8 +336,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
8631a2
 		&& ohdr->len == 1)
8631a2
 	      {
8631a2
 		grub_net_link_level_address_t ll_address;
8631a2
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
8631a2
+		ll_address.type = card->default_address.type;
8631a2
+		ll_address.len = card->default_address.len;
8631a2
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
8631a2
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
8631a2
 	      }
8631a2
 	  }
8631a2
@@ -384,8 +386,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
8631a2
 		&& ohdr->len == 1)
8631a2
 	      {
8631a2
 		grub_net_link_level_address_t ll_address;
8631a2
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
8631a2
+		ll_address.type = card->default_address.type;
8631a2
+		ll_address.len = card->default_address.len;
8631a2
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
8631a2
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
8631a2
 	      }
8631a2
 	    if (ohdr->type == OPTION_PREFIX && ohdr->len == 4)
8631a2
diff --git a/grub-core/net/ip.c b/grub-core/net/ip.c
f6e916
index 8411e0ecc..b2ca74b6e 100644
8631a2
--- a/grub-core/net/ip.c
8631a2
+++ b/grub-core/net/ip.c
8631a2
@@ -277,8 +277,8 @@ handle_dgram (struct grub_net_buff *nb,
8631a2
 	      && inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV
8631a2
 	      && inf->dhcp_xid == bootp->xid
8631a2
 	      && inf->hwaddress.type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
8631a2
-	      && grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
8631a2
-			      sizeof (inf->hwaddress.mac)) == 0)
8631a2
+	      && (grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
8631a2
+			       bootp->hw_len) == 0 || bootp->hw_len == 0))
8631a2
 	    {
8631a2
 	      grub_net_process_dhcp (nb, inf->card);
8631a2
 	      grub_netbuff_free (nb);
8631a2
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
f6e916
index fa3e29126..9b8944292 100644
8631a2
--- a/grub-core/net/net.c
8631a2
+++ b/grub-core/net/net.c
8631a2
@@ -128,8 +128,9 @@ grub_net_link_layer_resolve (struct grub_net_network_level_interface *inf,
8631a2
 								   << 48)
8631a2
 	  && proto_addr->ipv6[1] == (grub_be_to_cpu64_compile_time (1))))
8631a2
     {
8631a2
-      hw_addr->type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
-      grub_memset (hw_addr->mac, -1, 6);
8631a2
+      hw_addr->type = inf->card->default_address.type;
8631a2
+      hw_addr->len = inf->card->default_address.len;
8631a2
+      grub_memset (hw_addr->mac, -1, hw_addr->len);
8631a2
       return GRUB_ERR_NONE;
8631a2
     }
8631a2
 
8631a2
@@ -137,6 +138,7 @@ grub_net_link_layer_resolve (struct grub_net_network_level_interface *inf,
8631a2
       && ((grub_be_to_cpu64 (proto_addr->ipv6[0]) >> 56) == 0xff))
8631a2
     {
8631a2
       hw_addr->type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
8631a2
+      hw_addr->len = inf->card->default_address.len;
8631a2
       hw_addr->mac[0] = 0x33;
8631a2
       hw_addr->mac[1] = 0x33;
8631a2
       hw_addr->mac[2] = ((grub_be_to_cpu64 (proto_addr->ipv6[1]) >> 24) & 0xff);
8631a2
@@ -757,23 +759,21 @@ grub_net_addr_to_str (const grub_net_network_level_address_t *target, char *buf)
8631a2
 void
8631a2
 grub_net_hwaddr_to_str (const grub_net_link_level_address_t *addr, char *str)
8631a2
 {
8631a2
-  str[0] = 0;
8631a2
-  switch (addr->type)
8631a2
+  char *ptr;
8631a2
+  unsigned i;
8631a2
+
8631a2
+  if (addr->len > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
8631a2
     {
8631a2
-    case GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET:
8631a2
-      {
8631a2
-	char *ptr;
8631a2
-	unsigned i;
8631a2
-	for (ptr = str, i = 0; i < ARRAY_SIZE (addr->mac); i++)
8631a2
-	  {
8631a2
-	    grub_snprintf (ptr, GRUB_NET_MAX_STR_HWADDR_LEN - (ptr - str),
8631a2
-			   "%02x:", addr->mac[i] & 0xff);
8631a2
-	    ptr += (sizeof ("XX:") - 1);
8631a2
-	  }
8631a2
-      return;
8631a2
-      }
8631a2
+       str[0] = 0;
8631a2
+       grub_printf (_("Unsupported hw address type %d len %d\n"),
8631a2
+		    addr->type, addr->len);
8631a2
+       return;
8631a2
+    }
8631a2
+  for (ptr = str, i = 0; i < addr->len; i++)
8631a2
+    {
8631a2
+      ptr += grub_snprintf (ptr, GRUB_NET_MAX_STR_HWADDR_LEN - (ptr - str),
8631a2
+		     "%02x:", addr->mac[i] & 0xff);
8631a2
     }
8631a2
-  grub_printf (_("Unsupported hw address type %d\n"), addr->type);
8631a2
 }
8631a2
 
8631a2
 int
8631a2
@@ -784,13 +784,17 @@ grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,
8631a2
     return -1;
8631a2
   if (a->type > b->type)
8631a2
     return +1;
8631a2
-  switch (a->type)
8631a2
+  if (a->len < b->len)
8631a2
+    return -1;
8631a2
+  if (a->len > b->len)
8631a2
+    return +1;
8631a2
+  if (a->len > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
8631a2
     {
8631a2
-    case GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET:
8631a2
-      return grub_memcmp (a->mac, b->mac, sizeof (a->mac));
8631a2
+      grub_printf (_("Unsupported hw address type %d len %d\n"),
8631a2
+		   a->type, a->len);
8631a2
+      return + 1;
8631a2
     }
8631a2
-  grub_printf (_("Unsupported hw address type %d\n"), a->type);
8631a2
-  return 1;
8631a2
+  return grub_memcmp (a->mac, b->mac, a->len);
8631a2
 }
8631a2
 
8631a2
 int
8631a2
diff --git a/include/grub/net.h b/include/grub/net.h
f6e916
index de51894cb..e9ebc6a1b 100644
8631a2
--- a/include/grub/net.h
8631a2
+++ b/include/grub/net.h
8631a2
@@ -29,7 +29,8 @@
8631a2
 
8631a2
 enum
8631a2
   {
8631a2
-    GRUB_NET_MAX_LINK_HEADER_SIZE = 64,
8631a2
+    GRUB_NET_MAX_LINK_HEADER_SIZE = 96,
8631a2
+    GRUB_NET_MAX_LINK_ADDRESS_SIZE = 32,
8631a2
     GRUB_NET_UDP_HEADER_SIZE = 8,
8631a2
     GRUB_NET_TCP_HEADER_SIZE = 20,
8631a2
     GRUB_NET_OUR_IPV4_HEADER_SIZE = 20,
8631a2
@@ -42,15 +43,17 @@ enum
8631a2
 
8631a2
 typedef enum grub_link_level_protocol_id 
8631a2
 {
8631a2
-  GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
8631a2
+  /* IANA ARP constant to define hardware type. */
8631a2
+  GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET = 1,
8631a2
 } grub_link_level_protocol_id_t;
8631a2
 
8631a2
 typedef struct grub_net_link_level_address
8631a2
 {
8631a2
   grub_link_level_protocol_id_t type;
8631a2
+  grub_uint8_t len;
8631a2
   union
8631a2
   {
8631a2
-    grub_uint8_t mac[6];
8631a2
+    grub_uint8_t mac[GRUB_NET_MAX_LINK_ADDRESS_SIZE];
8631a2
   };
8631a2
 } grub_net_link_level_address_t;
8631a2
 
8631a2
@@ -555,11 +558,13 @@ grub_net_addr_cmp (const grub_net_network_level_address_t *a,
8631a2
 #define GRUB_NET_MAX_STR_ADDR_LEN sizeof ("XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX")
8631a2
 
8631a2
 /*
8631a2
-  Currently suppoerted adresses:
8631a2
-  ethernet:   XX:XX:XX:XX:XX:XX
8631a2
+  Up to 32 byte hardware address supported, see GRUB_NET_MAX_LINK_ADDRESS_SIZE
8631a2
  */
8631a2
-
8631a2
-#define GRUB_NET_MAX_STR_HWADDR_LEN (sizeof ("XX:XX:XX:XX:XX:XX"))
8631a2
+#define GRUB_NET_MAX_STR_HWADDR_LEN (sizeof (\
8631a2
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
8631a2
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
8631a2
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
8631a2
+	"XX:XX:XX:XX:XX:XX:XX:XX"))
8631a2
 
8631a2
 void
8631a2
 grub_net_addr_to_str (const grub_net_network_level_address_t *target,