• 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

58.68
/src/decode.c
1
/* Copyright (C) 2007-2024 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
 * \defgroup decode Packet decoding
20
 *
21
 * \brief Code in charge of protocol decoding
22
 *
23
 * The task of decoding packets is made in different files and
24
 * as Suricata is supporting encapsulation there is a potential
25
 * recursivity in the call.
26
 *
27
 * For each protocol a DecodePROTO function is provided. For
28
 * example we have DecodeIPV4() for IPv4 and DecodePPP() for
29
 * PPP.
30
 *
31
 * These functions have all a pkt and a len argument which
32
 * are respectively a pointer to the protocol data and the length
33
 * of this protocol data.
34
 *
35
 * \attention The pkt parameter must point to the effective data because
36
 * it will be used later to set per protocol pointer like Packet::tcph
37
 *
38
 * @{
39
 */
40

41

42
/**
43
 * \file
44
 *
45
 * \author Victor Julien <victor@inliniac.net>
46
 *
47
 * Decode the raw packet
48
 */
49

50
#include "suricata-common.h"
51
#include "decode.h"
52

53
#include "packet.h"
54
#include "flow.h"
55
#include "flow-storage.h"
56
#include "tmqh-packetpool.h"
57
#include "app-layer.h"
58
#include "output.h"
59

60
#include "decode-vxlan.h"
61
#include "decode-geneve.h"
62
#include "decode-erspan.h"
63
#include "decode-teredo.h"
64
#include "decode-arp.h"
65

66
#include "defrag-hash.h"
67

68
#include "util-hash.h"
69
#include "util-hash-string.h"
70
#include "util-print.h"
71
#include "util-profiling.h"
72
#include "util-validate.h"
73
#include "util-debug.h"
74
#include "util-exception-policy.h"
75
#include "action-globals.h"
76

77
uint32_t default_packet_size = 0;
78
extern bool stats_decoder_events;
79
extern const char *stats_decoder_events_prefix;
80
extern bool stats_stream_events;
81
uint8_t decoder_max_layers = PKT_DEFAULT_MAX_DECODED_LAYERS;
82
uint16_t packet_alert_max = PACKET_ALERT_MAX;
83

84
/* Settings order as in the enum */
85
// clang-format off
86
ExceptionPolicyStatsSetts defrag_memcap_eps_stats = {
87
    .valid_settings_ids = {
88
    /* EXCEPTION_POLICY_NOT_SET */      false,
89
    /* EXCEPTION_POLICY_AUTO */         false,
90
    /* EXCEPTION_POLICY_PASS_PACKET */  true,
91
    /* EXCEPTION_POLICY_PASS_FLOW */    false,
92
    /* EXCEPTION_POLICY_BYPASS_FLOW */  true,
93
    /* EXCEPTION_POLICY_DROP_PACKET */  false,
94
    /* EXCEPTION_POLICY_DROP_FLOW */    false,
95
    /* EXCEPTION_POLICY_REJECT */       true,
96
    /* EXCEPTION_POLICY_REJECT_BOTH */  true,
97
    },
98
    .valid_settings_ips = {
99
    /* EXCEPTION_POLICY_NOT_SET */      false,
100
    /* EXCEPTION_POLICY_AUTO */         false,
101
    /* EXCEPTION_POLICY_PASS_PACKET */  true,
102
    /* EXCEPTION_POLICY_PASS_FLOW */    false,
103
    /* EXCEPTION_POLICY_BYPASS_FLOW */  true,
104
    /* EXCEPTION_POLICY_DROP_PACKET */  true,
105
    /* EXCEPTION_POLICY_DROP_FLOW */    false,
106
    /* EXCEPTION_POLICY_REJECT */       true,
107
    /* EXCEPTION_POLICY_REJECT_BOTH */  true,
108
    },
109
};
110
// clang-format on
111

112
/* Settings order as in the enum */
113
// clang-format off
114
ExceptionPolicyStatsSetts flow_memcap_eps_stats = {
115
    .valid_settings_ids = {
116
    /* EXCEPTION_POLICY_NOT_SET */      false,
117
    /* EXCEPTION_POLICY_AUTO */         false,
118
    /* EXCEPTION_POLICY_PASS_PACKET */  true,
119
    /* EXCEPTION_POLICY_PASS_FLOW */    false,
120
    /* EXCEPTION_POLICY_BYPASS_FLOW */  true,
121
    /* EXCEPTION_POLICY_DROP_PACKET */  false,
122
    /* EXCEPTION_POLICY_DROP_FLOW */    false,
123
    /* EXCEPTION_POLICY_REJECT */       true,
124
    /* EXCEPTION_POLICY_REJECT_BOTH */  true,
125
    },
126
    .valid_settings_ips = {
127
    /* EXCEPTION_POLICY_NOT_SET */      false,
128
    /* EXCEPTION_POLICY_AUTO */         false,
129
    /* EXCEPTION_POLICY_PASS_PACKET */  true,
130
    /* EXCEPTION_POLICY_PASS_FLOW */    false,
131
    /* EXCEPTION_POLICY_BYPASS_FLOW */  true,
132
    /* EXCEPTION_POLICY_DROP_PACKET */  true,
133
    /* EXCEPTION_POLICY_DROP_FLOW */    false,
134
    /* EXCEPTION_POLICY_REJECT */       true,
135
    /* EXCEPTION_POLICY_REJECT_BOTH */  true,
136
    },
137
};
138
// clang-format on
139

140
/**
141
 * \brief Initialize PacketAlerts with dynamic alerts array size
142
 *
143
 */
144
PacketAlert *PacketAlertCreate(void)
145
{
18,736✔
146
    PacketAlert *pa_array = SCCalloc(packet_alert_max, sizeof(PacketAlert));
18,736✔
147
    DEBUG_VALIDATE_BUG_ON(pa_array == NULL);
18,736✔
148

149
    return pa_array;
18,736✔
150
}
18,736✔
151

152
void PacketAlertRecycle(PacketAlert *pa_array, uint16_t cnt)
UNCOV
153
{
×
UNCOV
154
    if (pa_array == NULL)
×
155
        return;
×
156
    /* Clean json content for alerts attached to the packet */
UNCOV
157
    for (int i = 0; i < cnt; i++) {
×
UNCOV
158
        struct PacketContextData *current_json = pa_array[i].json_info;
×
UNCOV
159
        while (current_json) {
×
UNCOV
160
            struct PacketContextData *next_json = current_json->next;
×
UNCOV
161
            SCFree(current_json->json_string);
×
UNCOV
162
            SCFree(current_json);
×
UNCOV
163
            current_json = next_json;
×
UNCOV
164
        }
×
UNCOV
165
        pa_array[i].json_info = NULL;
×
UNCOV
166
    }
×
UNCOV
167
}
×
168

169
void PacketAlertFree(PacketAlert *pa_array)
170
{
18,480✔
171
    if (pa_array == NULL)
18,480✔
172
        return;
×
173
    for (int i = 0; i < packet_alert_max; i++) {
295,680✔
174
        struct PacketContextData *allocated_json = pa_array[i].json_info;
277,200✔
175
        while (allocated_json) {
277,200✔
UNCOV
176
            struct PacketContextData *next_json = allocated_json->next;
×
UNCOV
177
            SCFree(allocated_json->json_string);
×
UNCOV
178
            SCFree(allocated_json);
×
UNCOV
179
            allocated_json = next_json;
×
UNCOV
180
        }
×
181
    }
277,200✔
182
    SCFree(pa_array);
18,480✔
183
}
18,480✔
184

185
static int DecodeTunnel(ThreadVars *, DecodeThreadVars *, Packet *, const uint8_t *, uint32_t,
186
        enum DecodeTunnelProto) WARN_UNUSED;
