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