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

OISF / suricata / 23373309628

21 Mar 2026 05:51AM UTC coverage: 79.187% (-0.1%) from 79.315%
23373309628

Pull #15029

github

web-flow
Merge 3c66d337f into 6587e363a
Pull Request #15029: Fw updates/v5

427 of 449 new or added lines in 26 files covered. (95.1%)

421 existing lines in 35 files now uncovered.

265408 of 335166 relevant lines covered (79.19%)

4177560.49 hits per line

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

87.63
/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
{
26,409,591✔
146
    PacketAlert *pa_array = SCCalloc(packet_alert_max, sizeof(PacketAlert));
26,409,591✔
147
    DEBUG_VALIDATE_BUG_ON(pa_array == NULL);
26,409,591✔
148

149
    return pa_array;
26,409,591✔
150
}
26,409,591✔
151

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

169
void PacketAlertFree(PacketAlert *pa_array)
170
{
26,409,136✔
171
    if (pa_array == NULL)
26,409,136✔
172
        return;
×
173
    for (int i = 0; i < packet_alert_max; i++) {
422,378,045✔
174
        struct PacketContextData *allocated_json = pa_array[i].json_info;
395,968,909✔
175
        while (allocated_json) {
395,968,931✔
176
            struct PacketContextData *next_json = allocated_json->next;
22✔
177
            SCFree(allocated_json->json_string);
22✔
178
            SCFree(allocated_json);
22✔
179
            allocated_json = next_json;
22✔
180
        }
22✔
181
    }
395,968,909✔
182
    SCFree(pa_array);
26,409,136✔
183
}
26,409,136✔
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
{
36,553✔
191
    switch (proto) {
36,553✔
192
        case DECODE_TUNNEL_PPP:
7,119✔
193
            return DecodePPP(tv, dtv, p, pkt, len);
7,119✔
194
        case DECODE_TUNNEL_IPV4:
3,249✔
195
            DEBUG_VALIDATE_BUG_ON(len > UINT16_MAX);
3,249✔
196
            return DecodeIPV4(tv, dtv, p, pkt, (uint16_t)len);
3,249✔
197
        case DECODE_TUNNEL_IPV6:
1,231✔
198
        case DECODE_TUNNEL_IPV6_TEREDO:
2,383✔
199
            DEBUG_VALIDATE_BUG_ON(len > UINT16_MAX);
2,383✔
200
            return DecodeIPV6(tv, dtv, p, pkt, (uint16_t)len);
2,383✔
201
        case DECODE_TUNNEL_VLAN:
639✔
202
            return DecodeVLAN(tv, dtv, p, pkt, len);
639✔
203
        case DECODE_TUNNEL_ETHERNET:
736✔
204
            return DecodeEthernet(tv, dtv, p, pkt, len);
736✔
205
        case DECODE_TUNNEL_ERSPANII:
2,028✔
206
            return DecodeERSPAN(tv, dtv, p, pkt, len);
2,028✔
207
        case DECODE_TUNNEL_ERSPANI:
340✔
208
            return DecodeERSPANTypeI(tv, dtv, p, pkt, len);
340✔
209
        case DECODE_TUNNEL_VXLAN:
19,967✔
210
            return DecodeEthernet(tv, dtv, p, pkt, len);
19,967✔
211
        case DECODE_TUNNEL_NSH:
×
212
            return DecodeNSH(tv, dtv, p, pkt, len);
×
213
        case DECODE_TUNNEL_ARP:
92✔
214
            return DecodeARP(tv, dtv, p, pkt, len);
92✔
215
        default:
×
216
            SCLogDebug("FIXME: DecodeTunnel: protocol %" PRIu32 " not supported.", proto);
×
217
            break;
×
218
    }
36,553✔
219
    return TM_ECODE_OK;
×
220
}
36,553✔
221

222
/**
223
 * \brief Return a malloced packet.
224
 */
