bc092b9
From d9f7de0ae3c0c09cff7257c55418450261f3e082 Mon Sep 17 00:00:00 2001
bc092b9
From: Paulo Flabiano Smorigo <pfsmorigo@br.ibm.com>
bc092b9
Date: Mon, 23 Dec 2013 12:32:02 -0200
6f1e3d5
Subject: [PATCH 013/198] Add Virtual LAN support.
bc092b9
bc092b9
This patch adds support for virtual LAN (VLAN) tagging. VLAN tagging allows
bc092b9
multiple VLANs in a bridged network to share the same physical network link
bc092b9
but maintain isolation:
bc092b9
bc092b9
http://en.wikipedia.org/wiki/IEEE_802.1Q
bc092b9
bc092b9
* grub-core/net/ethernet.c: Add check, get, and set vlan tag id.
bc092b9
* grub-core/net/drivers/ieee1275/ofnet.c: Get vlan tag id from bootargs.
bc092b9
* grub-core/net/arp.c: Add check.
bc092b9
* grub-core/net/ip.c: Likewise.
bc092b9
* include/grub/net/arp.h: Add vlantag attribute.
bc092b9
* include/grub/net/ip.h: Likewise.
bc092b9
---
bc092b9
 grub-core/net/arp.c                    | 12 +++++++--
bc092b9
 grub-core/net/drivers/ieee1275/ofnet.c |  9 ++++++-
bc092b9
 grub-core/net/ethernet.c               | 47 ++++++++++++++++++++++++++++++----
bc092b9
 grub-core/net/ip.c                     | 33 +++++++++++++++++-------
bc092b9
 include/grub/net.h                     |  3 +++
bc092b9
 include/grub/net/arp.h                 |  5 ++--
bc092b9
 include/grub/net/ip.h                  |  3 ++-
bc092b9
 7 files changed, 92 insertions(+), 20 deletions(-)
bc092b9
bc092b9
diff --git a/grub-core/net/arp.c b/grub-core/net/arp.c
bc092b9
index 4b68c4151..54306e3b1 100644
bc092b9
--- a/grub-core/net/arp.c
bc092b9
+++ b/grub-core/net/arp.c
bc092b9
@@ -111,8 +111,8 @@ grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
bc092b9
 }
bc092b9
 
bc092b9
 grub_err_t
