Update UDP csum field at runtime if:
1) UDP field was changed.
2) IPv4/6 source/destination addresses were changed
(which is a part of UDP pseudo header), this is
handled by IPv4/6 protocols.
Also changed proto_lower_header(...) function to use header index
to lookup lower header faster, the reason is that this function
is used for updating UDP csum.
Signed-off-by: Vadim Kochan <[email protected]>
---
trafgen_l3.c | 18 ++++++++++++++++++
trafgen_l4.c | 37 +++++++++++++++++++++++++++++--------
trafgen_proto.c | 36 +++++++++++++++++++++++-------------
trafgen_proto.h | 2 ++
4 files changed, 72 insertions(+), 21 deletions(-)
diff --git a/trafgen_l3.c b/trafgen_l3.c
index aaca5a9..dbfb1fe 100644
--- a/trafgen_l3.c
+++ b/trafgen_l3.c
@@ -46,6 +46,13 @@ static void ipv4_header_init(struct proto_hdr *hdr)
static void ipv4_field_changed(struct proto_hdr *hdr, struct proto_field
*field)
{
hdr->is_csum_valid = false;
+
+ if (field->id == IP4_SADDR || field->id == IP4_DADDR) {
+ struct proto_hdr *upper = proto_upper_header(hdr);
+
+ if (upper && upper->id == PROTO_UDP)
+ upper->is_csum_valid = false;
+ }
}
static void ipv4_csum_update(struct proto_hdr *hdr)
@@ -136,6 +143,16 @@ static void ipv6_header_init(struct proto_hdr *hdr)
proto_field_set_default_dev_ipv6(hdr, IP6_SADDR);
}
+static void ipv6_field_changed(struct proto_hdr *hdr, struct proto_field
*field)
+{
+ if (field->id == IP6_SADDR || field->id == IP6_DADDR) {
+ struct proto_hdr *upper = proto_upper_header(hdr);
+
+ if (upper && upper->id == PROTO_UDP)
+ upper->is_csum_valid = false;
+ }
+}
+
#define IPV6_HDR_LEN 40
static void ipv6_packet_finish(struct proto_hdr *hdr)
@@ -171,6 +188,7 @@ static struct proto_hdr ipv6_hdr = {
.id = PROTO_IP6,
.layer = PROTO_L3,
.header_init = ipv6_header_init,
+ .field_changed = ipv6_field_changed,
.packet_finish = ipv6_packet_finish,
.set_next_proto = ipv6_set_next_proto,
};
diff --git a/trafgen_l4.c b/trafgen_l4.c
index 908641c..5749a82 100644
--- a/trafgen_l4.c
+++ b/trafgen_l4.c
@@ -28,22 +28,29 @@ static void udp_header_init(struct proto_hdr *hdr)
proto_header_fields_add(hdr, udp_fields, array_size(udp_fields));
}
-static void udp_packet_finish(struct proto_hdr *hdr)
+static void udp_field_changed(struct proto_hdr *hdr, struct proto_field *field)
{
- struct proto_hdr *lower = proto_lower_header(hdr);
- struct packet *pkt = current_packet();
+ hdr->is_csum_valid = false;
+}
+
+static void udp_csum_update(struct proto_hdr *hdr)
+{
+ struct proto_hdr *lower;
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 (hdr->is_csum_valid)
+ return;
if (proto_field_is_set(hdr, UDP_CSUM))
return;
-
+ lower = proto_lower_header(hdr);
if (!lower)
return;
+ total_len = packet_get(hdr->pkt_id)->len - hdr->pkt_offset;
+
+ proto_field_set_default_be16(hdr, UDP_CSUM, 0);
+
switch (lower->id) {
case PROTO_IP4:
csum = p4_csum((void *) proto_header_ptr(lower),
proto_header_ptr(hdr),
@@ -58,14 +65,28 @@ static void udp_packet_finish(struct proto_hdr *hdr)
break;
}
- proto_field_set_be16(hdr, UDP_CSUM, bswap_16(csum));
+ proto_field_set_default_be16(hdr, UDP_CSUM, bswap_16(csum));
+ hdr->is_csum_valid = true;
+}
+
+static void udp_packet_finish(struct proto_hdr *hdr)
+{
+ struct packet *pkt = current_packet();
+ uint16_t total_len;
+
+ total_len = pkt->len - hdr->pkt_offset;
+ proto_field_set_default_be16(hdr, UDP_LEN, total_len);
+
+ udp_csum_update(hdr);
}
static struct proto_hdr udp_hdr = {
.id = PROTO_UDP,
.layer = PROTO_L4,
.header_init = udp_header_init,
+ .packet_update = udp_csum_update,
.packet_finish = udp_packet_finish,
+ .field_changed = udp_field_changed,
};
static struct proto_field tcp_fields[] = {
diff --git a/trafgen_proto.c b/trafgen_proto.c
index f57d390..9a26f04 100644
--- a/trafgen_proto.c
+++ b/trafgen_proto.c
@@ -39,20 +39,25 @@ static inline struct proto_hdr *proto_current_header(void)
struct proto_hdr *proto_lower_header(struct proto_hdr *hdr)
{
- struct proto_hdr **headers = ¤t_packet()->headers[0];
- size_t headers_count = current_packet()->headers_count;
- struct proto_hdr *lower = NULL;
- size_t i;
+ struct packet *pkt = packet_get(hdr->pkt_id);
+ struct proto_hdr **headers = &pkt->headers[0];
- if (headers_count == 0)
+ if (hdr->index == 0)
return NULL;
- for (i = 1, lower = headers[0]; i < headers_count; i++) {
- if (headers[i] == hdr)
- return headers[i - 1];
- }
+ return headers[hdr->index - 1];
+}
+
+struct proto_hdr *proto_upper_header(struct proto_hdr *hdr)
+{
+ struct packet *pkt = packet_get(hdr->pkt_id);
+ struct proto_hdr **headers = &pkt->headers[0];
+ size_t headers_count = pkt->headers_count;
+
+ if (hdr->index == headers_count - 1)
+ return NULL;
- return lower;
+ return headers[hdr->index + 1];
}
uint8_t *proto_header_ptr(struct proto_hdr *hdr)
@@ -130,11 +135,12 @@ bool proto_field_is_set(struct proto_hdr *hdr, uint32_t
fid)
struct proto_hdr *proto_header_init(enum proto_id pid)
{
- struct proto_hdr **headers = ¤t_packet()->headers[0];
+ struct packet *pkt = current_packet();
+ struct proto_hdr **headers = &pkt->headers[0];
struct proto_hdr *hdr = proto_header_by_id(pid);
struct proto_hdr *new_hdr;
- bug_on(current_packet()->headers_count >= PROTO_MAX_LAYERS);
+ bug_on(pkt->headers_count >= PROTO_MAX_LAYERS);
new_hdr = xmalloc(sizeof(*new_hdr));
memcpy(new_hdr, hdr, sizeof(*new_hdr));
@@ -144,7 +150,11 @@ struct proto_hdr *proto_header_init(enum proto_id pid)
if (new_hdr->header_init)
new_hdr->header_init(new_hdr);
- headers[current_packet()->headers_count++] = new_hdr;
+ /* This is very important to have it after header_init as
+ * pkt->headers_count might be changed by adding default lower headers
*/
+ new_hdr->index = pkt->headers_count;
+
+ headers[pkt->headers_count++] = new_hdr;
return new_hdr;
}
diff --git a/trafgen_proto.h b/trafgen_proto.h
index eb11896..dd3d0e8 100644
--- a/trafgen_proto.h
+++ b/trafgen_proto.h
@@ -68,6 +68,7 @@ struct proto_field {
struct proto_hdr {
enum proto_id id;
enum proto_layer layer;
+ uint32_t index;
struct proto_hdr *next;
struct proto_ctx *ctx;
@@ -99,6 +100,7 @@ extern struct proto_hdr *proto_lower_default_add(struct
proto_hdr *hdr,
enum proto_id pid);
extern struct proto_hdr *proto_lower_header(struct proto_hdr *hdr);
+extern struct proto_hdr *proto_upper_header(struct proto_hdr *hdr);
extern uint8_t *proto_header_ptr(struct proto_hdr *hdr);
extern void proto_header_fields_add(struct proto_hdr *hdr,
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.