Coverage Report

Created: 2026-06-30 07:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/detect-ipv4hdr.c
Line
Count
Source
1
/* Copyright (C) 2007-2019 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17
18
/**
19
 * \file
20
 *
21
 * \author Victor Julien <victor@inliniac.net>
22
 *
23
 */
24
25
#include "suricata-common.h"
26
27
#include "detect.h"
28
#include "detect-parse.h"
29
#include "detect-engine.h"
30
#include "detect-engine-mpm.h"
31
#include "detect-engine-prefilter.h"
32
#include "detect-engine-content-inspection.h"
33
#include "detect-fast-pattern.h"
34
#include "detect-ipv4hdr.h"
35
36
/* prototypes */
37
static int DetectIpv4hdrSetup (DetectEngineCtx *, Signature *, const char *);
38
#ifdef UNITTESTS
39
void DetectIpv4hdrRegisterTests (void);
40
#endif
41
42
static int g_ipv4hdr_buffer_id = 0;
43
44
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
45
        const DetectEngineTransforms *transforms, Packet *p, const int list_id);
46
47
/**
48
 * \brief Registration function for ipv4.hdr: keyword
49
 */
50
void DetectIpv4hdrRegister(void)
51
34
{
52
34
    sigmatch_table[DETECT_IPV4HDR].name = "ipv4.hdr";
53
34
    sigmatch_table[DETECT_IPV4HDR].desc = "sticky buffer to match on the IPV4 header";
54
34
    sigmatch_table[DETECT_IPV4HDR].url = "/rules/header-keywords.html#ipv4hdr";
55
34
    sigmatch_table[DETECT_IPV4HDR].Setup = DetectIpv4hdrSetup;
56
34
    sigmatch_table[DETECT_IPV4HDR].flags |= SIGMATCH_NOOPT | SIGMATCH_INFO_STICKY_BUFFER;
57
#ifdef UNITTESTS
58
    sigmatch_table[DETECT_IPV4HDR].RegisterTests = DetectIpv4hdrRegisterTests;
59
#endif
60
61
34
    g_ipv4hdr_buffer_id = DetectBufferTypeRegister("ipv4.hdr");
62
34
    BUG_ON(g_ipv4hdr_buffer_id < 0);
63
64
34
    DetectBufferTypeSupportsPacket("ipv4.hdr");
65
66
34
    DetectPktMpmRegister("ipv4.hdr", 2, PrefilterGenericMpmPktRegister, GetData);
67
68
34
    DetectPktInspectEngineRegister("ipv4.hdr", GetData,
69
34
            DetectEngineInspectPktBufferGeneric);
70
71
34
    return;
72
34
}
73
74
/**
75
 * \brief setup ipv4.hdr sticky buffer
76
 *
77
 * \param de_ctx pointer to the Detection Engine Context
78
 * \param s pointer to the Current Signature
79
 * \param _unused unused
80
 *
81
 * \retval 0 on Success
82
 * \retval -1 on Failure
83
 */
84
static int DetectIpv4hdrSetup (DetectEngineCtx *de_ctx, Signature *s, const char *_unused)
85
24.7k
{
86
24.7k
    s->proto.flags |= DETECT_PROTO_IPV4; // TODO
87
88
24.7k
    s->flags |= SIG_FLAG_REQUIRE_PACKET;
89
90
24.7k
    if (DetectBufferSetActiveList(de_ctx, s, g_ipv4hdr_buffer_id) < 0)
91
12
        return -1;
92
93
24.7k
    return 0;
94
24.7k
}
95
96
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
97
        const DetectEngineTransforms *transforms, Packet *p, const int list_id)
98
55.5k
{
99
55.5k
    SCEnter();
100
101
55.5k
    InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
102
55.5k
    if (buffer->inspect == NULL) {
103
55.1k
        if (p->ip4h == NULL) {
104
            // DETECT_PROTO_IPV4 does not prefilter
105
716
            return NULL;
106
716
        }
107
54.3k
        uint32_t hlen = IPV4_GET_HLEN(p);
108
54.3k
        if (((uint8_t *)p->ip4h + (ptrdiff_t)hlen) >
109
54.3k
                ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)))
110
0
        {
111
0
            SCLogDebug("data out of range: %p > %p",
112
0
                    ((uint8_t *)p->ip4h + (ptrdiff_t)hlen),
113
0
                    ((uint8_t *)GET_PKT_DATA(p) + (ptrdiff_t)GET_PKT_LEN(p)));
114
0
            return NULL;
115
0
        }
116
117
54.3k
        const uint32_t data_len = hlen;
118
54.3k
        const uint8_t *data = (const uint8_t *)p->ip4h;
119
120
54.3k
        InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
121
54.3k
        InspectionBufferApplyTransforms(buffer, transforms);
122
54.3k
    }
123
124
54.7k
    return buffer;
125
55.5k
}
126
127
#ifdef UNITTESTS
128
#include "tests/detect-ipv4hdr.c"
129
#endif