<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Bpf · Network Doodles</title><link>https://networkdoodles.com/tags/bpf/</link><description>Network automation, from first playbook to agentic operations. Ansible, NetBox, MCP, Python and the occasional doodle.</description><language>en-us</language><managingEditor>David Henderson</managingEditor><lastBuildDate>Mon, 16 Nov 2026 09:00:00 -0500</lastBuildDate><atom:link href="https://networkdoodles.com/tags/bpf/index.xml" rel="self" type="application/rss+xml"/><item><title>Capture Filters vs Display Filters (Wireshark 101, Lesson 2)</title><link>https://networkdoodles.com/lessons/wireshark-101/capture-vs-display-filters/</link><pubDate>Mon, 16 Nov 2026 09:00:00 -0500</pubDate><dc:creator>David Henderson</dc:creator><guid>https://networkdoodles.com/lessons/wireshark-101/capture-vs-display-filters/</guid><category>wireshark</category><category>packet-capture</category><category>networking-basics</category><category>bpf</category><description>Capture filters (BPF) decide what's saved; display filters decide what's shown. Field names, operators, the != change in Wireshark 3.6, and precedence.</description><content:encoded xmlns:content="http://purl.org/rss/1.0/modules/content/"><![CDATA[<p>Wireshark has two kinds of filter, written in two different languages, and they work at two different
times. A <strong>capture filter</strong> runs while you capture and decides which frames are saved at all: whatever it
rejects is gone for good. A <strong>display filter</strong> runs on a capture you already have and only hides frames:
clear it and they&rsquo;re all back. So the rule is simple. Capture wide, with no filter or a loose one, and filter
narrow afterwards.</p>
<p><a href="/lessons/wireshark-101/what-a-capture-shows/">Lesson 1</a> was about what a capture is and where to take it.
This one is about finding the frames you care about in it. Every count below comes from tshark 4.6.5,
Wireshark&rsquo;s command-line twin, run against the lab&rsquo;s reference capture of r1&rsquo;s switch port: 64 frames of ARP,
DNS, pings, HTTP, HTTPS and the switch&rsquo;s own STP messages, from about four seconds of a quiet network.</p>
<hr>
<h2 id="what-is-a-capture-filter">What is a capture filter?</h2>
<p>A capture filter is written in <strong>BPF</strong>, the <em>Berkeley Packet Filter</em> language: the same one tcpdump uses.
It&rsquo;s old, small and fast, because of where it runs. The capture library compiles your filter into a tiny
program, and the operating system&rsquo;s kernel runs that program on every frame before it&rsquo;s handed to the
capture tool. A frame the program rejects is never copied out of the kernel, never written to disk, and can
never be recovered.</p>
<p>You can see the program. <code>tcpdump -d 'udp port 53'</code> prints it, and it starts like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">(000) ldh      [12]
</span></span><span class="line"><span class="cl">(001) jeq      #0x86dd          jt 2	jf 8
</span></span><span class="line"><span class="cl">...
</span></span><span class="line"><span class="cl">(018) ret      #262144
</span></span><span class="line"><span class="cl">(019) ret      #0
</span></span></code></pre></div><p>Load the two bytes at offset 12 of the frame (the Ethernet type), check whether they say IPv6, and so on.
<code>ret #262144</code> means <em>keep up to 262,144 bytes of this frame</em>, and <code>ret #0</code> means <em>drop it</em>. That&rsquo;s the whole
idea: a capture filter is arithmetic on header bytes at fixed positions.</p>
<p>Which is also its limit. BPF knows addresses, ports and protocol numbers, and that&rsquo;s about it:</p>
<table>
  <thead>
      <tr>
          <th>BPF capture filter</th>
          <th>Keeps</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>host 192.168.2.10</code></td>
          <td>frames to or from that address</td>
      </tr>
      <tr>
          <td><code>src host 192.168.1.20</code></td>
          <td>frames from it</td>
      </tr>
      <tr>
          <td><code>udp port 53</code></td>
          <td>UDP to or from port 53</td>
      </tr>
      <tr>
          <td><code>tcp port 443</code></td>
          <td>TCP to or from port 443</td>
      </tr>
      <tr>
          <td><code>ether host aa:aa:aa:11:11:11</code></td>
          <td>frames to or from that MAC address</td>
      </tr>
      <tr>
          <td><code>arp</code>, <code>not arp</code></td>
          <td>ARP, or everything else</td>
      </tr>
      <tr>
          <td><code>host 192.168.2.10 and tcp port 80</code></td>
          <td>both must be true (<code>and</code>, <code>or</code>, <code>not</code>)</td>
      </tr>
  </tbody>