187

188
static int DecodeTunnel(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt,
189
        uint32_t len, enum DecodeTunnelProto proto)
190
{
33,462✔
191
    switch (proto) {
33,462✔
192
        case DECODE_TUNNEL_PPP:
5,071✔
193
            return DecodePPP(tv, dtv, p, pkt, len);
5,071✔
194
        case DECODE_TUNNEL_IPV4:
3,056✔
195
            DEBUG_VALIDATE_BUG_ON(len > UINT16_MAX);
3,056✔
196
            return DecodeIPV4(tv, dtv, p, pkt, (uint16_t)len);
3,056✔
197
        case DECODE_TUNNEL_IPV6:
1,226✔
198
        case DECODE_TUNNEL_IPV6_TEREDO:
2,324✔
199
            DEBUG_VALIDATE_BUG_ON(len > UINT16_MAX);
2,324✔
200
            return DecodeIPV6(tv, dtv, p, pkt, (uint16_t)len);
2,324✔
201
        case DECODE_TUNNEL_VLAN:
639✔
202
            return DecodeVLAN(tv, dtv, p, pkt, len);
639✔
203
        case DECODE_TUNNEL_ETHERNET:
466✔
204
            return DecodeEthernet(tv, dtv, p, pkt, len);
466✔
205
        case DECODE_TUNNEL_ERSPANII:
1,892✔
206
            return DecodeERSPAN(tv, dtv, p, pkt, len);
1,892✔
207
        case DECODE_TUNNEL_ERSPANI:
214✔
208
            return DecodeERSPANTypeI(tv, dtv, p, pkt, len);
214✔
209
        case DECODE_TUNNEL_VXLAN:
19,710✔
210
            return DecodeEthernet(tv, dtv, p, pkt, len);
19,710✔
211
        case DECODE_TUNNEL_NSH:
×
212
            return DecodeNSH(tv, dtv, p, pkt, len);
×
213
        case DECODE_TUNNEL_ARP:
90✔
214
            return DecodeARP(tv, dtv, p, pkt, len);
90✔
215
        default:
×
216
            SCLogDebug("FIXME: DecodeTunnel: protocol %" PRIu32 " not supported.", proto);
×
217
            break;
×
218
    }
33,462✔
219
    return TM_ECODE_OK;
×
220
}
33,462✔
221

222
/**
223
 * \brief Return a malloced packet.
224
 */
225
void PacketFree(Packet *p)
226
{
18,480✔
227
    PacketDestructor(p);
18,480✔
228
    SCFree(p);
18,480✔
229
}
18,480✔
230

231
/**
232
 * \brief Finalize decoding of a packet
233
 *
234
 * This function needs to be call at the end of decode
235
 * functions when decoding has been successful.
236
 *
237
 */
238
void PacketDecodeFinalize(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p)
239
{
2,232,849✔
240
    if (p->flags & PKT_IS_INVALID) {
2,232,849✔
241
        StatsCounterIncr(&tv->stats, dtv->counter_invalid);
416,944✔
242
    }
416,944✔
243
}
2,232,849✔
244

245
void PacketUpdateEngineEventCounters(ThreadVars *tv,
246
        DecodeThreadVars *dtv, Packet *p)
247
{
1,446,785✔
248
    for (uint8_t i = 0; i < p->events.cnt; i++) {
1,873,449✔
249
        const uint8_t e = p->events.events[i];
426,664✔
250

251
        if (e <= DECODE_EVENT_PACKET_MAX && !stats_decoder_events)
426,664✔
252
            continue;
×
253
        else if (e > DECODE_EVENT_PACKET_MAX && !stats_stream_events)
426,664✔
254
            continue;
94,703✔
255
        StatsCounterIncr(&tv->stats, dtv->counter_engine_events[e]);
331,961✔
256
    }
331,961✔
257
}
1,446,785✔
258

259
/**
260
 * \brief Get a malloced packet.
261
 *
262
 * \retval p packet, NULL on error
263
 */
264
Packet *PacketGetFromAlloc(void)
265
{
18,736✔
266
    Packet *p = SCCalloc(1, SIZE_OF_PACKET);
18,736✔
267
    if (unlikely(p == NULL)) {
18,736✔
268
        return NULL;
×
269
    }
×
270
    PacketInit(p);
18,736✔
271
    p->ReleasePacket = PacketFree;
18,736✔
272

273
    SCLogDebug("allocated a new packet only using alloc...");
18,736✔
274

275
    PACKET_PROFILING_START(p);
18,736✔
276
    return p;
18,736✔
277
}
18,736✔
278

279
/**
280
 * \brief Return a packet to where it was allocated.
281
 */
282
void PacketFreeOrRelease(Packet *p)
283
{
798,520✔
284
    if (likely(p->pool != NULL)) {
798,520✔
285
        p->ReleasePacket = PacketPoolReturnPacket;
798,520✔
286
        PacketPoolReturnPacket(p);
798,520✔
287
    } else {
798,520✔
UNCOV
288
        PacketFree(p);
×
UNCOV
289
    }
×
290
}
798,520✔
291

292
/**
293
 *  \brief Get a packet. We try to get a packet from the packetpool first, but
294
 *         if that is empty we alloc a packet that is free'd again after
295
 *         processing.
296
 *
297
 *  \retval p packet, NULL on error
298
 */
299
Packet *PacketGetFromQueueOrAlloc(void)
300
{
835,103✔
301
    /* try the pool first */
302
    Packet *p = PacketPoolGetPacket();
835,103✔
303

304
    if (p == NULL) {
835,103✔
305
        /* non fatal, we're just not processing a packet then */
UNCOV
306
        p = PacketGetFromAlloc();
×
307
    } else {
835,103✔
308
        DEBUG_VALIDATE_BUG_ON(p->ReleasePacket != PacketPoolReturnPacket);
835,103✔
309
        PACKET_PROFILING_START(p);
835,103✔
310
    }
835,103✔
311

312
    return p;
835,103✔
313
}
835,103✔
314

315
inline int PacketCallocExtPkt(Packet *p, int datalen)
UNCOV
316
{
×
UNCOV
317
    if (! p->ext_pkt) {
×
UNCOV
318
        p->ext_pkt = SCCalloc(1, datalen);
×
UNCOV
319
        if (unlikely(p->ext_pkt == NULL)) {
×
320
            SET_PKT_LEN(p, 0);
×
321
            return -1;
×
322
        }
×
UNCOV
323
    }
×
UNCOV
324
    return 0;
×
UNCOV
325
}
×
326

327
/**
328
 *  \brief Copy data to Packet payload at given offset
329
 *
330
 * This function copies data/payload to a Packet. It uses the
331
 * space allocated at Packet creation (pointed by Packet::pkt)
332
 * or allocate some memory (pointed by Packet::ext_pkt) if the
333
 * data size is to big to fit in initial space (of size
334
 * default_packet_size).
335
 *
336
 *  \param Pointer to the Packet to modify
337
 *  \param Offset of the copy relatively to payload of Packet
338
 *  \param Pointer to the data to copy
339
 *  \param Length of the data to copy
340
 */
