L2 tunnel over Wireguard
In the VPN over Wireguard I describe how to create a VPN tunnel using Wireguard. In this Howto, we see how to create a L2 tunnel over Wireguard which is a L3 only tunnel.
Configuration
For this purpose we gonna use vxlan(4), a Virtual eXtensible LAN interface which can create a L2 overlaid in a Layer 3 network.
We assume you have an existing wireguard tunnel connecting two devices wg-peer1 and wg-peer2 over their respective re1 interfaces, the configuration focus on the L2 tunnel only.
On wg-peer1:
- wg0 is 10.0.0.1
On wg-peer1 rc.conf
cloned_interfaces="wg0 vxlan0 bridge0"
create_args_vxlan0="vxlanid 1 vxlanlocal 10.0.0.1 vxlanremote 10.0.0.2"
ifconfig_vxlan0="up"
ifconfig_bridge0="addm re0 addm vxlan0"
On wg-peer2:
- wg0 is 10.0.0.2
On wg-peer2 rc.conf
cloned_interfaces="wg0 vxlan0 bridge0"
create_args_vxlan0="vxlanid 1 vxlanlocal 10.0.0.2 vxlanremote 10.0.0.1"
ifconfig_vxlan0="up"
ifconfig_bridge0="addm re0 addm vxlan0"
Reboot and connect two devices and see them communicating over the vxlan tunnel. This setup can be use to relocate multicast UDP stream via WireGuard from peer1 to peer2.
Advanced setup
Another requirement can be to access the devices in the vxlan tunnel from the home lan
On wg-peer2 assuming
- device connected on re0 has ip 10.190.0.5/24
- re1 ip is 192.168.1.17
Increase the fib number
echo "net.fibs=9" >> /boot/loader.conf
update /etc/rc.conf
ifconfig_bridge0="addm re0 addm vxlan0 DHCP fib 1"
route_route1="-net 10.190.0.0/24 -iface bridge0"
pf_enable="YES"
gateway_enable="YES"
update /etc/pf.conf
nat on bridge0 from any to 10.190.0.0/24 -> (bridge0)
On the PC set a route to access the 10.190.0.0/24 network
sudo route add 10.190.0.0/24 192.168.1.17
After reboot of wg-peer2 you are supposed to be able to ping 10.190.0.5 from the PC.
Some limitations on Rasberry pi with genet(4)
Building this setup in a raspberry pi 4, I fall in this nasty bug caused by the interaction of the bridge and the genet(4) network interfaces. In this situation genet0 replaces re0.
From time to time, bridge0 stops forwarding vxlan0 -> genet0 entirely, while genet0 -> vxlan0 keepsworking. genet0 clients can send DHCP DISCOVERs but never see the reply.
This is caused traffic coming vxlan and sent to genet0 before the device attached to the genet0 interface finish the Ethernet speed negotiation.
The bridge keeps pushing the vxlan stream into genet's if_snd queue until it hits ifqmaxlen (255). From then on every enqueue fails with ENOBUFS - and IFQ_HANDOFF() only calls if_start() when the enqueue *succeeds* Nothing else kicks TX: genet has no watchdog, the TX-completion interrupt cannot fire with an idle ring, and gen_link_task() does not restart the queue on link-up.
So the queue never drains again and bridge -> genet0 forwarding is dead until the NIC is reinitialised with no traffic pressure on it.