diff mbox

[ovs-dev,08/10] packets: Add support for modifying ICMP type and code.

Message ID 1445412271-22019-8-git-send-email-jpettit@nicira.com
State Deferred
Headers show

Commit Message

Justin Pettit Oct. 21, 2015, 7:24 a.m. UTC
Signed-off-by: Justin Pettit <jpettit@nicira.com>
---
 lib/packets.c |   18 ++++++++++++++++++
 lib/packets.h |    1 +
 2 files changed, 19 insertions(+), 0 deletions(-)
diff mbox

Patch

diff --git a/lib/packets.c b/lib/packets.c
index 342d8b7..0303801 100644
--- a/lib/packets.c
+++ b/lib/packets.c
@@ -905,6 +905,24 @@  packet_set_sctp_port(struct dp_packet *packet, ovs_be16 src, ovs_be16 dst)
     put_16aligned_be32(&sh->sctp_csum, old_csum ^ old_correct_csum ^ new_csum);
 }
 
+/* Sets the ICMP type and code of the ICMP header contained in 'packet'.
+ * 'packet' must be a valid ICMP packet with its l4 offset properly
+ * populated. */
+void
+packet_set_icmp(struct dp_packet *packet, uint8_t type, uint8_t code)
+{
+    struct icmp_header *ih = dp_packet_l4(packet);
+    uint16_t orig_tc = htons(ih->icmp_type << 8 | ih->icmp_code);
+    uint16_t new_tc = htons(type << 8 | code);
+
+    if (orig_tc != new_tc) {
+        ih->icmp_type = type;
+        ih->icmp_code = code;
+
+        ih->icmp_csum = recalc_csum16(ih->icmp_csum, orig_tc, new_tc);
+    }
+}
+
 void
 packet_set_nd(struct dp_packet *packet, const ovs_be32 target[4],
               const struct eth_addr sll, const struct eth_addr tll) {
diff --git a/lib/packets.h b/lib/packets.h
index 67f635e..de8d4b6 100644
--- a/lib/packets.h
+++ b/lib/packets.h
@@ -975,6 +975,7 @@  void packet_set_ipv6(struct dp_packet *, uint8_t proto, const ovs_be32 src[4],
 void packet_set_tcp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
 void packet_set_udp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
 void packet_set_sctp_port(struct dp_packet *, ovs_be16 src, ovs_be16 dst);
+void packet_set_icmp(struct dp_packet *, uint8_t type, uint8_t code);
 void packet_set_nd(struct dp_packet *, const ovs_be32 target[4],
                    const struct eth_addr sll, const struct eth_addr tll);