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