225
void PacketFree(Packet *p)
226
{
26,409,182✔
227
    PacketDestructor(p);
26,409,182✔
228
    SCFree(p);
26,409,182✔
229
}
26,409,182✔
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
{
114,887,086✔
240
    if (p->flags & PKT_IS_INVALID) {
114,887,086✔
241
        StatsCounterIncr(&tv->stats, dtv->counter_invalid);
449,410✔
242
    }
449,410✔
243
}
114,887,086✔
244

245
void PacketUpdateEngineEventCounters(ThreadVars *tv,
246
        DecodeThreadVars *dtv, Packet *p)
247
{
115,897,788✔
248
    for (uint8_t i = 0; i < p->events.cnt; i++) {
233,687,524✔
249
        const uint8_t e = p->events.events[i];
117,789,736✔
250

251
        if (e <= DECODE_EVENT_PACKET_MAX && !stats_decoder_events)
117,789,736✔
252
            continue;
×
253
        else if (e > DECODE_EVENT_PACKET_MAX && !stats_stream_events)
117,789,736✔
254
            continue;
100,849✔
255
        StatsCounterIncr(&tv->stats, dtv->counter_engine_events[e]);
117,688,887✔
256
    }
117,688,887✔
257
}
115,897,788✔
258

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

273
    SCLogDebug("allocated a new packet only using alloc...");
26,409,306✔
274

275
    PACKET_PROFILING_START(p);
26,409,306✔
276
    return p;
26,409,306✔
277
}
26,409,306✔
278

279
/**
280
 * \brief Return a packet to where it was allocated.
281
 */
282
void PacketFreeOrRelease(Packet *p)
283
{
120,251,817✔
284
    if (likely(p->pool != NULL)) {
121,400,721✔
285
        p->ReleasePacket = PacketPoolReturnPacket;
119,420,489✔
286
        PacketPoolReturnPacket(p);
119,420,489✔
287
    } else {
2,150,262,401✔
288
        PacketFree(p);
2,149,463,879✔
289
    }
2,149,463,879✔
290
}
120,251,817✔
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
{
114,524,227✔
301
    /* try the pool first */
302
    Packet *p = PacketPoolGetPacket();
114,524,227✔
303

304
    if (p == NULL) {
114,524,227✔
305
        /* non fatal, we're just not processing a packet then */
306
        p = PacketGetFromAlloc();
1,979,035✔
307
    } else {
113,693,177✔
308
        DEBUG_VALIDATE_BUG_ON(p->ReleasePacket != PacketPoolReturnPacket);
112,545,192✔
309
        PACKET_PROFILING_START(p);
112,545,192✔
310
    }
112,545,192✔
311

312
    return p;
114,524,227✔
313
}
114,524,227✔
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
{
5,135,016✔
343
    if (unlikely(offset + datalen > MAX_PAYLOAD_SIZE)) {
5,135,016✔
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) {
5,134,964✔
351
        uint32_t newsize = offset + datalen;
5,130,112✔
352
        // check overflow
353
        if (newsize < offset)
5,130,112✔
354
            return -1;
×
355
        if (newsize <= default_packet_size) {
5,130,112✔
356
            /* data will fit in memory allocated with packet */
357
            memcpy(GET_PKT_DIRECT_DATA(p) + offset, data, datalen);
5,100,091✔
358
        } else {
5,100,091✔
359
            /* here we need a dynamic allocation */
360
            p->ext_pkt = SCMalloc(MAX_PAYLOAD_SIZE);
30,021✔
361
            if (unlikely(p->ext_pkt == NULL)) {
30,021✔
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));
30,021✔
367
            /* copy data as asked */
368
            memcpy(p->ext_pkt + offset, data, datalen);
30,021✔
369
        }
30,021✔
370
    } else {
5,130,112✔
371
        memcpy(p->ext_pkt + offset, data, datalen);
4,852✔
372
    }
4,852✔
373
    return 0;
