This is the multi-page printable view of this section. Click here to print.
Firewall
- 1: Linux Router
- 2: OpenWRT
- 2.1: OpenWRT in PVE
- 3: OPNsense
1 - Linux Router
Creating a Linux router is fairly simple. Some distros like Alpine Linux are well suited for it but any will do. I used Debian in this example.
Install the base OS without a desktop system. Assuming you have two network interfaces, pick one to be the LAN interface (traditionally the first one, eth0 or such) and set the address statically.
Basic Routing
To route, all you really need do is enable forwarding.
# as root
# enable
sysctl -w net.ipv4.ip_forward=1
# and persist
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
Private Range
If one side is a private network, such as in the 192.168
, you probably need to masquerade. This assumes you already have nftables
installed and it’s default rules in /etc/nftables.conf
# As root
# Add the firewall rules to masquerade
nft flush ruleset
nft add table nat
nft add chain nat postrouting { type nat hook postrouting priority 100\; }
nft add rule nat postrouting masquerade
# Persist the rules and enable the firewall
nft list ruleset >> /etc/nftables.conf
systemctl enable --now nftables.service
DNS and DHCP
If you want to provide network services such as DHCP and DNS, you can add dnsmasq
sudo apt install dnsmasq
Assuming the LAN interface is named eth0
and set to 192.168.0.1
.
vi /etc/dnsmasq.d/netboot.conf
interface=eth0
dhcp-range=192.0.1.100,192.0.1.200,12h
dhcp-option=option:router,192.168.0.1
dhcp-authoritative
systemctl enable --now nftables.service
Firewall
You may want to add some firewall rules too.
# allow SSH from the lan interface
sudo nft add rule inet filter input iifname eth0 tcp dport ssh accept
# allow DNS and DHCP from the lan interface
sudo nft add rule inet filter input iifname eth0 tcp dport domain accept
sudo nft add rule inet filter input iifname eth0 udp dport {domain, bootps}
# Change the default input policy to drop
sudo nft add chain inet filter input {type filter hook input priority 0\; policy drop\;}
You can fine-tune these a bit more with the nft example.
2 - OpenWRT
I once read a comparison of router distros and was surprised to see OpenWRT score near the top in terms of speed and reliability. It seems that in addition to being well regarded as firmware for small platforms, it’s a worthy alternative to VyOS as well. And you can run in a container as opposed to needing a full VM like OPNsense.
2.1 - OpenWRT in PVE
When running a virtual lab in a PVE cluster, it helps to also have a virtual router. If you’re keeping things lean by using LXC containers, you can put your router in a container too with OpenWRT.
The process in PVE is to:
- Prepare Networking
- Download OpenWRT
- Create The Container
- Edit The FW Init
Prepare Networking
A default install of PVE creates a single Linux Bridge, usually named vmbr0. Think of this as a virtual switch. The management interface is on that bridge, as well as any containers or guests. Most things just need one interface, but OpenWRT expects two. It is a router, after all.
In most cases, adding a VLAN is best, but there are other options. You can see and make changes in the Proxmox web GUI by changing to Server View, selecting a ProxMox Host, then going to System -> Network
.
Overlay Network
The simplest thing to do is nothing. When you create a container for OpenWRT you just add a second interface and attach it to the same bridge. The downside is that you can’t offer DHCP services because they depend on broadcasts which you’re probably already handling with another router. You can however, just select another private network range for your OpenWRT LAN interface and manually address containers that you want to use it. You’ll have two networks operating on the same LAN (this is called an overlay) but that’s fine. As long as you don’t mind doing it all manually.
VLAN
Another way is to add a Virtual LAN. You don’t need smart network equipment as PVE handles it and normal switches are happy with it. Simply edit the config for vmbr0 and enable the VLAN aware checkbox. Then add an interface to the container and specify a VLAN Tag, such as “2”. This will let you enable DHCP without disrupting the rest of your network. This isn’t a security layer per se, but PVE will filter traffic and the rest of your network will ignore any broadcasts they see that have a VLAN Tag.
Additional Bridge
You can also create a new bridge. Select new
and allow it to select the name (which should be vmbr1). Leave the rest at the defaults (all blank with autostart checked). Important If you have a cluster you must actually connect this new bridge to a network adapter in the “Bridge ports” setting. Otherwise, it won’t be able to talk beyond the host it’s currently on. If you don’t have a second NIC, then this probably won’t do what you want.
Download OpenWRT
You want just the root file system, not the full image that includes the kernel. Happily, OpenWRT makes this available. Navigate to their releases, find the most recent, and drill down to targets / x86 / 64 / rootfs.tar.gz
. It will save along the lines of “openwrt-24.10.1-x86-64-rootfs.tar.gz”.
Next, upload it to PVE with a secure copy to the root home folder like scp openwrt* root@pve01:
Create The Container
What we uploaded earlier isn’t actually a template, but it’s close enough as along as we create the container at PVE’s command line1. The key here is that we provide an archive and set the OS type to unmanaged.
pct create \
201 \
./openwrt* \
--rootfs local-lvm:0.4 \
--ostype unmanaged \
--hostname openwrt \
--arch amd64 \
--cores 2 \
--memory 256 \
--swap 0 \
--features nesting=1 \
--net0 name=eth0,bridge=vmbr0,tag=2 \
--net1 name=eth1,bridge=vmbr0
Also of note, we enable nesting so that dnsmasq will start2 and set the VLAN tag on eth0, which comes up as LAN on this image of OpenWRT. The rootfs syntax is STORAGE_ID:SIZE_IN_GiB.
Add Clients and Rules
When creating guests, make sure to change their network settings in PVE to have a VLAN tag of ‘2’ (or whatever you’re using).
In OpenWRT, add rules Network -> Firewall -> Port Forwards
. There are no WAN rules discrete from port forwarding.
Updates
You should update by downloading new firmware, not by using the package manger. In fact: “Generally speaking, the use of opkg upgrade is very highly discouraged. It should be avoided in almost all circumstances3.”
But if you must;
opkg update
opkg list-upgradable | cut -f 1 -d ' ' | xargs opkg upgrade
3 - OPNsense
10G Speeds
When you set an OPNsense system up with supported 10G cards, say the Intel X540-AT2, you can move 6 to 8 Gb a second. Though this is better than in the past, but not line speed.
# iperf between two systems routed through a dial NIC on OPNsense
[ ID] Interval Transfer Bandwidth
[ 1] 0.0000-10.0040 sec 8.04 GBytes 6.90 Gbits/sec
This is because the packet filter is getting involved. If you disable that you’ll get closer to line speeds
Firewall –> Settings –> Advanced –> Disable Firewall
[ ID] Interval Transfer Bandwidth
[ 1] 0.0000-10.0067 sec 11.0 GBytes 9.40 Gbits/sec