Update TCP checksum field if any TCP of fields was changed. Use same checksum update function on 'packet_finish' and 'header update'.
Set 'is_csum_valid = false' if lower IPv4/IPv6 pseudo header was changed. Signed-off-by: Vadim Kochan <vadi...@gmail.com> --- trafgen_l3.c | 4 ++-- trafgen_l4.c | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/trafgen_l3.c b/trafgen_l3.c index 476f5df..03645a9 100644 --- a/trafgen_l3.c +++ b/trafgen_l3.c @@ -50,7 +50,7 @@ static void ipv4_field_changed(struct proto_hdr *hdr, struct proto_field *field) if (field->id == IP4_SADDR || field->id == IP4_DADDR) { struct proto_hdr *upper = proto_upper_header(hdr); - if (upper && upper->id == PROTO_UDP) + if (upper && (upper->id == PROTO_UDP || upper->id == PROTO_TCP)) upper->is_csum_valid = false; } } @@ -148,7 +148,7 @@ 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) + if (upper && (upper->id == PROTO_UDP || upper->id == PROTO_TCP)) upper->is_csum_valid = false; } } diff --git a/trafgen_l4.c b/trafgen_l4.c index 5749a82..3606e0f 100644 --- a/trafgen_l4.c +++ b/trafgen_l4.c @@ -118,13 +118,20 @@ static void tcp_header_init(struct proto_hdr *hdr) proto_field_set_default_be16(hdr, TCP_DOFF, 5); } -static void tcp_packet_finish(struct proto_hdr *hdr) +static void tcp_field_changed(struct proto_hdr *hdr, struct proto_field *field) +{ + hdr->is_csum_valid = false; +} + +static void tcp_csum_update(struct proto_hdr *hdr) { struct proto_hdr *lower = proto_lower_header(hdr); struct packet *pkt = current_packet(); uint16_t total_len; uint16_t csum; + if (hdr->is_csum_valid) + return; if (proto_field_is_set(hdr, TCP_CSUM)) return; @@ -133,6 +140,8 @@ static void tcp_packet_finish(struct proto_hdr *hdr) total_len = pkt->len - hdr->pkt_offset; + proto_field_set_default_be16(hdr, TCP_CSUM, 0); + switch (lower->id) { case PROTO_IP4: csum = p4_csum((void *) proto_header_ptr(lower), proto_header_ptr(hdr), @@ -147,14 +156,17 @@ static void tcp_packet_finish(struct proto_hdr *hdr) break; } - proto_field_set_be16(hdr, TCP_CSUM, bswap_16(csum)); + proto_field_set_default_be16(hdr, TCP_CSUM, bswap_16(csum)); + hdr->is_csum_valid = true; } static struct proto_hdr tcp_hdr = { .id = PROTO_TCP, .layer = PROTO_L4, .header_init = tcp_header_init, - .packet_finish = tcp_packet_finish, + .packet_update = tcp_csum_update, + .packet_finish = tcp_csum_update, + .field_changed = tcp_field_changed, }; static struct proto_field icmpv4_fields[] = { -- 2.9.2 -- 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.