5,134,964✔
374
}
5,134,964✔
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
{
5,084,965✔
385
    SET_PKT_LEN(p, pktlen);
5,084,965✔
386
    return PacketCopyDataOffset(p, 0, pktdata, pktlen);
5,084,965✔
387
}
5,084,965✔
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
{
36,590✔
402
    int ret;
36,590✔
403

404
    SCEnter();
36,590✔
405

406
    if (parent->nb_decoded_layers + 1 >= decoder_max_layers) {
36,590✔
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();
36,553✔
413
    if (unlikely(p == NULL)) {
36,553✔
414
        SCReturnPtr(NULL, "Packet");
×
415
    }
×
416

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

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

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

442
    if (unlikely(ret != TM_ECODE_OK) ||
36,553✔
443
            (proto == DECODE_TUNNEL_IPV6_TEREDO && (p->flags & PKT_IS_INVALID)))
36,553✔
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) {
31,959✔
454
        parent->ttype = PacketTunnelRoot;
30,694✔
455
    }
30,694✔
456
    TUNNEL_INCR_PKT_TPR(p);
31,959✔
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);
31,959✔
462
    SCReturnPtr(p, "Packet");
31,959✔
463
}
36,553✔
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,986✔
481
    SCEnter();
4,986✔
482

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

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

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

511
    SCReturnPtr(p, "Packet");
4,986✔
512
}
4,986✔
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,986✔
520
    /* tell parent packet it's part of a tunnel */
521
    if (parent->ttype == PacketTunnelNone)
4,986✔
522
        parent->ttype = PacketTunnelRoot;
4,974✔
523

524
    /* increment tunnel packet refcnt in the root packet */
525
    TUNNEL_INCR_PKT_TPR(parent);
4,986✔
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,986✔
531
}
4,986✔
532

533
/**
534
 *  \note if p->flow is set, the flow is locked
535
 */
536
void PacketBypassCallback(Packet *p)
537
{
28✔
538
    if (PKT_IS_PSEUDOPKT(p))
28✔
539
        return;
3✔
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) {
25✔
574
        int state = p->flow->flow_state;
25✔
575
        if (state == FLOW_STATE_LOCAL_BYPASSED)
25✔
576
            return;
577
        FlowUpdateState(p->flow, FLOW_STATE_LOCAL_BYPASSED);
25✔
578
    }
25✔
579
#endif
25✔
580
}
25✔
581

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

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

597
        if (p->flowflags & FLOW_PKT_TOCLIENT_FIRST) {
1,293✔
598
            p->flowflags &= ~FLOW_PKT_TOCLIENT_FIRST;
1,246✔
599
            p->flowflags |= FLOW_PKT_TOSERVER_FIRST;
1,246✔
600
        }
1,246✔
601
    }
1,293✔
602
}
4,040✔
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)
609
{
3,427✔
610
    SCMutexLock(&g_counter_table_mutex);
3,427✔
611
    if (g_counter_table) {
3,427✔
612
        HashTableFree(g_counter_table);
3,427✔
613
        g_counter_table = NULL;
3,427✔
614
    }
3,427✔
615
    SCMutexUnlock(&g_counter_table_mutex);
3,427✔
616
}
3,427✔
617

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

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

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

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

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

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

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

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

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

744
    for (int i = 0; i < DECODE_EVENT_MAX; i++) {
3,643,778✔
745
        BUG_ON(i != (int)DEvents[i].code);
3,626,751✔
746

747
        if (i <= DECODE_EVENT_PACKET_MAX && !stats_decoder_events)
3,626,751✔
748
            continue;
×
749
        else if (i > DECODE_EVENT_PACKET_MAX && !stats_stream_events)
3,626,751✔
750
            continue;
1,327,716✔
751

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

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

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

786
            SCMutexUnlock(&g_counter_table_mutex);
2,281,618✔
787
        } else {
2,281,618✔
788
            dtv->counter_engine_events[i] = StatsRegisterCounter(DEvents[i].event_name, &tv->stats);
17,417✔
789
        }
17,417✔
790
    }
2,299,035✔
791
}
17,027✔
792

793
void DecodeUpdatePacketCounters(ThreadVars *tv,
794
                                const DecodeThreadVars *dtv, const Packet *p)