341
inline int PacketCopyDataOffset(Packet *p, uint32_t offset, const uint8_t *data, uint32_t datalen)
342
{
2,290,076✔
343
    if (unlikely(offset + datalen > MAX_PAYLOAD_SIZE)) {
2,290,076✔
344
        /* too big */
345
        SET_PKT_LEN(p, 0);
52✔
346
        return -1;
52✔
347
    }
52✔
348

349
    /* Do we have already an packet with allocated data */
350
    if (! p->ext_pkt) {
2,290,024✔
351
        uint32_t newsize = offset + datalen;
2,287,172✔
352
        // check overflow
353
        if (newsize < offset)
2,287,172✔
354
            return -1;
×
355
        if (newsize <= default_packet_size) {
2,287,172✔
356
            /* data will fit in memory allocated with packet */
357
            memcpy(GET_PKT_DIRECT_DATA(p) + offset, data, datalen);
2,272,754✔
358
        } else {
2,272,754✔
359
            /* here we need a dynamic allocation */
360
            p->ext_pkt = SCMalloc(MAX_PAYLOAD_SIZE);
14,418✔
361
            if (unlikely(p->ext_pkt == NULL)) {
14,418✔
362
                SET_PKT_LEN(p, 0);
×
363
                return -1;
×
364
            }
×
365
            /* copy initial data */
366
            memcpy(p->ext_pkt, GET_PKT_DIRECT_DATA(p), GET_PKT_DIRECT_MAX_SIZE(p));
14,418✔
367
            /* copy data as asked */
368
            memcpy(p->ext_pkt + offset, data, datalen);
14,418✔
369
        }
14,418✔
370
    } else {
2,287,172✔
371
        memcpy(p->ext_pkt + offset, data, datalen);
2,852✔
372
    }
2,852✔
373
    return 0;
2,290,024✔
374
}
2,290,024✔
375

376
/**
377
 *  \brief Copy data to Packet payload and set packet length
378
 *
379
 *  \param Pointer to the Packet to modify
380
 *  \param Pointer to the data to copy
381
 *  \param Length of the data to copy
382
 */
383
inline int PacketCopyData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
384
{
2,274,214✔
385
    SET_PKT_LEN(p, pktlen);
2,274,214✔
386
    return PacketCopyDataOffset(p, 0, pktdata, pktlen);
2,274,214✔
387
}
2,274,214✔
388

389
/**
390
 *  \brief Setup a pseudo packet (tunnel)
391
 *
392
 *  \param parent parent packet for this pseudo pkt
393
 *  \param pkt raw packet data
394
 *  \param len packet data length
395
 *  \param proto protocol of the tunneled packet
396
 *
397
 *  \retval p the pseudo packet or NULL if out of memory
398
 */
399
Packet *PacketTunnelPktSetup(ThreadVars *tv, DecodeThreadVars *dtv, Packet *parent,
400
                             const uint8_t *pkt, uint32_t len, enum DecodeTunnelProto proto)
401
{
33,499✔
402
    int ret;
33,499✔
403

404
    SCEnter();
33,499✔
405

406
    if (parent->nb_decoded_layers + 1 >= decoder_max_layers) {
33,499✔
407
        ENGINE_SET_INVALID_EVENT(parent, GENERIC_TOO_MANY_LAYERS);
37✔
408
        SCReturnPtr(NULL, "Packet");
37✔
409
    }
37✔
410

411
    /* get us a packet */
412
    Packet *p = PacketGetFromQueueOrAlloc();
33,462✔
413
    if (unlikely(p == NULL)) {
33,462✔
414
        SCReturnPtr(NULL, "Packet");
×
415
    }
×
416

417
    /* copy packet and set length, proto */
418
    PacketCopyData(p, pkt, len);
33,462✔
419
    DEBUG_VALIDATE_BUG_ON(parent->recursion_level == 255);
33,462✔
420
    p->recursion_level = parent->recursion_level + 1;
33,462✔
421
    DEBUG_VALIDATE_BUG_ON(parent->nb_decoded_layers >= decoder_max_layers);
33,462✔
422
    p->nb_decoded_layers = parent->nb_decoded_layers + 1;
33,462✔
423
    p->ts = parent->ts;
33,462✔
424
    p->datalink = DLT_RAW;
33,462✔
425
    p->tenant_id = parent->tenant_id;
33,462✔
426
    p->livedev = parent->livedev;
33,462✔
427

428
    /* set the root ptr to the lowest layer */
429
    if (parent->root != NULL) {
33,462✔
430
        p->root = parent->root;
1,535✔
431
        BUG_ON(!PacketIsTunnelChild(parent));
1,535✔
432
    } else {
31,927✔
433
        p->root = parent;
31,927✔
434
        parent->ttype = PacketTunnelRoot;
31,927✔
435
    }
31,927✔
436
    /* tell new packet it's part of a tunnel */
437
    p->ttype = PacketTunnelChild;
33,462✔
438

439
    ret = DecodeTunnel(tv, dtv, p, GET_PKT_DATA(p),
33,462✔
440
                       GET_PKT_LEN(p), proto);
33,462✔
441

442
    if (unlikely(ret != TM_ECODE_OK) ||
33,462✔
443
            (proto == DECODE_TUNNEL_IPV6_TEREDO && (p->flags & PKT_IS_INVALID)))
33,462✔
444
    {
4,594✔
445
        /* Not a (valid) tunnel packet */
446
        SCLogDebug("tunnel packet is invalid");
4,594✔
447
        p->root = NULL;
4,594✔
448
        TmqhOutputPacketpool(tv, p);
4,594✔
449
        SCReturnPtr(NULL, "Packet");
4,594✔
450
    }
4,594✔
451

452
    /* Update tunnel settings in parent */
453
    if (parent->root == NULL) {
28,868✔
454
        parent->ttype = PacketTunnelRoot;
27,627✔
455
    }
27,627✔
456
    TUNNEL_INCR_PKT_TPR(p);
28,868✔
457

458
    /* disable payload (not packet) inspection on the parent, as the payload
459
     * is the packet we will now run through the system separately. We do
460
     * check it against the ip/port/other header checks though */
461
    DecodeSetNoPayloadInspectionFlag(parent);
28,868✔
462
    SCReturnPtr(p, "Packet");
28,868✔
463
}
33,462✔
464

465
/**
466
 *  \brief Setup a pseudo packet (reassembled frags)
467
 *
468
 *  Difference with PacketPseudoPktSetup is that this func doesn't increment
469
 *  the recursion level. It needs to be on the same level as the frags because
470
 *  we run the flow engine against this and we need to get the same flow.
471
 *
472
 *  \param parent parent packet for this pseudo pkt
473
 *  \param pkt raw packet data
474
 *  \param len packet data length
475
 *  \param proto protocol of the tunneled packet
476
 *
477
 *  \retval p the pseudo packet or NULL if out of memory
478
 */
479
Packet *PacketDefragPktSetup(Packet *parent, const uint8_t *pkt, uint32_t len, uint8_t proto)
480
{
4,911✔
481
    SCEnter();
4,911✔
482

483
    /* get us a packet */
484
    Packet *p = PacketGetFromQueueOrAlloc();
4,911✔
485
    if (unlikely(p == NULL)) {
4,911✔
486
        SCReturnPtr(NULL, "Packet");
×
487
    }
×
488

489
    /* set the root ptr to the lowest layer */
490
    if (parent->root != NULL) {
4,911✔
491
        p->root = parent->root;
10✔
492
        BUG_ON(!PacketIsTunnelChild(parent));
10✔
493
    } else {
4,901✔
494
        p->root = parent;
4,901✔
495
        // we set parent->ttype later
496
    }
4,901✔
497
    /* tell new packet it's part of a tunnel */
498
    p->ttype = PacketTunnelChild;
4,911✔
499

500
    /* copy packet and set length, proto */
501
    if (pkt && len) {
4,911✔
502
        PacketCopyData(p, pkt, len);
2,905✔
503
    }
2,905✔
504
    p->recursion_level = parent->recursion_level; /* NOT incremented */
4,911✔
505
    p->ts = parent->ts;
4,911✔
506
    p->tenant_id = parent->tenant_id;
4,911✔
507
    memcpy(&p->vlan_id[0], &parent->vlan_id[0], sizeof(p->vlan_id));
4,911✔
508
    p->vlan_idx = parent->vlan_idx;
4,911✔
509
    p->livedev = parent->livedev;
4,911✔
510

511
    SCReturnPtr(p, "Packet");
4,911✔
512
}
4,911✔
513

