android: fix include-mode DNS by inverting to exclude strategy
Problem
Include mode (allow only selected apps through the VPN tunnel) is broken on Android due to a DNS routing issue.
When VpnService.Builder.addAllowedApplication() is called, the system DNS
servers are globally set to the VPN's DNS (e.g., Tailscale MagicDNS at
100.x.x.x). Non-VPN apps (not in the allowed list) still see this DNS
server address -- but their traffic bypasses the VPN tunnel, making
100.x.x.x unreachable from the physical interface. DNS resolution times
out, and non-included apps lose internet access.
This issue has been reported in tailscale/tailscale#13816 (63 thumbs-up, 19 months open) and noted as a limitation in PRs #724 and #723.
Solution
Instead of calling addAllowedApplication(), this PR inverts the logic:
collect all installed packages, subtract the user-selected ones, and
call addDisallowedApplication() for the remainder.
| User selects | Current (broken) | This PR |
|---|---|---|
| Chrome, Firefox | addAllowedApplication(chrome, ff) |
addDisallowedApplication(all 450 apps - chrome - ff) |
Android's per-app VPN exclude path (addDisallowedApplication()) has
been part of the framework since API 21. It properly routes DNS from
excluded apps to the physical network's DNS servers -- completely avoiding
the include-mode DNS bug.
What changes
A single Kotlin file: IPNService.kt (around line 212).
When allowPackages == true (include mode), instead of iterating over the
user's selected packages and calling addAllowedApplication(), we now:
- Fetch all installed packages via
packageManager.getInstalledApplications(0) - Subtract the user-selected packages
- Subtract
builtInDisallowedPackageNames(voicemail, etc.) - Call
addDisallowedApplication()for the remainder
QUERY_ALL_PACKAGESpermission is already declared in AndroidManifest.xml.
Why this works
User sees: Include mode: [Chrome] [Firefox]
|
Implementation: addDisallowedApplication(all_installed - chrome - ff)
|
Android framework: DNS routed via physical network for excluded apps
|
Result: Selected apps -> VPN tunnel -> exit node
Other apps -> physical interface -> DNS works
The key insight: the bug is in addAllowedApplication()'s DNS behavior,
not in the concept of include mode itself. By using the well-tested
exclude path, DNS functions correctly for all apps.
Built-in disallowed packages are preserved
The existing UninitializedApp.get().builtInDisallowedPackageNames list
(Voicemail apps, etc.) is still excluded in both modes -- those packages
always bypass the tunnel.
Edge case handling
| Scenario | Behavior | Expected? |
|---|---|---|
| New app installed | Not in disallowed list -> goes through VPN | Correct -- "only selected apps" semantics |
| App uninstalled | addDisallowedApplication() throws NameNotFoundException |
Caught and logged by existing code |
| All apps selected | Exclude list empty -> all through VPN | Correct -- includes everything |
| No apps selected | Exclude list = all installed -> nothing through VPN | Falls through to MATCH rule |
Testing checklist
- Include mode: selected apps route through exit node, non-selected apps have working DNS and internet access
- Exclude mode: no regression, still works as before
- Built-in disallowed packages (voicemail, etc.) still bypass tunnel
- Rapid toggling between include/exclude modes doesn't cause crashes
- App list refresh handling (install/uninstall while VPN is active)
Related
- Updates tailscale/tailscale#13816 (Feature Request: allowlist mode)
- Supersedes the DNS limitation noted in PRs #724 and #723