summaryrefslogtreecommitdiff
path: root/trafgen_l4.c
diff options
context:
space:
mode:
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));
}