dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0248-Add-support-for-non-Ethernet-network-cards.patch

f725e3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f725e3
From: Andrzej Kacprowski <andrzej.kacprowski@intel.com>
f725e3
Date: Fri, 21 Apr 2017 10:06:20 +0200
f725e3
Subject: [PATCH] Add support for non-Ethernet network cards
f725e3
f725e3
This patch replaces fixed 6-byte link layer address with
f725e3
up to 32-byte variable sized address.
f725e3
This allows supporting Infiniband and Omni-Path fabric
f725e3
which use 20-byte address, but other network card types
f725e3
can also take advantage of this change.
f725e3
The network card driver is responsible for replacing L2
f725e3
header provided by grub2 if needed.
f725e3
This approach is compatible with UEFI network stack which
f725e3
also allows up to 32-byte variable size link address.
f725e3
f725e3
The BOOTP/DHCP packet format is limited to 16 byte client
f725e3
hardware address, if link address is more that 16-bytes
f725e3
then chaddr field in BOOTP it will be set to 0 as per rfc4390.
f725e3
f725e3
Resolves: rhbz#1370642
f725e3
f725e3
Signed-off-by: Andrzej Kacprowski <andrzej.kacprowski@intel.com>
f725e3
f725e3
Conflicts:
f725e3
	grub-core/net/ip.c
f725e3
---
f725e3
 grub-core/net/arp.c                    | 157 +++++++++++++++++++++------------
f725e3
 grub-core/net/bootp.c                  |  14 ++-
f725e3
 grub-core/net/drivers/efi/efinet.c     |   8 +-
f725e3
 grub-core/net/drivers/emu/emunet.c     |   1 +
f725e3
 grub-core/net/drivers/i386/pc/pxe.c    |  13 +--
f725e3
 grub-core/net/drivers/ieee1275/ofnet.c |   2 +
f725e3
 grub-core/net/drivers/uboot/ubootnet.c |   1 +
f725e3
 grub-core/net/ethernet.c               |  83 +++++++++--------
f725e3
 grub-core/net/icmp6.c                  |  15 ++--
f725e3
 grub-core/net/ip.c                     |   4 +-
f725e3
 grub-core/net/net.c                    |  48 +++++-----
f725e3
 include/grub/net.h                     |  19 ++--
f725e3
 12 files changed, 213 insertions(+), 152 deletions(-)
f725e3
f725e3
diff --git a/grub-core/net/arp.c b/grub-core/net/arp.c
f725e3
index 996473e1091..badd2d3e684 100644
f725e3
--- a/grub-core/net/arp.c
f725e3
+++ b/grub-core/net/arp.c
f725e3
@@ -31,22 +31,12 @@ enum
f725e3
     ARP_REPLY = 2
f725e3
   };
f725e3
 
f725e3
-enum
f725e3
-  {
f725e3
-    /* IANA ARP constant to define hardware type as ethernet. */
f725e3
-    GRUB_NET_ARPHRD_ETHERNET = 1
f725e3
-  };
f725e3
-
f725e3
-struct arppkt {
f725e3
+struct arphdr {
f725e3
   grub_uint16_t hrd;
f725e3
   grub_uint16_t pro;
f725e3
   grub_uint8_t hln;
f725e3
   grub_uint8_t pln;
f725e3
   grub_uint16_t op;
f725e3
-  grub_uint8_t sender_mac[6];
f725e3
-  grub_uint32_t sender_ip;
f725e3
-  grub_uint8_t recv_mac[6];
f725e3
-  grub_uint32_t recv_ip;
f725e3
 } GRUB_PACKED;
f725e3
 
f725e3
 static int have_pending;
f725e3
@@ -57,12 +47,16 @@ grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
f725e3
 			   const grub_net_network_level_address_t *proto_addr)