bc092b9
-grub_net_arp_receive (struct grub_net_buff *nb,
bc092b9
-		      struct grub_net_card *card)
bc092b9
+grub_net_arp_receive (struct grub_net_buff *nb, struct grub_net_card *card,
bc092b9
+                      grub_uint16_t *vlantag)
bc092b9
 {
bc092b9
   struct arppkt *arp_packet = (struct arppkt *) nb->data;
bc092b9
   grub_net_network_level_address_t sender_addr, target_addr;
bc092b9
@@ -138,6 +138,14 @@ grub_net_arp_receive (struct grub_net_buff *nb,
bc092b9
 
bc092b9
   FOR_NET_NETWORK_LEVEL_INTERFACES (inf)
bc092b9
   {
bc092b9
+    /* Verify vlantag id */
bc092b9
+    if (inf->card == card && inf->vlantag != *vlantag)
bc092b9
+      {
bc092b9
+        grub_dprintf ("net", "invalid vlantag! %x != %x\n",
bc092b9
+                      inf->vlantag, *vlantag);
bc092b9
+        break;
bc092b9
+      }
bc092b9
+
bc092b9
     /* Am I the protocol address target? */
bc092b9
     if (grub_net_addr_cmp (&inf->address, &target_addr) == 0
bc092b9
 	&& arp_packet->op == grub_cpu_to_be16_compile_time (ARP_REQUEST))
bc092b9
diff --git a/grub-core/net/drivers/ieee1275/ofnet.c b/grub-core/net/drivers/ieee1275/ofnet.c
bc092b9
index a78d164db..002446be1 100644
bc092b9
--- a/grub-core/net/drivers/ieee1275/ofnet.c
bc092b9
+++ b/grub-core/net/drivers/ieee1275/ofnet.c
bc092b9
@@ -153,11 +153,11 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
bc092b9
   char *comma_char = 0;
bc092b9
   char *equal_char = 0;
bc092b9
   grub_size_t field_counter = 0;
bc092b9
-
bc092b9
   grub_net_network_level_address_t client_addr, gateway_addr, subnet_mask;
bc092b9
   grub_net_link_level_address_t hw_addr;
bc092b9
   grub_net_interface_flags_t flags = 0;
bc092b9
   struct grub_net_network_level_interface *inter = NULL;
bc092b9
+  grub_uint16_t vlantag = 0;
bc092b9
 
bc092b9
   hw_addr.type = GRUB_NET_LINK_LEVEL_PROTOCOL_ETHERNET;
bc092b9
 
bc092b9
@@ -175,6 +175,11 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
bc092b9
           *equal_char = 0;
bc092b9
           grub_env_set_net_property ((*card)->name, args, equal_char + 1,
bc092b9
                                      grub_strlen(equal_char + 1));
bc092b9
+
bc092b9
+          if ((grub_strcmp (args, "vtag") == 0) &&
bc092b9
+              (grub_strlen (equal_char + 1) == 8))
bc092b9
+            vlantag = grub_strtoul (equal_char + 1 + 4, 0, 16);
bc092b9
+
bc092b9
           *equal_char = '=';
bc092b9
         }
bc092b9
       else
bc092b9
@@ -213,8 +218,10 @@ grub_ieee1275_parse_bootpath (const char *devpath, char *bootpath,
bc092b9
                                   hw_addr.mac, sizeof(hw_addr.mac), 0);
bc092b9
       inter = grub_net_add_addr ((*card)->name, *card, &client_addr, &hw_addr,
bc092b9
                                  flags);
bc092b9
+      inter->vlantag = vlantag;
bc092b9
       grub_net_add_ipv4_local (inter,
bc092b9
                           __builtin_ctz (~grub_le_to_cpu32 (subnet_mask.ipv4)));
bc092b9
+
bc092b9
     }
bc092b9
 
bc092b9
   if (gateway_addr.ipv4 != 0)
bc092b9
diff --git a/grub-core/net/ethernet.c b/grub-core/net/ethernet.c
bc092b9
index c397b1b34..4d7ceed6f 100644
bc092b9
--- a/grub-core/net/ethernet.c
bc092b9
+++ b/grub-core/net/ethernet.c
bc092b9
@@ -18,6 +18,7 @@
bc092b9
 
bc092b9
 #include <grub/misc.h>
bc092b9
 #include <grub/mm.h>
bc092b9
+#include <grub/env.h>
bc092b9
 #include <grub/net/ethernet.h>
bc092b9
 #include <grub/net/ip.h>
bc092b9
 #include <grub/net/arp.h>
bc092b9
@@ -56,10 +57,17 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
bc092b9
 {
bc092b9
   struct etherhdr *eth;
bc092b9
   grub_err_t err;
bc092b9
+  grub_uint8_t etherhdr_size;
bc092b9
+  grub_uint16_t vlantag_id = VLANTAG_IDENTIFIER;
bc092b9
 
bc092b9
-  COMPILE_TIME_ASSERT (sizeof (*eth) < GRUB_NET_MAX_LINK_HEADER_SIZE);
bc092b9
+  etherhdr_size = sizeof (*eth);
bc092b9
+  COMPILE_TIME_ASSERT (sizeof (*eth) + 4 < GRUB_NET_MAX_LINK_HEADER_SIZE);
bc092b9
 
bc092b9
-  err = grub_netbuff_push (nb, sizeof (*eth));
bc092b9
+  /* Increase ethernet header in case of vlantag */
bc092b9
+  if (inf->vlantag != 0)
bc092b9
+    etherhdr_size += 4;
bc092b9
+
bc092b9
+  err = grub_netbuff_push (nb, etherhdr_size);
bc092b9
   if (err)
bc092b9
     return err;
bc092b9
   eth = (struct etherhdr *) nb->data;
bc092b9
@@ -76,6 +84,19 @@ send_ethernet_packet (struct grub_net_network_level_interface *inf,
bc092b9
 	return err;
bc092b9
       inf->card->opened = 1;
bc092b9
     }
bc092b9
+
bc092b9
+  /* Check and add a vlan-tag if needed. */
bc092b9
+  if (inf->vlantag != 0)
bc092b9
+    {
bc092b9
+      /* Move eth type to the right */
bc092b9
+      grub_memcpy ((char *) nb->data + etherhdr_size - 2,
bc092b9
+                   (char *) nb->data + etherhdr_size - 6, 2);
bc092b9
+
bc092b9
+      /* Add the tag in the middle */
bc092b9
+      grub_memcpy ((char *) nb->data + etherhdr_size - 6, &vlantag_id, 2);
bc092b9
+      grub_memcpy ((char *) nb->data + etherhdr_size - 4, (char *) &(inf->vlantag), 2);
bc092b9
+    }
bc092b9
+
bc092b9
   return inf->card->driver->send (inf->card, nb);
bc092b9
 }
bc092b9
 
bc092b9
@@ -90,10 +111,25 @@ grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
bc092b9
   grub_net_link_level_address_t hwaddress;
bc092b9
   grub_net_link_level_address_t src_hwaddress;
bc092b9
   grub_err_t err;
bc092b9
+  grub_uint8_t etherhdr_size = sizeof (*eth);
bc092b9
+  grub_uint16_t vlantag = 0;
bc092b9
+
bc092b9
+
bc092b9
+  /* Check if a vlan-tag is present. If so, the ethernet header is 4 bytes */
bc092b9
+  /* longer than the original one. The vlantag id is extracted and the header */
bc092b9
+  /* is reseted to the original size. */
bc092b9
+  if (grub_get_unaligned16 (nb->data + etherhdr_size - 2) == VLANTAG_IDENTIFIER)
bc092b9
+    {
bc092b9
+      vlantag = grub_get_unaligned16 (nb->data + etherhdr_size);
bc092b9
+      etherhdr_size += 4;
bc092b9
+      /* Move eth type to the original position */
bc092b9
+      grub_memcpy((char *) nb->data + etherhdr_size - 6,
bc092b9
+                  (char *) nb->data + etherhdr_size - 2, 2);
bc092b9
+    }
bc092b9
 
bc092b9
   eth = (struct etherhdr *) nb->data;
bc092b9
   type = grub_be_to_cpu16 (eth->type);
bc092b9
-  err = grub_netbuff_pull (nb, sizeof (*eth));
bc092b9
+  err = grub_netbuff_pull (nb, etherhdr_size);
bc092b9
   if (err)
bc092b9
     return err;
bc092b9
 
bc092b9
@@ -121,13 +157,14 @@ grub_net_recv_ethernet_packet (struct grub_net_buff *nb,
bc092b9
     {
bc092b9
       /* ARP packet. */
bc092b9
     case GRUB_NET_ETHERTYPE_ARP:
bc092b9
-      grub_net_arp_receive (nb, card);
bc092b9
+      grub_net_arp_receive (nb, card, &vlantag);
bc092b9
       grub_netbuff_free (nb);
bc092b9
       return GRUB_ERR_NONE;
bc092b9
       /* IP packet.  */
bc092b9
     case GRUB_NET_ETHERTYPE_IP:
bc092b9
     case GRUB_NET_ETHERTYPE_IP6:
bc092b9
-      return grub_net_recv_ip_packets (nb, card, &hwaddress, &src_hwaddress);
bc092b9
+      return grub_net_recv_ip_packets (nb, card, &hwaddress, &src_hwaddress,
bc092b9
+                                       &vlantag);
bc092b9
     }
bc092b9
   grub_netbuff_free (nb);
bc092b9
   return GRUB_ERR_NONE;
bc092b9
diff --git a/grub-core/net/ip.c b/grub-core/net/ip.c
bc092b9
index aba4f8908..7c95cc746 100644
bc092b9
--- a/grub-core/net/ip.c
bc092b9
+++ b/grub-core/net/ip.c
bc092b9
@@ -228,12 +228,13 @@ handle_dgram (struct grub_net_buff *nb,
bc092b9
 	      grub_net_ip_protocol_t proto,
bc092b9
 	      const grub_net_network_level_address_t *source,
bc092b9
 	      const grub_net_network_level_address_t *dest,
bc092b9
+              grub_uint16_t *vlantag,
bc092b9
 	      grub_uint8_t ttl)
bc092b9
 {
bc092b9
   struct grub_net_network_level_interface *inf = NULL;
bc092b9
   grub_err_t err;
bc092b9
   int multicast = 0;
bc092b9
-  
bc092b9
+
bc092b9
   /* DHCP needs special treatment since we don't know IP yet.  */
bc092b9
   {
bc092b9
     struct udphdr *udph;
bc092b9
@@ -293,6 +294,15 @@ handle_dgram (struct grub_net_buff *nb,
bc092b9
 	&& grub_net_addr_cmp (&inf->address, dest) == 0
bc092b9
 	&& grub_net_hwaddr_cmp (&inf->hwaddress, hwaddress) == 0)
bc092b9
       break;
bc092b9
+
bc092b9
+    /* Verify vlantag id */
bc092b9
+    if (inf->card == card && inf->vlantag != *vlantag)
bc092b9
+      {
bc092b9
+        grub_dprintf ("net", "invalid vlantag! %x != %x\n",
bc092b9
+                      inf->vlantag, *vlantag);
bc092b9
+        break;
bc092b9
+      }
bc092b9
+
bc092b9
     /* Solicited node multicast.  */
bc092b9
     if (inf->card == card
bc092b9
 	&& inf->address.type == GRUB_NET_NETWORK_LEVEL_PROTOCOL_IPV6
bc092b9
@@ -383,7 +393,8 @@ static grub_err_t
bc092b9
 grub_net_recv_ip4_packets (struct grub_net_buff *nb,
bc092b9
 			   struct grub_net_card *card,
bc092b9
 			   const grub_net_link_level_address_t *hwaddress,
bc092b9
-			   const grub_net_link_level_address_t *src_hwaddress)
bc092b9
+			   const grub_net_link_level_address_t *src_hwaddress,
bc092b9
+                           grub_uint16_t *vlantag)
bc092b9
 {
bc092b9
   struct iphdr *iph = (struct iphdr *) nb->data;
bc092b9
   grub_err_t err;
bc092b9
@@ -458,7 +469,7 @@ grub_net_recv_ip4_packets (struct grub_net_buff *nb,
bc092b9
       dest.ipv4 = iph->dest;
bc092b9
 
bc092b9
       return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
bc092b9
-			   &source, &dest, iph->ttl);
bc092b9
+			   &source, &dest, vlantag, iph->ttl);
bc092b9
     }
bc092b9
 
bc092b9
   for (prev = &reassembles, rsm = *prev; rsm; prev = &rsm->next, rsm = *prev)
bc092b9
@@ -594,7 +605,7 @@ grub_net_recv_ip4_packets (struct grub_net_buff *nb,
bc092b9
       dest.ipv4 = dst;
bc092b9
 
bc092b9
       return handle_dgram (ret, card, src_hwaddress,
bc092b9
-			   hwaddress, proto, &source, &dest,
bc092b9
+			   hwaddress, proto, &source, &dest, vlantag,
bc092b9
 			   ttl);
bc092b9
     }
bc092b9
 }
bc092b9
@@ -652,7 +663,8 @@ static grub_err_t
bc092b9
 grub_net_recv_ip6_packets (struct grub_net_buff *nb,
bc092b9
 			   struct grub_net_card *card,
bc092b9
 			   const grub_net_link_level_address_t *hwaddress,
bc092b9
-			   const grub_net_link_level_address_t *src_hwaddress)
bc092b9
+			   const grub_net_link_level_address_t *src_hwaddress,
bc092b9
+                           grub_uint16_t *vlantag)
bc092b9
 {
bc092b9
   struct ip6hdr *iph = (struct ip6hdr *) nb->data;
bc092b9
   grub_err_t err;
bc092b9
@@ -703,21 +715,24 @@ grub_net_recv_ip6_packets (struct grub_net_buff *nb,
bc092b9
   grub_memcpy (dest.ipv6, &iph->dest, sizeof (dest.ipv6));
bc092b9
 
bc092b9
   return handle_dgram (nb, card, src_hwaddress, hwaddress, iph->protocol,
bc092b9
-		       &source, &dest, iph->ttl);
bc092b9
+		       &source, &dest, vlantag, iph->ttl);
bc092b9
 }
bc092b9
 
bc092b9
 grub_err_t
bc092b9
 grub_net_recv_ip_packets (struct grub_net_buff *nb,
bc092b9
 			  struct grub_net_card *card,
bc092b9
 			  const grub_net_link_level_address_t *hwaddress,
bc092b9
-			  const grub_net_link_level_address_t *src_hwaddress)
bc092b9
+			  const grub_net_link_level_address_t *src_hwaddress,
bc092b9
+                          grub_uint16_t *vlantag)
bc092b9
 {
bc092b9
   struct iphdr *iph = (struct iphdr *) nb->data;
bc092b9
 
bc092b9
   if ((iph->verhdrlen >> 4) == 4)
bc092b9
-    return grub_net_recv_ip4_packets (nb, card, hwaddress, src_hwaddress);
bc092b9
+    return grub_net_recv_ip4_packets (nb, card, hwaddress, src_hwaddress,
bc092b9
+                                      vlantag);
bc092b9
   if ((iph->verhdrlen >> 4) == 6)
bc092b9
-    return grub_net_recv_ip6_packets (nb, card, hwaddress, src_hwaddress);
bc092b9
+    return grub_net_recv_ip6_packets (nb, card, hwaddress, src_hwaddress,
bc092b9
+                                      vlantag);
bc092b9
   grub_dprintf ("net", "Bad IP version: %d\n", (iph->verhdrlen >> 4));
bc092b9
   grub_netbuff_free (nb);
bc092b9
   return GRUB_ERR_NONE;
bc092b9
diff --git a/include/grub/net.h b/include/grub/net.h
bc092b9
index 2192fa186..1096b2432 100644
bc092b9
--- a/include/grub/net.h
bc092b9
+++ b/include/grub/net.h
bc092b9
@@ -291,6 +291,7 @@ struct grub_net_network_level_interface
bc092b9
   grub_net_interface_flags_t flags;
bc092b9
   struct grub_net_bootp_packet *dhcp_ack;
bc092b9
   grub_size_t dhcp_acklen;
bc092b9
+  grub_uint16_t vlantag;
bc092b9
   void *data;
bc092b9
 };
