Add trafgen_l4.c module with implementation
of UDP header fields gneration.

UDP proto generation logic automaticaly sets by default
IPPROTO_UDP to the lower proto if it is IPv4, also cscum is
calculated if it is not set by user.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 trafgen/Makefile |  1 +
 trafgen_l4.c     | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 trafgen_l4.h     | 14 +++++++++++
 trafgen_proto.c  |  2 ++
 4 files changed, 88 insertions(+)
 create mode 100644 trafgen_l4.c
 create mode 100644 trafgen_l4.h

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..1a60ea5
--- /dev/null
+++ b/trafgen_l4.h
@@ -0,0 +1,14 @@
+#ifndef TRAFGEN_L4_I_H
+#define TRAFGEN_L4_I_H
+
+enum udp_field {
+       UDP_SPORT,
+       UDP_DPORT,
+       UDP_LEN,
+       UDP_CSUM,
+};
+
+extern void protos_l4_init(void);
+
+#endif /* TRAFGEN_L4_I_H */
+
diff --git a/trafgen_proto.c b/trafgen_proto.c
index e803f27..a36c0af 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) & \
@@ -367,6 +368,7 @@ void protos_init(char *dev)
 
        protos_l2_init();
        protos_l3_init();
+       protos_l4_init();
 
        for (p = registered; p; p = p->next)
                p->ctx = &ctx;
-- 
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