diff options
-rw-r--r-- | trafgen_l3.c | 18 | ||||
-rw-r--r-- | trafgen_l4.c | 37 |
2 files changed, 47 insertions, 8 deletions
diff --git a/trafgen_l3.c b/trafgen_l3.c index 9bb0de9..91052a0 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_field *field) { field->hdr->is_csum_valid = false; + + if (field->id == IP4_SADDR || field->id == IP4_DADDR) { + struct proto_hdr *upper = proto_upper_header(field->hdr); + + if (upper && upper->ops->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_field *field) +{ + if (field->id == IP6_SADDR || field->id == IP6_DADDR) { + struct proto_hdr *upper = proto_upper_header(field->hdr); + + if (upper && upper->ops->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 const struct proto_ops ipv6_proto_ops = { .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 7ec017e..ee8deb4 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_field *field) { - struct proto_hdr *lower = proto_lower_header(hdr); - struct packet *pkt = current_packet(); + field->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->ops->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 const struct proto_ops udp_proto_ops = { .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[] = { |