514
/**
515
 *  \brief inform defrag "parent" that a pseudo packet is
516
 *         now associated to it.
517
 */
518
void PacketDefragPktSetupParent(Packet *parent)
519
{
4,911✔
520
    /* tell parent packet it's part of a tunnel */
521
    if (parent->ttype == PacketTunnelNone)
4,911✔
522
        parent->ttype = PacketTunnelRoot;
4,901✔
523

524
    /* increment tunnel packet refcnt in the root packet */
525
    TUNNEL_INCR_PKT_TPR(parent);
4,911✔
526

527
    /* disable payload (not packet) inspection on the parent, as the payload
528
     * is the packet we will now run through the system separately. We do
529
     * check it against the ip/port/other header checks though */
530
    DecodeSetNoPayloadInspectionFlag(parent);
4,911✔
531
}
4,911✔
532

533
/**
534
 *  \note if p->flow is set, the flow is locked
535
 */
536
void PacketBypassCallback(Packet *p)
537
{
11✔
538
    if (PKT_IS_PSEUDOPKT(p))
11✔
UNCOV
539
        return;
×
540

541
#ifdef CAPTURE_OFFLOAD
542
    /* Don't try to bypass if flow is already out or
543
     * if we have failed to do it once */
544
    if (p->flow) {
545
        int state = p->flow->flow_state;
546
        if ((state == FLOW_STATE_LOCAL_BYPASSED) ||
547
                (state == FLOW_STATE_CAPTURE_BYPASSED)) {
548
            return;
549
        }
550

551
        FlowBypassInfo *fc;
552

553
        fc = FlowGetStorageById(p->flow, GetFlowBypassInfoID());
554
        if (fc == NULL) {
555
            fc = SCCalloc(sizeof(FlowBypassInfo), 1);
556
            if (fc) {
557
                FlowSetStorageById(p->flow, GetFlowBypassInfoID(), fc);
558
            } else {
559
                return;
560
            }
561
        }
562
    }
563
    if (p->BypassPacketsFlow && p->BypassPacketsFlow(p)) {
564
        if (p->flow) {
565
            FlowUpdateState(p->flow, FLOW_STATE_CAPTURE_BYPASSED);
566
        }
567
    } else {
568
        if (p->flow) {
569
            FlowUpdateState(p->flow, FLOW_STATE_LOCAL_BYPASSED);
570
        }
571
    }
572
#else /* CAPTURE_OFFLOAD */
573
    if (p->flow) {
11✔
574
        int state = p->flow->flow_state;
11✔
575
        if (state == FLOW_STATE_LOCAL_BYPASSED)
11✔
UNCOV
576
            return;
×
577
        FlowUpdateState(p->flow, FLOW_STATE_LOCAL_BYPASSED);
11✔
578
    }
11✔
579
#endif
11✔
580
}
11✔
581

582
/** \brief switch direction of a packet */
583
void PacketSwap(Packet *p)
584
{
3,999✔
585
    if (PKT_IS_TOSERVER(p)) {
3,999✔
586
        p->flowflags &= ~FLOW_PKT_TOSERVER;
2,719✔
587
        p->flowflags |= FLOW_PKT_TOCLIENT;
2,719✔
588

589
        if (p->flowflags & FLOW_PKT_TOSERVER_FIRST) {
2,719✔
590
            p->flowflags &= ~FLOW_PKT_TOSERVER_FIRST;
2,447✔
591
            p->flowflags |= FLOW_PKT_TOCLIENT_FIRST;
2,447✔
592
        }
2,447✔
593
    } else {
2,719✔
594
        p->flowflags &= ~FLOW_PKT_TOCLIENT;
1,280✔
595
        p->flowflags |= FLOW_PKT_TOSERVER;
1,280✔
596

597
        if (p->flowflags & FLOW_PKT_TOCLIENT_FIRST) {
1,280✔
598
            p->flowflags &= ~FLOW_PKT_TOCLIENT_FIRST;
1,234✔
599
            p->flowflags |= FLOW_PKT_TOSERVER_FIRST;
1,234✔
600
        }
1,234✔
601
    }
1,280✔
602
}
3,999✔
603

604
/* counter name store */
605
static HashTable *g_counter_table = NULL;
606
static SCMutex g_counter_table_mutex = SCMUTEX_INITIALIZER;
607

608
void DecodeUnregisterCounters(void)
UNCOV
609
{
×
UNCOV
610
    SCMutexLock(&g_counter_table_mutex);
×
UNCOV
611
    if (g_counter_table) {
×
UNCOV
612
        HashTableFree(g_counter_table);
×
UNCOV
613
        g_counter_table = NULL;
×
UNCOV
614
    }
×
UNCOV
615
    SCMutexUnlock(&g_counter_table_mutex);
×
UNCOV
616
}
×
617

618
static bool IsDefragMemcapExceptionPolicyStatsValid(enum ExceptionPolicy policy)
UNCOV
619
{
×
UNCOV
620
    if (EngineModeIsIPS()) {
×
UNCOV
621
        return defrag_memcap_eps_stats.valid_settings_ips[policy];
×
UNCOV
622
    }
×
623
    return defrag_memcap_eps_stats.valid_settings_ids[policy];
×
UNCOV
624
}
×
625

626
static bool IsFlowMemcapExceptionPolicyStatsValid(enum ExceptionPolicy policy)
UNCOV
627
{
×
UNCOV
628
    if (EngineModeIsIPS()) {
×
UNCOV
629
        return flow_memcap_eps_stats.valid_settings_ips[policy];
×
UNCOV
630
    }
×
631
    return flow_memcap_eps_stats.valid_settings_ids[policy];
×
UNCOV
632
}
×
633