f725e3
 {
f725e3
   struct grub_net_buff nb;
f725e3
-  struct arppkt *arp_packet;
f725e3
+  struct arphdr *arp_header;
f725e3
   grub_net_link_level_address_t target_mac_addr;
f725e3
   grub_err_t err;
f725e3
   int i;
f725e3
   grub_uint8_t *nbd;
f725e3
   grub_uint8_t arp_data[128];
f725e3
+  grub_uint8_t hln;
f725e3
+  grub_uint8_t pln;
f725e3
+  grub_uint8_t arp_packet_len;
f725e3
+  grub_uint8_t *tmp_ptr;
f725e3
 
f725e3
   if (proto_addr->type != GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4)
f725e3
     return grub_error (GRUB_ERR_BUG, "unsupported address family");
f725e3
@@ -73,25 +67,39 @@ grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
f725e3
   grub_netbuff_clear (&nb);
f725e3
   grub_netbuff_reserve (&nb, 128);
f725e3
 
f725e3
-  err = grub_netbuff_push (&nb, sizeof (*arp_packet));
f725e3
+  hln = inf->card->default_address.len;
f725e3
+  pln = sizeof (proto_addr->ipv4);
f725e3
+  arp_packet_len = sizeof (*arp_header) + 2 * (hln + pln);
f725e3
+
f725e3
+  err = grub_netbuff_push (&nb, arp_packet_len);
f725e3
   if (err)
f725e3
     return err;
f725e3
 
f725e3
-  arp_packet = (struct arppkt *) nb.data;
f725e3
-  arp_packet->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
f725e3
-  arp_packet->hln = 6;
f725e3
-  arp_packet->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
f725e3
-  arp_packet->pln = 4;
f725e3
-  arp_packet->op = grub_cpu_to_be16_compile_time (ARP_REQUEST);
f725e3
-  /* Sender hardware address.  */
f725e3
-  grub_memcpy (aux, &inf->hwaddress.mac, 6);
f725e3
-
f725e3
-  grub_memcpy (arp_packet->sender_mac, &inf->hwaddress.mac, 6);
f725e3
-  arp_packet->sender_ip = inf->address.ipv4;
f725e3
-  grub_memset (arp_packet->recv_mac, 0, 6);
f725e3
-  arp_packet->recv_ip = proto_addr->ipv4;
f725e3
-  /* Target protocol address */
f725e3
-  grub_memset (&target_mac_addr.mac, 0xff, 6);
f725e3
+  arp_header = (struct arphdr *) nb.data;
f725e3
+  arp_header->hrd = grub_cpu_to_be16 (inf->card->default_address.type);
f725e3
+  arp_header->hln = hln;
f725e3
+  arp_header->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
f725e3
+  arp_header->pln = pln;
f725e3
+  arp_header->op = grub_cpu_to_be16_compile_time (ARP_REQUEST);
f725e3
+  tmp_ptr = nb.data + sizeof (*arp_header);
f725e3
+
f725e3
+  /* The source hardware address. */
f725e3
+  grub_memcpy (tmp_ptr, inf->hwaddress.mac, hln);
f725e3
+  tmp_ptr += hln;
f725e3
+
f725e3
+  /* The source protocol address. */
f725e3
+  grub_memcpy (tmp_ptr, &inf->address.ipv4, pln);
f725e3
+  tmp_ptr += pln;
f725e3
+
f725e3
+  /* The target hardware address. */
f725e3
+  grub_memset (tmp_ptr, 0, hln);
f725e3
+  tmp_ptr += hln;
f725e3
+
f725e3
+  /* The target protocol address */
f725e3
+  grub_memcpy (tmp_ptr, &proto_addr->ipv4, pln);
f725e3
+  tmp_ptr += pln;
f725e3
+
f725e3
+  grub_memset (&target_mac_addr.mac, 0xff, hln);
f725e3
 
f725e3
   nbd = nb.data;
f725e3
   send_ethernet_packet (inf, &nb, target_mac_addr, GRUB_NET_ETHERTYPE_ARP);
f725e3
@@ -116,37 +124,62 @@ grub_err_t
f725e3
 grub_net_arp_receive (struct grub_net_buff *nb,
f725e3
 		      struct grub_net_card *card)
f725e3
 {
f725e3
-  struct arppkt *arp_packet = (struct arppkt *) nb->data;
f725e3
+  struct arphdr *arp_header = (struct arphdr *) nb->data;
f725e3
   grub_net_network_level_address_t sender_addr, target_addr;
f725e3
   grub_net_link_level_address_t sender_mac_addr;
f725e3
   struct grub_net_network_level_interface *inf;
f725e3
+  grub_uint16_t hw_type;
f725e3
+  grub_uint8_t hln;
f725e3
+  grub_uint8_t pln;
f725e3
+  grub_uint8_t arp_packet_len;
f725e3
+  grub_uint8_t *tmp_ptr;
f725e3
 
f725e3
-  if (arp_packet->pro != grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP)
f725e3
-      || arp_packet->pln != 4 || arp_packet->hln != 6
f725e3
-      || nb->tail - nb->data < (int) sizeof (*arp_packet))
f725e3
+  hw_type = card->default_address.type;
f725e3
+  hln = card->default_address.len;
f725e3
+  pln = sizeof(sender_addr.ipv4);
f725e3
+  arp_packet_len = sizeof (*arp_header) + 2 * (pln + hln);
f725e3
+
f725e3
+  if (arp_header->pro != grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP)
f725e3
+      || arp_header->hrd != grub_cpu_to_be16 (hw_type)
f725e3
+      || arp_header->hln != hln || arp_header->pln != pln
f725e3
+      || nb->tail - nb->data < (int) arp_packet_len) {
f725e3
     return GRUB_ERR_NONE;
f725e3
+  }
f725e3
 
f725e3
+  tmp_ptr =  nb->data + sizeof (*arp_header);
f725e3
+
f725e3
+  /* The source hardware address. */
f725e3
+  sender_mac_addr.type = hw_type;
f725e3
+  sender_mac_addr.len = hln;
f725e3
+  grub_memcpy (sender_mac_addr.mac, tmp_ptr, hln);
f725e3
+  tmp_ptr += hln;
f725e3
+
f725e3
+  /* The source protocol address. */
f725e3
   sender_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
f725e3
+  grub_memcpy(&sender_addr.ipv4, tmp_ptr, pln);
f725e3
+  tmp_ptr += pln;
f725e3
+
f725e3
+  grub_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1);
f725e3
+
f725e3
+  /* The target hardware address. */
f725e3
+  tmp_ptr += hln;
f725e3
+
f725e3
+  /* The target protocol address. */
f725e3
   target_addr.type = GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV4;