bc092b9
 
bc092b9
@@ -561,4 +562,6 @@ extern char *grub_net_default_server;
bc092b9
 #define GRUB_NET_INTERVAL 400
bc092b9
 #define GRUB_NET_INTERVAL_ADDITION 20
bc092b9
 
bc092b9
+#define VLANTAG_IDENTIFIER 0x8100
bc092b9
+
bc092b9
 #endif /* ! GRUB_NET_HEADER */
bc092b9
diff --git a/include/grub/net/arp.h b/include/grub/net/arp.h
bc092b9
index bb1703622..8d9d08113 100644
bc092b9
--- a/include/grub/net/arp.h
bc092b9
+++ b/include/grub/net/arp.h
bc092b9
@@ -22,10 +22,11 @@
bc092b9
 #include <grub/net.h>
bc092b9
 
bc092b9
 extern grub_err_t grub_net_arp_receive (struct grub_net_buff *nb,
bc092b9
-					struct grub_net_card *card);
bc092b9
+                                        struct grub_net_card *card,
bc092b9
+                                        grub_uint16_t *vlantag);
bc092b9
 
bc092b9
 grub_err_t
bc092b9
 grub_net_arp_send_request (struct grub_net_network_level_interface *inf,
bc092b9
-			   const grub_net_network_level_address_t *proto_addr);
bc092b9
+                           const grub_net_network_level_address_t *proto_addr);
bc092b9
 
bc092b9
 #endif 
bc092b9
diff --git a/include/grub/net/ip.h b/include/grub/net/ip.h
bc092b9
index dcceaa568..ab9d68f98 100644
bc092b9
--- a/include/grub/net/ip.h
bc092b9
+++ b/include/grub/net/ip.h
bc092b9
@@ -48,7 +48,8 @@ grub_err_t
bc092b9
 grub_net_recv_ip_packets (struct grub_net_buff *nb,
bc092b9
 			  struct grub_net_card *card,
bc092b9
 			  const grub_net_link_level_address_t *hwaddress,
bc092b9
-			  const grub_net_link_level_address_t *src_hwaddress);
bc092b9
+			  const grub_net_link_level_address_t *src_hwaddress,
bc092b9
+                          grub_uint16_t *vlantag);
bc092b9
 
bc092b9
 grub_err_t
bc092b9
 grub_net_send_ip_packet (struct grub_net_network_level_interface *inf,
bc092b9
-- 
da63b36
2.14.3
bc092b9