</table>
<p>BPF can&rsquo;t say <em>DNS</em>. It can say <em>port 53</em> and trust that whatever uses port 53 is DNS. It can&rsquo;t say
<em>the HTTP request for /login</em>, because that&rsquo;s inside the payload, at no fixed offset. If the answer you need
is past the headers, a capture filter can&rsquo;t find it.</p>
<p>One BPF detail catches people. <code>host 192.168.1.1</code> matches the address wherever BPF knows to look, and that
includes ARP. On the reference capture it keeps 4 frames, all of them ARP: the laptop and the printer asking
for r1, and r1 answering. Write <code>ip host 192.168.1.1</code> and it keeps 0, because nothing in that capture sent an
IP packet to or from r1 itself. The pings went <em>through</em> r1, to a server further away.</p>
<hr>
<h2 id="what-is-a-display-filter">What is a display filter?</h2>
<p>A display filter is written in Wireshark&rsquo;s own language, and it works on the decoded capture. Every line in
the packet details pane is a <strong>field</strong> with a name, made by the <strong>dissector</strong>, the part of Wireshark that
decodes one protocol. Click a field and its name appears in the status bar: <code>ip.src</code>, <code>dns.qry.name</code>,
<code>tcp.flags.syn</code>, <code>tls.handshake.extensions_server_name</code>. tshark 4.6.5 lists 269,301 of them, from
every protocol it can decode.</p>
<p>So a display filter can ask anything a dissector understood:</p>
<table>
  <thead>
      <tr>
          <th>Display filter</th>
          <th>On the reference capture</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>dns</code></td>
          <td>12 frames: every frame with a DNS layer</td>
      </tr>
      <tr>
          <td><code>ip.src == 192.168.1.20</code></td>
          <td>3: the printer&rsquo;s own packets</td>
      </tr>
      <tr>
          <td><code>ip.addr == 192.168.1.20</code></td>
          <td>6: the printer as source <strong>or</strong> destination</td>
      </tr>
      <tr>
          <td><code>ip.addr == 192.168.2.0/24</code></td>
          <td>57: anything to or from the server network</td>
      </tr>
      <tr>
          <td><code>tcp.port in {80, 443}</code></td>
          <td>33: both web connections</td>
      </tr>
      <tr>
          <td><code>http.host == &quot;web.lab&quot;</code></td>
          <td>1: the HTTP request that asked for that name</td>
      </tr>
      <tr>
          <td><code>tcp.flags.syn == 1 &amp;&amp; tcp.flags.ack == 0</code></td>
          <td>2: the first packet of each TCP connection</td>
      </tr>
      <tr>
          <td><code>tls.handshake.extensions_server_name == &quot;web.lab&quot;</code></td>
          <td>1: the HTTPS request&rsquo;s name, before encryption</td>
      </tr>
  </tbody>
