]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
decode/icmpv4: store embedded ip4h ptr as offset
authorVictor Julien <vjulien@oisf.net>
Thu, 11 Apr 2024 15:12:55 +0000 (17:12 +0200)
committerVictor Julien <victor@inliniac.net>
Fri, 26 Apr 2024 18:59:45 +0000 (20:59 +0200)
Reduces direct pointer usage and reduces Packet size.

Ticket: #6938.

src/decode-icmpv4.c
src/decode-icmpv4.h
src/decode.h

index d167b9733792243e35175121e98f1ea5cbbb0c38..cd5708a62626a56716970a6e744a0dd81bb12599 100644 (file)
@@ -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:
index a87e7a09b1e545cc6768b72dbaa8bc428b2f0d9e..920d896f928cb8db87077263729afdb56a1a2826 100644 (file)
@@ -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
 
index b7d00ef3a513e3e660b969f4d5159883151119e3..e8c86a13b051fdcb03301d1d92983f523e81814b 100644 (file)
@@ -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);