]> The Tcpdump Group git mirrors - libpcap/commit
Require a live capture for all Linux BPF extensions.
authorDenis Ovsienko <[email protected]>
Thu, 23 Jan 2025 19:13:38 +0000 (19:13 +0000)
committerDenis Ovsienko <[email protected]>
Sat, 25 Jan 2025 11:56:42 +0000 (11:56 +0000)
commitf9f9b228943efeb18f9d5867e4dd0b8deb11feb5
tree91618a896c0cc8c7db16fb95e821a517ca03694e
parent31e311a5537fa6c30284ea05f91bc91a52d9e107
Require a live capture for all Linux BPF extensions.

On Linux compiling a filter that includes "inbound", "outbound" or
"ifindex" for a pcap_t that was created using pcap_open_dead() produces
bytecode that uses BPF extensions if the DLT is not one of the known
DLTs:

$ filtertest RAW '(inbound and ifindex 1) or (outbound and ifindex 2)'
(000) ldh      [type]
(001) jeq      #0x4             jt 2 jf 4
(002) ld       [ifidx]
(003) jeq      #0x2             jt 6 jf 7
(004) ld       [ifidx]
(005) jeq      #0x1             jt 6 jf 7
(006) ret      #262144
(007) ret      #0

This is because both gen_ifindex() and gen_inbound_outbound() assume
that a live capture is equivalent to rfile == NULL, which is not true.

Fix this by using the logic that already works correctly for the "vlan"
keyword.  Define a new flag for bpf_codegen_flags and set it in
setup_socket() (which is in the code path for a live capture only) if
any BPF extensions are available.  Check for the flag in a new helper
function and use it instead of the incorrect rfile tests.  Set the flag
in filtertest as well.  Now pcap_compile() handles both cases correctly
on Linux:

$ filtertest RAW 'ifindex 1'
filtertest: ifindex not supported on Raw IP (not a live capture)

$ filtertest -l RAW 'ifindex 1'
(000) ld       [ifidx]
(001) jeq      #0x1             jt 2 jf 3
(002) ret      #262144
(003) ret      #0

Make two existing accept tests for "inbound" and "outbound" specific to
the extensions and add a similar test for "ifindex".  Add more reject
tests to cover the three keywords on Linux w/o extensions and non-Linux
OSes separately.
CHANGES
gencode.c
pcap-int.h
pcap-linux.c
testprogs/TESTrun
testprogs/filtertest.c