634
void DecodeRegisterPerfCounters(DecodeThreadVars *dtv, ThreadVars *tv)
635
{
3✔
636
    /* register counters */
637
    dtv->counter_pkts = StatsRegisterCounter("decoder.pkts", &tv->stats);
3✔
638
    dtv->counter_bytes = StatsRegisterCounter("decoder.bytes", &tv->stats);
3✔
639
    dtv->counter_invalid = StatsRegisterCounter("decoder.invalid", &tv->stats);
3✔
640
    dtv->counter_ipv4 = StatsRegisterCounter("decoder.ipv4", &tv->stats);
3✔
641
    dtv->counter_ipv6 = StatsRegisterCounter("decoder.ipv6", &tv->stats);
3✔
642
    dtv->counter_eth = StatsRegisterCounter("decoder.ethernet", &tv->stats);
3✔
643
    dtv->counter_arp = StatsRegisterCounter("decoder.arp", &tv->stats);
3✔
644
    dtv->counter_ethertype_unknown = StatsRegisterCounter("decoder.unknown_ethertype", &tv->stats);
3✔
645
    dtv->counter_chdlc = StatsRegisterCounter("decoder.chdlc", &tv->stats);
3✔
646
    dtv->counter_raw = StatsRegisterCounter("decoder.raw", &tv->stats);
3✔
647
    dtv->counter_null = StatsRegisterCounter("decoder.null", &tv->stats);
3✔
648
    dtv->counter_sll = StatsRegisterCounter("decoder.sll", &tv->stats);
3✔
649
    dtv->counter_sll2 = StatsRegisterCounter("decoder.sll2", &tv->stats);
3✔
650
    dtv->counter_tcp = StatsRegisterCounter("decoder.tcp", &tv->stats);
3✔
651

652
    dtv->counter_tcp_syn = StatsRegisterCounter("tcp.syn", &tv->stats);
3✔
653
    dtv->counter_tcp_synack = StatsRegisterCounter("tcp.synack", &tv->stats);
3✔
654
    dtv->counter_tcp_rst = StatsRegisterCounter("tcp.rst", &tv->stats);
3✔
655
    dtv->counter_tcp_urg = StatsRegisterCounter("tcp.urg", &tv->stats);
3✔
656

657
    dtv->counter_udp = StatsRegisterCounter("decoder.udp", &tv->stats);
3✔
658
    dtv->counter_sctp = StatsRegisterCounter("decoder.sctp", &tv->stats);
3✔
659
    dtv->counter_esp = StatsRegisterCounter("decoder.esp", &tv->stats);
3✔
660
    dtv->counter_icmpv4 = StatsRegisterCounter("decoder.icmpv4", &tv->stats);
3✔
661
    dtv->counter_icmpv6 = StatsRegisterCounter("decoder.icmpv6", &tv->stats);
3✔
662
    dtv->counter_igmp = StatsRegisterCounter("decoder.igmp", &tv->stats);
3✔
663
    dtv->counter_ppp = StatsRegisterCounter("decoder.ppp", &tv->stats);
3✔
664
    dtv->counter_pppoe = StatsRegisterCounter("decoder.pppoe", &tv->stats);
3✔
665
    dtv->counter_geneve = StatsRegisterCounter("decoder.geneve", &tv->stats);
3✔
666
    dtv->counter_gre = StatsRegisterCounter("decoder.gre", &tv->stats);
3✔
667
    dtv->counter_vlan = StatsRegisterCounter("decoder.vlan", &tv->stats);
3✔
668
    dtv->counter_vlan_qinq = StatsRegisterCounter("decoder.vlan_qinq", &tv->stats);
3✔
669
    dtv->counter_vlan_qinqinq = StatsRegisterCounter("decoder.vlan_qinqinq", &tv->stats);
3✔
670
    dtv->counter_vxlan = StatsRegisterCounter("decoder.vxlan", &tv->stats);
3✔
671
    dtv->counter_vntag = StatsRegisterCounter("decoder.vntag", &tv->stats);
3✔
672
    dtv->counter_etag = StatsRegisterCounter("decoder.etag", &tv->stats);
3✔
673
    dtv->counter_ieee8021ah = StatsRegisterCounter("decoder.ieee8021ah", &tv->stats);
3✔
674
    dtv->counter_teredo = StatsRegisterCounter("decoder.teredo", &tv->stats);
3✔
675
    dtv->counter_ipv4inipv4 = StatsRegisterCounter("decoder.ipv4_in_ipv4", &tv->stats);
3✔
676
    dtv->counter_ipv6inipv4 = StatsRegisterCounter("decoder.ipv6_in_ipv4", &tv->stats);
3✔
677
    dtv->counter_ipv4inipv6 = StatsRegisterCounter("decoder.ipv4_in_ipv6", &tv->stats);
3✔
678
    dtv->counter_ipv6inipv6 = StatsRegisterCounter("decoder.ipv6_in_ipv6", &tv->stats);
3✔
679
    dtv->counter_ipv4_unknown_proto =
3✔
680
            StatsRegisterCounter("decoder.ipv4.unknown_protocol", &tv->stats);
3✔
681
    dtv->counter_mpls = StatsRegisterCounter("decoder.mpls", &tv->stats);
3✔
682
    dtv->counter_avg_pkt_size = StatsRegisterDeriveDivCounter(
3✔
683
            "decoder.avg_pkt_size", "decoder.bytes", "decoder.pkts", &tv->stats);
3✔
684
    dtv->counter_max_pkt_size = StatsRegisterMaxCounter("decoder.max_pkt_size", &tv->stats);
3✔
685
    dtv->counter_max_mac_addrs_src =
3✔
686
            StatsRegisterMaxCounter("decoder.max_mac_addrs_src", &tv->stats);
3✔
687
    dtv->counter_max_mac_addrs_dst =
3✔
688
            StatsRegisterMaxCounter("decoder.max_mac_addrs_dst", &tv->stats);
3✔
689
    dtv->counter_erspan = StatsRegisterCounter("decoder.erspan", &tv->stats);
3✔
690
    dtv->counter_nsh = StatsRegisterCounter("decoder.nsh", &tv->stats);
3✔
691
    dtv->counter_flow_memcap = StatsRegisterCounter("flow.memcap", &tv->stats);
3✔
692
    ExceptionPolicySetStatsCounters(tv, &dtv->counter_flow_memcap_eps, &flow_memcap_eps_stats,
3✔
693
            FlowGetMemcapExceptionPolicy(), "exception_policy.flow.memcap.",
3✔
694
            IsFlowMemcapExceptionPolicyStatsValid);
3✔
695

696
    dtv->counter_tcp_active_sessions = StatsRegisterCounter("tcp.active_sessions", &tv->stats);
3✔
697
    dtv->counter_flow_total = StatsRegisterCounter("flow.total", &tv->stats);
3✔
698
    dtv->counter_flow_active = StatsRegisterCounter("flow.active", &tv->stats);
3✔
699
    dtv->counter_flow_tcp = StatsRegisterCounter("flow.tcp", &tv->stats);
3✔
700
    dtv->counter_flow_udp = StatsRegisterCounter("flow.udp", &tv->stats);
3✔
701
    dtv->counter_flow_icmp4 = StatsRegisterCounter("flow.icmpv4", &tv->stats);
3✔
702
    dtv->counter_flow_icmp6 = StatsRegisterCounter("flow.icmpv6", &tv->stats);
3✔
703
    dtv->counter_flow_tcp_reuse = StatsRegisterCounter("flow.tcp_reuse", &tv->stats);
3✔
704
    dtv->counter_flow_elephant = StatsRegisterCounter("flow.elephant", &tv->stats);
3✔
705
    dtv->counter_flow_elephant_toserver =
3✔
706
            StatsRegisterCounter("flow.elephant_toserver", &tv->stats);
3✔
707
    dtv->counter_flow_elephant_toclient =
3✔
708
            StatsRegisterCounter("flow.elephant_toclient", &tv->stats);
3✔
709
    dtv->counter_flow_get_used = StatsRegisterCounter("flow.get_used", &tv->stats);
3✔
710
    dtv->counter_flow_get_used_eval = StatsRegisterCounter("flow.get_used_eval", &tv->stats);
3✔
711
    dtv->counter_flow_get_used_eval_reject =
3✔
712
            StatsRegisterCounter("flow.get_used_eval_reject", &tv->stats);
3✔
713
    dtv->counter_flow_get_used_eval_busy =
3✔
714
            StatsRegisterCounter("flow.get_used_eval_busy", &tv->stats);
3✔
715
    dtv->counter_flow_get_used_failed = StatsRegisterCounter("flow.get_used_failed", &tv->stats);
3✔
716

717
    dtv->counter_flow_spare_sync_avg =
3✔
718
            StatsRegisterAvgCounter("flow.wrk.spare_sync_avg", &tv->stats);
3✔
719
    dtv->counter_flow_spare_sync = StatsRegisterCounter("flow.wrk.spare_sync", &tv->stats);
3✔
720
    dtv->counter_flow_spare_sync_incomplete =
3✔
721
            StatsRegisterCounter("flow.wrk.spare_sync_incomplete", &tv->stats);
3✔
722
    dtv->counter_flow_spare_sync_empty =
3✔
723
            StatsRegisterCounter("flow.wrk.spare_sync_empty", &tv->stats);
3✔
724

725
    dtv->counter_defrag_ipv4_fragments = StatsRegisterCounter("defrag.ipv4.fragments", &tv->stats);
3✔
726
    dtv->counter_defrag_ipv4_reassembled =
3✔
727
            StatsRegisterCounter("defrag.ipv4.reassembled", &tv->stats);
3✔
728
    dtv->counter_defrag_ipv6_fragments = StatsRegisterCounter("defrag.ipv6.fragments", &tv->stats);
3✔
729
    dtv->counter_defrag_ipv6_reassembled =
3✔
730
            StatsRegisterCounter("defrag.ipv6.reassembled", &tv->stats);
3✔
731
    dtv->counter_defrag_max_hit = StatsRegisterCounter("defrag.max_trackers_reached", &tv->stats);
3✔
732
    dtv->counter_defrag_no_frags = StatsRegisterCounter("defrag.max_frags_reached", &tv->stats);
3✔
733
    dtv->counter_defrag_tracker_soft_reuse =
3✔
734
            StatsRegisterCounter("defrag.tracker_soft_reuse", &tv->stats);
3✔
735
    dtv->counter_defrag_tracker_hard_reuse =
3✔
736
            StatsRegisterCounter("defrag.tracker_hard_reuse", &tv->stats);
3✔
737
    dtv->counter_defrag_tracker_timeout =
3✔
738
            StatsRegisterCounter("defrag.wrk.tracker_timeout", &tv->stats);
3✔
739

740
    ExceptionPolicySetStatsCounters(tv, &dtv->counter_defrag_memcap_eps, &defrag_memcap_eps_stats,
3✔
741
            DefragGetMemcapExceptionPolicy(), "exception_policy.defrag.memcap.",
3✔
742
            IsDefragMemcapExceptionPolicyStatsValid);
3✔
743

744
    for (int i = 0; i < DECODE_EVENT_MAX; i++) {
642✔
745
        BUG_ON(i != (int)DEvents[i].code);
639✔
746

747
        if (i <= DECODE_EVENT_PACKET_MAX && !stats_decoder_events)
639✔
748
            continue;
×
749
        else if (i > DECODE_EVENT_PACKET_MAX && !stats_stream_events)
639✔
750
            continue;
234✔
751

752
        if (i < DECODE_EVENT_PACKET_MAX &&
405✔
753
                strncmp(DEvents[i].event_name, "decoder.", 8) == 0)
405✔
754
        {
402✔
755
            SCMutexLock(&g_counter_table_mutex);
402✔
756
            if (g_counter_table == NULL) {
402✔
757
                g_counter_table = HashTableInit(256, StringHashFunc,
2✔
758
                        StringHashCompareFunc,
2✔
759
                        StringHashFreeFunc);
2✔
760
                if (g_counter_table == NULL) {
2✔
761
                    FatalError("decoder counter hash "
×
762
                               "table init failed");
×
763
                }
×
764
            }
2✔
765

766
            char name[256];
402✔
767
            char *dot = strchr(DEvents[i].event_name, '.');
402✔
768
            BUG_ON(!dot);
402✔
769
            snprintf(name, sizeof(name), "%s.%s",
402✔
770
                    stats_decoder_events_prefix, dot+1);
402✔
771

772
            const char *found = HashTableLookup(g_counter_table, name, 0);
402✔
773
            if (!found) {
402✔
774
                char *add = SCStrdup(name);
268✔
775
                if (add == NULL)
268✔
776
                    FatalError("decoder counter hash "
×
777
                               "table name init failed");
268✔
778
                int r = HashTableAdd(g_counter_table, add, 0);
268✔
779
                if (r != 0)
268✔
780
                    FatalError("decoder counter hash "
×
781
                               "table name add failed");
268✔
782
                found = add;
268✔
783
            }
268✔
784
            dtv->counter_engine_events[i] = StatsRegisterCounter(found, &tv->stats);
402✔
785

786
            SCMutexUnlock(&g_counter_table_mutex);
402✔
787
        } else {
402✔
788
            dtv->counter_engine_events[i] = StatsRegisterCounter(DEvents[i].event_name, &tv->stats);
3✔
789
        }
3✔
790
    }
405✔
791
}
3✔
792

