2 * netsniff-ng - the packet sniffing beast
3 * Copyright 2013 Daniel Borkmann.
4 * Subject to the GPL, version 2.
8 #include <linux/filter.h>
15 void bpf_try_compile(const char *rulefile
, struct sock_fprog
*bpf
, uint32_t link_type
)
19 struct sock_filter
*out
;
20 struct bpf_program _bpf
;
21 const struct bpf_insn
*ins
;
23 pcap_handle
= pcap_open_dead(link_type
, 65535);
25 panic("Cannot open fake pcap_t for compiling BPF code");
27 ret
= pcap_compile(pcap_handle
, &_bpf
, rulefile
, 1, PCAP_NETMASK_UNKNOWN
);
28 pcap_close(pcap_handle
);
30 panic("Cannot compile filter: %s\n", rulefile
);
32 bpf
->len
= _bpf
.bf_len
;
33 bpf
->filter
= xrealloc(bpf
->filter
, bpf
->len
* sizeof(*out
));
35 for (i
= 0, ins
= _bpf
.bf_insns
, out
= bpf
->filter
; i
< bpf
->len
;
37 out
->code
= ins
->code
;
42 if (out
->code
== 0x06 && out
->k
> 0)
48 if (__bpf_validate(bpf
) == 0)
49 panic("This is not a valid BPF program!\n");