795
{
120,490,104✔
796
    StatsCounterIncr(&tv->stats, dtv->counter_pkts);
120,490,104✔
797
    StatsCounterAddI64(&tv->stats, dtv->counter_bytes, GET_PKT_LEN(p));
120,490,104✔
798
    StatsCounterMaxUpdateI64(&tv->stats, dtv->counter_max_pkt_size, GET_PKT_LEN(p));
120,490,104✔
799
}
120,490,104✔
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)
809
{
1✔
810
    if (a == NULL)
1✔
811
        return;
×
812

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

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

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

832
    dtv->app_tctx = AppLayerGetCtxThread();
17,027✔
833

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

840
    return dtv;
17,027✔
841
}
17,027✔
842

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

849
        if (dtv->output_flow_thread_data != NULL)
17,024✔
850
            OutputFlowLogThreadDeinit(tv, dtv->output_flow_thread_data);
17,024✔
851

852
        SCFree(dtv);
17,024✔
853
    }
17,024✔
854
}
17,024✔
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)
864
{
112,665,736✔
865
    SET_PKT_LEN(p, pktlen);
112,665,736✔
866
    if (unlikely(!pktdata)) {
112,665,736✔
867
        return -1;
×
868
    }
×
869
    // ext_pkt cannot be const (because we sometimes copy)
870
    p->ext_pkt = (uint8_t *) pktdata;
112,665,736✔
871
    p->flags |= PKT_ZERO_COPY;
112,665,736✔
872

873
    return 0;
112,665,736✔
874
}
112,665,736✔
875

876
const char *PktSrcToString(enum PktSrcEnum pkt_src)
877
{
1,855,617✔
878
    const char *pkt_src_str = NULL;
1,855,617✔
879
    switch (pkt_src) {
1,855,617✔
880
        case PKT_SRC_WIRE:
1,843,864✔
881
            pkt_src_str = "wire/pcap";
1,843,864✔
882
            break;
1,843,864✔
883
        case PKT_SRC_DECODER_GRE:
134✔
884
            pkt_src_str = "gre tunnel";
134✔
885
            break;
134✔
886
        case PKT_SRC_DECODER_IPV4:
5✔
887
            pkt_src_str = "ipv4 tunnel";
5✔
888
            break;
5✔
889
        case PKT_SRC_DECODER_IPV6:
4✔
890
            pkt_src_str = "ipv6 tunnel";
4✔
891
            break;
4✔
892
        case PKT_SRC_DECODER_TEREDO:
45✔
893
            pkt_src_str = "teredo tunnel";
45✔
894
            break;
45✔
895
        case PKT_SRC_DEFRAG:
7✔
896
            pkt_src_str = "defrag";
7✔
897
            break;
7✔
898
        case PKT_SRC_STREAM_TCP_DETECTLOG_FLUSH:
3,491✔
899
            pkt_src_str = "stream (detect/log)";
3,491✔
900
            break;
3,491✔
901
        case PKT_SRC_FFR:
7,970✔
902
            pkt_src_str = "stream (flow timeout)";
7,970✔
903
            break;
7,970✔
904
        case PKT_SRC_DECODER_GENEVE:
36✔
905
            pkt_src_str = "geneve encapsulation";
36✔
906
            break;
36✔
907
        case PKT_SRC_DECODER_VXLAN:
54✔
908
            pkt_src_str = "vxlan encapsulation";
54✔
909
            break;
54✔
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
    }
1,855,617✔
920
    DEBUG_VALIDATE_BUG_ON(pkt_src_str == NULL);
1,855,626✔
921
    return pkt_src_str;
1,855,626✔
922
}
1,855,617✔
923

