<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/"><channel><title>OpenBSD | A DevOps Blog</title><link>https://blog.bsdserver.nl/tag/openbsd/</link><description>Hands-on DevOps and homelab content: Kubernetes clusters, self-hosted services, CI/CD pipelines and infrastructure as code, by Patrick de Ruiter of We Build Your Cloud.</description><language>en</language><image><url>https://blog.bsdserver.nl/images/app-icon.svg</url><title>A DevOps Blog</title><link>https://blog.bsdserver.nl/</link></image><lastBuildDate>Fri, 20 Nov 2026 01:22:00 +0100</lastBuildDate><atom:link href="https://blog.bsdserver.nl/tag/openbsd/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title>A Default-Deny NAT Firewall on OpenBSD pf, Managed with Terraform</title><link>https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/</link><guid isPermaLink="true">https://blog.bsdserver.nl/default-deny-nat-firewall-openbsd-pf-terraform/</guid><dc:creator>Patrick de Ruiter</dc:creator><pubDate>Fri, 20 Nov 2026 01:22:00 +0100</pubDate><description>Build a default-deny OpenBSD pf firewall with NAT, port forwarding, a blocklist and QoS, managed through pfapid and its Terraform provider.</description><category>OpenBSD</category><category>Security</category><category>Networking</category><category>Terraform</category><category>Automation</category><category>API</category><media:content url="https://blog.bsdserver.nl/content/images/2026/11/pf-terraform-nat-firewall.png" medium="image"/><content:encoded><![CDATA[<p>pf is the best firewall I have worked with: one readable file, atomic reloads, NAT and queueing in the same language as the filter rules. What it lacks is an API. Every tool that wants to change a pf firewall ends up templating <code>pf.conf</code>, copying it over SSH and hoping <code>pfctl -f</code> likes it.</p>
<p>I wrote <a href="https://github.com/pderuiter/pfapid" rel="noopener">pfapid</a> to fix that: a small REST API for pf that runs on the firewall itself, with a <a href="https://registry.terraform.io/providers/pderuiter/pf/latest" rel="noopener">Terraform provider</a> on top. This post builds a complete NAT firewall with it: a default-deny base, outbound NAT, a port forward to a DMZ web server, a blocklist and QoS on the uplink.</p>
<h2 id="what-pfapid-manages-and-what-it-doesnt">What pfapid manages, and what it doesn&rsquo;t <a class="anchor" data-pagefind-ignore href="#what-pfapid-manages-and-what-it-doesnt" aria-label="Link to this section">#</a></h2>
<p>pfapid splits the firewall into two owners:</p>
<table>
	<thead>
			<tr>
					<th>Part</th>
					<th>Owned by</th>
					<th>Lives in</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Defaults, <code>block all</code>, antispoof, management access</td>
					<td>You, by hand or config management</td>
					<td><code>/etc/pf.conf</code></td>
			</tr>
			<tr>
					<td>Filter and NAT rules, per unit of work</td>
					<td>Terraform via pfapid</td>
					<td>Anchors <code>api/&lt;name&gt;</code></td>
			</tr>
			<tr>
					<td>Contents of tables such as <code>&lt;blocklist&gt;</code></td>
					<td>Terraform via pfapid</td>
					<td>Tables declared in <code>pf.conf</code></td>
			</tr>
			<tr>
					<td>HFSC queues and the rules that classify into them</td>
					<td>Terraform via pfapid</td>
					<td>The <code>api-qos</code> anchor and a queue include</td>
			</tr>
	</tbody>
</table>
<p>The base ruleset is the part you can&rsquo;t afford to get wrong, so the API can&rsquo;t touch it. Whatever Terraform does, the <code>block</code> and management rules in <code>pf.conf</code> come first.</p>
<h3 id="why-not-just-template-pfconf">Why not just template pf.conf? <a class="anchor" data-pagefind-ignore href="#why-not-just-template-pfconf" aria-label="Link to this section">#</a></h3>
<ul>
<li><strong><code>terraform plan</code> asks pf.</strong> The provider sends the rules to the firewall, where pfapid runs <code>pfctl -n</code> on them. A rule pf would refuse fails the plan, not the apply, and the plan shows the exact lines pf will load.</li>
<li><strong>Commit-confirm.</strong> Every change is loaded, then rolled back by the firewall itself unless the provider confirms it within <code>confirm_timeout</code>. If an apply cuts off the path between Terraform and the firewall, the change undoes itself.</li>
<li><strong>Drift detection.</strong> Someone runs <code>pfctl</code> by hand at 3 a.m.? The next plan warns, and the next apply puts the rules back.</li>
<li><strong>Small blast radius.</strong> pfapid runs privilege-separated: the HTTPS side runs as <code>_pfapid</code> under <code>pledge(&quot;stdio inet&quot;)</code>, and only a tiny root process runs <code>pfctl</code>. Clients authenticate with mTLS and are scoped to anchors and tables. There is no raw pf.conf passthrough: rules are typed objects, rendered and checked before pf sees them.</li>
</ul>
<h3 id="when-to-use-it">When to use it <a class="anchor" data-pagefind-ignore href="#when-to-use-it" aria-label="Link to this section">#</a></h3>
<ul>
<li><strong>Use it</strong> when firewall rules belong to the same change as the thing they protect: a new service, a Kubernetes cluster, a customer network. They get reviewed and planned together.</li>
<li><strong>Skip it</strong> for a firewall nobody changes, or one with a single admin who is happy with <code>vi /etc/pf.conf</code>. A text file with good comments is hard to beat.</li>
</ul>
<h2 id="the-network">The network <a class="anchor" data-pagefind-ignore href="#the-network" aria-label="Link to this section">#</a></h2>
<pre tabindex="0"><code>                 internet
                     |
               wan  vio0   203.0.113.2/24, 2001:db8:1::2/64   (egress)
          +----------------------+
 mgmt     |                      |  lan  vio1   10.0.10.1/24, 2001:db8:10::1/64
 vio3 ----+   OpenBSD + pfapid   +-------------  office clients, VoIP phones
 192.168. |                      |
 100.1/24 |                      |  dmz  vio2   10.0.20.1/24, 2001:db8:20::1/64
          +----------------------+-------------  web server 10.0.20.10, 2001:db8:20::10
</code></pre><p>The rules use <strong>interface groups</strong> (<code>wan</code>, <code>lan</code>, <code>dmz</code>, <code>mgmt</code>), never device names. When you replace a NIC or move a network to a VLAN, you add the new interface to the group and no rule changes:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1"># /etc/hostname.vio0 and friends: one &#34;group&#34; line each</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s1">&#39;group wan&#39;</span> &gt;&gt; /etc/hostname.vio0
</span></span><span class="line"><span class="cl">ifconfig vio0 group wan
</span></span></code></pre></div><p>What we want:</p>
<table>
	<thead>
			<tr>
					<th>From</th>
					<th>To</th>
					<th>What</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>lan</td>
					<td>internet</td>
					<td>Everything, IPv4 behind NAT, IPv6 routed</td>
			</tr>
			<tr>
					<td>dmz</td>
					<td>internet</td>
					<td>DNS, NTP and HTTPS only (updates)</td>
			</tr>
			<tr>
					<td>internet, lan</td>
					<td>web server</td>
					<td>TCP 80 and 443; IPv4 through a port forward on the wan address</td>
			</tr>
			<tr>
					<td><code>&lt;blocklist&gt;</code></td>
					<td>anything</td>
					<td>Nothing</td>
			</tr>
			<tr>
					<td>mgmt network</td>
					<td>firewall</td>
					<td>SSH and the pfapid API</td>
			</tr>
			<tr>
					<td>Everything else</td>
					<td></td>
					<td>Dropped and logged</td>
			</tr>
	</tbody>
