Native install: nftables script for VPN clients (masquerade, MSS clamp, TTL)

deploy/native/firewall.sh reads the server interfaces and applies, for traffic
from the VPN network to all other networks: masquerade, TCP MSS clamping
(path MTU tuning) and TTL tuning, in its own table `ip ovpmon`. Rules are
persisted in /etc/ovpmon/nftables.nft and loaded at boot by the ovpmon-nft
service (systemd and OpenRC). Opt-in: install.sh and update.sh do not call it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ayurishchevandClaude Sonnet 5.5 committed 2026-10-05 14:48:39 +03:00
1 parent 8371d889a4
commit f95a0b63e6
9 files changed
+713 -3

No files matched your search

+38 -2
View File
@@ -199,6 +199,7 @@ Create the `ovpmon` user and the directories, copy the code, build both virtuale
| Helper configuration | `/etc/ovpmon/helper.conf` | `root:root` 644 |
| TLS certificate and key | `/etc/ovpmon/tls/ovpmon.crt`, `ovpmon.key` | `root:root` (key 600) |
| Deployment state | `/etc/ovpmon/deploy.state` | `root:root` 644 |
| nftables ruleset of the VPN clients (only after `firewall.sh`) | `/etc/ovpmon/nftables.nft`, service `ovpmon-nft` | `root:root` 644 |
| Root helper | `/usr/local/sbin/ovpmon-helper` | `root:root` 755 |
| doas rules | Alpine: `/etc/doas.d/ovpmon.conf`; Debian/Ubuntu: block in `/etc/doas.conf` | `root:root` |
| OpenVPN configuration | `/etc/openvpn/server.conf` (Alpine) or `/etc/openvpn/server/server.conf`, `/etc/openvpn/crl.pem`, `/etc/openvpn/scripts/` | `root:root` |
@@ -243,9 +244,44 @@ With the unprivileged profiler the API only stages `server.conf`; **the helper c
OpenRC runs `/sbin/rc-service <unit> <action>`, systemd runs `systemctl <action> <unit>` (fixed argument list, fixed `PATH`). Unknown keys or invalid values make the helper refuse.
## Firewall, NAT and forwarding (manual)
## Firewall, NAT and forwarding
The scripts **never touch the firewall**. `net.ipv4.ip_forward=1` is set persistently in `/etc/sysctl.d/98-ovpmon-forward.conf`. You open the ports and give the VPN clients access yourself. Replace `eth0` with the uplink interface and `172.20.1.0/24` with the VPN subnet of the server settings.
`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