f725e3
-  sender_addr.ipv4 = arp_packet->sender_ip;
f725e3
-  target_addr.ipv4 = arp_packet->recv_ip;
f725e3
-  if (arp_packet->sender_ip == pending_req)
f725e3
+  grub_memcpy(&target_addr.ipv4, tmp_ptr, pln);
f725e3
+
f725e3
+  if (sender_addr.ipv4 == pending_req)
f725e3
     have_pending = 1;
f725e3
 
f725e3
-  sender_mac_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-  grub_memcpy (sender_mac_addr.mac, arp_packet->sender_mac,
f725e3
-	       sizeof (sender_mac_addr.mac));
f725e3
-  grub_net_link_layer_add_address (card, &sender_addr, &sender_mac_addr, 1);
f725e3
-
f725e3
   FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
f725e3
   {
f725e3
     /* Am I the protocol address target? */
f725e3
     if (grub_net_addr_cmp (&inf->address, &target_addr) == 0
f725e3
-	&& arp_packet->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
f725e3
+	&& arp_header->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
f725e3
       {
f725e3
 	grub_net_link_level_address_t target;
f725e3
 	struct grub_net_buff nb_reply;
f725e3
-	struct arppkt *arp_reply;
f725e3
+	struct arphdr *arp_reply;
f725e3
 	grub_uint8_t arp_data[128];
f725e3
 	grub_err_t err;
f725e3
 
f725e3
@@ -155,25 +188,39 @@ grub_net_arp_receive (struct grub_net_buff *nb,
f725e3
 	grub_netbuff_clear (&nb_reply);
f725e3
 	grub_netbuff_reserve (&nb_reply, 128);
f725e3
 
f725e3
-	err = grub_netbuff_push (&nb_reply, sizeof (*arp_packet));
f725e3
+	err = grub_netbuff_push (&nb_reply, arp_packet_len);
f725e3
 	if (err)
f725e3
 	  return err;
f725e3
 
f725e3
-	arp_reply = (struct arppkt *) nb_reply.data;
f725e3
+	arp_reply = (struct arphdr *) nb_reply.data;
f725e3
 
f725e3
-	arp_reply->hrd = grub_cpu_to_be16_compile_time (GRUB_NET_ARPHRD_ETHERNET);
f725e3
+	arp_reply->hrd = grub_cpu_to_be16 (hw_type);
f725e3
 	arp_reply->pro = grub_cpu_to_be16_compile_time (GRUB_NET_ETHERTYPE_IP);
f725e3
-	arp_reply->pln = 4;
f725e3
-	arp_reply->hln = 6;
f725e3
+	arp_reply->pln = pln;
f725e3
+	arp_reply->hln = hln;
f725e3
 	arp_reply->op = grub_cpu_to_be16_compile_time (ARP_REPLY);
f725e3
-	arp_reply->sender_ip = arp_packet->recv_ip;
f725e3
-	arp_reply->recv_ip = arp_packet->sender_ip;
f725e3
-	arp_reply->hln = 6;
f725e3
-
f725e3
-	target.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-	grub_memcpy (target.mac, arp_packet->sender_mac, 6);
f725e3
-	grub_memcpy (arp_reply->sender_mac, inf->hwaddress.mac, 6);
f725e3
-	grub_memcpy (arp_reply->recv_mac, arp_packet->sender_mac, 6);
f725e3
+
f725e3
+	tmp_ptr = nb_reply.data + sizeof (*arp_reply);
f725e3
+
f725e3
+	/* The source hardware address. */
f725e3
+	grub_memcpy (tmp_ptr, inf->hwaddress.mac, hln);
f725e3
+	tmp_ptr += hln;
f725e3
+
f725e3
+	/* The source protocol address. */
f725e3
+	grub_memcpy (tmp_ptr, &target_addr.ipv4, pln);
f725e3
+	tmp_ptr += pln;
f725e3
+
f725e3
+	/* The target hardware address. */
f725e3
+	grub_memcpy (tmp_ptr, sender_mac_addr.mac, hln);
f725e3
+	tmp_ptr += hln;
f725e3
+
f725e3
+	/* The target protocol address */
f725e3
+	grub_memcpy (tmp_ptr, &sender_addr.ipv4, pln);
f725e3
+	tmp_ptr += pln;
f725e3
+
f725e3
+	target.type = hw_type;
f725e3
+	target.len = hln;
f725e3
+	grub_memcpy (target.mac, sender_mac_addr.mac, hln);
f725e3
 
f725e3
 	/* Change operation to REPLY and send packet */
f725e3
 	send_ethernet_packet (inf, &nb_reply, target, GRUB_NET_ETHERTYPE_ARP);
f725e3
diff --git a/grub-core/net/bootp.c b/grub-core/net/bootp.c
f725e3
index dfdf7e08d90..0a2d11b8820 100644
f725e3
--- a/grub-core/net/bootp.c
f725e3
+++ b/grub-core/net/bootp.c
f725e3
@@ -219,7 +219,6 @@ grub_net_configure_by_dhcp_ack (const char *name,
f725e3
 				int is_def, char **device, char **path)
f725e3
 {
f725e3
   grub_net_network_level_address_t addr;
f725e3
-  grub_net_link_level_address_t hwaddr;
f725e3
   struct grub_net_network_level_interface *inter;
f725e3
   int mask = -1;
f725e3
 
f725e3
@@ -231,12 +230,8 @@ grub_net_configure_by_dhcp_ack (const char *name,
f725e3
   if (path)
f725e3
     *path = 0;
f725e3
 
f725e3
-  grub_memcpy (hwaddr.mac, bp->mac_addr,
f725e3
-	       bp->hw_len < sizeof (hwaddr.mac) ? bp->hw_len
f725e3
-	       : sizeof (hwaddr.mac));
f725e3
-  hwaddr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-
f725e3
-  inter = grub_net_add_addr (name, card, &addr, &hwaddr, flags);
f725e3
+  grub_dprintf("dhcp", "configuring dhcp for %s\n", name);
f725e3
+  inter = grub_net_add_addr (name, card, &addr, &card->default_address, flags);
f725e3
   if (!inter)
f725e3
     return 0;
f725e3
 
f725e3
@@ -762,7 +757,8 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
f725e3
 	  grub_memset (pack, 0, sizeof (*pack) + 64);
f725e3
 	  pack->opcode = 1;
f725e3
 	  pack->hw_type = 1;
f725e3
-	  pack->hw_len = 6;
f725e3
+	  pack->hw_len = ifaces[j].hwaddress.len > 16 ? 0
f725e3
+						      : ifaces[j].hwaddress.len;
f725e3
 	  err = grub_get_datetime (&date);
f725e3
 	  if (err || !grub_datetime2unixtime (&date, &t))
f725e3
 	    {
f725e3
@@ -773,7 +769,7 @@ grub_cmd_bootp (struct grub_command *cmd __attribute__ ((unused)),
f725e3
 	  ifaces[j].dhcp_xid = pack->xid;
f725e3
 	  pack->seconds = grub_cpu_to_be16 (t);
f725e3
 
f725e3
-	  grub_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, 6); 
f725e3
+	  grub_memcpy (&pack->mac_addr, &ifaces[j].hwaddress.mac, pack->hw_len);
f725e3
 
f725e3
 	  grub_netbuff_push (nb, sizeof (*udph));
f725e3
 
f725e3
diff --git a/grub-core/net/drivers/efi/efinet.c b/grub-core/net/drivers/efi/efinet.c
f725e3
index 69b0fc7a8fe..b9ed13fcad1 100644
f725e3
--- a/grub-core/net/drivers/efi/efinet.c
f725e3
+++ b/grub-core/net/drivers/efi/efinet.c
f725e3
@@ -290,6 +290,9 @@ grub_efinet_findcards (void)
f725e3
 	/* This should not happen... Why?  */
f725e3
 	continue;
f725e3
 
f725e3
+      if (net->mode->hwaddr_size > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
f725e3
+	continue;
f725e3
+
f725e3
       if (net->mode->state == GRUB_EFI_NETWORK_STOPPED
f725e3
 	  && efi_call_1 (net->start, net) != GRUB_EFI_SUCCESS)
f725e3
 	continue;
f725e3
@@ -326,10 +329,11 @@ grub_efinet_findcards (void)
f725e3
       card->name = grub_xasprintf ("efinet%d", i++);
f725e3
       card->driver = &efidriver;
f725e3
       card->flags = 0;
f725e3
-      card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
+      card->default_address.type = net->mode->if_type;
f725e3
+      card->default_address.len = net->mode->hwaddr_size;
f725e3
       grub_memcpy (card->default_address.mac,
f725e3
 		   net->mode->current_address,
f725e3
-		   sizeof (card->default_address.mac));
f725e3
+		   net->mode->hwaddr_size);
f725e3
       card->efi_net = net;
f725e3
       card->efi_handle = *handle;
f725e3
 
f725e3
diff --git a/grub-core/net/drivers/emu/emunet.c b/grub-core/net/drivers/emu/emunet.c
f725e3
index 7c977cd52ca..962b0c86d2b 100644
f725e3
--- a/grub-core/net/drivers/emu/emunet.c
f725e3
+++ b/grub-core/net/drivers/emu/emunet.c
f725e3
@@ -46,6 +46,7 @@ static struct grub_net_card emucard =
f725e3
     .mtu = 1500,
f725e3
     .default_address = {
f725e3
 			 .type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET,
f725e3
+			 . len = 6,
f725e3
 			 {.mac = {0, 1, 2, 3, 4, 5}}
f725e3
 		       },
f725e3
     .flags = 0
f725e3
diff --git a/grub-core/net/drivers/i386/pc/pxe.c b/grub-core/net/drivers/i386/pc/pxe.c
f725e3
index e8c0b22e294..5660149503d 100644
f725e3
--- a/grub-core/net/drivers/i386/pc/pxe.c
f725e3
+++ b/grub-core/net/drivers/i386/pc/pxe.c
f725e3
@@ -387,20 +387,21 @@ GRUB_MOD_INIT(pxe)
f725e3
   grub_memset (ui, 0, sizeof (*ui));
f725e3
   grub_pxe_call (GRUB_PXENV_UNDI_GET_INFORMATION, ui, pxe_rm_entry);
f725e3
 
f725e3
+  grub_pxe_card.default_address.len = 6;
f725e3
   grub_memcpy (grub_pxe_card.default_address.mac, ui->current_addr,
f725e3
-	       sizeof (grub_pxe_card.default_address.mac));
f725e3
-  for (i = 0; i < sizeof (grub_pxe_card.default_address.mac); i++)
f725e3
+	       grub_pxe_card.default_address.len);
f725e3
+  for (i = 0; i < grub_pxe_card.default_address.len; i++)
f725e3
     if (grub_pxe_card.default_address.mac[i] != 0)
f725e3
       break;
f725e3
-  if (i != sizeof (grub_pxe_card.default_address.mac))
f725e3
+  if (i != grub_pxe_card.default_address.len)
f725e3
     {
f725e3
-      for (i = 0; i < sizeof (grub_pxe_card.default_address.mac); i++)
f725e3
+      for (i = 0; i < grub_pxe_card.default_address.len; i++)
f725e3
 	if (grub_pxe_card.default_address.mac[i] != 0xff)
f725e3
 	  break;
f725e3
     }
f725e3
-  if (i == sizeof (grub_pxe_card.default_address.mac))
f725e3
+  if (i == grub_pxe_card.default_address.len)
f725e3
     grub_memcpy (grub_pxe_card.default_address.mac, ui->permanent_addr,
f725e3
-		 sizeof (grub_pxe_card.default_address.mac));
f725e3
+		 grub_pxe_card.default_address.len);
f725e3
   grub_pxe_card.mtu = ui->mtu;
f725e3
 
f725e3
   grub_pxe_card.default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c
f725e3
index cd24ddc99c5..5a199bcaa75 100644
f725e3
--- a/grub-core/net/drivers/ieee1275/ofnet.c
f725e3
+++ b/grub-core/net/drivers/ieee1275/ofnet.c
f725e3
@@ -154,6 +154,7 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
f725e3
   struct grub_net_network_level_interface *inter;
f725e3
 
f725e3
   hw_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
+  hw_addr.len = 6;
f725e3
 
f725e3
   args = bootpath + grub_strlen (devpath) + 1;
f725e3
   do
f725e3
@@ -369,6 +370,7 @@ search_net_devices (struct grub_ieee1275_devalias *alias)
f725e3
     grub_memcpy (&lla.mac, pprop, 6);
f725e3
 
f725e3
   lla.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
+  lla.len = 6;
f725e3
   card->default_address = lla;
f725e3
 
f725e3
   card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
f725e3
diff --git a/grub-core/net/drivers/uboot/ubootnet.c b/grub-core/net/drivers/uboot/ubootnet.c
f725e3
index 056052e40d5..22ebcbf211e 100644
f725e3
--- a/grub-core/net/drivers/uboot/ubootnet.c
f725e3
+++ b/grub-core/net/drivers/uboot/ubootnet.c
f725e3
@@ -131,6 +131,7 @@ GRUB_MOD_INIT (ubootnet)
f725e3
 
f725e3
       grub_memcpy (&(card->default_address.mac), &devinfo->di_net.hwaddr, 6);
f725e3
       card->default_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
+      card->default_address.len = 6;
f725e3
 
f725e3
       card->txbufsize = ALIGN_UP (card->mtu, 64) + 256;
f725e3
       card->txbuf = grub_zalloc (card->txbufsize);
f725e3
diff --git a/grub-core/net/ethernet.c b/grub-core/net/ethernet.c
f725e3
index faaca67c50e..1b479d3cadc 100644
f725e3
--- a/grub-core/net/ethernet.c
f725e3
+++ b/grub-core/net/ethernet.c
f725e3
@@ -29,13 +29,6 @@
f725e3
 
f725e3
 #define LLCADDRMASK 0x7f
f725e3
 
f725e3
-struct etherhdr
f725e3
-{
f725e3
-  grub_uint8_t dst[6];
f725e3
-  grub_uint8_t src[6];
f725e3
-  grub_uint16_t type;
f725e3
-} GRUB_PACKED;
f725e3
-
f725e3
 struct llchdr
f725e3
 {
f725e3
   grub_uint8_t dsap;
f725e3
@@ -55,13 +48,15 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
f725e3
 		      grub_net_link_level_address_t target_addr,
f725e3
 		      grub_net_ethertype_t ethertype)
f725e3
 {
f725e3
-  struct etherhdr *eth;
f725e3
+  grub_uint8_t *eth;
f725e3
   grub_err_t err;
f725e3
   grub_uint32_t vlantag = 0;
f725e3
-  grub_uint8_t etherhdr_size;
f725e3
+  grub_uint8_t hw_addr_len = inf->card->default_address.len;
f725e3
+  grub_uint8_t etherhdr_size = 2 * hw_addr_len + 2;
f725e3
 
f725e3
-  etherhdr_size = sizeof (*eth);
f725e3
-  COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
f725e3
+  /* Source and destination link addresses + ethertype + vlan tag */
f725e3
+  COMPILE_TIME_ASSERT ((GRUB_NET_MAX_LINK_ADDRESS_SIZE * 2 + 2 + 4) <
f725e3
+		       GRUB_NET_MAX_LINK_HEADER_SIZE);
f725e3
 
f725e3
   const char *vlantag_text = grub_env_get ("vlan-tag");
f725e3
   if (vlantag_text != 0) {
f725e3
@@ -72,11 +67,22 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
f725e3
   err = grub_netbuff_push (nb, etherhdr_size);
f725e3
   if (err)
f725e3
     return err;
f725e3
-  eth = (struct etherhdr *) nb->data;
f725e3
-  grub_memcpy (eth->dst, target_addr.mac, 6);
f725e3
-  grub_memcpy (eth->src, inf->hwaddress.mac, 6);
f725e3
+  eth = nb->data;
f725e3
+  grub_memcpy (eth, target_addr.mac, hw_addr_len);
f725e3
+  eth += hw_addr_len;
f725e3
+  grub_memcpy (eth, inf->hwaddress.mac, hw_addr_len);
f725e3
+  eth += hw_addr_len;
f725e3
+
f725e3
+  /* Check if a vlan-tag is present. */
f725e3
+  if (vlantag != 0)
f725e3
+    {
f725e3
+      *((grub_uint32_t *)eth) = grub_cpu_to_be32 (vlantag);
f725e3
+      eth += sizeof (vlantag);
f725e3
+    }
f725e3
+
f725e3
+  /* Write ethertype */
f725e3
+  *((grub_uint16_t*) eth) = grub_cpu_to_be16 (ethertype);
f725e3
 
f725e3
-  eth->type = grub_cpu_to_be16 (ethertype);
f725e3
   if (!inf->card->opened)
f725e3
     {
f725e3
       err = GRUB_ERR_NONE;
f725e3
@@ -87,18 +93,6 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
f725e3
       inf->card->opened = 1;
f725e3
     }
f725e3
 
f725e3
-  /* Check if a vlan-tag is needed. */
f725e3
-  if (vlantag != 0)
f725e3
-    {
f725e3
-      /* Move eth type to the right */
f725e3
-      grub_memcpy((char *) nb->data + etherhdr_size - 2,
f725e3
-                  (char *) nb->data + etherhdr_size - 6, 2);
f725e3
-
f725e3
-      /* Add the tag in the middle */
f725e3
-      grub_memcpy((char *) nb->data + etherhdr_size - 6,
f725e3
-                  &vlantag, 4);
f725e3
-    }
f725e3
-
f725e3
   return inf->card->driver->send (inf->card, nb);
f725e3
 }
f725e3
 
f725e3
@@ -106,29 +100,37 @@ grub_err_t
f725e3
 grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
f725e3
 			       struct grub_net_card *card)
f725e3
 {
f725e3
-  struct etherhdr *eth;
f725e3
+  grub_uint8_t *eth;
f725e3
   struct llchdr *llch;
f725e3
   struct snaphdr *snaph;
f725e3
   grub_net_ethertype_t type;
f725e3
   grub_net_link_level_address_t hwaddress;
f725e3
   grub_net_link_level_address_t src_hwaddress;
f725e3
   grub_err_t err;
f725e3
-  grub_uint8_t etherhdr_size = sizeof (*eth);
f725e3
+  grub_uint8_t hw_addr_len = card->default_address.len;
f725e3
+  grub_uint8_t etherhdr_size = 2 * hw_addr_len + 2;
f725e3
 
f725e3
-  grub_uint16_t vlantag_identifier = 0;
f725e3
-  grub_memcpy (&vlantag_identifier, nb->data + etherhdr_size - 2, 2);
f725e3
+  eth = nb->data;
f725e3
 
f725e3
-  /* Check if a vlan-tag is present. */
f725e3
-  if (vlantag_identifier == VLANTAG_IDENTIFIER)
f725e3
+  hwaddress.type = card->default_address.type;
f725e3
+  hwaddress.len = hw_addr_len;
f725e3
+  grub_memcpy (hwaddress.mac, eth, hw_addr_len);
f725e3
+  eth += hw_addr_len;
f725e3
+
f725e3
+  src_hwaddress.type = card->default_address.type;
f725e3
+  src_hwaddress.len = hw_addr_len;
f725e3
+  grub_memcpy (src_hwaddress.mac, eth, hw_addr_len);
f725e3
+  eth += hw_addr_len;
f725e3
+
f725e3
+  type = grub_be_to_cpu16 (*(grub_uint16_t*)(eth));
f725e3
+  if (type == VLANTAG_IDENTIFIER)
f725e3
     {
f725e3
+      /* Skip vlan tag */
f725e3
       etherhdr_size += 4;
f725e3
-      /* Move eth type to the original position */
f725e3
-      grub_memcpy((char *) nb->data + etherhdr_size - 6,
f725e3
-                  (char *) nb->data + etherhdr_size - 2, 2);
f725e3
+      eth += 4;
f725e3
+      type = grub_be_to_cpu16 (*(grub_uint16_t*)(eth));
f725e3
     }
f725e3
 
f725e3
-  eth = (struct etherhdr *) nb->data;
f725e3
-  type = grub_be_to_cpu16 (eth->type);
f725e3
   err = grub_netbuff_pull (nb, etherhdr_size);
f725e3
   if (err)
f725e3
     return err;
f725e3
@@ -148,11 +150,6 @@ grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
f725e3
 	}
f725e3
     }
f725e3
 
f725e3
-  hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-  grub_memcpy (hwaddress.mac, eth->dst, sizeof (hwaddress.mac));
f725e3
-  src_hwaddress.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-  grub_memcpy (src_hwaddress.mac, eth->src, sizeof (src_hwaddress.mac));
f725e3
-
f725e3
   switch (type)
f725e3
     {
f725e3
       /* ARP packet. */
f725e3
diff --git a/grub-core/net/icmp6.c b/grub-core/net/icmp6.c
f725e3
index 7953e68ecfa..7e7a6bcbd68 100644
f725e3
--- a/grub-core/net/icmp6.c
f725e3
+++ b/grub-core/net/icmp6.c
f725e3
@@ -230,8 +230,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
f725e3
 		&& ohdr->len == 1)
f725e3
 	      {
f725e3
 		grub_net_link_level_address_t ll_address;
f725e3
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
f725e3
+		ll_address.type = card->default_address.type;
f725e3
+		ll_address.len = card->default_address.len;
f725e3
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
f725e3
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
f725e3
 	      }
f725e3
 	  }
f725e3
@@ -334,8 +335,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
f725e3
 		&& ohdr->len == 1)
f725e3
 	      {
f725e3
 		grub_net_link_level_address_t ll_address;
f725e3
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
f725e3
+		ll_address.type = card->default_address.type;
f725e3
+		ll_address.len = card->default_address.len;
f725e3
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
f725e3
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
f725e3
 	      }
f725e3
 	  }
f725e3
@@ -366,8 +368,9 @@ grub_net_recv_icmp6_packet (struct grub_net_buff *nb,
f725e3
 		&& ohdr->len == 1)
f725e3
 	      {
f725e3
 		grub_net_link_level_address_t ll_address;
f725e3
-		ll_address.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-		grub_memcpy (ll_address.mac, ohdr + 1, sizeof (ll_address.mac));
f725e3
+		ll_address.type = card->default_address.type;
f725e3
+		ll_address.len = card->default_address.len;
f725e3
+		grub_memcpy (ll_address.mac, ohdr + 1, ll_address.len);
f725e3
 		grub_net_link_layer_add_address (card, source, &ll_address, 0);
f725e3
 	      }
f725e3
 	    if (ohdr->type == OPTION_PREFIX && ohdr->len == 4)
f725e3
diff --git a/grub-core/net/ip.c b/grub-core/net/ip.c
f725e3
index 9e7930caa03..311213feec1 100644
f725e3
--- a/grub-core/net/ip.c
f725e3
+++ b/grub-core/net/ip.c
f725e3
@@ -273,8 +273,8 @@ handle_dgram (struct grub_net_buff *nb,
f725e3
 	      && inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_DHCP_RECV
f725e3
 	      && inf->dhcp_xid == bootp->xid
f725e3
 	      && inf->hwaddress.type == GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
f725e3
-	      && grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
f725e3
-			      sizeof (inf->hwaddress.mac)) == 0)
f725e3
+	      && (grub_memcmp (inf->hwaddress.mac, &bootp->mac_addr,
f725e3
+			       bootp->hw_len) == 0 || bootp->hw_len == 0))
f725e3
 	    {
f725e3
 	      grub_net_process_dhcp (nb, inf->card);
f725e3
 	      grub_netbuff_free (nb);
f725e3
diff --git a/grub-core/net/net.c b/grub-core/net/net.c
f725e3
index fc6f714bf7f..b3a80ba2954 100644
f725e3
--- a/grub-core/net/net.c
f725e3
+++ b/grub-core/net/net.c
f725e3
@@ -143,8 +143,9 @@ grub_net_link_layer_resolve (struct grub_net_network_level_interface *inf,
f725e3
 								   << 48)
f725e3
 	  && proto_addr->ipv6[1] == (grub_be_to_cpu64_compile_time (1))))