</table>
<h2 id="step-1-install-pfapid">Step 1: Install pfapid <a class="anchor" data-pagefind-ignore href="#step-1-install-pfapid" aria-label="Link to this section">#</a></h2>
<p>pfapid ships as signed release archives for OpenBSD amd64 and arm64. Check the signature before you install it:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="c1"># on a workstation with gpg</span>
</span></span><span class="line"><span class="cl"><span class="nv">v</span><span class="o">=</span><span class="k">$(</span>curl -s https://api.github.com/repos/pderuiter/pfapid/releases/latest <span class="p">|</span>
</span></span><span class="line"><span class="cl">	sed -n <span class="s1">&#39;s/.*&#34;tag_name&#34;: *&#34;v\([^&#34;]*\)&#34;.*/\1/p&#39;</span><span class="k">)</span>
</span></span><span class="line"><span class="cl"><span class="nv">base</span><span class="o">=</span>https://github.com/pderuiter/pfapid/releases/download/v<span class="nv">$v</span>
</span></span><span class="line"><span class="cl">curl -sSLO <span class="nv">$base</span>/pfapid_<span class="si">${</span><span class="nv">v</span><span class="si">}</span>_openbsd_amd64.tar.gz
</span></span><span class="line"><span class="cl">curl -sSLO <span class="nv">$base</span>/pfapid_<span class="si">${</span><span class="nv">v</span><span class="si">}</span>_SHA256SUMS -O <span class="nv">$base</span>/pfapid_<span class="si">${</span><span class="nv">v</span><span class="si">}</span>_SHA256SUMS.sig
</span></span><span class="line"><span class="cl">curl -sSL https://raw.githubusercontent.com/pderuiter/pfapid/main/scripts/release-signing-key.asc <span class="p">|</span> gpg --import
</span></span><span class="line"><span class="cl">gpg --verify pfapid_<span class="si">${</span><span class="nv">v</span><span class="si">}</span>_SHA256SUMS.sig pfapid_<span class="si">${</span><span class="nv">v</span><span class="si">}</span>_SHA256SUMS
</span></span><span class="line"><span class="cl">shasum -a <span class="m">256</span> --ignore-missing -c pfapid_<span class="si">${</span><span class="nv">v</span><span class="si">}</span>_SHA256SUMS
</span></span></code></pre></div><p><code>gpg</code> must report a good signature from key <code>10A2F1058821D55C61E45C7EE97C02FEE2F51B23</code>. Then on the firewall, as root:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">tar xzf pfapid_*_openbsd_amd64.tar.gz
</span></span><span class="line"><span class="cl">useradd -s /sbin/nologin -d /var/empty -L daemon -c <span class="s2">&#34;pfapid network process&#34;</span> _pfapid
</span></span><span class="line"><span class="cl">install -o root -g wheel -m <span class="m">0555</span> pfapid /usr/local/sbin/pfapid
</span></span><span class="line"><span class="cl">install -o root -g wheel -m <span class="m">0555</span> deploy/rc.d/pfapid /etc/rc.d/pfapid
</span></span><span class="line"><span class="cl">install -d -o root -g wheel -m <span class="m">0700</span> /etc/pfapid /var/db/pfapid
</span></span><span class="line"><span class="cl">install -d -o root -g wheel -m <span class="m">0755</span> /etc/pf.anchors/api
</span></span><span class="line"><span class="cl"><span class="k">for</span> f in api.conf api-queues.conf api-qos.conf<span class="p">;</span> <span class="k">do</span>
</span></span><span class="line"><span class="cl">	<span class="o">[</span> -f /etc/pf.anchors/<span class="nv">$f</span> <span class="o">]</span> <span class="o">||</span> install -o root -g wheel -m <span class="m">0600</span> /dev/null /etc/pf.anchors/<span class="nv">$f</span>
</span></span><span class="line"><span class="cl"><span class="k">done</span>
</span></span></code></pre></div><p>The three empty files must exist before <code>pf.conf</code> includes them; pfapid fills them later.</p>
<p>pfapid has a built-in CA, so you don&rsquo;t need a PKI to get started. Create the CA and the server certificate, then a client certificate for Terraform:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">pfapid pki init -dns fw.example.net -ip 192.168.100.1
</span></span><span class="line"><span class="cl">pfapid pki issue -name tf-firewall -out /root/tf-firewall   <span class="c1"># writes tf-firewall.crt and .key</span>
</span></span></code></pre></div><p>Copy <code>/root/tf-firewall.crt</code>, <code>/root/tf-firewall.key</code> and the CA certificate (<code>/etc/pfapid/pki/ca.crt</code>) to the machine that runs Terraform, and delete the key from the firewall. A company CA, Vault or SPIFFE IDs work too; see <a href="https://github.com/pderuiter/pfapid/blob/main/docs/CERTIFICATES.md" rel="noopener">CERTIFICATES.md</a>.</p>
<h2 id="step-2-the-default-deny-base-ruleset">Step 2: The default-deny base ruleset <a class="anchor" data-pagefind-ignore href="#step-2-the-default-deny-base-ruleset" aria-label="Link to this section">#</a></h2>
<p>This file is yours. It holds everything that must hold no matter what the API does:</p>
<pre tabindex="0"><code># /etc/pf.conf: default-deny base ruleset for a pfapid-managed NAT firewall.
#
#   wan   vio0 (egress)  203.0.113.2/24, 2001:db8:1::2/64
#   lan   vio1           10.0.10.1/24,   2001:db8:10::1/64
#   dmz   vio2           10.0.20.1/24,   2001:db8:20::1/64
#   mgmt  vio3           192.168.100.1/24
admin_nets = &#34;192.168.100.0/24&#34;

set skip on lo
set block-policy drop
set loginterface egress

# Tables whose contents pfapid manages: declared empty, with persist,
# so a reload of this file keeps what Terraform put in them.
table &lt;dmz_web&gt; persist
table &lt;blocklist&gt; persist

# Default deny: nothing passes, in or out, unless a rule says so.
block log all

# Packets on lan or dmz must come from that network.
antispoof quick for { lan dmz }

# Management first, and quick: nothing in the API anchors can take it away.
pass in quick on mgmt proto tcp from $admin_nets to (mgmt) port { 22 8443 }

# IPv6 breaks without these: neighbour discovery and path MTU discovery.
pass quick inet6 proto icmp6 icmp6-type { unreach toobig timex paramprob \
	routersol routeradv neighbrsol neighbradv }

# The firewall offers no services to the inside networks.
block in quick on { lan dmz } to self

# The firewall&#39;s own traffic (DNS, NTP, syspatch).
pass out quick on egress from (egress)

# pfapid: QoS classification, then the API anchors.
include &#34;/etc/pf.anchors/api-queues.conf&#34;
anchor &#34;api-qos&#34;
load anchor &#34;api-qos&#34; from &#34;/etc/pf.anchors/api-qos.conf&#34;
anchor &#34;api/*&#34;
include &#34;/etc/pf.anchors/api.conf&#34;
</code></pre><p>Block by block:</p>
<ul>
<li><strong><code>block log all</code></strong> is the default-deny. pf uses the <em>last</em> matching rule, so every <code>pass</code> further down (and in the anchors) can open a hole, but nothing is open by default. It applies in both directions, which matters for forwarded traffic later.</li>
<li><strong><code>antispoof</code></strong> drops packets on lan or dmz with a source address that doesn&rsquo;t belong there.</li>
<li><strong>Management</strong> comes before the anchors and is <code>quick</code>: pf stops evaluating on the first <code>quick</code> match, so no anchor can override it. pfapid also refuses the <code>mgmt</code> group in any API rule.</li>
<li><strong>ICMPv6</strong> for neighbour discovery and path MTU. Without it, IPv6 breaks in slow, confusing ways. pfapid won&rsquo;t let the API block these either.</li>
<li><strong><code>block in quick on { lan dmz } to self</code></strong> keeps the inside networks away from the firewall&rsquo;s own addresses, including the API. If the firewall runs DNS or DHCP for the LAN, add a <code>pass in quick</code> for exactly that above this line.</li>
<li><strong>The pfapid hooks</strong>: the queue include and the <code>api-qos</code> anchor come before <code>anchor &quot;api/*&quot;</code>, so traffic is classified before an API rule passes it. <code>api.conf</code> is generated by pfapid with one <code>load anchor</code> line per anchor, so the anchors survive a reboot.</li>
</ul>
<p>Check the file, then load it with a way back. I locked myself out of my lab firewall while writing this post: my workstation wasn&rsquo;t in <code>admin_nets</code>. A background job that restores the old file after 90 seconds unless I cancel it brought the firewall back on its own:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">pfctl -nf /etc/pf.conf.new            <span class="c1"># syntax check, loads nothing</span>
</span></span><span class="line"><span class="cl">cp -p /etc/pf.conf /etc/pf.conf.good
</span></span><span class="line"><span class="cl">install -m <span class="m">0600</span> /etc/pf.conf.new /etc/pf.conf
</span></span><span class="line"><span class="cl">rm -f /tmp/pf-keep
</span></span><span class="line"><span class="cl"><span class="o">(</span> sleep 90<span class="p">;</span> <span class="o">[</span> -f /tmp/pf-keep <span class="o">]</span> <span class="o">||</span> <span class="o">{</span> cp -p /etc/pf.conf.good /etc/pf.conf<span class="p">;</span> pfctl -f /etc/pf.conf<span class="p">;</span> <span class="o">}</span> <span class="o">)</span> <span class="p">&amp;</span>
</span></span><span class="line"><span class="cl">pfctl -f /etc/pf.conf
</span></span><span class="line"><span class="cl"><span class="c1"># from a NEW ssh session: it works, keep it</span>
</span></span><span class="line"><span class="cl">touch /tmp/pf-keep
</span></span></code></pre></div><p>The test has to be a <em>new</em> connection: existing SSH sessions keep their state and survive a rule change that would lock out the next login.</p>
<h2 id="step-3-tell-pfapid-what-it-may-touch">Step 3: Tell pfapid what it may touch <a class="anchor" data-pagefind-ignore href="#step-3-tell-pfapid-what-it-may-touch" aria-label="Link to this section">#</a></h2>
<p><code>/etc/pfapid.json</code> (mode 0600, root) lists the interface groups, tables and queue devices the API may use, and which client may do what:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-json" data-lang="json"><span class="line"><span class="cl"><span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;listen&#34;</span><span class="p">:</span> <span class="s2">&#34;192.168.100.1:8443&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;tls&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;cert&#34;</span><span class="p">:</span> <span class="s2">&#34;/etc/pfapid/pki/server.crt&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;key&#34;</span><span class="p">:</span> <span class="s2">&#34;/etc/pfapid/pki/server.key&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;client_cas&#34;</span><span class="p">:</span> <span class="p">{</span> <span class="nt">&#34;pfapid&#34;</span><span class="p">:</span> <span class="s2">&#34;/etc/pfapid/pki/ca.crt&#34;</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">  <span class="p">},</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;confirm_timeout&#34;</span><span class="p">:</span> <span class="s2">&#34;60s&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;policy&#34;</span><span class="p">:</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;interfaces&#34;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&#34;wan&#34;</span><span class="p">,</span> <span class="s2">&#34;lan&#34;</span><span class="p">,</span> <span class="s2">&#34;dmz&#34;</span><span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;tables&#34;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&#34;dmz_web&#34;</span><span class="p">,</span> <span class="s2">&#34;blocklist&#34;</span><span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;queue_interfaces&#34;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&#34;vio0&#34;</span><span class="p">],</span>
</span></span><span class="line"><span class="cl">    <span class="nt">&#34;internal_nets&#34;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&#34;10.0.0.0/8&#34;</span><span class="p">,</span> <span class="s2">&#34;172.16.0.0/12&#34;</span><span class="p">,</span> <span class="s2">&#34;192.168.0.0/16&#34;</span><span class="p">,</span> <span class="s2">&#34;fd00::/8&#34;</span><span class="p">]</span>
</span></span><span class="line"><span class="cl">  <span class="p">},</span>
</span></span><span class="line"><span class="cl">  <span class="nt">&#34;clients&#34;</span><span class="p">:</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="p">{</span> <span class="nt">&#34;name&#34;</span><span class="p">:</span> <span class="s2">&#34;tf-firewall&#34;</span><span class="p">,</span> <span class="nt">&#34;ca&#34;</span><span class="p">:</span> <span class="s2">&#34;pfapid&#34;</span><span class="p">,</span> <span class="nt">&#34;anchors&#34;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&#34;*&#34;</span><span class="p">],</span> <span class="nt">&#34;tables&#34;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&#34;dmz_web&#34;</span><span class="p">,</span> <span class="s2">&#34;blocklist&#34;</span><span class="p">],</span> <span class="nt">&#34;qos&#34;</span><span class="p">:</span> <span class="kc">true</span> <span class="p">}</span>
</span></span><span class="line"><span class="cl">  <span class="p">]</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><ul>
<li><strong><code>interfaces</code></strong> are the groups rules may name. <code>mgmt</code> is refused here and in every rule.</li>
<li><strong><code>tables</code></strong> must also be declared in <code>pf.conf</code>, empty and with <code>persist</code>.</li>
<li><strong><code>queue_interfaces</code></strong> are physical devices: pf can&rsquo;t hang a queue on a group.</li>
<li><strong><code>internal_nets</code></strong>: pfapid refuses NAT <em>between</em> two of these networks, a common mistake that hides where traffic really comes from. NAT from the LAN to the internet is fine.</li>
<li><strong><code>clients</code></strong>: <code>tf-firewall</code> (the certificate&rsquo;s CN) may manage every anchor, these two tables and QoS. A team that only owns its own service would get <code>&quot;anchors&quot;: [&quot;webshop-*&quot;]</code> and nothing else.</li>
</ul>
<p>Start it:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">pfapid -f /etc/pfapid.json -n       <span class="c1"># checks the configuration and certificates</span>
</span></span><span class="line"><span class="cl">rcctl <span class="nb">enable</span> pfapid
</span></span><span class="line"><span class="cl">rcctl start pfapid
</span></span></code></pre></div><h2 id="step-4-the-terraform-provider">Step 4: The Terraform provider <a class="anchor" data-pagefind-ignore href="#step-4-the-terraform-provider" aria-label="Link to this section">#</a></h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-hcl" data-lang="hcl"><span class="line"><span class="cl"><span class="k">terraform</span> {
</span></span><span class="line"><span class="cl">  <span class="k">required_providers</span> {
</span></span><span class="line"><span class="cl"><span class="n">    pf</span> <span class="o">=</span> {
</span></span><span class="line"><span class="cl"><span class="n">      source</span>  <span class="o">=</span> <span class="s2">&#34;pderuiter/pf&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      version</span> <span class="o">=</span> <span class="s2">&#34;~&gt; 0.1&#34;</span>
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">  }
</span></span><span class="line"><span class="cl">}<span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1"># Endpoint and certificates come from the environment:
</span></span></span><span class="line"><span class="cl"><span class="c1">#   PFAPID_ENDPOINT, PFAPID_CA_CERT, PFAPID_CLIENT_CERT, PFAPID_CLIENT_KEY
</span></span></span><span class="line"><span class="cl"><span class="k">provider</span> <span class="s2">&#34;pf&#34;</span> {}
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">export</span> <span class="nv">PFAPID_ENDPOINT</span><span class="o">=</span>https://192.168.100.1:8443
</span></span><span class="line"><span class="cl"><span class="nb">export</span> <span class="nv">PFAPID_CA_CERT</span><span class="o">=</span><span class="nv">$HOME</span>/.pfapid/ca.crt
</span></span><span class="line"><span class="cl"><span class="nb">export</span> <span class="nv">PFAPID_CLIENT_CERT</span><span class="o">=</span><span class="nv">$HOME</span>/.pfapid/tf-firewall.crt
</span></span><span class="line"><span class="cl"><span class="nb">export</span> <span class="nv">PFAPID_CLIENT_KEY</span><span class="o">=</span><span class="nv">$HOME</span>/.pfapid/tf-firewall.key
</span></span><span class="line"><span class="cl">terraform init
</span></span></code></pre></div><p>The provider is signed with the same key as the pfapid releases; <code>terraform init</code> checks that for you.</p>
<h2 id="step-5-tables">Step 5: Tables <a class="anchor" data-pagefind-ignore href="#step-5-tables" aria-label="Link to this section">#</a></h2>
<p>Tables are lists of addresses that rules refer to as <code>&lt;name&gt;</code>. Changing a table never reloads a rule, so it&rsquo;s the cheap way to add a web server or block a network. A table can hold IPv4 and IPv6 entries side by side:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-hcl" data-lang="hcl"><span class="line"><span class="cl"><span class="c1"># The web servers in the DMZ. pf rules reference &lt;dmz_web&gt;; changing the
</span></span></span><span class="line"><span class="cl"><span class="c1"># entries never reloads a rule.
</span></span></span><span class="line"><span class="cl"><span class="k">resource</span> <span class="s2">&#34;pf_table&#34; &#34;dmz_web&#34;</span> {
</span></span><span class="line"><span class="cl"><span class="n">  name</span>    <span class="o">=</span> <span class="s2">&#34;dmz_web&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">  entries</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&#34;10.0.20.10/32&#34;, &#34;2001:db8:20::10/128&#34;</span><span class="p">]</span>
</span></span><span class="line"><span class="cl">}<span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1"># Sources we never want to hear from.
</span></span></span><span class="line"><span class="cl"><span class="k">resource</span> <span class="s2">&#34;pf_table&#34; &#34;blocklist&#34;</span> {
</span></span><span class="line"><span class="cl"><span class="n">  name</span>    <span class="o">=</span> <span class="s2">&#34;blocklist&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">  entries</span> <span class="o">=</span> <span class="p">[</span><span class="s2">&#34;198.51.100.0/24&#34;, &#34;2001:db8:bad::/48&#34;</span><span class="p">]</span>
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></div><h2 id="step-6-filtering-and-outbound-nat">Step 6: Filtering and outbound NAT <a class="anchor" data-pagefind-ignore href="#step-6-filtering-and-outbound-nat" aria-label="Link to this section">#</a></h2>
<p>Each <code>pf_anchor</code> is one anchor, <code>api/&lt;name&gt;</code>, and its <code>rules</code> are loaded as a whole: pf is order-sensitive, so a ruleset is one atomic unit, never rule by rule. Split anchors along ownership: whoever owns the DMZ web server owns <code>dmz-web</code>.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-hcl" data-lang="hcl"><span class="line"><span class="cl"><span class="c1"># Anchor api/egress: the inside networks out to the internet.
</span></span></span><span class="line"><span class="cl"><span class="k">locals</span> {
</span></span><span class="line"><span class="cl"><span class="n">  lan4</span> <span class="o">=</span> <span class="s2">&#34;10.0.10.0/24&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">  lan6</span> <span class="o">=</span> <span class="s2">&#34;2001:db8:10::/64&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">  dmz4</span> <span class="o">=</span> <span class="s2">&#34;10.0.20.0/24&#34;</span>
</span></span><span class="line"><span class="cl">}
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">resource</span> <span class="s2">&#34;pf_anchor&#34; &#34;egress&#34;</span> {
</span></span><span class="line"><span class="cl"><span class="n">  name</span> <span class="o">=</span> <span class="s2">&#34;egress&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">  rules</span> <span class="o">=</span> <span class="p">[</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # LAN clients may start anything; the out rule below decides where.
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;lan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      from</span>      <span class="o">=</span><span class="n"> { prefixes</span> <span class="o">=</span> <span class="p">[</span><span class="k">local</span><span class="p">.</span><span class="k">lan4</span><span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;lan-in&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span>
</span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;lan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet6&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      from</span>      <span class="o">=</span><span class="n"> { prefixes</span> <span class="o">=</span> <span class="p">[</span><span class="k">local</span><span class="p">.</span><span class="k">lan6</span><span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;lan-in&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # DMZ servers only get DNS, NTP and HTTPS (updates) out.
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;dmz&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;udp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      from</span>      <span class="o">=</span><span class="n"> { prefixes</span> <span class="o">=</span> <span class="p">[</span><span class="k">local</span><span class="p">.</span><span class="k">dmz4</span><span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { ports</span> <span class="o">=</span><span class="n"> [{ from</span> <span class="o">=</span><span class="n"> 53 }, { from</span> <span class="o">=</span> <span class="m">123</span> }<span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;dmz-out&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span>
</span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;dmz&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;tcp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      from</span>      <span class="o">=</span><span class="n"> { prefixes</span> <span class="o">=</span> <span class="p">[</span><span class="k">local</span><span class="p">.</span><span class="k">dmz4</span><span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { ports</span> <span class="o">=</span><span class="n"> [{ from</span> <span class="o">=</span> <span class="m">443</span> }<span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;dmz-out&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # Out to the internet: IPv4 is translated to the wan address...
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;out&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;wan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      from</span>      <span class="o">=</span><span class="n"> { prefixes</span> <span class="o">=</span> <span class="p">[</span><span class="k">local</span><span class="p">.</span><span class="k">lan4</span><span class="p">,</span> <span class="k">local</span><span class="p">.</span><span class="k">dmz4</span><span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      nat</span>       <span class="o">=</span><span class="n"> { kind</span> <span class="o">=</span><span class="n"> &#34;nat-to&#34;, interface</span> <span class="o">=</span> <span class="s2">&#34;wan&#34;</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;nat-out&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # ...IPv6 is routed as is.
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;out&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;wan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet6&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      from</span>      <span class="o">=</span><span class="n"> { prefixes</span> <span class="o">=</span> <span class="p">[</span><span class="k">local</span><span class="p">.</span><span class="k">lan6</span><span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;v6-out&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">]</span>
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></div><p>Things worth knowing:</p>
<ul>
<li><strong>Every rule names one family.</strong> pfapid refuses a rule that mixes IPv4 and IPv6: <code>pfctl</code> would silently turn it into two rules, and what&rsquo;s loaded would no longer match what you wrote. Tables are the way to cover both.</li>
<li><strong>Forwarded traffic is filtered twice.</strong> A packet from the LAN to the internet goes <em>in</em> on lan and <em>out</em> on wan, and with <code>block all</code> both directions need a <code>pass</code>. The <code>lan-in</code> rule allows everything in, but the out rules decide where it can go: only out on wan. A LAN client trying to reach the DMZ gets in on lan and is then dropped going out on dmz, unless a rule there allows it (the next anchor does, for the web ports).</li>
<li><strong>NAT sits on the out rule.</strong> <code>nat = { kind = &quot;nat-to&quot;, interface = &quot;wan&quot; }</code> renders as <code>nat-to (wan:0)</code>: the first address of wan, re-read when it changes, so this works with DHCP on the uplink too.</li>
<li><strong>IPv6 isn&rsquo;t translated.</strong> The LAN has a routed prefix, so the IPv6 rule just passes it.</li>
</ul>
<h2 id="step-7-port-forwarding-to-the-dmz">Step 7: Port forwarding to the DMZ <a class="anchor" data-pagefind-ignore href="#step-7-port-forwarding-to-the-dmz" aria-label="Link to this section">#</a></h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-hcl" data-lang="hcl"><span class="line"><span class="cl"><span class="c1"># Anchor api/dmz-web: publish the web server.
</span></span></span><span class="line"><span class="cl"><span class="k">locals</span> {
</span></span><span class="line"><span class="cl"><span class="n">  web_ports</span> <span class="o">=</span><span class="n"> [{ from</span> <span class="o">=</span><span class="n"> 80 }, { from</span> <span class="o">=</span> <span class="m">443</span> }<span class="p">]</span>
</span></span><span class="line"><span class="cl">}
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">resource</span> <span class="s2">&#34;pf_anchor&#34; &#34;dmz_web&#34;</span> {
</span></span><span class="line"><span class="cl"><span class="n">  name</span> <span class="o">=</span> <span class="s2">&#34;dmz-web&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">  rules</span> <span class="o">=</span> <span class="p">[</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # IPv4: redirect the wan address to the server (port forward).
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;wan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;tcp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { interface</span> <span class="o">=</span><span class="n"> &#34;wan&#34;, ports</span> <span class="o">=</span> <span class="k">local</span><span class="p">.</span><span class="k">web_ports</span> }
</span></span><span class="line"><span class="cl"><span class="n">      nat</span>       <span class="o">=</span><span class="n"> { kind</span> <span class="o">=</span><span class="n"> &#34;rdr-to&#34;, address</span> <span class="o">=</span> <span class="s2">&#34;10.0.20.10/32&#34;</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;web-rdr&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # IPv6: the server has a public address, so just let it through.
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;wan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet6&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;tcp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { table</span> <span class="o">=</span><span class="n"> pf_table.dmz_web.name, ports</span> <span class="o">=</span> <span class="k">local</span><span class="p">.</span><span class="k">web_ports</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;web-in&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # Forwarded traffic is filtered twice: in on wan (or lan), and out on
</span></span></span><span class="line"><span class="cl"><span class="c1">    # dmz. Without these the redirect above goes nowhere.
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;out&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;dmz&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;tcp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { table</span> <span class="o">=</span><span class="n"> pf_table.dmz_web.name, ports</span> <span class="o">=</span> <span class="k">local</span><span class="p">.</span><span class="k">web_ports</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;web-out&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span>
</span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;pass&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;out&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;dmz&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet6&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;tcp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { table</span> <span class="o">=</span><span class="n"> pf_table.dmz_web.name, ports</span> <span class="o">=</span> <span class="k">local</span><span class="p">.</span><span class="k">web_ports</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;web-out&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">]</span>
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></div><p>The <code>rdr-to</code> rule changes the destination of connections to the wan address on port 80 or 443 to <code>10.0.20.10</code>. The same <code>pass</code> rule creates the state, so there is no separate redirect-then-filter step to keep in sync. The redirected packet then leaves on dmz, and the <code>web-out</code> rules let it through: forget those and the port forward silently goes nowhere. Those same rules let LAN clients reach the web server directly.</p>
<p>For IPv6 the server has its own address, so there is nothing to translate: pass it in on wan and out on dmz.</p>
<h2 id="step-8-the-blocklist">Step 8: The blocklist <a class="anchor" data-pagefind-ignore href="#step-8-the-blocklist" aria-label="Link to this section">#</a></h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-hcl" data-lang="hcl"><span class="line"><span class="cl"><span class="c1"># Anchor api/blocklist. A quick block ends evaluation, so these win over
</span></span></span><span class="line"><span class="cl"><span class="c1"># any pass rule in another anchor.
</span></span></span><span class="line"><span class="cl"><span class="k">resource</span> <span class="s2">&#34;pf_anchor&#34; &#34;blocklist&#34;</span> {
</span></span><span class="line"><span class="cl"><span class="n">  name</span> <span class="o">=</span> <span class="s2">&#34;blocklist&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">  rules</span> <span class="o">=</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl">    <span class="k">for</span> <span class="k">family</span> <span class="k">in</span> <span class="p">[</span><span class="s2">&#34;inet&#34;, &#34;inet6&#34;</span><span class="p">]</span> <span class="err">:</span> {
</span></span><span class="line"><span class="cl"><span class="n">      action</span>    <span class="o">=</span> <span class="s2">&#34;block&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      quick</span>     <span class="o">=</span> <span class="kt">true</span>
</span></span><span class="line"><span class="cl"><span class="n">      log</span>       <span class="o">=</span> <span class="kt">true</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;wan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="k">family</span>
</span></span><span class="line"><span class="cl"><span class="n">      from</span>      <span class="o">=</span><span class="n"> { table</span> <span class="o">=</span> <span class="k">pf_table</span><span class="p">.</span><span class="k">blocklist</span><span class="p">.</span><span class="k">name</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;blocklist&#34;</span>
</span></span><span class="line"><span class="cl">    }
</span></span><span class="line"><span class="cl">  <span class="p">]</span>
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></div><p><code>quick</code> ends evaluation on a match, so these blocks win over every pass rule in every anchor, whatever order pf evaluates the anchors in. <code>log</code> sends the dropped packets to <code>pflog0</code>.</p>
<p>Feed the table from wherever your threat data lives: a variable, a file, or a data source. Updating it is a table replace on the firewall, not a ruleset reload.</p>
<h2 id="step-9-qos-on-the-uplink">Step 9: QoS on the uplink <a class="anchor" data-pagefind-ignore href="#step-9-qos-on-the-uplink" aria-label="Link to this section">#</a></h2>
<p>An upload that fills the uplink makes everything else on the line feel slow: VoIP breaks up, SSH lags, and downloads crawl because their ACKs queue behind the upload. HFSC queues fix that by sharing the uplink by rule:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-hcl" data-lang="hcl"><span class="line"><span class="cl"><span class="c1"># QoS for a 100 Mbit/s uplink. Queues hang off a physical device (not a
</span></span></span><span class="line"><span class="cl"><span class="c1"># group), so the root names the device behind wan.
</span></span></span><span class="line"><span class="cl"><span class="k">variable</span> <span class="s2">&#34;uplink&#34;</span> {
</span></span><span class="line"><span class="cl"><span class="n">  type</span>    <span class="o">=</span> <span class="k">string</span>
</span></span><span class="line"><span class="cl"><span class="n">  default</span> <span class="o">=</span> <span class="s2">&#34;vio0&#34;</span>
</span></span><span class="line"><span class="cl">}
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">resource</span> <span class="s2">&#34;pf_qos&#34; &#34;uplink&#34;</span> {
</span></span><span class="line"><span class="cl"><span class="n">  queues</span> <span class="o">=</span> <span class="p">[</span>
</span></span><span class="line"><span class="cl"><span class="n">    { name</span> <span class="o">=</span><span class="n"> &#34;uplink&#34;, interface</span> <span class="o">=</span><span class="n"> var.uplink, bandwidth</span> <span class="o">=</span><span class="n"> &#34;100M&#34;, max</span> <span class="o">=</span> <span class="s2">&#34;100M&#34;</span> }<span class="p">,</span>
</span></span><span class="line"><span class="cl"><span class="n">    { name</span> <span class="o">=</span><span class="n"> &#34;std&#34;, parent</span> <span class="o">=</span><span class="n"> &#34;uplink&#34;, bandwidth</span> <span class="o">=</span><span class="n"> &#34;60M&#34;, default</span> <span class="o">=</span><span class="n"> true, flows</span> <span class="o">=</span> <span class="m">1024</span> }<span class="p">,</span>
</span></span><span class="line"><span class="cl"><span class="n">    { name</span> <span class="o">=</span><span class="n"> &#34;voip&#34;, parent</span> <span class="o">=</span><span class="n"> &#34;uplink&#34;, bandwidth</span> <span class="o">=</span><span class="n"> &#34;10M&#34;, min</span> <span class="o">=</span> <span class="s2">&#34;10M&#34;</span> }<span class="p">,</span>
</span></span><span class="line"><span class="cl"><span class="n">    { name</span> <span class="o">=</span><span class="n"> &#34;interactive&#34;, parent</span> <span class="o">=</span><span class="n"> &#34;uplink&#34;, bandwidth</span> <span class="o">=</span><span class="n"> &#34;10M&#34;, min</span> <span class="o">=</span> <span class="s2">&#34;5M&#34;</span> }<span class="p">,</span>
</span></span><span class="line"><span class="cl"><span class="n">    { name</span> <span class="o">=</span><span class="n"> &#34;bulk&#34;, parent</span> <span class="o">=</span><span class="n"> &#34;uplink&#34;, bandwidth</span> <span class="o">=</span><span class="n"> &#34;20M&#34;, max</span> <span class="o">=</span> <span class="s2">&#34;40M&#34;</span> }<span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">]</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">  rules</span> <span class="o">=</span> <span class="p">[</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # Classify where the state is created: in on lan or dmz. pf queues the
</span></span></span><span class="line"><span class="cl"><span class="c1">    # packets of that connection when they leave wan. Match rules run in
</span></span></span><span class="line"><span class="cl"><span class="c1">    # order and the last one that matches decides, so general rules first.
</span></span></span><span class="line"><span class="cl"><span class="c1">    #
</span></span></span><span class="line"><span class="cl"><span class="c1">    # All TCP from the LAN: data in std, bare ACKs in interactive. A full
</span></span></span><span class="line"><span class="cl"><span class="c1">    # uplink then can&#39;t hold back the ACKs that keep downloads going.
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;lan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;tcp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      queue</span>     <span class="o">=</span><span class="n"> { name</span> <span class="o">=</span><span class="n"> &#34;std&#34;, priority</span> <span class="o">=</span> <span class="s2">&#34;interactive&#34;</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;qos-acks&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span>
</span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;lan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;tcp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { ports</span> <span class="o">=</span><span class="n"> [{ from</span> <span class="o">=</span> <span class="m">22</span> }<span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      queue</span>     <span class="o">=</span><span class="n"> { name</span> <span class="o">=</span> <span class="s2">&#34;interactive&#34;</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;qos-ssh&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span>
</span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;lan&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;udp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { ports</span> <span class="o">=</span><span class="n"> [{ from</span> <span class="o">=</span><span class="n"> 5060, to</span> <span class="o">=</span><span class="n"> 5061 }, { from</span> <span class="o">=</span><span class="n"> 10000, to</span> <span class="o">=</span> <span class="m">20000</span> }<span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      queue</span>     <span class="o">=</span><span class="n"> { name</span> <span class="o">=</span> <span class="s2">&#34;voip&#34;</span> }
</span></span><span class="line"><span class="cl"><span class="n">      prio</span>      <span class="o">=</span><span class="n"> { normal</span> <span class="o">=</span> <span class="m">6</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;qos-voip&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span><span class="c1">
</span></span></span><span class="line"><span class="cl"><span class="c1">    # Offsite backups from the DMZ: capped, and last in line.
</span></span></span><span class="line"><span class="cl">    {
</span></span><span class="line"><span class="cl"><span class="n">      direction</span> <span class="o">=</span> <span class="s2">&#34;in&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      interface</span> <span class="o">=</span> <span class="s2">&#34;dmz&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      family</span>    <span class="o">=</span> <span class="s2">&#34;inet&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      protocol</span>  <span class="o">=</span> <span class="s2">&#34;tcp&#34;</span>
</span></span><span class="line"><span class="cl"><span class="n">      to</span>        <span class="o">=</span><span class="n"> { ports</span> <span class="o">=</span><span class="n"> [{ from</span> <span class="o">=</span> <span class="m">873</span> }<span class="p">]</span> }
</span></span><span class="line"><span class="cl"><span class="n">      queue</span>     <span class="o">=</span><span class="n"> { name</span> <span class="o">=</span> <span class="s2">&#34;bulk&#34;</span> }
</span></span><span class="line"><span class="cl"><span class="n">      prio</span>      <span class="o">=</span><span class="n"> { normal</span> <span class="o">=</span> <span class="m">1</span> }
</span></span><span class="line"><span class="cl"><span class="n">      label</span>     <span class="o">=</span> <span class="s2">&#34;qos-backup&#34;</span>
</span></span><span class="line"><span class="cl">    }<span class="p">,</span>
</span></span><span class="line"><span class="cl">  <span class="p">]</span>
</span></span><span class="line"><span class="cl">}
</span></span></code></pre></div><p>How it fits together:</p>
<ul>
<li><strong>Queues shape what <em>leaves</em> an interface.</strong> These queues sit on the wan device, so they shape uploads. To shape downloads, put a second tree on the LAN device.</li>
<li><strong>The parent caps the total</strong> at 100M with <code>max</code>; set it slightly below your real upload speed so the queue builds here, where pf controls it, not in the modem. Children share it by <code>bandwidth</code>, <code>min</code> is a guarantee, <code>max</code> a hard cap. pfapid refuses children that add up to more than their parent; <code>pfctl</code> would accept that.</li>
<li><strong><code>std</code> is the default</strong> and an FQ-CoDel queue (<code>flows = 1024</code>): it keeps one heavy flow from adding latency to all the others.</li>
<li><strong>Classify where the connection starts.</strong> The rules match <code>in on lan</code> (or dmz); pf stores the queue in the connection&rsquo;s state and uses it when its packets leave wan. Keep that habit: a <code>match out on wan</code> rule never sees the replies to connections opened from the internet, such as the web server&rsquo;s responses.</li>
<li><strong>Two queues in one rule</strong>: <code>queue = { name = &quot;std&quot;, priority = &quot;interactive&quot; }</code> puts data in <code>std</code> and bare TCP ACKs in <code>interactive</code>. That one line is what keeps downloads fast during a big upload.</li>
<li><strong><code>prio</code></strong> is pf&rsquo;s simple 0–7 priority on interfaces without HFSC queues, such as lan here: VoIP towards the phones goes first too.</li>
<li><strong>QoS rules can only classify.</strong> They&rsquo;re <code>match</code> rules with no <code>pass</code>, <code>block</code> or NAT, so QoS can never change what the firewall lets through. That&rsquo;s also why it&rsquo;s a separate permission (<code>&quot;qos&quot;: true</code>).</li>
</ul>
<h2 id="step-10-plan-and-apply">Step 10: Plan and apply <a class="anchor" data-pagefind-ignore href="#step-10-plan-and-apply" aria-label="Link to this section">#</a></h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">terraform plan
</span></span></code></pre></div><p>Each anchor&rsquo;s plan includes <code>pf_rules</code>: the rules exactly as pf will load them, as <code>pfctl -nv</code> on the firewall printed them. This is what the plan showed in my lab (the QoS root was on <code>vio1</code> there):</p>
<pre tabindex="0"><code>pf_anchor.dmz_web:
  &#34;pass in on wan inet proto tcp from any to (wan) port = 80 flags S/SA label \&#34;web-rdr\&#34; rdr-to 10.0.20.10&#34;,
  &#34;pass in on wan inet proto tcp from any to (wan) port = 443 flags S/SA label \&#34;web-rdr\&#34; rdr-to 10.0.20.10&#34;,
  &#34;pass in on wan inet6 proto tcp from any to &lt;dmz_web&gt; port = 80 flags S/SA label \&#34;web-in\&#34;&#34;,
  ...
pf_anchor.egress:
  &#34;pass out on wan inet from 10.0.10.0/24 to any flags S/SA label \&#34;nat-out\&#34; nat-to (wan:0) round-robin&#34;,
  &#34;pass out on wan inet from 10.0.20.0/24 to any flags S/SA label \&#34;nat-out\&#34; nat-to (wan:0) round-robin&#34;,
  ...
pf_qos.uplink:
  &#34;queue uplink on vio1 bandwidth 100M, max 100M&#34;,
  &#34;queue std parent uplink flows 1024 bandwidth 60M default&#34;,
  ...
  &#34;match in on lan inet proto tcp all set (queue(std, interactive)) label \&#34;qos-acks\&#34;&#34;,
</code></pre><p>Lists become one line per port and per prefix, and pf adds its defaults (<code>flags S/SA</code>, <code>round-robin</code>). You see that before anything changes, not after.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">terraform apply
</span></span></code></pre></div><pre tabindex="0"><code>pf_table.dmz_web: Creation complete after 0s [id=dmz_web]
pf_table.blocklist: Creation complete after 0s [id=blocklist]
pf_anchor.egress: Creation complete after 0s [id=egress]
pf_qos.uplink: Creation complete after 0s [id=qos]
pf_anchor.dmz_web: Creation complete after 0s [id=dmz-web]
pf_anchor.blocklist: Creation complete after 0s [id=blocklist]

Apply complete! Resources: 6 added, 0 changed, 0 destroyed.
</code></pre><p>Each change was loaded, confirmed by the provider over the same connection, and only then written to pfapid&rsquo;s state. A second <code>terraform plan</code> reports no changes.</p>
<h2 id="step-11-check-it-on-the-firewall">Step 11: Check it on the firewall <a class="anchor" data-pagefind-ignore href="#step-11-check-it-on-the-firewall" aria-label="Link to this section">#</a></h2>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">pfctl -sA                    <span class="c1"># anchors: api, api/blocklist, api/dmz-web, api/egress, api-qos</span>
</span></span><span class="line"><span class="cl">pfctl -a api/egress -vsr     <span class="c1"># an anchor&#39;s rules, with counters</span>
</span></span><span class="line"><span class="cl">pfctl -t blocklist -T show   <span class="c1"># table contents</span>
</span></span><span class="line"><span class="cl">pfctl -vsq                   <span class="c1"># queues, with packets and drops</span>
</span></span><span class="line"><span class="cl">tcpdump -n -e -ttt -i pflog0 <span class="c1"># what the default-deny and the blocklist drop, live</span>
</span></span></code></pre></div><h2 id="the-guardrails-in-action">The guardrails in action <a class="anchor" data-pagefind-ignore href="#the-guardrails-in-action" aria-label="Link to this section">#</a></h2>
<p><strong>Drift.</strong> I flushed an anchor by hand with <code>pfctl -a api/dmz-web -F rules</code>. The next plan:</p>
<pre tabindex="0"><code>Warning: pf anchor api/dmz-web differs from its committed rules

The rules loaded in pf were changed outside pfapid. The next apply loads the
configured rules again.
Expected:
  pass in on wan inet proto tcp from any to (wan) port = 80 flags S/SA label &#34;web-rdr&#34; rdr-to 10.0.20.10
  ...
Plan: 0 to add, 1 to change, 0 to destroy.
</code></pre><p><code>terraform apply</code> loaded the rules again.</p>
<p><strong>The management interface.</strong> A rule that opens RDP on <code>mgmt</code> fails at plan time:</p>
<pre tabindex="0"><code>Error: Rules rejected by the firewall

pfapid: 400 invalid: rule 0: invalid: interface &#34;mgmt&#34; is not allowed
</code></pre><p><strong>A rule pf won&rsquo;t take.</strong> pfapid runs <code>pfctl -n</code> during the plan, so a rule pf would refuse fails <code>terraform plan</code>, with pf&rsquo;s own error message, before anything is loaded.</p>
<h2 id="limits-worth-knowing">Limits worth knowing <a class="anchor" data-pagefind-ignore href="#limits-worth-knowing" aria-label="Link to this section">#</a></h2>
<ul>
<li><strong>One firewall, one provider block.</strong> For an HA pair, configure two providers (with <code>alias</code>) and apply the same modules to both; pfsync keeps the states in sync, not the rules.</li>
<li><strong><code>self</code> is expanded when <code>pf.conf</code> loads.</strong> If the firewall&rsquo;s addresses change, reload the base ruleset.</li>
<li><strong>No hairpin NAT.</strong> LAN clients reach the web server on its DMZ address, not on the public one. Use split DNS.</li>
<li><strong>Queues are bit rates, not percentages</strong>: pf doesn&rsquo;t support percentages in HFSC yet.</li>
</ul>
<p>Everything in this post was planned and applied with provider v0.1.0 and pfapid v0.1.2 on an OpenBSD 7.9 lab VM, and the pf output shown is what pf loaded there. The code, the design notes and the pf behaviour pfapid relies on are on GitHub: <a href="https://github.com/pderuiter/pfapid" rel="noopener">pfapid</a> and <a href="https://github.com/pderuiter/terraform-provider-pf" rel="noopener">terraform-provider-pf</a>. Issues and pull requests are welcome.</p>
]]></content:encoded></item><item><title>OpenBSD VPN Road Warrior Setup with IKEv2</title><link>https://blog.bsdserver.nl/openbsd-vpn-road-warrior-setup-with-ikev2/</link><guid isPermaLink="false">693b521479eb54000160a4bb</guid><dc:creator>Patrick de Ruiter</dc:creator><pubDate>Thu, 11 Dec 2025 23:21:56 +0000</pubDate><description>Introduction OpenBSD is known for its security and clean design, making it an ideal choice for a VPN server.</description><category>OpenBSD</category><category>VPN</category><category>Security</category><category>Networking</category><media:content url="https://blog.bsdserver.nl/content/images/2026/01/openbsd-road-warior-vpn.png" medium="image"/><content:encoded><![CDATA[<p>Laptops and phones on the road need a secure way back into your network, or a trusted exit to the internet from hotel and café Wi-Fi. OpenBSD is known for its security and clean design, making it an ideal choice for that VPN server.</p>
<p>This guide sets up a road warrior (mobile client) VPN on OpenBSD with iked, the native IKEv2 daemon. The steps were verified on OpenBSD 7.9. Clients authenticate the server by certificate and themselves with a username and password.</p>
<h2 id="why-iked">Why iked? <a class="anchor" data-pagefind-ignore href="#why-iked" aria-label="Link to this section">#</a></h2>
<ul>
<li><strong>In base</strong> - iked, PF and LibreSSL ship with OpenBSD. Nothing to install.</li>
<li><strong>IKEv2</strong> - The protocol most desktop and mobile operating systems support natively, so clients often need no extra app.</li>
<li><strong>Small configuration</strong> - One policy in <code>/etc/iked.conf</code> covers all road warriors.</li>
</ul>
<h3 id="when-to-use-it">When to use it <a class="anchor" data-pagefind-ignore href="#when-to-use-it" aria-label="Link to this section">#</a></h3>
<ul>
<li><strong>Use it</strong> for a handful to a few dozen mobile users who need access to your network or a trusted exit to the internet.</li>
<li><strong>Skip it</strong> for site-to-site links between fixed networks (a different iked policy) or when you need per-user certificates and central user management; this setup keeps users in a config file.</li>
</ul>
<h2 id="how-it-works">How It Works <a class="anchor" data-pagefind-ignore href="#how-it-works" aria-label="Link to this section">#</a></h2>
<table>
	<thead>
			<tr>
					<th>Component</th>
					<th>Runs on</th>
					<th>Job</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>iked</td>
					<td>VPN server</td>
					<td>Negotiates IKEv2, authenticates clients (EAP-MSCHAPv2), hands out addresses and DNS.</td>
			</tr>
			<tr>
					<td>Certificates (CA + server)</td>
					<td>VPN server, CA cert also on clients</td>
					<td>Prove to clients that they talk to the real server.</td>
			</tr>
			<tr>
					<td>PF</td>
					<td>VPN server</td>
					<td>Lets IKE and ESP in, passes tunnel traffic on <code>enc0</code>, NATs clients to the internet.</td>
			</tr>
			<tr>
					<td>IKEv2 client</td>
					<td>Laptop or phone</td>
					<td>Connects, checks the server certificate, logs in with username and password.</td>
			</tr>
	</tbody>
</table>
<p>The flow: client connects on UDP 500/4500 → verifies the server certificate against the CA → logs in with EAP → gets an address from <code>10.10.10.0/24</code> → its traffic goes through the tunnel → PF NATs it out of the server.</p>
<h2 id="prerequisites">Prerequisites <a class="anchor" data-pagefind-ignore href="#prerequisites" aria-label="Link to this section">#</a></h2>
<ul>
<li>A VPS or dedicated server running a supported OpenBSD release (the steps were verified on 7.9)</li>
<li>A domain name or static public IP address for the VPN server</li>
<li>Root access to your server</li>
</ul>
<h2 id="step-1-generate-certificates-and-keys">Step 1: Generate Certificates and Keys <a class="anchor" data-pagefind-ignore href="#step-1-generate-certificates-and-keys" aria-label="Link to this section">#</a></h2>
<p>Clients must be sure they talk to your server before they send a password. You build a small CA with OpenSSL (LibreSSL) and use it to sign the server certificate.</p>
<h3 id="create-the-directory-for-private-keys">Create the Directory for Private Keys <a class="anchor" data-pagefind-ignore href="#create-the-directory-for-private-keys" aria-label="Link to this section">#</a></h3>
<p>On OpenBSD <code>/etc/ssl/private</code> already exists, so <code>-p</code> keeps <code>mkdir</code> from failing; <code>chmod 700</code> keeps it root-only:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">cd</span> /etc/ssl
</span></span><span class="line"><span class="cl">mkdir -p private
</span></span><span class="line"><span class="cl">chmod <span class="m">700</span> private
</span></span></code></pre></div><h3 id="generate-the-ca-private-key">Generate the CA Private Key <a class="anchor" data-pagefind-ignore href="#generate-the-ca-private-key" aria-label="Link to this section">#</a></h3>
<p>This key signs the server&rsquo;s certificate. Anyone who has it can create certificates your clients trust, so keep it protected:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">openssl genrsa -out private/cakey.pem <span class="m">4096</span>
</span></span></code></pre></div><h3 id="create-the-ca-certificate">Create the CA Certificate <a class="anchor" data-pagefind-ignore href="#create-the-ca-certificate" aria-label="Link to this section">#</a></h3>
<p>This self-signed certificate is what clients trust. OpenBSD&rsquo;s LibreSSL <code>openssl.cnf</code> adds no extensions by default, so without the <code>-addext</code> lines you get an X.509 v1 certificate without the CA flag, which clients such as strongSwan reject (&ldquo;ca certificate lacks CA basic constraint&rdquo;):</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">openssl req -x509 -new -nodes <span class="se">\
</span></span></span><span class="line"><span class="cl">            -key private/cakey.pem <span class="se">\
</span></span></span><span class="line"><span class="cl">            -sha256 -days <span class="m">3650</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">            -out cacert.pem <span class="se">\
</span></span></span><span class="line"><span class="cl">            -subj <span class="s2">&#34;/C=NL/O=VPN/CN=VPN CA&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">            -addext <span class="s2">&#34;basicConstraints=critical,CA:TRUE&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">            -addext <span class="s2">&#34;keyUsage=critical,keyCertSign,cRLSign&#34;</span>
</span></span></code></pre></div><h3 id="generate-the-vpn-server-key">Generate the VPN Server Key <a class="anchor" data-pagefind-ignore href="#generate-the-vpn-server-key" aria-label="Link to this section">#</a></h3>
<p>The server&rsquo;s own private key:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">openssl genrsa -out private/vpn-server-key.pem <span class="m">4096</span>
</span></span></code></pre></div><h3 id="create-the-vpn-server-certificate-signing-request-csr">Create the VPN Server Certificate Signing Request (CSR) <a class="anchor" data-pagefind-ignore href="#create-the-vpn-server-certificate-signing-request-csr" aria-label="Link to this section">#</a></h3>
<p>The CSR carries the server&rsquo;s name and public key to the CA for signing:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">openssl req -new -key private/vpn-server-key.pem <span class="se">\
</span></span></span><span class="line"><span class="cl">            -out vpn-server-csr.pem <span class="se">\
</span></span></span><span class="line"><span class="cl">            -subj <span class="s2">&#34;/C=NL/O=VPN/CN=vpn.example.com&#34;</span>
</span></span></code></pre></div><h3 id="sign-the-vpn-server-certificate">Sign the VPN Server Certificate <a class="anchor" data-pagefind-ignore href="#sign-the-vpn-server-certificate" aria-label="Link to this section">#</a></h3>
<p>The CA signs the CSR. Two extensions matter:</p>
<ul>
<li><code>subjectAltName=DNS:vpn.example.com</code>: the name clients connect to. It must match.</li>
<li><code>extendedKeyUsage=serverAuth</code>: expected by Windows and Apple clients.</li>
</ul>
<p>OpenBSD&rsquo;s default shell (ksh) does not support <code>&lt;(...)</code> process substitution, so write the extensions to a file first:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl"><span class="nb">printf</span> <span class="s2">&#34;subjectAltName=DNS:vpn.example.com\nextendedKeyUsage=serverAuth\n&#34;</span> &gt; vpn-server-ext.cnf
</span></span><span class="line"><span class="cl">openssl x509 -req -in vpn-server-csr.pem <span class="se">\
</span></span></span><span class="line"><span class="cl">             -CA cacert.pem -CAkey private/cakey.pem <span class="se">\
</span></span></span><span class="line"><span class="cl">             -CAcreateserial -out vpn-server-cert.pem <span class="se">\
</span></span></span><span class="line"><span class="cl">             -days <span class="m">1825</span> -sha256 <span class="se">\
</span></span></span><span class="line"><span class="cl">             -extfile vpn-server-ext.cnf
</span></span></code></pre></div><h3 id="install-the-certificates-for-iked">Install the Certificates for iked <a class="anchor" data-pagefind-ignore href="#install-the-certificates-for-iked" aria-label="Link to this section">#</a></h3>
<p>iked does not look in <code>/etc/ssl</code>. It loads its files from its own directories:</p>
<table>
	<thead>
			<tr>
					<th>File</th>
					<th>Location</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>CA certificates</td>
					<td><code>/etc/iked/ca/</code></td>
			</tr>
			<tr>
					<td>Its own certificate</td>
					<td><code>/etc/iked/certs/</code></td>
			</tr>
			<tr>
					<td>Its private key</td>
					<td><code>/etc/iked/private/local.key</code></td>
			</tr>
	</tbody>
</table>
<p>Skip this step and iked silently authenticates with the raw public key it generated at install time; clients then fail with &ldquo;no trusted ECDSA public key found for &lsquo;vpn.example.com&rsquo;&rdquo;. The first command keeps a backup of that generated key:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">cp /etc/iked/private/local.key /etc/iked/private/local.key.orig
</span></span><span class="line"><span class="cl">cp /etc/ssl/cacert.pem /etc/iked/ca/ca.crt
</span></span><span class="line"><span class="cl">cp /etc/ssl/vpn-server-cert.pem /etc/iked/certs/vpn.example.com.crt
</span></span><span class="line"><span class="cl">install -m <span class="m">600</span> /etc/ssl/private/vpn-server-key.pem /etc/iked/private/local.key
</span></span></code></pre></div><p>Copy <code>cacert.pem</code> (never the key) to your clients so they can verify the server.</p>
<h2 id="step-2-edit-iked-configuration">Step 2: Edit iked Configuration <a class="anchor" data-pagefind-ignore href="#step-2-edit-iked-configuration" aria-label="Link to this section">#</a></h2>
<p><code>/etc/iked.conf</code> holds the users and one policy that accepts all road warrior clients. Open it with your preferred editor (vi is in base; vim is a package):</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">vi /etc/iked.conf
</span></span></code></pre></div><p>And add the following configuration:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">user <span class="s2">&#34;username&#34;</span> <span class="s2">&#34;password&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">ikev2 <span class="s2">&#34;roadwarrior&#34;</span> passive esp <span class="se">\
</span></span></span><span class="line"><span class="cl">    from 0.0.0.0/0 to dynamic <span class="se">\
</span></span></span><span class="line"><span class="cl">    <span class="nb">local</span> 82.197.198.151 peer any <span class="se">\
</span></span></span><span class="line"><span class="cl">    srcid vpn.example.com <span class="se">\
</span></span></span><span class="line"><span class="cl">    eap <span class="s2">&#34;mschap-v2&#34;</span> <span class="se">\
</span></span></span><span class="line"><span class="cl">    config address 10.10.10.0/24 <span class="se">\
</span></span></span><span class="line"><span class="cl">    config name-server 192.168.2.166 <span class="se">\
</span></span></span><span class="line"><span class="cl">    tag <span class="s2">&#34;</span><span class="nv">$name</span><span class="s2">&#34;</span>
</span></span></code></pre></div><table>
	<thead>
			<tr>
					<th>Setting</th>
					<th>What it does</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>user &quot;username&quot; &quot;password&quot;</code></td>
					<td>A client login. Add one <code>user</code> line per client.</td>
			</tr>
			<tr>
					<td><code>passive</code></td>
					<td>The server waits for clients to connect; it never initiates.</td>
			</tr>
			<tr>
					<td><code>from 0.0.0.0/0 to dynamic</code></td>
					<td>Clients reach everything through the tunnel; <code>dynamic</code> narrows the flow to the address each client is assigned.</td>
			</tr>
			<tr>
					<td><code>local 82.197.198.151 peer any</code></td>
					<td>Replace with your VPN server&rsquo;s public IP. <code>peer any</code>: clients connect from any address.</td>
			</tr>
			<tr>
					<td><code>srcid vpn.example.com</code></td>
					<td>The server&rsquo;s identity. Must match the certificate name.</td>
			</tr>
			<tr>
					<td><code>eap &quot;mschap-v2&quot;</code></td>
					<td>Clients authenticate with a <code>user</code> name and password.</td>
			</tr>
			<tr>
					<td><code>config address 10.10.10.0/24</code></td>
					<td>The pool client addresses come from.</td>
			</tr>
			<tr>
					<td><code>config name-server 192.168.2.166</code></td>
					<td>The DNS server pushed to clients. Set your own.</td>
			</tr>
			<tr>
					<td><code>tag &quot;$name&quot;</code></td>
					<td>Tags the tunnel&rsquo;s traffic for PF. The rules below do not need it.</td>
			</tr>
	</tbody>
</table>
<p>Do not use <code>to 0.0.0.0/0</code> instead of <code>to dynamic</code>: that installs a 0.0.0.0/0 -&gt; 0.0.0.0/0 flow on the server and sends all of the server&rsquo;s own traffic (including your SSH session) into the first client&rsquo;s tunnel.</p>
<p>Make sure the file is only readable by root, or iked refuses to load it. <code>iked -n</code> checks the syntax without starting anything:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">chmod <span class="m">600</span> /etc/iked.conf
</span></span><span class="line"><span class="cl">iked -n
</span></span></code></pre></div><h2 id="step-3-configure-pf-firewall-rules">Step 3: Configure PF Firewall Rules <a class="anchor" data-pagefind-ignore href="#step-3-configure-pf-firewall-rules" aria-label="Link to this section">#</a></h2>
<p>PF must let the VPN protocols in and NAT the clients out. Open the PF configuration file:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">vi /etc/pf.conf
</span></span></code></pre></div><p>Add the following rules to permit IKE and ESP traffic and to NAT the VPN clients out of the server:</p>
<pre tabindex="0"><code>pass in quick on egress proto udp from any to any port { 500, 4500 }
pass out quick on egress proto udp from any to any port { 500, 4500 }
pass in quick on egress proto esp from any to any
pass out quick on egress proto esp from any to any
pass in quick on enc0 all
pass out quick on enc0 all
match out on egress from 10.10.10.0/24 to any nat-to (egress:0)
</code></pre><table>
	<thead>
			<tr>
					<th>Rule</th>
					<th>Why</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>UDP 500, 4500</td>
					<td>IKE, and NAT traversal for clients behind NAT (most of them).</td>
			</tr>
			<tr>
					<td><code>proto esp</code></td>
					<td>Encrypted traffic from clients that are not behind NAT.</td>
			</tr>
			<tr>
					<td><code>enc0</code></td>
					<td>The decrypted tunnel traffic appears on this interface.</td>
			</tr>
			<tr>
					<td><code>nat-to (egress:0)</code></td>
					<td>Client traffic leaves with the server&rsquo;s public address.</td>
			</tr>
	</tbody>
</table>
<p>The server must also forward packets between the tunnel and the internet. The first command enables it now, the second after a reboot:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">sysctl net.inet.ip.forwarding<span class="o">=</span><span class="m">1</span>
</span></span><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s1">&#39;net.inet.ip.forwarding=1&#39;</span> &gt;&gt; /etc/sysctl.conf
</span></span></code></pre></div><p>Reload the PF configuration:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">pfctl -f /etc/pf.conf
</span></span></code></pre></div><h2 id="step-4-enable-and-start-the-iked-service">Step 4: Enable and Start the iked Service <a class="anchor" data-pagefind-ignore href="#step-4-enable-and-start-the-iked-service" aria-label="Link to this section">#</a></h2>
<p>Enable the service so iked starts at boot. No flags are needed: iked no longer blocks the server&rsquo;s own IPv6 traffic, and on 7.9 the old <code>-6</code> flag only logs &ldquo;the -6 option is ignored and will be removed in the future&rdquo;:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">rcctl <span class="nb">enable</span> iked
</span></span></code></pre></div><p>Start the service:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-shell" data-lang="shell"><span class="line"><span class="cl">rcctl start iked
</span></span></code></pre></div><h2 id="troubleshooting">Troubleshooting <a class="anchor" data-pagefind-ignore href="#troubleshooting" aria-label="Link to this section">#</a></h2>
<table>
	<thead>
			<tr>
					<th>Symptom</th>
					<th>Cause / fix</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Client: &ldquo;no trusted ECDSA public key found for &lsquo;vpn.example.com&rsquo;&rdquo;</td>
					<td>iked uses its raw install-time key. Install the certificates in <code>/etc/iked/</code> (Step 1).</td>
			</tr>
			<tr>
					<td>strongSwan: &ldquo;ca certificate lacks CA basic constraint&rdquo;</td>
					<td>CA created without the <code>-addext</code> lines. Recreate it.</td>
			</tr>
			<tr>
					<td>iked refuses to load the config</td>
					<td><code>/etc/iked.conf</code> is not mode 600.</td>
			</tr>
			<tr>
					<td>SSH to the server drops when a client connects</td>
					<td>The policy uses <code>to 0.0.0.0/0</code>. Use <code>to dynamic</code>.</td>
			</tr>
			<tr>
					<td>Clients connect but cannot reach the internet</td>
					<td>IP forwarding off, or the <code>nat-to</code> rule missing.</td>
			</tr>
	</tbody>
</table>
<h2 id="security-considerations">Security Considerations <a class="anchor" data-pagefind-ignore href="#security-considerations" aria-label="Link to this section">#</a></h2>
<ul>
<li><strong>Passwords are in plain text</strong> in <code>/etc/iked.conf</code>. Keep it mode 600 and use a unique password per client.</li>
<li><strong>Protect the CA key.</strong> <code>/etc/ssl/private/cakey.pem</code> can sign certificates your clients trust. Consider moving it off the server once the certificate is signed.</li>
<li><strong>Clients must verify the server.</strong> Install <code>cacert.pem</code> on every client; EAP passwords are only safe when the server is authenticated first.</li>
</ul>
<h2 id="conclusion">Conclusion <a class="anchor" data-pagefind-ignore href="#conclusion" aria-label="Link to this section">#</a></h2>
<p>OpenBSD is now an IKEv2 VPN server with base tools only: iked for the tunnels, PF for filtering and NAT. Road warrior clients connect from anywhere with encrypted traffic, and adding a user is one line in <code>iked.conf</code>.</p>
]]></content:encoded></item><item><title>Setting Up TACACS+ on OpenBSD 7.9 for Legacy Network Devices</title><link>https://blog.bsdserver.nl/setting-up-tacacs-on-openbsd-7-4-for-legacy-network-devices/</link><guid isPermaLink="false">693b516f79eb54000160a499</guid><dc:creator>Patrick de Ruiter</dc:creator><pubDate>Thu, 11 Dec 2025 23:19:11 +0000</pubDate><description>TACACS+ (Terminal Access Controller Access-Control System Plus) is a protocol that provides authentication, authorization, and accounting (AAA) services.</description><category>OpenBSD</category><category>TACACS+</category><category>Security</category><category>Networking</category><media:content url="https://blog.bsdserver.nl/content/images/2026/01/openbsd-tacacs.png" medium="image"/><content:encoded><![CDATA[<p>TACACS+ (Terminal Access Controller Access-Control System Plus) is a protocol that provides authentication, authorization, and accounting (AAA) services. It is commonly used to control access to network devices from one central place, instead of a local account on every switch.</p>
<p>This post installs the tac_plus TACACS+ server on OpenBSD, restricted to a management network. OpenBSD&rsquo;s security-oriented approach gives a stable environment for a protocol that itself is weak.</p>
<p><strong>Critical Security Warning:</strong> TACACS+ is <strong>horribly insecure</strong> and should only be used when no better alternatives, such as <strong>RADIUS with RadSec</strong>, are available. It transmits authentication information in a way that is susceptible to interception, making it unsuitable for modern secure environments. Additionally, the simplest tac_plus configuration (<code>global = cleartext</code>, used below) <strong>stores passwords in clear text</strong>, which poses a significant security risk. tac_plus can store bcrypt hashes instead (<code>login = des</code> and <code>pap = des</code>, hashes generated with <code>encrypt(1)</code>), but the <code>global</code> keyword only accepts cleartext.</p>
<p><strong>TACACS+ should only be used in a network segment that has no direct internet access and in a VLAN that is exclusively dedicated to management purposes.</strong> It should only be deployed when there is <strong>no other way</strong> to centrally manage devices.</p>
<h2 id="why-tacacs">Why TACACS+? <a class="anchor" data-pagefind-ignore href="#why-tacacs" aria-label="Link to this section">#</a></h2>
<ul>
<li><strong>Central logins</strong> - One user list for all devices, instead of local accounts per switch.</li>
<li><strong>Accounting</strong> - A log of who did what on which device.</li>
<li><strong>Legacy support</strong> - Old devices that only speak TACACS+, or handle RADIUS badly.</li>
</ul>
<h3 id="when-to-use-it">When to use it <a class="anchor" data-pagefind-ignore href="#when-to-use-it" aria-label="Link to this section">#</a></h3>
<ul>
<li><strong>Use it</strong> only for legacy devices without a better option. I use TACACS+ for authenticating to some old Zyxel switches that do not work well with RADIUS, a necessary compromise in specific legacy environments.</li>
<li><strong>Skip it</strong> for anything that supports RADIUS with RadSec or another modern mechanism.</li>
</ul>
<h2 id="how-it-works">How It Works <a class="anchor" data-pagefind-ignore href="#how-it-works" aria-label="Link to this section">#</a></h2>
<table>
	<thead>
			<tr>
					<th>Component</th>
					<th>Runs on</th>
					<th>Job</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>tac_plus</td>
					<td>OpenBSD server, management VLAN</td>
					<td>Checks logins against <code>/etc/tac_plus.conf</code>, writes accounting records.</td>
			</tr>
			<tr>
					<td>TACACS+ client</td>
					<td>Each network device</td>
					<td>Sends login attempts to tac_plus on TCP port 49, using the shared key.</td>
			</tr>
			<tr>
					<td>newsyslog</td>
					<td>OpenBSD server</td>
					<td>Rotates the tac_plus log files.</td>
			</tr>
	</tbody>
</table>
<p>The flow: an admin logs in on a switch → the switch sends the credentials to tac_plus on port 49 → tac_plus checks the user → accept or reject → the session is logged to the accounting file and syslog.</p>
<h2 id="step-1-install-tacacs-on-openbsd">Step 1: Install TACACS+ on OpenBSD <a class="anchor" data-pagefind-ignore href="#step-1-install-tacacs-on-openbsd" aria-label="Link to this section">#</a></h2>
<p>The server is a package from the OpenBSD ports tree:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">pkg_add tacacs+
</span></span></code></pre></div><p>This installs tac_plus F4.0.4.28 (package <code>tacacs+-4.0.4.28p4v0</code>, with Python 3 as a dependency), creates the <code>_tacacs</code> user and group, and installs <code>/etc/rc.d/tac_plus</code> plus a sample <code>/etc/tac_plus.conf</code>. These steps were verified on OpenBSD 7.9.</p>
<h2 id="step-2-configure-log-rotation">Step 2: Configure Log Rotation <a class="anchor" data-pagefind-ignore href="#step-2-configure-log-rotation" aria-label="Link to this section">#</a></h2>
<p>Accounting logs grow with every login and command. Add the following entries to <code>/etc/newsyslog.conf</code> so they are rotated:</p>
<pre tabindex="0"><code>/var/log/tac_plus/tac.acct        _tacacs:_tacacs 644  7     250  *     Z
/var/log/tac_plus/tac.log         _tacacs:_tacacs 644  7     250  *     Z
/var/log/tac_plus/tacwho.log      _tacacs:_tacacs 644  7     250  *     Z
</code></pre><ul>
<li><code>_tacacs:_tacacs 644</code>: owner and mode of the new log file, so tac_plus can keep writing.</li>
<li><code>7</code>: number of old files to keep.</li>
<li><code>250</code>: rotate when the file reaches 250 KB.</li>
<li><code>*</code>: no time-based rotation; size only.</li>
<li><code>Z</code>: compress rotated files.</li>
</ul>
<p>newsyslog runs from cron and rereads <code>/etc/newsyslog.conf</code> on every run, so nothing needs to be restarted. Check the entries with a dry run:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">newsyslog -n -v <span class="p">|</span> grep tac_plus
</span></span></code></pre></div><p>newsyslog skips files that do not exist yet, so on a fresh install this prints nothing. After the first login <code>tac.acct</code> shows up (<code>/var/log/tac_plus/tac.acct &lt;7Z&gt;: size (KB): ...</code>); a malformed line is reported as an error instead.</p>
<p>Note that tac_plus only writes <code>tac.log</code> when started with <code>-d</code>; its normal messages go to syslog (<code>/var/log/daemon</code>).</p>
<h2 id="step-3-configure-tacacs">Step 3: Configure TACACS+ <a class="anchor" data-pagefind-ignore href="#step-3-configure-tacacs" aria-label="Link to this section">#</a></h2>
<p><code>/etc/tac_plus.conf</code> holds the shared key, where accounting goes, and the users. Replace the sample with:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl"><span class="nv">key</span> <span class="o">=</span> <span class="s2">&#34;your_shared_secret_here&#34;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Default accounting file</span>
</span></span><span class="line"><span class="cl">accounting <span class="nv">file</span> <span class="o">=</span> /var/log/tac_plus/tac.acct
</span></span><span class="line"><span class="cl">accounting syslog
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># User definition</span>
</span></span><span class="line"><span class="cl"><span class="nv">user</span> <span class="o">=</span> administrator <span class="o">{</span>
</span></span><span class="line"><span class="cl">  default <span class="nv">service</span> <span class="o">=</span> permit
</span></span><span class="line"><span class="cl">  <span class="nv">global</span> <span class="o">=</span> cleartext your_password_here
</span></span><span class="line"><span class="cl">  <span class="nv">service</span> <span class="o">=</span> <span class="nb">exec</span> <span class="o">{</span>
</span></span><span class="line"><span class="cl">    priv-lvl <span class="o">=</span> <span class="m">15</span>
</span></span><span class="line"><span class="cl">  <span class="o">}</span>
</span></span><span class="line"><span class="cl"><span class="o">}</span>
</span></span></code></pre></div><table>
	<thead>
			<tr>
					<th>Setting</th>
					<th>What it does</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>key</code></td>
					<td>Shared secret between tac_plus and the devices. Configure the same value on each switch.</td>
			</tr>
			<tr>
					<td><code>accounting file</code></td>
					<td>Where accounting records are written (rotated in Step 2).</td>
			</tr>
			<tr>
					<td><code>accounting syslog</code></td>
					<td>Also send accounting records to syslog.</td>
			</tr>
			<tr>
					<td><code>user = ... { global = cleartext ... }</code></td>
					<td>A login and its password, for all services. Use <code>login = des</code> / <code>pap = des</code> with a hash to avoid cleartext.</td>
			</tr>
			<tr>
					<td><code>default service = permit</code></td>
					<td>Authorizes every service and command for this user. Must be the first line in the block.</td>
			</tr>
			<tr>
					<td><code>service = exec { priv-lvl = 15 }</code></td>
					<td>Gives the user privilege level 15 (full admin) on a device that asks for exec authorization.</td>
			</tr>
	</tbody>
</table>
<p>tac_plus denies authorization for anything that is not explicitly permitted. Without the last two settings the password check passes, but a switch that also asks for authorization refuses the shell.</p>
<p>Change the shared secret and user password to secure values. <code>-P</code> only parses the file and prints it, so you can check it before starting:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">tac_plus -C /etc/tac_plus.conf -P
</span></span></code></pre></div><h2 id="step-4-enable-tacacs-and-configure-the-listening-interface">Step 4: Enable TACACS+ and Configure the Listening Interface <a class="anchor" data-pagefind-ignore href="#step-4-enable-tacacs-and-configure-the-listening-interface" aria-label="Link to this section">#</a></h2>
<p>By default tac_plus listens on all addresses. <code>-B</code> binds it to the management address only.</p>
<p>Don&rsquo;t edit <code>/etc/rc.d/tac_plus</code> itself (package updates overwrite it and <code>pkg_check</code> flags it as modified); use <code>rcctl</code>, which stores the flags in <code>/etc/rc.conf.local</code>. Enable the service first: for a package service <code>rcctl set</code> refuses with &ldquo;rcctl: tac_plus is not enabled&rdquo;.</p>
<p>The flags replace the script&rsquo;s defaults, so repeat <code>-C</code>, <code>-Q</code> and <code>-U</code>; without <code>-C</code> tac_plus exits with &ldquo;no config file specified&rdquo;:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">rcctl <span class="nb">enable</span> tac_plus
</span></span><span class="line"><span class="cl">rcctl <span class="nb">set</span> tac_plus flags -C /etc/tac_plus.conf -Q _tacacs -U _tacacs -B 192.168.2.254
</span></span></code></pre></div><table>
	<thead>
			<tr>
					<th>Flag</th>
					<th>What it does</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>-C /etc/tac_plus.conf</code></td>
					<td>The configuration file. Required.</td>
			</tr>
			<tr>
					<td><code>-Q _tacacs</code> / <code>-U _tacacs</code></td>
					<td>Group and user tac_plus runs as, instead of root.</td>
			</tr>
			<tr>
					<td><code>-B 192.168.2.254</code></td>
					<td>The address to listen on. Use your management address.</td>
			</tr>
	</tbody>
</table>
<h2 id="step-5-start-tacacs">Step 5: Start TACACS+ <a class="anchor" data-pagefind-ignore href="#step-5-start-tacacs" aria-label="Link to this section">#</a></h2>
<p>The service now starts at boot; start it now as well:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">rcctl start tac_plus
</span></span></code></pre></div><p>Verify the service:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-sh" data-lang="sh"><span class="line"><span class="cl">rcctl check tac_plus
</span></span><span class="line"><span class="cl">netstat -an -f inet -p tcp <span class="p">|</span> grep <span class="s1">&#39;\.49 &#39;</span>
</span></span></code></pre></div><p>The listener should show your management address (for example <code>192.168.2.254.49</code>), not <code>*.49</code>.</p>
<h2 id="troubleshooting">Troubleshooting <a class="anchor" data-pagefind-ignore href="#troubleshooting" aria-label="Link to this section">#</a></h2>
<table>
	<thead>
			<tr>
					<th>Symptom</th>
					<th>Cause / fix</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>rcctl: tac_plus is not enabled</code></td>
					<td>Run <code>rcctl enable tac_plus</code> before <code>rcctl set</code>.</td>
			</tr>
			<tr>
					<td>tac_plus exits with &ldquo;no config file specified&rdquo;</td>
					<td>The <code>rcctl</code> flags lack <code>-C /etc/tac_plus.conf</code>.</td>
			</tr>
			<tr>
					<td>tac_plus exits with &ldquo;Unrecognised keyword default for user&rdquo;</td>
					<td><code>default service</code> is not the first line in the <code>user</code> block.</td>
			</tr>
			<tr>
					<td>Login accepted, but the switch refuses the shell</td>
					<td>The device asks for authorization; add <code>default service = permit</code> and <code>service = exec</code>.</td>
			</tr>
			<tr>
					<td>Listener shows <code>*.49</code></td>
					<td><code>-B</code> is not in the flags. Check <code>rcctl get tac_plus flags</code>.</td>
			</tr>
			<tr>
					<td><code>tac.log</code> stays empty</td>
					<td>Normal: without <code>-d</code> messages go to <code>/var/log/daemon</code>.</td>
			</tr>
	</tbody>
</table>
<h2 id="security-considerations">Security Considerations <a class="anchor" data-pagefind-ignore href="#security-considerations" aria-label="Link to this section">#</a></h2>
<ul>
<li><strong>Restrict access</strong>: Use firewall rules to allow only trusted devices to reach port 49.</li>
<li><strong>Use encryption</strong>: Implement encrypted communication channels where possible.</li>
<li><strong>Rotate credentials</strong>: Regularly update shared secrets and user passwords.</li>
<li><strong>Monitor logs</strong>: Periodically review logs for suspicious activities.</li>
</ul>
<h2 id="conclusion">Conclusion <a class="anchor" data-pagefind-ignore href="#conclusion" aria-label="Link to this section">#</a></h2>
<p>You now have a TACACS+ server on OpenBSD, listening only on the management network. However, <strong>TACACS+ is fundamentally insecure</strong> and should only be used when no better alternatives are available.</p>
<p><strong>Key Takeaways:</strong></p>
<ul>
<li>TACACS+ is commonly configured with clear-text passwords (as in this guide), making it <strong>extremely insecure</strong>.</li>
<li>It should <strong>only</strong> be deployed in a <strong>segmented network</strong> with no direct internet access.</li>
<li>It should only be used when no other option exists for centrally managing legacy devices.</li>
</ul>
<p><strong>If possible, consider migrating to a more secure authentication mechanism to better protect your network environment.</strong></p>
]]></content:encoded></item></channel></rss>