]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
decode/sctp: move sctph into L4 packet data
authorVictor Julien <vjulien@oisf.net>
Fri, 29 Mar 2024 18:42:22 +0000 (19:42 +0100)
committerVictor Julien <victor@inliniac.net>
Fri, 26 Apr 2024 18:59:45 +0000 (20:59 +0200)
Reduces Packet size.

Ticket: #6938.

src/decode-sctp.c
src/decode-sctp.h
src/decode.h
src/flow-util.c
src/packet.c
src/util-validate.h

index 9a6c4e8ead2a65a39c761902461611446656c940..381e935f3dc11967b9cb5c6c6cc320339f99c49c 100644 (file)
@@ -50,16 +50,12 @@ static int DecodeSCTPPacket(ThreadVars *tv, Packet *p, const uint8_t *pkt, uint1
         return -1;
     }
 
-    p->sctph = (SCTPHdr *)pkt;
-
-    SET_SCTP_SRC_PORT(p,&p->sp);
-    SET_SCTP_DST_PORT(p,&p->dp);
-
+    SCTPHdr *sctph = PacketSetSCTP(p, pkt);
+    p->sp = SCNtohs(sctph->sh_sport);
+    p->dp = SCNtohs(sctph->sh_dport);
     p->payload = (uint8_t *)pkt + sizeof(SCTPHdr);
     p->payload_len = len - sizeof(SCTPHdr);
-
     p->proto = IPPROTO_SCTP;
-
     return 0;
 }
 
@@ -69,14 +65,11 @@ int DecodeSCTP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p,
     StatsIncr(tv, dtv->counter_sctp);
 
     if (unlikely(DecodeSCTPPacket(tv, p,pkt,len) < 0)) {
-        CLEAR_SCTP_PACKET(p);
+        PacketClearL4(p);
         return TM_ECODE_FAILED;
     }
 
-#ifdef DEBUG
-    SCLogDebug("SCTP sp: %" PRIu32 " -> dp: %" PRIu32,
-        SCTP_GET_SRC_PORT(p), SCTP_GET_DST_PORT(p));
-#endif
+    SCLogDebug("SCTP sp: %u -> dp: %u", p->sp, p->dp);
 
     FlowSetupPacket(p);
 
index 6ec20cca331f45b0e7bfdaa005d002ef37057bc3..f83a4434ef056e4b31c18a63f4ed960e82b6479c 100644 (file)
 /** size of the packet header without any chunk headers */
 #define SCTP_HEADER_LEN                       12
 
-/* XXX RAW* needs to be really 'raw', so no SCNtohs there */
-#define SCTP_GET_RAW_SRC_PORT(sctph)          SCNtohs((sctph)->sh_sport)
-#define SCTP_GET_RAW_DST_PORT(sctph)          SCNtohs((sctph)->sh_dport)
-
-#define SCTP_GET_SRC_PORT(p)                  SCTP_GET_RAW_SRC_PORT(p->sctph)
-#define SCTP_GET_DST_PORT(p)                  SCTP_GET_RAW_DST_PORT(p->sctph)
-
 typedef struct SCTPHdr_
 {
     uint16_t sh_sport;     /* source port */
@@ -42,10 +35,6 @@ typedef struct SCTPHdr_
     uint32_t sh_sum;       /* checksum, computed via crc32 */
 } __attribute__((__packed__)) SCTPHdr;
 
-#define CLEAR_SCTP_PACKET(p) { \
-    (p)->sctph = NULL; \
-} while (0)
-
 void DecodeSCTPRegisterTests(void);
 
 #endif /* SURICATA_DECODE_SCTP_H */
index f547de957585aaf087b57f83a1563bb1af197303..039e5978f4e3cde1ecbaebea0efc470d6029c425 100644 (file)
@@ -198,17 +198,6 @@ typedef struct Address_ {
         SET_PORT(UDP_GET_DST_PORT((pkt)), *(prt)); \
     } while (0)
 
-/* Set the SCTP ports into the Ports of the Packet.
- * Make sure p->sctph is initialized and validated. */
-#define SET_SCTP_SRC_PORT(pkt, prt) do {            \
-        SET_PORT(SCTP_GET_SRC_PORT((pkt)), *(prt)); \
-    } while (0)
-
-#define SET_SCTP_DST_PORT(pkt, prt) do {            \
-        SET_PORT(SCTP_GET_DST_PORT((pkt)), *(prt)); \
-    } while (0)
-
-
 #define GET_IPV4_SRC_ADDR_U32(p) ((p)->src.addr_data32[0])
 #define GET_IPV4_DST_ADDR_U32(p) ((p)->dst.addr_data32[0])
 #define GET_IPV4_SRC_ADDR_PTR(p) ((p)->src.addr_data32)