</table>
<p>That last one is a field inside an encrypted connection&rsquo;s handshake, at a position that changes from packet
to packet. No capture filter could reach it. A display filter just names it.</p>
<p>A filter with a typo is an error, not a silent empty result, which is good:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">tshark: &#34;ip.adr&#34; is not a valid protocol or protocol field.
</span></span><span class="line"><span class="cl">    ip.adr == 1.2.3.4
</span></span><span class="line"><span class="cl">    ^~~~~~
</span></span></code></pre></div><p>In Wireshark the filter bar turns red instead, and green when the filter makes sense.</p>
<table>
  <thead>
      <tr>
          <th></th>
          <th>Capture filter</th>
          <th>Display filter</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Language</td>
          <td>BPF (tcpdump&rsquo;s)</td>
          <td>Wireshark&rsquo;s display filter language</td>
      </tr>
      <tr>
          <td>Runs</td>
          <td>in the kernel, while capturing</td>
          <td>in Wireshark, on a saved capture</td>
      </tr>
      <tr>
          <td>Can see</td>
          <td>header fields at fixed offsets</td>
          <td>every field any dissector decoded</td>
      </tr>
      <tr>
          <td>What it does to rejected frames</td>
          <td>never saved</td>
          <td>hidden, still in the file</td>
      </tr>
      <tr>
          <td>Undo</td>
          <td>no</td>
          <td>clear the filter</td>
      </tr>
      <tr>
          <td>Cost</td>
          <td>very cheap, so it keeps up with busy links</td>
          <td>needs the whole capture decoded first</td>
      </tr>
  </tbody>
</table>
<hr>
<h2 id="how-do-contains-and-matches-work">How do contains and matches work?</h2>
<p>Two operators search for text rather than compare values.</p>
<p><code>contains</code> looks for an exact sequence of bytes or characters in a field, or in the whole frame.
<code>frame contains &quot;web&quot;</code> keeps 13 frames on the reference capture: the DNS questions and answers for <code>web.lab</code>,
the HTTP request (its Host header), the page that came back, and the TLS Client Hello, whose server name is
sent in clear text. It&rsquo;s case-sensitive: <code>dns.qry.name contains &quot;PRINT&quot;</code> finds nothing, because the name is
<code>printer.lab</code>.</p>
<p><code>matches</code> takes a regular expression, and it&rsquo;s case-insensitive by default.
<code>dns.qry.name matches &quot;^PRINT&quot;</code> finds both printer frames. Put <code>(?-i)</code> at the front of the expression and it
becomes case-sensitive again, and finds none.</p>
<p>Use <code>contains</code> when you know the exact text, and <code>matches</code> when you need a pattern or don&rsquo;t trust the case.</p>
<hr>
<h2 id="why-is-ipaddr-a-two-way-field">Why is ip.addr a two-way field?</h2>
<p>A frame has one IP source and one IP destination, but Wireshark also gives it <code>ip.addr</code>, a field that appears
<strong>twice</strong>: once with the source and once with the destination. <code>ip.addr == 192.168.1.20</code> is true if <em>either</em>
copy matches, which is exactly what you want for &ldquo;everything to or from the printer&rdquo;. <code>eth.addr</code>, <code>tcp.port</code>
and <code>udp.port</code> work the same way.</p>
<p>It&rsquo;s the most useful shortcut in Wireshark, and it&rsquo;s the reason for the most famous trap in it.</p>
<hr>
<h2 id="what-does-ipaddr--x-really-match">What does ip.addr != x really match?</h2>
<p>The question is what <em>not equal</em> means for a field with two values. Is <code>ip.addr != 192.168.2.10</code> true when
<em>any</em> copy is different, or only when <em>all</em> of them are?</p>
<p>For years, Wireshark used <em>any</em>. Every packet between the laptop and the web server has one address that isn&rsquo;t
the web server&rsquo;s, so <code>ip.addr != 192.168.2.10</code> matched nearly everything, including all the traffic you were
trying to remove. Every guide warned you never to write it.</p>
<p><strong>Wireshark 3.6 changed that.</strong> Its <a href="https://www.wireshark.org/docs/relnotes/wireshark-3.6.0.html">release notes</a>
say &ldquo;a != b&rdquo; now has the &ldquo;same meaning as !(a == b)&rdquo;, and kept the old meaning available as <code>~=</code>. Wireshark
4.0 <a href="https://www.wireshark.org/docs/relnotes/wireshark-4.0.0.html">deprecated <code>~=</code></a> in favor of <code>!==</code>. In
tshark 4.6.5, <code>~=</code> is a syntax error.</p>
<p>So the old warning is out of date. But there&rsquo;s one difference left, and it&rsquo;s the one that bites now. Here are
the three versions on the reference capture:</p>
<table>
  <thead>
      <tr>
          <th>Filter</th>
          <th>Frames</th>
          <th>What it means</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>ip.addr != 192.168.2.10</code></td>
          <td>20</td>
          <td>frames that <strong>have</strong> an IP address, none of them web&rsquo;s</td>
      </tr>
      <tr>
          <td><code>!(ip.addr == 192.168.2.10)</code></td>
          <td>27</td>
          <td>every frame that doesn&rsquo;t mention web, IP or not</td>
      </tr>
      <tr>
          <td><code>ip.addr !== 192.168.2.10</code></td>
          <td>57</td>
          <td>the old meaning: any address that isn&rsquo;t web&rsquo;s, so every IP frame</td>
      </tr>
  </tbody>
