Coverage Report

Created: 2026-01-16 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/util-bpf.c
Line
Count
Source
1
/* Copyright (C) 2018 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17
18
/**
19
 * \file
20
 *
21
 * \author Eric Leblond <eric@regit.org>
22
 */
23
24
#include "suricata-common.h"
25
#include "util-bpf.h"
26
#include "threads.h"
27
#include "conf.h"
28
#include "util-debug.h"
29
30
void ConfSetBPFFilter(
31
        ConfNode *if_root, ConfNode *if_default, const char *iface, const char **bpf_filter)
32
0
{
33
0
    if (*bpf_filter != NULL) {
34
0
        SCLogInfo("BPF filter already configured");
35
0
        return;
36
0
    }
37
38
    /* command line value has precedence */
39
0
    if (ConfGet("bpf-filter", bpf_filter) == 1) {
40
0
        if (strlen(*bpf_filter) > 0) {
41
0
            SCLogConfig("%s: using command-line provided bpf filter '%s'", iface, *bpf_filter);
42
0
        }
43
0
    } else if (ConfGetChildValueWithDefault(if_root, if_default, "bpf-filter", bpf_filter) ==
44
0
               1) { // reading from a file
45
0
        if (strlen(*bpf_filter) > 0) {
46
0
            SCLogConfig("%s: using file provided bpf filter %s", iface, *bpf_filter);
47
0
        }
48
0
    } else {
49
0
        SCLogDebug("No BPF filter found, skipping");
50
0
    }
51
0
}
52
53
/** protect bpf filter build, as it is not thread safe */
54
static SCMutex bpf_set_filter_lock = SCMUTEX_INITIALIZER;
55
56
void SCBPFFree(struct bpf_program *program)
57
0
{
58
0
    if (program)
59
0
        pcap_freecode(program);
60
0
}
61
62
int SCBPFCompile(int snaplen_arg, int linktype_arg, struct bpf_program *program,
63
                 const char *buf,
64
                 int optimize, uint32_t mask,
65
                 char *errbuf, size_t errbuf_len)
66
0
{
67
0
    pcap_t *p;
68
0
    int ret;
69
70
0
    p = pcap_open_dead(linktype_arg, snaplen_arg);
71
0
    if (p == NULL)
72
0
        return (-1);
73
74
0
    SCMutexLock(&bpf_set_filter_lock);
75
0
    ret = pcap_compile(p, program, buf, optimize, mask);
76
0
    if (ret == -1) {
77
0
        if (errbuf) {
78
0
            snprintf(errbuf, errbuf_len, "%s", pcap_geterr(p));
79
0
        }
80
0
        pcap_close(p);
81
0
        SCMutexUnlock(&bpf_set_filter_lock);
82
0
        return (-1);
83
0
    }
84
0
    pcap_close(p);
85
0
    SCMutexUnlock(&bpf_set_filter_lock);
86
87
0
    if (program->bf_insns == NULL) {
88
0
        if (errbuf) {
89
0
            snprintf(errbuf, errbuf_len, "Filter badly setup");
90
0
        }
91
0
        SCBPFFree(program);
92
0
        return (-1);
93
0
    }
94
95
0
    return (ret);
96
0
}