From: Denis Ovsienko Date: Wed, 26 Feb 2025 13:49:49 +0000 (+0000) Subject: Have "outbound" mean Tx only for DLT_SLIP. X-Git-Url: https://git.tcpdump.org/libpcap/commitdiff_plain/127435bf8c1e3eb08166a6004b74d638e5afd4fb Have "outbound" mean Tx only for DLT_SLIP. In DLT_SLIP packet direction is the first and the only byte of the link-layer header, so it ought to be trivial to test, which the optimized filter program seems to implement: $ ./testprogs/filtertest SLIP outbound (000) ldb [0] (001) jeq #0x0 jt 2 jf 3 (002) ret #0 (003) ret #262144 However, the C code in gen_inbound_outbound() looks more complicated than the above requires: since libpcap 0.0 (June 1994) it has been a combination of four functions, and the unoptimized filter program it produces looks more complicated than it needs to be: $ ./testprogs/filtertest -O SLIP outbound (000) ld #0x0 (001) st M[1] (002) ldx M[1] (003) ldb [x + 0] (004) st M[2] (005) ld #0x0 (006) st M[0] (007) ldx M[0] (008) ld M[2] (009) sub x (010) jeq #0x0 jt 11 jf 12 (011) ret #0 (012) ret #262144 The C code means loading 1 byte from offset 0 of the link-layer header, testing it to be equal to 0 and reversing the result if testing for "outbound". The test for "inbound" correctly matches direction value 0 (received packets). The test for "outbound" matches direction value 1 (transmitted packets), as well as any other non-zero direction value, which is invalid in DLT_SLIP. Replace the custom code with gen_cmp() consistently with the other cases in the same switch block. This makes the unoptimized and the optimized filter programs identical, also fixes "outbound" to match Tx packets only. Update the affected filter tests to reflect these developments. --- diff --git a/CHANGES b/CHANGES index db338b35..c2dffc2a 100644 --- a/CHANGES +++ b/CHANGES @@ -49,6 +49,7 @@ DayOfTheWeek, Month DD, YYYY / The Tcpdump Group Fix the != comparison for ATM and MTP field values. Deprecate bpf_filter(). Require a live capture for all Linux BPF extensions. + Have "outbound" mean Tx only for DLT_SLIP. rpcap: Support user names and passwords in rpcap:// and rpcaps:// URLs. Add a -t flag to rpcapd to specify the data channel port; from diff --git a/gencode.c b/gencode.c index b2e092f4..050e7931 100644 --- a/gencode.c +++ b/gencode.c @@ -8763,9 +8763,7 @@ gen_inbound_outbound(compiler_state_t *cstate, const int outbound) */ switch (cstate->linktype) { case DLT_SLIP: - b0 = gen_relation_internal(cstate, BPF_JEQ, - gen_load_internal(cstate, Q_LINK, gen_loadi_internal(cstate, 0), 1), - gen_loadi_internal(cstate, 0), + b0 = gen_cmp(cstate, OR_LINKHDR, 0, BPF_B, outbound ? SLIPDIR_OUT : SLIPDIR_IN); break; diff --git a/testprogs/TESTrun b/testprogs/TESTrun index f3f3fe6f..cf0c072e 100755 --- a/testprogs/TESTrun +++ b/testprogs/TESTrun @@ -621,7 +621,7 @@ my %accept_blocks = ( DLT => 'SLIP', snaplen => 100, aliases => ['inbound'], - opt => ' + unopt => ' (000) ldb [0] (001) jeq #0x0 jt 2 jf 3 (002) ret #100 @@ -632,11 +632,11 @@ my %accept_blocks = ( DLT => 'SLIP', snaplen => 100, aliases => ['outbound'], - opt => ' + unopt => ' (000) ldb [0] - (001) jeq #0x0 jt 2 jf 3 - (002) ret #0 - (003) ret #100 + (001) jeq #0x1 jt 2 jf 3 + (002) ret #100 + (003) ret #0 ', }, # slip_outbound ipnet_inbound => { @@ -10045,7 +10045,7 @@ my %apply_blocks = ( slip_outbound_on_invalid => { savefile => 'slip-bad-direction.pcap', expr => 'outbound', - results => [262144], + results => [0], }, slip_inbound_on_rx => { savefile => 'slip-compressed_sl_print-oobr.pcap',