diff options
author | Vadim Kochan <vadim4j@gmail.com> | 2016-08-13 02:11:18 +0300 |
---|---|---|
committer | Tobias Klauser <tklauser@distanz.ch> | 2016-09-21 09:56:55 +0200 |
commit | 553a9c65beeb77072f08e4d8cd876b062dcdb193 (patch) | |
tree | bfd6b45eb220a1dc0168ff879780667a58bfd2b8 /trafgen_l4.c | |
parent | 88169061a70a2363f503f14e686e2b070e6e63ef (diff) |
trafgen: udp: Update csum at runtime if needed
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.
Signed-off-by: Vadim Kochan <vadim4j@gmail.com>
Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Diffstat (limited to 'trafgen_l4.c')
-rw-r--r-- | trafgen_l4.c | 37 |
1 files changed, 29 insertions, 8 deletions
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[] = { |