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

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