Add 'vlan()' function to generate VLAN header.

Fields supported:

        tpid|proto      Set TPID (Tag Protocol Identifier) (default 0x8100)
        1ad             Set TPID field as 0x88a8
        1q              Set TPID field as 0x8100
        tci             Set TCI (Tag Control Information) (default 0)
        pcp             Set PCP (Priority Code Point) (PCP) (default 0)
        dei|cfi         Set DEI (Drop Eligible Indicator) (default 0)
        id              Set VID (VLAN Identifier) (default 0)

VLAN identifier might be specified like just number or via 'id' parameter.

Examples:

        { eth(), vlan(1), ipv4() }
        { vlan(1, 1ad), vlan(100, pcp=3), ipv4() }

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen_lexer.l  |  9 +++++++++
 trafgen_parser.y | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/trafgen_lexer.l b/trafgen_lexer.l
index ef7ec2a..e1d1a3f 100644
--- a/trafgen_lexer.l
+++ b/trafgen_lexer.l
@@ -112,6 +112,14 @@ ip4_addr   ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)
 "saddr"|"sa"   { return K_SADDR; }
 [e]?"type"     { return K_ETYPE; }
 
+       /* VLAN (802.1Q & 802.1ad) */
+"tpid"         { return K_TPID; }
+"tci"          { return K_TCI; }
+"pcp"          { return K_PCP; }
+"dei"|"cfi"    { return K_DEI; }
+"1ad"          { return K_1AD; }
+"1q"           { return K_1Q; }
+
        /* ARP */
 "sha"|"smac"   { return K_SHA; }
 "spa"|"sip"    { return K_SPA; }
@@ -158,6 +166,7 @@ ip4_addr    ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+)
 "urgptr"       { return K_URG_PTR; }
 
 "eth"          { return K_ETH; }
+"vlan"         { return K_VLAN; }
 "arp"          { return K_ARP; }
 "ip4"|"ipv4"   { return K_IP4; }
 "udp"          { return K_UDP; }
diff --git a/trafgen_parser.y b/trafgen_parser.y
index 091d6b2..5f5b79d 100644
--- a/trafgen_parser.y
+++ b/trafgen_parser.y
@@ -19,6 +19,7 @@
 #include <libgen.h>
 #include <net/if_arp.h>
 #include <netinet/in.h>
+#include <linux/if_ether.h>
 
 #include "xmalloc.h"
 #include "trafgen_parser.tab.h"
@@ -354,8 +355,10 @@ static void proto_add(enum proto_id pid)
 %token K_PROT K_TTL K_DSCP K_ECN K_TOS K_LEN K_ID K_FLAGS K_FRAG K_IHL K_VER 
K_CSUM K_DF K_MF
 %token K_SPORT K_DPORT
 %token K_SEQ K_ACK_SEQ K_DOFF K_CWR K_ECE K_URG K_ACK K_PSH K_RST K_SYN K_FIN 
K_WINDOW K_URG_PTR
+%token K_TPID K_TCI K_PCP K_DEI K_1Q K_1AD
 
 %token K_ETH
+%token K_VLAN
 %token K_ARP
 %token K_IP4
 %token K_UDP K_TCP
@@ -579,6 +582,7 @@ ddec
 
 proto
        : eth_proto { }
+       | vlan_proto { }
        | arp_proto { }
        | ip4_proto { }
        | udp_proto { }
@@ -613,6 +617,41 @@ eth_field
                { proto_field_set_be16(hdr, ETH_TYPE, $5); }
        ;
 
+vlan_proto
+       : vlan '(' vlan_param_list ')' { }
+       ;
+
+vlan
+       : K_VLAN { proto_add(PROTO_VLAN); }
+       ;
+
+vlan_param_list
+       : { }
+       | vlan_field { }
+       | vlan_field delimiter vlan_param_list { }
+       ;
+
+vlan_field
+       : K_TPID  skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, VLAN_TPID, $5); }
+       | K_PROT  skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, VLAN_TPID, $5); }
+       | K_1Q
+               { proto_field_set_be16(hdr, VLAN_TPID, ETH_P_8021Q); }
+       | K_1AD
+               { proto_field_set_be16(hdr, VLAN_TPID, ETH_P_8021AD); }
+       | K_TCI  skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, VLAN_TCI, $5); }
+       | K_PCP skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, VLAN_PCP, $5); }
+       | K_DEI skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, VLAN_DEI, $5); }
+       | K_ID skip_white '=' skip_white number
+               { proto_field_set_be16(hdr, VLAN_VID, $5); }
+       | number
+               { proto_field_set_be16(hdr, VLAN_VID, $1); }
+       ;
+
 arp_proto
        : arp '(' arp_param_list ')' { }
        ;
-- 
2.6.3

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to