Add support for MPLS header creating with fields:

    Label, TClass, Bottom-Stack, TTL

By default S-field is set to 1 (last label), but resets to 0
if lower MPLS header is added.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen_l2.c    | 32 ++++++++++++++++++++++++++++++++
 trafgen_l2.h    |  7 +++++++
 trafgen_proto.h |  1 +
 3 files changed, 40 insertions(+)

diff --git a/trafgen_l2.c b/trafgen_l2.c
index a7387b8..fd2ebf1 100644
--- a/trafgen_l2.c
+++ b/trafgen_l2.c
@@ -28,6 +28,8 @@ static uint16_t pid_to_eth(enum proto_id pid)
                return ETH_P_IPV6;
        case PROTO_VLAN:
                return ETH_P_8021Q;
+       case PROTO_MPLS:
+               return ETH_P_MPLS_UC;
        default:
                panic("eth: Not supported protocol id %u\n", pid);
        }
@@ -94,6 +96,35 @@ static struct proto_hdr vlan_hdr = {
        .set_next_proto = vlan_set_next_proto,
 };
 
+static struct proto_field mpls_fields[] = {
+       { .id = MPLS_LABEL, .len = 4, .shift = 12, .mask = 0xfffff000 },
+       { .id = MPLS_TC,    .len = 4, .shift = 9,  .mask = 0xe00 },
+       { .id = MPLS_LAST,  .len = 4, .shift = 8,  .mask = 0x100 },
+       { .id = MPLS_TTL,   .len = 4, .shift = 0,  .mask = 0xff },
+};
+
+static void mpls_header_init(struct proto_hdr *hdr)
+{
+       proto_lower_default_add(hdr, PROTO_ETH);
+
+       proto_header_fields_add(hdr, mpls_fields, array_size(mpls_fields));
+
+       proto_field_set_default_be32(hdr, MPLS_LAST, 1);
+}
+
+static void mpls_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
+{
+       if (pid == PROTO_MPLS)
+               proto_field_set_default_be32(hdr, MPLS_LAST, 0);
+}
+
+static struct proto_hdr mpls_hdr = {
+       .id             = PROTO_MPLS,
+       .layer          = PROTO_L2,
+       .header_init    = mpls_header_init,
+       .set_next_proto = mpls_set_next_proto,
+};
+
 static struct proto_field arp_fields[] = {
        { .id = ARP_HTYPE, .len = 2 },
        { .id = ARP_PTYPE, .len = 2, .offset = 2 },
@@ -141,5 +172,6 @@ void protos_l2_init(void)
 {
        proto_header_register(&eth_hdr);
        proto_header_register(&vlan_hdr);
+       proto_header_register(&mpls_hdr);
        proto_header_register(&arp_hdr);
 }
diff --git a/trafgen_l2.h b/trafgen_l2.h
index 9beb2aa..14f0e84 100644
--- a/trafgen_l2.h
+++ b/trafgen_l2.h
@@ -28,6 +28,13 @@ enum vlan_field {
        VLAN_ETYPE,
 };
 
+enum mpls_field {
+       MPLS_LABEL,
+       MPLS_TC,
+       MPLS_LAST,
+       MPLS_TTL,
+};
+
 extern void protos_l2_init(void);
 
 #endif /* TRAFGEN_L2_H */
diff --git a/trafgen_proto.h b/trafgen_proto.h
index 10ded79..0878a86 100644
--- a/trafgen_proto.h
+++ b/trafgen_proto.h
@@ -13,6 +13,7 @@ enum proto_id {
        PROTO_NONE,
        PROTO_ETH,
        PROTO_VLAN,
+       PROTO_MPLS,
        PROTO_ARP,
        PROTO_IP4,
        PROTO_IP6,
-- 
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