793
void DecodeUpdatePacketCounters(ThreadVars *tv,
794
                                const DecodeThreadVars *dtv, const Packet *p)
795
{
2,232,884✔
796
    StatsCounterIncr(&tv->stats, dtv->counter_pkts);
2,232,884✔
797
    StatsCounterAddI64(&tv->stats, dtv->counter_bytes, GET_PKT_LEN(p));
2,232,884✔
798
    StatsCounterMaxUpdateI64(&tv->stats, dtv->counter_max_pkt_size, GET_PKT_LEN(p));
2,232,884✔
799
}
2,232,884✔
800

801
/**
802
 *  \brief Debug print function for printing addresses
803
 *
804
 *  \param Address object
805
 *
806
 *  \todo IPv6
807
 */
808
void AddressDebugPrint(Address *a)
UNCOV
809
{
×
UNCOV
810
    if (a == NULL)
×
811
        return;
×
812

UNCOV
813
    switch (a->family) {
×
UNCOV
814
        case AF_INET:
×
UNCOV
815
        {
×
UNCOV
816
            char s[16];
×
UNCOV
817
            PrintInet(AF_INET, (const void *)&a->addr_data32[0], s, sizeof(s));
×
UNCOV
818
            SCLogDebug("%s", s);
×
UNCOV
819
            break;
×
820
        }
×
UNCOV
821
    }
×
UNCOV
822
}
×
823

824
/** \brief Alloc and setup DecodeThreadVars */
825
DecodeThreadVars *DecodeThreadVarsAlloc(ThreadVars *tv)
826
{
3✔
827
    DecodeThreadVars *dtv = NULL;
3✔
828

829
    if ((dtv = SCCalloc(1, sizeof(DecodeThreadVars))) == NULL)
3✔
830
        return NULL;
×
831

832
    dtv->app_tctx = AppLayerGetCtxThread();
3✔
833

834
    if (OutputFlowLogThreadInit(tv, &dtv->output_flow_thread_data) != TM_ECODE_OK) {
3✔
835
        SCLogError("initializing flow log API for thread failed");
×
836
        DecodeThreadVarsFree(tv, dtv);
×
837
        return NULL;
×
838
    }
×
839

840
    return dtv;
3✔
841
}
3✔
842

843
void DecodeThreadVarsFree(ThreadVars *tv, DecodeThreadVars *dtv)
UNCOV
844
{
×
UNCOV
845
    if (dtv != NULL) {
×
UNCOV
846
        if (dtv->app_tctx != NULL)
×
UNCOV
847
            AppLayerDestroyCtxThread(dtv->app_tctx);
×
848

UNCOV
849
        if (dtv->output_flow_thread_data != NULL)
×
UNCOV
850
            OutputFlowLogThreadDeinit(tv, dtv->output_flow_thread_data);
×
851

UNCOV
852
        SCFree(dtv);
×
UNCOV
853
    }
×
UNCOV
854
}
×
855

856
/**
857
 * \brief Set data for Packet and set length when zero copy is used
858
 *
859
 *  \param Pointer to the Packet to modify
860
 *  \param Pointer to the data
861
 *  \param Length of the data
862
 */
863
inline int PacketSetData(Packet *p, const uint8_t *pktdata, uint32_t pktlen)
UNCOV
864
{
×
UNCOV
865
    SET_PKT_LEN(p, pktlen);
×
UNCOV
866
    if (unlikely(!pktdata)) {
×
867
        return -1;
×
868
    }
×
869
    // ext_pkt cannot be const (because we sometimes copy)
UNCOV
870
    p->ext_pkt = (uint8_t *) pktdata;
×
UNCOV
871
    p->flags |= PKT_ZERO_COPY;
×
872

UNCOV
873
    return 0;
×
UNCOV
874
}
×
875

