summaryrefslogtreecommitdiff
path: root/trafgen_l3.c
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2016-04-22 16:55:31 +0200
committerTobias Klauser <tklauser@distanz.ch>2016-04-25 17:56:34 +0200
commitc09e06a6efaa94768660636a9c58ce9666e29d05 (patch)
treea41455899d30c7a8de30fdc9eb917702d9769975 /trafgen_l3.c
parent0cf15c320f9211d59b055157c895537e4b682a07 (diff)
trafgen: proto: Add IPv6 header generation
Support for generating simple IPv6 headers using the 'ip6()/ipv6()' trafgen generation function. Fields supported: ver|version Version (default: 6) tc|tclass Traffic class (default: 0) fl|flow Flow Label (default: 0) len|length Payload length (calculated by default) nh|nexthdr Type of next header (default: 0) hl|hoplimit|ttl Hop Limit, TTL (default: 0) sa|saddr Source IPv6 address (default: device address) da|daddr Destination IPv6 address (default: 0:0:0:0:0:0:0:0) Examples: { eth(), ipv6(daddr=1:2:3:4:5:6:7:8) } { ipv6(tc=2, hl=3, daddr=::1) } { eth(), ipv6(nh=58, sa=2001:db8::, da=::1), 128, 0, 0x52, 0x03, 0, 0, 0, 0 } If not explicitely specified, the lower header is initialized as Ethernet. Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trafgen_l3.c')
-rw-r--r--trafgen_l3.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/trafgen_l3.c b/trafgen_l3.c
index 88766ea..94c0755 100644
--- a/trafgen_l3.c
+++ b/trafgen_l3.c
@@ -90,7 +90,65 @@ static struct proto_hdr ipv4_hdr = {
.set_next_proto = ipv4_set_next_proto,
};
+static struct proto_field ipv6_fields[] = {
+ { .id = IP6_VER, .len = 4, .offset = 0, .shift = 28, .mask = 0xf0000000 },
+ { .id = IP6_CLASS, .len = 4, .offset = 0, .shift = 20, .mask = 0x0ff00000 },
+ { .id = IP6_FLOW_LBL, .len = 4, .offset = 0, .shift = 0, .mask = 0x000fffff },
+ { .id = IP6_LEN, .len = 2, .offset = 4 },
+ { .id = IP6_NEXT_HDR, .len = 1, .offset = 6 },
+ { .id = IP6_HOP_LIMIT, .len = 1, .offset = 7 },
+ { .id = IP6_SADDR, .len = 16, .offset = 8 },
+ { .id = IP6_DADDR, .len = 16, .offset = 24 },
+};
+
+static void ipv6_header_init(struct proto_hdr *hdr)
+{
+ proto_lower_default_add(hdr, PROTO_ETH);
+
+ proto_header_fields_add(hdr, ipv6_fields, array_size(ipv6_fields));
+
+ proto_field_set_default_be32(hdr, IP6_VER, 6);
+ proto_field_set_default_dev_ipv6(hdr, IP6_SADDR);
+}
+
+#define IPV6_HDR_LEN 40
+
+static void ipv6_packet_finish(struct proto_hdr *hdr)
+{
+ struct packet *pkt = current_packet();
+ uint16_t total_len = pkt->len - hdr->pkt_offset - IPV6_HDR_LEN;
+
+ proto_field_set_default_be16(hdr, IP6_LEN, total_len);
+}
+
+static void ipv6_set_next_proto(struct proto_hdr *hdr, enum proto_id pid)
+{
+ uint8_t ip_proto;
+
+ switch(pid) {
+ case PROTO_UDP:
+ ip_proto = IPPROTO_UDP;
+ break;
+ case PROTO_TCP:
+ ip_proto = IPPROTO_TCP;
+ break;
+ default:
+ bug();
+ }
+
+ proto_field_set_default_u8(hdr, IP6_NEXT_HDR, ip_proto);
+}
+
+static struct proto_hdr ipv6_hdr = {
+ .id = PROTO_IP6,
+ .layer = PROTO_L3,
+ .header_init = ipv6_header_init,
+ .packet_finish = ipv6_packet_finish,
+ .set_next_proto = ipv6_set_next_proto,
+};
+
void protos_l3_init(void)
{
proto_header_register(&ipv4_hdr);
+ proto_header_register(&ipv6_hdr);
}