</table>
<p>The difference between 20 and 27 is 4 ARP frames and 3 STP frames. They have no <code>ip.addr</code> field at all, and a
comparison on a field that isn&rsquo;t there is false, so <code>!=</code> drops them. <code>!(ip.addr == ...)</code> asks the positive
question first (<em>is web in it?</em>: no, for ARP), then turns the answer round, so they stay.</p>
<p>That&rsquo;s usually what you want when you say &ldquo;everything except web&rdquo;. The habit to build: write the filter for
the thing you want to hide, then put <code>!( )</code> round it.</p>
<hr>
<h2 id="does--bind-tighter-than-">Does &amp;&amp; bind tighter than ||?</h2>
<p>Yes, since <strong>Wireshark 4.0</strong>, whose release notes say logical AND &ldquo;now has higher precedence than logical OR,
in line with most programming languages&rdquo;. Before 4.0 it didn&rsquo;t, so a filter that mixes the two can give a
different answer on an older version.</p>
<p>It changes results. Take <em>DNS or ARP, sent by the laptop</em>:</p>
<table>
  <thead>
      <tr>
          <th>Filter</th>
          <th>Frames</th>
          <th>Read as</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td><code>dns || arp &amp;&amp; eth.src == aa:aa:aa:11:11:11</code></td>
          <td>13</td>
          <td>DNS from anyone, or ARP from the laptop</td>
      </tr>
      <tr>
          <td><code>(dns || arp) &amp;&amp; eth.src == aa:aa:aa:11:11:11</code></td>
          <td>6</td>
          <td>what you meant</td>
      </tr>
  </tbody>