876
const char *PktSrcToString(enum PktSrcEnum pkt_src)
877
{
759,001✔
878
    const char *pkt_src_str = NULL;
759,001✔
879
    switch (pkt_src) {
759,001✔
880
        case PKT_SRC_WIRE:
750,149✔
881
            pkt_src_str = "wire/pcap";
750,149✔
882
            break;
750,149✔
UNCOV
883
        case PKT_SRC_DECODER_GRE:
×
UNCOV
884
            pkt_src_str = "gre tunnel";
×
UNCOV
885
            break;
×
UNCOV
886
        case PKT_SRC_DECODER_IPV4:
×
UNCOV
887
            pkt_src_str = "ipv4 tunnel";
×
UNCOV
888
            break;
×
UNCOV
889
        case PKT_SRC_DECODER_IPV6:
×
UNCOV
890
            pkt_src_str = "ipv6 tunnel";
×
UNCOV
891
            break;
×
UNCOV
892
        case PKT_SRC_DECODER_TEREDO:
×
UNCOV
893
            pkt_src_str = "teredo tunnel";
×
UNCOV
894
            break;
×
UNCOV
895
        case PKT_SRC_DEFRAG:
×
UNCOV
896
            pkt_src_str = "defrag";
×
UNCOV
897
            break;
×
898
        case PKT_SRC_STREAM_TCP_DETECTLOG_FLUSH:
2,539✔
899
            pkt_src_str = "stream (detect/log)";
2,539✔
900
            break;
2,539✔
901
        case PKT_SRC_FFR:
6,313✔
902
            pkt_src_str = "stream (flow timeout)";
6,313✔
903
            break;
6,313✔
UNCOV
904
        case PKT_SRC_DECODER_GENEVE:
×
UNCOV
905
            pkt_src_str = "geneve encapsulation";
×
UNCOV
906
            break;
×
UNCOV
907
        case PKT_SRC_DECODER_VXLAN:
×
UNCOV
908
            pkt_src_str = "vxlan encapsulation";
×
UNCOV
909
            break;
×
910
        case PKT_SRC_DETECT_RELOAD_FLUSH:
×
911
            pkt_src_str = "detect reload flush";
×
912
            break;
×
913
        case PKT_SRC_CAPTURE_TIMEOUT:
×
914
            pkt_src_str = "capture timeout flush";
×
915
            break;
×
916
        case PKT_SRC_SHUTDOWN_FLUSH:
×
917
            pkt_src_str = "shutdown flush";
×
918
            break;
×
919
    }
759,001✔
920
    DEBUG_VALIDATE_BUG_ON(pkt_src_str == NULL);
759,001✔
921
    return pkt_src_str;
759,001✔
922
}
759,001✔
923

924
const char *PacketDropReasonToString(enum PacketDropReason r)
UNCOV
925
{
×
UNCOV
926
    switch (r) {
×
927
        case PKT_DROP_REASON_DECODE_ERROR:
×
928
            return "decode error";
×
929
        case PKT_DROP_REASON_DEFRAG_ERROR:
×
930
            return "defrag error";
×
931
        case PKT_DROP_REASON_DEFRAG_MEMCAP:
×
932
            return "defrag memcap";
×
933
        case PKT_DROP_REASON_FLOW_MEMCAP:
×
934
            return "flow memcap";
×
UNCOV
935
        case PKT_DROP_REASON_FLOW_DROP:
×
UNCOV
936
            return "flow drop";
×
UNCOV
937
        case PKT_DROP_REASON_STREAM_ERROR:
×
UNCOV
938
            return "stream error";
×
939
        case PKT_DROP_REASON_STREAM_MEMCAP:
×
940
            return "stream memcap";
×
UNCOV
941
        case PKT_DROP_REASON_STREAM_MIDSTREAM:
×
UNCOV
942
            return "stream midstream";
×
943
        case PKT_DROP_REASON_STREAM_URG:
×
944
            return "stream urgent";
×
945
        case PKT_DROP_REASON_STREAM_REASSEMBLY:
×
946
            return "stream reassembly";
×
UNCOV
947
        case PKT_DROP_REASON_APPLAYER_ERROR:
×
UNCOV
948
            return "applayer error";
×
949
        case PKT_DROP_REASON_APPLAYER_MEMCAP:
×
950
            return "applayer memcap";
×
UNCOV
951
        case PKT_DROP_REASON_RULES:
×
UNCOV
952
            return "rules";
×
UNCOV
953
        case PKT_DROP_REASON_RULES_THRESHOLD:
×
UNCOV
954
            return "threshold detection_filter";
×
955
        case PKT_DROP_REASON_NFQ_ERROR:
×
956
            return "nfq error";
×
UNCOV
957
        case PKT_DROP_REASON_INNER_PACKET:
×
UNCOV
958
            return "tunnel packet drop";
×
UNCOV
959
        case PKT_DROP_REASON_DEFAULT_PACKET_POLICY:
×
UNCOV
960
            return "default packet policy";
×
UNCOV
961
        case PKT_DROP_REASON_DEFAULT_APP_POLICY:
×
UNCOV
962
            return "default app policy";
×
UNCOV
963
        case PKT_DROP_REASON_STREAM_PRE_HOOK:
×
UNCOV
964
            return "pre stream hook";
×
UNCOV
965
        case PKT_DROP_REASON_FLOW_PRE_HOOK:
×
UNCOV
966
            return "pre flow hook";
×
967
        case PKT_DROP_REASON_NOT_SET:
×
968
        case PKT_DROP_REASON_MAX:
×
969
            return NULL;
×
UNCOV
970
    }
×
971
    return NULL;
×
UNCOV
972
}
×
973

974
static const char *PacketDropReasonToJsonString(enum PacketDropReason r)
UNCOV
975
{
×
UNCOV
976
    switch (r) {
×
UNCOV
977
        case PKT_DROP_REASON_DECODE_ERROR:
×
UNCOV
978
            return "ips.drop_reason.decode_error";
×
UNCOV
979
        case PKT_DROP_REASON_DEFRAG_ERROR:
×
UNCOV
980
            return "ips.drop_reason.defrag_error";
×
UNCOV
981
        case PKT_DROP_REASON_DEFRAG_MEMCAP:
×
UNCOV
982
            return "ips.drop_reason.defrag_memcap";
×
UNCOV
983
        case PKT_DROP_REASON_FLOW_MEMCAP:
×
UNCOV
984
            return "ips.drop_reason.flow_memcap";
×
UNCOV
985
        case PKT_DROP_REASON_FLOW_DROP:
×
UNCOV
986
            return "ips.drop_reason.flow_drop";
×
UNCOV
987
        case PKT_DROP_REASON_STREAM_ERROR:
×
UNCOV
988
            return "ips.drop_reason.stream_error";
×
UNCOV
989
        case PKT_DROP_REASON_STREAM_MEMCAP:
×
UNCOV
990
            return "ips.drop_reason.stream_memcap";
×
UNCOV
991
        case PKT_DROP_REASON_STREAM_MIDSTREAM:
×
UNCOV
992
            return "ips.drop_reason.stream_midstream";
×
UNCOV
993
        case PKT_DROP_REASON_STREAM_URG:
×
UNCOV
994
            return "ips.drop_reason.stream_urgent";
×
UNCOV
995
        case PKT_DROP_REASON_STREAM_REASSEMBLY:
×
UNCOV
996
            return "ips.drop_reason.stream_reassembly";
×
UNCOV
997
        case PKT_DROP_REASON_APPLAYER_ERROR:
×
UNCOV
998
            return "ips.drop_reason.applayer_error";
×
UNCOV
999
        case PKT_DROP_REASON_APPLAYER_MEMCAP:
×
UNCOV
1000
            return "ips.drop_reason.applayer_memcap";
×
UNCOV
1001
        case PKT_DROP_REASON_RULES:
×
UNCOV
1002
            return "ips.drop_reason.rules";
×
UNCOV
1003
        case PKT_DROP_REASON_RULES_THRESHOLD:
×
UNCOV
1004
            return "ips.drop_reason.threshold_detection_filter";
×
UNCOV
1005
        case PKT_DROP_REASON_NFQ_ERROR:
×
UNCOV
1006
            return "ips.drop_reason.nfq_error";
×
UNCOV
1007
        case PKT_DROP_REASON_INNER_PACKET:
×
UNCOV
1008
            return "ips.drop_reason.tunnel_packet_drop";
×
UNCOV
1009
        case PKT_DROP_REASON_DEFAULT_PACKET_POLICY:
×
UNCOV
1010
            return "ips.drop_reason.default_packet_policy";
×
UNCOV
1011
        case PKT_DROP_REASON_DEFAULT_APP_POLICY:
×
UNCOV
1012
            return "ips.drop_reason.default_app_policy";
×
UNCOV
1013
        case PKT_DROP_REASON_STREAM_PRE_HOOK:
×
UNCOV
1014
            return "ips.drop_reason.pre_stream_hook";
×
UNCOV
1015
        case PKT_DROP_REASON_FLOW_PRE_HOOK:
×
UNCOV
1016
            return "ips.drop_reason.pre_flow_hook";
×
UNCOV
1017
        case PKT_DROP_REASON_NOT_SET:
×
UNCOV
1018
        case PKT_DROP_REASON_MAX:
×
UNCOV
1019
            return NULL;
×
UNCOV
1020
    }
×
1021
    return NULL;
×
UNCOV
1022
}
×
1023

