#!/bin/sh
iptables -t mangle -N MARKFORCEVPN
iptables -t mangle -A MARKFORCEVPN -j MARK --set-mark 0x32
iptables -t mangle -A MARKFORCEVPN -j CONNMARK --save-mark
##if you want some logging in dmesg, uncomment next line
#iptables -t mangle -A MARKFORCEVPN -j LOG --log-prefix "[MARKFORCEVPN] "
iptables -t mangle -N MARKNOVPN
iptables -t mangle -A MARKNOVPN -j MARK --set-mark 0x64
iptables -t mangle -A MARKNOVPN -j CONNMARK --save-mark
##if you want some logging in dmesg, uncomment next line
#iptables -t mangle -A MARKNOVPN -j LOG --log-prefix "[MARKNOVPN] "
# get the mark on the packet that belongs to an existing connection (outbound from lan)
iptables -t mangle -A PREROUTING -i br0 -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
# get the mark on the packet that belongs to an existing connection (outbound from router)
iptables -t mangle -A OUTPUT -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
######## ONLY MAKE CHANGES BENEATH HERE ###########################
#below this line, add rules for traffic that you want to push to a different route.
#Should mainly be used to make exceptions on process level (uid-owner or gid-owner; only for processes on the router itself) or port / ipaddress level (also for traffic from or to other devices)
#i.e. if you have an ip rule that routes all traffic from a host via the VPN tunnel, then you can override this for a specific port, or destination IP the traffic must go directly
##if you have a configured a process on the router to run with gid vpnroute, then you can route its traffic to go via VPN by:
# iptables -t mangle -A OUTPUT -m owner --gid-owner vpnroute -m state --state NEW -j MARKFORCEVPN
##if you have port-forwarding enabled (for example 80 & 443) and use a static public IP address with static DNS name, you can route traffic of those ports to always go directly via wan interface via:
#iptables -t mangle -A PREROUTING -d <public-ip> -p tcp -m multiport --dports 80,443 -m state --state NEW -j MARKNOVPN
##or for allowing access to the OpenVPN server:
#iptables -t mangle -A PREROUTING -d <public-ip> -p tcp -m multiport --dports 12973,12974 -m state --state NEW -j MARKNOVPN
##or
#iptables -t mangle -A PREROUTING -d <public-ip> -p udp -m multiport --dports 12973,12974 -m state --state NEW -j MARKNOVPN
##if you'd want to force usenet traffic of a specific internal client to go via VPN then you could use:
#iptables -t mangle -A PREROUTING -s <ip.of.internal.client> -p tcp --dport 119 -m state --state NEW -j MARKFORCEVPN
#iptables -t mangle -A PREROUTING -s <ip.of.internal.client> -p tcp --dport 565 -m state --state NEW -j MARKFORCEVPN
## below this line put commands to prevent some traffic from falling back to regular internet connection
##if you have a configured a process on the router to run with gid vpnroute then this would prevent the process from communicating if the VPN is down. (tun21 for OpenVPN; replace with wg0 for WireGuard
#iptables -I OUTPUT 1 -m owner --gid-owner vpnroute ! -o tun21 -m state --state NEW -j REJECT
## and this would block usenet traffic from internal client if VPN is down:
#iptables -t filter -I FORWARD 1 ! -o tun21 -m state --state NEW -j REJECT