I just went through setting this up myself, and I wanted to use FreeBSD's existing network scripts and not have to install any packages or download any 3rd party scripts as they mostly don't have any support for multiple fibs and the existing netif scripts do.
Add
net.fibs=2 to
/boot/loader.conf
In
/etc/rc.conf add lines like below, using IP's provided by your VPN provider
Code:
cloned_interfaces="wg0"
ifconfig_wg0="inet 10.1.2.3/32"
ifconfig_wg0_ipv6="inet6 fd00:10:10::1234/128"
static_routes="vpn:wg0"
route_vpn="-inet default -interface wg0 -fib 1"
Create
/usr/local/etc/wireguard/wg0.conf with your wireguard settings according to
wg(8)
Code:
[Interface]
PrivateKey = private_key
[Peer]
PublicKey = public_key
PresharedKey = preshared_key
AllowedIPs = 0.0.0.0/1, 128.0.0.0/1, ::/1, 8000::/1
Endpoint = vpn.server:port
PersistentKeepalive = 25
Create the file
/etc/start_if.wg0 with the contents:
Code:
/usr/bin/wg setconf $1 /usr/local/etc/wireguard/$1.conf
If your primary interface is configured using DHCP then edit the config in
/etc/rc.conf to change it to
SYNCDHCP so that the interface is up before the wireguard config is applied.
Code:
ifconfig_genet0="SYNCDHCP inet6 accept_rtadv"
And now reboot and test the results with:
Code:
root@rpi4:~ # setfib 0 drill -4 -Q whoami.cloudflare @1.1.1.1 TXT CH
"<real public ip>"
root@rpi4:~ # setfib 1 drill -4 -Q whoami.cloudflare @1.1.1.1 TXT CH
"<vpn public ip>"
Now you can attach jails or run daemons or programs under FIB 1 and their traffic will go through the VPN, you can also assign FIB 1 to a interface and any traffic coming in on that interface will be forwarded using the FIB1 routing table.
/etc/rc.conf
Code:
ifconfig_ue0="192.168.194.1 netmask 255.255.255.0 fib 1"
/etc/pf.conf
Code:
nat on wg0 from 192.168.194.0/24 to any -> ( wg0 )
match in from 192.168.194.0/24 to any rtable 1