924
const char *PacketDropReasonToString(enum PacketDropReason r)
925
{
3,616✔
926
    switch (r) {
3,616✔
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";
×
935
        case PKT_DROP_REASON_FLOW_DROP:
2,341✔
936
            return "flow drop";
2,341✔
937
        case PKT_DROP_REASON_STREAM_ERROR:
10✔
938
            return "stream error";
10✔
939
        case PKT_DROP_REASON_STREAM_MEMCAP:
×
940
            return "stream memcap";
×
941
        case PKT_DROP_REASON_STREAM_MIDSTREAM:
14✔
942
            return "stream midstream";
14✔
943
        case PKT_DROP_REASON_STREAM_URG:
×
944
            return "stream urgent";
×
945
        case PKT_DROP_REASON_STREAM_REASSEMBLY:
×
946
            return "stream reassembly";
×
947
        case PKT_DROP_REASON_APPLAYER_ERROR:
2✔
948
            return "applayer error";
2✔
949
        case PKT_DROP_REASON_APPLAYER_MEMCAP:
×
950
            return "applayer memcap";
×
951
        case PKT_DROP_REASON_RULES:
715✔
952
            return "rules";
715✔
953
        case PKT_DROP_REASON_RULES_THRESHOLD:
8✔
954
            return "threshold detection_filter";
8✔
955
        case PKT_DROP_REASON_NFQ_ERROR:
×
956
            return "nfq error";
×
957
        case PKT_DROP_REASON_INNER_PACKET:
21✔
958
            return "tunnel packet drop";
21✔
959
        case PKT_DROP_REASON_DEFAULT_PACKET_POLICY:
480✔
960
            return "default packet policy";
480✔
961
        case PKT_DROP_REASON_DEFAULT_APP_POLICY:
10✔
962
            return "default app policy";
10✔
963
        case PKT_DROP_REASON_STREAM_PRE_HOOK:
2✔
964
            return "pre stream hook";
2✔
965
        case PKT_DROP_REASON_FLOW_PRE_HOOK:
13✔
966
            return "pre flow hook";
13✔
967
        case PKT_DROP_REASON_NOT_SET:
×
968
        case PKT_DROP_REASON_MAX:
×
969
            return NULL;
×
970
    }
3,616✔
971
    return NULL;
×
972
}
3,616✔
973

