netsniff-ng: add packet fanout support
[netsniff-ng.git] / bpf_comp.c
blob3434daf8337ffafbf882d368ad490cf9fa27d8e1
1 /*
2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
5 */
7 #include <pcap.h>
8 #include <linux/filter.h>
10 #include "xmalloc.h"
11 #include "config.h"
12 #include "bpf.h"
13 #include "die.h"
15 void bpf_try_compile(const char *rulefile, struct sock_fprog *bpf, uint32_t link_type)
17 int i, ret;
18 const struct bpf_insn *ins;
19 struct sock_filter *out;
20 struct bpf_program _bpf;
22 ret = pcap_compile_nopcap(65535, link_type, &_bpf, rulefile, 1, 0xffffffff);
23 if (ret < 0)
24 panic("Cannot compile filter %s\n", rulefile);
26 bpf->len = _bpf.bf_len;
27 bpf->filter = xrealloc(bpf->filter, bpf->len * sizeof(*out));
29 for (i = 0, ins = _bpf.bf_insns, out = bpf->filter; i < bpf->len;
30 ++i, ++ins, ++out) {
31 out->code = ins->code;
32 out->jt = ins->jt;
33 out->jf = ins->jf;
34 out->k = ins->k;
36 if (out->code == 0x06 && out->k > 0)
37 out->k = 0xFFFFFFFF;
40 pcap_freecode(&_bpf);
42 if (__bpf_validate(bpf) == 0)
43 panic("This is not a valid BPF program!\n");