f725e3
     {
f725e3
-      hw_addr->type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
-      grub_memset (hw_addr->mac, -1, 6);
f725e3
+      hw_addr->type = inf->card->default_address.type;
f725e3
+      hw_addr->len = inf->card->default_address.len;
f725e3
+      grub_memset (hw_addr->mac, -1, hw_addr->len);
f725e3
       return GRUB_ERR_NONE;
f725e3
     }
f725e3
 
f725e3
@@ -152,6 +153,7 @@ grub_net_link_layer_resolve (struct grub_net_network_level_interface *inf,
f725e3
       && ((grub_be_to_cpu64 (proto_addr->ipv6[0]) >> 56) == 0xff))
f725e3
     {
f725e3
       hw_addr->type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
f725e3
+      hw_addr->len = inf->card->default_address.len;
f725e3
       hw_addr->mac[0] = 0x33;
f725e3
       hw_addr->mac[1] = 0x33;
f725e3
       hw_addr->mac[2] = ((grub_be_to_cpu64 (proto_addr->ipv6[1]) >> 24) & 0xff);
f725e3
@@ -771,23 +773,21 @@ grub_net_addr_to_str (const grub_net_network_level_address_t *target, char *buf)
f725e3
 void
f725e3
 grub_net_hwaddr_to_str (const grub_net_link_level_address_t *addr, char *str)
f725e3
 {
f725e3
-  str[0] = 0;
f725e3
-  switch (addr->type)
f725e3
+  char *ptr;
f725e3
+  unsigned i;
f725e3
+
f725e3
+  if (addr->len > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
f725e3
     {
f725e3
-    case GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET:
f725e3
-      {
f725e3
-	char *ptr;
f725e3
-	unsigned i;
f725e3
-	for (ptr = str, i = 0; i < ARRAY_SIZE (addr->mac); i++)
f725e3
-	  {
f725e3
-	    grub_snprintf (ptr, GRUB_NET_MAX_STR_HWADDR_LEN - (ptr - str),
f725e3
-			   "%02x:", addr->mac[i] & 0xff);
f725e3
-	    ptr += (sizeof ("XX:") - 1);
f725e3
-	  }
f725e3
-      return;
f725e3
-      }
f725e3
+       str[0] = 0;
f725e3
+       grub_printf (_("Unsupported hw address type %d len %d\n"),
f725e3
+		    addr->type, addr->len);
f725e3
+       return;
f725e3
+    }
f725e3
+  for (ptr = str, i = 0; i < addr->len; i++)
f725e3
+    {
f725e3
+      ptr += grub_snprintf (ptr, GRUB_NET_MAX_STR_HWADDR_LEN - (ptr - str),
f725e3
+		     "%02x:", addr->mac[i] & 0xff);
f725e3
     }
f725e3
-  grub_printf (_("Unsupported hw address type %d\n"), addr->type);
f725e3
 }
f725e3
 
f725e3
 int
f725e3
@@ -798,13 +798,17 @@ grub_net_hwaddr_cmp (const grub_net_link_level_address_t *a,
f725e3
     return -1;
f725e3
   if (a->type > b->type)
f725e3
     return +1;
f725e3
-  switch (a->type)
f725e3
+  if (a->len < b->len)
f725e3
+    return -1;
f725e3
+  if (a->len > b->len)
f725e3
+    return +1;
f725e3
+  if (a->len > GRUB_NET_MAX_LINK_ADDRESS_SIZE)
f725e3
     {
f725e3
-    case GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET:
f725e3
-      return grub_memcmp (a->mac, b->mac, sizeof (a->mac));
f725e3
+      grub_printf (_("Unsupported hw address type %d len %d\n"),
f725e3
+		   a->type, a->len);
f725e3
+      return + 1;
f725e3
     }
f725e3
-  grub_printf (_("Unsupported hw address type %d\n"), a->type);
f725e3
-  return 1;
f725e3
+  return grub_memcmp (a->mac, b->mac, a->len);
f725e3
 }
f725e3
 
f725e3
 int
f725e3
diff --git a/include/grub/net.h b/include/grub/net.h
f725e3
index b1bc23048f1..0d9213d6759 100644
f725e3
--- a/include/grub/net.h
f725e3
+++ b/include/grub/net.h
f725e3
@@ -29,7 +29,8 @@
f725e3
 
f725e3
 enum
f725e3
   {
f725e3
-    GRUB_NET_MAX_LINK_HEADER_SIZE = 64,
f725e3
+    GRUB_NET_MAX_LINK_HEADER_SIZE = 96,
f725e3
+    GRUB_NET_MAX_LINK_ADDRESS_SIZE = 32,
f725e3
     GRUB_NET_UDP_HEADER_SIZE = 8,
f725e3
     GRUB_NET_TCP_HEADER_SIZE = 20,
f725e3
     GRUB_NET_OUR_IPV4_HEADER_SIZE = 20,
f725e3
@@ -42,15 +43,17 @@ enum
f725e3
 
f725e3
 typedef enum grub_link_level_protocol_id 
f725e3
 {
f725e3
-  GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET
f725e3
+  /* IANA ARP constant to define hardware type. */
f725e3
+  GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET = 1,
f725e3
 } grub_link_level_protocol_id_t;
f725e3
 
f725e3
 typedef struct grub_net_link_level_address
f725e3
 {
f725e3
   grub_link_level_protocol_id_t type;
f725e3
+  grub_uint8_t len;
f725e3
   union
f725e3
   {
f725e3
-    grub_uint8_t mac[6];
f725e3
+    grub_uint8_t mac[GRUB_NET_MAX_LINK_ADDRESS_SIZE];
f725e3
   };
f725e3
 } grub_net_link_level_address_t;
f725e3
 
f725e3
@@ -531,11 +534,13 @@ grub_net_addr_cmp (const grub_net_network_level_address_t *a,
f725e3
 #define GRUB_NET_MAX_STR_ADDR_LEN sizeof ("XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX")
f725e3
 
f725e3
 /*
f725e3
-  Currently suppoerted adresses:
f725e3
-  ethernet:   XX:XX:XX:XX:XX:XX
f725e3
+  Up to 32 byte hardware address supported, see GRUB_NET_MAX_LINK_ADDRESS_SIZE
f725e3
  */
f725e3
-
f725e3
-#define GRUB_NET_MAX_STR_HWADDR_LEN (sizeof ("XX:XX:XX:XX:XX:XX"))
f725e3
+#define GRUB_NET_MAX_STR_HWADDR_LEN (sizeof (\
f725e3
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
f725e3
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
f725e3
+	"XX:XX:XX:XX:XX:XX:XX:XX:"\
f725e3
+	"XX:XX:XX:XX:XX:XX:XX:XX"))
f725e3
 
f725e3
 void
f725e3
 grub_net_addr_to_str (const grub_net_network_level_address_t *target,