Coverage Report

Created: 2026-09-28 07:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata/src/decode-esp.c
Line
Count
Source
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
13.5k
{
38
13.5k
    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
39
40
13.5k
    if (unlikely(len < ESP_HEADER_LEN)) {
41
1.18k
        ENGINE_SET_INVALID_EVENT(p, ESP_PKT_TOO_SMALL);
42
1.18k
        return -1;
43
1.18k
    }
44
45
12.4k
    (void)PacketSetESP(p, pkt);
46
47
12.4k
    p->payload = (uint8_t *)pkt + sizeof(ESPHdr);
48
12.4k
    p->payload_len = len - sizeof(ESPHdr);
49
50
12.4k
    p->proto = IPPROTO_ESP;
51
52
12.4k
    return 0;
53
13.5k
}
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
13.8k
{
66
13.8k
    DEBUG_VALIDATE_BUG_ON(pkt == NULL);
67
68
13.8k
    StatsCounterIncr(&tv->stats, dtv->counter_esp);
69
70
13.8k
    if (!PacketIncreaseCheckLayers(p)) {
71
233
        return TM_ECODE_FAILED;
72
233
    }
73
13.5k
    if (unlikely(DecodeESPPacket(tv, p, pkt, len) < 0)) {
74
1.18k
        PacketClearL4(p);
75
1.18k
        return TM_ECODE_FAILED;
76
1.18k
    }
77
78
12.4k
    SCLogDebug("ESP spi: %" PRIu32 " sequence: %" PRIu32, ESP_GET_SPI(PacketGetESP(p)),
79
12.4k
            ESP_GET_SEQUENCE(PacketGetESP(p)));
80
81
12.4k
    FlowSetupPacket(p);
82
83
12.4k
    return TM_ECODE_OK;
84
13.5k
}
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)
197
0
{
198
#ifdef UNITTESTS
199
    UtRegisterTest("DecodeESPTest01", DecodeESPTest01);
200
    UtRegisterTest("DecodeESPTest02", DecodeESPTest02);
201
    UtRegisterTest("DecodeESPTest03", DecodeESPTest03);
202
    UtRegisterTest("DecodeESPTest04", DecodeESPTest04);
203
#endif /* UNITTESTS */
204
0
}
205
206
/**
207
 * @}
208
 */