From f2288ee39b7de0caf134dd72996aa9919b3b3e06 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Thu, 11 Apr 2024 17:12:55 +0200 Subject: [PATCH] decode/icmpv4: store embedded ip4h ptr as offset Reduces direct pointer usage and reduces Packet size. Ticket: #6938. --- src/decode-icmpv4.c | 4 +++- src/decode-icmpv4.h | 10 ++++------ src/decode.h | 7 +++++++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/decode-icmpv4.c b/src/decode-icmpv4.c index d167b97337..cd5708a626 100644 --- a/src/decode-icmpv4.c +++ b/src/decode-icmpv4.c @@ -69,7 +69,9 @@ static int DecodePartialIPV4(Packet* p, uint8_t* partial_packet, uint16_t len) } /** We need to fill icmpv4vars */ - p->l4.vars.icmpv4.emb_ipv4h = icmp4_ip4h; + const uint8_t *icmpv4_ptr = (const uint8_t *)p->l4.hdrs.icmpv4h; + DEBUG_VALIDATE_BUG_ON((ptrdiff_t)(partial_packet - icmpv4_ptr) > (ptrdiff_t)UINT16_MAX); + p->l4.vars.icmpv4.emb_ip4h_offset = (uint16_t)(partial_packet - icmpv4_ptr); switch (IPV4_GET_RAW_IPPROTO(icmp4_ip4h)) { case IPPROTO_TCP: diff --git a/src/decode-icmpv4.h b/src/decode-icmpv4.h index a87e7a09b1..920d896f92 100644 --- a/src/decode-icmpv4.h +++ b/src/decode-icmpv4.h @@ -181,11 +181,10 @@ typedef struct ICMPV4ExtHdr_ /* ICMPv4 vars */ typedef struct ICMPV4Vars_ { - /** Pointers to the embedded packet headers */ - IPV4Hdr *emb_ipv4h; + uint16_t emb_ip4h_offset; - uint16_t id; - uint16_t seq; + uint16_t id; + uint16_t seq; /** Actual header length **/ uint16_t hlen; @@ -242,8 +241,7 @@ typedef struct ICMPV4Timestamp_ { /** macro for icmpv4 embedded "protocol" access */ #define ICMPV4_GET_EMB_PROTO(p) (p)->l4.vars.icmpv4.emb_ip4_proto -/** macro for icmpv4 embedded "ipv4h" header access */ -#define ICMPV4_GET_EMB_IPV4(p) (p)->l4.vars.icmpv4.emb_ipv4h + /** macro for icmpv4 header length */ #define ICMPV4_GET_HLEN_ICMPV4H(p) (p)->l4.vars.icmpv4.hlen diff --git a/src/decode.h b/src/decode.h index b7d00ef3a5..e8c86a13b0 100644 --- a/src/decode.h +++ b/src/decode.h @@ -845,6 +845,13 @@ static inline bool PacketIsICMPv4(const Packet *p) return p->l4.type == PACKET_L4_ICMPV4; } +static inline const IPV4Hdr *ICMPV4_GET_EMB_IPV4(const Packet *p) +{ + const uint8_t *start = (const uint8_t *)PacketGetICMPv4(p); + const uint8_t *ip = start + p->l4.vars.icmpv4.emb_ip4h_offset; + return (const IPV4Hdr *)ip; +} + static inline ICMPV6Hdr *PacketSetICMPv6(Packet *p, const uint8_t *buf) { DEBUG_VALIDATE_BUG_ON(p->l4.type != PACKET_L4_UNKNOWN); -- 2.47.2