Setting up a home gateway with OpenBSD + other goodies

If you have a spare box with two or more NICs, you can turn it into a powerful OpenBSD router.
In this tutorial, I will walk you through my gateway configuration.

Remember! Do not blindly copy paste the configuration files!

Available hardware and network layout

My router is a Shuttle XH81V. It has two Realtek NICs.

I have a single local subnet,

Because of lack of additional NICs or a VLAN capable switch, there is no DMZ. To avoid exposing many services to the outside, I typically use ssh tunneling or a VPN to access local services behind the gateway.

I have a dedicated server hosted in a DC. I use tinc in a bridged mode configuration to make the server appear on my local subnet. This way, I can access the server transparently even on machines on my local network that I cannot install tinc to.

For IPv6, I use a Hurricane Electric tunnel. Their service has proven reliable with virtually no downtime experienced in the past year.

Topics covered

The following topics will be discussed:

Firewall, routing and NAT configuration

There is an excellent tutorial in the pf FAQ.
Fore more information, check the pf.conf(5), hostname.if(5) and ifconfig(8) manpages.


int_if   = "re1"
sshbox   = ""
martians = "{ \

set loginterface egress
set skip on lo0
set block-policy return

match in all scrub (no-df random-id)

match out on egress inet from !(egress:network) to any nat-to (egress:0)

block drop in quick on egress from { no-route urpf-failed $martians }
block return out quick on egress to $martians

pass in on $int_if
pass in on egress inet proto icmp to (egress)
pass in on egress inet proto tcp to (egress) port ssh rdr-to $sshbox
pass out




description "WAN"


description "LAN"

Reboot the router. This isn't required but it is a good idea to test that your changes are correctly set after a fresh boot.

DHCP server configuration

I use 2f30.org as the default search domain. I have a split horizon DNS configuration so I can access my machines from my local network as well as from the outside.


option domain-name "2f30.org";
option domain-name-servers;

subnet netmask {
    option routers;

    host sshbox {
        hardware ethernet aa:bb:cc:dd:ee:ff;

Update /etc/rc.conf.local:


Restart dhcpd:

/etc/rc.d/dhcpd restart

Split horizon DNS

I am using unbound(8) as a caching DNS resolver.


    access-control: allow

    local-data: "gw.2f30.org. IN A"
    local-data-ptr: " gw.2f30.org."

    local-data: "sshbox.2f30.org. IN A"
    local-data-ptr: " sshbox.2f30.org."

    name: "."

Update /etc/rc.conf.local:


Restart unbound:

/etc/rc.d/unbound restart

At this point, you should be able to plug a machine to your switch, get an IP address and browse the web.

You should also be able to access $sshbox from the outside over ssh on the default port. In my configuration this is a separate machine but could just as well be the router itself.

Using dnscrypt_proxy with unbound

First install dnscrypt_proxy from packages.

Adjust /etc/rc.conf.local:

dnscrypt_proxy_flags="-l /dev/null -R dnscrypt.eu-nl -a"

Start it:

/etc/rc.d/dnscrypt_proxy start

Then adjust the unbound configration:


    access-control: allow
    do-not-query-localhost: no  # needed as dnscrypt is listening on localhost

    local-data: "gw.2f30.org. IN A"
    local-data-ptr: " gw.2f30.org."

    local-data: "sshbox.2f30.org. IN A"
    local-data-ptr: " sshbox.2f30.org."

    name: "."

Restart unbound:

/etc/rc.d/unbound restart

You should use tcpdump(8) to confirm that DNS requests are encrypted.

PXE booting

I use PXE booting on my laptop to upgrade OpenBSD. I run a tftp server on my router to serve the latest bsd.rd.

Setting up tftpd and dhcpd for PXE booting

Prepare /tftpboot:

mkdir /tftpboot
cp /usr/mdec/pxeboot /tftpboot

Update /etc/dhcpd.conf:

subnet netmask {
    filename "pxeboot";

Update /etc/rc.conf.local:

tftpd_flags="-l /tftpboot"

Restart dhcpd and tftpd:

/etc/rc.d/tftpd restart
/etc/rc.d/dhcpd restart

Cron job to fetch latest bsd.rd

Use crontab -e as root to add a new job as follows:

15 10 * * * /usr/bin/ftp -o /tftpboot/bsd.rd http://ftp.openbsd.org/pub/OpenBSD/snapshots/amd64/bsd.rd 1>/dev/null

It will download bsd.rd once a day at 10:15 in the morning.

To test, plug your laptop to the switch and choose to boot over the network. Once you get to the OpenBSD boot prompt, type /bsd.rd and hit return.

Configuring an IPv6 gif(4) tunnel with Hurricane Electric

First of all, you will have to create an account on their website. From there, follow their guide to set up a tunnel. You will basically have to choose the tunnel endpoint. Find the one with the minimum latency.




description "Hurricane Electric 6in4 link"
tunnel <your-ipv4-endpoint> <their-ipv4-endpoint>
mtu 1480
!ifconfig gif0 inet6 alias 2001:XXXX:XXXX:XXXX::2 2001:XXXX:XXXX:XXXX::1 prefixlen 128
!route -n add -inet6 default 2001:XXXX:XXXX:XXXX::1


inet6 alias 2001:XXXX:XXXX:XXXX::1 64

This will add an IPv6 alias on your router's internal interface.


pass in on egress inet proto 41 from <their-ipv4-endpoint> to (egress)
pass in on gif0 inet6

Update /etc/rc.conf.local:


Reboot your router.

On your OpenBSD client, enable autoconfiguration:

ifconfig em0 inet6 autoconf

NetFlow sensor and collector configuration

I use NetFlow to get an idea of what kind of traffic passes through my gateway.

To configure a netflow sensor on the gateway:


set state-defaults pflow


flowsrc flowdst

Activate sensor:

pfctl -f /etc/pf.conf
sh /etc/netstart pflow0

On the receiver, in this case the box with address we'll install flowd. It is a secure and minimal netflow collector written by Damien Miller.

First, install flowd from ports.


logfile "/var/log/flowd"
listen on
flow source
store ALL

Restart flowd:

/etc/rc.d/flowd restart

Give it a moment and use flowd-reader(8) on the specified logfile to examine the flows.

Debugging tips

One of the advantages of using OpenBSD as opposed to a standard consumer grade router is that you have all the needed tools at your disposal for debugging your network. The following manpages should be of interest.

Keep your configuration as simple as possible. Do not randomly poke on sysctl knobs you do not understand. Rely on the defaults unless you have a good reason not to.

If you have made extensive changes on a running system, do a final reboot to make sure everything comes back up as expected.

Consider having a second machine connected over serial to your router. This way you can capture a trace if the router crashes. It can also be used as an out-of-band mechanism to configure your router without hooking up a monitor and a keyboard.

Reading material

I've found the following references highly informative and useful.