</table>
<p>Use brackets whenever you mix them. They make a filter mean the same thing in every version, and to every
person who reads it.</p>
<hr>
<h2 id="how-much-should-you-capture">How much should you capture?</h2>
<p>Capture wide, filter narrow. You can always hide a frame you saved. You can never get back a frame a capture
filter threw away, and the clue you didn&rsquo;t expect is usually in the traffic you&rsquo;d have thought irrelevant: an
ARP storm, a second DHCP server, a retransmission from a host you weren&rsquo;t watching.</p>
<p>Capture filters still have their place, for three reasons:</p>
<ul>
<li><strong>Volume.</strong> A busy link fills a disk quickly. A filter keeps the file to what you need.</li>
<li><strong>Load.</strong> Capturing every frame on a fast link can drop frames on a slow capture machine. Filtering in the
kernel is the cheapest way to keep up.</li>
<li><strong>Privacy and policy.</strong> Sometimes you&rsquo;re only allowed to keep one application&rsquo;s traffic. The safest copy of
someone else&rsquo;s data is the one you never saved.</li>
</ul>
<p>Two more settings decide how much a capture keeps.</p>
<h3 id="snap-length">Snap length</h3>
<p>The <strong>snap length</strong> is how many bytes of each frame are saved. The lab&rsquo;s files save up to 262,144 bytes per
frame, the same number as that <code>ret #262144</code> in the BPF program: in practice, all of every frame. Set it
small and you keep the headers and drop the payload. Here&rsquo;s the HTTP request from the reference capture,
cut to 54 bytes per frame with <code>editcap -s 54</code>:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Frame 24: Packet, 137 bytes on wire (1096 bits), 54 bytes captured (432 bits) on interface sw1:eth1, id 0
</span></span><span class="line"><span class="cl">   24 2.382133001 192.168.1.175 → 192.168.2.10 TCP 137 50124 → 80 [PSH, ACK] Seq=1 Ack=1 Win=502 Len=71[Packet size limited during capture]
</span></span></code></pre></div><p>The addresses and ports survived, the <code>GET</code> is gone, and Wireshark says so. A small snap length is a good
way to capture <em>who talked to whom</em> for a long time without keeping <em>what they said</em>. It&rsquo;s a bad idea when
you&rsquo;re troubleshooting the application.</p>
<h3 id="ring-buffers">Ring buffers</h3>
<p>A <strong>ring buffer</strong> is a set of capture files used in turn: write file 1 until it reaches a size or an age,
then file 2, and when the last one is full, overwrite the oldest. tshark&rsquo;s <code>-b</code> option does it (<code>filesize</code>,
<code>duration</code>, <code>files</code>). It&rsquo;s how you wait for an intermittent problem: leave a capture running for days with,
say, ten files of 100 MB, and when the problem happens, stop it. The last gigabyte of traffic before the
problem is on disk, and the disk never filled.</p>
<hr>
<h2 id="what-i-see-go-wrong">What I see go wrong</h2>
<p><strong>A capture filter that removed the answer.</strong> <code>host 192.168.2.10</code> looks like a sensible filter for &ldquo;web&rsquo;s
traffic&rdquo;, until the problem turns out to be the client&rsquo;s ARP or DNS, which never involved that address. If you
filter at capture time, filter on the client, not the server, and keep it loose.</p>
<p><strong><code>ip.addr != x</code> from an old guide.</strong> It&rsquo;s no longer the disaster those guides describe, but it still drops
every frame without an IP address. When you want &ldquo;everything except&rdquo;, use <code>!(field == value)</code>.</p>
<p><strong>Mixing <code>&amp;&amp;</code> and <code>||</code> without brackets.</strong> It works, in Wireshark 4.0 and later, by precedence. Someone on an
older version, or someone reading it quickly, gets a different answer.</p>
<p><strong>Treating BPF and display filters as one language.</strong> <code>port 53</code> is BPF; <code>udp.port == 53</code> is a display filter.
The filter bar rejects the first, and tcpdump rejects the second. Know which box you&rsquo;re typing in.</p>
<p><strong>Trusting an empty result.</strong> A display filter that shows nothing might mean the traffic isn&rsquo;t there, or
that you asked for a field from the wrong protocol. Before you conclude anything, take a frame you <em>know</em>
should match and check that the filter matches it.</p>
<hr>
<h2 id="try-it">Try it</h2>
<p><a href="/labs/wireshark-101/#chapter-2">Chapter 2 of the lab</a> gives you the same reference capture used here. You
write display filters that have to pick exactly the right frames, see the <code>!=</code> difference for yourself, and
then write capture filters in BPF and find out what they threw away.</p>
<h2 id="whats-next">What&rsquo;s next</h2>
<p><a href="/lessons/wireshark-101/arp/">Lesson 3</a> uses those filters on the first protocol every conversation needs:
ARP, the question a host asks before it can send anything to its neighbor.</p>
]]></content:encoded></item></channel></rss>