Split tunneling is essential for performance and selective privacy, but it increases the risk of accidental leaks when the VPN drops or an app bypasses routing. This guide shows how to build a pragmatic, production-ready leak‑resistant split‑tunnel stack on Linux (desktop or router) by combining three layers: robust routing with WireGuard, kernel‑level monitoring with eBPF (as a watchdog), and atomic policy enforcement with nftables and systemd hooks. The approach trades the complexity of in‑kernel policy rewriting for a safe, observable enforcement loop that works on Linux kernels common in 2026.
Why use eBPF here?
eBPF is not strictly required to block leaks, but it provides two valuable capabilities:
- Low-overhead, kernel‑level observability of socket connect events and interface associations so you can detect potential bypass attempts in real time.
- A reliable trigger source for an enforcement service: instead of guessing when a VPN fails, an eBPF monitor signals the policy engine to apply atomic nftables rules (or revert split tunnels) immediately.
This guide uses eBPF primarily as a monitor + watchdog. That reduces the amount of custom in‑kernel code you must maintain while delivering fast, reliable reaction to leaks and bad state.
What you'll build
- WireGuard-based VPN configuration that uses IP rule / routing table segregation for split tunneling.
- nftables baseline policy that defaults to "deny outbound unless explicitly allowed" for the host, with exceptions applied to processes and interfaces.
- An eBPF monitor (using libbpf-tools tcpconnect or bcc tcpconnect) that alerts when a process creates a non‑VPN outbound connection.
- A systemd service that uses the eBPF monitor to apply emergency nftables rules (atomic replacement) and notify the user or orchestrator.
Prerequisites
- Linux kernel 5.10+ (kernel eBPF tooling improved in 5.x and later). For best compatibility use a distro kernel from 5.15+ up to 6.x/7.x (2026 kernels are common on rolling distros).
- WireGuard installed (kernel module or userspace, wg-quick recommended for simple setups).
- nftables (nft) as your firewall backend — we use atomic rule set replaces.
- libbpf-tools or bcc utilities installed for tcpconnect monitoring. Install via your package manager (Ubuntu 24.04+, Debian 12/13, Fedora 38+ have libbpf-tools packages; bcc is an alternative on older distros).
- systemd for service orchestration (systemd‑run/systemctl) or an equivalent init system.
- root access to configure networking and load kernels modules.
Design the traffic policy (decide what is split)
Before you touch eBPF or nftables, map the traffic that must go via the VPN and the traffic allowed to go direct:
- VPN‑bound: Browsers and apps that require private exit IPs, remote admin tools, SSH sessions to private servers, etc.
- Direct (non‑VPN): Local LAN services, local printers, some cloud APIs, or performance‑sensitive streams you accept to keep out of the VPN.
Assign the policy by process, UID/GID, or destination subnets. This guide uses UID/GID and destination subnets together — it’s simpler and robust for desktop environments and headless routers.
Step 1 — WireGuard and routing baseline
Set up WireGuard in the usual way. Important choices for split tunneling:
- Create a dedicated routing table for VPN traffic, for example table 51820.
- Use ip rule to route packets marked for VPN into that table. Marking is done either by nftables or by source-based rules (for example, running VPN‑only apps under a specific UID).
- Keep your system default route intact for direct traffic.
Example high-level sequence (run with root privileges):
- Create table: ip route add default dev wg0 table 51820
- Add rule for marked packets: ip rule add fwmark 0x1 table 51820
- When wg0 comes up, add the peer endpoint(s) and routes via wg0. Put these commands into wg-quick PostUp so they are applied atomically when the tunnel starts.
Marking outflow via nftables is atomic and preferred — we cover that next.
Step 2 — nftables baseline: default deny with explicit passthrough
Use nftables to create a simple outbound policy that denies any non‑loopback, non‑VPN traffic except explicitly allowed flows. Structure your rules so you can atomically replace the entire table on detection of a leak.
Key nftables patterns:
- Create a “policy” table with a chain for outbound IPv4 (and one for IPv6) that checks packet marks and interface.
- Apply marks for packets that should be routed to VPN (set mark 0x1 for those sockets/processes).
- Default policy: drop outbound packets that are not marked or not going out the VPN interface.
Placing allowed direct-exit exceptions (LAN subnet, management IPs) is important so legitimate direct traffic still flows. Store the nftables rules as a single file and use nft -f and nft --check to validate before loading.
Step 3 — eBPF monitoring: detect non‑VPN outbound connects
Instead of trying to embed complex enforcement into eBPF, use it as an accurate, low-latency detector of problematic connects. The libbpf-tools suite includes a tcpconnect example that records TCP connects and their socket's egress interface or route.
Install libbpf-tools (or bcc): on Ubuntu/Debian, apt install libbpf-tools or apt install bpfcc-tools. On Fedora use dnf install libbpf-tools. If your distro lacks packages, build libbpf-tools from its repo — it's common in 2026 and well maintained.
Run tcpconnect with a filter that excludes connections originating from the VPN interface. The tool prints PID, UID, destination, and the outgoing interface or route. Example (conceptual):
- sudo tcpconnect -t -U -i | grep -v wg0
When tcpconnect reports a connection that would bypass wg0 (or is unmarked), the watchdog should trigger.
Step 4 — Watchdog service: from detection to atomic enforcement
Implement a small systemd service (vpn-watchdog.service) that:
- Starts the eBPF tcpconnect monitor at boot and runs it in a supervised loop.
- On detection of a disallowed outbound connect, runs an atomic nftables replace to switch to an “emergency” policy that blocks all non‑VPN egress and optionally kills or moves misbehaving processes into a quarantine cgroup.
- Notifies the user via desktop notification, syslog, or an orchestration webhook so you can react or roll the VPN back up.
Design notes:
- Make nftables replacement atomic: keep a validated nft file (policy-normal.nft) and an emergency file (policy-lockdown.nft). Use nft -f policy-lockdown.nft to replace the table so the switch is consistent.
- Avoid destructive actions by default. The watchdog should by default block and notify, and only if explicitly configured may it kill processes or revert routing.
- Log all events with PID, UID, command line and destination IP for later audit.
Step 5 — Integration points for reliability
To make the system robust:
- Wire the watcher to WireGuard PostUp/PostDown so startup and teardown reapply the correct nftables baseline.
- Run critical apps under dedicated UIDs or systemd slices (systemd-run --uid) to allow per‑UID policies rather than per‑package rules. UID-based policies are straightforward to mark in nftables and easier to enforce.
- Keep a fallback: a simple ip rule that forces all traffic through a local blackhole routing table if wg0 goes down, applied by wg‑quick PostDown. That prevents transient races where monitoring might not yet detect state changes.
Step 6 — Testing and validation
- Simulate VPN drop: bring down wg0 and confirm nftables emergency rules block non‑VPN egress.
- Test leaking apps: start a browser configured for the direct interface and verify the monitor detects and triggers lockdown.
- Check DNS: ensure DNS queries are also enforced. If you use systemd-resolved or DoH, monitor the process that performs DNS requests and include it in the VPN‑bound set or allow via secure exceptions.
- Audit logs for false positives: tune the tcpconnect filter so legitimate local connections (e.g., to 127.0.0.1) do not trigger lockdowns.
Edge cases and limitations
- Non‑TCP traffic: tcpconnect focuses on TCP. For UDP (e.g., DNS over HTTPS using UDP at the socket layer rarely happens, but DoT/DoH over TCP/TLS should be captured), you may need packet‑level monitoring (libbpf-tools' xdp or tc samples) or rely on process‑level policies.
- Raw sockets, packet injection, or kernel bypass: root processes can bypass userland policies. Treat watchdog as a defense-in-depth measure and constrain the set of privileged users.
- Kernel tool compatibility: older kernels or distributions without libbpf-tools may require bcc; adjust scripts accordingly. Test on your target platform (Ubuntu LTS, Debian stable, or OpenWrt variants) before deployment.
- Performance: eBPF monitoring is lightweight; however, noisy environments with thousands of connects per second require careful sampling and logging to avoid resource exhaustion.
Operational tips
- Keep your nftables policy files in a git repo and sign them. Treat the policy as configuration-as-code so you can roll back changes safely.
- Expose a metrics endpoint (Prometheus or simple JSON) from your watchdog so you can alert on repeated lockdowns — repeated triggers indicate misconfiguration or app behavior changes.
- Include a safe “recovery” key sequence accessible from the console in case the watchdog misfires in remote headless deployments (a local console key press or a timed auto‑recovery that reverts to baseline after operator confirmation).
Real-world example scenarios
Two scenarios where this approach helps:
- Work laptop using split tunneling: developer tools and corporate traffic must use a corporate VPN, but video conferencing can go direct. The watchdog detects non‑VPN connections to corporate IP ranges and immediately locks down until the user re‑establishes the VPN.
- Home router with selective exit nodes: certain IoT devices should never use the VPN. Running the watchdog on the router prevents accidental firmware updates from leaking through a temporary VPN outage.
Wrap up
Combining eBPF monitoring with nftables and WireGuard gives you a powerful, pragmatic defense-in-depth approach to leak‑resistant split tunneling. You get high‑fidelity detection from the kernel and safe, atomic enforcement via nftables without needing to maintain large in‑kernel policy code. The pattern scales from single‑user desktops to routers and small gateway appliances.
In 2026, with mature libbpf tooling and widespread WireGuard adoption, this approach balances maintainability and security — suitable for VPN enthusiasts and operators who want deterministic, testable protection against split‑tunnel leaks.
Further reading and tooling
- libbpf-tools tcpconnect and other connection tracing tools (install from your distro or GitHub libbpf‑tools repo)
- WireGuard documentation and wg‑quick PostUp/PostDown hooks
- nftables user guide and examples for atomic rule replacement
- Systemd service units and slices for process isolation