summaryrefslogtreecommitdiff
path: root/trafgen_l4.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_l4.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_l4.c')
-rw-r--r--trafgen_l4.c84
1 files changed, 54 insertions, 30 deletions
diff --git a/trafgen_l4.c b/trafgen_l4.c
index 64aada4..a5981c4 100644
--- a/trafgen_l4.c
+++ b/trafgen_l4.c
@@ -15,30 +15,10 @@
#include "trafgen_proto.h"
static struct proto_field udp_fields[] = {
- { .id = UDP_SPORT, .len = 2, .offset = 0 },
- { .id = UDP_DPORT, .len = 2, .offset = 2 },
- { .id = UDP_LEN, .len = 2, .offset = 4 },
- { .id = UDP_CSUM, .len = 2, .offset = 6 },
-};
-
-static struct proto_field tcp_fields[] = {
- { .id = TCP_SPORT, .len = 2, .offset = 0 },
- { .id = TCP_DPORT, .len = 2, .offset = 2 },
- { .id = TCP_SEQ, .len = 4, .offset = 4 },
- { .id = TCP_ACK_SEQ, .len = 4, .offset = 8 },
- { .id = TCP_DOFF, .len = 2, .offset = 12, .shift = 12, .mask = 0xf000 },
- /* reserved (4 bits) */
- { .id = TCP_CWR, .len = 2, .offset = 12, .shift = 7, .mask = 0x0080 },
- { .id = TCP_ECE, .len = 2, .offset = 12, .shift = 6, .mask = 0x0040 },
- { .id = TCP_URG, .len = 2, .offset = 12, .shift = 5, .mask = 0x0020 },
- { .id = TCP_ACK, .len = 2, .offset = 12, .shift = 4, .mask = 0x0010 },
- { .id = TCP_PSH, .len = 2, .offset = 12, .shift = 3, .mask = 0x0008 },
- { .id = TCP_RST, .len = 2, .offset = 12, .shift = 2, .mask = 0x0004 },
- { .id = TCP_SYN, .len = 2, .offset = 12, .shift = 1, .mask = 0x0002 },
- { .id = TCP_FIN, .len = 2, .offset = 12, .shift = 0, .mask = 0x0001 },
- { .id = TCP_WINDOW, .len = 2, .offset = 14 },
- { .id = TCP_CSUM, .len = 2, .offset = 16 },
- { .id = TCP_URG_PTR, .len = 2, .offset = 18 },
+ { .id = UDP_SPORT, .len = 2, .offset = 0 },
+ { .id = UDP_DPORT, .len = 2, .offset = 2 },
+ { .id = UDP_LEN, .len = 2, .offset = 4 },
+ { .id = UDP_CSUM, .len = 2, .offset = 6 },
};
static void udp_header_init(struct proto_hdr *hdr)
@@ -61,12 +41,24 @@ static void udp_packet_finish(struct proto_hdr *hdr)
if (proto_field_is_set(hdr, UDP_CSUM))
return;
- if (!lower || lower->id != PROTO_IP4)
+ if (!lower)
return;
total_len = proto_field_get_u16(hdr, UDP_LEN);
- csum = p4_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
- total_len, IPPROTO_UDP);
+
+ switch (lower->id) {
+ case PROTO_IP4:
+ csum = p4_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
+ total_len, IPPROTO_UDP);
+ break;
+ case PROTO_IP6:
+ csum = p6_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
+ total_len, IPPROTO_UDP);
+ break;
+ default:
+ csum = 0;
+ break;
+ }
proto_field_set_be16(hdr, UDP_CSUM, bswap_16(csum));
}
@@ -78,6 +70,26 @@ static struct proto_hdr udp_hdr = {
.packet_finish = udp_packet_finish,
};
+static struct proto_field tcp_fields[] = {
+ { .id = TCP_SPORT, .len = 2, .offset = 0 },
+ { .id = TCP_DPORT, .len = 2, .offset = 2 },
+ { .id = TCP_SEQ, .len = 4, .offset = 4 },
+ { .id = TCP_ACK_SEQ, .len = 4, .offset = 8 },
+ { .id = TCP_DOFF, .len = 2, .offset = 12, .shift = 12, .mask = 0xf000 },
+ /* reserved (4 bits) */
+ { .id = TCP_CWR, .len = 2, .offset = 12, .shift = 7, .mask = 0x0080 },
+ { .id = TCP_ECE, .len = 2, .offset = 12, .shift = 6, .mask = 0x0040 },
+ { .id = TCP_URG, .len = 2, .offset = 12, .shift = 5, .mask = 0x0020 },
+ { .id = TCP_ACK, .len = 2, .offset = 12, .shift = 4, .mask = 0x0010 },
+ { .id = TCP_PSH, .len = 2, .offset = 12, .shift = 3, .mask = 0x0008 },
+ { .id = TCP_RST, .len = 2, .offset = 12, .shift = 2, .mask = 0x0004 },
+ { .id = TCP_SYN, .len = 2, .offset = 12, .shift = 1, .mask = 0x0002 },
+ { .id = TCP_FIN, .len = 2, .offset = 12, .shift = 0, .mask = 0x0001 },
+ { .id = TCP_WINDOW, .len = 2, .offset = 14 },
+ { .id = TCP_CSUM, .len = 2, .offset = 16 },
+ { .id = TCP_URG_PTR, .len = 2, .offset = 18 },
+};
+
static void tcp_header_init(struct proto_hdr *hdr)
{
proto_lower_default_add(hdr, PROTO_IP4);
@@ -97,12 +109,24 @@ static void tcp_packet_finish(struct proto_hdr *hdr)
if (proto_field_is_set(hdr, TCP_CSUM))
return;
- if (!lower || lower->id != PROTO_IP4)
+ if (!lower)
return;
total_len = pkt->len - hdr->pkt_offset;
- csum = p4_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
- total_len, IPPROTO_TCP);
+
+ switch (lower->id) {
+ case PROTO_IP4:
+ csum = p4_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
+ total_len, IPPROTO_TCP);
+ break;
+ case PROTO_IP6:
+ csum = p6_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr),
+ total_len, IPPROTO_TCP);
+ break;
+ default:
+ csum = 0;
+ break;
+ }
proto_field_set_be16(hdr, TCP_CSUM, bswap_16(csum));
}