@@ -437,9 +426,18 @@ struct PacketL3 {
     } vars;
 };
 
+enum PacketL4Types {
+    PACKET_L4_UNKNOWN = 0,
+    PACKET_L4_SCTP,
+};
+
 struct PacketL4 {
+    enum PacketL4Types type;
     bool csum_set;
     uint16_t csum;
+    union L4Hdrs {
+        SCTPHdr *sctph;
+    } hdrs;
 };
 
 /* sizes of the members:
@@ -577,7 +575,6 @@ typedef struct Packet_
 
     TCPHdr *tcph;
     UDPHdr *udph;
-    SCTPHdr *sctph;
     ESPHdr *esph;
     ICMPV4Hdr *icmpv4h;
     ICMPV6Hdr *icmpv6h;
@@ -779,6 +776,25 @@ static inline bool PacketIsICMPv6(const Packet *p)
     return PKT_IS_ICMPV6(p);
 }
 
+static inline SCTPHdr *PacketSetSCTP(Packet *p, const uint8_t *buf)
+{
+    DEBUG_VALIDATE_BUG_ON(p->l4.type != PACKET_L4_UNKNOWN);
+    p->l4.type = PACKET_L4_SCTP;
+    p->l4.hdrs.sctph = (SCTPHdr *)buf;
+    return p->l4.hdrs.sctph;
+}
+
+static inline const SCTPHdr *PacketGetSCTP(const Packet *p)
+{
+    DEBUG_VALIDATE_BUG_ON(p->l4.type != PACKET_L4_SCTP);
+    return p->l4.hdrs.sctph;
+}
+
+static inline bool PacketIsSCTP(const Packet *p)
+{
+    return p->l4.type == PACKET_L4_SCTP;
+}
+
 /** \brief Structure to hold thread specific data for all decode modules */
 typedef struct DecodeThreadVars_
 {
index 3cce866b3ed46b31f87bee0a1a00b61aa12ce3e9..53fc1058dcaf0a5f98d14ec99d2824e48313e3f2 100644 (file)
@@ -184,9 +184,9 @@ void FlowInit(Flow *f, const Packet *p)
         f->icmp_s.type = p->icmp_s.type;
         f->icmp_s.code = p->icmp_s.code;
         FlowSetICMPv6CounterPart(f);
-    } else if (p->sctph != NULL) { /* XXX MACRO */
-        SET_SCTP_SRC_PORT(p,&f->sp);
-        SET_SCTP_DST_PORT(p,&f->dp);
+    } else if (PacketIsSCTP(p)) {
+        f->sp = p->sp;
+        f->dp = p->dp;
     } else if (p->esph != NULL) {
         f->esp.spi = ESP_GET_SPI(p);
     } else {
index 9bdbe2677da02fc787f6e0c67e4f906d34f01a98..11e3837393211d39201fd4139e8fd29547186eac 100644 (file)
@@ -121,9 +121,6 @@ void PacketReinit(Packet *p)
     if (p->udph != NULL) {
         CLEAR_UDP_PACKET(p);
     }
-    if (p->sctph != NULL) {
-        CLEAR_SCTP_PACKET(p);
-    }
     if (p->esph != NULL) {
         CLEAR_ESP_PACKET(p);
     }
index 7d543404f1dbcf54bb137048bb6a0836397a3685..2c94190e055bae1b913af735adc3c748de4bab81 100644 (file)
@@ -79,7 +79,7 @@
                 } else if ((p)->proto == IPPROTO_ICMP) {                                           \
                     BUG_ON((p)->icmpv4h == NULL);                                                  \
                 } else if ((p)->proto == IPPROTO_SCTP) {                                           \
-                    BUG_ON((p)->sctph == NULL);                                                    \
+                    BUG_ON(PacketGetSCTP((p)) == NULL);                                            \
                 } else if ((p)->proto == IPPROTO_ICMPV6) {                                         \
                     BUG_ON((p)->icmpv6h == NULL);                                                  \
                 }                                                                                  \