summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVadim Kochan <vadim4j@gmail.com>2016-01-29 00:06:26 +0200
committerTobias Klauser <tklauser@distanz.ch>2016-01-29 08:59:25 +0100
commitd275f16591ca44d91ee4e56d65553529a429a62d (patch)
tree01b3095e29d9b13c5feceeb1b563f41e3f1f56f4
parentbe21df123309c83cd47a1fa5f2c0ee1cf4657810 (diff)
trafgen: l4: Add UDP header generation logic
Add trafgen_l4.c module with generation UDP header fields. UDP protocol generation logic automaticaly sets the protocol field of the lower level protocol to IPPROTO_UDP by default (if it is IPv4). Also checksum & length are calculated and set if it is not given by the user. Signed-off-by: Vadim Kochan <vadim4j@gmail.com> Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
-rw-r--r--trafgen/Makefile1
-rw-r--r--trafgen_l4.c71
-rw-r--r--trafgen_l4.h13
-rw-r--r--trafgen_proto.c2
4 files changed, 87 insertions, 0 deletions
diff --git a/trafgen/Makefile b/trafgen/Makefile
index 4f342ca..3f78f07 100644
--- a/trafgen/Makefile
+++ b/trafgen/Makefile
@@ -22,6 +22,7 @@ trafgen-objs = xmalloc.o \
trafgen_proto.o \
trafgen_l2.o \
trafgen_l3.o \
+ trafgen_l4.o \
trafgen_lexer.yy.o \
trafgen_parser.tab.o \
trafgen.o
diff --git a/trafgen_l4.c b/trafgen_l4.c
new file mode 100644
index 0000000..286e54a
--- /dev/null
+++ b/trafgen_l4.c
@@ -0,0 +1,71 @@
+/*
+ * netsniff-ng - the packet sniffing beast
+ * Subject to the GPL, version 2.
+ */
+
+#include <stdbool.h>
+#include <netinet/in.h>
+
+#include "die.h"
+#include "csum.h"
+#include "built_in.h"
+#include "trafgen_l3.h"
+#include "trafgen_l4.h"
+#include "trafgen_conf.h"
+#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 void udp_header_init(struct proto_hdr *hdr)
+{
+ struct proto_hdr *lower;
+
+ proto_lower_default_add(PROTO_IP4);
+
+ lower = proto_current_header();
+
+ if (lower->id == PROTO_IP4)
+ proto_field_set_default_u8(lower, IP4_PROTO, IPPROTO_UDP);
+
+ proto_header_fields_add(hdr, udp_fields, array_size(udp_fields));
+}
+
+static void udp_packet_finish(struct proto_hdr *hdr)
+{
+ struct proto_hdr *lower = proto_lower_header(hdr);
+ struct packet *pkt = current_packet();
+ uint16_t total_len;
+ uint16_t csum;
+
+ total_len = pkt->len - hdr->pkt_offset;
+ proto_field_set_default_be16(hdr, UDP_LEN, total_len);
+
+ if (proto_field_is_set(hdr, UDP_CSUM))
+ return;
+
+ if (!lower || lower->id != PROTO_IP4)
+ 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);
+
+ proto_field_set_be16(hdr, UDP_CSUM, bswap_16(csum));
+}
+
+static struct proto_hdr udp_hdr = {
+ .id = PROTO_UDP,
+ .layer = PROTO_L4,
+ .header_init = udp_header_init,
+ .packet_finish = udp_packet_finish,
+};
+
+void protos_l4_init(void)
+{
+ proto_header_register(&udp_hdr);
+}
diff --git a/trafgen_l4.h b/trafgen_l4.h
new file mode 100644
index 0000000..4651009
--- /dev/null
+++ b/trafgen_l4.h
@@ -0,0 +1,13 @@
+#ifndef TRAFGEN_L4_H
+#define TRAFGEN_L4_H
+
+enum udp_field {
+ UDP_SPORT,
+ UDP_DPORT,
+ UDP_LEN,
+ UDP_CSUM,
+};
+
+extern void protos_l4_init(void);
+
+#endif /* TRAFGEN_L4_H */
diff --git a/trafgen_proto.c b/trafgen_proto.c
index 75556f9..5fcb5cc 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -13,6 +13,7 @@
#include "trafgen_conf.h"
#include "trafgen_l2.h"
#include "trafgen_l3.h"
+#include "trafgen_l4.h"
#include "trafgen_proto.h"
#define field_shift_and_mask(f, v) (((v) << (f)->shift) & \
@@ -362,6 +363,7 @@ void protos_init(const char *dev)
protos_l2_init();
protos_l3_init();
+ protos_l4_init();
for (p = registered; p; p = p->next)
p->ctx = &ctx;