974
static const char *PacketDropReasonToJsonString(enum PacketDropReason r)
975
{
22,974✔
976
    switch (r) {
22,974✔
977
        case PKT_DROP_REASON_DECODE_ERROR:
1,094✔
978
            return "ips.drop_reason.decode_error";
1,094✔
979
        case PKT_DROP_REASON_DEFRAG_ERROR:
1,094✔
980
            return "ips.drop_reason.defrag_error";
1,094✔
981
        case PKT_DROP_REASON_DEFRAG_MEMCAP:
1,094✔
982
            return "ips.drop_reason.defrag_memcap";
1,094✔
983
        case PKT_DROP_REASON_FLOW_MEMCAP:
1,094✔
984
            return "ips.drop_reason.flow_memcap";
1,094✔
985
        case PKT_DROP_REASON_FLOW_DROP:
1,094✔
986
            return "ips.drop_reason.flow_drop";
1,094✔
987
        case PKT_DROP_REASON_STREAM_ERROR:
1,094✔
988
            return "ips.drop_reason.stream_error";
1,094✔
989
        case PKT_DROP_REASON_STREAM_MEMCAP:
1,094✔
990
            return "ips.drop_reason.stream_memcap";
1,094✔
991
        case PKT_DROP_REASON_STREAM_MIDSTREAM:
1,094✔
992
            return "ips.drop_reason.stream_midstream";
1,094✔
993
        case PKT_DROP_REASON_STREAM_URG:
1,094✔
994
            return "ips.drop_reason.stream_urgent";
1,094✔
995
        case PKT_DROP_REASON_STREAM_REASSEMBLY:
1,094✔
996
            return "ips.drop_reason.stream_reassembly";
1,094✔
997
        case PKT_DROP_REASON_APPLAYER_ERROR:
1,094✔
998
            return "ips.drop_reason.applayer_error";
1,094✔
999
        case PKT_DROP_REASON_APPLAYER_MEMCAP:
1,094✔
1000
            return "ips.drop_reason.applayer_memcap";
1,094✔
1001
        case PKT_DROP_REASON_RULES:
1,094✔
1002
            return "ips.drop_reason.rules";
1,094✔
1003
        case PKT_DROP_REASON_RULES_THRESHOLD:
1,094✔
1004
            return "ips.drop_reason.threshold_detection_filter";
1,094✔
1005
        case PKT_DROP_REASON_NFQ_ERROR:
1,094✔
1006
            return "ips.drop_reason.nfq_error";
1,094✔
1007
        case PKT_DROP_REASON_INNER_PACKET:
1,094✔
1008
            return "ips.drop_reason.tunnel_packet_drop";
1,094✔
1009
        case PKT_DROP_REASON_DEFAULT_PACKET_POLICY:
1,094✔
1010
            return "ips.drop_reason.default_packet_policy";
1,094✔
1011
        case PKT_DROP_REASON_DEFAULT_APP_POLICY:
1,094✔
1012
            return "ips.drop_reason.default_app_policy";
1,094✔
1013
        case PKT_DROP_REASON_STREAM_PRE_HOOK:
1,094✔
1014
            return "ips.drop_reason.pre_stream_hook";
1,094✔
1015
        case PKT_DROP_REASON_FLOW_PRE_HOOK:
1,094✔
1016
            return "ips.drop_reason.pre_flow_hook";
1,094✔
1017
        case PKT_DROP_REASON_NOT_SET:
1,094✔
1018
        case PKT_DROP_REASON_MAX:
1,094✔
1019
            return NULL;
1,094✔
1020
    }
22,974✔
1021
    return NULL;
×
1022
}
22,974✔
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
{
112,190,762✔
1037
    if (!EngineModeIsIPS() || PKT_IS_PSEUDOPKT(p))
112,190,762✔
1038
        return;
66,728,222✔
1039

1040
    CaptureStats *s = &t_capture_stats;
45,462,540✔
1041
    if (unlikely(PacketCheckAction(p, ACTION_REJECT_ANY))) {
45,462,540✔
1042
        StatsCounterIncr(&tv->stats, s->counter_ips_rejected);
13✔
1043
    } else if (unlikely(PacketCheckAction(p, ACTION_DROP))) {
45,462,527✔
1044
        StatsCounterIncr(&tv->stats, s->counter_ips_blocked);
5,662✔
1045
    } else if (unlikely(p->flags & PKT_STREAM_MODIFIED)) {
45,456,865✔
1046
        StatsCounterIncr(&tv->stats, s->counter_ips_replaced);
7✔
1047
    } else {
45,456,858✔
1048
        StatsCounterIncr(&tv->stats, s->counter_ips_accepted);
45,456,858✔
1049
    }
45,456,858✔
1050
    if (p->drop_reason != PKT_DROP_REASON_NOT_SET) {
45,462,540✔
1051
        StatsCounterIncr(&tv->stats, s->counter_drop_reason[p->drop_reason]);
5,675✔
1052
    }
5,675✔
1053
}
45,462,540✔
1054

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

1071
void DecodeGlobalConfig(void)
1072
{
2,218✔
1073
    DecodeTeredoConfig();
2,218✔
1074
    DecodeGeneveConfig();
2,218✔
1075
    DecodeVXLANConfig();
2,218✔
1076
    DecodeERSPANConfig();
2,218✔
1077
    intmax_t value = 0;
2,218✔
1078
    if (SCConfGetInt("decoder.max-layers", &value) == 1) {
2,218✔
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,218✔
1086
}
2,218✔
1087

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

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

1107
inline uint64_t PcapPacketCntGet(const Packet *p)
1108
{
1,861,608✔
1109
    if (PcapPacketCntRunmodeCanAccess() && p != NULL) {
1,861,608✔
1110
        return p->pcap_v.pcap_cnt;
1,861,343✔
1111
    }
1,861,343✔
1112
    return 0;
265✔
1113
}
1,861,608✔
1114

1115
inline void PcapPacketCntSet(Packet *p, uint64_t pcap_cnt)
1116
{
1,418,188✔
1117
    if (PcapPacketCntRunmodeCanAccess() && p != NULL) {
1,418,188✔
1118
        p->pcap_v.pcap_cnt = pcap_cnt;
1,418,188✔
1119
    }
1,418,188✔
1120
}
1,418,188✔
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