28f7f8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
606ea6
From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
606ea6
Date: Tue, 30 Oct 2012 15:19:39 -0200
28f7f8
Subject: [PATCH] Add vlan-tag support
606ea6
606ea6
This patch adds support for virtual LAN (VLAN) tagging. VLAN tagging allows
606ea6
multiple VLANs in a bridged network to share the same physical network link but
606ea6
maintain isolation:
606ea6
606ea6
http://en.wikipedia.org/wiki/IEEE_802.1Q
606ea6
606ea6
This patch should fix this bugzilla:
606ea6
https://bugzilla.redhat.com/show_bug.cgi?id=871563
606ea6
---
606ea6
 grub-core/kern/ieee1275/init.c   |  1 +
606ea6
 grub-core/kern/ieee1275/openfw.c | 30 ++++++++++++++++++++++++++++
606ea6
 grub-core/net/ethernet.c         | 42 +++++++++++++++++++++++++++++++++++++---
606ea6
 include/grub/ieee1275/ieee1275.h |  1 +
606ea6
 include/grub/net.h               |  2 ++
606ea6
 5 files changed, 73 insertions(+), 3 deletions(-)
606ea6
606ea6
diff --git a/grub-core/kern/ieee1275/init.c b/grub-core/kern/ieee1275/init.c
28f7f8
index d5bd74d3552..8191f8cd658 100644
606ea6
--- a/grub-core/kern/ieee1275/init.c
606ea6
+++ b/grub-core/kern/ieee1275/init.c
39700a
@@ -117,6 +117,7 @@ grub_machine_get_bootlocation (char **device, char **path)
606ea6
       char *dev, *canon;
606ea6
       char *ptr;
606ea6
       dev = grub_ieee1275_get_aliasdevname (bootpath);
606ea6
+      grub_ieee1275_parse_net_options (bootpath);
606ea6
       canon = grub_ieee1275_canonicalise_devname (dev);
606ea6
       ptr = canon + grub_strlen (canon) - 1;
606ea6
       while (ptr > canon && (*ptr == ',' || *ptr == ':'))
606ea6
diff --git a/grub-core/kern/ieee1275/openfw.c b/grub-core/kern/ieee1275/openfw.c
28f7f8
index 6db8b986551..81276fae7b7 100644
606ea6
--- a/grub-core/kern/ieee1275/openfw.c
606ea6
+++ b/grub-core/kern/ieee1275/openfw.c
606ea6
@@ -23,6 +23,7 @@
606ea6
 #include <grub/mm.h>
606ea6
 #include <grub/ieee1275/ieee1275.h>
606ea6
 #include <grub/net.h>
606ea6
+#include <grub/env.h>
606ea6
 
606ea6
 enum grub_ieee1275_parse_type
606ea6
 {
606ea6
@@ -451,6 +452,35 @@ fail:
606ea6
   return ret;
606ea6
 }
606ea6
 
606ea6
+int
606ea6
+grub_ieee1275_parse_net_options (const char *path)
606ea6
+{
606ea6
+  char *comma;
606ea6
+  char *args;
606ea6
+  char *option = 0;
606ea6
+
606ea6
+  args = grub_ieee1275_get_devargs (path);
606ea6
+  if (!args)
606ea6
+    /* There is no option.  */
606ea6
+    return -1;
606ea6
+
606ea6
+  do
606ea6
+    {
606ea6
+      comma = grub_strchr (args, ',');
606ea6
+      if (! comma)
606ea6
+        option = grub_strdup (args);
606ea6
+      else
606ea6
+        option = grub_strndup (args, (grub_size_t)(comma - args));
606ea6
+      args = comma + 1;
606ea6
+
606ea6
+      if (! grub_strncmp(option, "vtag", 4))
606ea6
+          grub_env_set ("vlan-tag", option + grub_strlen("vtag="));
606ea6
+
606ea6
+    } while (comma);
606ea6
+
606ea6
+  return 0;
606ea6
+}
606ea6
+
606ea6
 char *
606ea6
 grub_ieee1275_get_device_type (const char *path)