1024
typedef struct CaptureStats_ {
1025
    StatsCounterId counter_ips_accepted;
1026
    StatsCounterId counter_ips_blocked;
1027
    StatsCounterId counter_ips_rejected;
1028
    StatsCounterId counter_ips_replaced;
1029

1030
    StatsCounterId counter_drop_reason[PKT_DROP_REASON_MAX];
1031
} CaptureStats;
1032

1033
thread_local CaptureStats t_capture_stats;
1034

1035
void CaptureStatsUpdate(ThreadVars *tv, const Packet *p)
1036
{
804,438✔
1037
    if (!EngineModeIsIPS() || PKT_IS_PSEUDOPKT(p))
804,438✔
1038
        return;
804,438✔
1039

UNCOV
1040
    CaptureStats *s = &t_capture_stats;
×
UNCOV
1041
    if (unlikely(PacketCheckAction(p, ACTION_REJECT_ANY))) {
×
UNCOV
1042
        StatsCounterIncr(&tv->stats, s->counter_ips_rejected);
×
UNCOV
1043
    } else if (unlikely(PacketCheckAction(p, ACTION_DROP))) {
×
UNCOV
1044
        StatsCounterIncr(&tv->stats, s->counter_ips_blocked);
×
UNCOV
1045
    } else if (unlikely(p->flags & PKT_STREAM_MODIFIED)) {
×
UNCOV
1046
        StatsCounterIncr(&tv->stats, s->counter_ips_replaced);
×
UNCOV
1047
    } else {
×
UNCOV
1048
        StatsCounterIncr(&tv->stats, s->counter_ips_accepted);
×
UNCOV
1049
    }
×
UNCOV
1050
    if (p->drop_reason != PKT_DROP_REASON_NOT_SET) {
×
UNCOV
1051
        StatsCounterIncr(&tv->stats, s->counter_drop_reason[p->drop_reason]);
×
UNCOV
1052
    }
×
UNCOV
1053
}
×
1054

1055
void CaptureStatsSetup(ThreadVars *tv)
UNCOV
1056
{
×
UNCOV
1057
    if (EngineModeIsIPS()) {
×
UNCOV
1058
        CaptureStats *s = &t_capture_stats;
×
UNCOV
1059
        s->counter_ips_accepted = StatsRegisterCounter("ips.accepted", &tv->stats);
×
UNCOV
1060
        s->counter_ips_blocked = StatsRegisterCounter("ips.blocked", &tv->stats);
×
UNCOV
1061
        s->counter_ips_rejected = StatsRegisterCounter("ips.rejected", &tv->stats);
×
UNCOV
1062
        s->counter_ips_replaced = StatsRegisterCounter("ips.replaced", &tv->stats);
×
UNCOV
1063
        for (int i = PKT_DROP_REASON_NOT_SET; i < PKT_DROP_REASON_MAX; i++) {
×
UNCOV
1064
            const char *name = PacketDropReasonToJsonString(i);
×
UNCOV
1065
            if (name != NULL)
×
UNCOV
1066
                s->counter_drop_reason[i] = StatsRegisterCounter(name, &tv->stats);
×
UNCOV
1067
        }
×
UNCOV
1068
    }
×
UNCOV
1069
}
×
1070

1071
void DecodeGlobalConfig(void)
1072
{
2✔
1073
    DecodeTeredoConfig();
2✔
1074
    DecodeGeneveConfig();
2✔
1075
    DecodeVXLANConfig();
2✔
1076
    DecodeERSPANConfig();
2✔
1077
    intmax_t value = 0;
2✔
1078
    if (SCConfGetInt("decoder.max-layers", &value) == 1) {
2✔
1079
        if (value < 0 || value > UINT8_MAX) {
×
1080
            SCLogWarning("Invalid value for decoder.max-layers");
×
1081
        } else {
×
1082
            decoder_max_layers = (uint8_t)value;
×
1083
        }
×
1084
    }
×
1085
    PacketAlertGetMaxConfig();
2✔
1086
}
2✔
1087

1088
void PacketAlertGetMaxConfig(void)
1089
{
2✔
1090
    intmax_t max = 0;
2✔
1091
    if (SCConfGetInt("packet-alert-max", &max) == 1) {
2✔
UNCOV
1092
        if (max <= 0 || max > UINT8_MAX) {
×
UNCOV
1093
            SCLogWarning("Invalid value for packet-alert-max, default value set instead");
×
UNCOV
1094
        } else {
×
UNCOV
1095
            packet_alert_max = (uint16_t)max;
×
UNCOV
1096
        }
×
UNCOV
1097
    }
×
1098
    SCLogDebug("detect->packet_alert_max set to %d", packet_alert_max);
2✔
1099
}
2✔
1100

1101
static inline bool PcapPacketCntRunmodeCanAccess(void)
1102
{
2,177,248✔
1103
    SCRunMode m = SCRunmodeGet();
2,177,248✔
1104
    return m == RUNMODE_PCAP_FILE || m == RUNMODE_UNITTEST || m == RUNMODE_UNIX_SOCKET;
2,177,248✔
1105
}
2,177,248✔
1106

1107
inline uint64_t PcapPacketCntGet(const Packet *p)
1108
{
759,090✔
1109
    if (PcapPacketCntRunmodeCanAccess() && p != NULL) {
759,090✔
1110
        return p->pcap_v.pcap_cnt;
759,090✔
1111
    }
759,090✔
UNCOV
1112
    return 0;
×
1113
}
759,090✔
1114

1115
inline void PcapPacketCntSet(Packet *p, uint64_t pcap_cnt)
1116
{
1,418,158✔
1117
    if (PcapPacketCntRunmodeCanAccess() && p != NULL) {
1,418,158✔
1118
        p->pcap_v.pcap_cnt = pcap_cnt;
1,418,158✔
1119
    }
1,418,158✔
1120
}
1,418,158✔
1121

1122
/**
1123
 * @}
1124
 */
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