# System services: firewall, NAT and forwarding Install: [System services](README.md). `install.sh` and `update.sh` **never touch the firewall**. `net.ipv4.ip_forward=1` is set persistently in `/etc/sysctl.d/98-ovpmon-forward.conf`. Opening the ports (UI, VPN) is yours. NAT and packet tuning for the VPN clients are done by a separate opt-in script. ## `deploy/native/firewall.sh` (nftables) Reads the interfaces of this server and applies, for traffic **from the VPN network to every other network**: masquerade, TCP MSS clamping (path MTU tuning) and TTL tuning. Run it as root after the VPN server configuration exists (the VPN network is read from the `server` line of `server.conf`). ```sh deploy/native/firewall.sh --dry-run # show what was detected and the ruleset; change nothing deploy/native/firewall.sh # apply, ask for confirmation, load at boot deploy/native/firewall.sh status # loaded rules with counters, boot service, detected parameters deploy/native/firewall.sh remove # delete the rules, the ruleset file and the boot service ``` | Option | Meaning | |---|---| | `--vpn-net CIDR` | VPN network. Default: `server` line of the OpenVPN config, else the address of the VPN interface | | `--vpn-if NAME` | VPN interface. Default: the interface with an address in the VPN network, else the `dev` line, else `tun*` | | `--out-if A,B` | Only these interfaces (exact names) are "other networks". Default: every interface except `lo` and the VPN interface, so new uplinks (VLAN, `hytun`, a second ISP) need no rerun | | `--ttl N` / `--no-ttl` | TTL of packets leaving towards the other networks (default 64, set after the forwarding decrement) | | `--mss pmtu\|N` / `--no-mss` | TCP MSS of SYN and SYN-ACK in both directions: `pmtu` = MTU of the route of each connection (default), or a fixed value | | `--no-persist` | Running kernel only: no ruleset file, no boot service | | `--yes`, `--force` | No questions; continue on an untested OS version | What it creates: only the table `ip ovpmon` (replaced atomically with `nft -f`; other tables and the distribution's `nftables.service` are not touched), the ruleset file `/etc/ovpmon/nftables.nft` and the boot service `ovpmon-nft` (systemd oneshot / OpenRC). The `nftables` package is installed on request only (it is not part of the install packages). IPv4 only. Notes: - "Every other network" includes container bridges and tunnels on the host; use `--out-if` to limit it. - The VPN network must not overlap with the network of another interface (the script stops). - The options are not remembered: after a change of the VPN subnet in the UI run the script again with the same options. - The MSS clamp complements `tun-mtu` / `mssfix` of the server settings, it does not replace them. If another nftables table has a `policy drop`, allow the VPN traffic and ICMP "fragmentation needed" (type 3, code 4) there, otherwise path MTU discovery fails. - Old manual rules (`iptables -t nat ... MASQUERADE` for the same network) are reported: remove them when this script is the one to keep. - The filtering policy (INPUT/FORWARD allow/drop, ports, anti-leak) is not part of the script. ## Manual rules (iptables / nftables) Replace `eth0` with the uplink interface and `172.20.1.0/24` with the VPN subnet of the server settings. ```sh # iptables: services iptables -A INPUT -p tcp --dport 8088 -j ACCEPT # UI (your HTTPS port) iptables -A INPUT -p udp --dport 1194 -j ACCEPT # OpenVPN (your VPN port) # iptables: NAT and forwarding for the VPN clients iptables -t nat -A POSTROUTING -s 172.20.1.0/24 -o eth0 -j MASQUERADE iptables -A FORWARD -i tun+ -o eth0 -j ACCEPT iptables -A FORWARD -i eth0 -o tun+ -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT ``` nftables equivalent: ```sh nft add table inet ovpmon nft 'add chain inet ovpmon input { type filter hook input priority 0 ; }' nft add rule inet ovpmon input tcp dport 8088 accept nft add rule inet ovpmon input udp dport 1194 accept nft 'add chain inet ovpmon forward { type filter hook forward priority 0 ; }' nft add rule inet ovpmon forward iifname "tun*" oifname "eth0" accept nft add rule inet ovpmon forward iifname "eth0" oifname "tun*" ct state established,related accept nft 'add table ip ovpmon_nat' nft 'add chain ip ovpmon_nat postrouting { type nat hook postrouting priority 100 ; }' nft add rule ip ovpmon_nat postrouting ip saddr 172.20.1.0/24 oifname "eth0" masquerade ``` (Add the `input`/`forward` rules only when your firewall policy is `drop`; a chain with priority 0 and policy accept does not block anything on its own.) Persistence: | System | Make the rules survive a reboot | |---|---| | Alpine | `rc-service iptables save && rc-update add iptables` (rules in `/etc/iptables/rules-save`); nftables: `rc-update add nftables`, rules in `/etc/nftables.nft` | | Debian / Ubuntu | `apt-get install iptables-persistent`, then `netfilter-persistent save`; nftables: put the rules in `/etc/nftables.conf` and `systemctl enable nftables` |