606ea6
 {
606ea6
diff --git a/grub-core/net/ethernet.c b/grub-core/net/ethernet.c
28f7f8
index c397b1b348c..faaca67c50e 100644
606ea6
--- a/grub-core/net/ethernet.c
606ea6
+++ b/grub-core/net/ethernet.c
606ea6
@@ -23,6 +23,7 @@
606ea6
 #include <grub/net/arp.h>
606ea6
 #include <grub/net/netbuff.h>
606ea6
 #include <grub/net.h>
606ea6
+#include <grub/env.h>
606ea6
 #include <grub/time.h>
606ea6
 #include <grub/net/arp.h>
606ea6
 
606ea6
@@ -56,10 +57,19 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
606ea6
 {
606ea6
   struct etherhdr *eth;
606ea6
   grub_err_t err;
606ea6
+  grub_uint32_t vlantag = 0;
606ea6
+  grub_uint8_t etherhdr_size;
606ea6
 
606ea6
-  COMPILE_TIME_ASSERT (sizeof (*eth) < GRUB_NET_MAX_LINK_HEADER_SIZE);
606ea6
+  etherhdr_size = sizeof (*eth);
606ea6
+  COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
606ea6
 
606ea6
-  err = grub_netbuff_push (nb, sizeof (*eth));
606ea6
+  const char *vlantag_text = grub_env_get ("vlan-tag");
606ea6
+  if (vlantag_text != 0) {
606ea6
+      etherhdr_size += 4;
606ea6
+      vlantag = grub_strtoul (vlantag_text, 0, 16);
606ea6
+  }
606ea6
+
606ea6
+  err = grub_netbuff_push (nb, etherhdr_size);
606ea6
   if (err)
606ea6
     return err;
606ea6
   eth = (struct etherhdr *) nb->data;
606ea6
@@ -76,6 +86,19 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
606ea6
 	return err;
606ea6
       inf->card->opened = 1;
606ea6
     }
606ea6
+
606ea6
+  /* Check if a vlan-tag is needed. */
606ea6
+  if (vlantag != 0)
606ea6
+    {
606ea6
+      /* Move eth type to the right */
606ea6
+      grub_memcpy((char *) nb->data + etherhdr_size - 2,
606ea6
+                  (char *) nb->data + etherhdr_size - 6, 2);
606ea6
+
606ea6
+      /* Add the tag in the middle */
606ea6
+      grub_memcpy((char *) nb->data + etherhdr_size - 6,
606ea6
+                  &vlantag, 4);
606ea6
+    }
606ea6
+
606ea6
   return inf->card->driver->send (inf->card, nb);
606ea6
 }
606ea6
 
606ea6
@@ -90,10 +113,23 @@ grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
606ea6
   grub_net_link_level_address_t hwaddress;
606ea6
   grub_net_link_level_address_t src_hwaddress;
606ea6
   grub_err_t err;
606ea6
+  grub_uint8_t etherhdr_size = sizeof (*eth);
606ea6
+
606ea6
+  grub_uint16_t vlantag_identifier = 0;
606ea6
+  grub_memcpy (&vlantag_identifier, nb->data + etherhdr_size - 2, 2);
606ea6
+
606ea6
+  /* Check if a vlan-tag is present. */
606ea6
+  if (vlantag_identifier == VLANTAG_IDENTIFIER)
606ea6
+    {
606ea6
+      etherhdr_size += 4;
606ea6
+      /* Move eth type to the original position */
606ea6
+      grub_memcpy((char *) nb->data + etherhdr_size - 6,
606ea6
+                  (char *) nb->data + etherhdr_size - 2, 2);
606ea6
+    }
606ea6
 
606ea6
   eth = (struct etherhdr *) nb->data;
606ea6
   type = grub_be_to_cpu16 (eth->type);
606ea6
-  err = grub_netbuff_pull (nb, sizeof (*eth));
606ea6
+  err = grub_netbuff_pull (nb, etherhdr_size);
606ea6
   if (err)
606ea6
     return err;
606ea6
 
606ea6
diff --git a/include/grub/ieee1275/ieee1275.h b/include/grub/ieee1275/ieee1275.h
28f7f8
index 9f26c69a2df..6a21f5d6de8 100644
606ea6
--- a/include/grub/ieee1275/ieee1275.h
606ea6
+++ b/include/grub/ieee1275/ieee1275.h
39700a
@@ -236,6 +236,7 @@ void EXPORT_FUNC(grub_ieee1275_children_first) (const char *devpath,
606ea6
 						struct grub_ieee1275_devalias *alias);
606ea6
 int EXPORT_FUNC(grub_ieee1275_cas_reboot) (char *script);
606ea6
 int EXPORT_FUNC(grub_ieee1275_set_boot_last_label) (const char *text);
606ea6
+int EXPORT_FUNC(grub_ieee1275_parse_net_options) (const char *path);
606ea6
 
606ea6
 #define FOR_IEEE1275_DEVALIASES(alias) for (grub_ieee1275_devalias_init_iterator (&(alias)); grub_ieee1275_devalias_next (&(alias));)
606ea6
 
606ea6
diff --git a/include/grub/net.h b/include/grub/net.h
28f7f8
index 538baa33eca..a799e6b8b69 100644
606ea6
--- a/include/grub/net.h
606ea6
+++ b/include/grub/net.h
39700a
@@ -538,4 +538,6 @@ extern char *grub_net_default_server;
606ea6
 #define GRUB_NET_INTERVAL 400
39700a
 #define GRUB_NET_INTERVAL_ADDITION 20
606ea6
 
606ea6
+#define VLANTAG_IDENTIFIER 0x8100
606ea6
+
606ea6
 #endif /* ! GRUB_NET_HEADER */