• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.1
/src/decode-esp.c
1
/* Copyright (C) 2020-2021 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
 * \ingroup decode
20
 *
21
 * @{
22
 */
23

24
/**
25
 * \file
26
 *
27
 * Decode Encapsulating Security Payload (ESP)
28
 */
29

30
#include "suricata-common.h"
31
#include "decode-esp.h"
32
#include "flow.h"
33

34
#include "util-validate.h"
35

36
static int DecodeESPPacket(ThreadVars *tv, Packet *p, const uint8_t *pkt, uint16_t len)
37
{
886✔
38
    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
886✔
39

40
    if (unlikely(len < ESP_HEADER_LEN)) {
886✔
41
        ENGINE_SET_INVALID_EVENT(p, ESP_PKT_TOO_SMALL);
439✔
42
        return -1;
439✔
43
    }
439✔
44

45
    (void)PacketSetESP(p, pkt);
447✔
46

47
    p->payload = (uint8_t *)pkt + sizeof(ESPHdr);
447✔
48
    p->payload_len = len - sizeof(ESPHdr);
447✔
49

50
    p->proto = IPPROTO_ESP;
447✔
51

52
    return 0;
447✔
53
}
886✔
54

55
/**
56
 * \brief Function to decode IPSEC-ESP packets
57
 * \param tv thread vars
58
 * \param dtv decoder thread vars
59
 * \param p packet
60
 * \param pkt raw packet data
61
 * \param len length in bytes of pkt array
62
 * \retval TM_ECODE_OK or TM_ECODE_FAILED on serious error
63
 */
64
int DecodeESP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
65
{
916✔
66
    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
916✔
67

68
    StatsCounterIncr(&tv->stats, dtv->counter_esp);
916✔
69

70
    if (!PacketIncreaseCheckLayers(p)) {
916✔
71
        return TM_ECODE_FAILED;
30✔
72
    }
30✔
73
    if (unlikely(DecodeESPPacket(tv, p, pkt, len) < 0)) {
886✔
74
        PacketClearL4(p);
439✔
75
        return TM_ECODE_FAILED;
439✔
76
    }
439✔
77

78
    SCLogDebug("ESP spi: %" PRIu32 " sequence: %" PRIu32, ESP_GET_SPI(PacketGetESP(p)),
447✔
79
            ESP_GET_SEQUENCE(PacketGetESP(p)));
447✔
80

81
    FlowSetupPacket(p);
447✔
82

83
    return TM_ECODE_OK;
447✔
84
}
886✔
85

86
#ifdef UNITTESTS
87

88
#include "util-unittest.h"
89

90
/** \test Successful decoding */
91
static int DecodeESPTest01(void)
92
{
93
    uint8_t raw_esp[] = { 0x00, 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x08 };
94

95
    Packet *p = PacketGetFromAlloc();
96
    FAIL_IF_NULL(p);
97

98
    ThreadVars tv;
99
    DecodeThreadVars dtv;
100

101
    memset(&tv, 0, sizeof(ThreadVars));
102
    memset(&dtv, 0, sizeof(DecodeThreadVars));
103

104
    int ret = DecodeESP(&tv, &dtv, p, raw_esp, sizeof(raw_esp));
105
    FAIL_IF(ret != TM_ECODE_OK);
106

107
    FAIL_IF(p->proto != IPPROTO_ESP);
108
    FAIL_IF(p->payload_len != sizeof(raw_esp) - ESP_HEADER_LEN);
109
    FAIL_IF(ESP_GET_SPI(PacketGetESP(p)) != 0x7b);
110
    FAIL_IF(ESP_GET_SEQUENCE(PacketGetESP(p)) != 0x08);
111

112
    PacketFree(p);
113

114
    PASS;
115
}
116

117
/** \test Successful decoding, with payload data */
118
static int DecodeESPTest02(void)
119
{
120
    uint8_t raw_esp[] = { 0x00, 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x08, 0xFF, 0xFF };
121

122
    Packet *p = PacketGetFromAlloc();
123
    FAIL_IF_NULL(p);
124

125
    ThreadVars tv;
126
    DecodeThreadVars dtv;
127

128
    memset(&tv, 0, sizeof(ThreadVars));
129
    memset(&dtv, 0, sizeof(DecodeThreadVars));
130

131
    int ret = DecodeESP(&tv, &dtv, p, raw_esp, sizeof(raw_esp));
132
    FAIL_IF(ret != TM_ECODE_OK);
133

134
    FAIL_IF(p->proto != IPPROTO_ESP);
135
    FAIL_IF(p->payload_len != sizeof(raw_esp) - ESP_HEADER_LEN);
136
    FAIL_IF(memcmp(p->payload, raw_esp + ESP_HEADER_LEN, p->payload_len) != 0);
137
    FAIL_IF(ESP_GET_SPI(PacketGetESP(p)) != 0x7b);
138
    FAIL_IF(ESP_GET_SEQUENCE(PacketGetESP(p)) != 0x08);
139

140
    PacketFree(p);
141

142
    PASS;
143
}
144

145
/** \test Failure decoding, not enough data */
146
static int DecodeESPTest03(void)
147
{
148
    uint8_t raw_esp[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
149

150
    Packet *p = PacketGetFromAlloc();
151
    FAIL_IF_NULL(p);
152

153
    ThreadVars tv;
154
    DecodeThreadVars dtv;
155

156
    memset(&tv, 0, sizeof(ThreadVars));
157
    memset(&dtv, 0, sizeof(DecodeThreadVars));
158

159
    int ret = DecodeESP(&tv, &dtv, p, raw_esp, sizeof(raw_esp));
160
    FAIL_IF(ret != TM_ECODE_FAILED);
161

162
    // expect ESP_PKT_TOO_SMALL
163
    FAIL_IF_NOT(ENGINE_ISSET_EVENT(p, ESP_PKT_TOO_SMALL));
164

165
    PacketFree(p);
166

167
    PASS;
168
}
169

170
/** \test Failure decoding, no data */
171
static int DecodeESPTest04(void)
172
{
173
    uint8_t raw_esp[] = {};
174

175
    Packet *p = PacketGetFromAlloc();
176
    FAIL_IF_NULL(p);
177

178
    ThreadVars tv;
179
    DecodeThreadVars dtv;
180

181
    memset(&tv, 0, sizeof(ThreadVars));
182
    memset(&dtv, 0, sizeof(DecodeThreadVars));
183

184
    int ret = DecodeESP(&tv, &dtv, p, raw_esp, sizeof(raw_esp));
185
    FAIL_IF(ret != TM_ECODE_FAILED);
186

187
    // expect ESP_PKT_TOO_SMALL
188
    FAIL_IF_NOT(ENGINE_ISSET_EVENT(p, ESP_PKT_TOO_SMALL));
189

190
    PacketFree(p);
191

192
    PASS;
193
}
194
#endif /* UNITTESTS */
195

196
void DecodeESPRegisterTests(void)
UNCOV
197
{
×
198
#ifdef UNITTESTS
199
    UtRegisterTest("DecodeESPTest01", DecodeESPTest01);
200
    UtRegisterTest("DecodeESPTest02", DecodeESPTest02);
201
    UtRegisterTest("DecodeESPTest03", DecodeESPTest03);
202
    UtRegisterTest("DecodeESPTest04", DecodeESPTest04);
203
#endif /* UNITTESTS */
UNCOV
204
}
×
205

206
/**
207
 * @}
208
 */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc