Allow traffic only through tun0 via wlan0, ssh, and localhost in and out
Hi all, I'm trying to have my rpi5 running raspberry OS communicate with the Internet only through the tun0 interface (vpn). For this I wanted to create a ufw ruleset. Unfortunately, I've hit a roadblock and I can't figure out where I'm going wrong.
Can you help me discover why this ruleset doesn't allow Internet communication over tun0? When I disable ufw I can access the Internet.
The VPN connection is already established, so it should keep working, right?
I hope you can help me out!
This is the script with the ruleset:
sudo ufw reset
Set default policies
sudo ufw default deny incoming
sudo ufw default deny outgoing
Allow SSH access
sudo ufw allow ssh
Allow local network traffic
sudo ufw allow from 192.168.0.0/16
sudo ufw allow out to 192.168.0.0/16
Allow traffic through VPN tunnel
sudo ufw allow in on tun0
sudo ufw allow out on tun0
Add routing between interfaces (I read its necessary, not sure why?)
sudo ufw route allow in on tun0 out on wlan0
sudo ufw route allow in on wlan0 out on tun0
sudo ufw enable
sykaster
Unknown parent • • •sykaster
Unknown parent • • •HelloRoot
in reply to sykaster • • •mnmalst
in reply to sykaster • • •You have to do the last line for all your VPN server ips or the initial DNS request will not go through. If you connect through udp.
sykaster
in reply to mnmalst • • •mnmalst
in reply to sykaster • • •Just to be clear this is a killswitch, that's what you want right? So that it's only possible to connect through the VPN (tun0). And if the VPN goes down your internet gets "killed" so you don't leak your IP.
In that case you want to start ufw when you system starts, so you would need to whitelist your VPN but if your VPN is already connected it should work without whitelisting the IP I guess but never tried it since that's not recommended.
Joe
in reply to sykaster • • •wg-quick takes a different approach, using an ip rule to send all traffic (except its own) to a different routing table with only the wireguard interface. I topped it up with iptables rules to block everything except DNS and the wireguard udp port on the main interface. I also disabled ipv6 on the main interface, to avoid any non-RFC1918 addresses appearing in the (in my case) container at all.
edit: you can also do ip rule matching based on uid, such that you could force all non-root users to use your custom route table.
oshu
in reply to sykaster • • •If your concern is ensuring a killswitch type vpn setup, I do that but in a different and simple way.
I have a GLinet microrouter configured to join the vpn and active killswitch mode. This is 2 clicks in the menu. I connect it to my network via its wan port.
Everything I want behind the VPN gets connected to the microrouter lan port and job done.
sykaster
in reply to oshu • • •oshu
in reply to sykaster • • •Its simple and I can easily put a laptop or phone or whatever behind the microrouter and have confidence its only using the vpn.
When I travel I take a second microrouter with me to connect to the hotel wifi. All my devices are set to use the microrouter wifi so they never touch the hotel network, only the vpn. Easy, private, and avoids any filtering the hotek is doing.
sykaster
in reply to oshu • • •oshu
in reply to sykaster • • •