]> The Tcpdump Group git mirrors - libpcap/commitdiff
Have "outbound" mean Tx only for DLT_SLIP.
authorDenis Ovsienko <[email protected]>
Wed, 26 Feb 2025 13:49:49 +0000 (13:49 +0000)
committerDenis Ovsienko <[email protected]>
Thu, 27 Feb 2025 20:34:08 +0000 (20:34 +0000)
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.

CHANGES
gencode.c
testprogs/TESTrun

diff --git a/CHANGES b/CHANGES
index db338b35a430133d0b2775c301e7097c81c50630..c2dffc2ac7e2b7e0b7ba2acdadee19e20d5f9f38 100644 (file)
--- 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
index b2e092f4751c6ef53a1274e7ebe6def2148a595c..050e7931aa2e992dfbef7bfd39f27093de9399e6 100644 (file)
--- 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;
 
index f3f3fe6f9080ddcb52425449571ba66a14bb0574..cf0c072e3e82a55025176c184566eb39d0a8d96d 100755 (executable)
@@ -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',